Skip to content

Commit 88c3abf

Browse files
committed
fix: handle zero-sized memory intrinsics
1 parent 9886ca5 commit 88c3abf

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

example/mini_core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ pub mod intrinsics {
662662
#[rustc_intrinsic]
663663
pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize);
664664
#[rustc_intrinsic]
665+
pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
666+
#[rustc_intrinsic]
665667
pub unsafe fn transmute<T, U>(e: T) -> U;
666668
#[rustc_intrinsic]
667669
pub unsafe fn ctlz_nonzero<T>(x: T) -> u32;

src/intrinsic/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,13 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
338338
);
339339
return Ok(());
340340
}
341+
sym::copy | sym::copy_nonoverlapping | sym::write_bytes => {
342+
let ty = fn_args.type_at(0);
343+
if self.layout_of(ty).size.bytes() == 0 {
344+
return Ok(());
345+
}
346+
return Err(Instance::new_raw(instance.def_id(), instance.args));
347+
}
341348
sym::breakpoint => {
342349
unimplemented!();
343350
}

tests/run/zero_sized_mem.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Compiler:
2+
//
3+
// Run-time:
4+
// status: 0
5+
6+
#![feature(no_core)]
7+
#![no_std]
8+
#![no_core]
9+
#![no_main]
10+
11+
extern crate mini_core;
12+
use mini_core::*;
13+
14+
#[inline(never)]
15+
unsafe fn zero_sized_mem_ops(count: usize) {
16+
let src = 0usize as *const ();
17+
let dst = 0usize as *mut ();
18+
19+
intrinsics::copy_nonoverlapping(src, dst, count);
20+
intrinsics::copy(src, dst, count);
21+
intrinsics::write_bytes(dst, 0xab, count);
22+
}
23+
24+
#[no_mangle]
25+
extern "C" fn main(_argc: i32, _argv: *const *const u8) -> i32 {
26+
unsafe { zero_sized_mem_ops(1) };
27+
0
28+
}

0 commit comments

Comments
 (0)