Auto merge of #15168 - HKalbasi:mir, r=HKalbasi
Fix realloc problem in allocating smaller amounts
This commit is contained in:
commit
70a01fead5
@ -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
|
||||
};
|
||||
|
@ -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"),
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user