Skip to content

Commit

Permalink
Added debug code to debug dladdr of function pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
meister committed Jun 30, 2024
1 parent c3e6294 commit bce1c6d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
18 changes: 4 additions & 14 deletions include/clasp/gctools/snapshotSaveLoad.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace snapshotSaveLoad {
extern bool global_debugSnapshot;
extern bool global_InSnapshotLoad;

enum PointerType { UninitializedPointer = 0, EndPointer = 1, FunctionPointer = 2, VtablePointer = 3 };
enum PointerType { UninitializedPointer = '?', EndPointer = 'E', FunctionPointer = 'f', VtablePointer = 'v' };

struct PointerBase {
PointerType _pointerType;
Expand Down Expand Up @@ -83,19 +83,9 @@ struct Fixup {
uintptr_t fixedAddress(bool functionP, uintptr_t* ptrptr, const char* addressName);
size_t ensureLibraryRegistered(uintptr_t address);

void registerVtablePointer(size_t libraryIndex, core::T_O* vtablePtrPtr) {
this->_Libraries[libraryIndex]._InternalPointers.emplace_back(VtablePointer, (uintptr_t*)vtablePtrPtr,
*(uintptr_t*)vtablePtrPtr);
};
void registerVtablePointer(size_t libraryIndex, core::T_O* vtablePtrPtr);

void registerFunctionPointer(size_t libraryIndex, uintptr_t* functionPtrPtr) {
if (libraryIndex > LIBRARY_ID_MAX) {
printf("%s:%d:%s The library id %lu is too large - change the pointer coding scheme to add more bits to the library id\n",
__FILE__, __LINE__, __FUNCTION__, libraryIndex);
abort();
}
this->_Libraries[libraryIndex]._InternalPointers.emplace_back(FunctionPointer, (uintptr_t*)functionPtrPtr, *functionPtrPtr);
};
void registerFunctionPointer(size_t libraryIndex, uintptr_t* functionPtrPtr, const char* location);

void addAddressName(void* address, std::string name) {
if (this->_trackAddressName) {
Expand Down Expand Up @@ -125,7 +115,7 @@ void clearLibraries();
void encodeEntryPointInLibrary(Fixup* fixup, uintptr_t* ptrptr);
void decodeEntryPointInLibrary(Fixup* fixup, uintptr_t* ptrptr);

void encodeEntryPoint(Fixup* fixup, uintptr_t* ptrptr, core::T_sp code);
void encodeEntryPoint(Fixup* fixup, uintptr_t* ptrptr, core::T_sp code, core::FunctionDescription_sp functionDescription );
void decodeEntryPoint(Fixup* fixup, uintptr_t* ptrptr, core::T_sp code);

struct LibraryLookup {
Expand Down
4 changes: 2 additions & 2 deletions src/core/function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void SimpleFun_O::fixupOneCodePointer(snapshotSaveLoad::Fixup* fixup, void** ptr
}
} else if (snapshotSaveLoad::operation(fixup) == snapshotSaveLoad::SaveOp) {
uintptr_t* ptrptr = (uintptr_t*)&ptr[0];
snapshotSaveLoad::encodeEntryPoint(fixup, ptrptr, this->_Code);
snapshotSaveLoad::encodeEntryPoint(fixup, ptrptr, this->_Code, this->_FunctionDescription );
} else if (snapshotSaveLoad::operation(fixup) == snapshotSaveLoad::LoadOp) {
uintptr_t* ptrptr = (uintptr_t*)&ptr[0];
snapshotSaveLoad::decodeEntryPoint(fixup, ptrptr, this->_Code);
Expand Down Expand Up @@ -184,7 +184,7 @@ void CoreFun_O::fixupInternalsForSnapshotSaveLoad(snapshotSaveLoad::Fixup* fixup
abort();
}
uintptr_t* ptrptr = (uintptr_t*)&ptr[0];
snapshotSaveLoad::encodeEntryPoint(fixup, ptrptr, this->_Code);
snapshotSaveLoad::encodeEntryPoint(fixup, ptrptr, this->_Code, this->_FunctionDescription );
} else if (snapshotSaveLoad::operation(fixup) == snapshotSaveLoad::LoadOp) {
uintptr_t* ptrptr = (uintptr_t*)&ptr[0];
snapshotSaveLoad::decodeEntryPoint(fixup, ptrptr, this->_Code);
Expand Down
57 changes: 47 additions & 10 deletions src/gctools/snapshotSaveLoad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#define DEBUG_LEVEL_FULL

//#define DEBUG_ENTRY_POINTS 1

// #include <llvm/Support/system_error.h>
#include <dlfcn.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -592,6 +594,30 @@ bool virtualMethodP(uintptr_t* ptrptr) {
return false;
}


void Fixup::registerVtablePointer(size_t libraryIndex, core::T_O* vtablePtrPtr) {
this->_Libraries[libraryIndex]._InternalPointers.emplace_back(VtablePointer, (uintptr_t*)vtablePtrPtr,
*(uintptr_t*)vtablePtrPtr);
};

void Fixup::registerFunctionPointer(size_t libraryIndex, uintptr_t* functionPtrPtr, const char* location) {
if (libraryIndex > LIBRARY_ID_MAX) {
printf("%s:%d:%s The library id %lu is too large - change the pointer coding scheme to add more bits to the library id\n",
__FILE__, __LINE__, __FUNCTION__, libraryIndex);
abort();
}
this->_Libraries[libraryIndex]._InternalPointers.emplace_back(FunctionPointer, (uintptr_t*)functionPtrPtr, *functionPtrPtr);
#ifdef DEBUG_ENTRY_POINTS
printf("%s:%d:%s libraryIndex[%lu] functionPtrPtr @%p -> %p location: %s\n",
__FILE__, __LINE__, __FUNCTION__,
libraryIndex,
(void*)functionPtrPtr,
(void*)*functionPtrPtr,
location);
#endif
};


uintptr_t Fixup::fixedAddress(bool functionP, uintptr_t* ptrptr, const char* addressName) {
uint8_t firstByte;
uintptr_t libidx;
Expand Down Expand Up @@ -714,9 +740,9 @@ uintptr_t encodeEntryPointOffset(uintptr_t address, uintptr_t codeStart, uintptr
return offset;
}

void encodeEntryPointInLibrary(Fixup* fixup, uintptr_t* ptrptr) {
void encodeEntryPointInLibrary(Fixup* fixup, uintptr_t* ptrptr, const char* location) {
size_t libraryIndex = fixup->ensureLibraryRegistered(*ptrptr);
fixup->registerFunctionPointer(libraryIndex, ptrptr);
fixup->registerFunctionPointer(libraryIndex, ptrptr, location);
}

void decodeEntryPointInLibrary(Fixup* fixup, uintptr_t* ptrptr) {
Expand Down Expand Up @@ -770,20 +796,25 @@ bool decodeEntryPointForCompiledCode(Fixup* fixup, uintptr_t* ptrptr, llvmo::Obj
return true;
}

void encodeEntryPoint(Fixup* fixup, uintptr_t* ptrptr, core::T_sp codebase) {
void encodeEntryPoint(Fixup* fixup, uintptr_t* ptrptr, core::T_sp codebase, core::FunctionDescription_sp functionDescription ) {
if (virtualMethodP(ptrptr))
return;
if (gc::IsA<llvmo::ObjectFile_sp>(codebase)) {
llvmo::ObjectFile_sp code = gc::As_unsafe<llvmo::ObjectFile_sp>(codebase);
if (!encodeEntryPointForCompiledCode(fixup, ptrptr, code)) {
// The entry point wasnt into the compiled code
// so it must be to one of the libraries - apply that fixup.
encodeEntryPointInLibrary(fixup, ptrptr);
encodeEntryPointInLibrary(fixup, ptrptr,"ObjectFile");
}
} else if (gc::IsA<llvmo::Library_sp>(codebase)) {
encodeEntryPointInLibrary(fixup, ptrptr);
encodeEntryPointInLibrary(fixup, ptrptr, "Library");
#ifdef DEBUG_ENTRY_POINTS
llvmo::Library_sp lib = gc::As<llvmo::Library_sp>(codebase);
printf("%s:%d:%s entryPoint library -> %s\n", __FILE__, __LINE__, __FUNCTION__, lib->_Name->get_std_string().c_str() );
printf(" function name -> %s\n", _rep_(functionDescription->_functionName).c_str() );
#endif
} else if (gc::IsA<core::BytecodeModule_sp>(codebase)) {
encodeEntryPointInLibrary(fixup, ptrptr);
encodeEntryPointInLibrary(fixup, ptrptr,"BytecodeModule");
} else {
printf("%s:%d:%s The codebase must be a Code_sp or a Library_sp it is %s\n", __FILE__, __LINE__, __FUNCTION__,
_rep_(codebase).c_str());
Expand Down Expand Up @@ -1512,7 +1543,7 @@ struct prepare_for_snapshot_save_t : public walker_callback_t {
// printf("%s:%d:%s [%lu] before target: %lu cast_function@%p: %p\n", __FILE__, __LINE__, __FUNCTION__, ii,
// (*edges)[ii].target, &(*edges)[ii].cast, (*edges)[ii].cast);
void** ptrptr = (void**)&(*edges)[ii].cast;
encodeEntryPointInLibrary(this->_fixup, (uintptr_t*)ptrptr);
encodeEntryPointInLibrary(this->_fixup, (uintptr_t*)ptrptr,"prepare_for_snapshot_save_t");
}
}
// Handle them on a case by case basis
Expand Down Expand Up @@ -2054,7 +2085,7 @@ struct LoadSymbolCallback : public core::SymbolCallback {
const char* myName = (const char*)&this->_Library._SymbolBuffer[offset];
if ((namelen == this->_Library._SymbolInfo[ii]._SymbolLength) && (strcmp(name, myName) == 0)) {
this->_Library._GroupedPointers[gpindex]._address = start;
#if 0
#ifdef DEBUG_ENTRY_POINTS
printf("%s:%d:%s GroupedPointers[%lu] saved address %p symbol address %p @%p\n name: %s\n",
__FILE__, __LINE__, __FUNCTION__,
gpindex,
Expand Down Expand Up @@ -2116,12 +2147,12 @@ struct LoadSymbolCallback : public core::SymbolCallback {
}
#endif
this->_Library._GroupedPointers[gpindex]._address = mysymStart;
#if 0
#ifdef DEBUG_ENTRY_POINTS
printf("%s:%d:%s GroupedPointers[%lu] restored address %p offset: %lu saved symbol address %p @%p\n name: %s\n",
__FILE__, __LINE__, __FUNCTION__,
gpindex,
(void*)dlsymStart,
this->_Library._SymbolInfo[ii]._AddressOffset,
(uintptr_t)this->_Library._SymbolInfo[ii]._AddressOffset,
(void*)this->_Library._SymbolInfo[ii]._Address,
(void*)&this->_Library._SymbolInfo[ii]._Address,
myName);
Expand Down Expand Up @@ -2160,6 +2191,12 @@ void prepareRelocationTableForSave(Fixup* fixup, SymbolLookup& symbolLookup) {
groupPointerIdx = curLib._GroupedPointers.size();
uniques[curLib._InternalPointers[ii]._address] = groupPointerIdx;
curLib._GroupedPointers.emplace_back(curLib._InternalPointers[ii]._pointerType, curLib._InternalPointers[ii]._address);
#ifdef DEBUG_ENTRY_POINTS
printf("%s:%d:%s emplace_back into GroupPointers[%lu] -> type: %c @%p\n", __FILE__, __LINE__, __FUNCTION__,
curLib._GroupedPointers.size(),
curLib._InternalPointers[ii]._pointerType,
(void*)curLib._InternalPointers[ii]._address );
#endif
} else {
groupPointerIdx = it->second;
}
Expand Down

0 comments on commit bce1c6d

Please sign in to comment.