radareorg / radare2

UNIX-like reverse engineering framework and command-line toolset
https://www.radare.org/
GNU Lesser General Public License v3.0
20.3k stars 2.97k forks source link

Compiled Java Libraries not working #469

Closed ghost closed 10 years ago

ghost commented 10 years ago

Hi,

I was wondering if anyone has tried to use the Java libraries compiled from r2-bindings. I am currently trying to use them and thought that I had everything compiled correctly, however when I tried to compile and run the TestRBin.java file found in the r2-bindings-0.9.6/java/ folder, it compiles fine (and I have added the radare2 libraries to my build path) but when I run it I get the error

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.radare.radare2.r_binJNI.new_RBin()J
 at org.radare.radare2.r_binJNI.new_RBin(Native Method)
 at org.radare.radare2.RBin.<init>(RBin.java:64)
 at TestRBin.main(TestRBin.java:17)

I traced the error back to the org/radare/radare2/r_binJNI.java file and it looks like the constructor for RBin() is not actually implemented? I tried using some other constructors (e.g. RBinClass) as well and they gave me the same error.

If anyone has any idea how to get the radare2 java libraries to work that would be great! Also is there any more detailed documentation for the java libraries?

Thanks, -4leaf

radare commented 10 years ago

Looks like a swig bug. I have contacted the guy who did the last commit on that directory.. it was working in the past...

AitorATuin commented 10 years ago

Hi, this evening i will take a look to that directory. Maybe is a good idea to try the JNA api again.

radare commented 10 years ago

I think that python and java should be the main supported bindings. In addition we can have nodejs and ctypes which should work out of the box.. So we should build and check them in the testsuite

On 20 Dec 2013, at 06:57, "Aitor P. Iturri" notifications@github.com wrote:

Hi, this evening i will take a look to that directory. Maybe is a good idea to try the JNA api again.

— Reply to this email directly or view it on GitHub.

XVilka commented 10 years ago

@radare if move to the autogenerated testsutes you can autotest all supported langauges. Btw, why not make ctypes default for python (instead of crappy swig stuff)?

radare commented 10 years ago

Because im still waiting for hugo teso feedback (after 3 months) to verify that the ctypes work fine with their unpublished bokken branch...

Yeah.. I'll think about the testsuite ;)

About ctypes... We need to make them work on py3. (There's a guy already working on that)

On 20 Dec 2013, at 13:04, Anton Kochkov notifications@github.com wrote:

@radare if move to the autogenerated testsutes you can autotest all supported langauges. Btw, why not make ctypes default for python (instead of crappy swig stuff)?

— Reply to this email directly or view it on GitHub.

ghost commented 10 years ago

Thanks @radare and @AitorATuin, in the meantime I'll try compiling them again on another computer. Also is there anywhere other than here [http://radare.org/vdoc/libr/index.htm] that has an explanation of what each class is used for? It doesn't have to be java specific!

Thanks again, -4leaf

radare commented 10 years ago

There's no documentation about the api. But several examples and the function name should be clear enought. Grep is your friend if you want to see how to use it ;)

The valadoc needs some work to add more documentation, explanations and examples.

On 20 Dec 2013, at 14:53, 4leafclovrs notifications@github.com wrote:

Thanks @radare and @AitorATuin, in the meantime I'll try compiling them again on another computer. Also is there anywhere other than here [http://radare.org/vdoc/libr/index.htm] that has an explanation of what each class is used for? It doesn't have to be java specific!

Thanks again, -4leaf

— Reply to this email directly or view it on GitHub.

ghost commented 10 years ago

Okay I'll look through all of the examples then! Thanks again.

AitorATuin commented 10 years ago

Im just now with the java binding, the problem seems to be that the .so files are not found (maybe they are not installed). Anyway, seems that the bindings are now in a separate repository, arent they?

ghost commented 10 years ago

The .jar file I am using for libraries was compiled from the separate bindings repository, so maybe that is the issue?
-- nope, it's not the issue. I just tried using the libraries from java directory from the radare2/r2-bindings/ folder and it gave me the same error.

Also, is there a way to make sure that all of the dependencies get compiled into the .jar file/are there any dependencies left out by default?

radare commented 10 years ago

Yep i moved them out to make clear that separation.

On 20 Dec 2013, at 20:06, "Aitor P. Iturri" notifications@github.com wrote:

Im just now with the java binding, the problem seems to be that the .so files are not found (maybe they are not installed). Anyway, seems that the bindings are now in a separate repository, arent they?

— Reply to this email directly or view it on GitHub.

AitorATuin commented 10 years ago

@4leafclovrs mmmm i installed radare2 from git using arch packages and then i installed radare2-bindings, also from git, using prefix /tmp and everything works nice.

λ hilbert java → λ git java* →./configure --prefix=/tmp/radare2
λ hilbert java → λ git java* →make (just only the java binding removing everything but java from supported.langs)
λ hilbert java → λ git java* →make install
λ hilbert /tmp → javac -cp /tmp/radare2/share/radare2/java/jni/jar/radare2.jar TestRBin.java
λ hilbert /tmp → java -cp /tmp/radare2/share/radare2/java/jni/jar/radare2.jar:. -Djava.library.path=/tmp/radare2/share/radare2/java/jni/lib/ TestRBin 

offset=0x00000238 size=00028 .interp                                      
offset=0x00000254 size=00032 .note.ABI-tag
offset=0x00000274 size=00036 .note.gnu.build-id
offset=0x00000298 size=00180 .gnu.hash
offset=0x00000350 size=03072 .dynsym
offset=0x00000f50 size=01420 .dynstr
offset=0x000014dc size=00256 .gnu.version
offset=0x000015e0 size=00144 .gnu.version_r
offset=0x00001670 size=00168 .rela.dyn
offset=0x00001718 size=02664 .rela.plt
offset=0x00002180 size=00026 .init
offset=0x000021a0 size=01792 .plt
offset=0x000028a0 size=63948 .text
offset=0x0001226c size=00009 .fini
offset=0x00012280 size=20700 .rodata
offset=0x0001735c size=01836 .eh_frame_hdr
offset=0x00017a88 size=08476 .eh_frame
offset=0x00019de8 size=00008 .init_array
offset=0x00019df0 size=00008 .fini_array
offset=0x00019df8 size=00008 .jcr
offset=0x00019e00 size=00496 .dynamic
offset=0x00019ff0 size=00016 .got
offset=0x0001a000 size=00912 .got.plt
offset=0x0001a3a0 size=00608 .data
offset=0x0001a600 size=03432 .bss
offset=0x0001a600 size=00017 .comment
offset=0x0001a611 size=00248 .shstrtab

did you have installed the correct libraries for the JNI interface?

radare commented 10 years ago

I have enhaced the Makefile inside java/ to separate compilation and installation steps. (this is in the new radare2-bindings repo)

radare commented 10 years ago

thanks!

On 21 Dec 2013, at 21:50, Aitor P. Iturri notifications@github.com wrote:

@4leafclovrs mmmm i installed radare2 from git using arch packages and then i installed radare2-bindings, also from git, using prefix /tmp and everything works nice.

λ hilbert java → λ git java* →./configure --prefix=/tmp/radare2 λ hilbert java → λ git java* →make (just only the java binding removing everything but java from supported.langs) λ hilbert java → λ git java* →make install λ hilbert /tmp → javac -cp /tmp/radare2/share/radare2/java/jni/jar/radare2.jar TestRBin.java λ hilbert /tmp → java -cp /tmp/radare2/share/radare2/java/jni/jar/radare2.jar:. -Djava.library.path=/tmp/radare2/share/radare2/java/jni/lib/ TestRBin

offset=0x00000238 size=00028 .interp
offset=0x00000254 size=00032 .note.ABI-tag offset=0x00000274 size=00036 .note.gnu.build-id offset=0x00000298 size=00180 .gnu.hash offset=0x00000350 size=03072 .dynsym offset=0x00000f50 size=01420 .dynstr offset=0x000014dc size=00256 .gnu.version offset=0x000015e0 size=00144 .gnu.version_r offset=0x00001670 size=00168 .rela.dyn offset=0x00001718 size=02664 .rela.plt offset=0x00002180 size=00026 .init offset=0x000021a0 size=01792 .plt offset=0x000028a0 size=63948 .text offset=0x0001226c size=00009 .fini offset=0x00012280 size=20700 .rodata offset=0x0001735c size=01836 .eh_frame_hdr offset=0x00017a88 size=08476 .eh_frame offset=0x00019de8 size=00008 .init_array offset=0x00019df0 size=00008 .fini_array offset=0x00019df8 size=00008 .jcr offset=0x00019e00 size=00496 .dynamic offset=0x00019ff0 size=00016 .got offset=0x0001a000 size=00912 .got.plt offset=0x0001a3a0 size=00608 .data offset=0x0001a600 size=03432 .bss offset=0x0001a600 size=00017 .comment offset=0x0001a611 size=00248 .shstrtab did you have installed the correct libraries for the JNI interface?

— Reply to this email directly or view it on GitHub.

ghost commented 10 years ago

@AitorATuin I just tried installing the java bindings and all of the dependencies for the bindings again from scratch, and I believe that you have found the problem!

I tried installing the JNI libraries by going to http://packages.ubuntu.com/raring/amd64/allpackages and checking to see which ones would be relevant for JNI and installing those, however doing that and then trying to install the java bindings again proved unsuccessful.

Can you briefly explain to me how to install the correct libraries for the JNI interface or point me to a tutorial if it isn't just a "apt-get install"? I am running Ubuntu 13.04.

Thanks, -4leaf

radare commented 10 years ago

I'm forwarding this issue to the debian maintainer as long as he is currently working on packaging the bindings of 0.9.6.

On 23 Dec 2013, at 14:47, 4leafclovrs notifications@github.com wrote:

@AitorATuin I just tried installing the java bindings and all of the dependencies for the bindings again from scratch, and I believe that you have found the problem!

I tried installing the JNI libraries by going to http://packages.ubuntu.com/raring/amd64/allpackages and checking to see which ones would be relevant for JNI and installing those, however doing that and then trying to install the java bindings again proved unsuccessful.

Can you briefly explain to me how to install the correct libraries for the JNI interface or point me to a tutorial if it isn't just a "apt-get install"? I am running Ubuntu 13.04.

Thanks, -4leaf

— Reply to this email directly or view it on GitHub.

AitorATuin commented 10 years ago

@4leafclovrs I dont know how is the package working on ubuntu, but anyway the idea is the following: the radare2-bindings will install the radare2.jar file with all the java code compiled:

and a lib directory under jni

Under that lib directory are placed the libraries for which the jni is compiled against, so at runtime your JMV must to be pointing to that directory in order to load the correct libraries. Take in care that the java example calls to a lib "r_bin" which is transformed to libr_bin.so on linux, the problem is that there is another libr_bin.so (the library used by radare2) so if you dont pass a the correct path to the so in JVM invocation (with -Djava.library.path option) the JVM is going to use the default one (your system library, the radare2's libr_bin.so library) so you wont get an exception about not founding the library but JVM will tell you that it cant found any of the methods in the binding.

In short, you have to use the jni libraries, not the ones that come with radare2 in order to use the jni interface. Maybe is a good idea to rename the jni libraries to avoid this kind of confusions, libjr_bin.so or something like that.

Im going to package the radare2-bindigs for using in the AUR repo on arch.

Cheers.

radare commented 10 years ago

Renaming looks fine to me

On 23 Dec 2013, at 15:26, "Aitor P. Iturri" notifications@github.com wrote:

@4leafclovrs I dont know how is the package working on ubuntu, but anyway the idea is the following: the radare2-bindings will install the radare2.jar file with all the java code compiled:

/tmp/radare2/share/radare2/java/jni/jar/radare2.jar - Using /tmp/radare2 as prefix and a lib directory under jni

/tmp/radare2/share/radare2/java/jni/lib - Using also /tmp/radare2 as prefix Under that lib directory are placed the libraries for which the jni is compiled against, so at runtime your JMV must to be pointing to that directory in order to load the correct libraries. Take in care that the java example calls to a lib "r_bin" which is transformed to libr_bin.so on linux, the problem is that there is another libr_bin.so (the library used by radare2) so if you dont pass a the correct path to the so in JVM invocation (with -Djava.library.path option) the JVM is going to use the default one (your system library, the radare2's libr_bin.so library) so you wont get an exception about not founding the library but JVM will tell you that it cant found any of the methods in the binding.

In short, you have to use the jni libraries, not the ones that come with radare2 in order to use the jni interface. Maybe is a good idea to rename the jni libraries to avoid this kind of confusions, libjr_bin.so or something like that.

Im going to package the radare2-bindigs for using in the AUR repo on arch.

Cheers.

— Reply to this email directly or view it on GitHub.

Jo-Con-El commented 10 years ago

@4leafclovrs: I am the Debian maintainer now for all the radare/valabind suite. In response to your question, the Java libraries are not yet created but I don't think it would take me a lot of time to package them. I had in mind to make them for the 0.9.6-2 version but I can definitely release a preliminary package.

Let me get my act together and see what I can do to make this happen.

All my development happens in the open, by the way: http://anonscm.debian.org/gitweb/?p=collab-maint/radare2.git http://anonscm.debian.org/gitweb/?p=collab-maint/radare2-bindings.git http://anonscm.debian.org/gitweb/?p=pkg-vala/valabind.git

ghost commented 10 years ago

@Jo-Con-El Thanks a lot. I actually got everything working on another computer of mine running a fresh version of Ubuntu 13.10. I tried copying and pasting the radare2.jar file from the working computer to the eclipse project on my other computer, and adding it to the build path etc... but I keep getting the same unsatisfied link error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.radare.radare2.r_binJNI.new_RBin()J
        at org.radare.radare2.r_binJNI.new_RBin(Native Method)
        at org.radare.radare2.RBin.<init>(RBin.java:64)
        at radaretest.radaretest.main(radaretest.java:26)

Is there something special other than copying the .jar file that I might have to do? During the past hour I've tried a lot of copying/pasting things from one computer to the next but I cannot get the test file to run on my "not working" computer even outside of eclipse.

I have a feeling that the .jar file I am compiling is not putting all of the libraries in it -- it might link some of them back to the hard disk on my working computer. Is there a way (a flag or something) I can give that will package all of the necessary libraries in the .jar file?

(Also is there a better way I should respond to you/other users on GitHub other than replying to the same issue that seems to have been resolved for most people?)

Thanks, -4leaf

Jo-Con-El commented 10 years ago

Hey, @4leafclovrs, I have been working in the radare2-java package and I feel confident about it. Would you like to give it a try? They are not final ones (so you should change them once I release the official ones). Oh well.

You will need libradare2-.0.9.6 and radare2-java, from http://people.debian.org/~ender. Tell me how it goes.

ghost commented 10 years ago

Hi @Jo-Con-El, I just tried to install the libradare2 and radare2-java packages, but it says that I need a version of libmagic1 that is >=5.12, but doing a little research I don't think that the Ubuntu libmagic1 has been released in that version. Do you think I should try to convert a .rpm package to a .deb package and install the dependency? Also, once I install the java-libraries, do you think I would still have to add the .war file to my build path in eclipse? Or would it be in the path already from the package installation.

Thanks, -4leaf

Jo-Con-El commented 10 years ago

Hey, @ 4leafclovrs. I'd just install libmagic1 from trusty (you are in 5.14 there) or Debian.

The JAR is in /usr/lib/jni.

radare commented 10 years ago

i would love to know why debian link against libmagic, when its an optional feature that its used by nobody… the previous maintainer told me that r2 ships a slightly modified version of libmagic from openbsd. and debian specifies that if a separate pkg implements a feature it must be linked to the external lib instead of using the embedded one. thats why i added —with-sysmagic configure flag, but im not sure if we should use the internal libmagic or link to the system one (shared). the embedded one is much smaller and faster than the gnu one..

On 26 Dec 2013, at 18:34, Jo-Con-El notifications@github.com wrote:

Hey, @ 4leafclovrs. I'd just install libmagic1 from trusty (you are in 5.14 there) or Debian.

The JAR is in /usr/lib/jni. — Reply to this email directly or view it on GitHub.

Jo-Con-El commented 10 years ago

I don't have context on that decision, but definitely makes sense to use a system one if they don't differ much. libmagic (the GNU one at least) is not free from security problems so I prefer being covered by the Debian advisories. That said, I was surprised to find a dependency on libmagic1

= 5.12, it seems that they changed the ABI or something like that.

Do you still need the Java bindings? I can definitely build them for you in a dedicated chroot.

radare commented 10 years ago

That libmagic dependency thing is a total mess. I would prefer to only depend on libr/magic one instead of having the possibility to use the system one. Is this possible with debian?

Yep. Having java bindings packaged can be useful i guess

Thanks

On 28 Jan 2014, at 19:07, Jo-Con-El notifications@github.com wrote:

I don't have context on that decision, but definitely makes sense to use a system one if they don't differ much. libmagic (the GNU one at least) is not free from security problems so I prefer being covered by the Debian advisories. That said, I was surprised to find a dependency on libmagic1

= 5.12, it seems that they changed the ABI or something like that.

Do you still need the Java bindings? I can definitely build them for you in a dedicated chroot. — Reply to this email directly or view it on GitHub.