-1

I need to find automatically all instructions in java bytecode that are executed for sure. An analog example in pseudocode:

x=a; //will be executed for sure
y=b; //will be executed for sure
if(x>y) //will be executed for sure
 x++; //might not be executed
else
 y++; //might not be executed
k=4; //will be executed for sure

One possible solution might be to create a control flow graph and find a deterministic path there. (graph theory?) According to the example:

x=a;
y=b;
if(x>y)
 |   \
 |    \
x++;  y++;
 |    /
 |   /
k=4;

Do you have an idea how to find a solution to this task?

  • 3
    This feels a little like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the underlying problem you are attempting to resolve? – Duncan Jones Mar 26 '14 at 16:24
  • You mean unconditional not deterministic. In the absence of threading, execution of bytecodes is already deterministic. – Stephen C Mar 26 '14 at 16:39
  • I don't think it is a XY problem, because it really is a requirement of the task I have to do that the execution is guaranteed. I explained it a bit more detailed in the answer of Antimonys post. – user3057508 Mar 27 '14 at 09:05

1 Answers1

1

Update: Now that the asker's actual problem has been revealed, here's my updated answer.

First off, there is no need to determine instructions that are "always executed". If you look at the paper, it is referring to the trace of instructions when the program is executed with one particular input. To find the instructions, simply execute the program with that input and record which instructions are executed (of course this assumes the program is purely functional, one of many problems in the paper).

The big question though is why you would want to implement this. From my understanding of the paper, this isn't a very useful or interesting obfuscation+watermarking technique. And implementing it in Java instead only makes it even worse.

If you're just implementing it for fun, knock yourself out, but if you simply want a Java watermarking tool, I'd take a look at an existing project like Sandmark.

Original answer

There are no instructions which are guaranteed to be executed, because execution could stop at any point thanks to Thread.stop().

In order to do meaningful analysis, you have to ignore certain types of errors. For example, Krakatau ignores the possibility of Thread.stop as well as most VirtualMachineErrors.

At that point, it's just a matter of following the control flow. I'm not sure what your problem is there. If you explain what you're trying to accomplish and what the project requirements are I might be able to suggest a tool.

Edit: If you are trying to find all instructions which are guaranteed to be executed by the program at runtime, rather than guaranteed by static analysis, that is of course an intractable problem. In fact, it's undecideable. And I'm not sure what the use case of this is anyway.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • Thanks for the answers so far, I should have explained it a bit more detailed in the start post. I'll try now. – user3057508 Mar 27 '14 at 08:37
  • I'll try now: The to be analyzed program gets a certain input, e.g. 1,2 and 3. Now I have to debug the path the program takes (a set of functions F) and save that set. For my task I now have to find instructions that are guaranteed to be executed within that functions. Why I need that? Because I have to manipulate jump instructions, but I have to be sure that they are taken. – user3057508 Mar 27 '14 at 08:45
  • @user - The problem is that it is in general undecideable. If you want to see the techniques used in practice, look into symbolic execution and SMT. It's an active area of research. If you explain why you want to manipulate jump instructions, maybe I could tell you a better way. What is the actual problem you are trying to solve? – Antimony Mar 27 '14 at 14:14
  • Ok, I have to implement a software watermarking technique in java. [Link to the technique.](http://profs.sci.univr.it/~giaco/download/Watermarking-Obfuscation/self-validating%20brench%20watermarking.pdf) (check "Extension to Java Bytecode") And "since the algorithm requires the branches to be on a deterministic path" I need to find instructions that will be executed for sure. There is nothing else I can imagine with "deterministic path" and it also kinda fits the logic behind. – user3057508 Mar 27 '14 at 14:35
  • 1
    @user See you should have started by just saying that. This is why XY questions are bad. – Antimony Mar 27 '14 at 18:21
  • "it is referring to the trace of instructions when the program is executed with one particular input." Yes this is true, but I think you misunderstood the algorithm. Because the chosen jumps will be relocated to a new funtion (fpf) which produces a key k_i and the functions depend on eachother (one argument of the fpf is the key k_i from the previous fpf execution) it is not sufficient just to pick the recorded instructions. If you pick the recorded instructions, it could be possible that with another input you also reach parts of this recorded instructions and then you maybe skip some of the – user3057508 Mar 27 '14 at 19:04
  • jumps in the instructions. Now this is bad because a wrong k_i in the fpf will most likely (k_i is part of the input for a hash map) lead to a wrong jump (it's not really a jump) back to the desired normal execution of the program and this will crash the program. And this is also why you thought it is obfocusation. Sandmark sadly does not provide that watermarking technique. – user3057508 Mar 27 '14 at 19:08
  • @user And you've identified one of the major failings in the paper. It assumes the program executes deterministically. And why would Sandmark implement this particular watermarking technique? It's a really stupid technique. – Antimony Mar 27 '14 at 19:15
  • Hm yeah it really seems so and I think there is even another problem with the dependences of the fpf functions. I'll think about the whole thing again, thanks for the answers so far. – user3057508 Mar 27 '14 at 19:33