1// RUN: %clangxx -O0 %s -o %t
2// RUN: %env_tool_opts=strip_path_prefix=/TestCases/ %run %t 2>&1 | FileCheck %s
3//
4// Tests __sanitizer_symbolize_pc.
5
6// FIXME: Investigate why it does not print GLOBAL_VAR_ABC.
7// XFAIL: hwasan && target=aarch64{{.*}}
8// LSan tests fail on Darwin
9// UNSUPPORTED: darwin && lsan
10// tsan and ubsan are supported on darwin, but they currently fail
11// on some platforms likely because the test platform is too old
12// UNSUPPORTED: darwin && tsan
13// UNSUPPORTED: darwin && ubsan
14
15#include <stdio.h>
16#include <sanitizer/common_interface_defs.h>
17
18int GLOBAL_VAR_ABC;
19
20void SymbolizeSmallBuffer() {
21 char data[] = "abcdef";
22 __sanitizer_symbolize_pc(__sanitizer_return_address(), fmt: "%p %F %L", out_buf: data, out_buf_size: 0);
23 printf(format: "UNCHANGED '%s'\n", data);
24 __sanitizer_symbolize_pc(__sanitizer_return_address(), fmt: "%p %F %L", out_buf: data, out_buf_size: 1);
25 printf(format: "EMPTY '%s'\n", data);
26 __sanitizer_symbolize_pc(__sanitizer_return_address(), fmt: "%p %F %L", out_buf: data,
27 out_buf_size: sizeof(data));
28 printf(format: "PARTIAL '%s'\n", data);
29}
30
31void SymbolizeCaller() {
32 char data[100];
33 __sanitizer_symbolize_pc(__sanitizer_return_address(), fmt: "%p %F %L", out_buf: data,
34 out_buf_size: sizeof(data));
35 printf(format: "FIRST_FORMAT %s\n", data);
36 __sanitizer_symbolize_pc(__sanitizer_return_address(),
37 fmt: "FUNC:%f LINE:%l FILE:%s", out_buf: data, out_buf_size: sizeof(data));
38 printf(format: "SECOND_FORMAT %s\n", data);
39 __sanitizer_symbolize_pc(__sanitizer_return_address(),
40 fmt: "LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"
41 "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"
42 "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"
43 "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"
44 "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG"
45 "FUNC:%f LINE:%l FILE:%s",
46 out_buf: data, out_buf_size: sizeof(data));
47 printf(format: "LONG_FORMAT %s\n", data);
48}
49
50struct s {
51 int i;
52};
53
54struct s SymbolizeSRet() {
55 char data[100];
56 __sanitizer_symbolize_pc(__sanitizer_return_address(),
57 fmt: "FUNC:%f LINE:%l FILE:%s", out_buf: data, out_buf_size: sizeof(data));
58 printf(format: "SRET: %s\n", data);
59 struct s s = {.i: 1};
60 return s;
61}
62
63void SymbolizeData() {
64 char data[100];
65 __sanitizer_symbolize_global(data_ptr: &GLOBAL_VAR_ABC, fmt: "%g %s:%l", out_buf: data, out_buf_size: sizeof(data));
66 printf(format: "GLOBAL: %s\n", data);
67}
68
69int main() {
70 // CHECK: UNCHANGED 'abcdef'
71 // CHECK: EMPTY ''
72 // CHECK: PARTIAL '0x{{.*}}'
73 SymbolizeSmallBuffer();
74
75 // CHECK: FIRST_FORMAT 0x{{.*}} in main symbolize_pc.cpp:[[@LINE+2]]
76 // CHECK: SECOND_FORMAT FUNC:main LINE:[[@LINE+1]] FILE:symbolize_pc.cpp
77 SymbolizeCaller();
78
79 struct s s;
80 // CHECK: SRET: FUNC:main LINE:[[@LINE+1]] FILE:symbolize_pc.cpp
81 s = SymbolizeSRet();
82
83 // CHECK: GLOBAL: GLOBAL_VAR_ABC
84 SymbolizeData();
85}
86

source code of compiler-rt/test/sanitizer_common/TestCases/symbolize_pc.cpp