From 9d4b5cf03477af91c5623d48abc28c053f05e98c Mon Sep 17 00:00:00 2001 From: Andrew Haberlandt Date: Fri, 27 Dec 2024 05:30:16 -0500 Subject: [PATCH] chore: preprocessor macros toward building on macOS --- pyda_core/CMakeLists.txt | 6 +++++- pyda_core/pyda_core.c | 19 +++++++++++++------ pyda_core/pyda_core_py.c | 2 +- pyda_core/pyda_patch_python.c | 2 ++ pyda_core/pyda_threads.c | 6 ++++++ pyda_core/pyda_unwind.c | 30 ++++++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/pyda_core/CMakeLists.txt b/pyda_core/CMakeLists.txt index 4155d38..6ed8e00 100644 --- a/pyda_core/CMakeLists.txt +++ b/pyda_core/CMakeLists.txt @@ -14,7 +14,11 @@ if (NOT Python3_FOUND) endif(NOT Python3_FOUND) target_include_directories(tool PRIVATE ${Python3_INCLUDE_DIRS}) -target_link_libraries(tool ${Python3_LIBRARIES} unwind) +if (APPLE) + target_link_libraries(tool ${Python3_LIBRARIES}) +else() + target_link_libraries(tool ${Python3_LIBRARIES} unwind) +endif() target_compile_options(tool PUBLIC -DPYDA_DYNAMORIO_CLIENT -Werror) configure_DynamoRIO_client(tool) diff --git a/pyda_core/pyda_core.c b/pyda_core/pyda_core.c index cc8c23b..595c9a7 100644 --- a/pyda_core/pyda_core.c +++ b/pyda_core/pyda_core.c @@ -3,7 +3,12 @@ #include "pyda_threads.h" #include "util.h" #include + +#ifdef LINUX #include +#elif MACOS +#include +#endif #define CONTEXT_STACK_LIMIT 10 @@ -50,11 +55,11 @@ pyda_process* pyda_mk_process() { // Setup locks, etc. pthread_condattr_t condattr; int ret; - if (ret = pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED)) { + if ((ret = pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED))) { dr_fprintf(STDERR, "pthread_condattr_setpshared failed: %d\n", ret); dr_abort(); } - if (ret = pthread_cond_init(&proc->thread_exit_cond, &condattr)) { + if ((ret = pthread_cond_init(&proc->thread_exit_cond, &condattr))) { dr_fprintf(STDERR, "pthread_cond_init failed %d\n", ret); dr_abort(); } @@ -63,7 +68,7 @@ pyda_process* pyda_mk_process() { pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); - if (ret = pthread_mutex_init(&proc->refcount_mutex, &attr)) { + if ((ret = pthread_mutex_init(&proc->refcount_mutex, &attr))) { dr_fprintf(STDERR, "pthread_mutex_init failed %d\n", ret); dr_abort(); } @@ -122,9 +127,11 @@ void pyda_capture_io(pyda_process *proc, int use_pty, int pty_raw) { } // Try to make a larger pipe (1M) +#ifdef LINUX if (fcntl(pipe1[0], F_SETPIPE_SZ, 1024*1024) || fcntl(pipe2[0], F_SETPIPE_SZ, 1024*1024) || fcntl(pipe3[0], F_SETPIPE_SZ, 1024*1024)) { DEBUG_PRINTF("Failed to set pipe size to 1M\n"); } +#endif dup2(pipe1[0], 0); dup2(pipe2[1], 1); @@ -172,11 +179,11 @@ pyda_thread* pyda_mk_thread(pyda_process *proc) { pthread_condattr_t condattr; pthread_condattr_init(&condattr); int ret; - if (ret = pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED)) { + if ((ret = pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED))) { dr_fprintf(STDERR, "pthread_condattr_setpshared failed: %d\n", ret); dr_abort(); } - if (ret = pthread_cond_init(&thread->resume_cond, &condattr)) { + if ((ret = pthread_cond_init(&thread->resume_cond, &condattr))) { dr_fprintf(STDERR, "pthread_cond_init failed %d\n", ret); dr_abort(); } @@ -203,7 +210,7 @@ pyda_thread* pyda_mk_thread(pyda_process *proc) { thread->yield_count = 0; - static volatile unsigned int tid = 0; + static volatile int tid = 0; thread->tid = dr_atomic_add32_return_sum(&tid, 1); thread->rip_updated_in_python = 0; thread->skip_next_hook = 0; diff --git a/pyda_core/pyda_core_py.c b/pyda_core/pyda_core_py.c index 560029d..f96815a 100644 --- a/pyda_core/pyda_core_py.c +++ b/pyda_core/pyda_core_py.c @@ -514,7 +514,7 @@ PydaProcess_get_register(PyObject *self, PyObject *args) { // Convert to decimal string char buf[64]; - if (snprintf(buf, sizeof(buf), "0x%lx%lx", val[1], val[0]) >= sizeof(buf)) { + if (snprintf(buf, sizeof(buf), "0x%" PRIx64 "%" PRIx64, val[1], val[0]) >= sizeof(buf)) { PyErr_SetString(PyExc_RuntimeError, "Internal error: reg buffer too small"); return NULL; } diff --git a/pyda_core/pyda_patch_python.c b/pyda_core/pyda_patch_python.c index 60ca3f4..be97489 100644 --- a/pyda_core/pyda_patch_python.c +++ b/pyda_core/pyda_patch_python.c @@ -22,7 +22,9 @@ static redirect_import_t python_redirect_imports[] = { { "pthread_detach", (app_pc)pyda_thread_detach }, { "dlopen", (app_pc)pyda_dlopen }, { "dlsym", (app_pc)pyda_dlsym }, +#ifdef LINUX { "getauxval", (app_pc)pyda_getauxval }, +#endif { "getenv", (app_pc)pyda_getenv } }; diff --git a/pyda_core/pyda_threads.c b/pyda_core/pyda_threads.c index 6527542..c95c084 100644 --- a/pyda_core/pyda_threads.c +++ b/pyda_core/pyda_threads.c @@ -8,7 +8,10 @@ #include "privload.h" #include "Python.h" #include "util.h" + +#ifdef LINUX #include +#endif // These are used by python as shims to dynamorio-safe pthread functions @@ -164,6 +167,7 @@ int pyda_thread_detach(pthread_t thread) { return 0; } +#ifdef LINUX extern size_t os_minsigstksz(void); unsigned long pyda_getauxval(unsigned long type) { @@ -174,6 +178,8 @@ unsigned long pyda_getauxval(unsigned long type) { return getauxval(type); } +#endif + int pyda_attach_mode; extern const char *our_getenv(const char *name); diff --git a/pyda_core/pyda_unwind.c b/pyda_core/pyda_unwind.c index 90c05da..cdc8f7b 100644 --- a/pyda_core/pyda_unwind.c +++ b/pyda_core/pyda_unwind.c @@ -14,6 +14,7 @@ int pyda_get_backtrace (pyda_thread *t, drvector_t *res) { unw_getcontext(&uc); ucontext_t *uc2 = (ucontext_t *) &uc; +#if defined(LINUX) #if defined(__x86_64__) uc2->uc_mcontext.gregs[REG_RIP] = (uintptr_t)t->cur_context.pc; uc2->uc_mcontext.gregs[REG_RSP] = t->cur_context.rsp; @@ -40,6 +41,35 @@ int pyda_get_backtrace (pyda_thread *t, drvector_t *res) { #error "Unsupported architecture" #endif +#elif defined(MACOS) /* continuation of defined(LINUX) */ +#if defined(__x86_64__) + uc2->uc_mcontext->__ss.__rip = (uintptr_t)t->cur_context.pc; + uc2->uc_mcontext->__ss.__rsp = t->cur_context.rsp; + uc2->uc_mcontext->__ss.__rbp = t->cur_context.rbp; + uc2->uc_mcontext->__ss.__rdi = t->cur_context.rdi; + uc2->uc_mcontext->__ss.__rsi = t->cur_context.rsi; + uc2->uc_mcontext->__ss.__rdx = t->cur_context.rdx; + uc2->uc_mcontext->__ss.__rcx = t->cur_context.rcx; + uc2->uc_mcontext->__ss.__r8 = t->cur_context.r8; + uc2->uc_mcontext->__ss.__r9 = t->cur_context.r8; +#elif defined(AARCH64) + uc2->uc_mcontext->__ss.__pc = (uintptr_t)t->cur_context.pc; + uc2->uc_mcontext->__ss.__x[0] = t->cur_context.r0; + uc2->uc_mcontext->__ss.__x[1] = t->cur_context.r1; + uc2->uc_mcontext->__ss.__x[2] = t->cur_context.r2; + uc2->uc_mcontext->__ss.__x[3] = t->cur_context.r3; + uc2->uc_mcontext->__ss.__x[4] = t->cur_context.r4; + uc2->uc_mcontext->__ss.__x[5] = t->cur_context.r5; + uc2->uc_mcontext->__ss.__x[6] = t->cur_context.r6; + uc2->uc_mcontext->__ss.__x[7] = t->cur_context.r7; + uc2->uc_mcontext->__ss.__sp = t->cur_context.sp; + uc2->uc_mcontext->__ss.__lr = t->cur_context.lr; +#else +#error "Unsupported architecture" +#endif + +#endif /* end of defined(MACOS) */ + unw_init_local(&cursor, &uc); drvector_init(res, 0, true, free_bt_entry);