ASM (from ObjectWeb) does not calculate MaxStack correctly, even if ClassWriter (COMPUTE_MAX + COMPUTE_STACK) is set

I get the expected ClassVerifyErrors when I try to load the class i generated using ASM. Upon further inspection, I see that jvm is correct and that the method indicates an invalid MAX_STACK value. It is strange that I use automatic stack counting and maximum local parameters, so this should not be a problem ... The method with an invalid parameter is very simple, and the result is bad bytecode.

I wrote a class with the intended method and compared my generated asm class with what javac was creating and bytecodes matching with the only error being the maximum stack, 0, which is wrong, while javac sets the value to 2.

I like it when I don't need to calculate max stack / locales.

+1
source share
1 answer

The maximum calculation of the stack and variable may lead to incorrect results if the bytecode is invalid. You can verify by executing the generated code through CheckClassAdapter .

For instance,

  ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
  // generate code into cw instance...

  PrintWriter pw = new PrintWriter(System.out);
  CheckClassAdapter.verify(new ClassReader(cw.toByteArray()), true, pw);
+6
source

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


All Articles