Skip to content

Commit 916fbc3

Browse files
Sandboxed API Teamcopybara-github
authored andcommitted
Add support for symbol lookup in PassthroughBackend.
This change extends PassthroughBackend and PassthroughRPCChannel to handle symbol lookup requests. A new SymbolFunctionT type is introduced, and the constructors are updated to accept and store a function for handling these symbol messages. The code generation for passthrough sandboxes is also updated to provide the newly introduced HandleSymbolMsg function. PiperOrigin-RevId: 897124952 Change-Id: I4358eb87f14014b459aeb568670bba96f43a85bc
1 parent 2b65eaa commit 916fbc3

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

sandboxed_api/passthrough_backend.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ class SandboxBase;
3131
class PassthroughBackend {
3232
public:
3333
using CallFunctionT = PassthroughRPCChannel::CallFunctionT;
34-
PassthroughBackend(SandboxConfig config, CallFunctionT call_function)
35-
: rpc_channel_(
36-
std::make_unique<PassthroughRPCChannel>(std::move(call_function))) {
37-
}
34+
using SymbolFunctionT = PassthroughRPCChannel::SymbolFunctionT;
35+
36+
PassthroughBackend(SandboxConfig config, CallFunctionT call_function,
37+
SymbolFunctionT symbol_function)
38+
: rpc_channel_(std::make_unique<PassthroughRPCChannel>(
39+
std::move(call_function), std::move(symbol_function))) {}
3840

3941
// Initializes a new sandboxing session.
4042
absl::Status Init() { return absl::OkStatus(); }

sandboxed_api/passthrough_rpcchannel.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "absl/status/status.h"
2626
#include "absl/status/statusor.h"
2727
#include "absl/types/span.h"
28+
#include "sandboxed_api/call.h"
2829

2930
namespace sapi {
3031

@@ -67,11 +68,13 @@ absl::StatusOr<size_t> PassthroughRPCChannel::CopyToSandbox(
6768
}
6869

6970
absl::Status PassthroughRPCChannel::Symbol(const char* symname, void** addr) {
70-
*addr = dlsym(RTLD_DEFAULT, symname);
71-
if (!*addr) {
72-
return absl::ErrnoToStatus(errno,
73-
"PassthroughRPCChannel::Symbol: dlsym failed");
71+
FuncRet ret{};
72+
symbol_function_(symname, &ret);
73+
if (!ret.success) {
74+
return absl::InternalError(
75+
"PassthroughRPCChannel::Symbol: symbol not found");
7476
}
77+
*addr = reinterpret_cast<void*>(ret.int_val);
7578
return absl::OkStatus();
7679
}
7780

sandboxed_api/passthrough_rpcchannel.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ class PassthroughRPCChannel : public RPCChannel {
3838
public:
3939
using CallFunctionT =
4040
absl::AnyInvocable<void(const FuncCall& call, FuncRet* ret) const>;
41-
explicit PassthroughRPCChannel(CallFunctionT call_function)
42-
: call_function_(std::move(call_function)) {}
41+
using SymbolFunctionT =
42+
absl::AnyInvocable<void(const char* symname, FuncRet* ret) const>;
43+
44+
explicit PassthroughRPCChannel(CallFunctionT call_function,
45+
SymbolFunctionT symbol_function)
46+
: call_function_(std::move(call_function)),
47+
symbol_function_(std::move(symbol_function)) {}
4348

4449
absl::Status Allocate(size_t size, void** addr,
4550
bool disable_shared_memory) override;
@@ -78,6 +83,7 @@ class PassthroughRPCChannel : public RPCChannel {
7883

7984
private:
8085
CallFunctionT call_function_;
86+
SymbolFunctionT symbol_function_;
8187
};
8288

8389
} // namespace sapi

sandboxed_api/tools/clang_generator/emitter.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class %1$s : public ::sapi::Sandbox<::sapi::PassthroughBackend> {
142142
%1$s()
143143
: %1$s(::sapi::SandboxConfig{}) {}
144144
%1$s(::sapi::SandboxConfig config)
145-
: %1$s(::sapi::PassthroughBackend(std::move(config), %2$s)) {}
145+
: %1$s(::sapi::PassthroughBackend(std::move(config), %2$s, %3$s)) {}
146146
%1$s(::sapi::PassthroughBackend backend)
147147
: ::sapi::Sandbox<::sapi::PassthroughBackend>(std::move(backend)) {}
148148
};
@@ -617,7 +617,9 @@ absl::StatusOr<std::string> Emitter::DoEmitHeader() {
617617
case SandboxMode::kPassthrough: {
618618
absl::StrAppendFormat(
619619
&out, kPassthroughClassTemplate, sandbox_class_name,
620-
absl::StrCat("::sapi::client::", handle_msg_prefix, "HandleCallMsg"));
620+
absl::StrCat("::sapi::client::", handle_msg_prefix, "HandleCallMsg"),
621+
absl::StrCat("::sapi::client::", handle_msg_prefix,
622+
"HandleSymbolMsg"));
621623
break;
622624
}
623625
}

0 commit comments

Comments
 (0)