X-Git-Url: https://git.saurik.com/apple/dyld.git/blobdiff_plain/39a8cd101b922f08058746122efff58c14b57605..17a1d27dac9f840f614dcca486e07a34f9380394:/src/dyldNew.cpp diff --git a/src/dyldNew.cpp b/src/dyldNew.cpp index fe828ee..cf64f58 100644 --- a/src/dyldNew.cpp +++ b/src/dyldNew.cpp @@ -27,6 +27,7 @@ #include #include #include +#include extern "C" void* __dso_handle; @@ -61,12 +62,16 @@ static dyld_static_pool* currentPool = &initialPool; void* malloc(size_t size) { - if ( dyld::gLibSystemHelpers != NULL) { + if ( (dyld::gLibSystemHelpers != NULL) && dyld::gProcessInfo->libSystemInitialized ) { void* p = dyld::gLibSystemHelpers->malloc(size); //dyld::log("malloc(%lu) => %p from libSystem\n", size, p); return p; } else { + if ( size > DYLD_POOL_CHUNK_SIZE ) { + dyld::log("dyld malloc overflow: size=%zu\n", size); + exit(1); + } size = (size+sizeof(void*)-1) & (-sizeof(void*)); // pointer align uint8_t* result = currentPool->current; currentPool->current += size; @@ -101,8 +106,8 @@ void free(void* ptr) // ignore any pointer within dyld (i.e. stuff from pool or static strings) if ( (dyld::gLibSystemHelpers != NULL) && ((ptr < &__dso_handle) || (ptr >= &initialPoolContent[DYLD_POOL_CHUNK_SIZE])) ) { // ignore stuff in any dynamically alloated dyld pools - for (dyld_static_pool* p = initialPool.previousPool; p != NULL; p = p->previousPool) { - if ( (p->pool < ptr) && (ptr < p->end) ) { + for (dyld_static_pool* p = currentPool; p != NULL; p = p->previousPool) { + if ( (p->pool <= ptr) && (ptr < p->end) ) { // do nothing, pool entries can't be reclaimed //dyld::log("free(%p) from dynamic pool\n", ptr); return; @@ -127,7 +132,13 @@ void* calloc(size_t count, size_t size) return result; } else { - return malloc(count*size); + // Check for overflow of integer multiplication + size_t total = count * size; + if ( total/count != size ) { + dyld::log("dyld calloc overflow: count=%zu, size=%zu\n", count, size); + exit(1); + } + return malloc(total); } } @@ -147,3 +158,26 @@ extern "C" int _malloc_lock; int _malloc_lock = 0; +// dyld calls this which uses libSystem.dylib's vm_allocate if available +int vm_alloc(vm_address_t* addr, vm_size_t size, uint32_t flags) +{ + if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 12) ) { + return dyld::gLibSystemHelpers->vm_alloc(mach_task_self(), addr, size, flags); + } + else { + return ::vm_allocate(mach_task_self(), addr, size, flags); + } +} + +void* xmmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset) +{ + if ( (dyld::gLibSystemHelpers != NULL) && (dyld::gLibSystemHelpers->version >= 12) ) { + return dyld::gLibSystemHelpers->mmap(addr, len, prot, flags, fd, offset); + } + else { + return ::mmap(addr, len, prot, flags, fd, offset); + } +} + + +