Skip to content

Commit ee96293

Browse files
Sandboxed API Teamcopybara-github
authored andcommitted
Add a name property to SAPI Sandbox class and update the code generator to propagate sandbox class names for both embedded and non-embedded libraries.
PiperOrigin-RevId: 964101509 Change-Id: I9db14c8260c32da79ac7785b9b11ba3ab4ee937a
1 parent 89647e3 commit ee96293

10 files changed

Lines changed: 76 additions & 18 deletions

File tree

sandboxed_api/BUILD

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ cc_library(
168168
":vars",
169169
"@abseil-cpp//absl/base:core_headers",
170170
"@abseil-cpp//absl/base:dynamic_annotations",
171+
"@abseil-cpp//absl/base:no_destructor",
171172
"@abseil-cpp//absl/cleanup",
172173
"@abseil-cpp//absl/container:flat_hash_map",
173-
"@abseil-cpp//absl/container:flat_hash_set",
174174
"@abseil-cpp//absl/functional:any_invocable",
175175
"@abseil-cpp//absl/log",
176176
"@abseil-cpp//absl/log:check",
@@ -188,7 +188,6 @@ cc_library(
188188
"//sandboxed_api/sandbox2:fork_client",
189189
"//sandboxed_api/sandbox2:sandbox_config",
190190
"//sandboxed_api/util:fileops",
191-
"//sandboxed_api/util:status",
192191
],
193192
)
194193

sandboxed_api/bazel/embed_data.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ def sapi_cc_embed_data(name, srcs = [], namespace = "", **kwargs):
7777
**kwargs: extra arguments like testonly, visibility, etc.
7878
"""
7979
embed_rule = "_%s_sapi" % name
80+
testonly = kwargs.get("testonly", None)
81+
common = {}
82+
if testonly != None:
83+
common["testonly"] = testonly
84+
8085
_sapi_cc_embed_data(
8186
name = embed_rule,
8287
srcs = srcs,
@@ -86,6 +91,7 @@ def sapi_cc_embed_data(name, srcs = [], namespace = "", **kwargs):
8691
"%s.h" % name,
8792
"%s.cc" % name,
8893
],
94+
**common
8995
)
9096
cc_library(
9197
name = name,

sandboxed_api/bazel/sapi.bzl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,16 @@ def symbol_list_gen(name, lib, out, **kwargs):
283283
**kwargs
284284
)
285285

286-
def _common_kwargs(tags, visibility, compatible_with):
286+
def _common_kwargs(tags, visibility, compatible_with, testonly = None):
287287
common = {
288288
"tags": tags,
289289
}
290290
if visibility:
291291
common["visibility"] = visibility
292292
if compatible_with != None:
293293
common["compatible_with"] = compatible_with
294+
if testonly != None:
295+
common["testonly"] = testonly
294296
return common
295297

296298
def sapi_library(
@@ -313,6 +315,7 @@ def sapi_library(
313315
deps = [],
314316
tags = [],
315317
generator_version = 2,
318+
testonly = None,
316319
visibility = None,
317320
compatible_with = None,
318321
default_copts = [],
@@ -359,7 +362,7 @@ def sapi_library(
359362
sandbox_mode: Sandbox mode to use for the generated library. Either "sandbox2" (default) or "passthrough".
360363
"""
361364

