Skip to content

Commit 910659f

Browse files
Sandboxed API Teamcopybara-github
authored andcommitted
Don't emit wrappers for functions that have thunks
PiperOrigin-RevId: 897564703 Change-Id: I75e73a3f9fccd81b9eca1ec37682ec8eaf14fec1
1 parent 29e9c41 commit 910659f

4 files changed

Lines changed: 74 additions & 0 deletions

File tree

sandboxed_api/tools/clang_generator/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ cc_test(
8383
"emitter_test.cc",
8484
"frontend_action_test_util.cc",
8585
"frontend_action_test_util.h",
86+
"sandboxed_library_emitter_test.cc",
8687
],
8788
copts = sapi_platform_copts(),
8889
data = [

sandboxed_api/tools/clang_generator/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ if(BUILD_TESTING AND SAPI_BUILD_TESTING)
150150
frontend_action_test_util.cc
151151
frontend_action_test_util.h
152152
emitter_test.cc
153+
sandboxed_library_emitter_test.cc
153154
)
154155
target_link_libraries(sapi_generator_test PRIVATE
155156
absl::check

sandboxed_api/tools/clang_generator/sandboxed_library_emitter.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,9 @@ absl::StatusOr<std::string> SandboxedLibraryEmitter::EmitSandboxeeHdr(
468468
EmitLibraryHeaders(options, out);
469469

470470
for (const auto* func : SortedFuncs()) {
471+
if (func->sandboxee_thunk) {
472+
continue;
473+
}
471474
EmitWrapperDecl(out, *func);
472475
out += ";\n\n";
473476
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "sandboxed_api/tools/clang_generator/sandboxed_library_emitter.h"
16+
17+
#include <memory>
18+
#include <string>
19+
20+
#include "gmock/gmock.h"
21+
#include "gtest/gtest.h"
22+
#include "absl/status/status.h"
23+
#include "absl/status/status_matchers.h"
24+
#include "absl/status/statusor.h"
25+
#include "sandboxed_api/tools/clang_generator/frontend_action_test_util.h"
26+
#include "sandboxed_api/tools/clang_generator/generator.h"
27+
28+
namespace sapi {
29+
namespace {
30+
31+
using ::absl_testing::IsOk;
32+
using ::testing::HasSubstr;
33+
using ::testing::Not;
34+
35+
class SandboxedLibraryEmitterTest : public FrontendActionTest {};
36+
37+
TEST_F(SandboxedLibraryEmitterTest, SandboxeeThunkNotUsed) {
38+
GeneratorOptions options;
39+
options.name = "MyLib";
40+
SandboxedLibraryEmitter emitter;
41+
ASSERT_THAT(
42+
RunFrontendAction(R"(
43+
extern "C" int func_with_thunk(int a);
44+
45+
[[clang::annotate("sandbox", "sandboxee_thunk", "func_with_thunk")]]
46+
int func_with_thunk_thunk(int a) {
47+
return func_with_thunk(a) + 1;
48+
}
49+
)",
50+
std::make_unique<GeneratorAction>(&emitter, &options)),
51+
IsOk());
52+
53+
ASSERT_EQ(emitter.PostParseAllFiles(), absl::OkStatus());
54+
absl::StatusOr<std::string> host_src = emitter.EmitHostSrc(options);
55+
ASSERT_THAT(host_src, IsOk());
56+
EXPECT_THAT(*host_src, HasSubstr("api.sapi_wrapper_func_with_thunk("));
57+
58+
absl::StatusOr<std::string> sandboxee_src = emitter.EmitSandboxeeSrc(options);
59+
ASSERT_THAT(sandboxee_src, IsOk());
60+
// Problem: wrapper for func_with_thunk is generated, and thunk is not used.
61+
EXPECT_THAT(*sandboxee_src, HasSubstr("sapi_wrapper_func_with_thunk"));
62+
// The wrapper should call func_with_thunk, not func_with_thunk_thunk
63+
EXPECT_THAT(*sandboxee_src,
64+
HasSubstr("int sapi_ret_val = func_with_thunk(a);"));
65+
EXPECT_THAT(*sandboxee_src, Not(HasSubstr("func_with_thunk_thunk")));
66+
}
67+
68+
} // namespace
69+
} // namespace sapi

0 commit comments

Comments
 (0)