Re: Unexpected (for me) output when injecting new instructions
Remi Forax <[email protected]> Thu, 23 Nov 2017 20:40:36 +0100 (CET)
| Newsgroups | gmane.comp.java.objectweb.asm |
|---|---|
| Message-ID | <[email protected]> |
Hi Jon, ----- Mail original ----- > De: [email protected] > À: "asm" <[email protected]> > Envoyé: Jeudi 23 Novembre 2017 18:00:54 > Objet: [asm] Unexpected (for me) output when injecting new instructions > TLDR I built my own java bytecode modification framework for a project, > however I'm exploring ObjectWeb because it seems like an overall better > solution. > > So I built a simple test, inject some junk, and didn't get what I expected > back. > > I have a simple method to return true > public static boolean isTurkeyDay() { > iconst_1 > ireturn > } > > As a test I want to inject a nop after the ireturn > > public void visitInsn(int opcode) { > switch (opcode) { > case Opcodes.IRETURN: > mv.visitInsn(Opcodes.IRETURN); > mv.visitInsn(Opcodes.NOP); > break; > default: > super.visitInsn(opcode); > } > > I expect: > public static boolean isTurkeyDay() { > iconst_1 > ireturn > nop > } > > However I get > > public static boolean isTurkeyDay() { > iconst_1 > ireturn > athrow > } > > What am I missing here? It's an artifact of the StackFrame calculation algorithm, you ask for it when creating the ClassWriter, when you generate dead code, the code after 'ireturn' is unreachable. Section 3.1.5 of the user guide [1] has a description of what a StackFrame is. Section 3.5 of the developer guide [2] has a good overview of the algorithm used to generate the StackFrame, and section 3.5.4 of the same guide [3] has a discussion of how to generate StackFrame in case of dead code. I hope it helps :) > > Thank you for this > > cheers, Rémi [1] http://download.forge.objectweb.org/asm/asm4-guide.pdf [2] http://asm.ow2.org/doc/developer-guide.html [3] http://asm.ow2.org/doc/developer-guide.html#deadcode > > > -- > You receive this message as a subscriber of the [email protected] mailing list. > To unsubscribe: mailto:[email protected] > For general help: mailto:[email protected]?subject=help > OW2 mailing lists service home page: http://www.ow2.org/wws -- You receive this message as a subscriber of the [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help OW2 mailing lists service home page: http://www.ow2.org/wws