|
| 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