reallocarray shim linux/freebsd support proposal.

This commit is contained in:
David Carlier 2023-11-15 22:46:40 +00:00 committed by David CARLIER
parent 012bd49b8a
commit c6acc058c0
3 changed files with 48 additions and 1 deletions

View File

@ -108,7 +108,7 @@ case $HOST_TARGET in
MIRI_TEST_TARGET=aarch64-unknown-linux-gnu run_tests
MIRI_TEST_TARGET=aarch64-apple-darwin run_tests
MIRI_TEST_TARGET=i686-pc-windows-gnu run_tests
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec panic/panic concurrency/simple pthreads libc-getentropy libc-getrandom atomic env/var
MIRI_TEST_TARGET=x86_64-unknown-freebsd run_tests_minimal hello integer vec panic/panic concurrency/simple pthreads libc-getentropy libc-getrandom libc-reallocarray atomic env/var
MIRI_TEST_TARGET=aarch64-linux-android run_tests_minimal hello integer vec panic/panic
MIRI_TEST_TARGET=wasm32-wasi run_tests_minimal no_std integer strings wasm
MIRI_TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std integer strings wasm

View File

@ -252,6 +252,37 @@ fn emulate_foreign_item_inner(
this.write_scalar(result, dest)?;
}
"reallocarray" => {
// Currently this function does not exist on all Unixes, e.g. on macOS.
if !matches!(&*this.tcx.sess.target.os, "linux" | "freebsd") {
throw_unsup_format!(
"`reallocarray` is not supported on {}",
this.tcx.sess.target.os
);
}
let [ptr, nmemb, size] =
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let ptr = this.read_pointer(ptr)?;
let nmemb = this.read_target_usize(nmemb)?;
let size = this.read_target_usize(size)?;
// reallocarray checks a possible overflow and returns ENOMEM
// if that happens.
//
// Linux: https://www.unix.com/man-page/linux/3/reallocarray/
// FreeBSD: https://man.freebsd.org/cgi/man.cgi?query=reallocarray
match nmemb.checked_mul(size) {
None => {
let einval = this.eval_libc("ENOMEM");
this.set_last_error(einval)?;
this.write_null(dest)?;
}
Some(len) => {
let res = this.realloc(ptr, len, MiriMemoryKind::C)?;
this.write_pointer(res, dest)?;
}
}
}
// Dynamic symbol loading
"dlsym" => {
let [handle, symbol] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;

View File

@ -0,0 +1,16 @@
//@ignore-target-windows: no libc
//@ignore-target-apple: no support (yet)
use core::ptr;
fn main() {
unsafe {
let mut p = libc::reallocarray(ptr::null_mut(), 4096, 2);
assert!(!p.is_null());
libc::free(p);
p = libc::malloc(16);
let r = libc::reallocarray(p, 2, 32);
assert!(!r.is_null());
libc::free(r);
}
}