Finding code that allocates

Hi! I'm trying to help set up the futures-lite crate to work without std and without an allocator (GitHub issue). Now I'm seeing this error:

error: no global memory allocator found but one is required; link to std or add `#[global_allocator]` to a static item that implements the GlobalAlloc trait.

Is there an easy way to see what dependency or part of code is requiring allocation? My best idea so far is to write an allocator that prints a backtrace and then run the tests with that allocator enabled. That wouldn't be 100% guaranteed to find every possible allocation but it might be good enough to work. But I feel like there is probably a more official way to go about this.

If you write an allocator you can compile it and then you can look at the disassembly (should be reasonably comparable to source with a debug build and no optimizations), find invocations and trace them back. If you enable optimizations and LTO the allocator should be completely optimized away.

Thanks for that idea! Do you know of an easy way to see the disassembly for all tests?

The error you mentioned happens when any crate directly or indirectly depends on liballoc. Be it through libstd or an explicit extern crate alloc;. It doesn't matter if liballoc is actually used or not.

Found the culprit:

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;

@bjorn3 thanks! I'm guessing you just searched for the string use alloc or something like that? Nice and simple...

I actually first tried if there was still a problem when only using the dependencies of futures-lite. There wasn't. I then looked through the files of futures-lite to see if I could find either a liballoc or libstd reference when the std feature is not enabled..

1 Like