Skip to content

Commit

Permalink
Make not_alloc use max address
Browse files Browse the repository at this point in the history
  • Loading branch information
Speykious committed Feb 21, 2024
1 parent c956b5e commit 5b2ee71
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub use segfault::segfault;
pub use transmute::transmute;
pub use use_after_free::use_after_free;

pub use references::{null, null_mut};
pub use references::{not_alloc, null, null_mut};

/// Construct a [`String`] from a pointer, capacity and length, in a completely safe manner.
///
Expand Down
6 changes: 4 additions & 2 deletions src/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ pub fn null_mut<'a, T: 'static>() -> &'a mut T {
}

/// Not allocate an object. The returned reference is always invalid.
/// This is equivalent to [`crate::null_mut()`].
///
/// **Note:** It turns out that `null` is a valid memory address in WASM.
/// So here we use the maximum address instead.
pub fn not_alloc<'a, T: 'static>() -> &'a mut T {
null_mut()
crate::transmute(usize::MAX)
}
2 changes: 1 addition & 1 deletion src/segfault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn segfault() -> ! {

// If null doesn't work, try max. Surely that'll stop it.
// Confirmed to be effective on WASM.
let max = crate::transmute::<usize, &'static mut u8>(usize::MAX);
let max = crate::not_alloc::<u8>();
*max = 69;

unreachable!("Sorry, your platform is too strong.")
Expand Down

0 comments on commit 5b2ee71

Please sign in to comment.