0
public class some_class
{
    public static void som_func() {
        var some_object = new SomeObject();
        var ret = 0;
        do
        {
            ret = some_object.DoSomething();
        } while(ret != 1);
        System.out.printf("Return value: %d", ret);
    }
}

Now in C++ if I call like this way:

auto some_cls = g_env->FindClass("some_class");
auto some_func = g_env->GetStaticMethodID(some_cls, "som_func", "()V");
g_env->CallStaticVoidMethod(some_cls, some_func);

Is there any way to access that some_object variable var some_object = new SomeObject(); or that ret variable var ret = 0; through JNI or any JVM memory accessing trick?

I want to do something like:

auto some_object_class = g_env->FindClass("SomeObject");
/* 
and then some way this 'some_object_class' will be a reference to that local variable 'some_object'
*/

I suppose I am trying to know how to access the JVM's heap memory? I heard that JVM allocates an object on the heap and a reference to that allocated object is stored on the stack, if that's the case can I access that stack memory and retrieve the reference? I am confused, I guess I don't know what I am talking about in the end. Please guide me to the right path.

  • I have used JNI a lot and I'm pretty sure it is impossible. unless that variable is a class member. – Afshin Jul 09 '21 at 19:30
  • ah so it's impossible through JNI? what if i try to break the 4th wall or something? like directly accessing the jvm heap memory or something? is something like that possible? – Sanjid Haque Jul 09 '21 at 19:34
  • You are trying to access a function local variable. It may be optimized in several way. even trying to access directly to jvm memory(that I'm not sure if is possible) will be really unreliable. I suggest you find another way to solve your problem. – Afshin Jul 09 '21 at 19:37
  • In JNI you can't, unless you go a really long way and mess with JVM internals, making you dependent on ONE JVM only. You could, however, use Bytecode Weaving or a Preprocessor for the Java Compilation (.java -> .class) step, and change certain aspects, like replace certain variables or log those certain variables to someplace where you can access them later on. The preprocessor is IMO the best option because it's easiest to write and the most compatible method. Check out Project Lombok to see how that works in action, for example with their Extension Methods => 'Aspect Oriented Programming' – JayC667 Jul 09 '21 at 20:38
  • That being said, Java Reflection can access a `Class`, any of the `Class`'s static (class) variables, and any `Object`s' methods and member variables. So if you can restructure that code above, maybe you can take advantage of those, too. – JayC667 Jul 09 '21 at 20:40
  • @JayC667 ah thanks for some optimistic comments, but I was thinking what about the Main function or the entry point. I think most of us wouldn't want to store the initiated local objects as static fields in the Main Class? and am I allowed to create more JVMs per process? ah sorry I guess I am going too much deep without being more familiar with JNI and JVM. – Sanjid Haque Jul 09 '21 at 20:59
  • 2
    You can't do this with pure JNI, but it is possible using [JVM TI](https://docs.oracle.com/en/java/javase/11/docs/specs/jvmti.html#local) – apangin Jul 09 '21 at 21:36
  • @SanjidHaque Well then check out "JVM TI", "Bytecode Weaving", "Aspect Oriented Programming", and check "Project Lombok" (that uses Java Compilation Preprocessor), they all might help you find a clean solution to your problem. – JayC667 Jul 09 '21 at 21:55

1 Answers1

1

Can't be done. Note that you can't access these local variables from a regular Java method either. Part of the problem is that the method has no idea where these local variables are. They might be allocated in a stack frame, or they might be in registers. Even if you implemented some assembly code to access the stack and registers directly, you'll have no idea where to look, and because of the dynamic nature of JVM code compilation, the locations can change.

boneill
  • 1,478
  • 1
  • 11
  • 18
  • these things also applies to the main or the root function too right? the best thing then I can do is may be recreate that function or that function's functionality in C++ which will work with the Java objects. – Sanjid Haque Jul 09 '21 at 19:47