xsscx / macos-research

Welcome to Hoyt's macOS Fuzzing & Code Repo. Contribute Code or Open an Issue or Discussion.
https://srd.cx
GNU General Public License v3.0
38 stars 2 forks source link

Add Global debug flag | Anonymize memory address for logging purposes #2

Closed xsscx closed 10 months ago

xsscx commented 10 months ago

Changes contained in the modified main.cpp, instrumentation.cpp and instrumentation.h

Changes Proposed to Stub main.cpp

Discussion & Analysis

void DebugBreakpoint(const std::string& message) { if (debugMode) { std::cout << "[DEBUG BREAK] " << message << "\n"; std::cout << "Press enter to continue...\n"; std::cin.get(); } }

void SignalHandler(int signal) { std::cout << "Caught signal " << signal << ". Entering debug mode.\n"; debugMode = true; }

void SetupDebugMode() { signal(SIGINT, SignalHandler); }

### Anonymize memory address for logging purposes in instrumentation.cpp

std::string AnonymizeAddress(void* addr);

// Debugging aids static void DebugBreakpoint(const std::string& message); static void SignalHandler(int signal); static void SetupDebugMode();

private: // Flag to control debug mode static bool debugMode;

### Proposed modifications to instrumentation.cpp

bool Instrumentation::debugMode = true;

std::string Instrumentation::AnonymizeAddress(void* addr) { char buf[20]; snprintf(buf, sizeof(buf), "%p", addr);

if (!strcmp(buf, "(nil)")) {
    std::cerr << "[" << __TIME__ << "] AnonymizeAddress: Address is nil" << std::endl;
    return std::string("0");
}

int addr_start = (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) ? 2 : 0;
int len = static_cast<int>(strlen(buf));
int firstnonzero = len;
for (int i = addr_start; i < len; i++) {
    if (buf[i] != '0') {
        firstnonzero = i;
        break;
    }
}

assert(firstnonzero < len); // Sanity check

for (int i = firstnonzero; i < len - 3; i++) {
    buf[i] = 'x';
}

std::string anonymizedAddr(buf);
std::cerr << "[" << __TIME__ << "] AnonymizeAddress: Original: " << addr
          << ", Anonymized: " << anonymizedAddr << std::endl;
return anonymizedAddr;

}