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 #include <stdio.h>
6 #include <sanitizer/common_interface_defs.h>
7 
8 int GLOBAL_VAR_ABC;
9 
SymbolizeSmallBuffer()10 void SymbolizeSmallBuffer() {
11   char data[] = "abcdef";
12   __sanitizer_symbolize_pc(__builtin_return_address(0), "%p %F %L", data, 0);
13   printf("UNCHANGED '%s'\n", data);
14   __sanitizer_symbolize_pc(__builtin_return_address(0), "%p %F %L", data, 1);
15   printf("EMPTY '%s'\n", data);
16   __sanitizer_symbolize_pc(__builtin_return_address(0), "%p %F %L", data,
17                            sizeof(data));
18   printf("PARTIAL '%s'\n", data);
19 }
20 
SymbolizeCaller()21 void SymbolizeCaller() {
22   char data[100];
23   __sanitizer_symbolize_pc(__builtin_return_address(0), "%p %F %L", data,
24                            sizeof(data));
25   printf("FIRST_FORMAT %s\n", data);
26   __sanitizer_symbolize_pc(__builtin_return_address(0),
27                            "FUNC:%f LINE:%l FILE:%s", data, sizeof(data));
28   printf("SECOND_FORMAT %s\n", data);
29   __sanitizer_symbolize_pc(__builtin_return_address(0),
30                           "LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"
31                           "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"
32                           "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"
33                           "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"
34                           "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG"
35                           "FUNC:%f LINE:%l FILE:%s", data, sizeof(data));
36   printf("LONG_FORMAT %s\n", data);
37 }
38 
SymbolizeData()39 void SymbolizeData() {
40   char data[100];
41   __sanitizer_symbolize_global(&GLOBAL_VAR_ABC, "%g %s:%l", data, sizeof(data));
42   printf("GLOBAL: %s\n", data);
43 }
44 
main()45 int main() {
46   // CHECK: UNCHANGED 'abcdef'
47   // CHECK: EMPTY ''
48   // CHECK: PARTIAL '0x{{.*}}'
49   SymbolizeSmallBuffer();
50 
51   // CHECK: FIRST_FORMAT 0x{{.*}} in main symbolize_pc.cpp:[[@LINE+2]]
52   // CHECK: SECOND_FORMAT FUNC:main LINE:[[@LINE+1]] FILE:symbolize_pc.cpp
53   SymbolizeCaller();
54 
55   // CHECK: GLOBAL: GLOBAL_VAR_ABC
56   SymbolizeData();
57 }
58