Re: Unexpected (for me) output when injecting new instructions

Jon Sawyer <[email protected]> Thu, 23 Nov 2017 11:52:55 -0800
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <CABrNosH=_QvS7qXq6qM+Ssm5eHzYjk4AYiqTOEyAX-saRegzHA@mail.gmail.com>
Remi,

Thank you. Yes we found the code responsible for it, and a way around it.
Found that it also happens if I inject the deadcode anywhere else and use a
goto to jump over. A friend provided the suggestion of using if (false) to
jump around it, and it seems to have worked.

I will spend some more time on the guide, thank you.

Ironically, stackframe and calculating tge max_stack is why im trying to
move to ASM over my own framework. My implementation is buggy.

On Thu, Nov 23, 2017 at 11:40 AM, Remi Forax <[email protected]> wrote:

> 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