Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Last active August 29, 2015 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcrichton/8638ac35a79834adcc57 to your computer and use it in GitHub Desktop.
Save alexcrichton/8638ac35a79834adcc57 to your computer and use it in GitHub Desktop.
#include <assert.h>
void free(void *ptr) {
assert(0);
}
void foo(void *ptr) {
free(ptr);
}
#include <dlfcn.h>
#include <assert.h>
#include <stdlib.h>
int main() {
void *lib = dlopen("libfoo." EXT, RTLD_NOW);
assert(lib);
void* ptr = malloc(8);
void (*foo_fn)(void*) = dlsym(lib, "foo");
assert(foo_fn);
foo_fn(ptr);
}
ifeq ($(shell uname),Darwin)
all:
$(CC) -dynamiclib foo.c -o libfoo.dylib
$(CC) main.c -o main -DEXT='"dylib"'
./main
else
all:
$(CC) -fPIC -shared foo.c -o libfoo.dylib
$(CC) main.c -o main -ldl -DEXT='"so"'
./main
endif
$ make
cc -fPIC -shared foo.c -o libfoo.so
cc main.c -o main -ldl -DEXT='"so"'
./main
$ make
cc -dynamiclib foo.c -o libfoo.dylib
cc main.c -o main -DEXT='"dylib"'
./main
Assertion failed: (0), function free, file foo.c, line 4.
make: *** [all] Abort trap: 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment