1 //===-- sanitizer_stacktrace.cc -------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries.
10 //===----------------------------------------------------------------------===//
11 
12 #include "sanitizer_common.h"
13 #include "sanitizer_procmaps.h"
14 #include "sanitizer_stacktrace.h"
15 #include "sanitizer_symbolizer.h"
16 
17 namespace __sanitizer {
StripPathPrefix(const char * filepath,const char * strip_file_prefix)18 const char *StripPathPrefix(const char *filepath,
19                             const char *strip_file_prefix) {
20   if (filepath == 0) return 0;
21   if (filepath == internal_strstr(filepath, strip_file_prefix))
22     return filepath + internal_strlen(strip_file_prefix);
23   return filepath;
24 }
25 
26 // ----------------------- StackTrace ----------------------------- {{{1
GetPreviousInstructionPc(uptr pc)27 uptr StackTrace::GetPreviousInstructionPc(uptr pc) {
28 #ifdef __arm__
29   // Cancel Thumb bit.
30   pc = pc & (~1);
31 #endif
32 #if defined(__powerpc__) || defined(__powerpc64__)
33   // PCs are always 4 byte aligned.
34   return pc - 4;
35 #elif defined(__sparc__)
36   return pc - 8;
37 #else
38   return pc - 1;
39 #endif
40 }
41 
PrintStackFramePrefix(uptr frame_num,uptr pc)42 static void PrintStackFramePrefix(uptr frame_num, uptr pc) {
43   Printf("    #%zu 0x%zx", frame_num, pc);
44 }
45 
PrintSourceLocation(const char * file,int line,int column,const char * strip_file_prefix)46 static void PrintSourceLocation(const char *file, int line, int column,
47                                 const char *strip_file_prefix) {
48   CHECK(file);
49   Printf(" %s", StripPathPrefix(file, strip_file_prefix));
50   if (line > 0) {
51     Printf(":%d", line);
52     if (column > 0)
53       Printf(":%d", column);
54   }
55 }
56 
PrintModuleAndOffset(const char * module,uptr offset,const char * strip_file_prefix)57 static void PrintModuleAndOffset(const char *module, uptr offset,
58                                  const char *strip_file_prefix) {
59   Printf(" (%s+0x%zx)", StripPathPrefix(module, strip_file_prefix), offset);
60 }
61 
PrintStack(const uptr * addr,uptr size,bool symbolize,const char * strip_file_prefix,SymbolizeCallback symbolize_callback)62 void StackTrace::PrintStack(const uptr *addr, uptr size,
63                             bool symbolize, const char *strip_file_prefix,
64                             SymbolizeCallback symbolize_callback ) {
65   MemoryMappingLayout proc_maps;
66   InternalScopedBuffer<char> buff(GetPageSizeCached() * 2);
67   InternalScopedBuffer<AddressInfo> addr_frames(64);
68   uptr frame_num = 0;
69   for (uptr i = 0; i < size && addr[i]; i++) {
70     // PCs in stack traces are actually the return addresses, that is,
71     // addresses of the next instructions after the call.
72     uptr pc = GetPreviousInstructionPc(addr[i]);
73     uptr addr_frames_num = 0;  // The number of stack frames for current
74                                // instruction address.
75     if (symbolize_callback) {
76       if (symbolize_callback((void*)pc, buff.data(), buff.size())) {
77         addr_frames_num = 1;
78         PrintStackFramePrefix(frame_num, pc);
79         // We can't know anything about the string returned by external
80         // symbolizer, but if it starts with filename, try to strip path prefix
81         // from it.
82         Printf(" %s\n", StripPathPrefix(buff.data(), strip_file_prefix));
83         frame_num++;
84       }
85     }
86     if (symbolize && addr_frames_num == 0) {
87       // Use our own (online) symbolizer, if necessary.
88       addr_frames_num = SymbolizeCode(pc, addr_frames.data(),
89                                       addr_frames.size());
90       for (uptr j = 0; j < addr_frames_num; j++) {
91         AddressInfo &info = addr_frames[j];
92         PrintStackFramePrefix(frame_num, pc);
93         if (info.function) {
94           Printf(" in %s", info.function);
95         }
96         if (info.file) {
97           PrintSourceLocation(info.file, info.line, info.column,
98                               strip_file_prefix);
99         } else if (info.module) {
100           PrintModuleAndOffset(info.module, info.module_offset,
101                                strip_file_prefix);
102         }
103         Printf("\n");
104         info.Clear();
105         frame_num++;
106       }
107     }
108     if (addr_frames_num == 0) {
109       // If online symbolization failed, try to output at least module and
110       // offset for instruction.
111       PrintStackFramePrefix(frame_num, pc);
112       uptr offset;
113       if (proc_maps.GetObjectNameAndOffset(pc, &offset,
114                                            buff.data(), buff.size())) {
115         PrintModuleAndOffset(buff.data(), offset, strip_file_prefix);
116       }
117       Printf("\n");
118       frame_num++;
119     }
120   }
121 }
122 
GetCurrentPc()123 uptr StackTrace::GetCurrentPc() {
124   return GET_CALLER_PC();
125 }
126 
FastUnwindStack(uptr pc,uptr bp,uptr stack_top,uptr stack_bottom)127 void StackTrace::FastUnwindStack(uptr pc, uptr bp,
128                                  uptr stack_top, uptr stack_bottom) {
129   CHECK(size == 0 && trace[0] == pc);
130   size = 1;
131   uhwptr *frame = (uhwptr *)bp;
132   uhwptr *prev_frame = frame - 1;
133   // Avoid infinite loop when frame == frame[0] by using frame > prev_frame.
134   while (frame > prev_frame &&
135          frame < (uhwptr *)stack_top - 2 &&
136          frame > (uhwptr *)stack_bottom &&
137          size < max_size) {
138     uhwptr pc1 = frame[1];
139     if (pc1 != pc) {
140       trace[size++] = (uptr) pc1;
141     }
142     prev_frame = frame;
143     frame = (uhwptr *)frame[0];
144   }
145 }
146 
PopStackFrames(uptr count)147 void StackTrace::PopStackFrames(uptr count) {
148   CHECK(size >= count);
149   size -= count;
150   for (uptr i = 0; i < size; i++) {
151     trace[i] = trace[i + count];
152   }
153 }
154 
155 // On 32-bits we don't compress stack traces.
156 // On 64-bits we compress stack traces: if a given pc differes slightly from
157 // the previous one, we record a 31-bit offset instead of the full pc.
158 SANITIZER_INTERFACE_ATTRIBUTE
CompressStack(StackTrace * stack,u32 * compressed,uptr size)159 uptr StackTrace::CompressStack(StackTrace *stack, u32 *compressed, uptr size) {
160 #if SANITIZER_WORDSIZE == 32
161   // Don't compress, just copy.
162   uptr res = 0;
163   for (uptr i = 0; i < stack->size && i < size; i++) {
164     compressed[i] = stack->trace[i];
165     res++;
166   }
167   if (stack->size < size)
168     compressed[stack->size] = 0;
169 #else  // 64 bits, compress.
170   uptr prev_pc = 0;
171   const uptr kMaxOffset = (1ULL << 30) - 1;
172   uptr c_index = 0;
173   uptr res = 0;
174   for (uptr i = 0, n = stack->size; i < n; i++) {
175     uptr pc = stack->trace[i];
176     if (!pc) break;
177     if ((s64)pc < 0) break;
178     // Printf("C pc[%zu] %zx\n", i, pc);
179     if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) {
180       uptr offset = (s64)(pc - prev_pc);
181       offset |= (1U << 31);
182       if (c_index >= size) break;
183       // Printf("C co[%zu] offset %zx\n", i, offset);
184       compressed[c_index++] = offset;
185     } else {
186       uptr hi = pc >> 32;
187       uptr lo = (pc << 32) >> 32;
188       CHECK_EQ((hi & (1 << 31)), 0);
189       if (c_index + 1 >= size) break;
190       // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo);
191       compressed[c_index++] = hi;
192       compressed[c_index++] = lo;
193     }
194     res++;
195     prev_pc = pc;
196   }
197   if (c_index < size)
198     compressed[c_index] = 0;
199   if (c_index + 1 < size)
200     compressed[c_index + 1] = 0;
201 #endif  // SANITIZER_WORDSIZE
202 
203   // debug-only code
204 #if 0
205   StackTrace check_stack;
206   UncompressStack(&check_stack, compressed, size);
207   if (res < check_stack.size) {
208     Printf("res %zu check_stack.size %zu; c_size %zu\n", res,
209            check_stack.size, size);
210   }
211   // |res| may be greater than check_stack.size, because
212   // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames.
213   CHECK(res >= check_stack.size);
214   CHECK_EQ(0, REAL(memcmp)(check_stack.trace, stack->trace,
215                           check_stack.size * sizeof(uptr)));
216 #endif
217 
218   return res;
219 }
220 
221 SANITIZER_INTERFACE_ATTRIBUTE
UncompressStack(StackTrace * stack,u32 * compressed,uptr size)222 void StackTrace::UncompressStack(StackTrace *stack,
223                                  u32 *compressed, uptr size) {
224 #if SANITIZER_WORDSIZE == 32
225   // Don't uncompress, just copy.
226   stack->size = 0;
227   for (uptr i = 0; i < size && i < kStackTraceMax; i++) {
228     if (!compressed[i]) break;
229     stack->size++;
230     stack->trace[i] = compressed[i];
231   }
232 #else  // 64 bits, uncompress
233   uptr prev_pc = 0;
234   stack->size = 0;
235   for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) {
236     u32 x = compressed[i];
237     uptr pc = 0;
238     if (x & (1U << 31)) {
239       // Printf("U co[%zu] offset: %x\n", i, x);
240       // this is an offset
241       s32 offset = x;
242       offset = (offset << 1) >> 1;  // remove the 31-byte and sign-extend.
243       pc = prev_pc + offset;
244       CHECK(pc);
245     } else {
246       // CHECK(i + 1 < size);
247       if (i + 1 >= size) break;
248       uptr hi = x;
249       uptr lo = compressed[i+1];
250       // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo);
251       i++;
252       pc = (hi << 32) | lo;
253       if (!pc) break;
254     }
255     // Printf("U pc[%zu] %zx\n", stack->size, pc);
256     stack->trace[stack->size++] = pc;
257     prev_pc = pc;
258   }
259 #endif  // SANITIZER_WORDSIZE
260 }
261 
262 }  // namespace __sanitizer
263