Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: base/debug/stack_trace.cc

Issue 2203053003: Check stack pointer to be inside the stack when unwinding on Linux. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase; address comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/debug/stack_trace.h" 5 #include "base/debug/stack_trace.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <limits>
10 #include <sstream> 11 #include <sstream>
11 12
12 #include "base/macros.h" 13 #include "base/macros.h"
13 14
14 #if HAVE_TRACE_STACK_FRAME_POINTERS && defined(OS_ANDROID) 15 #if HAVE_TRACE_STACK_FRAME_POINTERS
16
17 #if defined(OS_LINUX) || defined(OS_ANDROID)
15 #include <pthread.h> 18 #include <pthread.h>
16 #include "base/process/process_handle.h" 19 #include "base/process/process_handle.h"
17 #include "base/threading/platform_thread.h" 20 #include "base/threading/platform_thread.h"
18 #endif 21 #endif
19 22
23 #if defined(OS_LINUX) && defined(__GLIBC__)
24 extern "C" void* __libc_stack_end;
25 #endif
26
27 #endif // HAVE_TRACE_STACK_FRAME_POINTERS
28
20 namespace base { 29 namespace base {
21 namespace debug { 30 namespace debug {
22 31
23 StackTrace::StackTrace(const void* const* trace, size_t count) { 32 StackTrace::StackTrace(const void* const* trace, size_t count) {
24 count = std::min(count, arraysize(trace_)); 33 count = std::min(count, arraysize(trace_));
25 if (count) 34 if (count)
26 memcpy(trace_, trace, count * sizeof(trace_[0])); 35 memcpy(trace_, trace, count * sizeof(trace_[0]));
27 count_ = count; 36 count_ = count;
28 } 37 }
29 38
(...skipping 10 matching lines...) Expand all
40 std::string StackTrace::ToString() const { 49 std::string StackTrace::ToString() const {
41 std::stringstream stream; 50 std::stringstream stream;
42 #if !defined(__UCLIBC__) 51 #if !defined(__UCLIBC__)
43 OutputToStream(&stream); 52 OutputToStream(&stream);
44 #endif 53 #endif
45 return stream.str(); 54 return stream.str();
46 } 55 }
47 56
48 #if HAVE_TRACE_STACK_FRAME_POINTERS 57 #if HAVE_TRACE_STACK_FRAME_POINTERS
49 58
59 static uintptr_t GetStackEnd() {
50 #if defined(OS_ANDROID) 60 #if defined(OS_ANDROID)
51
52 static uintptr_t GetStackEnd() {
53 // Bionic reads proc/maps on every call to pthread_getattr_np() when called 61 // Bionic reads proc/maps on every call to pthread_getattr_np() when called
54 // from the main thread. So we need to cache end of stack in that case to get 62 // from the main thread. So we need to cache end of stack in that case to get
55 // acceptable performance. 63 // acceptable performance.
56 // For all other threads pthread_getattr_np() is fast enough as it just reads 64 // For all other threads pthread_getattr_np() is fast enough as it just reads
57 // values from its pthread_t argument. 65 // values from its pthread_t argument.
58 static uintptr_t main_stack_end = 0; 66 static uintptr_t main_stack_end = 0;
59 67
60 bool is_main_thread = GetCurrentProcId() == PlatformThread::CurrentId(); 68 bool is_main_thread = GetCurrentProcId() == PlatformThread::CurrentId();
61
62 if (is_main_thread && main_stack_end) { 69 if (is_main_thread && main_stack_end) {
63 return main_stack_end; 70 return main_stack_end;
64 } 71 }
65 72
66 uintptr_t stack_begin = 0; 73 uintptr_t stack_begin = 0;
67 size_t stack_size = 0; 74 size_t stack_size = 0;
68 pthread_attr_t attributes; 75 pthread_attr_t attributes;
69 int error = pthread_getattr_np(pthread_self(), &attributes); 76 int error = pthread_getattr_np(pthread_self(), &attributes);
70 if (!error) { 77 if (!error) {
71 error = pthread_attr_getstack( 78 error = pthread_attr_getstack(
72 &attributes, 79 &attributes,
73 reinterpret_cast<void**>(&stack_begin), 80 reinterpret_cast<void**>(&stack_begin),
74 &stack_size); 81 &stack_size);
75 pthread_attr_destroy(&attributes); 82 pthread_attr_destroy(&attributes);
76 } 83 }
77 DCHECK(!error); 84 DCHECK(!error);
78 85
79 uintptr_t stack_end = stack_begin + stack_size; 86 uintptr_t stack_end = stack_begin + stack_size;
80 if (is_main_thread) { 87 if (is_main_thread) {
81 main_stack_end = stack_end; 88 main_stack_end = stack_end;
82 } 89 }
83 return stack_end; 90 return stack_end;
91
92 #elif defined(OS_LINUX) && defined(__GLIBC__)
93
94 if (GetCurrentProcId() == PlatformThread::CurrentId()) {
95 // For the main thread we have a shortcut.
96 return reinterpret_cast<uintptr_t>(__libc_stack_end);
97 } else {
98 // No easy way to get stack end for non-main threads,
99 // see crbug.com/617730.
100 return std::numeric_limits<uintptr_t>::max();
101 }
102
103 #else
Primiano Tucci (use gerrit) 2016/08/25 11:03:18 if you make this an #endif, you can have a unique
Dmitry Skiba 2016/08/26 19:14:07 Done.
104
105 // TODO(dskiba): support Windows, macOS
106 return std::numeric_limits<uintptr_t>::max();
107
108 #endif
84 } 109 }
85 110
86 #endif // defined(OS_ANDROID)
87
88 size_t TraceStackFramePointers(const void** out_trace, 111 size_t TraceStackFramePointers(const void** out_trace,
89 size_t max_depth, 112 size_t max_depth,
90 size_t skip_initial) { 113 size_t skip_initial) {
91 // Usage of __builtin_frame_address() enables frame pointers in this 114 // Usage of __builtin_frame_address() enables frame pointers in this
92 // function even if they are not enabled globally. So 'sp' will always 115 // function even if they are not enabled globally. So 'sp' will always
93 // be valid. 116 // be valid.
94 uintptr_t sp = reinterpret_cast<uintptr_t>(__builtin_frame_address(0)); 117 uintptr_t sp = reinterpret_cast<uintptr_t>(__builtin_frame_address(0));
95 118
96 #if defined(OS_ANDROID)
97 uintptr_t stack_end = GetStackEnd(); 119 uintptr_t stack_end = GetStackEnd();
98 #endif
99 120
100 size_t depth = 0; 121 size_t depth = 0;
101 while (depth < max_depth) { 122 while (depth < max_depth) {
102 #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__) 123 #if defined(__arm__) && defined(__GNUC__) && !defined(__clang__)
103 // GCC and LLVM generate slightly different frames on ARM, see 124 // GCC and LLVM generate slightly different frames on ARM, see
104 // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates 125 // https://llvm.org/bugs/show_bug.cgi?id=18505 - LLVM generates
105 // x86-compatible frame, while GCC needs adjustment. 126 // x86-compatible frame, while GCC needs adjustment.
106 sp -= sizeof(uintptr_t); 127 sp -= sizeof(uintptr_t);
107 #endif 128 #endif
108 129
109 #if defined(OS_ANDROID)
110 // Both sp[0] and s[1] must be valid. 130 // Both sp[0] and s[1] must be valid.
111 if (sp + 2 * sizeof(uintptr_t) > stack_end) { 131 if (sp + 2 * sizeof(uintptr_t) > stack_end) {
112 break; 132 break;
113 } 133 }
114 #endif
115 134
116 if (skip_initial != 0) { 135 if (skip_initial != 0) {
117 skip_initial--; 136 skip_initial--;
118 } else { 137 } else {
119 out_trace[depth++] = reinterpret_cast<const void**>(sp)[1]; 138 out_trace[depth++] = reinterpret_cast<const void**>(sp)[1];
120 } 139 }
121 140
122 // Find out next frame pointer 141 // Find out next frame pointer
123 // (heuristics are from TCMalloc's stacktrace functions) 142 // (heuristics are from TCMalloc's stacktrace functions)
124 { 143 {
(...skipping 13 matching lines...) Expand all
138 } 157 }
139 } 158 }
140 159
141 return depth; 160 return depth;
142 } 161 }
143 162
144 #endif // HAVE_TRACE_STACK_FRAME_POINTERS 163 #endif // HAVE_TRACE_STACK_FRAME_POINTERS
145 164
146 } // namespace debug 165 } // namespace debug
147 } // namespace base 166 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698