Add an implementation of mifki's sizecheck library (tested on Linux)

develop
lethosor 2020-02-08 21:17:53 -05:00
parent b375586b67
commit f54c361718
4 changed files with 56 additions and 1 deletions

@ -540,3 +540,9 @@ endif()
# Store old build arch
set(DFHACK_BUILD_ARCH_PREV "${DFHACK_BUILD_ARCH}" CACHE STRING "Previous build architecture" FORCE)
option(BUILD_SIZECHECK "Build the sizecheck library, for research" OFF)
if(BUILD_SIZECHECK)
add_subdirectory(depends/sizecheck)
add_dependencies(dfhack sizecheck)
endif()

@ -0,0 +1,6 @@
project(sizecheck)
add_library(sizecheck SHARED sizecheck.cpp)
ide_folder(sizecheck "Depends")
install(TARGETS sizecheck
LIBRARY DESTINATION ${DFHACK_LIBRARY_DESTINATION}
RUNTIME DESTINATION ${DFHACK_LIBRARY_DESTINATION})

@ -0,0 +1,43 @@
// adapted from https://github.com/mifki/df-sizecheck/blob/master/b.cpp
// usage:
// linux: PRELOAD_LIB=hack/libsizecheck.so ./dfhack
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
using namespace std;
const uint32_t MAGIC = 0xdfdf4ac8;
void* alloc(size_t n) {
void* addr;
if (posix_memalign(&addr, 32, n + 16) != 0) {
return addr;
}
memset(addr, 0, 16);
*(size_t*)addr = n;
*(uint32_t*)((uint8_t*)addr + 8) = MAGIC;
return (uint8_t*)addr + 16;
}
void dealloc(void* addr) {
if (intptr_t(addr) % 32 == 16 && *(size_t*)((uint8_t*)addr - 8) == MAGIC) {
addr = (void*)((uint8_t*)addr - 16);
}
free(addr);
}
void* operator new (size_t n, const nothrow_t& tag) {
return alloc(n);
}
void* operator new (size_t n) {
return alloc(n);
}
void operator delete (void* addr) {
return dealloc(addr);
}

@ -117,7 +117,7 @@ void Deinit()
size_t detect_size(void *addr) {
size_t *size = (size_t*)((char*)addr - 16);
int32_t *tag = (int32_t*)((char*)addr - 8);
if (isAddr(size, memdata.ranges) && *tag == 0x11223344) {
if (isAddr(size, memdata.ranges) && (*tag == 0x11223344 || *tag == 0xdfdf4ac8)) {
return *size;
}
// default