From d4166a5c8d843d9a1ee3bf4522e165e22f399db9 Mon Sep 17 00:00:00 2001 From: Elias Bachaalany Date: Wed, 26 Nov 2025 17:56:48 -0800 Subject: [PATCH 1/2] cmake: add SOGEN_STATIC_CRT option for static runtime linking Add option to use static CRT (/MT) instead of dynamic (/MD) for projects that require static linking, such as IDA Pro plugins. - Default remains /MD (no change for existing users) - Set -DSOGEN_STATIC_CRT=ON to use /MT - Also respects parent's CMAKE_MSVC_RUNTIME_LIBRARY if already set Includes warning about potential heap corruption when allocations cross library boundaries with static CRT. --- cmake/compiler-env.cmake | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/cmake/compiler-env.cmake b/cmake/compiler-env.cmake index 6ec71ddc..a4b85e54 100644 --- a/cmake/compiler-env.cmake +++ b/cmake/compiler-env.cmake @@ -217,10 +217,26 @@ if(MOMO_ENABLE_SANITIZER) endif() ########################################## -# Must be a dynamic runtime (/MD or /MDd) to enforce -# shared allocators between emulator and implementation +# MSVC Runtime Library Selection +# +# Default is dynamic runtime (/MD or /MDd) to enforce shared allocators +# between emulator and implementation. +# +# Use SOGEN_STATIC_CRT=ON for static runtime (/MT or /MTd) when embedding +# in projects that require it (e.g., IDA plugins). +# +# WARNING: Static CRT may cause heap corruption if memory is allocated +# in one module and freed in another. Ensure allocation ownership is clear. -set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$:Debug>DLL) +option(SOGEN_STATIC_CRT "Use static CRT (/MT) instead of dynamic (/MD)" OFF) + +if(SOGEN_STATIC_CRT) + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") +elseif(DEFINED CMAKE_MSVC_RUNTIME_LIBRARY) + # Respect parent project's setting +else() + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") +endif() ########################################## From 7a42bc7ad3d1c30f79af5214e413046c52055191 Mon Sep 17 00:00:00 2001 From: Elias Bachaalany Date: Tue, 2 Dec 2025 16:24:27 -0800 Subject: [PATCH 2/2] cmake: rename MOMO_BUILD_AS_LIBRARY to SOGEN_BUILD_STATIC Also adds FATAL_ERROR guard when SOGEN_STATIC_CRT=ON without SOGEN_BUILD_STATIC=ON, since static CRT with shared libraries causes heap corruption (each DLL gets its own allocator but sogen passes ownership across boundaries). These options are designed to be used together for full static linking, useful for embedding sogen in projects like IDA Pro plugins. --- CMakeLists.txt | 2 +- cmake/compiler-env.cmake | 13 ++++++++++--- src/CMakeLists.txt | 2 +- src/backends/icicle-emulator/CMakeLists.txt | 2 +- .../icicle-emulator/icicle_x86_64_emulator.hpp | 2 +- src/backends/unicorn-emulator/CMakeLists.txt | 2 +- .../unicorn-emulator/unicorn_x86_64_emulator.hpp | 2 +- 7 files changed, 16 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 342aa659..8415adfb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,7 @@ endif() ########################################## -option(MOMO_BUILD_AS_LIBRARY "Configure and Build the sogen as a shared library (without the samples and tests)" ${MOMO_IS_SUBPROJECT}) +option(SOGEN_BUILD_STATIC "Build sogen as static libraries for embedding (e.g., IDA plugins)" ${MOMO_IS_SUBPROJECT}) ########################################## diff --git a/cmake/compiler-env.cmake b/cmake/compiler-env.cmake index a4b85e54..4ea6fe60 100644 --- a/cmake/compiler-env.cmake +++ b/cmake/compiler-env.cmake @@ -31,10 +31,10 @@ endif() ########################################## -if(MOMO_BUILD_AS_LIBRARY) - add_compile_definitions(MOMO_BUILD_AS_LIBRARY=1) +if(SOGEN_BUILD_STATIC) + add_compile_definitions(SOGEN_BUILD_STATIC=1) else() - add_compile_definitions(MOMO_BUILD_AS_LIBRARY=0) + add_compile_definitions(SOGEN_BUILD_STATIC=0) endif() ########################################## @@ -230,6 +230,13 @@ endif() option(SOGEN_STATIC_CRT "Use static CRT (/MT) instead of dynamic (/MD)" OFF) +if(SOGEN_STATIC_CRT AND NOT SOGEN_BUILD_STATIC) + message(FATAL_ERROR + "SOGEN_STATIC_CRT=ON requires SOGEN_BUILD_STATIC=ON.\n" + "Static CRT with shared libraries causes heap corruption - " + "each DLL gets its own allocator, but sogen passes ownership across boundaries.") +endif() + if(SOGEN_STATIC_CRT) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") elseif(DEFINED CMAKE_MSVC_RUNTIME_LIBRARY) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 300b763d..3ddaf3a2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,7 +8,7 @@ add_subdirectory(backend-selection) momo_add_subdirectory_and_get_targets("backends" BACKEND_TARGETS) momo_targets_set_folder("backends" ${BACKEND_TARGETS}) -if (NOT MOMO_BUILD_AS_LIBRARY) +if (NOT SOGEN_BUILD_STATIC) add_subdirectory(analyzer) add_subdirectory(debugger) add_subdirectory(fuzzing-engine) diff --git a/src/backends/icicle-emulator/CMakeLists.txt b/src/backends/icicle-emulator/CMakeLists.txt index da62e3af..72cd7396 100644 --- a/src/backends/icicle-emulator/CMakeLists.txt +++ b/src/backends/icicle-emulator/CMakeLists.txt @@ -8,7 +8,7 @@ file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS list(SORT SRC_FILES) -if(MOMO_BUILD_AS_LIBRARY) +if(SOGEN_BUILD_STATIC) add_library(icicle-emulator STATIC ${SRC_FILES}) else() add_library(icicle-emulator SHARED ${SRC_FILES}) diff --git a/src/backends/icicle-emulator/icicle_x86_64_emulator.hpp b/src/backends/icicle-emulator/icicle_x86_64_emulator.hpp index bc2ab6fb..4bf121b4 100644 --- a/src/backends/icicle-emulator/icicle_x86_64_emulator.hpp +++ b/src/backends/icicle-emulator/icicle_x86_64_emulator.hpp @@ -12,7 +12,7 @@ namespace icicle { -#if !MOMO_BUILD_AS_LIBRARY +#if !SOGEN_BUILD_STATIC ICICLE_EMULATOR_DLL_STORAGE #endif std::unique_ptr create_x86_64_emulator(); diff --git a/src/backends/unicorn-emulator/CMakeLists.txt b/src/backends/unicorn-emulator/CMakeLists.txt index 8b44e286..c2b24557 100644 --- a/src/backends/unicorn-emulator/CMakeLists.txt +++ b/src/backends/unicorn-emulator/CMakeLists.txt @@ -6,7 +6,7 @@ file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS list(SORT SRC_FILES) -if(MOMO_BUILD_AS_LIBRARY) +if(SOGEN_BUILD_STATIC) add_library(unicorn-emulator STATIC ${SRC_FILES}) else() add_library(unicorn-emulator SHARED ${SRC_FILES}) diff --git a/src/backends/unicorn-emulator/unicorn_x86_64_emulator.hpp b/src/backends/unicorn-emulator/unicorn_x86_64_emulator.hpp index a64b399f..84fbf62f 100644 --- a/src/backends/unicorn-emulator/unicorn_x86_64_emulator.hpp +++ b/src/backends/unicorn-emulator/unicorn_x86_64_emulator.hpp @@ -12,7 +12,7 @@ namespace unicorn { -#if !MOMO_BUILD_AS_LIBRARY +#if !SOGEN_BUILD_STATIC UNICORN_EMULATOR_DLL_STORAGE #endif std::unique_ptr create_x86_64_emulator();