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
35 changes: 26 additions & 9 deletions .github/workflows/build_with_macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,32 @@ jobs:
echo "Waiting for MySQL... ($i)"
sleep 2
done
# Set root password (Homebrew MySQL starts with empty password)
# Try without password first, then with 'root' if already set
MYSQL_PWD="" mysql -h 127.0.0.1 -uroot -e "SELECT 1" 2>/dev/null && echo "MYSQL_AUTH=empty" >> $GITHUB_ENV || {
MYSQL_PWD="root" mysql -h 127.0.0.1 -uroot -e "SELECT 1" 2>/dev/null && echo "MYSQL_AUTH=root" >> $GITHUB_ENV || {
echo "MYSQL_AUTH=failed" >> $GITHUB_ENV
}
}
# Create test database (required for integration tests)
MYSQL_PWD="" mysql -h 127.0.0.1 -uroot -e "CREATE DATABASE IF NOT EXISTS test" 2>/dev/null || true
# Homebrew MySQL starts with an empty root password. The workflow
# comment previously claimed to set it, but it only detected auth and
# created the database with MYSQL_PWD="". Tests require root/root on
# 127.0.0.1:3306 and database test (same contract as Linux/Windows).
SQL="
CREATE DATABASE IF NOT EXISTS test;
CREATE USER IF NOT EXISTS 'root'@'127.0.0.1' IDENTIFIED BY 'root';
ALTER USER 'root'@'127.0.0.1' IDENTIFIED BY 'root';
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;
FLUSH PRIVILEGES;
"
configured=0
for pwd in "" "root"; do
if MYSQL_PWD="$pwd" mysql -h 127.0.0.1 -uroot --protocol=tcp -e "$SQL"; then
configured=1
break
fi
done
if [ "$configured" -ne 1 ]; then
echo "Failed to configure MySQL test user (root/root) and database"
exit 1
fi
MYSQL_PWD="root" mysql -h 127.0.0.1 -uroot --protocol=tcp -e "SELECT 1"
echo "MySQL configured (root/root, database test)"

- name: Setup Redis
run: |
Expand Down
19 changes: 18 additions & 1 deletion .github/workflows/build_with_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,24 @@ jobs:

- name: Install MySQL
run: |
choco install mysql -y
# Unpinned `choco install mysql` follows latest. 26.10.0 (current
# latest) aborted mid-download from cdn.mysql.com on this PR with
# 404 / connection reset, even though the zip is published. Pin the
# last Approved 9.x package that still serves a live zip, and retry
# transient CDN failures. Start/Configure MySQL steps stay the same.
$version = "9.6.0"
$installed = $false
for ($i = 1; $i -le 3; $i++) {
Write-Host "Installing MySQL $version (attempt $i/3)"
choco install mysql --version=$version -y --no-progress
if ($LASTEXITCODE -eq 0) {
$installed = $true
break
}
Write-Host "MySQL install failed with exit $LASTEXITCODE"
if ($i -lt 3) { Start-Sleep -Seconds (15 * $i) }
}
if (-not $installed) { throw "Failed to install MySQL $version after 3 attempts" }
echo "MySQL installed"

- name: Start MySQL
Expand Down
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ if (WIN32 AND NOT OPENSSL_ROOT_DIR)
endif()
endif()
find_package(OpenSSL REQUIRED)
# Imported OpenSSL targets live in this directory scope. Mark them global so
# test/ can link them without a second find_package (which would miss the
# MinGW OPENSSL_ROOT_DIR / static-vs-shared policy set above).
set_property(TARGET OpenSSL::SSL PROPERTY IMPORTED_GLOBAL TRUE)
set_property(TARGET OpenSSL::Crypto PROPERTY IMPORTED_GLOBAL TRUE)
find_package(Threads REQUIRED)

if (OPENSSL_USE_STATIC_LIBS)
Expand Down
36 changes: 26 additions & 10 deletions src/jit/gcc_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,29 @@ class WinHandle {
public:
HANDLE h = INVALID_HANDLE_VALUE;
WinHandle() = default;
explicit WinHandle(HANDLE handle) : h(handle) {}
~WinHandle() { reset(); }

explicit WinHandle(HANDLE handle) : h(handle) {
}

~WinHandle() {
reset();
}

WinHandle(const WinHandle &) = delete;
WinHandle &operator=(const WinHandle &) = delete;
WinHandle(WinHandle &&other) noexcept : h(other.h) { other.h = INVALID_HANDLE_VALUE; }

WinHandle(WinHandle &&other) noexcept : h(other.h) {
other.h = INVALID_HANDLE_VALUE;
}

void reset() {
if (h != INVALID_HANDLE_VALUE) CloseHandle(h);
h = INVALID_HANDLE_VALUE;
}
explicit operator bool() const { return h != INVALID_HANDLE_VALUE; }

explicit operator bool() const {
return h != INVALID_HANDLE_VALUE;
}
};

