Skip to content

Instantly share code, notes, and snippets.

@mazhe
Last active November 30, 2016 10:49
Show Gist options
  • Save mazhe/f6993d7a48ecbab6c0c979b9a7a40912 to your computer and use it in GitHub Desktop.
Save mazhe/f6993d7a48ecbab6c0c979b9a7a40912 to your computer and use it in GitHub Desktop.
#include <dlfcn.h>
#include <stdio.h>
#include <jni.h>
typedef void* (*malloc_t)(size_t size);
/* The function */
void
foo(void)
{
malloc_t real_malloc;
/* Find the malloc function */
if ((real_malloc = (void *(*)(size_t))dlfunc(RTLD_NEXT, "malloc")) == NULL) {
fprintf(stderr, "could not locate function malloc: %s\n", dlerror());
}
else {
fprintf(stderr, "malloc function located at %p\n", real_malloc);
}
}
/* Java JNI call function */
JNIEXPORT void JNICALL
Java_testfoo_foo(JNIEnv *env, jobject thisObj)
{
foo();
}
JAVA_HOME?=/usr/local/openjdk8
CFLAGS+=-fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/$$(uname|tr '[:upper:]' '[:lower:]')
all: testfoo testfoo.class
testfoo.class: testfoo.java
javac testfoo.java
testfoo: testfoo.o libfoo.so
${CC} -o $@ testfoo.o ./libfoo.so
libfoo.so: foo.o
${CC} -shared -o $@ foo.o
.PHONY: clean
clean:
rm -f testfoo testfoo.class libfoo.so *.o
void foo(void);
int
main(void)
{
foo();
return 0;
}
public class testfoo {
static {
System.loadLibrary("foo");
}
private native void foo();
public static void main(String[] args) {
new testfoo().foo();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment