Mixed code (native, managed): how does it (technically) interact?

I basically understand the idea of โ€‹โ€‹managed and native code and their difference . But how is it technically possible for them to communicate with each other? Imagine the following example:

I have a static or dynamic C ++ library that is compiled for a specific platform. Now I am writing a Java program. Inside this code, I call library functions with the keyword "native". I create a jar file with bytecode and the C ++ library files remain separate. The result will no longer be platform independent.

  • But how does a Java program know if called native methods exist?

  • How is all the program code executed at runtime? I know that bytecode will be interpreted or compiled using JIT.

  • How does all this fit into the sandbox paradigm? Does native code also execute inside the sandbox?

  • Does this work because both codes (java and C ++) are machine code at the end?

Perhaps this is a stupid question. But I was always interested ...

EDIT: I have 3 good answers. really can't decide what helped me the most. But I will mark this question as an answer to close this topic on my part.

+5
source share
3 answers
  • He does not know until you call the method. The native code is in .DLL or .so; java runtime looks for specific entry points that match the built-in methods that you created (if you use JNI, there is a tool that can analyze methods and create stubs of functions that will lead to these entry points when compiling). If the desired entry point is missing, an exception will be thrown.

  • The code generated by JIT is not completely self-contained; it must from time to time call external native code (both for low-level execution procedures and OS services). The same mechanism is used to call code for your own methods.

  • No. You can do everything you would do in a clean C / C ++ program. The only thing that can prevent it from doing any damage is the external security measures that you have (restrictions on access privileges, other OS protections, security software, etc.). But VM will not protect you.

  • No, JNI existed before the advent of JIT. The mechanism is the same, if the bytecode is started by the interpreter, and you want this interpreter to call its own code, you just need some logic to determine that this method is "external" and should be called as native code. This information is contained in a compiled .class file, and when the interpreter or JIT loads it, it creates a memory view that makes it easy to direct the call when searching for a method.

+2
source

It depends on the platform. On Linux, Solaris, etc. JRE uses dlopen . On Windows, it uses LoadLibraryEx and GetProcAddress . If the JRE runs in interpreted mode, it calls this function; in compiled mode, it compiles Java bytecode into native code that calls this function.

In all the JREs that I'm familiar with, you cannot directly call your own function in a static library; only one in the dynamic library.

Parent code should not be limited to one platform; if it is standard C, you can compile it using a cross-compiler for each platform on which the JRE is available.

+1
source
  • The JVM will check the libraries you defined and see if the method exists

  • Bytecode will be interpreted or JITted and a call to native code will be added. This may include boxing / deboxing values โ€‹โ€‹and other things necessary to convert the data to a suitable format. Libraries have a specific interface, which is explained to the Java compiler, and it will create the required interface logic.

  • Depends on the sandbox. By default, native code is native code. It does not call the Java API, so the JVM cannot manage it at all. But there may be other limitations, for example, the JVM may run native code with libraries that provide an isolated environment, or the operating system may have a sandboxed way.

  • It depends on what you mean. In the end, all the computer does is machine code, but in this case it doesn't really matter. The role of translation and execution is important. This is the glue that makes everything work.

Think of the system as people. Man A speaks only Japanese, but wants to book a hotel in Paris. Registrar B speaks only French. Person A can get an interpreter who will translate his commands into French, reception administrator B and in return translate what B has done to the person that person A understands. This is part of the JNI.

+1
source

Source: https://habr.com/ru/post/1236917/


All Articles