1// Copyright (c) 2005, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30// ---
31// Author: Sanjay Ghemawat
32//
33// Produce stack trace
34
35#ifndef BASE_STACKTRACE_X86_INL_H_
36#define BASE_STACKTRACE_X86_INL_H_
37// Note: this file is included into stacktrace.cc more than once.
38// Anything that should only be defined once should be here:
39
40#include "config.h"
41#include <stdlib.h>   // for NULL
42#include <assert.h>
43#if defined(HAVE_SYS_UCONTEXT_H)
44#include <sys/ucontext.h>
45#elif defined(HAVE_UCONTEXT_H)
46#include <ucontext.h>  // for ucontext_t
47#elif defined(HAVE_CYGWIN_SIGNAL_H)
48// cygwin/signal.h has a buglet where it uses pthread_attr_t without
49// #including <pthread.h> itself.  So we have to do it.
50# ifdef HAVE_PTHREAD
51# include <pthread.h>
52# endif
53#include <cygwin/signal.h>
54typedef ucontext ucontext_t;
55#endif
56#ifdef HAVE_STDINT_H
57#include <stdint.h>   // for uintptr_t
58#endif
59#ifdef HAVE_UNISTD_H
60#include <unistd.h>
61#endif
62#ifdef HAVE_MMAP
63#include <sys/mman.h> // for msync
64#include "base/vdso_support.h"
65#endif
66
67#include "gperftools/stacktrace.h"
68
69#if defined(__linux__) && defined(__i386__) && defined(__ELF__) && defined(HAVE_MMAP)
70// Count "push %reg" instructions in VDSO __kernel_vsyscall(),
71// preceeding "syscall" or "sysenter".
72// If __kernel_vsyscall uses frame pointer, answer 0.
73//
74// kMaxBytes tells how many instruction bytes of __kernel_vsyscall
75// to analyze before giving up. Up to kMaxBytes+1 bytes of
76// instructions could be accessed.
77//
78// Here are known __kernel_vsyscall instruction sequences:
79//
80// SYSENTER (linux-2.6.26/arch/x86/vdso/vdso32/sysenter.S).
81// Used on Intel.
82//  0xffffe400 <__kernel_vsyscall+0>:       push   %ecx
83//  0xffffe401 <__kernel_vsyscall+1>:       push   %edx
84//  0xffffe402 <__kernel_vsyscall+2>:       push   %ebp
85//  0xffffe403 <__kernel_vsyscall+3>:       mov    %esp,%ebp
86//  0xffffe405 <__kernel_vsyscall+5>:       sysenter
87//
88// SYSCALL (see linux-2.6.26/arch/x86/vdso/vdso32/syscall.S).
89// Used on AMD.
90//  0xffffe400 <__kernel_vsyscall+0>:       push   %ebp
91//  0xffffe401 <__kernel_vsyscall+1>:       mov    %ecx,%ebp
92//  0xffffe403 <__kernel_vsyscall+3>:       syscall
93//
94// i386 (see linux-2.6.26/arch/x86/vdso/vdso32/int80.S)
95//  0xffffe400 <__kernel_vsyscall+0>:       int $0x80
96//  0xffffe401 <__kernel_vsyscall+1>:       ret
97//
98static const int kMaxBytes = 10;
99
100// We use assert()s instead of DCHECK()s -- this is too low level
101// for DCHECK().
102
103static int CountPushInstructions(const unsigned char *const addr) {
104  int result = 0;
105  for (int i = 0; i < kMaxBytes; ++i) {
106    if (addr[i] == 0x89) {
107      // "mov reg,reg"
108      if (addr[i + 1] == 0xE5) {
109        // Found "mov %esp,%ebp".
110        return 0;
111      }
112      ++i;  // Skip register encoding byte.
113    } else if (addr[i] == 0x0F &&
114               (addr[i + 1] == 0x34 || addr[i + 1] == 0x05)) {
115      // Found "sysenter" or "syscall".
116      return result;
117    } else if ((addr[i] & 0xF0) == 0x50) {
118      // Found "push %reg".
119      ++result;
120    } else if (addr[i] == 0xCD && addr[i + 1] == 0x80) {
121      // Found "int $0x80"
122      assert(result == 0);
123      return 0;
124    } else {
125      // Unexpected instruction.
126      assert(0 == "unexpected instruction in __kernel_vsyscall");
127      return 0;
128    }
129  }
130  // Unexpected: didn't find SYSENTER or SYSCALL in
131  // [__kernel_vsyscall, __kernel_vsyscall + kMaxBytes) interval.
132  assert(0 == "did not find SYSENTER or SYSCALL in __kernel_vsyscall");
133  return 0;
134}
135#endif
136
137// Given a pointer to a stack frame, locate and return the calling
138// stackframe, or return NULL if no stackframe can be found. Perform sanity
139// checks (the strictness of which is controlled by the boolean parameter
140// "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned.
141template<bool STRICT_UNWINDING, bool WITH_CONTEXT>
142static void **NextStackFrame(void **old_sp, const void *uc) {
143  void **new_sp = (void **) *old_sp;
144
145#if defined(__linux__) && defined(__i386__) && defined(HAVE_VDSO_SUPPORT)
146  if (WITH_CONTEXT && uc != NULL) {
147    // How many "push %reg" instructions are there at __kernel_vsyscall?
148    // This is constant for a given kernel and processor, so compute
149    // it only once.
150    static int num_push_instructions = -1;  // Sentinel: not computed yet.
151    // Initialize with sentinel value: __kernel_rt_sigreturn can not possibly
152    // be there.
153    static const unsigned char *kernel_rt_sigreturn_address = NULL;
154    static const unsigned char *kernel_vsyscall_address = NULL;
155    if (num_push_instructions == -1) {
156      base::VDSOSupport vdso;
157      if (vdso.IsPresent()) {
158        base::VDSOSupport::SymbolInfo rt_sigreturn_symbol_info;
159        base::VDSOSupport::SymbolInfo vsyscall_symbol_info;
160        if (!vdso.LookupSymbol("__kernel_rt_sigreturn", "LINUX_2.5",
161                               STT_FUNC, &rt_sigreturn_symbol_info) ||
162            !vdso.LookupSymbol("__kernel_vsyscall", "LINUX_2.5",
163                               STT_FUNC, &vsyscall_symbol_info) ||
164            rt_sigreturn_symbol_info.address == NULL ||
165            vsyscall_symbol_info.address == NULL) {
166          // Unexpected: 32-bit VDSO is present, yet one of the expected
167          // symbols is missing or NULL.
168          assert(0 == "VDSO is present, but doesn't have expected symbols");
169          num_push_instructions = 0;
170        } else {
171          kernel_rt_sigreturn_address =
172              reinterpret_cast<const unsigned char *>(
173                  rt_sigreturn_symbol_info.address);
174          kernel_vsyscall_address =
175              reinterpret_cast<const unsigned char *>(
176                  vsyscall_symbol_info.address);
177          num_push_instructions =
178              CountPushInstructions(kernel_vsyscall_address);
179        }
180      } else {
181        num_push_instructions = 0;
182      }
183    }
184    if (num_push_instructions != 0 && kernel_rt_sigreturn_address != NULL &&
185        old_sp[1] == kernel_rt_sigreturn_address) {
186      const ucontext_t *ucv = static_cast<const ucontext_t *>(uc);
187      // This kernel does not use frame pointer in its VDSO code,
188      // and so %ebp is not suitable for unwinding.
189      void **const reg_ebp =
190          reinterpret_cast<void **>(ucv->uc_mcontext.gregs[REG_EBP]);
191      const unsigned char *const reg_eip =
192          reinterpret_cast<unsigned char *>(ucv->uc_mcontext.gregs[REG_EIP]);
193      if (new_sp == reg_ebp &&
194          kernel_vsyscall_address <= reg_eip &&
195          reg_eip - kernel_vsyscall_address < kMaxBytes) {
196        // We "stepped up" to __kernel_vsyscall, but %ebp is not usable.
197        // Restore from 'ucv' instead.
198        void **const reg_esp =
199            reinterpret_cast<void **>(ucv->uc_mcontext.gregs[REG_ESP]);
200        // Check that alleged %esp is not NULL and is reasonably aligned.
201        if (reg_esp &&
202            ((uintptr_t)reg_esp & (sizeof(reg_esp) - 1)) == 0) {
203          // Check that alleged %esp is actually readable. This is to prevent
204          // "double fault" in case we hit the first fault due to e.g. stack
205          // corruption.
206          //
207          // page_size is linker-initalized to avoid async-unsafe locking
208          // that GCC would otherwise insert (__cxa_guard_acquire etc).
209          static int page_size;
210          if (page_size == 0) {
211            // First time through.
212            page_size = getpagesize();
213          }
214          void *const reg_esp_aligned =
215              reinterpret_cast<void *>(
216                  (uintptr_t)(reg_esp + num_push_instructions - 1) &
217                  ~(page_size - 1));
218          if (msync(reg_esp_aligned, page_size, MS_ASYNC) == 0) {
219            // Alleged %esp is readable, use it for further unwinding.
220            new_sp = reinterpret_cast<void **>(
221                reg_esp[num_push_instructions - 1]);
222          }
223        }
224      }
225    }
226  }
227#endif
228
229  // Check that the transition from frame pointer old_sp to frame
230  // pointer new_sp isn't clearly bogus
231  if (STRICT_UNWINDING) {
232    // With the stack growing downwards, older stack frame must be
233    // at a greater address that the current one.
234    if (new_sp <= old_sp) return NULL;
235    // Assume stack frames larger than 100,000 bytes are bogus.
236    if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL;
237  } else {
238    // In the non-strict mode, allow discontiguous stack frames.
239    // (alternate-signal-stacks for example).
240    if (new_sp == old_sp) return NULL;
241    if (new_sp > old_sp) {
242      // And allow frames upto about 1MB.
243      const uintptr_t delta = (uintptr_t)new_sp - (uintptr_t)old_sp;
244      const uintptr_t acceptable_delta = 1000000;
245      if (delta > acceptable_delta) {
246        return NULL;
247      }
248    }
249  }
250  if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL;
251#ifdef __i386__
252  // On 64-bit machines, the stack pointer can be very close to
253  // 0xffffffff, so we explicitly check for a pointer into the
254  // last two pages in the address space
255  if ((uintptr_t)new_sp >= 0xffffe000) return NULL;
256#endif
257#ifdef HAVE_MMAP
258  if (!STRICT_UNWINDING) {
259    // Lax sanity checks cause a crash on AMD-based machines with
260    // VDSO-enabled kernels.
261    // Make an extra sanity check to insure new_sp is readable.
262    // Note: NextStackFrame<false>() is only called while the program
263    //       is already on its last leg, so it's ok to be slow here.
264    static int page_size = getpagesize();
265    void *new_sp_aligned = (void *)((uintptr_t)new_sp & ~(page_size - 1));
266    if (msync(new_sp_aligned, page_size, MS_ASYNC) == -1)
267      return NULL;
268  }
269#endif
270  return new_sp;
271}
272
273#endif  // BASE_STACKTRACE_X86_INL_H_
274
275// Note: this part of the file is included several times.
276// Do not put globals below.
277
278// The following 4 functions are generated from the code below:
279//   GetStack{Trace,Frames}()
280//   GetStack{Trace,Frames}WithContext()
281//
282// These functions take the following args:
283//   void** result: the stack-trace, as an array
284//   int* sizes: the size of each stack frame, as an array
285//               (GetStackFrames* only)
286//   int max_depth: the size of the result (and sizes) array(s)
287//   int skip_count: how many stack pointers to skip before storing in result
288//   void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
289
290int GET_STACK_TRACE_OR_FRAMES {
291  void **sp;
292#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2) || __llvm__
293  // __builtin_frame_address(0) can return the wrong address on gcc-4.1.0-k8.
294  // It's always correct on llvm, and the techniques below aren't (in
295  // particular, llvm-gcc will make a copy of pcs, so it's not in sp[2]),
296  // so we also prefer __builtin_frame_address when running under llvm.
297  sp = reinterpret_cast<void**>(__builtin_frame_address(0));
298#elif defined(__i386__)
299  // Stack frame format:
300  //    sp[0]   pointer to previous frame
301  //    sp[1]   caller address
302  //    sp[2]   first argument
303  //    ...
304  // NOTE: This will break under llvm, since result is a copy and not in sp[2]
305  sp = (void **)&result - 2;
306#elif defined(__x86_64__)
307  unsigned long rbp;
308  // Move the value of the register %rbp into the local variable rbp.
309  // We need 'volatile' to prevent this instruction from getting moved
310  // around during optimization to before function prologue is done.
311  // An alternative way to achieve this
312  // would be (before this __asm__ instruction) to call Noop() defined as
313  //   static void Noop() __attribute__ ((noinline));  // prevent inlining
314  //   static void Noop() { asm(""); }  // prevent optimizing-away
315  __asm__ volatile ("mov %%rbp, %0" : "=r" (rbp));
316  // Arguments are passed in registers on x86-64, so we can't just
317  // offset from &result
318  sp = (void **) rbp;
319#else
320# error Using stacktrace_x86-inl.h on a non x86 architecture!
321#endif
322
323  int n = 0;
324  while (sp && n < max_depth) {
325    if (*(sp+1) == reinterpret_cast<void *>(0)) {
326      // In 64-bit code, we often see a frame that
327      // points to itself and has a return address of 0.
328      break;
329    }
330#if !IS_WITH_CONTEXT
331    const void *const ucp = NULL;
332#endif
333    void **next_sp = NextStackFrame<!IS_STACK_FRAMES, IS_WITH_CONTEXT>(sp, ucp);
334    if (skip_count > 0) {
335      skip_count--;
336    } else {
337      result[n] = *(sp+1);
338#if IS_STACK_FRAMES
339      if (next_sp > sp) {
340        sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp;
341      } else {
342        // A frame-size of 0 is used to indicate unknown frame size.
343        sizes[n] = 0;
344      }
345#endif
346      n++;
347    }
348    sp = next_sp;
349  }
350  return n;
351}
352