Last active
November 30, 2016 10:49
-
-
Save mazhe/f6993d7a48ecbab6c0c979b9a7a40912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void foo(void); | |
int | |
main(void) | |
{ | |
foo(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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