0

I am currently making a java application that runs on an embedded devie (a POS terminal). This device has a manufacter specific virtual machine.

The functions that interact with the hardware are defined in C files inside this virtual machine's source (in the profile AFAIK).

I cannot call these functions from my java code because, according to the official documentation, the virtual machine doesn't support neither JNI nor KNI, it only supports "stack manipulation" as a method of interfacing C and java code.

As far as I could see, the arguments of the native functions are passed using stacks. Pushing the arguments before calling the function and poping them inside the functions (the prototypes have void arguments). I guess something like that should be done to call the functions, maybe something related to the call stack?

The real question is, How do I interface C and java using "stack manipulation"?

UPDATE: Example of function to be called (C code) It prints a message on the device's screen.

void PrintAt(void)
{
    Array msg = popStackAsType(Array);
    int y = popStack();
    int x = popStack();
    NativePrint(x,y,msg->bdata);
}
5
  • Does the manufacter provide samples? I'd be interested in seeing one. Who is the manufacter and what is the model of the device.
    – Java42
    Mar 23, 2012 at 17:22
  • I tried finding one, but sadly there is none included
    – user363834
    Mar 23, 2012 at 17:24
  • Can you give an example of one of the native function prototypes? And any description that comes with it? Mar 23, 2012 at 17:59
  • I will ad it to the main question as it can't be formatted here. I had to change the names of the functions, but the code inside the C files is something like that. I have to call that PrintAt function from my java app.
    – user363834
    Mar 23, 2012 at 18:02
  • For this to be possible at all the JVM must provide some way of looking up the position of "PrintAt" and making the program counter point there. The manufacturer doesn't supply any class to call PrintAt? Is there any vendor specific class to lookup and call PrintAt? Do you have access to the JVM source code or full documentation?
    – Joni
    Mar 23, 2012 at 19:23

2 Answers 2

3

You can't realistically expect to do this since you don't have control over the stack. The JVM controls the stack and any so-called stack manipulation is just a vile hack. Without JNI or something similar your best solution is probably to execute the C code in a separate process and use some form of IPC to communicate with it.

2
  • Thanks for answering, the documentation explicitely says "stack manipulation", if the java stack can't be touched, then could it be a C stack?, also the virtual machine it is using is neither jvm nor kvm, is a manufacter one, which might have different rules about stack modifications. The IPC method might be a good alternative solution, but I will leave it as a last resource.
    – user363834
    Mar 23, 2012 at 17:44
  • If you want to pursue your current approach you will need help from the vendor because you need a deep understanding of the VM. I would go for IPC right now. It will be far and away the quickest and easiest solution. Mar 23, 2012 at 17:46
1

Since you said "the prototypes have void arguments", I'm assuming that you have Java native methods you can call, but they use the top elements of the stack as arguments without actually consuming them. Something like this:

class Native {
   native static void doSomething() {
      // do something with stack[sp] as an int and stack[sp - 1] as
      // an object reference, but do not modify sp.
   }
}

If you wrote raw bytecode, you could just push the arguments, then do invokestatic, then pop. If you want a way to do that that looks like Java, I think this ought to work:

class Dummy {
   static void doSomething(int a, Object b) {
      Native.doSomething();
   }
}

Then a and b would already be on the stack from the call to Dummy.doSomething(), and could be inspected by the native method.

Edit:

Do you really mean that the arguments are being popped inside the native functions? In that case it shouldn't be possible to call them from any Java or bytecode code, because the bytecode verifier will choke: it checks that every execution path through a function leaves the stack the same size as it started, and it can't know about the hidden pops in the native code that modify the stack directly.

2
  • You are right about the function doing its work with the elements of the top of the stack, but those functions I'm trying to call are C code inside virtual machine files. Inside those functions popstack is used to retrieve the different arguments.
    – user363834
    Mar 23, 2012 at 17:59
  • I added An example function to the main post, I am not sure if that does modify the stack.
    – user363834
    Mar 23, 2012 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.