362-
common = _common_kwargs(tags, visibility, compatible_with)
365+
common = _common_kwargs(tags, visibility, compatible_with, testonly)
363366
generated_file_prefix = name + ".sapi"
364367
generated_header = generated_file_prefix + ".h"
365368
generated_sandboxee_src = generated_file_prefix + ".sandboxee.cc"
@@ -587,7 +590,8 @@ def cc_sandboxed_library(
587590
# TODO(dvyukov): add hash/flattening of the full library /path:name, just the name is not
588591
# necessarily globally unique.
589592
wrapper_name = "Sapi" + name
590-
common = _common_kwargs(tags, visibility, compatible_with)
593+
testonly = kwargs.get("testonly", None)
594+
common = _common_kwargs(tags, visibility, compatible_with, testonly)
591595

592596
cc_library(
593597
name = "_unsandboxed_" + name,

sandboxed_api/examples/sum/BUILD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,19 @@ sh_test(
104104
srcs = ["main_sum_test.sh"],
105105
data = [":main_sum"],
106106
)
107+
108+
sapi_library(
109+
name = "sum_sapi_noembed",
110+
embed = False,
111+
functions = [
112+
"sum",
113+
],
114+
input_files = [
115+
"sum.c",
116+
"sum_cpp.cc",
117+
],
118+
lib = ":sum",
119+
lib_name = "SumNoEmbed",
120+
visibility = ["//visibility:public"],
121+
deps = [":sum_params_cc_proto"],
122+
)

sandboxed_api/passthrough_backend.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ class PassthroughBackend {
3535

3636
PassthroughBackend(SandboxConfig config, CallFunctionT call_function,
3737
SymbolFunctionT symbol_function)
38-
: rpc_channel_(std::make_unique<PassthroughRPCChannel>(
38+
: name_(std::move(config.name)),
39+
rpc_channel_(std::make_unique<PassthroughRPCChannel>(
3940
std::move(call_function), std::move(symbol_function))) {}
4041

42+
const std::string& name() const { return name_; }
43+
4144
// Initializes a new sandboxing session.
4245
absl::Status Init() { return absl::OkStatus(); }
4346

@@ -60,6 +63,7 @@ class PassthroughBackend {
6063
void Terminate(bool attempt_graceful_exit = true) {}
6164

6265
private:
66+
std::string name_;
6367
std::unique_ptr<PassthroughRPCChannel> rpc_channel_;
6468
};
6569

sandboxed_api/sandbox.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "absl/status/status.h"
2929
#include "absl/status/status_macros.h"
3030
#include "absl/status/statusor.h"
31+
#include "absl/strings/string_view.h"
3132
#include "absl/time/time.h"
3233
#include "absl/types/span.h"
3334
#include "sandboxed_api/call.h"
@@ -39,6 +40,7 @@
3940
#include "sandboxed_api/vars.h"
4041

4142
namespace sapi {
43+
4244
namespace sandbox_internal {
4345

4446
class PtrOrCallable {
@@ -64,7 +66,7 @@ class PtrOrCallable {
6466
// means to communicate with it (make function calls, transfer memory).
6567
class SandboxBase {
6668
public:
67-
SandboxBase() = default;
69+
explicit SandboxBase(std::string name = "unknown") : name_(std::move(name)) {}
6870

6971
virtual ~SandboxBase() = default;
7072

@@ -156,6 +158,8 @@ class SandboxBase {
156158
// sandboxee is not running or we're using an in-process sandbox.
157159
virtual absl::StatusOr<int> GetPid() const = 0;
158160

161+
const std::string& name() const { return name_; }
162+
159163
protected:
160164
// WrapCallStatus is called with the status returned by a Call. The default
161165
// implementation simply returns the status as is.
@@ -166,6 +170,7 @@ class SandboxBase {
166170
absl::Status Call(
167171
const std::string& func, v::Callable* ret,
168172
std::initializer_list<sandbox_internal::PtrOrCallable> args);
173+
std::string name_;
169174
};
170175

171176
// The Sandbox class represents the sandboxed library. It provides users with
@@ -174,23 +179,26 @@ template <typename Backend>
174179
class Sandbox : public SandboxBase {
175180
public:
176181
explicit Sandbox(SandboxConfig config)
177-
: SandboxBase(), backend_(std::move(config), [this] {
182+
: SandboxBase(config.name), backend_(std::move(config), [this] {
178183
return CreateNotifier(); // NOLINT
179184
}) {}
180185

181186
// This constructor should only be used for special cases, e.g. when using the
182187
// CreateNotifier() method of the Sandbox2Backend. Otherwise, prefer to use
183188
// the SandboxConfig constructor above.
184189
explicit Sandbox(Backend backend)
185-
: SandboxBase(), backend_(std::move(backend)) {}
190+
: SandboxBase(backend.name()), backend_(std::move(backend)) {}
186191

187192
Sandbox(const Sandbox&) = delete;
188193
Sandbox& operator=(const Sandbox&) = delete;
189194

190195
virtual ~Sandbox() = default;
191196

192197
// Initializes a new sandboxing session.
193-
absl::Status Init() override { return backend().Init(); }
198+
absl::Status Init() override {
199+
ABSL_RETURN_IF_ERROR(backend().Init());
200+
return absl::OkStatus();
201+
}
194202

195203
// Returns whether the current sandboxing session is active.
196204
bool is_active() const override { return backend().is_active(); }

sandboxed_api/sandbox2_backend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Sandbox2Backend {
4848
Sandbox2Backend(Sandbox2Backend&&);
4949
Sandbox2Backend& operator=(Sandbox2Backend&&);
5050

51+
const std::string& name() const { return config_.name; }
52+
5153
virtual ~Sandbox2Backend();
5254

5355
// Initializes a new sandboxing session.

sandboxed_api/sandbox_config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ struct Sandbox2Config {
104104
};
105105

106106
struct SandboxConfig {
107+
std::string name = "unknown";
108+
109+
SandboxConfig& set_name(std::string n) {
110+
name = std::move(n);
111+
return *this;
112+
}
113+
107114
std::optional<std::vector<std::string>> environment_variables;
108115
std::optional<absl::flat_hash_map<std::string, std::string>>
109116
command_line_flags;

sandboxed_api/tests/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ cc_test(
2525
name = "sapi_test",
2626
srcs = ["sapi_test.cc"],
2727
copts = sapi_platform_copts(),
28+
data = [
29+
"//sandboxed_api/examples/sum:sum_sapi_noembed.bin",
30+
],
2831
tags = ["local"],
2932
deps = [
3033
":sapi_test-sapi",
@@ -36,6 +39,7 @@ cc_test(
3639
"//sandboxed_api/examples/stringop:stringop-sapi",
3740
"//sandboxed_api/examples/stringop:stringop_params_cc_proto",
3841
"//sandboxed_api/examples/sum:sum-sapi",
42+
"//sandboxed_api/examples/sum:sum_sapi_noembed",
3943
"//sandboxed_api/sandbox2:result",
4044
"//sandboxed_api/util:fileops",
4145
"//sandboxed_api/util:thread",
@@ -55,17 +59,20 @@ cc_test(
5559

5660
cc_library(
5761
name = "sapi_test_lib",
62+
testonly = 1,
5863
srcs = [
5964
"sapi_test_lib_cpp.cc",
6065
],
6166
deps = [
6267
"@abseil-cpp//absl/algorithm:container",
6368
"@abseil-cpp//absl/types:span",
6469
],
70+
alwayslink = 1,
6571
)
6672

6773
sapi_library(
6874
name = "sapi_test-sapi",
75+
testonly = 1,
6976
functions = [
7077
"accumulate",
7178
"compare_self_symbol",

sandboxed_api/tools/clang_generator/emitter.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class %1$s : public ::sapi::Sandbox<::sapi::Sandbox2Backend> {
8989
: %1$s(::sapi::SandboxConfig::DefaultConfig()) {}
9090
explicit %1$s(::sapi::SandboxConfig config)
9191
: %1$s(::sapi::Sandbox2Backend(
92-
ConfigWithForkClientContext(std::move(config)),
92+
ConfigWithForkClientContext(std::move(config.set_name("%1$s"))),
9393
[this] { return CreateNotifier(); })) {}
9494
// This constructor should only be used for special cases, e.g. when using the
9595
// CreateNotifier() method. Otherwise, prefer to use the SandboxConfig
@@ -107,13 +107,19 @@ class %1$s : public ::sapi::Sandbox<::sapi::Sandbox2Backend> {
107107
return config;
108108
}
109109
};
110-
111110
)";
112111

113112
// Text template arguments:
114113
// 1. Class name
115-
constexpr absl::string_view kSandboxTypedefTemplate = R"(
116-
using %1$s = ::sapi::Sandbox<::sapi::Sandbox2Backend>;
114+
constexpr absl::string_view kSandboxClassTemplate = R"(
115+
// Sandbox class with default policy (non-embedded)
116+
class %1$s : public ::sapi::Sandbox<::sapi::Sandbox2Backend> {
117+
public:
118+
explicit %1$s(::sapi::SandboxConfig config)
119+
: ::sapi::Sandbox<::sapi::Sandbox2Backend>(std::move(config.set_name("%1$s"))) {}
120+
explicit %1$s(::sapi::Sandbox2Backend backend)
121+
: ::sapi::Sandbox<::sapi::Sandbox2Backend>(std::move(backend)) {}
122+
};
117123
)";
118124

119125
// Text template arguments:
@@ -135,7 +141,7 @@ class %1$s : public ::sapi::Sandbox<::sapi::PassthroughBackend> {
135141
%1$s()
136142
: %1$s(::sapi::SandboxConfig{}) {}
137143
explicit %1$s(::sapi::SandboxConfig config)
138-
: %1$s(::sapi::PassthroughBackend(std::move(config), %2$s, %3$s)) {}
144+
: %1$s(::sapi::PassthroughBackend(std::move(config.set_name("%1$s")), %2$s, %3$s)) {}
139145
explicit %1$s(::sapi::PassthroughBackend backend)
140146
: ::sapi::Sandbox<::sapi::PassthroughBackend>(std::move(backend)) {}
141147
};
@@ -621,9 +627,8 @@ absl::StatusOr<std::string> Emitter::DoEmitHeader() {
621627
&out, kEmbedClassTemplate, sandbox_class_name,
622628
absl::StrReplaceAll(options_.embed_name, {{"-", "_"}}));
623629
} else {
624-
// Or a typedef for the sandbox class if no embedded sandboxee is used.
625-
absl::StrAppendFormat(&out, kSandboxTypedefTemplate,
626-
sandbox_class_name);
630+
// Or a class for the sandbox if no embedded sandboxee is used.
631+
absl::StrAppendFormat(&out, kSandboxClassTemplate, sandbox_class_name);
627632
}
628633
break;
629634
}

0 commit comments

Comments
 (0)