Auto merge of #15168 - HKalbasi:mir, r=HKalbasi

Fix realloc problem in allocating smaller amounts
This commit is contained in:
bors 2023-06-29 13:59:52 +00:00
commit 70a01fead5
2 changed files with 11 additions and 6 deletions

View File

@ -183,6 +183,7 @@ fn allocator() {
*ptr = 23;
*ptr2 = 32;
let ptr = __rust_realloc(ptr, 4, 1, 8);
let ptr = __rust_realloc(ptr, 8, 1, 3);
let ptr2 = ((ptr as usize) + 1) as *mut u8;
*ptr + *ptr2
};

View File

@ -144,14 +144,18 @@ fn exec_alloc_fn(
let [ptr, old_size, align, new_size] = args else {
return Err(MirEvalError::TypeError("rustc_allocator args are not provided"));
};
let ptr = Address::from_bytes(ptr.get(self)?)?;
let old_size = from_bytes!(usize, old_size.get(self)?);
let new_size = from_bytes!(usize, new_size.get(self)?);
let align = from_bytes!(usize, align.get(self)?);
let result = self.heap_allocate(new_size, align);
Interval { addr: result, size: old_size }
.write_from_interval(self, Interval { addr: ptr, size: old_size })?;
destination.write_from_bytes(self, &result.to_bytes())?;
if old_size >= new_size {
destination.write_from_interval(self, ptr.interval)?;
} else {
let ptr = Address::from_bytes(ptr.get(self)?)?;
let align = from_bytes!(usize, align.get(self)?);
let result = self.heap_allocate(new_size, align);
Interval { addr: result, size: old_size }
.write_from_interval(self, Interval { addr: ptr, size: old_size })?;
destination.write_from_bytes(self, &result.to_bytes())?;
}
}
_ => not_supported!("unknown alloc function"),
}