r/programming • u/bowbahdoe • 7d ago
Java4Shell is a reason to use Java
https://mccue.dev/pages/11-18-26-java4shell92
u/KryptosFR 7d ago
Such a weird flex and weird take. Tries to make C# look bad by linking to something entirely unrelated: inline assemblies which a C/C++ MSVC specific feature and only supported on x86 (which is being phased out in . NET).
The coping is hard on that one.
43
u/Professional_Price89 7d ago
"Faster than C", then show a 71% slower than C table.
13
u/za3faran_tea 6d ago
Some guys on the JDK team benchmarked the caffeine caching library (written in Java) with a similar one Moka (written in Rust), and Java had lower CPU usage and lower latencies at the same throughput
9
u/coderemover 4d ago
Do you have a link? A benchmark without information how to reproduce is just an ad.
1
u/za3faran_tea 4d ago
I don't, but here's the chart: https://imgur.com/a/LUG597k
8
u/coderemover 4d ago
It’s garbage. It doesn’t even say what allocator was used and what size of objects. I did benchmark mimalloc against ZGC using a high residency database-like workload and Rust ran circles around Java in all aspects, including CPU spent on memory management.
-38
-23
u/bowbahdoe 7d ago edited 7d ago
Is it being phased out? I'll admit to being out of the loop somewhat. I know there are memory-unsafe features like https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code
I think the core criticism that "C# dependencies can quietly bring in memory-unsafe code" holds.
Correct me if I'm wrong though.
24
u/KryptosFR 7d ago
First link has nothing to do with .NET.
And yes you can use unsafe code so what? It's a needed feature for interop and it must be enabled with
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>.6
u/bowbahdoe 7d ago
Do you need to enable it for dependencies too? Like if you don't have
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>you know no dependencies are using unsafe code?And yep, my bad. Replacing it with a more appropriate link.
14
u/KryptosFR 7d ago
You need it if you call directly API that are marked
unsafe. Note that the new model makes it more explicit so even with dependencies you will likely need it more often.That doesn't make C# unsafe or dangerous. Rust use a similar contract where unsafe code can be wrapped by safe code and code calling the safe version isn't considered unsafe.
4
u/bowbahdoe 7d ago edited 7d ago
Okay but there are still dependencies where the unsafe-ness is something you don't know about unless you read their code?
Like is this a new thing for "directional" safety? And yeah rust does something similar. But people do end up accepting unsafe if it "feels audited" or "feels neccesary." I didn't touch on Rust because I feel like all these
-5s on my comments would be-50s if those folks get awakened, but it is a similar criticism1
u/simon_o 6d ago
That doesn't make C# unsafe or dangerous.
No, just less safe and more dangerous then Java, which is the point OP wanted to make.
--enable-native-accessworks transitively, and at runtime, which means the person executing the code sees if anything shady might be going on and where, and needs to explicitly acknowledge it.This is way more useful than a compiler option like
AllowUnsafeBlocksthat is only relevant at build time.1
u/robert_7524 1d ago
No: AllowUnsafeBlocks applies per project; it doesn't audit referenced assemblies. A dependency can wrap unsafe internals behind a safe API.
66
u/BenchEmbarrassed7316 7d ago
What a nonsense.
tldr: A vulnerability was found in Java. Java isn't much slower than C++. So, use Java.
80
u/bowbahdoe 7d ago •
![]()
I would say its closer to
tldr:
A vulnerability was found in the C++ code behind Java.
We need to fix that and audit that C++ pretty deeply
But Java code is usually immune to these kinds of problems.
And auditing one C++ codebase is easier than auditing N dependencies
So use Java
30
u/bowbahdoe 7d ago
With the corollary that
Other GC'ed languages are less immune to these kinds of problems because of their "seamless" support for calling C++.
Which is the part that people seem to be taking exception to.
5
u/coderemover 4d ago edited 4d ago
Java is not immune to those problems. It has its own share of problems. And maybe C++ has some problems that Java doesn’t have, but that part is solved by Rust. And Rust is generally safer than Java.
-1
u/Virviil 4d ago edited 4d ago
Its statements contradicts themselves.
If dependencies are NOT allowed not use native code - they are slow and so my Java program is. If dependencies ARE allowed - Java is the same “vulnerable” as other languages.
If dependencies turring-full compiles JIT into native - it’s impossible to audit2
u/bowbahdoe 4d ago
Well take for context that the response above is framed by the way the first person framed it.
If dependencies are NOT allowed to use native code
They are pure Java and are optimized like the rest of your program. Probably worst case it's like 2.5x slower or something. It's not Python where it's 50x (and like I alluded, sometimes it's 1x or 0.9x. really depends on the code)
If dependencies are allowed to use native code
You have to explicitly add a flag to enable it, so at least you are aware that you have a different risk profile than pure Java
You can have that flag be per-library so you know exactly what you need to audit deeply to have confidence you don't have buffer overflows
And in both situations
you need to trust the native code in the VM. But at least that's only one "thing." Let's audit it more, of course. Or find ways to make it safer by construction. But it does make it so most people don't need to go to these depths.
28
u/New-Anybody-6206 7d ago
I ignore posts with a dogmatic title, because it's almost always hilariously misinformed. Tech people are IMO no exception to Dunning-Kruger.
5
u/bowbahdoe 7d ago
I mean I seem to be a somewhat misinformed on C#. No easier way to learn than saying the wrong thing out loud.
But can you point out anything else where I'm hilariously misinformed?
-10
u/BenchEmbarrassed7316 6d ago
combined with JIT compilation can make the actual performance of a Java program competitive with C. Sometimes a Java program can even be faster. A JIT compiler knows what code paths are actually taken and can optimize around them. They can even refocus what is optimized if the workload changes. It is pretty cool!
Well, it’s so impressive that they had to write an assembly snippet by hand (at least, that’s what I gathered from the message). If the JVM is capable of such excellent optimization, why bother writing an assembly snippet?
10
u/davidalayachew 6d ago
Well, it’s so impressive that they had to write an assembly snippet by hand (at least, that’s what I gathered from the message). If the JVM is capable of such excellent optimization, why bother writing an assembly snippet?
Because the small print answer is -- everything you quoted is true, but it takes a surprisingly long time for the JIT to "get it" and spit out that "faster-than-C" answer. Long, as in it could take seconds or minutes even to get to that point.
For long running applications, this is usually fine. For short-running ones, not usually.
In the (rare) situation where the answer is obvious to humans but not the JIT, the OpenJDK team likes to hand-code assembly. That's basically what happened here.
5
u/BenchEmbarrassed7316 6d ago
but it takes a surprisingly long time for the JIT to "get it" and spit out that "faster-than-C" answer
I disagree with that: JIT compilation takes a significant amount of time because specific code must be executed repeatedly to gather statistics, such as which classes are used for which interfaces, what values might be
null, and so on - information that isn't explicitly present in the code itself.For a function that converts
String->byte[], you generally don't need a JIT compiler, as the operation essentially boils down to raw byte manipulation. At most, a JIT compiler might reorder branches withiniforswitchstatements based on which paths are executed more frequently.5
u/davidalayachew 6d ago
I disagree with that: JIT compilation takes a significant amount of time because specific code must be executed repeatedly to gather statistics, such as which classes are used for which interfaces, what values might be null, and so on - information that isn't explicitly present in the code itself.
You disagree with my claim that "that's basically what happened here" or the info I presented?
If you mean my conclusion -- possibly, I wasn't there. Maybe they had different or more pressing reasons to handcode assembly.
But if you mean my argument itself, no, you are oversimplifying.
The reason why JIT is so powerful is because it doesn't just boil down individual methods into something more optimized -- sometimes, it will boil down multiple consecutive methods into optimized instructions.
So, saying it as
String -> byte[]is not the full picture -- we must also look where that method is being used. For example, if we are cycling through aString[]to turn each into some Base64 output, JIT compilation may very well construct an optimized set of logic that convertsString[] -> byte[][]. Not to say that that is what it did here, or that that was what is was aiming for, but just clarifying that the JIT is not nearly as limited as you are making it out to be, nor is its scope of optimization as limited as the boundaries of a single method.The purpose of the JIT is to optimize the whole program. And if it can, it will outright eliminate or optimize every single line of code into what it believes is best, given enough time and proper input. But if proper input isn't available, sometimes the OpenJDK folks like to give their own hand-rolled assembly to "help it along".
3
u/BenchEmbarrassed7316 6d ago
I disagree that a JIT is needed to optimize a simple byte manipulation.
JIT compilation is highly significant when information cannot be derived from the code itself, for instance, in cases of dynamic typing.
One of the key optimizations within the JVM is devirtualization, which paves the way for inlining and other further optimizations. For AOT compilers, the equivalent approach is monomorphization. These techniques are particularly relevant when dealing with long call chains, code distributed across multiple modules, and similar scenarios. In Java, with dynamic class loading, and other features, it is impossible to do complete monomorphism. This is where JIT comes in.
Regarding low-level byte manipulation, a JIT compiler can theoretically provide insights into which branches within
iforswitchstatements are executed most frequently, with minimal impact on overall performance. However, for most cases, JIT is not needed to perform this optimization.My guess is that the C compiler spends a lot of time making sure that the loop meets many conditions, and only a fraction of the time generating SIMD code. If the JVM could generate code as efficiently as C (or another compiled language), it would be much more logical not to write assembly instructions, but to provide an intermediate representation of the code in which all possible hints have already been calculated, because the JIT also spends a lot of time on data and code checking, the code generation itself is probably cheaper.
4
u/bowbahdoe 6d ago
Well one thing it can do, even if there is no runtime info needed to do the compilation, is use instruction specific to your exact CPU make and model. Again, relevant to the security issue too.
4
u/davidalayachew 6d ago
I disagree that a JIT is needed to optimize a simple byte manipulation.
But that's my point -- you are thinking way too small scale.
It's not just the method, but the code calling it -- application code.
If you have one method doing byte manipulation amongst many other operations doing more byte manipulation, the JIT can try and find a specialized operation that would do the both in combination.
That is what JIT Compilation is good at -- taking 2 almost unrelated pieces of code that, when called in-tandem many times in a row, optimizes them into something even faster.
The fact that one of the methods in question is a simple byte manipulation method is irrelevant to whether or not savings could be found.
0
u/BenchEmbarrassed7316 5d ago
That is what JIT Compilation is good at -- taking 2 almost unrelated pieces of code that, when called in-tandem many times in a row, optimizes them into something even faster.
Any optimizing compiler works this way, whether it's JIT or AOT. The difference is that in some languages, you have to gather statistics and make assumptions to perform devirtualization, while in other languages, it's known right away.
Here is a simple example:
result = (a + b) / a.foo();There are many optimizations that can be done here. If the compiler knows which
foowill be called, it can check whether it can return 0, 1, or any other number. Also, if the compiler knows thataandbare constants or their values are known in advance, it can perform more optimizations (for example, if this calculation is guaranteed to be zero, there is no point in performing thedivoperation).Now, if you are using a JIT compiler, it would be wasteful to check the left-hand side of the expression
(a + b)at every optimization and deoptimization. It is much simpler to have some intermediate representation of these instructions, which could be created either in AOT mode when compiling the sources into bytecode, or at program startup, or on first access.I believe that byte manipulation when decoding base64 is a fairly simple operation, which is confirmed by the fact that they were able to describe it in assembly language at all.
So the fact that they used ASM instead of some intermediate representation makes me doubt the effectiveness of this intermediate representation, to the extent that it allows generating high-quality code that would be "almost like C".
3
u/davidalayachew 4d ago
Now, if you are using a JIT compiler, it would be wasteful to check the left-hand side of the expression (a + b) at every optimization and deoptimization.
Again, you are thinking way too small scale.
The reason why it makes sense to check that on a regular basis (maybe not everytime) is that programs change over time, and the needs on start or first access are not necessarily the same for peak times.
Again, I encourage you to think much larger scale. You're looking at this as an isolated problem when it is actually a tiny piece of a much larger program.
Yes, optimizing individual components tends to result in optimizing the larger program, and that's not a wrong approach to take.
But you are wrong to claim things like it is wasteful to repeatedly check when the JIT has been demonstrating optimizations for use cases like this for nearly a decade. Just because it is wasteful in certain contexts, doesn't mean that applies for this one.
→ More replies (0)1
u/bowbahdoe 6d ago
Good question! I do wonder how much was gained here over what the JIT spits out normally.
My bet is "not enough to justify it," but who knows.
3
u/sintrastes 6d ago
Java4Shell is a reason to use Lean 4, Rocq, and/or Rust.
7
u/bowbahdoe 6d ago
That is a valid position to take, so long as you accept that all of those options have a cost in program maintenance.
Expressing everything via a proof or with an involved type system is overhead when you are reading code after the fact. You can argue that this is offset by any number of things, but that is how it is.
There's also something to say about the fact that we already have an educational industrial complex that pumps out Java developers of at least some level of training. We don't have that for lean.
If you wanted to investigate replacing parts of the jvm that are currently C++ with lean proved components, that would have high value.
2
u/coderemover 4d ago
In my experience Rust is much lower cost to maintain than Java. Rust is harder to initially write, but overall it forces developers to use simpler designs - especially the aspect of sharing data. Rust programs have usually simpler data flow and are easier to reason about.
2
5
1
u/zerosign0 4d ago
Its miss leading title this is their internal lib problem, so the most closer conclusion would be promote new valhalla and new ffm api on some of the java lib which is something that are being progressing in background as we talks right now. However, overheads might varies across jvm impl.
68
u/gredr 6d ago edited 6d ago
That's a lot of words to say
--enable-native-accessis a good security feature. OP isn't really wrong. Without it, you're vulnerable to MS07-052: Code execution leads to code execution.Interesting that the JVM doesn't frame
--enable-native-accessas a security feature at all; it's an integrity feature. If you're running a malicious (or vulnerable) dependency, blocking native access isn't going to save you. See also: Java4Shell's namesake, Log4Shell.