Skip to content

Commit 0b6b44f

Browse files
committed
Auto merge of rust-lang#3166 - devnexen:reallocarray, r=RalfJung
reallocarray shim linux/freebsd support proposal.
2 parents 7df74eb + c6acc05 commit 0b6b44f

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/tools/miri/ci.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ case $HOST_TARGET in
108108
MIRI_TEST_TARGET=aarch64-unknown-linux-gnu run_tests
109109
MIRI_TEST_TARGET=aarch64-apple-darwin run_tests
110110
MIRI_TEST_TARGET=i686-pc-windows-gnu run_tests
111-
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
111+
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
112112
MIRI_TEST_TARGET=aarch64-linux-android run_tests_minimal hello integer vec panic/panic
113113
MIRI_TEST_TARGET=wasm32-wasi run_tests_minimal no_std integer strings wasm
114114
MIRI_TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std integer strings wasm

src/tools/miri/src/shims/unix/foreign_items.rs

+31
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,37 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
252252
this.write_scalar(result, dest)?;
253253
}
254254

255+
"reallocarray" => {
256+
// Currently this function does not exist on all Unixes, e.g. on macOS.
257+
if !matches!(&*this.tcx.sess.target.os, "linux" | "freebsd") {
258+
throw_unsup_format!(
259+
"`reallocarray` is not supported on {}",
260+
this.tcx.sess.target.os
261+
);
262+
}
263+
let [ptr, nmemb, size] =
264+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
265+
let ptr = this.read_pointer(ptr)?;
266+
let nmemb = this.read_target_usize(nmemb)?;
267+
let size = this.read_target_usize(size)?;
268+
// reallocarray checks a possible overflow and returns ENOMEM
269+
// if that happens.
270+
//
271+
// Linux: https://www.unix.com/man-page/linux/3/reallocarray/
272+
// FreeBSD: https://man.freebsd.org/cgi/man.cgi?query=reallocarray
273+
match nmemb.checked_mul(size) {
274+
None => {
275+
let einval = this.eval_libc("ENOMEM");
276+
this.set_last_error(einval)?;
277+
this.write_null(dest)?;
278+
}
279+
Some(len) => {
280+
let res = this.realloc(ptr, len, MiriMemoryKind::C)?;
281+
this.write_pointer(res, dest)?;
282+
}
283+
}
284+
}
285+
255286
// Dynamic symbol loading
256287
"dlsym" => {
257288
let [handle, symbol] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ignore-target-windows: no libc
2+
//@ignore-target-apple: no support (yet)
3+
4+
use core::ptr;
5+
6+
fn main() {
7+
unsafe {
8+
let mut p = libc::reallocarray(ptr::null_mut(), 4096, 2);
9+
assert!(!p.is_null());
10+
libc::free(p);
11+
p = libc::malloc(16);
12+
let r = libc::reallocarray(p, 2, 32);
13+
assert!(!r.is_null());
14+
libc::free(r);
15+
}
16+
}

0 commit comments

Comments
 (0)