]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_ssl/Security/sslMemory.c
2 * Copyright (c) 1999-2001,2005-2007,2010-2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * sslMemory.c - Memory allocator implementation
28 /* THIS FILE CONTAINS KERNEL CODE */
30 #include "sslMemory.h"
33 #include <string.h> /* memset */
34 #include <AssertMacros.h>
37 // MARK: Basic low-level malloc/free
40 * For now, all allocs/frees go thru here.
46 #include <sys/malloc.h>
47 #include <IOKit/IOLib.h>
48 #include <libkern/libkern.h>
50 /* Define this for debugging sslMalloc and sslFree */
54 sslMalloc(size_t length
)
62 p
= _MALLOC(length
, M_TEMP
, M_WAITOK
);
69 *(uint32_t *)p
=(uint32_t)length
-8;
70 printf("sslMalloc @%p of 0x%08lx bytes\n", p
, length
-8);
71 *(uint32_t *)(p
+length
-4)=0xdeadbeed;
85 uint32_t len
=*(uint32_t *)p
;
86 uint32_t marker
=*(uint32_t *)(p
+4+len
);
87 printf("sslFree @%p len=0x%08x\n", p
, len
);
88 if(marker
!=0xdeadbeef)
89 panic("Buffer overflow in SSL!\n");
97 sslRealloc(void *oldPtr
, size_t oldLen
, size_t newLen
)
99 /* _REALLOC is in sys/malloc.h but is only exported in debug kernel */
100 /* return _REALLOC(oldPtr, newLen, M_TEMP, M_NOWAIT); */
105 newPtr
=sslMalloc(newLen
);
107 memcpy(newPtr
, oldPtr
, oldLen
);
121 sslMalloc(size_t length
)
123 return malloc(length
);
135 sslRealloc(void *oldPtr
, size_t oldLen
, size_t newLen
)
137 return realloc(oldPtr
, newLen
);
143 // MARK: SSLBuffer-level alloc/free
149 buf
->data
= (uint8_t *)sslMalloc(length
);
150 if(buf
->data
== NULL
) {
151 sslErrorLog("SSLAllocBuffer: NULL buf!\n");
156 buf
->length
= length
;
161 SSLFreeBuffer(SSLBuffer
*buf
)
164 sslErrorLog("SSLFreeBuffer: NULL buf!\n");
175 SSLReallocBuffer(SSLBuffer
*buf
, size_t newSize
)
177 buf
->data
= (uint8_t *)sslRealloc(buf
->data
, buf
->length
, newSize
);
178 if(buf
->data
== NULL
) {
179 sslErrorLog("SSLReallocBuffer: NULL buf!\n");
184 buf
->length
= newSize
;
189 // MARK: Convenience routines
191 uint8_t *sslAllocCopy(
197 dst
= (uint8_t *)sslMalloc(len
);
201 memmove(dst
, src
, len
);
205 int SSLAllocCopyBuffer(
206 const SSLBuffer
*src
,
207 SSLBuffer
**dst
) // buffer and data mallocd and returned
211 SSLBuffer
*rtn
= (SSLBuffer
*)sslMalloc(sizeof(SSLBuffer
));
213 sslErrorLog("SSLAllocCopyBuffer: NULL buf!\n");
217 serr
= SSLCopyBuffer(src
, rtn
);
227 int SSLCopyBufferFromData(
230 SSLBuffer
*dst
) // data mallocd and returned
232 dst
->data
= sslAllocCopy((const uint8_t *)src
, len
);
233 if(dst
->data
== NULL
) {
234 sslErrorLog("SSLCopyBufferFromData: NULL buf!\n");
243 const SSLBuffer
*src
,
244 SSLBuffer
*dst
) // data mallocd and returned
246 return SSLCopyBufferFromData(src
->data
, src
->length
, dst
);