Make stack trace test more resilient against optimizer

This fixes tests for PPC, where the tail-call optimization would consistently
remove 'violate()' from the stack trace.

PiperOrigin-RevId: 371103794
Change-Id: Ifb1a7d588a455041a6b0f3c763276ed44de47e60
This commit is contained in:
Christian Blichmann 2021-04-29 06:00:51 -07:00 committed by Copybara-Service
parent 00a7cc5a33
commit 0750216bc1
4 changed files with 17 additions and 5 deletions

View File

@ -32,6 +32,7 @@ cc_library(
deps = [
":stringop_params_cc_proto",
"//sandboxed_api:lenval_core",
"@com_google_absl//absl/base:core_headers",
],
alwayslink = 1, # All functions are linked into dependent binaries
)

View File

@ -39,6 +39,7 @@ add_dependencies(sapi_stringop
target_link_libraries(sapi_stringop
PRIVATE $<TARGET_OBJECTS:sapi_stringop_params_proto>
sapi::base
absl::core_headers
sapi::lenval_core
PUBLIC protobuf::libprotobuf
)

View File

@ -17,6 +17,8 @@
#include <algorithm>
#include <iostream>
#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
#include "sandboxed_api/examples/stringop/lib/stringop_params.pb.h"
#include "sandboxed_api/lenval_core.h"
@ -77,9 +79,17 @@ extern "C" const void* get_raw_c_string() { return "Ten chars."; }
extern "C" void nop() {}
extern "C" void violate() {
ptrace((__ptrace_request)990, 991, 992, 993);
// Once more to avoid tail-call optimization, so that violate is in the
// stacktrace.
// The "no tail-call" annotation and the additional indirection ensure that this
// function shows up in the violation stack trace. Otherwise, depending on
// optimization level and optimizer aggressiveness, functions may be inlined,
// hoisted or omitted (in case of tail calls).
static ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL void
ViolateIndirect() {
ptrace((__ptrace_request)990, 991, 992, 993);
ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
}
extern "C" void violate() {
ViolateIndirect();
ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
}

View File

@ -143,7 +143,7 @@ TEST(SapiTest, HasStackTraces) {
StringopApi api(sandbox.get());
EXPECT_THAT(api.violate(), StatusIs(absl::StatusCode::kUnavailable));
const auto& result = sandbox->AwaitResult();
EXPECT_THAT(result.GetStackTrace(), HasSubstr("violate"));
EXPECT_THAT(result.GetStackTrace(), HasSubstr("ViolateIndirect"));
EXPECT_THAT(result.final_status(), Eq(sandbox2::Result::VIOLATION));
}