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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
11 changes: 8 additions & 3 deletions core/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PacketReceiveHook>();
if (!packetSendHook_->Install(cfg.packetReceiveTarget,
if (!packetSendHook_->Install(cfg.packetSendTarget,
cfg.packetCaptureAbi, backend,
packetCaptureHub_.get(),
PacketDir::Outbound)) {
Expand All @@ -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");
}
}

Expand Down
18 changes: 15 additions & 3 deletions core/net/multicast_lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions core/script/luau_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion include/berry/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

//----------------------------------------------------------------------------
Expand Down
61 changes: 61 additions & 0 deletions tests/test_packet_capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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) {
Expand Down Expand Up @@ -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();
Expand All @@ -480,6 +540,7 @@ int main() {
TestProductionObservers();
TestClientInstallsPacketHook();
TestClientInstallsReceiveHookFromConfig();
TestClientInstallsSendHookFromConfig();

if (g_fail) {
printf("test_packet_capture: %d FAILURE(S)\n", g_fail);
Expand Down
Loading