VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.1/crypto/mem_sec.c@ 94096

最後變更 在這個檔案從94096是 94082,由 vboxsync 提交於 3 年 前

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

檔案大小: 19.1 KB
 
1/*
2 * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11/*
12 * This file is in two halves. The first half implements the public API
13 * to be used by external consumers, and to be used by OpenSSL to store
14 * data in a "secure arena." The second half implements the secure arena.
15 * For details on that implementation, see below (look for uppercase
16 * "SECURE HEAP IMPLEMENTATION").
17 */
18#include "e_os.h"
19#include <openssl/crypto.h>
20
21#include <string.h>
22
23#ifndef OPENSSL_NO_SECURE_MEMORY
24# if defined(_WIN32)
25# include <windows.h>
26# endif
27# include <stdlib.h>
28# include <assert.h>
29# if defined(OPENSSL_SYS_UNIX)
30# include <unistd.h>
31# endif
32# include <sys/types.h>
33# if defined(OPENSSL_SYS_UNIX)
34# include <sys/mman.h>
35# if defined(__FreeBSD__)
36# define MADV_DONTDUMP MADV_NOCORE
37# endif
38# if !defined(MAP_CONCEAL)
39# define MAP_CONCEAL 0
40# endif
41# endif
42# if defined(OPENSSL_SYS_LINUX)
43# include <sys/syscall.h>
44# if defined(SYS_mlock2)
45# include <linux/mman.h>
46# include <errno.h>
47# endif
48# include <sys/param.h>
49# endif
50# include <sys/stat.h>
51# include <fcntl.h>
52#elif defined(VBOX)
53# include <iprt/memsafer.h>
54#endif
55
56#define CLEAR(p, s) OPENSSL_cleanse(p, s)
57#ifndef PAGE_SIZE
58# define PAGE_SIZE 4096
59#endif
60#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
61# define MAP_ANON MAP_ANONYMOUS
62#endif
63
64#ifndef OPENSSL_NO_SECURE_MEMORY
65static size_t secure_mem_used;
66
67static int secure_mem_initialized;
68
69static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
70
71/*
72 * These are the functions that must be implemented by a secure heap (sh).
73 */
74static int sh_init(size_t size, size_t minsize);
75static void *sh_malloc(size_t size);
76static void sh_free(void *ptr);
77static void sh_done(void);
78static size_t sh_actual_size(char *ptr);
79static int sh_allocated(const char *ptr);
80#endif
81
82int CRYPTO_secure_malloc_init(size_t size, size_t minsize)
83{
84#ifndef OPENSSL_NO_SECURE_MEMORY
85 int ret = 0;
86
87 if (!secure_mem_initialized) {
88 sec_malloc_lock = CRYPTO_THREAD_lock_new();
89 if (sec_malloc_lock == NULL)
90 return 0;
91 if ((ret = sh_init(size, minsize)) != 0) {
92 secure_mem_initialized = 1;
93 } else {
94 CRYPTO_THREAD_lock_free(sec_malloc_lock);
95 sec_malloc_lock = NULL;
96 }
97 }
98
99 return ret;
100#else
101 return 0;
102#endif /* OPENSSL_NO_SECURE_MEMORY */
103}
104
105int CRYPTO_secure_malloc_done(void)
106{
107#ifndef OPENSSL_NO_SECURE_MEMORY
108 if (secure_mem_used == 0) {
109 sh_done();
110 secure_mem_initialized = 0;
111 CRYPTO_THREAD_lock_free(sec_malloc_lock);
112 sec_malloc_lock = NULL;
113 return 1;
114 }
115#endif /* OPENSSL_NO_SECURE_MEMORY */
116 return 0;
117}
118
119int CRYPTO_secure_malloc_initialized(void)
120{
121#ifndef OPENSSL_NO_SECURE_MEMORY
122 return secure_mem_initialized;
123#else
124 return 0;
125#endif /* OPENSSL_NO_SECURE_MEMORY */
126}
127
128void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
129{
130#ifndef OPENSSL_NO_SECURE_MEMORY
131 void *ret;
132 size_t actual_size;
133
134 if (!secure_mem_initialized) {
135 return CRYPTO_malloc(num, file, line);
136 }
137 if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
138 return NULL;
139 ret = sh_malloc(num);
140 actual_size = ret ? sh_actual_size(ret) : 0;
141 secure_mem_used += actual_size;
142 CRYPTO_THREAD_unlock(sec_malloc_lock);
143 return ret;
144#elif defined(VBOX)
145 RT_NOREF(line);
146 return RTMemSaferAllocZTag(num, file);
147#else
148 return CRYPTO_malloc(num, file, line);
149#endif /* OPENSSL_NO_SECURE_MEMORY */
150}
151
152void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
153{
154#ifndef OPENSSL_NO_SECURE_MEMORY
155 if (secure_mem_initialized)
156 /* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
157 return CRYPTO_secure_malloc(num, file, line);
158#endif
159#if !defined(OPENSSL_SECURE_MEMORY) && defined(VBOX)
160 RT_NOREF(line);
161 return RTMemSaferAllocZTag(num, file);
162#else
163 return CRYPTO_zalloc(num, file, line);
164#endif
165}
166
167void CRYPTO_secure_free(void *ptr, const char *file, int line)
168{
169#ifndef OPENSSL_NO_SECURE_MEMORY
170 size_t actual_size;
171
172 if (ptr == NULL)
173 return;
174 if (!CRYPTO_secure_allocated(ptr)) {
175 CRYPTO_free(ptr, file, line);
176 return;
177 }
178 if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
179 return;
180 actual_size = sh_actual_size(ptr);
181 CLEAR(ptr, actual_size);
182 secure_mem_used -= actual_size;
183 sh_free(ptr);
184 CRYPTO_THREAD_unlock(sec_malloc_lock);
185#elif defined(VBOX)
186 RT_NOREF(line);
187 RTMemSaferFree(ptr, 0);
188#else
189 CRYPTO_free(ptr, file, line);
190#endif /* OPENSSL_NO_SECURE_MEMORY */
191}
192
193void CRYPTO_secure_clear_free(void *ptr, size_t num,
194 const char *file, int line)
195{
196#ifndef OPENSSL_NO_SECURE_MEMORY
197 size_t actual_size;
198
199 if (ptr == NULL)
200 return;
201 if (!CRYPTO_secure_allocated(ptr)) {
202 OPENSSL_cleanse(ptr, num);
203 CRYPTO_free(ptr, file, line);
204 return;
205 }
206 if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
207 return;
208 actual_size = sh_actual_size(ptr);
209 CLEAR(ptr, actual_size);
210 secure_mem_used -= actual_size;
211 sh_free(ptr);
212 CRYPTO_THREAD_unlock(sec_malloc_lock);
213#elif defined(VBOX)
214 RT_NOREF(line);
215 RTMemSaferFree(ptr, 0);
216#else
217 if (ptr == NULL)
218 return;
219 OPENSSL_cleanse(ptr, num);
220 CRYPTO_free(ptr, file, line);
221#endif /* OPENSSL_NO_SECURE_MEMORY */
222}
223
224int CRYPTO_secure_allocated(const void *ptr)
225{
226#ifndef OPENSSL_NO_SECURE_MEMORY
227 if (!secure_mem_initialized)
228 return 0;
229 /*
230 * Only read accesses to the arena take place in sh_allocated() and this
231 * is only changed by the sh_init() and sh_done() calls which are not
232 * locked. Hence, it is safe to make this check without a lock too.
233 */
234 return sh_allocated(ptr);
235#elif defined(VBOX)
236 return RTMemSaferGetSize(ptr) > 0;
237#else
238 return 0;
239#endif /* OPENSSL_NO_SECURE_MEMORY */
240}
241
242size_t CRYPTO_secure_used(void)
243{
244#ifndef OPENSSL_NO_SECURE_MEMORY
245 return secure_mem_used;
246#else
247 return 0;
248#endif /* OPENSSL_NO_SECURE_MEMORY */
249}
250
251size_t CRYPTO_secure_actual_size(void *ptr)
252{
253#ifndef OPENSSL_NO_SECURE_MEMORY
254 size_t actual_size;
255
256 if (!CRYPTO_THREAD_write_lock(sec_malloc_lock))
257 return 0;
258 actual_size = sh_actual_size(ptr);
259 CRYPTO_THREAD_unlock(sec_malloc_lock);
260 return actual_size;
261#elif defined(VBOX)
262 return RTMemSaferGetSize(ptr);
263#else
264 return 0;
265#endif
266}
267
268/*
269 * SECURE HEAP IMPLEMENTATION
270 */
271#ifndef OPENSSL_NO_SECURE_MEMORY
272
273
274/*
275 * The implementation provided here uses a fixed-sized mmap() heap,
276 * which is locked into memory, not written to core files, and protected
277 * on either side by an unmapped page, which will catch pointer overruns
278 * (or underruns) and an attempt to read data out of the secure heap.
279 * Free'd memory is zero'd or otherwise cleansed.
280 *
281 * This is a pretty standard buddy allocator. We keep areas in a multiple
282 * of "sh.minsize" units. The freelist and bitmaps are kept separately,
283 * so all (and only) data is kept in the mmap'd heap.
284 *
285 * This code assumes eight-bit bytes. The numbers 3 and 7 are all over the
286 * place.
287 */
288
289#define ONE ((size_t)1)
290
291# define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
292# define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
293# define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
294
295#define WITHIN_ARENA(p) \
296 ((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
297#define WITHIN_FREELIST(p) \
298 ((char*)(p) >= (char*)sh.freelist && (char*)(p) < (char*)&sh.freelist[sh.freelist_size])
299
300
301typedef struct sh_list_st
302{
303 struct sh_list_st *next;
304 struct sh_list_st **p_next;
305} SH_LIST;
306
307typedef struct sh_st
308{
309 char* map_result;
310 size_t map_size;
311 char *arena;
312 size_t arena_size;
313 char **freelist;
314 ossl_ssize_t freelist_size;
315 size_t minsize;
316 unsigned char *bittable;
317 unsigned char *bitmalloc;
318 size_t bittable_size; /* size in bits */
319} SH;
320
321static SH sh;
322
323static size_t sh_getlist(char *ptr)
324{
325 ossl_ssize_t list = sh.freelist_size - 1;
326 size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
327
328 for (; bit; bit >>= 1, list--) {
329 if (TESTBIT(sh.bittable, bit))
330 break;
331 OPENSSL_assert((bit & 1) == 0);
332 }
333
334 return list;
335}
336
337
338static int sh_testbit(char *ptr, int list, unsigned char *table)
339{
340 size_t bit;
341
342 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
343 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
344 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
345 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
346 return TESTBIT(table, bit);
347}
348
349static void sh_clearbit(char *ptr, int list, unsigned char *table)
350{
351 size_t bit;
352
353 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
354 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
355 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
356 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
357 OPENSSL_assert(TESTBIT(table, bit));
358 CLEARBIT(table, bit);
359}
360
361static void sh_setbit(char *ptr, int list, unsigned char *table)
362{
363 size_t bit;
364
365 OPENSSL_assert(list >= 0 && list < sh.freelist_size);
366 OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
367 bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
368 OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
369 OPENSSL_assert(!TESTBIT(table, bit));
370 SETBIT(table, bit);
371}
372
373static void sh_add_to_list(char **list, char *ptr)
374{
375 SH_LIST *temp;
376
377 OPENSSL_assert(WITHIN_FREELIST(list));
378 OPENSSL_assert(WITHIN_ARENA(ptr));
379
380 temp = (SH_LIST *)ptr;
381 temp->next = *(SH_LIST **)list;
382 OPENSSL_assert(temp->next == NULL || WITHIN_ARENA(temp->next));
383 temp->p_next = (SH_LIST **)list;
384
385 if (temp->next != NULL) {
386 OPENSSL_assert((char **)temp->next->p_next == list);
387 temp->next->p_next = &(temp->next);
388 }
389
390 *list = ptr;
391}
392
393static void sh_remove_from_list(char *ptr)
394{
395 SH_LIST *temp, *temp2;
396
397 temp = (SH_LIST *)ptr;
398 if (temp->next != NULL)
399 temp->next->p_next = temp->p_next;
400 *temp->p_next = temp->next;
401 if (temp->next == NULL)
402 return;
403
404 temp2 = temp->next;
405 OPENSSL_assert(WITHIN_FREELIST(temp2->p_next) || WITHIN_ARENA(temp2->p_next));
406}
407
408
409static int sh_init(size_t size, size_t minsize)
410{
411 int ret;
412 size_t i;
413 size_t pgsize;
414 size_t aligned;
415#if defined(_WIN32)
416 DWORD flOldProtect;
417 SYSTEM_INFO systemInfo;
418#endif
419
420 memset(&sh, 0, sizeof(sh));
421
422 /* make sure size is a powers of 2 */
423 OPENSSL_assert(size > 0);
424 OPENSSL_assert((size & (size - 1)) == 0);
425 if (size == 0 || (size & (size - 1)) != 0)
426 goto err;
427
428 if (minsize <= sizeof(SH_LIST)) {
429 OPENSSL_assert(sizeof(SH_LIST) <= 65536);
430 /*
431 * Compute the minimum possible allocation size.
432 * This must be a power of 2 and at least as large as the SH_LIST
433 * structure.
434 */
435 minsize = sizeof(SH_LIST) - 1;
436 minsize |= minsize >> 1;
437 minsize |= minsize >> 2;
438 if (sizeof(SH_LIST) > 16)
439 minsize |= minsize >> 4;
440 if (sizeof(SH_LIST) > 256)
441 minsize |= minsize >> 8;
442 minsize++;
443 } else {
444 /* make sure minsize is a powers of 2 */
445 OPENSSL_assert((minsize & (minsize - 1)) == 0);
446 if ((minsize & (minsize - 1)) != 0)
447 goto err;
448 }
449
450 sh.arena_size = size;
451 sh.minsize = minsize;
452 sh.bittable_size = (sh.arena_size / sh.minsize) * 2;
453
454 /* Prevent allocations of size 0 later on */
455 if (sh.bittable_size >> 3 == 0)
456 goto err;
457
458 sh.freelist_size = -1;
459 for (i = sh.bittable_size; i; i >>= 1)
460 sh.freelist_size++;
461
462 sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof(char *));
463 OPENSSL_assert(sh.freelist != NULL);
464 if (sh.freelist == NULL)
465 goto err;
466
467 sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
468 OPENSSL_assert(sh.bittable != NULL);
469 if (sh.bittable == NULL)
470 goto err;
471
472 sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
473 OPENSSL_assert(sh.bitmalloc != NULL);
474 if (sh.bitmalloc == NULL)
475 goto err;
476
477 /* Allocate space for heap, and two extra pages as guards */
478#if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
479 {
480# if defined(_SC_PAGE_SIZE)
481 long tmppgsize = sysconf(_SC_PAGE_SIZE);
482# else
483 long tmppgsize = sysconf(_SC_PAGESIZE);
484# endif
485 if (tmppgsize < 1)
486 pgsize = PAGE_SIZE;
487 else
488 pgsize = (size_t)tmppgsize;
489 }
490#elif defined(_WIN32)
491 GetSystemInfo(&systemInfo);
492 pgsize = (size_t)systemInfo.dwPageSize;
493#else
494 pgsize = PAGE_SIZE;
495#endif
496 sh.map_size = pgsize + sh.arena_size + pgsize;
497
498#if !defined(_WIN32)
499# ifdef MAP_ANON
500 sh.map_result = mmap(NULL, sh.map_size,
501 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE|MAP_CONCEAL, -1, 0);
502# else
503 {
504 int fd;
505
506 sh.map_result = MAP_FAILED;
507 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
508 sh.map_result = mmap(NULL, sh.map_size,
509 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
510 close(fd);
511 }
512 }
513# endif
514 if (sh.map_result == MAP_FAILED)
515 goto err;
516#else
517 sh.map_result = VirtualAlloc(NULL, sh.map_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
518
519 if (sh.map_result == NULL)
520 goto err;
521#endif
522
523 sh.arena = (char *)(sh.map_result + pgsize);
524 sh_setbit(sh.arena, 0, sh.bittable);
525 sh_add_to_list(&sh.freelist[0], sh.arena);
526
527 /* Now try to add guard pages and lock into memory. */
528 ret = 1;
529
530#if !defined(_WIN32)
531 /* Starting guard is already aligned from mmap. */
532 if (mprotect(sh.map_result, pgsize, PROT_NONE) < 0)
533 ret = 2;
534#else
535 if (VirtualProtect(sh.map_result, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE)
536 ret = 2;
537#endif
538
539 /* Ending guard page - need to round up to page boundary */
540 aligned = (pgsize + sh.arena_size + (pgsize - 1)) & ~(pgsize - 1);
541#if !defined(_WIN32)
542 if (mprotect(sh.map_result + aligned, pgsize, PROT_NONE) < 0)
543 ret = 2;
544#else
545 if (VirtualProtect(sh.map_result + aligned, pgsize, PAGE_NOACCESS, &flOldProtect) == FALSE)
546 ret = 2;
547#endif
548
549#if defined(OPENSSL_SYS_LINUX) && defined(MLOCK_ONFAULT) && defined(SYS_mlock2)
550 if (syscall(SYS_mlock2, sh.arena, sh.arena_size, MLOCK_ONFAULT) < 0) {
551 if (errno == ENOSYS) {
552 if (mlock(sh.arena, sh.arena_size) < 0)
553 ret = 2;
554 } else {
555 ret = 2;
556 }
557 }
558#elif defined(_WIN32)
559 if (VirtualLock(sh.arena, sh.arena_size) == FALSE)
560 ret = 2;
561#else
562 if (mlock(sh.arena, sh.arena_size) < 0)
563 ret = 2;
564#endif
565#ifdef MADV_DONTDUMP
566 if (madvise(sh.arena, sh.arena_size, MADV_DONTDUMP) < 0)
567 ret = 2;
568#endif
569
570 return ret;
571
572 err:
573 sh_done();
574 return 0;
575}
576
577static void sh_done(void)
578{
579 OPENSSL_free(sh.freelist);
580 OPENSSL_free(sh.bittable);
581 OPENSSL_free(sh.bitmalloc);
582#if !defined(_WIN32)
583 if (sh.map_result != MAP_FAILED && sh.map_size)
584 munmap(sh.map_result, sh.map_size);
585#else
586 if (sh.map_result != NULL && sh.map_size)
587 VirtualFree(sh.map_result, 0, MEM_RELEASE);
588#endif
589 memset(&sh, 0, sizeof(sh));
590}
591
592static int sh_allocated(const char *ptr)
593{
594 return WITHIN_ARENA(ptr) ? 1 : 0;
595}
596
597static char *sh_find_my_buddy(char *ptr, int list)
598{
599 size_t bit;
600 char *chunk = NULL;
601
602 bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
603 bit ^= 1;
604
605 if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
606 chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
607
608 return chunk;
609}
610
611static void *sh_malloc(size_t size)
612{
613 ossl_ssize_t list, slist;
614 size_t i;
615 char *chunk;
616
617 if (size > sh.arena_size)
618 return NULL;
619
620 list = sh.freelist_size - 1;
621 for (i = sh.minsize; i < size; i <<= 1)
622 list--;
623 if (list < 0)
624 return NULL;
625
626 /* try to find a larger entry to split */
627 for (slist = list; slist >= 0; slist--)
628 if (sh.freelist[slist] != NULL)
629 break;
630 if (slist < 0)
631 return NULL;
632
633 /* split larger entry */
634 while (slist != list) {
635 char *temp = sh.freelist[slist];
636
637 /* remove from bigger list */
638 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
639 sh_clearbit(temp, slist, sh.bittable);
640 sh_remove_from_list(temp);
641 OPENSSL_assert(temp != sh.freelist[slist]);
642
643 /* done with bigger list */
644 slist++;
645
646 /* add to smaller list */
647 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
648 sh_setbit(temp, slist, sh.bittable);
649 sh_add_to_list(&sh.freelist[slist], temp);
650 OPENSSL_assert(sh.freelist[slist] == temp);
651
652 /* split in 2 */
653 temp += sh.arena_size >> slist;
654 OPENSSL_assert(!sh_testbit(temp, slist, sh.bitmalloc));
655 sh_setbit(temp, slist, sh.bittable);
656 sh_add_to_list(&sh.freelist[slist], temp);
657 OPENSSL_assert(sh.freelist[slist] == temp);
658
659 OPENSSL_assert(temp-(sh.arena_size >> slist) == sh_find_my_buddy(temp, slist));
660 }
661
662 /* peel off memory to hand back */
663 chunk = sh.freelist[list];
664 OPENSSL_assert(sh_testbit(chunk, list, sh.bittable));
665 sh_setbit(chunk, list, sh.bitmalloc);
666 sh_remove_from_list(chunk);
667
668 OPENSSL_assert(WITHIN_ARENA(chunk));
669
670 /* zero the free list header as a precaution against information leakage */
671 memset(chunk, 0, sizeof(SH_LIST));
672
673 return chunk;
674}
675
676static void sh_free(void *ptr)
677{
678 size_t list;
679 void *buddy;
680
681 if (ptr == NULL)
682 return;
683 OPENSSL_assert(WITHIN_ARENA(ptr));
684 if (!WITHIN_ARENA(ptr))
685 return;
686
687 list = sh_getlist(ptr);
688 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
689 sh_clearbit(ptr, list, sh.bitmalloc);
690 sh_add_to_list(&sh.freelist[list], ptr);
691
692 /* Try to coalesce two adjacent free areas. */
693 while ((buddy = sh_find_my_buddy(ptr, list)) != NULL) {
694 OPENSSL_assert(ptr == sh_find_my_buddy(buddy, list));
695 OPENSSL_assert(ptr != NULL);
696 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
697 sh_clearbit(ptr, list, sh.bittable);
698 sh_remove_from_list(ptr);
699 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
700 sh_clearbit(buddy, list, sh.bittable);
701 sh_remove_from_list(buddy);
702
703 list--;
704
705 /* Zero the higher addressed block's free list pointers */
706 memset(ptr > buddy ? ptr : buddy, 0, sizeof(SH_LIST));
707 if (ptr > buddy)
708 ptr = buddy;
709
710 OPENSSL_assert(!sh_testbit(ptr, list, sh.bitmalloc));
711 sh_setbit(ptr, list, sh.bittable);
712 sh_add_to_list(&sh.freelist[list], ptr);
713 OPENSSL_assert(sh.freelist[list] == ptr);
714 }
715}
716
717static size_t sh_actual_size(char *ptr)
718{
719 int list;
720
721 OPENSSL_assert(WITHIN_ARENA(ptr));
722 if (!WITHIN_ARENA(ptr))
723 return 0;
724 list = sh_getlist(ptr);
725 OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
726 return sh.arena_size / (ONE << list);
727}
728#endif /* OPENSSL_NO_SECURE_MEMORY */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette