Skip to content

Commit 9a567db

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 9a567db

File tree

9 files changed

+157
-19
lines changed

9 files changed

+157
-19
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.

datadog-profiling-ffi/src/exporter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ pub unsafe extern "C" fn ddog_prof_Exporter_send(
300300
let exporter = exporter.to_inner_mut()?;
301301
let cancel = cancel.to_inner_mut().ok();
302302
let response = exporter.send(request, cancel.as_deref())?;
303-
304303
anyhow::Ok(HttpStatus(response.status().as_u16()))
305304
})
306305
}

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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ 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 { message: Vec::new() };
11+
12+
// This error message is used as a placeholder for errors without message -- corresponding to an error where we
13+
// couldn't even _allocate_ the message (or some other even weirder error).
14+
const CANNOT_ALLOCATE: &std::ffi::CStr = c"Panic: Cannot allocate error message";
15+
const CANNOT_ALLOCATE_CHAR_SLICE: CharSlice = unsafe { crate::Slice::from_raw_parts(
16+
CANNOT_ALLOCATE.as_ptr(), CANNOT_ALLOCATE.to_bytes_with_nul().len()) };
17+
818
/// Please treat this as opaque; do not reach into it, and especially don't
919
/// write into it! The most relevant APIs are:
1020
/// * `ddog_Error_message`, to get the message as a slice.
@@ -104,7 +114,13 @@ pub unsafe extern "C" fn ddog_Error_drop(error: Option<&mut Error>) {
104114
pub unsafe extern "C" fn ddog_Error_message(error: Option<&Error>) -> CharSlice<'_> {
105115
match error {
106116
None => CharSlice::empty(),
107-
Some(err) => CharSlice::from(err.as_ref()),
117+
// When the error is empty (CANNOT_ALLOCATE_ERROR) we assume we failed to allocate an actual error and
118+
// return this placeholder message instead.
119+
Some(err) => if *err == CANNOT_ALLOCATE_ERROR {
120+
CANNOT_ALLOCATE_CHAR_SLICE
121+
} else {
122+
CharSlice::from(err.as_ref())
123+
},
108124
}
109125
}
110126

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: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
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(|| { $crate::wrap_with_ffi_result_no_catch!({ $body }) }))
15+
.map_or_else(
16+
|e| $crate::utils::handle_panic_error(e, function_name!()).into(),
17+
|result| result,
18+
)
19+
}};
20+
}
21+
22+
/// Wraps a C-FFI function in standard form (no catch variant).
23+
/// Same as `wrap_with_ffi_result` but does not try to catch panics.
24+
#[macro_export]
25+
macro_rules! wrap_with_ffi_result_no_catch {
926
($body:block) => {{
1027
use anyhow::Context;
1128
(|| $body)()
@@ -18,6 +35,21 @@ macro_rules! wrap_with_ffi_result {
1835
/// Expects the function to return a VoidResult and to be decorated with #[named].
1936
#[macro_export]
2037
macro_rules! wrap_with_void_ffi_result {
38+
($body:block) => {{
39+
use std::panic::{catch_unwind, AssertUnwindSafe};
40+
41+
catch_unwind(AssertUnwindSafe(|| { libdd_common_ffi::wrap_with_void_ffi_result_no_catch!({ $body }) }))
42+
.map_or_else(
43+
|e| libdd_common_ffi::utils::handle_panic_error(e, function_name!()).into(),
44+
|result| result,
45+
)
46+
}};
47+
}
48+
49+
/// Wraps a C-FFI function in standard form (no catch variant).
50+
/// Same as `wrap_with_void_ffi_result` but does not try to catch panics.
51+
#[macro_export]
52+
macro_rules! wrap_with_void_ffi_result_no_catch {
2153
($body:block) => {{
2254
use anyhow::Context;
2355
(|| {
@@ -38,3 +70,73 @@ impl ToHexStr for usize {
3870
format!("0x{self:X}")
3971
}
4072
}
73+
74+
/// You probably don't want to use this directly. This is used by `wrap_with_*_ffi_result` macros to turn a panic error
75+
/// into an actual nice error. Because the original panic may have been caused by being unable to allocate,
76+
/// this helper handles failures to allocate as well, turning them into a fallback error.
77+
pub fn handle_panic_error(error: Box<dyn std::any::Any + Send + 'static>, function_name: &str) -> crate::Error {
78+
catch_unwind(AssertUnwindSafe(|| {
79+
// This pattern of String vs &str comes from
80+
// https://doc.rust-lang.org/std/panic/struct.PanicHookInfo.html#method.payload
81+
if let Some(s) = error.downcast_ref::<String>() {
82+
anyhow::anyhow!("Panic inside {}: {}", function_name, s)
83+
} else if let Some(s) = error.downcast_ref::<&str>() {
84+
// panic!("double panic");
85+
anyhow::anyhow!("Panic inside {}: {}", function_name, s)
86+
} else {
87+
anyhow::anyhow!("Panic: Unable to retrieve panic context")
88+
}.into()
89+
})).unwrap_or_else(|_panic_while_allocating_nice_error| crate::error::CANNOT_ALLOCATE_ERROR)
90+
}
91+
92+
#[cfg(test)]
93+
mod tests {
94+
use assert_no_alloc::{ assert_no_alloc, AllocDisabler };
95+
use function_name::named;
96+
97+
#[cfg(debug_assertions)] // required when disable_release is set (default)
98+
#[global_allocator]
99+
static ALLOCATOR: AllocDisabler = AllocDisabler;
100+
101+
#[test]
102+
fn test_handle_panic_error_fallback_does_not_allocate() {
103+
let mut error_result_buffer: [i8; 100] = [0; 100];
104+
105+
assert_no_alloc(|| {
106+
// Simulate fallback code path of handle_panic_error + ddog_Error_message
107+
let fallback_error = crate::error::CANNOT_ALLOCATE_ERROR;
108+
let error_message = unsafe {
109+
crate::ddog_Error_message(Some(&fallback_error))
110+
};
111+
112+
// Stash error message so we can assert on it
113+
let n = error_message.len().min(error_result_buffer.len());
114+
error_result_buffer[..n].copy_from_slice(&error_message[..n]);
115+
});
116+
117+
unsafe {
118+
let c_str = std::ffi::CStr::from_ptr(error_result_buffer.as_ptr());
119+
assert_eq!(c_str.to_str().unwrap(), "Panic: Cannot allocate error message");
120+
};
121+
}
122+
123+
#[test]
124+
#[named]
125+
fn test_wrap_with_ffi_result_turns_panic_into_error() {
126+
// Save the current panic handler and replace it with a no-op so that Rust doesn't print anything inside
127+
// `wrap_with_ffi_result`...
128+
let original_hook = std::panic::take_hook();
129+
std::panic::set_hook(Box::new(|_| {}));
130+
131+
let result: crate::Result<()> = wrap_with_ffi_result!({
132+
panic!("this is a test panic message");
133+
#[allow(unreachable_code)]
134+
anyhow::Ok(())
135+
});
136+
137+
// ...restore original behavior
138+
std::panic::set_hook(original_hook);
139+
140+
assert_eq!(result.unwrap_err().to_string(), "Panic inside test_wrap_with_ffi_result_turns_panic_into_error: this is a test panic message");
141+
}
142+
}

libdd-common-ffi/src/vec.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ 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 { return; }
61+
5662
let vec =
5763
unsafe { alloc::vec::Vec::from_raw_parts(self.ptr as *mut T, self.len, self.capacity) };
5864
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)