From 0221a8fab3bfef5cab1c1eed9b8378e998322648 Mon Sep 17 00:00:00 2001 From: Lynxifyy <297587416+Lynxifyy@users.noreply.github.com> Date: Tue, 1 Dec 2026 18:21:53 +0000 Subject: [PATCH] Fix packet send-hook target, JNI class lookups, and script timer reset - Install the outbound packet hook on a distinct packetSendTarget instead of reusing packetReceiveTarget (which is already hooked, so the install always failed with AlreadyHooked). Add packetSendTarget to ClientConfig and cover the no-target and real-target paths in the packet-capture test. - Use the JNI '/' name for android/app/ActivityThread and clear pending exceptions so a missing class no longer aborts the process. - Reset vmctx_.nativeMs on every (re)start so the execution deadline cannot underflow into a spurious Timeout. - Call enable_testing() so ctest sees the per-file test binaries. --- CMakeLists.txt | 1 + core/client/client.cpp | 11 +++++-- core/net/multicast_lock.cpp | 18 +++++++++-- core/script/luau_vm.cpp | 3 ++ include/berry/client.h | 8 ++++- tests/test_packet_capture.cpp | 61 +++++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 313e841..c4cb2a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,7 @@ set_target_properties(BerryAPI PROPERTIES VERSION ${PROJECT_VERSION}) # One test binary per tests/test_*.cpp, mirroring the Makefile. +enable_testing() foreach(_test_src ${TEST_SRC}) get_filename_component(_name ${_test_src} NAME_WE) add_executable(${_name} ${_test_src}) diff --git a/core/client/client.cpp b/core/client/client.cpp index 292c6a1..ead5f7c 100644 --- a/core/client/client.cpp +++ b/core/client/client.cpp @@ -552,10 +552,12 @@ void BerryClient::BuildPacketCapture(const ClientConfig& cfg) { // The send path is opt-in: a recording needs what the client received, and // the player's own input is already captured as `PlayerInput`. Installing it // only on request keeps the hook count (and the risk) to what a recording - // needs. - if (cfg.captureOutboundPackets) { + // needs. It targets the send function, never the receive one: the receive + // target is already hooked above, so installing there always fails with + // AlreadyHooked. + if (cfg.captureOutboundPackets && cfg.packetSendTarget) { packetSendHook_ = std::make_unique(); - if (!packetSendHook_->Install(cfg.packetReceiveTarget, + if (!packetSendHook_->Install(cfg.packetSendTarget, cfg.packetCaptureAbi, backend, packetCaptureHub_.get(), PacketDir::Outbound)) { @@ -564,6 +566,9 @@ void BerryClient::BuildPacketCapture(const ClientConfig& cfg) { } else { Log::Info("BerryClient: packet send hook installed"); } + } else if (cfg.captureOutboundPackets) { + Log::Warn("BerryClient: outbound capture requested but no send target; " + "the send path stays unhooked"); } } diff --git a/core/net/multicast_lock.cpp b/core/net/multicast_lock.cpp index 9ff1b1a..6066b4b 100644 --- a/core/net/multicast_lock.cpp +++ b/core/net/multicast_lock.cpp @@ -65,12 +65,24 @@ struct ScopedEnv { /// The application Context, or null. Goes through ActivityThread, which every /// Android app process has, rather than requiring a caller to pass one in. jobject CurrentApplication(JNIEnv* env) { - jclass at = env->FindClass("android.app.ActivityThread"); - if (at == nullptr) return nullptr; + // JNI names use '/', not '.', or FindClass fails with ClassNotFoundException + // and leaves a pending exception behind. + jclass at = env->FindClass("android/app/ActivityThread"); + if (at == nullptr) { + if (env->ExceptionCheck()) env->ExceptionClear(); + return nullptr; + } jmethodID cur = env->GetStaticMethodID( at, "currentApplication", "()Landroid/app/Application;"); - if (cur == nullptr) return nullptr; + if (cur == nullptr) { + if (env->ExceptionCheck()) env->ExceptionClear(); + return nullptr; + } jobject app = env->CallStaticObjectMethod(at, cur); + if (env->ExceptionCheck()) { + env->ExceptionClear(); + return nullptr; + } return app; // may be null if no application is up yet } diff --git a/core/script/luau_vm.cpp b/core/script/luau_vm.cpp index 700cd3a..7b261ac 100644 --- a/core/script/luau_vm.cpp +++ b/core/script/luau_vm.cpp @@ -244,6 +244,7 @@ class LuauVm final : public IScriptVm { lua_setthreaddata(loader, &vmctx_); vmctx_.vm = this; vmctx_.startMs = SteadyMs(); + vmctx_.nativeMs = 0; vmctx_.aborting = false; instructions_ = 0; interrupt_ = false; @@ -356,6 +357,7 @@ class LuauVm final : public IScriptVm { lua_setthreaddata(call_, &vmctx_); vmctx_.vm = this; vmctx_.startMs = SteadyMs(); + vmctx_.nativeMs = 0; vmctx_.aborting = false; instructions_ = 0; interrupt_ = false; @@ -397,6 +399,7 @@ class LuauVm final : public IScriptVm { lua_settop(call_, 0); PushScriptValue(call_, value, 0); vmctx_.startMs = SteadyMs(); + vmctx_.nativeMs = 0; vmctx_.aborting = false; instructions_ = 0; inCall_ = true; diff --git a/include/berry/client.h b/include/berry/client.h index 2e502b6..203e39e 100644 --- a/include/berry/client.h +++ b/include/berry/client.h @@ -244,8 +244,14 @@ struct BERRY_API ClientConfig { /// Install the outbound (send) hook as well as the receive one. Off by /// default: a recording needs what the client received, and the send path is /// the player's own input (already captured as `PlayerInput`). Turn it on to - /// record both directions. + /// record both directions. It has no effect unless `packetSendTarget` is + /// also set: the send hook must not be installed on `packetReceiveTarget`, + /// which is a different function and is already hooked. bool captureOutboundPackets = false; + /// The game's packet-send function, resolved by the embedder. Only used when + /// `captureOutboundPackets` is set. Null (the default) leaves the outbound + /// path unhooked. + void* packetSendTarget = nullptr; }; //---------------------------------------------------------------------------- diff --git a/tests/test_packet_capture.cpp b/tests/test_packet_capture.cpp index d61bd72..c6a62b5 100644 --- a/tests/test_packet_capture.cpp +++ b/tests/test_packet_capture.cpp @@ -359,6 +359,16 @@ void ClientFakeReceive(void* /*self*/, const void* packet) { (void)packet; } +// A distinct fake send function, so the outbound hook has its own target (the +// receive target is already hooked). +std::atomic g_clientSendOrigCalls{0}; + +__attribute__((noinline)) +void ClientFakeSend(void* /*self*/, const void* packet) { + ++g_clientSendOrigCalls; + (void)packet; +} + // The decode ABI the client's config carries: id in the first 4 bytes, the rest // is the payload. Matches `FakePacket`. bool ClientDecode(const void* p, u32* id, std::string* payload) { @@ -469,6 +479,56 @@ static void TestClientInstallsReceiveHookFromConfig() { eng.Detach(); } +static void TestClientInstallsSendHookFromConfig() { + HookManager::Instance().BackendEmulateShim(); + g_clientSendOrigCalls = 0; + + Engine& eng = Engine::Instance(); + BerryConfigC bcfg{}; + bcfg.size = sizeof(bcfg); + bcfg.abi_version = BERRY_ABI_VERSION; + bcfg.auto_attach_modules = 1; + bcfg.enable_render = 1; + bcfg.enable_client = 1; + CHECK(eng.Bootstrap(bcfg)); + CHECK(eng.Attach()); + BerryClient* client = eng.Client(); + CHECK(client != nullptr); + client->Detach(); + + // Asking for outbound capture without a send target must not install the + // send hook on the receive target (which is already hooked). + ClientConfig noTarget; + noTarget.packetReceiveTarget = (void*)&ClientFakeReceive; + noTarget.packetCaptureAbi.decode = ClientDecode; + noTarget.captureOutboundPackets = true; + CHECK(client->Attach(eng, noTarget)); + CHECK(client->packetReceiveHook() != nullptr); + CHECK(client->packetSendHook() == nullptr); + client->Detach(); + + // With a real send target the outbound hook installs on it, not on the + // receive target, and the original send function still runs. + ClientConfig ccfg; + ccfg.packetReceiveTarget = (void*)&ClientFakeReceive; + ccfg.packetSendTarget = (void*)&ClientFakeSend; + ccfg.packetCaptureAbi.decode = ClientDecode; + ccfg.packetCaptureAbi.tick = []() { return (u64)88; }; + ccfg.captureOutboundPackets = true; + CHECK(client->Attach(eng, ccfg)); + CHECK(client->packetReceiveHook() != nullptr); + CHECK(client->packetSendHook() != nullptr); + CHECK(client->packetSendHook()->installed()); + + FakePacket pkt{20, "outbound-bytes"}; + CallReceive(&ClientFakeSend, &pkt); + CHECK(g_clientSendOrigCalls == 1); + CHECK(client->packetSendHook()->captured() == 1); + + client->Detach(); + eng.Detach(); +} + int main() { TestTableClassifies(); TestTableCustomRows(); @@ -480,6 +540,7 @@ int main() { TestProductionObservers(); TestClientInstallsPacketHook(); TestClientInstallsReceiveHookFromConfig(); + TestClientInstallsSendHookFromConfig(); if (g_fail) { printf("test_packet_capture: %d FAILURE(S)\n", g_fail);