Skip to content

Commit 80ce1bc

Browse files
committed
[PROF-12853] Catch panics inside wrap_with_ffi_result and wrap_with_void_ffi_result
**What does this PR do?** This PR updates the `wrap_with_ffi_result` and `wrap_with_void_ffi_result` macros to catch any panics that happen inside them, returning them as errors. The error handling is made in such a way (see `handle_panic_error` for details) that it should be able to report back an error even if we fail to do any allocations. Important note: Because only the macros have been changed, and ffi APIs that don't use the macros are of course not affected and can still trigger panics. If we like this approach, I'll follow-up with a separate PR to update other APIs to use the new macros. **Motivation:** In <https://docs.google.com/document/d/1weMu9P03KKhPQ-gh9BMqRrEzpa1BnnY0LaSRGJbfc7A/edit?usp=sharing> (Datadog-only link, sorry!) we saw `ddog_prof_Exporter_send` crashing due to what can be summed up as `ddog_prof_Exporter_send` (report a profile) -> hyper-util tries to do dns resolution in a separate thread pool -> tokio failed to create a new thread -> panic and we tear down the app because we can't report a profile This is not good at all, and this PR solves this inspired by earlier work in #815 and #1083. **Additional Notes:** While I don't predict that will happen very often, callers that want to opt-out of the catch unwind behavior can still use the `..._no_catch` variants of the macros. **How to test the change?** This change includes test coverage. I've also separately tried to sprinkle a few `panic!` calls manually and tested that it works as expected.
1 parent 64205d3 commit 80ce1bc

File tree

8 files changed

+187
-18
lines changed

8 files changed

+187
-18
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libdd-common-ffi/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ serde = "1.0"
2929

3030
[dev-dependencies]
3131
bolero = "0.13"
32+
assert_no_alloc = "1.1.2"
33+
function_name = "0.3.0"

libdd-common-ffi/src/error.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ use crate::slice::{AsBytes, CharSlice};
55
use crate::vec::Vec;
66
use std::fmt::{Debug, Display, Formatter};
77

8+
/// You probably don't want to use this directly. This constant is used by `handle_panic_error` to signal that something
9+
/// went wrong, but avoid needing any allocations to represent it.
10+
pub(crate) const CANNOT_ALLOCATE_ERROR: Error = Error {
11+
message: Vec::new(),
12+
};
13+
14+
// This error message is used as a placeholder for errors without message -- corresponding to an error where we
15+
// couldn't even _allocate_ the message (or some other even weirder error).
16+
const CANNOT_ALLOCATE: &std::ffi::CStr = c"Panic: Cannot allocate error message";
17+
const CANNOT_ALLOCATE_CHAR_SLICE: CharSlice = unsafe {
18+
crate::Slice::from_raw_parts(
19+
CANNOT_ALLOCATE.as_ptr(),
20+
CANNOT_ALLOCATE.to_bytes_with_nul().len(),
21+
)
22+
};
23+
824
/// Please treat this as opaque; do not reach into it, and especially don't
925
/// write into it! The most relevant APIs are:
1026
/// * `ddog_Error_message`, to get the message as a slice.
@@ -104,7 +120,15 @@ pub unsafe extern "C" fn ddog_Error_drop(error: Option<&mut Error>) {
104120
pub unsafe extern "C" fn ddog_Error_message(error: Option<&Error>) -> CharSlice<'_> {
105121
match error {
106122
None => CharSlice::empty(),
107-
Some(err) => CharSlice::from(err.as_ref()),
123+
// When the error is empty (CANNOT_ALLOCATE_ERROR) we assume we failed to allocate an actual error and
124+
// return this placeholder message instead.
125+
Some(err) => {
126+
if *err == CANNOT_ALLOCATE_ERROR {
127+
CANNOT_ALLOCATE_CHAR_SLICE
128+
} else {
129+
CharSlice::from(err.as_ref())
130+
}
131+
}
108132
}
109133
}
110134

libdd-common-ffi/src/result.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ impl From<anyhow::Result<()>> for VoidResult {
3434
}
3535
}
3636

37+
impl From<Error> for VoidResult {
38+
fn from(value: Error) -> Self {
39+
Self::Err(value)
40+
}
41+
}
42+
3743
/// A generic result type for when an operation may fail,
3844
/// or may return <T> in case of success.
3945
#[repr(C)]
@@ -68,3 +74,9 @@ impl<T> From<anyhow::Result<T>> for Result<T> {
6874
}
6975
}
7076
}
77+
78+
impl<T> From<Error> for Result<T> {
79+
fn from(value: Error) -> Self {
80+
Self::Err(value)
81+
}
82+
}

