Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/native/crypto/native_crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
#include <boost/uuid.hpp>
#include <climits>
#include <cstdint>
#include <cstdlib>
#include <format>
#include <iterator>
#include <memory>
#include <mutex>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/opensslv.h>
#include <string>
Expand Down Expand Up @@ -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;

Expand All @@ -554,15 +581,17 @@ 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",
"failed to load the OpenSSL \"legacy\" provider: crypto.rc4 / blowfish / des "
"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
});

Expand Down
40 changes: 26 additions & 14 deletions test/test_jitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <atomic>
#include <cstdlib>
#include <filesystem>
#include <memory>
#include <fstream>
#include <string>
#include <thread>
Expand All @@ -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<VarInterface> TableToVi(State *s, const CVar &cv) {
return std::unique_ptr<VarInterface>(inter::FakeluaToNativeObj(s, cv));
}

// Helper: find value VarInterface* by int key in a VarInterface table.
Expand Down Expand Up @@ -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 <typename Key>
static int64_t ViTableGetInt(const std::unique_ptr<VarInterface> &t, const Key &key) {
return ViTableGetInt(t.get(), key);
}

template <typename Key>
static std::string ViTableGetStr(const std::unique_ptr<VarInterface> &t, const Key &key) {
return ViTableGetStr(t.get(), key);
}

static void JitterRunHelper(const std::function<void(State *, JITType, bool)> &f) {
const auto s = FakeluaNewState();
ASSERT_NE(s, nullptr);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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), "");
Expand All @@ -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), "");
Expand Down Expand Up @@ -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<int64_t>(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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down
5 changes: 5 additions & 0 deletions test/test_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}));

Expand Down
Loading