diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
commit | dc0db358abe19481e475e10c32149b53370f1a1c (patch) | |
tree | ab8ce99c4b255ce46f99ef402c27916055b899ee /vendor/tokio/tests/support | |
parent | Releasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff) | |
download | rustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip |
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tokio/tests/support')
-rw-r--r-- | vendor/tokio/tests/support/leaked_buffers.rs | 26 | ||||
-rw-r--r-- | vendor/tokio/tests/support/panic.rs | 34 |
2 files changed, 60 insertions, 0 deletions
diff --git a/vendor/tokio/tests/support/leaked_buffers.rs b/vendor/tokio/tests/support/leaked_buffers.rs new file mode 100644 index 000000000..a6079fb70 --- /dev/null +++ b/vendor/tokio/tests/support/leaked_buffers.rs @@ -0,0 +1,26 @@ +/// Can create buffers of arbitrary lifetime. +/// Frees created buffers when dropped. +/// +/// This struct is of course unsafe and the fact that +/// it must outlive the created slices has to be ensured by +/// the programmer. +/// +/// Used at certain test scenarios as a safer version of +/// Vec::leak, to satisfy the address sanitizer. +pub struct LeakedBuffers { + leaked_vecs: Vec<Box<[u8]>>, +} + +impl LeakedBuffers { + pub fn new() -> Self { + Self { + leaked_vecs: vec![], + } + } + pub unsafe fn create<'a>(&mut self, size: usize) -> &'a mut [u8] { + let new_mem = vec![0u8; size].into_boxed_slice(); + self.leaked_vecs.push(new_mem); + let new_mem = self.leaked_vecs.last_mut().unwrap(); + std::slice::from_raw_parts_mut(new_mem.as_mut_ptr(), new_mem.len()) + } +} diff --git a/vendor/tokio/tests/support/panic.rs b/vendor/tokio/tests/support/panic.rs new file mode 100644 index 000000000..df2f59d30 --- /dev/null +++ b/vendor/tokio/tests/support/panic.rs @@ -0,0 +1,34 @@ +use std::panic; +use std::sync::{Arc, Mutex}; + +pub fn test_panic<Func: FnOnce() + panic::UnwindSafe>(func: Func) -> Option<String> { + static PANIC_MUTEX: Mutex<()> = Mutex::new(()); + + { + let _guard = PANIC_MUTEX.lock(); + let panic_file: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None)); + + let prev_hook = panic::take_hook(); + { + let panic_file = panic_file.clone(); + panic::set_hook(Box::new(move |panic_info| { + let panic_location = panic_info.location().unwrap(); + panic_file + .lock() + .unwrap() + .clone_from(&Some(panic_location.file().to_string())); + })); + } + + let result = panic::catch_unwind(func); + // Return to the previously set panic hook (maybe default) so that we get nice error + // messages in the tests. + panic::set_hook(prev_hook); + + if result.is_err() { + panic_file.lock().unwrap().clone() + } else { + None + } + } +} |