Skip to content

Instantly share code, notes, and snippets.

/foo.c Secret

Created November 12, 2014 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4feb29bbec79f9a124e0 to your computer and use it in GitHub Desktop.
Save anonymous/4feb29bbec79f9a124e0 to your computer and use it in GitHub Desktop.
#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 _
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment