Skip to content

Commit 34e22e8

Browse files
authored
Rollup merge of rust-lang#130245 - RalfJung:miri-alloc-backtrace, r=Amanieu
make basic allocation functions track_caller in Miri for nicer backtraces This matches what we did with basic pointer and atomic operations.
2 parents 283ce13 + dbf585c commit 34e22e8

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

alloc/src/alloc.rs

+13
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub use std::alloc::Global;
8989
#[stable(feature = "global_alloc", since = "1.28.0")]
9090
#[must_use = "losing the pointer will leak memory"]
9191
#[inline]
92+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
9293
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
9394
unsafe {
9495
// Make sure we don't accidentally allow omitting the allocator shim in
@@ -113,6 +114,7 @@ pub unsafe fn alloc(layout: Layout) -> *mut u8 {
113114
/// See [`GlobalAlloc::dealloc`].
114115
#[stable(feature = "global_alloc", since = "1.28.0")]
115116
#[inline]
117+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
116118
pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
117119
unsafe { __rust_dealloc(ptr, layout.size(), layout.align()) }
118120
}
@@ -132,6 +134,7 @@ pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
132134
#[stable(feature = "global_alloc", since = "1.28.0")]
133135
#[must_use = "losing the pointer will leak memory"]
134136
#[inline]
137+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
135138
pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
136139
unsafe { __rust_realloc(ptr, layout.size(), layout.align(), new_size) }
137140
}
@@ -166,13 +169,15 @@ pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8
166169
#[stable(feature = "global_alloc", since = "1.28.0")]
167170
#[must_use = "losing the pointer will leak memory"]
168171
#[inline]
172+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
169173
pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
170174
unsafe { __rust_alloc_zeroed(layout.size(), layout.align()) }
171175
}
172176

173177
#[cfg(not(test))]
174178
impl Global {
175179
#[inline]
180+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
176181
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
177182
match layout.size() {
178183
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
@@ -187,6 +192,7 @@ impl Global {
187192

188193
// SAFETY: Same as `Allocator::grow`
189194
#[inline]
195+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
190196
unsafe fn grow_impl(
191197
&self,
192198
ptr: NonNull<u8>,
@@ -237,16 +243,19 @@ impl Global {
237243
#[cfg(not(test))]
238244
unsafe impl Allocator for Global {
239245
#[inline]
246+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
240247
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
241248
self.alloc_impl(layout, false)
242249
}
243250

244251
#[inline]
252+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
245253
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
246254
self.alloc_impl(layout, true)
247255
}
248256

249257
#[inline]
258+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
250259
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
251260
if layout.size() != 0 {
252261
// SAFETY: `layout` is non-zero in size,
@@ -256,6 +265,7 @@ unsafe impl Allocator for Global {
256265
}
257266

258267
#[inline]
268+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
259269
unsafe fn grow(
260270
&self,
261271
ptr: NonNull<u8>,
@@ -267,6 +277,7 @@ unsafe impl Allocator for Global {
267277
}
268278

269279
#[inline]
280+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
270281
unsafe fn grow_zeroed(
271282
&self,
272283
ptr: NonNull<u8>,
@@ -278,6 +289,7 @@ unsafe impl Allocator for Global {
278289
}
279290

280291
#[inline]
292+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
281293
unsafe fn shrink(
282294
&self,
283295
ptr: NonNull<u8>,
@@ -325,6 +337,7 @@ unsafe impl Allocator for Global {
325337
#[cfg(all(not(no_global_oom_handling), not(test)))]
326338
#[lang = "exchange_malloc"]
327339
#[inline]
340+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
328341
unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
329342
let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
330343
match Global.allocate(layout) {

alloc/src/boxed.rs

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ impl<T> Box<T> {
250250
#[stable(feature = "rust1", since = "1.0.0")]
251251
#[must_use]
252252
#[rustc_diagnostic_item = "box_new"]
253+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
253254
pub fn new(x: T) -> Self {
254255
#[rustc_box]
255256
Box::new(x)

0 commit comments

Comments
 (0)