Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have a few options:</p> <ol> <li><p><strong>GLIBC-specific solution (mostly Linux).</strong> If your compilation environment is <code>glibc</code> with <code>gcc</code>, the preferred way is to use <a href="http://www.gnu.org/software/libtool/manual/libc/Hooks-for-Malloc.html" rel="noreferrer">malloc hooks</a>. Not only it lets you specify custom <code>malloc</code> and <code>free</code>, but will also identify the caller by the return address on the stack.</p></li> <li><p><strong>POSIX-specific solution.</strong> Define <code>malloc</code> and <code>free</code> as wrappers to the original allocation routines in your executable, which will "override" the version from libc. Inside the wrapper you can call into the original <code>malloc</code> implementation, which you can look up using <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html" rel="noreferrer"><code>dlsym</code></a> with <code>RTLD_NEXT</code> handle. Your application or library that defines wrapper functions needs to link with <code>-ldl</code>.</p> <pre><code>#define _GNU_SOURCE #include &lt;dlfcn.h&gt; #include &lt;stdio.h&gt; void* malloc(size_t sz) { void *(*libc_malloc)(size_t) = dlsym(RTLD_NEXT, "malloc"); printf("malloc\n"); return libc_malloc(sz); } void free(void *p) { void (*libc_free)(void*) = dlsym(RTLD_NEXT, "free"); printf("free\n"); libc_free(p); } int main() { free(malloc(10)); return 0; } </code></pre></li> <li><p><strong>Linux specific.</strong> You can override functions from dynamic libraries non-invasively by specifying them in the <code>LD_PRELOAD</code> environment variable.</p> <pre><code>LD_PRELOAD=mymalloc.so ./exe </code></pre></li> <li><p><strong>Mac OSX specific.</strong></p> <p>Same as Linux, except you will be using <code>DYLD_INSERT_LIBRARIES</code> environment variable.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload