Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / sanitizer_common / tests / sanitizer_stacktrace_printer_test.cpp
blob9602ba31621e64723cb221c2b1a16227d6bc87ed
1 //===-- sanitizer_common_printer_test.cpp ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of sanitizer_common test suite.
11 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common/sanitizer_stacktrace_printer.h"
14 #include "gtest/gtest.h"
15 #include "interception/interception.h"
17 namespace __sanitizer {
19 class TestFormattedStackTracePrinter final : public FormattedStackTracePrinter {
20 public:
21 ~TestFormattedStackTracePrinter() {}
24 TEST(FormattedStackTracePrinter, RenderSourceLocation) {
25 InternalScopedString str;
26 TestFormattedStackTracePrinter printer;
28 printer.RenderSourceLocation(&str, "/dir/file.cc", 10, 5, false, "");
29 EXPECT_STREQ("/dir/file.cc:10:5", str.data());
31 str.clear();
32 printer.RenderSourceLocation(&str, "/dir/file.cc", 11, 0, false, "");
33 EXPECT_STREQ("/dir/file.cc:11", str.data());
35 str.clear();
36 printer.RenderSourceLocation(&str, "/dir/file.cc", 0, 0, false, "");
37 EXPECT_STREQ("/dir/file.cc", str.data());
39 str.clear();
40 printer.RenderSourceLocation(&str, "/dir/file.cc", 10, 5, false, "/dir/");
41 EXPECT_STREQ("file.cc:10:5", str.data());
43 str.clear();
44 printer.RenderSourceLocation(&str, "/dir/file.cc", 10, 5, true, "");
45 EXPECT_STREQ("/dir/file.cc(10,5)", str.data());
47 str.clear();
48 printer.RenderSourceLocation(&str, "/dir/file.cc", 11, 0, true, "");
49 EXPECT_STREQ("/dir/file.cc(11)", str.data());
51 str.clear();
52 printer.RenderSourceLocation(&str, "/dir/file.cc", 0, 0, true, "");
53 EXPECT_STREQ("/dir/file.cc", str.data());
55 str.clear();
56 printer.RenderSourceLocation(&str, "/dir/file.cc", 10, 5, true, "/dir/");
57 EXPECT_STREQ("file.cc(10,5)", str.data());
60 TEST(FormattedStackTracePrinter, RenderModuleLocation) {
61 InternalScopedString str;
62 TestFormattedStackTracePrinter printer;
63 printer.RenderModuleLocation(&str, "/dir/exe", 0x123, kModuleArchUnknown, "");
64 EXPECT_STREQ("(/dir/exe+0x123)", str.data());
66 // Check that we strip file prefix if necessary.
67 str.clear();
68 printer.RenderModuleLocation(&str, "/dir/exe", 0x123, kModuleArchUnknown,
69 "/dir/");
70 EXPECT_STREQ("(exe+0x123)", str.data());
72 // Check that we render the arch.
73 str.clear();
74 printer.RenderModuleLocation(&str, "/dir/exe", 0x123, kModuleArchX86_64H,
75 "/dir/");
76 EXPECT_STREQ("(exe:x86_64h+0x123)", str.data());
79 TEST(FormattedStackTracePrinter, RenderFrame) {
80 TestFormattedStackTracePrinter printer;
81 int frame_no = 42;
82 AddressInfo info;
83 info.address = 0x400000;
84 info.module = internal_strdup("/path/to/my/module");
85 info.module_offset = 0x200;
86 info.function = internal_strdup("foo");
87 info.function_offset = 0x100;
88 info.file = internal_strdup("/path/to/my/source");
89 info.line = 10;
90 info.column = 5;
91 InternalScopedString str;
93 // Dump all the AddressInfo fields.
94 printer.RenderFrame(&str,
95 "%% Frame:%n PC:%p Module:%m ModuleOffset:%o "
96 "Function:%f FunctionOffset:%q Source:%s Line:%l "
97 "Column:%c",
98 frame_no, info.address, &info, false, "/path/to/");
99 EXPECT_STREQ("% Frame:42 PC:0x400000 Module:my/module ModuleOffset:0x200 "
100 "Function:foo FunctionOffset:0x100 Source:my/source Line:10 "
101 "Column:5",
102 str.data());
104 str.clear();
105 // Check that RenderFrame() strips interceptor prefixes.
106 info.function = internal_strdup(SANITIZER_STRINGIFY(WRAP(bar)));
107 printer.RenderFrame(&str,
108 "%% Frame:%n PC:%p Module:%m ModuleOffset:%o "
109 "Function:%f FunctionOffset:%q Source:%s Line:%l "
110 "Column:%c",
111 frame_no, info.address, &info, false, "/path/to/");
112 EXPECT_STREQ("% Frame:42 PC:0x400000 Module:my/module ModuleOffset:0x200 "
113 "Function:bar FunctionOffset:0x100 Source:my/source Line:10 "
114 "Column:5",
115 str.data());
116 info.Clear();
117 str.clear();
119 // Test special format specifiers.
120 info.address = 0x400000;
121 printer.RenderFrame(&str, "%M", frame_no, info.address, &info, false);
122 EXPECT_NE(nullptr, internal_strstr(str.data(), "400000"));
123 str.clear();
125 printer.RenderFrame(&str, "%L", frame_no, info.address, &info, false);
126 EXPECT_STREQ("(<unknown module>)", str.data());
127 str.clear();
129 info.module = internal_strdup("/path/to/module");
130 info.module_offset = 0x200;
131 printer.RenderFrame(&str, "%M", frame_no, info.address, &info, false);
132 EXPECT_NE(nullptr, internal_strstr(str.data(), "(module+0x"));
133 EXPECT_NE(nullptr, internal_strstr(str.data(), "200"));
134 str.clear();
136 printer.RenderFrame(&str, "%L", frame_no, info.address, &info, false);
137 EXPECT_STREQ("(/path/to/module+0x200)", str.data());
138 str.clear();
140 printer.RenderFrame(&str, "%b", frame_no, info.address, &info, false);
141 EXPECT_STREQ("", str.data());
142 str.clear();
144 info.uuid_size = 2;
145 info.uuid[0] = 0x55;
146 info.uuid[1] = 0x66;
148 printer.RenderFrame(&str, "%M", frame_no, info.address, &info, false);
149 EXPECT_NE(nullptr, internal_strstr(str.data(), "(module+0x"));
150 EXPECT_NE(nullptr, internal_strstr(str.data(), "200"));
151 #if SANITIZER_APPLE
152 EXPECT_EQ(nullptr, internal_strstr(str.data(), "BuildId: 5566"));
153 #else
154 EXPECT_NE(nullptr, internal_strstr(str.data(), "BuildId: 5566"));
155 #endif
156 str.clear();
158 printer.RenderFrame(&str, "%L", frame_no, info.address, &info, false);
159 #if SANITIZER_APPLE
160 EXPECT_STREQ("(/path/to/module+0x200)", str.data());
161 #else
162 EXPECT_STREQ("(/path/to/module+0x200) (BuildId: 5566)", str.data());
163 #endif
164 str.clear();
166 printer.RenderFrame(&str, "%b", frame_no, info.address, &info, false);
167 EXPECT_STREQ("(BuildId: 5566)", str.data());
168 str.clear();
170 info.function = internal_strdup("my_function");
171 printer.RenderFrame(&str, "%F", frame_no, info.address, &info, false);
172 EXPECT_STREQ("in my_function", str.data());
173 str.clear();
175 info.function_offset = 0x100;
176 printer.RenderFrame(&str, "%F %S", frame_no, info.address, &info, false);
177 EXPECT_STREQ("in my_function+0x100 <null>", str.data());
178 str.clear();
180 info.file = internal_strdup("my_file");
181 printer.RenderFrame(&str, "%F %S", frame_no, info.address, &info, false);
182 EXPECT_STREQ("in my_function my_file", str.data());
183 str.clear();
185 info.line = 10;
186 printer.RenderFrame(&str, "%F %S", frame_no, info.address, &info, false);
187 EXPECT_STREQ("in my_function my_file:10", str.data());
188 str.clear();
190 info.column = 5;
191 printer.RenderFrame(&str, "%S %L", frame_no, info.address, &info, false);
192 EXPECT_STREQ("my_file:10:5 my_file:10:5", str.data());
193 str.clear();
195 printer.RenderFrame(&str, "%S %L", frame_no, info.address, &info, true);
196 EXPECT_STREQ("my_file(10,5) my_file(10,5)", str.data());
197 str.clear();
199 info.column = 0;
200 printer.RenderFrame(&str, "%F %S", frame_no, info.address, &info, true);
201 EXPECT_STREQ("in my_function my_file(10)", str.data());
202 str.clear();
204 info.line = 0;
205 printer.RenderFrame(&str, "%F %S", frame_no, info.address, &info, true);
206 EXPECT_STREQ("in my_function my_file", str.data());
207 str.clear();
209 info.Clear();
212 } // namespace __sanitizer