File: stacktrace_test.cc

package info (click to toggle)
chromium 145.0.7632.109-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,974,804 kB
  • sloc: cpp: 36,197,696; ansic: 7,602,761; javascript: 3,563,590; python: 1,649,324; xml: 838,427; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,022; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (254 lines) | stat: -rw-r--r-- 9,195 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <array>
#include <string>

#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/environment.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/sanitizer_buildflags.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/libfuzzer/buildflags.h"
#include "third_party/re2/src/re2/re2.h"

namespace {

using testing::ContainsRegex;
using testing::HasSubstr;
using testing::Not;

base::FilePath FuzzerPath() {
  base::FilePath out_dir;
  base::PathService::Get(base::DIR_OUT_TEST_DATA_ROOT, &out_dir);

  return out_dir.AppendASCII("stacktrace_test_fuzzer");
}

base::FilePath FuzzerInputPath(std::string_view input_file) {
  base::FilePath src_dir;
  base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &src_dir);

  return src_dir.AppendASCII("testing/libfuzzer/tests/data")
      .AppendASCII(input_file);
}

// The UaF is not detected under UBSan, which happily runs the fuzzer forever.
#if !BUILDFLAG(IS_UBSAN) && !BUILDFLAG(IS_UBSAN_SECURITY)

TEST(FuzzerStacktraceTest, SymbolizesUAF) {
  base::CommandLine cmd(FuzzerPath());
  cmd.AppendArgPath(FuzzerInputPath("uaf"));

  // This loosely replicates how we run fuzzers in production. Wet let ASAN
  // handle the symbolization online - which fails for sandboxed processes, but
  // this toy fuzzer does not run any sandboxed code.
  auto environment = base::Environment::Create();
  environment->SetVar("ASAN_OPTIONS", "symbolize=1");

  std::string output;
  EXPECT_FALSE(base::GetAppOutputAndError(cmd, &output));  // Target crashes.

// TODO(https://crbug.com/40948553): Get MSan fuzzer build to work and expect
// the correct output here.
#if defined(ADDRESS_SANITIZER)

  constexpr std::array<std::string_view, 3> kRegexLines = {
      R"(ERROR: AddressSanitizer: heap-use-after-free on address 0x[0-9a-f]+.*)",
      R"(READ of size 4 at 0x[0-9a-f]+ thread T[0-9]+)",
#if BUILDFLAG(IS_WIN)
      R"(#0 0x[0-9a-f]+ in TriggerUAF [A-Z]:\\.*testing\\libfuzzer\\tests\\stacktrace_test_fuzzer.cc:[0-9]+)",
#elif BUILDFLAG(IS_MAC)
      R"(#0 0x[0-9a-f]+ in TriggerUAF\(\) \(.*/stacktrace_test_fuzzer:arm64\+0x[0-9a-f]+\))",
#else
      R"(#0 0x[0-9a-f]+ in TriggerUAF\(\) testing/libfuzzer/tests/stacktrace_test_fuzzer.cc:[0-9]+:[0-9]+)",
#endif
  };

  EXPECT_THAT(output, ContainsRegex(base::JoinString(kRegexLines, "\n *")))
      << output;  // Print unescaped stack trace for easier debugging.

#endif  // defined(ADDRESS_SANITIZER)
}

#endif  // !BUILDFLAG(IS_UBSAN) && !BUILDFLAG(IS_UBSAN_SECURITY)

// RE2 and thus GTest compile regexes without "dotall" semantics, so '.' does
// not match newlines and ".*"  matches a single line at most. This regex
// can be used instead to match any number of lines.
constexpr std::string_view kSkipLines = R"([\s\S]*)";

// If the format of the check failure changes, then ClusterFuzz's regex
// should be adjusted to match. See e.g. https://crbug.com/443678564.
constexpr std::string_view kCheckFailedLineRegex =
    R"(\[[^:]*:FATAL:[^:]*[/\\]stacktrace_test_fuzzer.cc:24\] Check failed: false. *)";

