Skip to content

Instantly share code, notes, and snippets.

@alexcrichton

alexcrichton/foo.c Secret

Forked from anonymous/foo.c
Last active Aug 29, 2015
Embed
What would you like to do?
#include <assert.h>
#include <stdlib.h>
#include <dlfcn.h>
int main() {
void *lib = dlopen("./libfoo.so", RTLD_NOW | RTLD_DEEPBIND);
assert(lib);
void* (*foo_fn)(void) = dlsym(lib, "foo");
assert(foo_fn);
void *foo_ptr = foo_fn();
assert(foo_ptr);
free(foo_ptr);
void* (*bar_fn)(void) = dlsym(lib, "bar");
assert(bar_fn);
void *bar_ptr = bar_fn();
assert(bar_ptr);
free(bar_ptr);
}
extern crate libc;
extern {
fn strdup(a: *mut u8) -> *mut u8;
}
#[no_mangle]
pub unsafe extern fn foo() -> *mut u8 {
strdup("foo\0".as_ptr() as *mut _)
}
#[no_mangle]
pub unsafe extern fn bar() -> *mut u8 {
libc::malloc(8) as *mut _
}
$ gcc foo.c -ldl
# this rustc is today's nightly
$ rustc foo.rs --crate-type dylib
$ ./a.out
# this rustc is patched with no jemalloc prefix
$ ./x86_64-unknown-linux-gnu/stage2/bin/rustc foo.rs --crate-type dylib
$ ./a.out
*** Error in `./a.out': free(): invalid pointer: 0x00007f1045823000 ***
zsh: abort (core dumped) ./a.out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.