# LD\_PRELOAD does not override malloc\_usable\_size from glibc

**URL:** <https://discourse.julialang.org/t/ld-preload-does-not-override-malloc-usable-size-from-glibc/115369>\
**Category:** General Usage\
**Created:** [June 8, 2024, 7:24pm UTC](https://discourse.julialang.org/t/ld-preload-does-not-override-malloc-usable-size-from-glibc/115369 "2024-06-08T19:24:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![remysucre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remysucre/32/209833_2.png) [@remysucre](https://discourse.julialang.org/u/remysucre)\
**Post date:** [June 8, 2024, 7:24pm UTC](https://discourse.julialang.org/t/ld-preload-does-not-override-malloc-usable-size-from-glibc/115369/1 "2024-06-08T19:24:29Z")

</div>

I’m trying to override glibc’s `malloc_usable_size`, but it seems LD\_PRELOAD doesn’t work. To replicate on linux (glibc):

Save the following content in `rmalloc.c`:

```julia
#include <stddef.h>
#include <stdio.h>

size_t malloc_usable_size(const void *ptr) {
  printf("RMALLOC");
  return 0;
}

```

Compile with `gcc -shared -fPIC rmalloc.c -o rmalloc.so`; then start Julia with `LD_PRELOAD=./rmalloc.so julia --project`. In the REPL, run the following:

```julia
malloc_usable_size(ptr::Ptr{Cvoid}) = ccall(:malloc_usable_size, Csize_t, (Ptr{Cvoid},), ptr)
ptr = ccall(:malloc, Ptr{Cvoid}, (Csize_t,), 1024)
size = malloc_usable_size(ptr)
println("Usable size of allocated memory: ", size)

```

Then see that `RMALLOC` does not get printed and the allocated memory is not 0.

However, if I override `malloc` to return `NULL`, `LD_PRELOAD` crashes Julia which suggests the override succeeds. Why does this not also work for `malloc_usable_size`?
