Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistent stack height 0 != 1

I'm modifying a Java class bytecode through an hexadecimal editor, and I want to force a method to always return true.

  1. Replaced all its bytecode with nops to keep the size intact (original size is 1890).
  2. Execute a pop to restore the stack height since it receives an argument.
  3. Return true with iconst_1 followed by ireturn.
public static boolean test(java.lang.String);
        descriptor: (Ljava/lang/String;)Z
        flags: ACC_PUBLIC, ACC_STATIC
        Code:
          stack=5, locals=12, args_size=1
             0: nop
             1: nop
             2: nop
             3: nop
             4: nop
             [...]
          1886: nop
          1887: nop
          1888: pop
          1889: iconst_1
          1890: ireturn

But when executing it, I'm getting the following error

java.lang.VerifyError: (class: com/example/test/TestBytecode, method: test signature: (Ljava/lang/String;)Z) Inconsistent stack height 0 != 1

NOTE: with or without pop the result is exactly the same.

like image 712
m0skit0 Avatar asked Aug 18 '15 10:08

m0skit0


1 Answers

The pop is unnecessary, since the arguments are not on the stack at first. They are only pushed onto the stack when using *load instructions as if they were local variables, which can happen at any time.

like image 94
Clashsoft Avatar answered Oct 03 '22 16:10

Clashsoft