// One-pass POSIX unlink: never retries, and does not block on a share-delete open handle.
Expand All @@ -57,9 +70,11 @@ bool UnlinkPath(const fs::path &p) {
const auto err = GetLastError();
return err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND;
}

struct DispositionEx {
DWORD Flags;
} posix{0x00000001 | 0x00000002};// FILE_DISPOSITION_FLAG_DELETE | POSIX_SEMANTICS

if (SetFileInformationByHandle(handle.h, static_cast<FILE_INFO_BY_HANDLE_CLASS>(21), &posix, sizeof(posix))) return true;
FILE_DISPOSITION_INFO info{};
info.DeleteFile = TRUE;
Expand Down Expand Up @@ -144,7 +159,9 @@ void WriteBytes(const fs::path &p, const std::string &data) {
if (!out) throw std::runtime_error("cache write");
}

void RemoveTree(const fs::path &p, std::error_code &ec) { fs::remove_all(p, ec); }
void RemoveTree(const fs::path &p, std::error_code &ec) {
fs::remove_all(p, ec);
}
#endif

std::string Digest(const std::string &data) {
Expand Down Expand Up @@ -412,7 +429,8 @@ class CacheFileLock {
WinHandle named(CreateFileW(path_.wstring().c_str(), 0, kShareAll, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
if (!named) return false;
BY_HANDLE_FILE_INFORMATION info{};
return GetFileInformationByHandle(named.h, &info) && held.dwVolumeSerialNumber == info.dwVolumeSerialNumber && held.nFileIndexHigh == info.nFileIndexHigh && held.nFileIndexLow == info.nFileIndexLow;
return GetFileInformationByHandle(named.h, &info) && held.dwVolumeSerialNumber == info.dwVolumeSerialNumber && held.nFileIndexHigh == info.nFileIndexHigh &&
held.nFileIndexLow == info.nFileIndexLow;
#else
struct stat held{}, named{};

Expand Down Expand Up @@ -548,9 +566,8 @@ void GccCache::Release() {
}

GccCache::GccCache(const StateGCCConfig &cfg, const std::string &compiler, const std::string &code, bool debug) {
// Arbitrary user headers/libraries (and coverage files) cannot be reliably
// fingerprinted as a closed toolchain. Retain the uncached behavior there.
#if !defined(USE_COV)
// Arbitrary user headers/libraries cannot be reliably fingerprinted as a
// closed toolchain. Retain the uncached behavior there.
if (!cfg.enable_cache || !cfg.include_paths.empty() || !cfg.library_paths.empty() || !cfg.libraries.empty()) return;
// Search-path overrides can add a newly shadowing header/library without
// changing any previously recorded dependency. Such open-ended toolchains
Expand Down Expand Up @@ -599,7 +616,6 @@ GccCache::GccCache(const StateGCCConfig &cfg, const std::string &compiler, const
impl_ = std::move(p);
} catch (...) { /* Caching is an optimization, never a compile requirement. */
}
#endif
}

bool GccCache::Restore(const std::string &destination) {
Expand Down
8 changes: 7 additions & 1 deletion src/jit/gcc_jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ void GccJitter::Compile(const ParseResult &pr, const GenResult &gr, const Compil
}
#endif

GccCache cache(s_->GetStateConfig().gcc_config, compiler, gr.c_code, cfg.debug_mode);
auto cache_config = s_->GetStateConfig().gcc_config;
#if defined(USE_COV)
// Instrumented images own per-compilation gcno/gcda paths. Never reuse them;
// keep the filesystem cache itself instrumented and directly testable.
cache_config.enable_cache = false;
#endif
GccCache cache(cache_config, compiler, gr.c_code, cfg.debug_mode);
bool cache_hit = cache.Restore(so_file);
const std::string dep_file = c_file + ".d";

Expand Down
4 changes: 3 additions & 1 deletion src/native/http/native_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,12 @@ class HttpServerConn : public std::enable_shared_from_this<HttpServerConn> {

void Reply(int status, std::string reason, HeaderList headers, std::string body) {
if (closed_ || writing_ || !bev_) return;
writing_ = true;
HeaderSet(headers, "Server", "fakelua");
HeaderSet(headers, "Connection", "close");
std::string wire = FormatResponse(status, std::move(reason), std::move(headers), body, req_.version);
// Validation can throw. A rejected reply must not prevent a corrected
// reply from being sent on this still-open connection.
writing_ = true;
bufferevent_write(bev_, wire.data(), wire.size());
ArmTimeout();
}
Expand Down
16 changes: 16 additions & 0 deletions src/native/mysql/mysql_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ void MysqlConnection::FinishConnect(MYSQL *ret) {
pending_connect_ = true;
}

void MysqlConnection::Query(const std::string &sql, const ResultCallback &callback) {
// A queued submission must not replace the callback of the query already
// in flight. Store its callback together with its SQL until it starts.
if (mysql_ && !close_pending_ && (state_ != ConnState::Ready || !ready_)) {
queued_queries_.push_back(QueuedQuery{sql, callback});
return;
}
result_cb_ = callback;
Query(sql);
}

void MysqlConnection::Query(const std::string &sql) {
// 契约:conn:query 一旦被调用,回调恰好被调用一次。
// - 连接已死(mysql_ 释放 / 已请求关闭):下一轮 tick 给回调派发错误;
Expand Down Expand Up @@ -398,6 +409,11 @@ void MysqlConnection::StmtExecute(uint32_t stmt_id, const std::vector<StmtParam>
pending_result_ = true;
return;
}
if (params.size() != mysql_stmt_param_count(it->second)) {
pending_result_err_ = "statement parameter count mismatch";
pending_result_ = true;
return;
}
state_ = ConnState::Querying;
query_type_ = QueryType::StmtExecute;
pending_results_.clear();
Expand Down
1 change: 1 addition & 0 deletions src/native/mysql/mysql_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class MysqlConnection {
std::string ssl_ca = {});

void Query(const std::string &sql);
void Query(const std::string &sql, const ResultCallback &callback);
void StmtPrepare(const std::string &sql);
void StmtExecute(uint32_t stmt_id, const std::vector<StmtParam> &params);
void StmtClose(uint32_t stmt_id);
Expand Down
3 changes: 1 addition & 2 deletions src/native/mysql/native_mysql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ CVar ConnQuery(NativeObject *self, State *s, CVar *args, int n) {
// 连接未就绪(握手中/重连中/上一条 query 仍在飞行)不再抛错:
// 回调随 query 入队,由连接的 tick 在可用时自动启动(见 MysqlConnection::Query)。
conn->SetState(s);
conn->SetResultCallback(cb);
conn->Query(sql);
conn->Query(sql, cb);
MaybeReleaseOwnedConn(self);

return inter::NativeToFakeluaNil(s);
Expand Down
9 changes: 7 additions & 2 deletions src/native/redis/native_redis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,18 @@ class RedisConnection {
op->err = std::string(reply->str, reply->len);
return;
}
op->reply = reply;
// hiredis frees reply after callback unless we take it. Copy by keeping?
// Default: hiredis frees reply after callback returns. We must copy now.
// We'll convert later in Tick — so duplicate via redisReply isn't easy.
// Convert immediately into Lua? Can't, might be inside poll.
// Duplicate the reply tree:
op->reply = CloneReply(reply);
try {
op->reply = CloneReply(reply);
} catch (const std::exception &e) {
// Never unwind through hiredis: it still owns the original reply and
// must finish its callback cleanup even when a depth limit is hit.
op->err = e.what();
}
}

static redisReply *CloneReply(const redisReply *r, int depth = 0) {
Expand Down
5 changes: 4 additions & 1 deletion src/native/tls_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ inline SSL_CTX *MakeClientContext(bool verify, const std::string &ca) {
if (verify) {
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, nullptr);
if (!ca.empty()) {
SSL_CTX_load_verify_locations(ctx, ca.c_str(), nullptr);
if (SSL_CTX_load_verify_locations(ctx, ca.c_str(), nullptr) != 1) {
SSL_CTX_free(ctx);
return nullptr;
}
} else {
SSL_CTX_set_default_verify_paths(ctx);
}
Expand Down
31 changes: 18 additions & 13 deletions src/native/utf8/native_utf8.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "native/utf8/native_utf8.h"
#include "jit/jit_error_boundary.h"
#include "native/native_common.h"
#include "native/object/native_object.h"
#include "native/string/native_string.h"
Expand Down Expand Up @@ -247,19 +248,23 @@ extern "C" CVar Utf8CodesIterator(VarClosure *cl, CVar /*s*/, CVar /*var*/) {
if (!iter_state || !gs) {
return inter::NativeToFakeluaNil(iter_state);
}
if (gs->pos >= gs->text.size()) {
return inter::NativeToFakeluaNil(iter_state);
}
const size_t start = gs->pos;
std::string_view sv = gs->text;
int64_t cp = DecodeUtf8(sv, gs->pos);
if (cp < 0) {
ThrowFakeluaException("invalid UTF-8 code");
}
CVar multi = inter::AllocMultiCVar(iter_state, 2);
inter::SetMultiCVarElement(multi, 0, inter::NativeToFakeluaLonglong(iter_state, static_cast<int64_t>(start + 1)));
inter::SetMultiCVarElement(multi, 1, inter::NativeToFakeluaLonglong(iter_state, cp));
return multi;
// This closure is called directly by generated C, bypassing the native
// function bridge. Do not unwind a C++ exception through a TCC frame.
return GuardJitEntry(iter_state, [&]() -> CVar {
if (gs->pos >= gs->text.size()) {
return inter::NativeToFakeluaNil(iter_state);
}
const size_t start = gs->pos;
std::string_view sv = gs->text;
int64_t cp = DecodeUtf8(sv, gs->pos);
if (cp < 0) {
ThrowFakeluaException("invalid UTF-8 code");
}
CVar multi = inter::AllocMultiCVar(iter_state, 2);
inter::SetMultiCVarElement(multi, 0, inter::NativeToFakeluaLonglong(iter_state, static_cast<int64_t>(start + 1)));
inter::SetMultiCVarElement(multi, 1, inter::NativeToFakeluaLonglong(iter_state, cp));
return multi;
});
}

static CVar Utf8Codes(State *state, CVar *args, int n) {
Expand Down
10 changes: 9 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ ELSE ()
ENDIF ()

add_executable(unit_tests ${TEST_SRC_LIST})
# Direct MySQL connection/pool unit tests use their internal dependency headers.
foreach (_fl_test_dependency event_core mariadbclient libmariadb)
if (TARGET ${_fl_test_dependency})
target_include_directories(unit_tests PRIVATE $<TARGET_PROPERTY:${_fl_test_dependency},INCLUDE_DIRECTORIES>)
endif ()
endforeach ()
if (USE_COV)
target_compile_definitions(unit_tests PRIVATE FAKELUA_TEST_NO_GCC_CACHE=1)
endif ()
Expand All @@ -87,7 +93,9 @@ if (NOT WIN32 AND NOT APPLE)
target_link_libraries(unit_tests PRIVATE dl)
endif ()
find_package(Threads REQUIRED)
target_link_libraries(unit_tests PRIVATE fakelua gtest_main ${LUA_LIBRARIES} Threads::Threads)
# test_crypto calls OpenSSL APIs directly; Windows fakelua.dll does not
# re-export them (--exclude-libs,ALL), so unit_tests must link OpenSSL itself.
target_link_libraries(unit_tests PRIVATE fakelua gtest_main ${LUA_LIBRARIES} Threads::Threads Boost::container lz4_static OpenSSL::SSL OpenSSL::Crypto)

add_test(NAME unit COMMAND ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/unit_tests
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
Expand Down
18 changes: 18 additions & 0 deletions test/lua/basic/test_basic_host_exceptions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function test_host_exception_boundaries()
local ok, message = pcall(function() host_standard_error() end)
assert(not ok and string.find(message, "host failure", 1, true))
ok, message = pcall(function() host_unknown_error() end)
assert(not ok and string.find(message, "unknown", 1, true))
ok, message = xpcall(function() error("original failure") end, function(err) host_standard_error() end)
assert(not ok and string.find(message, "original failure", 1, true))
assert(not pcall(function() xpcall(function() return 1 end, 123) end))
local key = {}
local iterator, state, control = pairs({[key] = 9})
local returned, value = iterator(state, control)
assert(returned == key and value == 9)
assert(iterator(state, returned) == nil)
-- Protected-call failures must not poison the next ordinary call.
local success, result = pcall(function() return 42 end)
assert(success and result == 42)
return 1
end
Loading
Loading