Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#[track_caller] for allocator APIs? #74763

Closed
anp opened this issue Jul 25, 2020 · 3 comments
Closed

#[track_caller] for allocator APIs? #74763

anp opened this issue Jul 25, 2020 · 3 comments

Comments

@anp
Copy link
Member

anp commented Jul 25, 2020

Apologies if this has been raised before, but I've been playing around with trying to track where allocations happen with something like so:

use libc_print::libc_println;
use std::alloc::{GlobalAlloc, Layout};
use std::panic::Location;

pub struct TracedAlloc<T: GlobalAlloc> {
    pub allocator: T,
}

unsafe impl<T> GlobalAlloc for TracedAlloc<T>
where
    T: GlobalAlloc,
{
    #[track_caller]
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        libc_println!("Alloc {:?} at {:?}", layout, Location::caller());
        self.allocator.alloc(layout)
    }

    #[track_caller]
    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        libc_println!("Dealloc {:?} at {:?}", layout, Location::caller());
        self.allocator.dealloc(ptr, layout)
    }
}

However the caller location is always the line I've put #[global_allocator] which makes #[track_caller] useless in this context.

Originally posted by @xd009642 in #47809 (comment)


note from @anp: I'm not sure how the global allocator hooks in but this might be possible?

@jdm
Copy link
Contributor

jdm commented Jul 25, 2020

I'm not sure if this will work, given the indirection in

#[stable(feature = "global_alloc", since = "1.28.0")]
#[inline]
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
unsafe { __rust_alloc(layout.size(), layout.align()) }
}
. That method is called by the methods generated by #[global_alloc], per this comment.

@anp
Copy link
Member Author

anp commented Jul 25, 2020

Yeah, the only way to carry #[track_caller] through an extern "Rust" block is if both sides have it declared. It's technically possible but it would be a new (breaking) requirement on all allocators so I don't think that's feasible.

@anp anp closed this as completed Jul 25, 2020
@lqd
Copy link
Member

lqd commented Jul 26, 2020

cc #74433

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants