diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-15 08:20:37 +0800 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-15 08:20:37 +0800 |
| commit | ab86ae0284469a9a941f5b196f9cc99a90305e3a (patch) | |
| tree | 2e22e00e6b2c9465be964a58e10dd71f9aa457b4 /tools | |
| parent | bb64298ef26c9b42ae1bb50fb92b15e509b48c07 (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/driver/EmbeddedRunner.cc | 13 |
1 files changed, 10 insertions, 3 deletions
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 <cstdio> +#include <cstdlib> #include <cxxabi.h> #include <typeinfo> #include <unwind.h> -// 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<void *>(&__ms_snprintf)); + DynamicLibrary::AddSymbol("strtoll", reinterpret_cast<void *>(&std::strtoll)); +#endif DynamicLibrary::AddSymbol("__cxa_allocate_exception", reinterpret_cast<void *>(&__cxxabiv1::__cxa_allocate_exception)); DynamicLibrary::AddSymbol("__cxa_throw", reinterpret_cast<void *>(&__cxxabiv1::__cxa_throw)); DynamicLibrary::AddSymbol("__cxa_begin_catch", reinterpret_cast<void *>(&__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())); |
