X-Git-Url: http://plrg.eecs.uci.edu/git/?p=model-checker.git;a=blobdiff_plain;f=mymemory.cc;h=95459be561af6b7bab74d50610d470f1005c4464;hp=e8518182c55a71800637c981f1d3ad0db0cc3b2d;hb=ea0cb2585eee658354f48b4bbfebaf030cccf317;hpb=123c66e0c1b0f58aae2916cc22b2100143a2ceb4 diff --git a/mymemory.cc b/mymemory.cc index e851818..95459be 100644 --- a/mymemory.cc +++ b/mymemory.cc @@ -5,6 +5,7 @@ #include #include #include +#include "common.h" #define REQUESTS_BEFORE_ALLOC 1024 size_t allocatedReqs[ REQUESTS_BEFORE_ALLOC ] = { 0 }; int nextRequest = 0; @@ -13,10 +14,37 @@ int howManyFreed = 0; static mspace sStaticSpace = NULL; #endif +/** Non-snapshotting calloc for our use. */ +void *model_calloc(size_t count, size_t size) { +#if USE_MPROTECT_SNAPSHOT + static void *(*callocp)(size_t count, size_t size)=NULL; + char *error; + void *ptr; + + /* get address of libc malloc */ + if (!callocp) { + callocp = ( void * ( * )( size_t, size_t ) )dlsym(RTLD_NEXT, "calloc"); + if ((error = dlerror()) != NULL) { + fputs(error, stderr); + exit(EXIT_FAILURE); + } + } + ptr = callocp(count, size); + return ptr; +#else + if( !snapshotrecord) { + createSharedMemory(); + } + if( NULL == sStaticSpace ) + sStaticSpace = create_mspace_with_base( ( void * )( snapshotrecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct SnapShot ), 1 ); + return mspace_calloc( sStaticSpace, count, size ); +#endif +} + /** Non-snapshotting malloc for our use. */ -void *MYMALLOC(size_t size) { +void *model_malloc(size_t size) { #if USE_MPROTECT_SNAPSHOT - static void *(*mallocp)(size_t size); + static void *(*mallocp)(size_t size)=NULL; char *error; void *ptr; @@ -40,6 +68,24 @@ void *MYMALLOC(size_t size) { #endif } +/** @brief Snapshotting malloc, for use by model-checker (not user progs) */ +void * snapshot_malloc(size_t size) +{ + return malloc(size); +} + +/** @brief Snapshotting calloc, for use by model-checker (not user progs) */ +void * snapshot_calloc(size_t count, size_t size) +{ + return calloc(count, size); +} + +/** @brief Snapshotting free, for use by model-checker (not user progs) */ +void snapshot_free(void *ptr) +{ + free(ptr); +} + void *system_malloc( size_t size ){ static void *(*mallocp)(size_t size); char *error; @@ -73,7 +119,7 @@ void system_free( void * ptr ){ } /** Non-snapshotting free for our use. */ -void MYFREE(void *ptr) { +void model_free(void *ptr) { #if USE_MPROTECT_SNAPSHOT static void (*freep)(void *); char *error; @@ -93,83 +139,101 @@ void MYFREE(void *ptr) { } -/** This global references the mspace for the snapshotting heap */ +/** @brief Global mspace reference for the snapshotting heap */ mspace mySpace = NULL; -/** This global references the unaligned memory address that was malloced for the snapshotting heap */ -void * basemySpace = NULL; +/** @brief Global reference to the unaligned memory address that was malloc'd + * for the snapshotting heap */ +void *basemySpace = NULL; + +/** Bootstrap allocation. Problem is that the dynamic linker calls + * require calloc to work and calloc requires the dynamic linker to + * work. */ + +#define BOOTSTRAPBYTES 4096 +char bootstrapmemory[BOOTSTRAPBYTES]; +size_t offset=0; -/** Adding the fix for not able to allocate through a reimplemented calloc at the beginning before instantiating our allocator -A bit circumspect about adding an sbrk. linux docs say to avoid using it... */ void * HandleEarlyAllocationRequest( size_t sz ){ - if( 0 == mySpace ){ - void * returnAddress = sbrk( sz ); - if( nextRequest >= REQUESTS_BEFORE_ALLOC ){ - exit( EXIT_FAILURE ); - } - allocatedReqs[ nextRequest++ ] = ( size_t )returnAddress; - return returnAddress; + /*Align to 8 byte boundary*/ + sz=(sz+7)&~7; + + if (sz > (BOOTSTRAPBYTES-offset)) { + printf("OUT OF BOOTSTRAP MEMORY\n"); + exit(EXIT_FAILURE); } - return NULL; + + void * pointer= (void *) & bootstrapmemory[offset]; + offset+=sz; + return pointer; } -/** The fact that I am not expecting more than a handful requests is implicit in my not using a binary search here*/ +/** Check whether this is bootstrapped memory that we should not + free. */ + bool DontFree( void * ptr ){ - if( howManyFreed == nextRequest ) return false; //a minor optimization to reduce the number of instructions executed on each free call.... - if( NULL == ptr ) return true; - for( int i = nextRequest - 1; i >= 0; --i ){ - if( allocatedReqs[ i ] == ( size_t )ptr ) { - ++howManyFreed; - return true; - } - } - return false; + return (ptr>=(&bootstrapmemory[0])&&ptr<(&bootstrapmemory[BOOTSTRAPBYTES])); } -/** Snapshotting malloc implementation for user programs. */ -void *malloc( size_t size ) { - void * earlyReq = HandleEarlyAllocationRequest( size ); - if( earlyReq ) return earlyReq; - return mspace_malloc( mySpace, size ); +/** @brief Snapshotting malloc implementation for user programs */ +void *malloc( size_t size ) +{ + if (mySpace) { + void *tmp=mspace_malloc( mySpace, size ); + ASSERT(tmp); + return tmp; + } else + return HandleEarlyAllocationRequest( size ); } -/** Snapshotting free implementation for user programs. */ +/** @brief Snapshotting free implementation for user programs */ void free( void * ptr ){ - if( DontFree( ptr ) ) return; - mspace_free( mySpace, ptr ); + if (!DontFree(ptr)) + mspace_free(mySpace, ptr); } -/** Snapshotting realloc implementation for user programs. */ -void *realloc( void *ptr, size_t size ){ - return mspace_realloc( mySpace, ptr, size ); +/** @brief Snapshotting realloc implementation for user programs */ +void *realloc( void *ptr, size_t size ) +{ + void *tmp = mspace_realloc(mySpace, ptr, size); + ASSERT(tmp); + return tmp; } -/** Snapshotting calloc implementation for user programs. */ -void * calloc( size_t num, size_t size ){ - void * earlyReq = HandleEarlyAllocationRequest( size * num ); - if( earlyReq ) { - std::memset( earlyReq, 0, size * num ); - return earlyReq; +/** @brief Snapshotting calloc implementation for user programs */ +void * calloc( size_t num, size_t size ) +{ + if (mySpace) { + void *tmp = mspace_calloc(mySpace, num, size); + ASSERT(tmp); + return tmp; + } else { + void *tmp=HandleEarlyAllocationRequest( size * num ); + std::memset( tmp, 0, size * num ); + return tmp; } - return mspace_calloc( mySpace, num, size ); } -/** Snapshotting new operator for user programs. */ -void * operator new(size_t size) throw(std::bad_alloc) { +/** @brief Snapshotting new operator for user programs */ +void * operator new(size_t size) throw(std::bad_alloc) +{ return malloc(size); } -/** Snapshotting delete operator for user programs. */ -void operator delete(void *p) throw() { +/** @brief Snapshotting delete operator for user programs */ +void operator delete(void *p) throw() +{ free(p); } -/** Snapshotting new[] operator for user programs. */ -void * operator new[](size_t size) throw(std::bad_alloc) { +/** @brief Snapshotting new[] operator for user programs */ +void * operator new[](size_t size) throw(std::bad_alloc) +{ return malloc(size); } -/** Snapshotting delete[] operator for user programs. */ -void operator delete[](void *p, size_t size) { +/** @brief Snapshotting delete[] operator for user programs */ +void operator delete[](void *p, size_t size) +{ free(p); }