Skip to content

Commit 7870817

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: 964265477 Change-Id: I296910f90c62cb0bf8eb80a487f2ddf1937ef812
1 parent 86bd090 commit 7870817

10 files changed

Lines changed: 18 additions & 76 deletions

File tree

sandboxed_api/BUILD

Lines changed: 2 additions & 1 deletion
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",
172171
"@abseil-cpp//absl/cleanup",
173172
"@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,6 +188,7 @@ cc_library(
188188
"//sandboxed_api/sandbox2:fork_client",
189189
"//sandboxed_api/sandbox2:sandbox_config",
190190
"//sandboxed_api/util:fileops",
191+
"//sandboxed_api/util:status",
191192
],
192193
)
193194

sandboxed_api/bazel/embed_data.bzl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ 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-
8580
_sapi_cc_embed_data(
8681
name = embed_rule,
8782
srcs = srcs,
@@ -91,7 +86,6 @@ def sapi_cc_embed_data(name, srcs = [], namespace = "", **kwargs):
9186
"%s.h" % name,
9287
"%s.cc" % name,
9388
],
94-
**common
9589
)
9690
cc_library(
9791
name = name,

sandboxed_api/bazel/sapi.bzl

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

286-
def _common_kwargs(tags, visibility, compatible_with, testonly = None):
286+
def _common_kwargs(tags, visibility, compatible_with):
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
296294
return common
297295

298296
def sapi_library(
@@ -315,7 +313,6 @@ def sapi_library(
315313
deps = [],
316314
tags = [],
317315
generator_version = 2,
318-
testonly = None,
319316
visibility = None,
320317
compatible_with = None,
321318
default_copts = [],
@@ -362,7 +359,7 @@ def sapi_library(
362359
sandbox_mode: Sandbox mode to use for the generated library. Either "sandbox2" (default) or "passthrough".
363360
"""
364361

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

596592
cc_library(
597593
name = "_unsandboxed_" + name,

sandboxed_api/examples/sum/BUILD

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,3 @@ 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: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@ class PassthroughBackend {
3535

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

42-
const std::string& name() const { return name_; }
43-
4441
// Initializes a new sandboxing session.
4542
absl::Status Init() { return absl::OkStatus(); }
4643

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

6562
private:
66-
std::string name_;
6763
std::unique_ptr<PassthroughRPCChannel> rpc_channel_;
6864
};
6965

sandboxed_api/sandbox.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
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"
3231
#include "absl/time/time.h"
3332
#include "absl/types/span.h"
3433
#include "sandboxed_api/call.h"
@@ -40,7 +39,6 @@
4039
#include "sandboxed_api/vars.h"
4140

4241
namespace sapi {
43-
4442
namespace sandbox_internal {
4543

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

7169
virtual ~SandboxBase() = default;
7270

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

161-
const std::string& name() const { return name_; }
162-
163159
protected:
164160
// WrapCallStatus is called with the status returned by a Call. The default
165161
// implementation simply returns the status as is.
@@ -170,7 +166,6 @@ class SandboxBase {
170166
absl::Status Call(
171167
const std::string& func, v::Callable* ret,
172168
std::initializer_list<sandbox_internal::PtrOrCallable> args);
173-
std::string name_;
174169
};
175170

176171
// The Sandbox class represents the sandboxed library. It provides users with
@@ -179,26 +174,23 @@ template <typename Backend>
179174
class Sandbox : public SandboxBase {
180175
public:
181176
explicit Sandbox(SandboxConfig config)
182-
: SandboxBase(config.name), backend_(std::move(config), [this] {
177+
: SandboxBase(), backend_(std::move(config), [this] {
183178
return CreateNotifier(); // NOLINT
184179
}) {}
185180

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

192187
Sandbox(const Sandbox&) = delete;
193188
Sandbox& operator=(const Sandbox&) = delete;
194189

195190
virtual ~Sandbox() = default;
196191

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

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

sandboxed_api/sandbox2_backend.h

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

51-
const std::string& name() const { return config_.name; }
52-
5351
virtual ~Sandbox2Backend();
5452

5553
// Initializes a new sandboxing session.

sandboxed_api/sandbox_config.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,6 @@ 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-
114107
std::optional<std::vector<std::string>> environment_variables;
115108
std::optional<absl::flat_hash_map<std::string, std::string>>
116109
command_line_flags;

sandboxed_api/tests/BUILD

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ 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-
],
3128
tags = ["local"],
3229
deps = [
3330
":sapi_test-sapi",
@@ -39,7 +36,6 @@ cc_test(
3936
"//sandboxed_api/examples/stringop:stringop-sapi",
4037
"//sandboxed_api/examples/stringop:stringop_params_cc_proto",
4138
"//sandboxed_api/examples/sum:sum-sapi",
42-
"//sandboxed_api/examples/sum:sum_sapi_noembed",
4339
"//sandboxed_api/sandbox2:result",
4440
"//sandboxed_api/util:fileops",
4541
"//sandboxed_api/util:thread",
@@ -59,20 +55,17 @@ cc_test(
5955

6056
cc_library(
6157
name = "sapi_test_lib",
62-
testonly = 1,
6358
srcs = [
6459
"sapi_test_lib_cpp.cc",
6560
],
6661
deps = [
6762
"@abseil-cpp//absl/algorithm:container",
6863
"@abseil-cpp//absl/types:span",
6964
],
70-
alwayslink = 1,
7165
)
7266

7367
sapi_library(
7468
name = "sapi_test-sapi",
75-
testonly = 1,
7669
functions = [
7770
"accumulate",
7871
"compare_self_symbol",

sandboxed_api/tools/clang_generator/emitter.cc

Lines changed: 8 additions & 13 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.set_name("%1$s"))),
92+
ConfigWithForkClientContext(std::move(config)),
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,19 +107,13 @@ class %1$s : public ::sapi::Sandbox<::sapi::Sandbox2Backend> {
107107
return config;
108108
}
109109
};
110+
110111
)";
111112

112113
// Text template arguments:
113114
// 1. Class name
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-
};
115+
constexpr absl::string_view kSandboxTypedefTemplate = R"(
116+
using %1$s = ::sapi::Sandbox<::sapi::Sandbox2Backend>;
123117
)";
124118

125119
// Text template arguments:
@@ -141,7 +135,7 @@ class %1$s : public ::sapi::Sandbox<::sapi::PassthroughBackend> {
141135
%1$s()
142136
: %1$s(::sapi::SandboxConfig{}) {}
143137
explicit %1$s(::sapi::SandboxConfig config)
144-
: %1$s(::sapi::PassthroughBackend(std::move(config.set_name("%1$s")), %2$s, %3$s)) {}
138+
: %1$s(::sapi::PassthroughBackend(std::move(config), %2$s, %3$s)) {}
145139
explicit %1$s(::sapi::PassthroughBackend backend)
146140
: ::sapi::Sandbox<::sapi::PassthroughBackend>(std::move(backend)) {}
147141
};
@@ -627,8 +621,9 @@ absl::StatusOr<std::string> Emitter::DoEmitHeader() {
627621
&out, kEmbedClassTemplate, sandbox_class_name,
628622
absl::StrReplaceAll(options_.embed_name, {{"-", "_"}}));
629623
} else {
630-
// Or a class for the sandbox if no embedded sandboxee is used.
631-
absl::StrAppendFormat(&out, kSandboxClassTemplate, sandbox_class_name);
624+
// Or a typedef for the sandbox class if no embedded sandboxee is used.
625+
absl::StrAppendFormat(&out, kSandboxTypedefTemplate,
626+
sandbox_class_name);
632627
}
633628
break;
634629
}

0 commit comments

Comments
 (0)