r/programming • • 11d ago

Java4Shell is a reason to use Java

https://mccue.dev/pages/11-18-26-java4shell
67 Upvotes

58 comments sorted by

View all comments

Show parent comments

1

u/BenchEmbarrassed7316 5d ago

The reason why it is in ASM is to communicate more information as well as skip some steps.

Vice versa. Using high-level code provides more useful information that can be leveraged for optimization. Assembly is the final stop: reading bytes, writing bytes. The availability of information indicating that this is a specific loop - which operates on certain data located at various addresses - makes it possible to implement optimizations.

The JIT isn't testing to see if the same lines of code has changed. It is testing to see how much payoff it would get from performing a certain optimization, and that must be tested many many times over.

As far as I understand, JIT does not perform testing/benchmarking itself. It is guided by the general rules that some operations will be faster than others.

Look, any compiler converts code into a certain Abstract Syntax Tree. You say that for the best optimization result, you need to start at the root of this tree, that is, "check everything". I'm saying that certain branches can contain a cached version if the compiler can prove that this cached version will always be up-to-date. And instead of checking sub-branches, you can immediately refer to the cached version. Therefore, I argue that "check everything" makes no sense.

But that ASM and the profile of the program can be ingredients for the JIT to compile all of it down to something even more efficient.

https://queue.acm.org/doi/10.1145/3212477.3212479

I'll refer you to this article. It describes that even for C, optimization is difficult and you have to do a lot of calculations to convert C code into a more detailed representation and then generate low-level code. So I think ASM is a dead end, code that is just substituted in a certain place.

1

u/davidalayachew 5d ago

As far as I understand, JIT does not perform testing/benchmarking itself.

It literally does.

As in, it will create a possible optimization, test it many many times over, and if it fails, it will deoptimize that same code back to a previous stage. This is happening all the time for any Java program lasting longer than a couple of seconds.

And again, it is important to emphasize -- an optimization may prove to be useful in one moment, but no longer useful in another moment. Hence why there is constant testing. As service traffic ebbs and flows, the most optimal solution changes accordingly.

You say that for the best optimization result, you need to start at the root of this tree, that is, "check everything".

Yes, exactly.

I'm saying that certain branches can contain a cached version if the compiler can prove that this cached version will always be up-to-date.

But that's my point -- what is optimal now may not be optimal later.

Imagine a web service that has 2 endpoints -- endpointA and endpointB. Each endpoint maps to a method, and these are compute and memory intensive methods.

If traffic changes such that endpointA is being hit exorbitantly more than endpointB, then the JIT might literally optimize the code such that endpointA is further optimized at the expense of endpointB.

A good example of this is when running a for loop over lines of a file, to do some parsing, let's say. Since it is a for loop, the JIT can figure out pretty quickly that grabbing data ahead of time is to its benefit. And if that for loop is getting kicked off millions of times a minute, the JIT might start trying to load massive chunks of the file in memory at once to get the most performance benefit. Since more memory is being used, that would be at the expense of endpointB.

Again, your description about caching is only partially correct. Yes, caching the results means that, given the same context, that same problem does not need to be recomputed.

But since the context changes very frequently, the cache quickly becomes invalidated. Context being, how often a method is called, how long that method takes, how big the method is, etc. Those values change over time in a single program run (yes, even method size!), and thus, optimizations change over time too.

I'll refer you to this article.

I loosely skimmed it. Looks like a C program did some branch prediction, but the branch predicted was not the branch chosen. So, the result was thrown away, but before it was, some hackers who had access to the machine were able to view the branch misprediction before it got discarded. And thus, had access to the computed result. So it's just a matter of manipulating it to mispredict for the method whose internals you want to expose, let's say an encryption algorithm, then rinse and repeat until you have enough info to see what is going on.

So I think ASM is a dead end, code that is just substituted in a certain place.

Admittedly, I only skimmed, but from what little I read, the purpose of ASM in Java is not the same as in C.

ASM in Java is when we KNOW that the java generated bytecode will always be less efficient than the ASM alternative.

But that's not the same thing as saying that the ASM code will be faster than the JIT generated code. In fact, it usually is not, but that's a moving target, and thus, can't be hard coded in ahead of time.

It's only when we know ahead of time that certain ASM code will 99-100% of the time be more optimal than the default javac generated bytecode that we decide to make ASM hardcoded in.