How to add prints inside library/std/src/sys/alloc/unix.rs?

⚓ rust    📅 2025-05-09    👤 surdeus    👁️ 2      

surdeus

I am trying to add prints right before the call to libc::malloc here. Whenever I do that and build the library with ./x.py build library && ./x.py install , it succeeds.

However, when I want to test it by using that new compiler I just built on my rust programs, I get the following error:

thread 'main' has overflowed its stack
fatal runtime error: stack overflow, aborting
Aborted (core dumped)

My objective is to track the number of times libc::malloc gets called by my rust programs.

As a concrete example, following is one simple rust program from the rustbook I was hoping my change to rust std library would help reveal:

fn main()
{
	let b = Box::new(5);
	println!("b = {b}");
	println!("b as :p = {:p}, &b as :p = {:p}", b, &b);
}

When I build and run the above program with my newly built compiler, I get that stack overflow runtime error.

Here's some of my attempts to modify library/std/src/sys/alloc/unix.rs:

diff --git a/library/std/src/sys/alloc/unix.rs b/library/std/src/sys/alloc/unix.rs
index a7ac4117ec9..e0eeef18a40 100644
--- a/library/std/src/sys/alloc/unix.rs
+++ b/library/std/src/sys/alloc/unix.rs
@@ -1,17 +1,37 @@
 use super::{MIN_ALIGN, realloc_fallback};
 use crate::alloc::{GlobalAlloc, Layout, System};
 use crate::ptr;
+use crate::env::var;
+use libc::{c_char, c_int};
+
+unsafe extern "C" {
+    fn printf(fmt: *const c_char, ...) -> c_int;
+}
 
 #[stable(feature = "alloc_system_type", since = "1.28.0")]
 unsafe impl GlobalAlloc for System {
-    #[inline]
+    // #[inline]
     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
         // jemalloc provides alignment less than MIN_ALIGN for small allocations.
         // So only rely on MIN_ALIGN if size >= align.
         // Also see <https://github.com/rust-lang/rust/issues/45955> and
         // <https://github.com/rust-lang/rust/issues/62251#issuecomment-507580914>.
         if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
-            unsafe { libc::malloc(layout.size()) as *mut u8 }
+            if let Ok(_) = var("FOO_DEBUG") {
+                unsafe {
+                    printf("FOO_DEBUG:library/std/src/sys/alloc/unix.rs:25:HERE\n\0".as_ptr() as *const i8);
+                }
+            }
+            unsafe {
+                //if let Ok(val) = var("FOO_DEBUG") {
+                    //libc::printf("FOO_DEBUG:library/std/src/sys/alloc/unix.rs:15:HERE\n\0".as_ptr() as *const i8);
+                    //set_var("FOO_DEBUG", "library/std/src/sys/alloc/unix.rs:18");
+                    //if "1" == val {
+                        //set_var("FOO_DEBUG", "28");
+                    //}
+                //}
+                libc::malloc(layout.size()) as *mut u8
+            }
         } else {
             // `posix_memalign` returns a non-aligned value if supplied a very
             // large alignment on older versions of Apple's platforms (unknown

2 posts - 2 participants

Read full topic

🏷️ rust_feed