libdd-common-ffi/src/string.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,9 @@ impl From<String> for StringWrapperResult {
104104
Self::Ok(value.into())
105105
}
106106
}
107+
108+
impl From<Error> for StringWrapperResult {
109+
fn from(value: Error) -> Self {
110+
Self::Err(value)
111+
}
112+
}

libdd-common-ffi/src/utils.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use std::panic::{catch_unwind, AssertUnwindSafe};
5+
46
/// Wraps a C-FFI function in standard form
57
/// Expects the function to return a result type that implements into and to be decorated with
68
/// #[named].
79
#[macro_export]
810
macro_rules! wrap_with_ffi_result {
11+
($body:block) => {{
12+
use std::panic::{catch_unwind, AssertUnwindSafe};
13+
14+
catch_unwind(AssertUnwindSafe(|| {
15+
$crate::wrap_with_ffi_result_no_catch!({ $body })
16+
}))
17+
.map_or_else(
18+
|e| $crate::utils::handle_panic_error(e, function_name!()).into(),
19+
|result| result,
20+
)
21+
}};
22+
}
23+
24+
/// Wraps a C-FFI function in standard form (no catch variant).
25+
/// Same as `wrap_with_ffi_result` but does not try to catch panics.
26+
#[macro_export]
27+
macro_rules! wrap_with_ffi_result_no_catch {
928
($body:block) => {{
1029
use anyhow::Context;
1130
(|| $body)()
@@ -18,6 +37,23 @@ macro_rules! wrap_with_ffi_result {
1837
/// Expects the function to return a VoidResult and to be decorated with #[named].
1938
#[macro_export]
2039
macro_rules! wrap_with_void_ffi_result {
40+
($body:block) => {{
41+
use std::panic::{catch_unwind, AssertUnwindSafe};
42+
43+
catch_unwind(AssertUnwindSafe(|| {
44+
$crate::wrap_with_void_ffi_result_no_catch!({ $body })
45+
}))
46+
.map_or_else(
47+
|e| $crate::utils::handle_panic_error(e, function_name!()).into(),
48+
|result| result,
49+
)
50+
}};
51+
}
52+
53+
/// Wraps a C-FFI function in standard form (no catch variant).
54+
/// Same as `wrap_with_void_ffi_result` but does not try to catch panics.
55+
#[macro_export]
56+
macro_rules! wrap_with_void_ffi_result_no_catch {
2157
($body:block) => {{
2258
use anyhow::Context;
2359
(|| {
@@ -38,3 +74,89 @@ impl ToHexStr for usize {
3874
format!("0x{self:X}")
3975
}
4076
}
77+
78+
/// You probably don't want to use this directly. This is used by `wrap_with_*_ffi_result` macros to turn a panic error
79+
/// into an actual nice error. Because the original panic may have been caused by being unable to allocate,
80+
/// this helper handles failures to allocate as well, turning them into a fallback error.
81+
pub fn handle_panic_error(
82+
error: Box<dyn std::any::Any + Send + 'static>,
83+
function_name: &str,
84+
) -> crate::Error {
85+
catch_unwind(AssertUnwindSafe(|| {
86+
// This pattern of String vs &str comes from
87+
// https://doc.rust-lang.org/std/panic/struct.PanicHookInfo.html#method.payload
88+
if let Some(s) = error.downcast_ref::<String>() {
89+
anyhow::anyhow!("{} failed: (panic) {}", function_name, s)
90+
} else if let Some(s) = error.downcast_ref::<&str>() {
91+
// panic!("double panic");
92+
anyhow::anyhow!("{} failed: (panic) {}", function_name, s)
93+
} else {
94+
anyhow::anyhow!("{} failed: (panic) Unable to retrieve panic context", function_name)
95+
}
96+
.into()
97+
}))
98+
.unwrap_or_else(|_panic_while_allocating_nice_error| crate::error::CANNOT_ALLOCATE_ERROR)
99+
}
100+
101+
#[cfg(test)]
102+
mod tests {
103+
use assert_no_alloc::{assert_no_alloc, AllocDisabler};
104+
use function_name::named;
105+
106+
#[cfg(debug_assertions)] // required when disable_release is set (default)
107+
#[global_allocator]
108+
static ALLOCATOR: AllocDisabler = AllocDisabler;
109+
110+
#[test]
111+
fn test_handle_panic_error_fallback_does_not_allocate() {
112+
let mut error_result_buffer: [i8; 100] = [0; 100];
113+
114+
assert_no_alloc(|| {
115+
// Simulate fallback code path of handle_panic_error + ddog_Error_message
116+
let fallback_error = crate::error::CANNOT_ALLOCATE_ERROR;
117+
let error_message = unsafe { crate::ddog_Error_message(Some(&fallback_error)) };
118+
119+
// Stash error message so we can assert on it
120+
let n = error_message.len().min(error_result_buffer.len());
121+
error_result_buffer[..n].copy_from_slice(&error_message[..n]);
122+
});
123+
124+
unsafe {
125+
let c_str = std::ffi::CStr::from_ptr(error_result_buffer.as_ptr());
126+
assert_eq!(
127+
c_str.to_str().unwrap(),
128+
"Panic: Cannot allocate error message"
129+
);
130+
};
131+
}
132+
133+
#[test]
134+
#[named]
135+
fn test_wrap_with_ffi_result_turns_panic_into_error() {
136+
// Save the current panic handler and replace it with a no-op so that Rust doesn't print anything inside
137+
// `wrap_with_ffi_result`...
138+
let original_hook = std::panic::take_hook();
139+
std::panic::set_hook(Box::new(|_| {}));
140+
141+
let result: crate::Result<()> = wrap_with_ffi_result!({
142+
panic!("this is a test panic message");
143+
#[allow(unreachable_code)]
144+
anyhow::Ok(())
145+
});
146+
147+
// ...restore original behavior
148+
std::panic::set_hook(original_hook);
149+
150+
assert_eq!(result.unwrap_err().to_string(), "test_wrap_with_ffi_result_turns_panic_into_error failed: (panic) this is a test panic message");
151+
}
152+
153+
#[test]
154+
#[named]
155+
fn test_wrap_with_ffi_result_does_not_modify_other_kinds_of_errors() {
156+
let result: crate::result::VoidResult = wrap_with_void_ffi_result!({
157+
Err(anyhow::anyhow!("this is a test error message"))?;
158+
});
159+
160+
assert_eq!(result.unwrap_err().to_string(), "test_wrap_with_ffi_result_does_not_modify_other_kinds_of_errors failed: this is a test error message");
161+
}
162+
}

libdd-common-ffi/src/vec.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ impl<T: Eq> Eq for Vec<T> {}
5353

5454
impl<T> Drop for Vec<T> {
5555
fn drop(&mut self) {
56+
// A Rust Vec of size 0 [has no allocated memory](https://doc.rust-lang.org/std/vec/struct.Vec.html#guarantees):
57+
// "In particular, if you construct a Vec with capacity 0 via Vec::new, vec![], Vec::with_capacity(0), or by calling shrink_to_fit on an empty Vec, it will not allocate memory."
58+
// And as per https://doc.rust-lang.org/nomicon/vec/vec-dealloc.html:
59+
// "We must not call alloc::dealloc when self.cap == 0, as in this case we haven't actually allocated any memory."
60+
if self.capacity == 0 {
61+
return;
62+
}
63+
5664
let vec =
5765
unsafe { alloc::vec::Vec::from_raw_parts(self.ptr as *mut T, self.len, self.capacity) };
5866
drop(vec)

libdd-crashtracker-ffi/src/crash_info/builder.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,18 @@ pub unsafe extern "C" fn ddog_crasht_CrashInfoBuilder_drop(builder: *mut Handle<
4242
}
4343
}
4444

45-
#[allow(dead_code)]
46-
#[repr(C)]
47-
pub enum CrashInfoNewResult {
48-
Ok(Handle<CrashInfo>),
49-
Err(Error),
50-
}
45+
// Name the type so it's prettier for the consumer
46+
pub type CrashInfoNewResult = libdd_common_ffi::Result<Handle<CrashInfo>>;
5147

5248
/// # Safety
5349
/// The `builder` can be null, but if non-null it must point to a Builder made by this module,
5450
/// which has not previously been dropped.
5551
#[no_mangle]
5652
#[must_use]
57-
pub unsafe extern "C" fn ddog_crasht_CrashInfoBuilder_build(
58-
builder: *mut Handle<CrashInfoBuilder>,
59-
) -> CrashInfoNewResult {
60-
match ddog_crasht_crash_info_builder_build_impl(builder) {
61-
Ok(crash_info) => CrashInfoNewResult::Ok(crash_info),
62-
Err(err) => CrashInfoNewResult::Err(err.into()),
63-
}
64-
}
65-
6653
#[named]
67-
unsafe fn ddog_crasht_crash_info_builder_build_impl(
54+
pub unsafe extern "C" fn ddog_crasht_CrashInfoBuilder_build(
6855
mut builder: *mut Handle<CrashInfoBuilder>,
69-
) -> anyhow::Result<Handle<CrashInfo>> {
56+
) -> CrashInfoNewResult {
7057
wrap_with_ffi_result!({ anyhow::Ok(builder.take()?.build()?.into()) })
7158
}
7259

0 commit comments

Comments
 (0)