// ChromeOS has a different prefix for its logs.
constexpr std::string_view kCheckFailedLineRegexChromeOs =
    R"(.* FATAL stacktrace_test_fuzzer: \[testing/libfuzzer/tests/stacktrace_test_fuzzer.cc:24\] Check failed: false. *)";

// Printed by libfuzzer's signal handler.
constexpr std::string_view kLibfuzzerSignalRegex =
    R"(==\d+== ERROR: libFuzzer: deadly signal)";

// Printed by ASan when it cannot or has not symbolized the stack frame.
constexpr std::string_view kUnsymbolizedAsanStackFrameRegex =
    R"(#0 0x[0-9a-f]+ <unknown>)";

// Topmost stack frame as printed by libfuzzer.
constexpr std::string_view kTopLibfuzzerStackFrameRegex =
    R"(#0 0x[0-9a-f]+ in __sanitizer_print_stack_trace .*)";

// `TriggerCheck()` stack frame as printed by libfuzzer.
constexpr std::string_view kTriggerCheckLibfuzzerStackFrameRegex =
    R"(#\d+ 0x[0-9a-f]+ in TriggerCheck\(\) testing/libfuzzer/tests/stacktrace_test_fuzzer\.cc:24:3)";

std::string JoinRegexLines(const std::vector<std::string_view>& lines) {
  return base::JoinString(lines, R"(\r?\n[ \t]*)");
}

std::string CheckFailureStackRegexLinux() {
  return JoinRegexLines({
      kCheckFailedLineRegex,
      kSkipLines,
      R"(#\d+ 0x[0-9a-f]+ TriggerCheck\(\).*)",
      kSkipLines,
      kLibfuzzerSignalRegex,
      kSkipLines,
      kTriggerCheckLibfuzzerStackFrameRegex,
  });
}

std::string CheckFailureStackRegexLinuxAsan64Bit() {
  return JoinRegexLines({
      kCheckFailedLineRegex,
      // TODO(https://crbug.com/40948553): Expect `TriggerCheck` here or in the
      // libfuzzer stack trace below.
      kUnsymbolizedAsanStackFrameRegex,
      kSkipLines,
      kLibfuzzerSignalRegex,
      // This part has symbols, but the stack does not include `TriggerCheck`.
      kTopLibfuzzerStackFrameRegex,
  });
}

std::string CheckFailureStackRegexLinuxCentipedeAsan64Bit() {
  return JoinRegexLines({
      kCheckFailedLineRegex,
      kUnsymbolizedAsanStackFrameRegex,
      kSkipLines,
  });
}

std::string CheckFailureStackRegexLinuxAsan32Bit() {
  return JoinRegexLines({
      kCheckFailedLineRegex,
      kUnsymbolizedAsanStackFrameRegex,
      kSkipLines,
      kLibfuzzerSignalRegex,
      kSkipLines,
      kTriggerCheckLibfuzzerStackFrameRegex,
  });
}

std::string CheckFailureStackRegexChromeOs() {
  return JoinRegexLines({
      kCheckFailedLineRegexChromeOs,
      // TODO(https://crbug.com/40948553): Expect `TriggerCheck` here or in the
      // libfuzzer stack trace below.
      kUnsymbolizedAsanStackFrameRegex,
      kSkipLines,
      kLibfuzzerSignalRegex,
      // This part has symbols, but the stack does not include `TriggerCheck`.
      kTopLibfuzzerStackFrameRegex,
  });
}

std::string CheckFailureStackRegexWin() {
  return JoinRegexLines({
      kCheckFailedLineRegex,
      // TODO(https:/crbug.com/40948553): Expect symbols instead.
      R"(Symbols not available\. Dumping unresolved backtrace:)",
      R"(0x[0-9a-f]+)",
  });
}

