Browse Source

Use `*mut u8` instead of `usize` to hold pointers.

This will reduce casting, and better preserve pointer provenance.
pull/6374/head
Dan Gohman 2 years ago
parent
commit
7ad3b586b3
  1. 4
      build.rs
  2. 7
      src/lib.rs

4
build.rs

@ -28,9 +28,9 @@ fn main() {
/// );
///
/// #[no_mangle]
/// extern "C" fn replace_realloc_global(val: usize) -> usize {
/// extern "C" fn replace_realloc_global(val: *mut u8) -> *mut u8 {
/// unsafe {
/// let ret: usize;
/// let ret: *mut u8;
/// std::arch::asm!(
/// "
/// global.get internal_realloc_global

7
src/lib.rs

@ -8,10 +8,11 @@ wit_bindgen_guest_rust::import!("wit/wasi-poll.wit.md");
wit_bindgen_guest_rust::import!("wit/wasi-random.wit.md");
use std::arch::wasm32::unreachable;
use std::ptr::null_mut;
use wasi::*;
extern "C" {
fn replace_realloc_global(val: usize) -> usize;
fn replace_realloc_global(val: *mut u8) -> *mut u8;
}
#[no_mangle]
@ -24,8 +25,8 @@ pub unsafe extern "C" fn cabi_realloc(
if !old_ptr.is_null() || old_size != 0 {
unreachable();
}
let base = replace_realloc_global(0);
if base == 0 {
let base = replace_realloc_global(null_mut());
if base.is_null() {
unreachable();
}
base as *mut u8

Loading…
Cancel
Save