r/programming • • Jun 05 '18

Code golfing challenge leads to discovery of string concatenation bug in JDK 9+ compiler

https://stackoverflow.com/questions/50683786/why-does-arrayin-i-give-different-results-in-java-8-and-java-10
2.2k Upvotes

355 comments sorted by

View all comments

Show parent comment

1

u/yawkat Jun 05 '18

Do you have any idea on how to analyse it? Directly, without translating into something else. I don't.

Using asm. Unless you count parsing as "translating into something else", which would match basically any IR

1

u/[deleted] Jun 05 '18

How do you analyse JVM bytecode without rewriting it into more suitable IRs? ASM does quite a lot of rewriting. Remember, javac is supposed to be fast.

1

u/yawkat Jun 05 '18

ASM does not do major rewriting beyond parsing stuff like resolving constant pool entries.

1

u/[deleted] Jun 05 '18

Actually, you're right, this ASM thing does not really do any useful analysis, the only thing it maintains on top is a CFG.

So, again, how will you analyse JVM bytecode? Let's make it more specific and relevant to StringBuilder detection: how will you identify loop induction variables? Likewise, how to identify loop invariants.

Remember, you must keep a stack VM representation.

1

u/yawkat Jun 05 '18

Yes, doing control flow analysis directly on java bytecode is not a great idea. But this was never the goal of java bytecode. The goal is doing the basic parsing and resolving and then storing a flat representation of the ast graph for further processing by the jit, or for immediate execution by the interpreter (and it really is that!).

1

u/[deleted] Jun 05 '18

And this is exactly why this is an amateurish approach. Before the potentially immediately executable bytecode and after your AST you need few more IRs, to do a more safe syntax sugar expansion, to do more semantic analysis, to do some high level optimisations (constant folding included). Going to a bytecode straight from an AST is dumb.

0

u/yawkat Jun 05 '18

Why? Optimization is explicitly not a goal of javac. You don't need five IRs just to transform java source code to bytecode, the few "optimizations" javac does are perfectly doable on the regular AST.

In fact, the other big java compiler eclipsec doesn't really have any IRs before emitting bytecode from the AST either.

0

u/[deleted] Jun 05 '18

You don't need five IRs just to transform java source code to bytecode

You do need many IRs in order to lower one language to another in a sequence or simple rewrites that are easy to reason about.

Otherwise your compiler is much more complex and error prone than it should have been.

1

u/yawkat Jun 05 '18

Have you actually looked at the two compilers? They're pretty large because the language is complex, but having worked with jdt a lot, I don't feel like an additional IR would help in any way. The final compilation to bytecode is actually fairly straight-forward - the really difficult part is doing the resolving and binding, and there are very good reasons to be doing those on an ast (because they're literally defined by rules on the ast).

1

u/[deleted] Jun 05 '18

Yes, of course I've seen the code, javac is a horrible, amateurish, overengineered compiler, doing pretty much everything the worst possible way.

I recommend that you read about Nanopass approach as a far better way of doing things.

→ More replies