Skip to content

Commit

Permalink
Refactor the alternate signal stack setup/cleanup into a fixture.
Browse files Browse the repository at this point in the history
Previously the test stack is freed in the first iteration, which makes the later iterations useless.

PiperOrigin-RevId: 606617457
  • Loading branch information
xinhaoyuan authored and copybara-github committed Feb 13, 2024
1 parent b67bf91 commit 972b8a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
3 changes: 2 additions & 1 deletion e2e_tests/functional_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,8 @@ TEST_P(FuzzingModeCrashFindingTest,
"Please run with --config=fuzztest to enable these tests!";
#endif
auto [status, std_out, std_err] =
Run("MySuite.StackCalculationWorksWithAlternateStackForSignalHandlers");
Run("AlternateSignalStackFixture."
"StackCalculationWorksWithAlternateStackForSignalHandlers");
EXPECT_THAT(std_err, HasSubstr("argument 0: 123456789"));
EXPECT_THAT(std_err,
Not(HasSubstr("You can change the limit by specifying")));
Expand Down
63 changes: 35 additions & 28 deletions e2e_tests/testdata/fuzz_tests_for_functional_testing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -689,14 +689,9 @@ void DataDependentStackOverflow(const std::string& s) {
FUZZ_TEST(MySuite, DataDependentStackOverflow)
.WithDomains(fuzztest::Arbitrary<std::string>().WithSize(100000));

void StackCalculationWorksWithAlternateStackForSignalHandlers(int i) {
static size_t dummy_to_trigger_cmp_in_handler;
static bool setup_signal_with_altstack = [] {
stack_t sigstk = {};
sigstk.ss_size = 1 << 20;
sigstk.ss_sp = malloc(sigstk.ss_size);
FUZZTEST_INTERNAL_CHECK(sigaltstack(&sigstk, nullptr) == 0, errno);

class AlternateSignalStackFixture {
public:
AlternateSignalStackFixture() {
struct sigaction new_sigact = {};
sigemptyset(&new_sigact.sa_mask);
new_sigact.sa_sigaction = [](auto...) {
Expand All @@ -713,28 +708,40 @@ void StackCalculationWorksWithAlternateStackForSignalHandlers(int i) {

FUZZTEST_INTERNAL_CHECK(sigaction(SIGUSR1, &new_sigact, nullptr) == 0,
errno);
stack_t test_stack = {};
test_stack.ss_size = 1 << 20;
test_stack.ss_sp = malloc(test_stack.ss_size);
FUZZTEST_INTERNAL_CHECK(sigaltstack(&test_stack, &old_stack) == 0, errno);
}

void StackCalculationWorksWithAlternateStackForSignalHandlers(int i) {
dummy_to_trigger_cmp_in_handler = 0;
// Raise the signal to get the handler running.
// If the stack calculations are done correctly, that code will not trigger
// "stack overflow" detection and we will continue here.
raise(SIGUSR1);
// Just make sure the signal handler ran.
FUZZTEST_INTERNAL_CHECK(dummy_to_trigger_cmp_in_handler != 0, "");

if (i == 123456789) {
std::abort();
}
}

return true;
}();
(void)setup_signal_with_altstack;
// Raise the signal to get the handler running.
// If the stack calculations are done correctly, that code will not trigger
// "stack overflow" detection and we will continue here.
raise(SIGUSR1);
// Just make sure the signal handler ran.
FUZZTEST_INTERNAL_CHECK(dummy_to_trigger_cmp_in_handler != 0, "");
// Disable and free the previous signal stack.
stack_t sigstk = {};
sigstk.ss_flags = SS_DISABLE;
stack_t prev_sigstk;
FUZZTEST_INTERNAL_CHECK(sigaltstack(&sigstk, &prev_sigstk) == 0, errno);
free(prev_sigstk.ss_sp);

if (i == 123456789) {
std::abort();
~AlternateSignalStackFixture() {
stack_t test_stack = {};
// Resume to the old signal stack.
FUZZTEST_INTERNAL_CHECK(sigaltstack(&old_stack, &test_stack) == 0, errno);
free(test_stack.ss_sp);
}
}
FUZZ_TEST(MySuite, StackCalculationWorksWithAlternateStackForSignalHandlers);

private:
stack_t old_stack = {};
static size_t dummy_to_trigger_cmp_in_handler;
};
size_t AlternateSignalStackFixture::dummy_to_trigger_cmp_in_handler = 0;
FUZZ_TEST_F(AlternateSignalStackFixture,
StackCalculationWorksWithAlternateStackForSignalHandlers);

void DetectRegressionAndCoverageInputs(const std::string& input) {
if (absl::StartsWith(input, "regression")) {
Expand Down

0 comments on commit 972b8a0

Please sign in to comment.