Skip to content

Commit 6e4264b

Browse files
authored
Add calloc
1 parent 5c5cc2a commit 6e4264b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/fn_call.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,28 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a + 'mir>: crate::MiriEvalContextExt<'
9393
this.write_scalar(Scalar::Ptr(ptr.with_default_tag()), dest)?;
9494
}
9595
}
96+
"calloc" => {
97+
let items = this.read_scalar(args[0])?.to_usize(this)?;
98+
let count = this.read_scalar(args[1])?.to_usize(this)?;
99+
let size = if let Some(size) = items.checked_add(count) {
100+
size
101+
} else {
102+
return err!(MachineError(format!(
103+
"calloc: overflow of items * size: {} * {}",
104+
items, size,
105+
)));
106+
};
107+
if size == 0 {
108+
this.write_null(dest)?;
109+
} else {
110+
let align = this.tcx.data_layout.pointer_align.abi;
111+
let ptr = this.memory_mut().allocate(Size::from_bytes(size), align, MiriMemoryKind::C.into());
112+
this.memory_mut()
113+
.get_mut(ptr.alloc_id)?
114+
.write_repeat(tcx, ptr, 0, Size::from_bytes(size))?;
115+
this.write_scalar(Scalar::Ptr(ptr.with_default_tag()), dest)?;
116+
}
117+
}
96118
"posix_memalign" => {
97119
let ret = this.deref_operand(args[0])?;
98120
let align = this.read_scalar(args[1])?.to_usize(this)?;

0 commit comments

Comments
 (0)