std::string CheckFailureStackRegexMacArm64() {
  return JoinRegexLines({
      kCheckFailedLineRegex,
      kSkipLines,
      R"(\d+ +stacktrace_test_fuzzer +0x[0-9a-f]+ .*TriggerCheck.* \+ \d+)",
      kSkipLines,
      kLibfuzzerSignalRegex,
      kSkipLines,
      R"(#\d+ 0x[0-9a-f]+ in TriggerCheck\(\) \(.*/stacktrace_test_fuzzer:arm64\+0x[0-9a-f]+\))",
  });
}

std::string CheckFailureStackRegex() {
#if BUILDFLAG(IS_LINUX) && defined(ADDRESS_SANITIZER) && \
    defined(ARCH_CPU_32_BITS)
  return CheckFailureStackRegexLinuxAsan32Bit();
#elif BUILDFLAG(IS_LINUX) && BUILDFLAG(USE_CENTIPEDE) && \
    defined(ADDRESS_SANITIZER)
  return CheckFailureStackRegexLinuxCentipedeAsan64Bit();
#elif BUILDFLAG(IS_LINUX) && defined(ADDRESS_SANITIZER)
  return CheckFailureStackRegexLinuxAsan64Bit();
#elif BUILDFLAG(IS_LINUX)
  return CheckFailureStackRegexLinux();
#elif BUILDFLAG(IS_CHROMEOS)
  return CheckFailureStackRegexChromeOs();
#elif BUILDFLAG(IS_MAC) && defined(ARCH_CPU_ARM64)
  return CheckFailureStackRegexMacArm64();
#elif BUILDFLAG(IS_WIN)
  return CheckFailureStackRegexWin();
#endif
}

// This test mostly exists to silence unused code compiler warnings. It is
// useful to define and compile all regexes in all build configurations, to
// surface any errors quickly during local development builds.
TEST(FuzzerStacktraceTest, CheckFailureRegexesAreValid) {
  EXPECT_EQ(re2::RE2(CheckFailureStackRegexLinux()).error(), "");
  EXPECT_EQ(re2::RE2(CheckFailureStackRegexLinuxAsan32Bit()).error(), "");
  EXPECT_EQ(re2::RE2(CheckFailureStackRegexLinuxAsan64Bit()).error(), "");
  EXPECT_EQ(re2::RE2(CheckFailureStackRegexLinuxCentipedeAsan64Bit()).error(),
            "");
  EXPECT_EQ(re2::RE2(CheckFailureStackRegexChromeOs()).error(), "");
  EXPECT_EQ(re2::RE2(CheckFailureStackRegexMacArm64()).error(), "");
  EXPECT_EQ(re2::RE2(CheckFailureStackRegexWin()).error(), "");
}

// Fuzzer fails to run under MSan.
// TODO(https://crbug.com/326101784): Re-enable this once MSan build is fixed.
#if defined(MEMORY_SANITIZER)
#define MAYBE_SymbolizesCheck DISABLED_SymbolizesCheck
#else
#define MAYBE_SymbolizesCheck SymbolizesCheck
#endif
TEST(FuzzerStacktraceTest, MAYBE_SymbolizesCheck) {
  base::CommandLine cmd(FuzzerPath());
  cmd.AppendArgPath(FuzzerInputPath("check"));

  std::string output;
  EXPECT_FALSE(base::GetAppOutputAndError(cmd, &output));  // Target crashes.

  EXPECT_THAT(output, ContainsRegex(CheckFailureStackRegex()))
      << output;  // Print unescaped stack trace for easier debugging.

  // If the regex does not mention `TriggerCheck`, then check that the stack
  // trace does not contain it either. This checks that the stack trace is not
  // unexpectedly symbolized.
  if (CheckFailureStackRegex().find("TriggerCheck") == std::string::npos) {
    EXPECT_THAT(output, Not(HasSubstr("TriggerCheck"))) << output;
  }
}

}  // namespace