- Timestamp:
- Aug 7, 2008, 9:13:24 PM (16 years ago)
- Location:
- trunk/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/dnmalloc.c
r171 r172 1 1 /* DistriNet malloc (dnmalloc): a more secure memory allocator. 2 Copyright (C) 2005, Yves Younan, Wouter Joosen, Frank Piessens and Rainer Wichmann 2 Copyright (C) 2005, Yves Younan, Wouter Joosen, Frank Piessens 3 and Rainer Wichmann 4 3 5 The authors can be contacted by: 4 6 Email: dnmalloc@fort-knox.org … … 25 27 */ 26 28 27 /* Current version: dnmalloc 1.0 29 /* Current version: dnmalloc 1.0 */ 28 30 /* Includes arc4random from OpenBSD, which is under the BDS license */ 29 31 … … 93 95 works on OpenBSD. 94 96 97 further to 1.0: 98 Valgrind client requests inserted (#define USE_VALGRIND) 99 Fix malloc_consolidate (nextchunk->fd, nextchunk->bck may be NULL) 100 Portability: minsize = 32 bit on 64bit architecture 101 Minor cleanups 102 95 103 There may be some bugs left in this version. please use with caution. 96 104 */ … … 176 184 #endif 177 185 186 #ifdef USE_VALGRIND 187 #include <valgrind/memcheck.h> 188 #else 189 #define VALGRIND_FREELIKE_BLOCK(a,b) ((void)0) 190 #define VALGRIND_MALLOCLIKE_BLOCK(a,b,c,d) ((void)0) 191 #define VALGRIND_CREATE_MEMPOOL(a,b,c) ((void)0) 192 #define VALGRIND_MEMPOOL_ALLOC(a,b,c) ((void)0) 193 #define VALGRIND_MEMPOOL_FREE(a,b) ((void)0) 194 #define VALGRIND_DESTROY_MEMPOOL(a) ((void)0) 195 #define VALGRIND_MAKE_MEM_DEFINED(a,b) ((void)0) 196 #define VALGRIND_MAKE_MEM_UNDEFINED(a,b) ((void)0) 197 #define VALGRIND_MAKE_MEM_NOACCESS(a,b) ((void)0) 198 #endif 199 178 200 #if defined (__GNUC__) && __GNUC__ > 2 179 201 # define LIKELY(expression) (__builtin_expect(!!(expression), 1)) … … 277 299 #endif 278 300 279 #if !defined(SAMHAIN)280 301 static void default_assert_handler(const char *error, 281 302 const char *file, int line) … … 300 321 fputc('\n', stderr); 301 322 #endif 323 (void) line; 302 324 abort(); 303 325 } 304 326 static assert_handler_tp *assert_handler = default_assert_handler; 305 #else 306 extern void safe_fatal(const char *error, 307 const char *file, int line); 308 static assert_handler_tp *assert_handler = safe_fatal; 309 #endif 327 310 328 311 329 #define assert(x) \ … … 337 355 /* #define DNMALLOC_DEBUG */ 338 356 339 /* Do some extra checks? */357 /* Do some extra checks? if not, covered by assrt()s */ 340 358 /* #define DNMALLOC_CHECKS */ 341 359 … … 668 686 669 687 #ifndef HAVE_MMAP 670 # ifdef SAMHAIN 671 # error mmap not available, use --disable-dnmalloc 672 # else 673 # error HAVE_MMAP not defined, has your operating system mmap? 674 # endif 688 # error HAVE_MMAP not defined, has your operating system mmap? 675 689 #endif 676 690 … … 1898 1912 guard_set_p = (char*)(chunk(P)); \ 1899 1913 guard_set_p += request; \ 1914 VALGRIND_MAKE_MEM_UNDEFINED(guard_set_p,GUARD_SIZE); \ 1900 1915 guard_set_q = (char*)(guard); \ 1901 1916 *guard_set_p = *guard_set_q; ++guard_set_p; ++guard_set_q; \ … … 1903 1918 *guard_set_p = *guard_set_q; ++guard_set_p; ++guard_set_q; \ 1904 1919 *guard_set_p = *guard_set_q; \ 1920 VALGRIND_MAKE_MEM_NOACCESS((((char*)chunk(P))+request),GUARD_SIZE); \ 1905 1921 (P)->req = request 1906 1922 1907 1923 #define guard_check(guard, P) \ 1908 assert(0 == memcmp((((char *)chunk(P))+(P)->req),(void*)(guard),GUARD_SIZE)); 1924 VALGRIND_MAKE_MEM_DEFINED((((char *)chunk(P))+(P)->req), GUARD_SIZE); \ 1925 assert(0 == memcmp((((char *)chunk(P))+(P)->req),(void*)(guard),GUARD_SIZE));\ 1926 VALGRIND_MAKE_MEM_NOACCESS((((char *)chunk(P))+(P)->req), GUARD_SIZE); 1909 1927 1910 1928 #else … … 2164 2182 /* #define FIRST_SORTED_BIN_SIZE 65536 */ 2165 2183 2166 /* 12288 1m59 1m58 1m582167 * 2560 1m56 1m59 1m572168 * MIN_LARGE_SIZE 2m01 1m56 1m572169 */2170 #ifdef SAMHAIN2171 #define FIRST_SORTED_BIN_SIZE 25602172 #else2173 2184 #define FIRST_SORTED_BIN_SIZE MIN_LARGE_SIZE 2174 #endif 2185 2175 2186 2176 2187 /* … … 2372 2383 2373 2384 /* Minimum size of a chunk */ 2374 #define MINCHUNKSIZE 16 2385 2386 #if (SIZEOF_UNSIGNED_LONG == 8) || defined(__arch64__) || defined(__ia64__) || defined(__x86_64__) || defined(__LP64__) || defined(__64BIT__) || defined(_LP64) || defined(_M_IA64) || (defined(_MIPS_SZLONG) && (_MIPS_SZLONG == 64)) 2387 # define MINCHUNKSIZE 32 2388 #else 2389 # define MINCHUNKSIZE 16 2390 #endif 2391 2375 2392 2376 2393 /* The amount of hashtable entries for each page: … … 2542 2559 firstciinfo = currciinfo; 2543 2560 totalcictr++; 2561 VALGRIND_CREATE_MEMPOOL(newcireg, 0, 0); 2544 2562 } 2545 2563 … … 2567 2585 if (freelst) 2568 2586 { 2569 freeci = freelst; 2570 newciinfo->freelist = freelst->fd; 2571 newciinfo->freecounter--; 2572 /* memset(freeci, 0, sizeof(struct chunkinfo)); */ 2573 freeci->prev_size = 0; 2574 freeci->size = 0; 2575 freeci->req = 0; 2576 freeci->hash_next = NULL; 2577 freeci->fd = NULL; 2578 freeci->bk = NULL; 2579 freeci->chunk = NULL; 2580 return (freeci); 2587 freeci = freelst; 2588 newciinfo->freecounter--; 2589 newciinfo->freelist = freelst->fd; 2590 2591 VALGRIND_MEMPOOL_ALLOC((char*)currciinfo, (char*)freeci, 2592 sizeof(struct chunkinfo)); 2593 2594 freeci->prev_size = 0; 2595 freeci->size = 0; 2596 freeci->req = 0; 2597 freeci->hash_next = NULL; 2598 freeci->fd = NULL; 2599 freeci->bk = NULL; 2600 freeci->chunk = NULL; 2601 return (freeci); 2581 2602 } 2582 2603 else … … 2591 2612 currciinfo->freecounter--; 2592 2613 currciinfo->position++; 2614 2615 VALGRIND_MEMPOOL_ALLOC((char*)currciinfo, (char*)freeci, 2616 sizeof(struct chunkinfo)); 2617 2593 2618 return (freeci); 2594 2619 } … … 2600 2625 struct cireginfo *prevciinfo = firstciinfo; 2601 2626 void *unmapme; 2627 2602 2628 while (newciinfo && newciinfo != freeme) { 2603 2629 prevciinfo = newciinfo; … … 2609 2635 prevciinfo->next = newciinfo->next; 2610 2636 unmapme = (void *) ((char*)freeme - PGSIZE); 2637 VALGRIND_DESTROY_MEMPOOL((char*)freeme); 2611 2638 #if PARANOIA > 1 2612 2639 munmap(unmapme, CIREGSIZE+(2*PGSIZE)); … … 2620 2647 2621 2648 struct cireginfo *newciinfo; 2649 2622 2650 newciinfo = currciinfo; 2623 2651 if (((chunkinfoptr) newciinfo < p) && (p < (chunkinfoptr) (newciinfo+NUMBER_FREE_CHUNKS))) { … … 2625 2653 newciinfo->freelist = p; 2626 2654 newciinfo->freecounter++; 2655 VALGRIND_MEMPOOL_FREE((char*)newciinfo, (char*)p); 2656 VALGRIND_MAKE_MEM_DEFINED(p,sizeof(struct chunkinfo)); 2657 VALGRIND_MAKE_MEM_NOACCESS(p->size, sizeof(INTERNAL_SIZE_T)); 2658 VALGRIND_MAKE_MEM_NOACCESS(p->req, sizeof(INTERNAL_SIZE_T)); 2659 VALGRIND_MAKE_MEM_NOACCESS(p->bk, sizeof(struct chunkinfo*)); 2660 VALGRIND_MAKE_MEM_NOACCESS(p->chunk, sizeof(mchunkptr)); 2627 2661 } else { 2628 2662 newciinfo = firstciinfo; … … 2633 2667 newciinfo->freelist = p; 2634 2668 newciinfo->freecounter++; 2669 VALGRIND_MEMPOOL_FREE((char*)newciinfo, (char*)p); 2670 VALGRIND_MAKE_MEM_DEFINED(p,sizeof(struct chunkinfo)); 2671 VALGRIND_MAKE_MEM_NOACCESS(p->size, sizeof(INTERNAL_SIZE_T)); 2672 VALGRIND_MAKE_MEM_NOACCESS(p->req, sizeof(INTERNAL_SIZE_T)); 2673 VALGRIND_MAKE_MEM_NOACCESS(p->bk, sizeof(struct chunkinfo*)); 2674 VALGRIND_MAKE_MEM_NOACCESS(p->chunk, sizeof(mchunkptr)); 2635 2675 if (UNLIKELY(newciinfo->freecounter == NUMBER_FREE_CHUNKS)) 2636 2676 freeciregion(newciinfo); … … 2738 2778 } 2739 2779 else { 2780 2740 2781 ci_insert->hash_next = ci_orig->hash_next; 2741 2782 ci_orig->hash_next = ci_insert; … … 2797 2838 } 2798 2839 2799 /* mmapped chunks are multiples of pagesize, no hash_nexts, just remove from the hashtable */ 2840 /* mmapped chunks are multiples of pagesize, no hash_nexts, 2841 * just remove from the hashtable 2842 */ 2800 2843 #define hashtable_remove_mmapped(p) hashtable[hash(p)] = 0; 2801 2844 … … 2807 2850 2808 2851 #ifdef DNMALLOC_DEBUG 2809 fprintf(stderr, "hashtable_ski : %p, %lu\n", chunk(ci_todelete), hash(chunk(ci_todelete)));2852 fprintf(stderr, "hashtable_skiprm: %p, %lu\n", chunk(ci_todelete), hash(chunk(ci_todelete))); 2810 2853 #endif 2811 2854 … … 3260 3303 3261 3304 if (have_fastchunks(av)) { 3305 Void_t * retval; 3262 3306 assert(in_smallbin_range(nb)); 3263 3307 malloc_consolidate(av); … … 3265 3309 fprintf(stderr, "Return sysmalloc have_fastchunks\n"); 3266 3310 #endif 3267 return mALLOc(nb - MALLOC_ALIGN_MASK); 3311 retval = mALLOc(nb - MALLOC_ALIGN_MASK); 3312 VALGRIND_FREELIKE_BLOCK(retval, 0); 3313 return retval; 3268 3314 } 3269 3315 … … 3296 3342 if (mm != (char*)(MORECORE_FAILURE)) { 3297 3343 3344 VALGRIND_MAKE_MEM_NOACCESS(mm,size); 3345 3298 3346 /* 3299 3347 The offset to the start of the mmapped region is stored … … 3404 3452 if (brk != (char*)(MORECORE_FAILURE)) { 3405 3453 av->sbrked_mem += size; 3454 VALGRIND_MAKE_MEM_NOACCESS(brk,size); 3406 3455 } 3407 3456 … … 3430 3479 if (brk != (char*)(MORECORE_FAILURE)) { 3431 3480 3481 VALGRIND_MAKE_MEM_NOACCESS(brk,size); 3482 3432 3483 av->mmapped_mem += size; 3433 3484 #ifdef DNMALLOC_DEBUG … … 3574 3625 set_noncontiguous(av); 3575 3626 } 3627 else { 3628 VALGRIND_MAKE_MEM_NOACCESS(snd_brk,correction); 3629 } 3576 3630 3577 3631 } … … 3673 3727 set_head(old_top, old_size | PREV_INUSE | INUSE); 3674 3728 guard_set(av->guard_stored, old_top, 0, old_size); 3729 VALGRIND_MALLOCLIKE_BLOCK(chunk(old_top), old_size, 0, 0); 3675 3730 fREe(chunk(old_top)); 3676 3731 av->trim_threshold = tt; … … 3885 3940 check_remalloced_chunk(victim, nb); 3886 3941 guard_set(av->guard_stored, victim, bytes, nb); 3942 VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0); 3887 3943 return chunk(victim); 3888 3944 } … … 3910 3966 check_malloced_chunk(victim, nb); 3911 3967 guard_set(av->guard_stored, victim, bytes, nb); 3968 VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0); 3912 3969 return chunk(victim); 3913 3970 } … … 3970 4027 check_malloced_chunk(victim, nb); 3971 4028 guard_set(av->guard_stored, victim, bytes, nb); 4029 VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0); 3972 4030 return chunk(victim); 3973 4031 } … … 3983 4041 check_malloced_chunk(victim, nb); 3984 4042 guard_set(av->guard_stored, victim, bytes, nb); 4043 VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0); 3985 4044 return chunk(victim); 3986 4045 } … … 4058 4117 check_malloced_chunk(victim, nb); 4059 4118 guard_set(av->guard_stored, victim, bytes, nb); 4119 VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0); 4060 4120 return chunk(victim); 4061 4121 } … … 4065 4125 check_malloced_chunk(victim, nb); 4066 4126 guard_set(av->guard_stored, victim, bytes, nb); 4127 VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0); 4067 4128 return chunk(victim); 4068 4129 } … … 4140 4201 remainder->bk = remainder->fd = unsorted_chunks(av); 4141 4202 /* advertise as last remainder */ 4142 if (in_smallbin_range(nb)) 4143 av->last_remainder = remainder; 4203 if (in_smallbin_range(nb)) 4204 av->last_remainder = remainder; 4144 4205 4145 4206 set_head(victim, nb | PREV_INUSE | INUSE); … … 4148 4209 check_malloced_chunk(victim, nb); 4149 4210 guard_set(av->guard_stored, victim, bytes, nb); 4211 VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0); 4150 4212 return chunk(victim); 4151 4213 } … … 4155 4217 check_malloced_chunk(victim, nb); 4156 4218 guard_set(av->guard_stored, victim, bytes, nb); 4219 VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0); 4157 4220 return chunk(victim); 4158 4221 } … … 4183 4246 4184 4247 if ((CHUNK_SIZE_T)(size) >= (CHUNK_SIZE_T)(nb + MINSIZE)) { 4185 4248 remainder = cireg_getfree(); 4186 4249 remainder_size = size - nb; 4187 4250 remainder->chunk = chunk_at_offset(chunk(victim), nb); … … 4192 4255 check_malloced_chunk(victim, nb); 4193 4256 guard_set(av->guard_stored, victim, bytes, nb); 4257 VALGRIND_MALLOCLIKE_BLOCK(chunk(victim), bytes, 0, 0); 4194 4258 return chunk(victim); 4195 4259 } … … 4203 4267 guard_set(av->guard_stored, victim, bytes, nb); 4204 4268 } 4269 VALGRIND_MALLOCLIKE_BLOCK(retval, bytes, 0, 0); 4205 4270 return retval; 4206 4271 } … … 4254 4319 } 4255 4320 4321 VALGRIND_FREELIKE_BLOCK(mem, 0); 4322 4256 4323 guard_check(av->guard_stored, p); 4257 4324 4258 4325 size = chunksize(p); 4259 4326 … … 4471 4538 */ 4472 4539 if (av && av->max_fast != 0) { 4540 4473 4541 clear_fastchunks(av); 4474 4542 … … 4529 4597 clear_previnuse(nextchunk); 4530 4598 4599 /* if mmap is used instead of sbrk, we may have a 4600 * chunk with !nextchunk->fd && !nextchunk->bk 4601 */ 4531 4602 if (!inuse(nextchunk)) { 4532 size += nextsize; 4533 unlink(nextchunk, bck, fwd); 4534 set_head(p, size | PREV_INUSE); 4535 hashtable_skiprm(p,nextchunk); 4536 freecilst_add(nextchunk); 4603 if( nextchunk->fd && nextchunk->bk) { 4604 size += nextsize; 4605 unlink(nextchunk, bck, fwd); 4606 set_head(p, size | PREV_INUSE); 4607 hashtable_skiprm(p,nextchunk); 4608 freecilst_add(nextchunk); 4609 } 4537 4610 } 4538 4611 … … 4551 4624 } 4552 4625 4553 else {4626 else if (nextchunk == av->top) { 4554 4627 size += nextsize; 4555 4628 set_head(p, size | PREV_INUSE); … … 4558 4631 av->top = p; 4559 4632 } 4560 } 4633 } /* if (nextchunk) */ 4561 4634 4562 4635 } while ( (p = nextp) != 0); … … 4646 4719 } 4647 4720 4721 VALGRIND_FREELIKE_BLOCK(oldmem, 0); 4648 4722 guard_check(av->guard_stored, oldp); 4649 4723 … … 4675 4749 hashtable_insert(oldp, av->top); 4676 4750 guard_set(av->guard_stored, oldp, bytes, nb); 4751 VALGRIND_MALLOCLIKE_BLOCK(chunk(oldp), bytes, 0, 0); 4677 4752 return chunk(oldp); 4678 4753 } … … 4696 4771 4697 4772 newmem = mALLOc(nb - MALLOC_ALIGN_MASK); 4698 if (newmem == 0)4773 if (newmem == 0) 4699 4774 return 0; /* propagate failure */ 4700 4775 … … 4702 4777 newsize = chunksize(newp); 4703 4778 4779 4704 4780 /* next = next_chunkinfo(oldp); *//* 'next' never used rw 19.05.2008 */ 4705 4781 /* … … 4720 4796 */ 4721 4797 4798 VALGRIND_MALLOCLIKE_BLOCK(chunk(oldp), chunksize(oldp), 0, 0); 4799 4722 4800 copysize = oldsize; 4723 4801 s = (INTERNAL_SIZE_T*)(oldmem); … … 4774 4852 set_all_inuse(remainder); 4775 4853 guard_set(av->guard_stored, remainder, 0, remainder_size); 4854 VALGRIND_MALLOCLIKE_BLOCK(chunk(remainder), remainder_size, 0, 0); 4776 4855 fREe(chunk(remainder)); 4777 4856 } … … 4783 4862 check_inuse_chunk(newp); 4784 4863 guard_set(av->guard_stored, newp, bytes, nb); 4864 VALGRIND_MALLOCLIKE_BLOCK(chunk(newp), bytes, 0, 0); 4785 4865 return chunk(newp); 4786 4866 } … … 4798 4878 CHUNK_SIZE_T sum; 4799 4879 4880 4800 4881 /* Note the extra SIZE_SZ overhead */ 4801 4882 //newsize = (nb + offset + SIZE_SZ + pagemask) & ~pagemask; … … 4806 4887 { 4807 4888 guard_set(av->guard_stored, oldp, bytes, nb); 4889 VALGRIND_FREELIKE_BLOCK(oldmem, 0); 4890 VALGRIND_MALLOCLIKE_BLOCK(oldmem, bytes, 0, 0); 4808 4891 return oldmem; 4809 4892 } … … 4832 4915 4833 4916 guard_set(av->guard_stored, oldp, bytes, nb); 4917 VALGRIND_FREELIKE_BLOCK(oldmem, 0); 4918 VALGRIND_MALLOCLIKE_BLOCK(chunk(oldp), bytes, 0, 0); 4834 4919 return chunk(oldp); 4835 4920 } … … 4847 4932 } 4848 4933 } 4934 VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, 0, 0); 4849 4935 guard_set(av->guard_stored, mem2chunk(newmem), bytes, nb); 4850 4936 return newmem; -
trunk/src/make-tests.sh
r171 r172 57 57 int main(void) 58 58 { 59 #if !defined(USE_SYSTEM_MALLOC) 60 typedef void assert_handler_tp(const char * error, const char *file, int line); 61 extern assert_handler_tp *dnmalloc_set_handler(assert_handler_tp *new); 62 extern void safe_fatal (const char * details, const char *f, int l); 63 #endif 59 64 #if !defined(USE_SYSTEM_MALLOC) && defined(USE_MALLOC_LOCK) 60 65 extern int dnmalloc_pthread_init(void); 61 66 dnmalloc_pthread_init(); 67 #endif 68 #if !defined(USE_SYSTEM_MALLOC) 69 (void) dnmalloc_set_handler(safe_fatal); 62 70 #endif 63 71 int retval; -
trunk/src/samhain.c
r171 r172 1220 1220 int my_argc = 0; 1221 1221 char * my_argv[32]; 1222 #endif 1223 1224 #if !defined(USE_SYSTEM_MALLOC) 1225 typedef void assert_handler_tp(const char * error, const char *file, int line); 1226 extern assert_handler_tp *dnmalloc_set_handler(assert_handler_tp *new); 1227 (void) dnmalloc_set_handler(safe_fatal); 1222 1228 #endif 1223 1229 -
trunk/src/sh_unix.c
r171 r172 557 557 #endif 558 558 { 559 #if defined(SL_DEBUG) 559 #if defined(SL_DEBUG) && (defined(USE_SYSTEM_MALLOC) || !defined(USE_MALLOC_LOCK)) 560 560 int retval; 561 561 #endif … … 576 576 /* Check whether the heap is ok; otherwise _exit 577 577 */ 578 #if !defined(SL_DEBUG) 578 #if !defined(SL_DEBUG) || (!defined(USE_SYSTEM_MALLOC) && defined(USE_MALLOC_LOCK)) 579 579 ++immediate_exit_fast; 580 580 if (skey != NULL && immediate_exit_fast < 2)
Note:
See TracChangeset
for help on using the changeset viewer.