Skip to content

Instantly share code, notes, and snippets.

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
static void *(*malloc_internal)(size_t size);
void __attribute__ ((constructor)) init(void);
/* Library constructor */
$ LD_PRELOAD=./malloc_wrap.so /bin/uname
Malloc request for 34 bytes got memory: 0x1103010
Malloc request for 10 bytes got memory: 0x1103040
Linux
$ gcc -Wall -shared -fPIC -o malloc_wrap.so malloc_wrap.c -ldl
$ LD_PRELOAD=./malloc_wrap.so /bin/uname
Malloc request for 34 bytes got memory: 0x1103010
Malloc request for 10 bytes got memory: 0x1103040
Linux
$ ./condwait_test
Thread exiting
$ gcc -g3 -Wall -fPIC -shared -o condwait_wrap.so condwait_wrap.c -ldl -lpthread
$ gcc -g3 -Wall -o condwait_test condwait_test.c -lpthread
$ ./condwait_test
Thread exiting
$ LD_PRELOAD=./condwait_wrap.so ./condwait_test
Calling func condwait with cond: 0x600d80, mutex: 0x600dc0...^C
Exit 130
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
static int okexit = 0;
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <pthread.h>
static int (*condwait_internal)(pthread_cond_t *cond, pthread_mutex_t *mutex);
void __attribute__ ((constructor)) init(void);
$ gdb condwait_test
<snip LICENSE>
(gdb) b condwait_test.c:17
Breakpoint 1 at 0x400864: file condwait_test.c, line 17.
(gdb) r
Starting program: condwait_test
[Thread debugging using libthread_db enabled]
[New Thread 0x7ffff7fd5910 (LWP 32595)]
$ gdb condwait_test
<snip LICENSE>
(gdb) set environment LD_PRELOAD ./condwait_wrap.so
(gdb) b condwait_test.c:17
Breakpoint 1 at 0x400864: file condwait_test.c, line 17.
(gdb) r
Starting program: condwait_test
[Thread debugging using libthread_db enabled]
#if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
int
__pthread_cond_wait_2_0 (cond, mutex)
pthread_cond_2_0_t *cond;
pthread_mutex_t *mutex;
{
if (cond->cond == NULL)
{
pthread_cond_t *newcond;