mem.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* crypto/mem.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <openssl/crypto.h>
  61. #include "cryptlib.h"
  62. static int allow_customize = 1; /* we provide flexible functions for */
  63. static int allow_customize_debug = 1;/* exchanging memory-related functions at
  64. * run-time, but this must be done
  65. * before any blocks are actually
  66. * allocated; or we'll run into huge
  67. * problems when malloc/free pairs
  68. * don't match etc. */
  69. /* may be changed as long as `allow_customize' is set */
  70. static void *(*malloc_locked_func)(size_t) = malloc;
  71. static void (*free_locked_func)(void *) = free;
  72. static void *(*malloc_func)(size_t) = malloc;
  73. static void *(*realloc_func)(void *, size_t)= realloc;
  74. static void (*free_func)(void *) = free;
  75. /* may be changed as long as `allow_customize_debug' is set */
  76. /* XXX use correct function pointer types */
  77. #ifdef CRYPTO_MDEBUG
  78. /* use default functions from mem_dbg.c */
  79. static void (*malloc_debug_func)(void *,int,const char *,int,int)
  80. = CRYPTO_dbg_malloc;
  81. static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
  82. = CRYPTO_dbg_realloc;
  83. static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free;
  84. static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options;
  85. static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options;
  86. #else
  87. /* applications can use CRYPTO_malloc_debug_init() to select above case
  88. * at run-time */
  89. static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL;
  90. static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
  91. = NULL;
  92. static void (*free_debug_func)(void *,int) = NULL;
  93. static void (*set_debug_options_func)(long) = NULL;
  94. static long (*get_debug_options_func)(void) = NULL;
  95. #endif
  96. int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
  97. void (*f)(void *))
  98. {
  99. if (!allow_customize)
  100. return 0;
  101. if ((m == NULL) || (r == NULL) || (f == NULL))
  102. return 0;
  103. malloc_func=m;
  104. realloc_func=r;
  105. free_func=f;
  106. malloc_locked_func=m;
  107. free_locked_func=f;
  108. return 1;
  109. }
  110. int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
  111. {
  112. if (!allow_customize)
  113. return 0;
  114. if ((m == NULL) || (f == NULL))
  115. return 0;
  116. malloc_locked_func=m;
  117. free_locked_func=f;
  118. return 1;
  119. }
  120. int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
  121. void (*r)(void *,void *,int,const char *,int,int),
  122. void (*f)(void *,int),
  123. void (*so)(long),
  124. long (*go)(void))
  125. {
  126. if (!allow_customize_debug)
  127. return 0;
  128. malloc_debug_func=m;
  129. realloc_debug_func=r;
  130. free_debug_func=f;
  131. set_debug_options_func=so;
  132. get_debug_options_func=go;
  133. return 1;
  134. }
  135. void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
  136. void (**f)(void *))
  137. {
  138. if (m != NULL) *m=malloc_func;
  139. if (r != NULL) *r=realloc_func;
  140. if (f != NULL) *f=free_func;
  141. }
  142. void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
  143. {
  144. if (m != NULL) *m=malloc_locked_func;
  145. if (f != NULL) *f=free_locked_func;
  146. }
  147. void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int),
  148. void (**r)(void *,void *,int,const char *,int,int),
  149. void (**f)(void *,int),
  150. void (**so)(long),
  151. long (**go)(void))
  152. {
  153. if (m != NULL) *m=malloc_debug_func;
  154. if (r != NULL) *r=realloc_debug_func;
  155. if (f != NULL) *f=free_debug_func;
  156. if (so != NULL) *so=set_debug_options_func;
  157. if (go != NULL) *go=get_debug_options_func;
  158. }
  159. void *CRYPTO_malloc_locked(int num, const char *file, int line)
  160. {
  161. void *ret = NULL;
  162. extern unsigned char cleanse_ctr;
  163. if (num <= 0) return NULL;
  164. allow_customize = 0;
  165. if (malloc_debug_func != NULL)
  166. {
  167. allow_customize_debug = 0;
  168. malloc_debug_func(NULL, num, file, line, 0);
  169. }
  170. ret = malloc_locked_func(num);
  171. #ifdef LEVITTE_DEBUG
  172. fprintf(stderr, "LEVITTE_DEBUG: > 0x%p (%d)\n", ret, num);
  173. #endif
  174. if (malloc_debug_func != NULL)
  175. malloc_debug_func(ret, num, file, line, 1);
  176. /* Create a dependency on the value of 'cleanse_ctr' so our memory
  177. * sanitisation function can't be optimised out. NB: We only do
  178. * this for >2Kb so the overhead doesn't bother us. */
  179. if(ret && (num > 2048))
  180. ((unsigned char *)ret)[0] = cleanse_ctr;
  181. return ret;
  182. }
  183. void CRYPTO_free_locked(void *str)
  184. {
  185. if (free_debug_func != NULL)
  186. free_debug_func(str, 0);
  187. #ifdef LEVITTE_DEBUG
  188. fprintf(stderr, "LEVITTE_DEBUG: < 0x%p\n", str);
  189. #endif
  190. free_locked_func(str);
  191. if (free_debug_func != NULL)
  192. free_debug_func(NULL, 1);
  193. }
  194. void *CRYPTO_malloc(int num, const char *file, int line)
  195. {
  196. void *ret = NULL;
  197. extern unsigned char cleanse_ctr;
  198. if (num <= 0) return NULL;
  199. allow_customize = 0;
  200. if (malloc_debug_func != NULL)
  201. {
  202. allow_customize_debug = 0;
  203. malloc_debug_func(NULL, num, file, line, 0);
  204. }
  205. ret = malloc_func(num);
  206. #ifdef LEVITTE_DEBUG
  207. fprintf(stderr, "LEVITTE_DEBUG: > 0x%p (%d)\n", ret, num);
  208. #endif
  209. if (malloc_debug_func != NULL)
  210. malloc_debug_func(ret, num, file, line, 1);
  211. /* Create a dependency on the value of 'cleanse_ctr' so our memory
  212. * sanitisation function can't be optimised out. NB: We only do
  213. * this for >2Kb so the overhead doesn't bother us. */
  214. if(ret && (num > 2048))
  215. ((unsigned char *)ret)[0] = cleanse_ctr;
  216. return ret;
  217. }
  218. void *CRYPTO_realloc(void *str, int num, const char *file, int line)
  219. {
  220. void *ret = NULL;
  221. if (str == NULL)
  222. return CRYPTO_malloc(num, file, line);
  223. if (num <= 0) return NULL;
  224. if (realloc_debug_func != NULL)
  225. realloc_debug_func(str, NULL, num, file, line, 0);
  226. ret = realloc_func(str,num);
  227. #ifdef LEVITTE_DEBUG
  228. fprintf(stderr, "LEVITTE_DEBUG: | 0x%p -> 0x%p (%d)\n", str, ret, num);
  229. #endif
  230. if (realloc_debug_func != NULL)
  231. realloc_debug_func(str, ret, num, file, line, 1);
  232. return ret;
  233. }
  234. void CRYPTO_free(void *str)
  235. {
  236. if (free_debug_func != NULL)
  237. free_debug_func(str, 0);
  238. #ifdef LEVITTE_DEBUG
  239. fprintf(stderr, "LEVITTE_DEBUG: < 0x%p\n", str);
  240. #endif
  241. free_func(str);
  242. if (free_debug_func != NULL)
  243. free_debug_func(NULL, 1);
  244. }
  245. void *CRYPTO_remalloc(void *a, int num, const char *file, int line)
  246. {
  247. if (a != NULL) OPENSSL_free(a);
  248. a=(char *)OPENSSL_malloc(num);
  249. return(a);
  250. }
  251. void CRYPTO_set_mem_debug_options(long bits)
  252. {
  253. if (set_debug_options_func != NULL)
  254. set_debug_options_func(bits);
  255. }
  256. long CRYPTO_get_mem_debug_options(void)
  257. {
  258. if (get_debug_options_func != NULL)
  259. return get_debug_options_func();
  260. return 0;
  261. }