From ab86ae0284469a9a941f5b196f9cc99a90305e3a Mon Sep 17 00:00:00 2001 From: _ov4 Date: Thu, 15 Oct 2026 08:20:37 +0800 Subject: fix(codegen): prevent NUM_TO_STR buffer overflow Size REAL buffers for the full fixed-point double range and bound writes with snprintf. Register MinGW CRT aliases for embedded execution. Co-Authored-By: Claude Fable 5.1 --- tools/driver/EmbeddedRunner.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'tools/driver/EmbeddedRunner.cc') diff --git a/tools/driver/EmbeddedRunner.cc b/tools/driver/EmbeddedRunner.cc index a654d5a..cad1f84 100644 --- a/tools/driver/EmbeddedRunner.cc +++ b/tools/driver/EmbeddedRunner.cc @@ -8,11 +8,13 @@ #include "llvm/TargetParser/Host.h" #include "llvm/TargetParser/Triple.h" #include "llvm/Transforms/Utils/Cloning.h" +#include +#include #include #include #include -// Static C++ runtime symbols are not available through dlsym. Register their +// Static runtime symbols are not available through dlsym. Register their // actual addresses so the JIT and its unwind tables use the host's runtime. extern "C" int #if defined(__SEH__) @@ -24,8 +26,13 @@ __gxx_personality_v0 #endif (int, _Unwind_Action, _Unwind_Exception_Class, _Unwind_Exception *, _Unwind_Context *); -static void registerExceptionSymbols() { +static void registerRuntimeSymbols() { using llvm::sys::DynamicLibrary; +#ifdef __MINGW32__ + // Use the same CRT aliases as native IR linking, including its stdio formatting. + DynamicLibrary::AddSymbol("snprintf", reinterpret_cast(&__ms_snprintf)); + DynamicLibrary::AddSymbol("strtoll", reinterpret_cast(&std::strtoll)); +#endif DynamicLibrary::AddSymbol("__cxa_allocate_exception", reinterpret_cast(&__cxxabiv1::__cxa_allocate_exception)); DynamicLibrary::AddSymbol("__cxa_throw", reinterpret_cast(&__cxxabiv1::__cxa_throw)); DynamicLibrary::AddSymbol("__cxa_begin_catch", reinterpret_cast(&__cxxabiv1::__cxa_begin_catch)); @@ -52,7 +59,7 @@ int runEmbedded(const cps::CodeGen &CG) { llvm::errs() << "error: cannot load process symbols: " << Error << "\n"; return 1; } - registerExceptionSymbols(); + registerRuntimeSymbols(); // Keep CodeGen's module and context alive while the engine owns its clone. auto Module = llvm::CloneModule(CG.getModule()); Module->setTargetTriple(llvm::Triple(llvm::sys::getProcessTriple())); -- cgit v1.3.1-4-g156e