Skip to content

Commit 7ef89a9

Browse files
committed
Document panic-related caveats
Make sure that it's documented that if `cb` ever panics it may abort the process on some platforms due to restrictions of the C libraries that we're using.
1 parent 5c5f66a commit 7ef89a9

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

src/backtrace/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ use types::c_void;
2727
/// This function requires the `std` feature of the `backtrace` crate to be
2828
/// enabled, and the `std` feature is enabled by default.
2929
///
30+
/// # Panics
31+
///
32+
/// This function strives to never panic, but if the `cb` provided panics then
33+
/// some platforms will force a double abort to abort the process. Some
34+
/// platforms use a C library which internally uses callbacks which cannot be
35+
/// unwound through, so panicking from `cb` may trigger a process abort.
36+
///
3037
/// # Example
3138
///
3239
/// ```
@@ -51,6 +58,10 @@ pub fn trace<F: FnMut(&Frame) -> bool>(cb: F) {
5158
/// This function does not have synchronization guarentees but is available
5259
/// when the `std` feature of this crate isn't compiled in. See the `trace`
5360
/// function for more documentation and examples.
61+
///
62+
/// # Panics
63+
///
64+
/// See information on `trace` for caveats on `cb` panicking.
5465
pub unsafe fn trace_unsynchronized<F: FnMut(&Frame) -> bool>(mut cb: F) {
5566
trace_imp(&mut cb)
5667
}

src/symbolize/libbacktrace.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ extern "C" fn syminfo_cb(
167167
_symval: uintptr_t,
168168
_symsize: uintptr_t,
169169
) {
170+
let mut bomb = ::Bomb { enabled: true };
171+
170172
// Once this callback is invoked from `backtrace_syminfo` when we start
171173
// resolving we go further to call `backtrace_pcinfo`. The
172174
// `backtrace_pcinfo` function will consult debug information and attemp tto
@@ -189,16 +191,16 @@ extern "C" fn syminfo_cb(
189191
&mut pcinfo_state as *mut _ as *mut _,
190192
);
191193
if !pcinfo_state.called {
192-
let mut bomb = ::Bomb { enabled: true };
193194
(pcinfo_state.cb)(&super::Symbol {
194195
inner: Symbol::Syminfo {
195196
pc: pc,
196197
symname: symname,
197198
},
198199
});
199-
bomb.enabled = false;
200200
}
201201
}
202+
203+
bomb.enabled = false;
202204
}
203205

204206
/// Type of the `data` pointer passed into `pcinfo_cb`
@@ -215,12 +217,13 @@ extern "C" fn pcinfo_cb(
215217
lineno: c_int,
216218
function: *const c_char,
217219
) -> c_int {
220+
if filename.is_null() || function.is_null() {
221+
return -1;
222+
}
223+
let mut bomb = ::Bomb { enabled: true };
224+
218225
unsafe {
219-
if filename.is_null() || function.is_null() {
220-
return -1;
221-
}
222226
let state = &mut *(data as *mut PcinfoState);
223-
let mut bomb = ::Bomb { enabled: true };
224227
state.called = true;
225228
(state.cb)(&super::Symbol {
226229
inner: Symbol::Pcinfo {
@@ -231,10 +234,10 @@ extern "C" fn pcinfo_cb(
231234
function,
232235
},
233236
});
234-
bomb.enabled = false;
235-
236-
return 0;
237237
}
238+
239+
bomb.enabled = false;
240+
return 0;
238241
}
239242

240243
// The libbacktrace API supports creating a state, but it does not

src/symbolize/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ use backtrace::Frame;
3333
/// This function requires the `std` feature of the `backtrace` crate to be
3434
/// enabled, and the `std` feature is enabled by default.
3535
///
36+
/// # Panics
37+
///
38+
/// This function strives to never panic, but if the `cb` provided panics then
39+
/// some platforms will force a double abort to abort the process. Some
40+
/// platforms use a C library which internally uses callbacks which cannot be
41+
/// unwound through, so panicking from `cb` may trigger a process abort.
42+
///
3643
/// # Example
3744
///
3845
/// ```
@@ -70,6 +77,13 @@ pub fn resolve<F: FnMut(&Symbol)>(addr: *mut c_void, cb: F) {
7077
/// This function requires the `std` feature of the `backtrace` crate to be
7178
/// enabled, and the `std` feature is enabled by default.
7279
///
80+
/// # Panics
81+
///
82+
/// This function strives to never panic, but if the `cb` provided panics then
83+
/// some platforms will force a double abort to abort the process. Some
84+
/// platforms use a C library which internally uses callbacks which cannot be
85+
/// unwound through, so panicking from `cb` may trigger a process abort.
86+
///
7387
/// # Example
7488
///
7589
/// ```
@@ -111,6 +125,10 @@ impl<'a> ResolveWhat<'a> {
111125
/// This function does not have synchronization guarentees but is available when
112126
/// the `std` feature of this crate isn't compiled in. See the `resolve`
113127
/// function for more documentation and examples.
128+
///
129+
/// # Panics
130+
///
131+
/// See information on `resolve` for caveats on `cb` panicking.
114132
pub unsafe fn resolve_unsynchronized<F>(addr: *mut c_void, mut cb: F)
115133
where
116134
F: FnMut(&Symbol),
@@ -123,6 +141,10 @@ where
123141
/// This function does not have synchronization guarentees but is available
124142
/// when the `std` feature of this crate isn't compiled in. See the
125143
/// `resolve_frame` function for more documentation and examples.
144+
///
145+
/// # Panics
146+
///
147+
/// See information on `resolve_frame` for caveats on `cb` panicking.
126148
pub unsafe fn resolve_frame_unsynchronized<F>(frame: &Frame, mut cb: F)
127149
where
128150
F: FnMut(&Symbol),

0 commit comments

Comments
 (0)