Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 70a01fe

Browse files
committed
Auto merge of rust-lang#15168 - HKalbasi:mir, r=HKalbasi
Fix realloc problem in allocating smaller amounts
2 parents d7f4c21 + 2272803 commit 70a01fe

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

crates/hir-ty/src/consteval/tests/intrinsics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ fn allocator() {
183183
*ptr = 23;
184184
*ptr2 = 32;
185185
let ptr = __rust_realloc(ptr, 4, 1, 8);
186+
let ptr = __rust_realloc(ptr, 8, 1, 3);
186187
let ptr2 = ((ptr as usize) + 1) as *mut u8;
187188
*ptr + *ptr2
188189
};

crates/hir-ty/src/mir/eval/shim.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,18 @@ impl Evaluator<'_> {
144144
let [ptr, old_size, align, new_size] = args else {
145145
return Err(MirEvalError::TypeError("rustc_allocator args are not provided"));
146146
};
147-
let ptr = Address::from_bytes(ptr.get(self)?)?;
148147
let old_size = from_bytes!(usize, old_size.get(self)?);
149148
let new_size = from_bytes!(usize, new_size.get(self)?);
150-
let align = from_bytes!(usize, align.get(self)?);
151-
let result = self.heap_allocate(new_size, align);
152-
Interval { addr: result, size: old_size }
153-
.write_from_interval(self, Interval { addr: ptr, size: old_size })?;
154-
destination.write_from_bytes(self, &result.to_bytes())?;
149+
if old_size >= new_size {
150+
destination.write_from_interval(self, ptr.interval)?;
151+
} else {
152+
let ptr = Address::from_bytes(ptr.get(self)?)?;
153+
let align = from_bytes!(usize, align.get(self)?);
154+
let result = self.heap_allocate(new_size, align);
155+
Interval { addr: result, size: old_size }
156+
.write_from_interval(self, Interval { addr: ptr, size: old_size })?;
157+
destination.write_from_bytes(self, &result.to_bytes())?;
158+
}
155159
}
156160
_ => not_supported!("unknown alloc function"),
157161
}

0 commit comments

Comments
 (0)