From f2095d2917fb90c1f38a6c2112ad0539131070fd Mon Sep 17 00:00:00 2001 From: esrrhs Date: Mon, 30 Nov 2026 16:01:59 +0800 Subject: [PATCH] fix: unload OpenSSL providers before the ASAN leak check Leak detection now replaces flua's exit code when providers are still loaded, and a few tests left the VarInterface tables they allocated. Co-authored-by: Cursor --- src/native/crypto/native_crypto.cpp | 33 ++++++++++++++++++++++-- test/test_jitter.cpp | 40 +++++++++++++++++++---------- test/test_native.cpp | 5 ++++ 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/native/crypto/native_crypto.cpp b/src/native/crypto/native_crypto.cpp index 8670c55c..faa3f989 100644 --- a/src/native/crypto/native_crypto.cpp +++ b/src/native/crypto/native_crypto.cpp @@ -11,10 +11,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -544,6 +546,31 @@ static CVar CryptoDecryptEvp(State *s, CVar *args, int n) { return inter::NativeToFakeluaString(s, std::string(out.begin(), out.end())); } +#if OPENSSL_VERSION_MAJOR >= 3 +namespace { + +OSSL_PROVIDER *g_legacy_provider = nullptr; +OSSL_PROVIDER *g_default_provider = nullptr; + +// LeakSanitizer runs from an atexit handler installed at process start, so it +// observes whatever OpenSSL has not freed yet and then replaces the process +// exit code. flua's set_exit / update return value is that exit code. Unload +// the providers we loaded before the check runs. +void ReleaseCryptoProviders() { + if (g_legacy_provider != nullptr) { + OSSL_PROVIDER_unload(g_legacy_provider); + g_legacy_provider = nullptr; + } + if (g_default_provider != nullptr) { + OSSL_PROVIDER_unload(g_default_provider); + g_default_provider = nullptr; + } + OPENSSL_cleanup(); +} + +}// namespace +#endif + void RegisterCryptoLibraryApi(State *s) { if (!s) return; @@ -554,7 +581,8 @@ void RegisterCryptoLibraryApi(State *s) { static std::once_flag legacy_provider_init; std::call_once(legacy_provider_init, [s]() { #if OPENSSL_VERSION_MAJOR >= 3 - if (OSSL_PROVIDER_load(nullptr, "legacy") == nullptr) { + g_legacy_provider = OSSL_PROVIDER_load(nullptr, "legacy"); + if (g_legacy_provider == nullptr) { // Most common cause: libcrypto cannot find its provider modules // (ossl-modules/legacy), e.g. a static OpenSSL whose MODULESDIR is wrong. LOG_WARN(s, "crypto", @@ -562,7 +590,8 @@ void RegisterCryptoLibraryApi(State *s) { "and the matching crypto.encrypt/decrypt algorithms will fail. " "check OPENSSL_MODULES or the OpenSSL modules directory"); } - OSSL_PROVIDER_load(nullptr, "default"); + g_default_provider = OSSL_PROVIDER_load(nullptr, "default"); + std::atexit(ReleaseCryptoProviders); #endif }); diff --git a/test/test_jitter.cpp b/test/test_jitter.cpp index 59c892f7..f7ac4938 100644 --- a/test/test_jitter.cpp +++ b/test/test_jitter.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -25,9 +26,10 @@ using namespace fakelua; -// Helper: convert CVar table to VarInterface* for test verification. -static VarInterface *TableToVi(State *s, const CVar &cv) { - return inter::FakeluaToNativeObj(s, cv); +// Helper: convert CVar table to an owned VarInterface for test verification. +// FakeluaToNativeObj hands back a heap object; the caller has to delete it. +static std::unique_ptr TableToVi(State *s, const CVar &cv) { + return std::unique_ptr(inter::FakeluaToNativeObj(s, cv)); } // Helper: find value VarInterface* by int key in a VarInterface table. @@ -72,6 +74,16 @@ static std::string ViTableGetStr(VarInterface *t, int64_t key) { return v && v->ViGetType() == VarInterface::Type::STRING ? std::string(v->ViGetString()) : ""; } +template +static int64_t ViTableGetInt(const std::unique_ptr &t, const Key &key) { + return ViTableGetInt(t.get(), key); +} + +template +static std::string ViTableGetStr(const std::unique_ptr &t, const Key &key) { + return ViTableGetStr(t.get(), key); +} + static void JitterRunHelper(const std::function &f) { const auto s = FakeluaNewState(); ASSERT_NE(s, nullptr); @@ -218,7 +230,7 @@ TEST(jitter, multi_return_table_expand) { CompileFile(s, "./jit/test_multi_return_table_expand.lua", {.debug_mode = debug_mode}); CVar ret; Call(s, type, "test", ret); - VarInterface *t = TableToVi(s, ret); + auto t = TableToVi(s, ret); ASSERT_EQ(t->ViGetTableSize(), 4); ASSERT_EQ(ViTableGetInt(t, 1), 1); ASSERT_EQ(ViTableGetInt(t, 2), 2); @@ -232,7 +244,7 @@ TEST(jitter, multi_return_table_truncate) { CompileFile(s, "./jit/test_multi_return_table_truncate.lua", {.debug_mode = debug_mode}); CVar ret; Call(s, type, "test", ret); - VarInterface *t = TableToVi(s, ret); + auto t = TableToVi(s, ret); ASSERT_EQ(t->ViGetTableSize(), 4); ASSERT_EQ(ViTableGetInt(t, 1), 1); ASSERT_EQ(ViTableGetInt(t, 2), 2); @@ -246,7 +258,7 @@ TEST(jitter, multi_return_table_keyed_val) { CompileFile(s, "./jit/test_multi_return_table_keyed_val.lua", {.debug_mode = debug_mode}); CVar ret; Call(s, type, "test", ret); - VarInterface *t = TableToVi(s, ret); + auto t = TableToVi(s, ret); ASSERT_EQ(ViTableGetInt(t, "a"), 3); // key "b" not present → sentinel ASSERT_EQ(ViTableGetInt(t, "b"), INT64_MIN); @@ -258,7 +270,7 @@ TEST(jitter, multi_return_table_keyed_exp) { CompileFile(s, "./jit/test_multi_return_table_keyed_exp.lua", {.debug_mode = debug_mode}); CVar ret; Call(s, type, "test", ret); - VarInterface *t = TableToVi(s, ret); + auto t = TableToVi(s, ret); // Keys are integers: 3 -> "hello", 4 -> nil (not present) ASSERT_EQ(ViTableGetStr(t, 3), "hello"); ASSERT_EQ(ViTableGetStr(t, 4), ""); @@ -274,7 +286,7 @@ TEST(jitter, multi_return_table_indexing) { ASSERT_EQ(ConstString::GetString(ret.data_.i), "val3"); Call(s, type, "test_set", ret); - VarInterface *t = TableToVi(s, ret); + auto t = TableToVi(s, ret); // Keys are integers: 3 -> "inserted", 4 -> nil (not present) ASSERT_EQ(ViTableGetStr(t, 3), "inserted"); ASSERT_EQ(ViTableGetStr(t, 4), ""); @@ -603,19 +615,19 @@ TEST(jitter, test_const_table) { VarMulti *m = ret.data_.m; ASSERT_EQ(m->GetCount(), 3); - VarInterface *t1 = TableToVi(s, m->GetVars()[0]); + auto t1 = TableToVi(s, m->GetVars()[0]); ASSERT_EQ(t1->ViGetTableSize(), 10); for (int i = 1; i <= 10; ++i) { ASSERT_EQ(ViTableGetInt(t1, static_cast(i)), i); } - VarInterface *t2 = TableToVi(s, m->GetVars()[1]); + auto t2 = TableToVi(s, m->GetVars()[1]); ASSERT_EQ(t2->ViGetTableSize(), 3); ASSERT_EQ(ViTableGetInt(t2, "a"), 1); ASSERT_EQ(ViTableGetInt(t2, "b"), 2); ASSERT_EQ(ViTableGetInt(t2, "c"), 3); - VarInterface *t3 = TableToVi(s, m->GetVars()[2]); + auto t3 = TableToVi(s, m->GetVars()[2]); ASSERT_EQ(t3->ViGetTableSize(), 5); ASSERT_EQ(ViTableGetInt(t3, 1), 1); ASSERT_EQ(ViTableGetInt(t3, 2), 3); @@ -631,7 +643,7 @@ TEST(jitter, test_const_nested_table) { CVar ret; Call(s, type, "test", ret); - VarInterface *t = TableToVi(s, ret); + auto t = TableToVi(s, ret); ASSERT_EQ(t->ViGetType(), VarInterface::Type::TABLE); // Get nested "array" table @@ -2016,7 +2028,7 @@ TEST(jitter, test_table_zero_key) { CompileFile(s, "./jit/test_table_zero_key.lua", {.debug_mode = debug_mode}); CVar ret; Call(s, type, "test", ret); - VarInterface *t = TableToVi(s, ret); + auto t = TableToVi(s, ret); ASSERT_EQ(t->ViGetTableSize(), 1); ASSERT_EQ(ViTableGetInt(t, 0), 100); }); @@ -2822,7 +2834,7 @@ TEST(jitter, test_const_complex_expr) { ASSERT_EQ(m->GetVars()[2].data_.i, -20); // d = { val = -20 } - VarInterface *t = TableToVi(s, m->GetVars()[3]); + auto t = TableToVi(s, m->GetVars()[3]); ASSERT_EQ(t->ViGetTableSize(), 1); ASSERT_EQ(ViTableGetInt(t, "val"), -20); }); diff --git a/test/test_native.cpp b/test/test_native.cpp index 9cdb0006..878e8a0f 100644 --- a/test/test_native.cpp +++ b/test/test_native.cpp @@ -333,6 +333,11 @@ TEST(test_native, test_native_var_interface_table_result) { auto *res = new SimpleVarImpl(); res->ViSetTable(kv); + // ViSetTable clones; the inputs stay owned by the callback. + for (auto &[k, v]: kv) { + delete k; + delete v; + } return res; }));