News for hackers. Stuff that matters.
Re:macOS Sonoma 14.4 might break Java on your machine by karolist on Monday March 18, 2024 @03:52AM (#39741342)
> As a normal part of the just-in-time compile and execute cycle, processes running on macOS may access memory in protected memory regions. Prior to the macOS 14.4 update, in certain circumstances, the macOS kernel would respond to these protected memory accesses by sending a signal, SIGBUS or SIGSEGV, to the process.

> With macOS 14.4, when a thread is operating in the write mode, if a memory access to a protected memory region is attempted, macOS will send the signal SIGKILL instead.

What is bizarre to me is that Oracle relied on receiving SIGSEGV as normal mode of operation. That should have been a hint where things are going, no?

Re:macOS Sonoma 14.4 might break Java on your machine by vardump on Monday March 18, 2024 @04:03AM (#39741400)
That's actually a pretty normal way to do things. Optimization for JITting, let the CPU hardware to do the heavy lifting instead of putting conditional jumps everywhere.

It's useful for other things as well. I've used SIGSEGV to emulate hardware interrupts. Normal execution wouldn't trap and there's no need for tests + branches (= normally no slowdown), but when an interrupt occurs a specific often accessed page is marked unreadable.

Re:macOS Sonoma 14.4 might break Java on your machine by mort96 on Monday March 18, 2024 @04:47AM (#39741607)
Which other JITs behave like this? AFAIK neither V8 nor Spidermonkey nor LuaJIT rely on segfaults as a normal part of their operation?
Re:macOS Sonoma 14.4 might break Java on your machine by silon42 on Monday March 18, 2024 @05:35AM (#39741889)
Personally I'd expect this would affect the GC more than the JIT. But I'm not surprised that the JVM uses every trick for speed.
Re:macOS Sonoma 14.4 might break Java on your machine by quotemstr on Monday March 18, 2024 @06:22AM (#39742141)
Android ART uses this mode of operation too. There's absolutely nothing wrong with relying on SIGSEGV and other synchronous signals in this manner and POSIX should make it easier and safer instead of trying to pretend signals are bad and useless.
Re:macOS Sonoma 14.4 might break Java on your machine by fransje26 on Monday March 18, 2024 @05:30AM (#39741865)
That you can doesn't necessarily mean that you should.

https://xkcd.com/1172/

Re:macOS Sonoma 14.4 might break Java on your machine by Vogtinator on Monday March 18, 2024 @04:03AM (#39741390)
It's documented and part of the interface for POSIX:

> Write attempts to memory that was mapped without write access, or any access to > memory mapped PROT_NONE, shall result in a SIGSEGV signal. > > References to unmapped addresses shall result in a SIGSEGV signal.

How a SIGSEGV can be handled by the program to continue execution normally need some OS specific code. For Linux there's also userfaultfd to suit this need better.

Re:macOS Sonoma 14.4 might break Java on your machine by wahern on Monday March 18, 2024 @05:33AM (#39741876)
> How a SIGSEGV can be handled by the program to continue execution normally need some OS specific code

A JVM's use of SIGSEGV might include platform-dependent details for recovery. But for simple application usages (e.g. eliding inlined bounds checks in a performance critical loop operating on an array) longjmp can suffice for recovery. POSIX very carefully defines async-safety and longjmp to permit jumping out of a signal handler and resuming normal execution, provided certain constraints are met, such as that the signal did not interrupt a non-async-signal-safe function.

Re:macOS Sonoma 14.4 might break Java on your machine by zozbot234 on Monday March 18, 2024 @06:55AM (#39742369)
> ...such as that the signal did not interrupt a non-async-signal-safe function.

So you have to disable signals prior to doing anything "non-async-signal-safe" and re-enable them thereafter? That's a pretty big "but"...

Re:macOS Sonoma 14.4 might break Java on your machine by cataphract on Monday March 18, 2024 @04:02AM (#39741387)
> What is bizarre to me is that Oracle relied on receiving SIGSEGV as normal mode of operation. That should have been a hint where things are going, no?

Not bizarre at all, this how the runtime has always operated, as anyone one who's ever attached a debugger to a Java process knows. The SIGSEGV handler is also responsible to handling NullPointerExceptions IIRC.

Re:macOS Sonoma 14.4 might break Java on your machine by fniephaus on Monday March 18, 2024 @05:19AM (#39741799)
Correct:

> ... the JVM can intercept the resulting SIGSEGV ("Signal: Segmentation Fault"), look at the return address for that signal, and figure out where that access was made in the generated code. Once it figures that bit out, it can then know where to dispatch the control to handle this case — in most cases, throwing NullPointerException or branching somewhere.

https://shipilev.net/jvm/anatomy-quarks/25-implicit-null-che...

Re:macOS Sonoma 14.4 might break Java on your machine by arghwhat on Monday March 18, 2024 @04:16AM (#39741458)
Despite what the other commenters are saying, it is bizarre.

1. There is very little you can safely do in a signal handler. For a threaded application, that pretty much boils entirely down to setting a bit and leaving it at that. If they did anything more, the behavior is undefined.

2. The memory state that a program receiving a SIGSEGV in is often undefined/garbage, and attempting to execute further at this point is at best unsafe, at worst trampling on state further, continuing execution in a broken state and destroying all evidence that would be useful for debugging - whereas a coredump preserves the state at the time the issue occurs.

There are cases where you need to catch SIGBUS, such as if an anonymous file has been truncated after you mmap'ed it.

Re:macOS Sonoma 14.4 might break Java on your machine by fweimer on Monday March 18, 2024 @04:28AM (#39741517)
The signal comes from a safe fetch, which is just a read that allows ignoring the fault as if it never happened. Such a signal is deliver synchronously, so the usual restrictions for asynchronous signal handlers do not apply.

The code in question takes into account that the value read might be garbage. See the big comment here: https://github.com/openjdk/jdk/commit/29397d29baac3b29083b1b...

On current CPUs and operating systems, this is not an optimization, so the code was removed earlier this year: https://bugs.openjdk.org/browse/JDK-8320317

Re:macOS Sonoma 14.4 might break Java on your machine by arghwhat on Monday March 18, 2024 @04:54AM (#39741654)
The "safe fetch" code relies on a signal handler (either here https://github.com/openjdk/jdk/blob/48717d63cc58f693f0917e61... or here https://github.com/openjdk/jdk/blob/3c70f26b2f3fa9bc143e2506...), which is considered asynchronous delivery (i.e., delivered mid-execution, see `man 7 signal`) - which is why the `async-signal-safe` manpage simply states that it is functions that can safely be called within a signal handler.

This is opposed to calling `sigwait` or similar to actively suspend and wait for a signal, which is not possible to do here.

Granted, it may be that the stars align and their implementation works in practice, but that does not make it any less bizarre.

Re:macOS Sonoma 14.4 might break Java on your machine by quotemstr on Monday March 18, 2024 @06:24AM (#39742155)
It's not "bizarre" at all. It's a direct translation of hardware CPU traps to userspace API. That's what signal handers are: virtualized interrupts! There's nothing wrong with using signals to achieve performance levels otherwise not possible.
Re:macOS Sonoma 14.4 might break Java on your machine by zozbot234 on Monday March 18, 2024 @06:32AM (#39742210)
But this also means that signal handlers are running in what's effectively a separate thread of execution - even in otherwise single-threaded code! So the things you're allowed to do safely in your signal handler are very limited, they boil down to atomically tweaking some lightweight data structure (or even just setting a flag) that the main code will look at later and behave accordingly.
Re:macOS Sonoma 14.4 might break Java on your machine by quotemstr on Monday March 18, 2024 @06:57AM (#39742397)
While it's true that async-signal-safe programming bears some similarities to multi-threaded programming, an interrupt really isn't the same as a thread. Also, there's a lot more you can do in a signal handler than just set a flag: you can longjmp or even make calls to regular functions.

For example, one kind of program organization that used to be more common in Unix but that remains legal involves keeping signals masked all the time except around certain blocking system calls, e.g. ppoll, that atomically unblock signals and wait. In such a program, a signal can arrive only inside the blocking system call and so the handler can call regular functions without the usual strictures of asynchronous signal safety. (Consider responding to SIGWINCH, which tells you about changing terminal size.)

Synchronous signals are also special in that they're, well, synchronous. That means that in the signal handler you can examine the target memory address or instruction pointer and take action specific to a given spot in your program --- e.g. longjmp to an error handler.

All of this is useful, safe, and legal under POSIX. The main problems with POSIX signals are that, 1) as this thread underscores, most people don't understand them, and 2) signal handlers are process global and hard to share. (Consider if you're running two VMs in a process and each wants to use a SIGSEGV GC safe point trick.)

(userfaultfd, sadly, requires more system calls than a synchronous signal handler to handle anomalous memory access.)

We should be enhancing POSIX signals to make them easier to share, not casting aspersions on them.

Re:macOS Sonoma 14.4 might break Java on your machine by zozbot234 on Monday March 18, 2024 @07:05AM (#39742452)
How is this pattern any different from "keep interrupts disabled all the time except when the program is ready to yield()"? I'm not really seeing the inherent difference w/ the well-known pitfalls of multi-threaded programming. The defining characteristics of signals is that, like interrupts and multi-threaded execution, they can pre-empt your program at any time. (Synchronous signals as you describe them are indeed different, they look more like a "recoverable exceptions" system.)
Re:macOS Sonoma 14.4 might break Java on your machine by quotemstr on Monday March 18, 2024 @07:07AM (#39742468)
> keep interrupts disabled all the time except

It's not. So what?

> The defining characteristics of signals is that, like interrupts and multi-threaded execution

No, they can't. You control when they are masked and unmasked. Certain signals are delivered in response only to certain actions (e.g. floating point errors when enabled, or memory access failures). It's not the chaos you think.

Re:macOS Sonoma 14.4 might break Java on your machine by arghwhat on Monday March 18, 2024 @09:53AM (#39744533)
> Also, there's a lot more you can do in a signal handler than just set a flag: you can longjmp or even make calls to regular functions.

... As long as the called functions are fully async-signal-safe/reentrant. It used to be even more sensitive, in that not all register state was correctly saved/restored on Linux.

(On the fabled plan9, where signals are replaced with arbitrary-text "notes", the issue is even bigger as floating point registers are not saved/restored)

> We should be enhancing POSIX signals to make them easier to share, not casting aspersions on them.

That's fair, but they have already been fixed with signalfd, which more modern processes use to deal with the few POSIX-isms that require it, but many of the original uses have been superseded.

E.g., few modern applications use `timer_create` and SIGALRM, as they can instead use timerfd or poll with strategic timeout (although usually abstracted away by their eventloop). SIGBUS is a POSIX-way to deal with mmap truncation, but sealed memfds can be used to avoid the issue altogether. SIGUSR1/2 can be replaced by any IPC mechanism to give much more flexible controls than a single "reload"/"toggle" signal.

(The POSIX ways can still be useful in certain simpler programs of course.)

Re:macOS Sonoma 14.4 might break Java on your machine by quotemstr on Monday March 18, 2024 @11:45AM (#39746081)
> As long as the called functions are fully async-signal-safe/reentrant.

That is not accurate. If I have a single-threaded program sitting around a ppoll(2) loop and a signal can arrive only inside my main loop ppoll(), then I know a priori that my signal handler can't be interrupting non-reentrant code and so I can call whatever I want inside it.

> It used to be even more sensitive, in that not all register state was correctly saved/restored on Linux.

Don't confuse architectural flaws with implementation bugs. I'm reminded of an argument on emacs-devel in which someone argued that we couldn't call malloc(3) in a multi-threaded program because one beta version of glibc once had a thread safety bug in the malloc implementation.

> SIGBUS is a POSIX-way to deal with mmap truncation, but sealed memfds can be used to avoid the issue altogether.

Sealing a file descriptor doesn't physically seal a USB key into a USB port. :-) You also get SIGBUS on IO failures on mmaped files, and surprise removal is an IO failure. There's really no alternative to a synchronous signal here --- a regular file being mmap()ed isn't a userfaultfd.

IMHO, I think it's crying shame that Hotspot (last time I checked --- maybe it's fixed?) doesn't watch for SIGBUS on access to MappedByteBuffer and translate surprise USB device file removal into a nice clear VM-level exception.

Even for cases for which userfaultfd can work, a synchronous signal can be more efficient because it involves just one entry into the kernel. (sigreturn is optional.) I'd really hate to give up the conventional signal mechanism entirely, although of course I approve of things like signalfd and userfault that reduce the need for signal handling.

Re:macOS Sonoma 14.4 might break Java on your machine by arghwhat on Monday March 18, 2024 @08:34PM (#39751739)
> That is not accurate. If I have a single-threaded program sitting around a ppoll(2) loop and a signal can arrive only inside my main loop ppoll(), then I know a priori that my signal handler can't be interrupting non-reentrant code and so I can call whatever I want inside it.

If you have a single-threaded program sitting in ppoll, you cannot ever receive a SIGSEGV during said ppoll unless you passed it bugus fds, timeout or sigmask pointers. A sleeping process cannot segfault.

If you register a SIGSEGV handler, you have zero guarantees that it will only fire at a particular time in your code as it is delivered on any pagefault from any code accidentally generating it. This is why the async-signal-safe rules apply.

If you try handling such fault, what needs to be reentrant is the full call stack that lead to the fault, and every call made from the handler. If, for example, the fault is generated from an event loop handler (in case of a single-threaded example, most things run off event loop handlers), the signal handler must in turn not touch the event loop (no adding/removing/adjusting events, no dispatch) unless the event loop is fully reentrant.

Re:macOS Sonoma 14.4 might break Java on your machine by quotemstr on Tuesday March 19, 2024 @12:19AM (#39752914)
> you cannot ever receive a SIGSEGV during said ppoll

Unless someone sends one with kill(2). Also, I was talking about signals in general, not SIGSEGV in particular. Who uses SIGSEGV as an async work dispatch mechanism?

> unless you passed it bugus fds, timeout or sigmask pointers. A sleeping process cannot segfault.

No, you get EFAULT. System calls don't work that way.

> If you register a SIGSEGV handler, you have zero guarantees that it will only fire at a particular time in your code as it is delivered on any pagefault from any code accidentally generating it

That's why you use sigaction(2) to register signal handlers --- your callback gets both a siginfo_t and a ucontext_t you can use to figure out whether your segfault came from a region of code you know about or some other random thing going wrong in your process. In principle, you can do the non-reentrant thing after having checked that the signal came from the context you expect, and you can do this checking in an async-signal-safe manner.

> If, for example, the fault is generated from an event loop handler (in case of a single-threaded example, most things run off event loop handlers), the signal handler must in turn not touch the event loop (no adding/removing/adjusting events, no dispatch) unless the event loop is fully reentrant.

Of course. That's a matter of program design.

Re:macOS Sonoma 14.4 might break Java on your machine by arghwhat on Wednesday March 20, 2024 @06:42AM (#39764779)
> No, you get EFAULT. System calls don't work that way.

`ppoll(2)` is a C library function you call. On glibc, depending on which implementation you hit on your architecture, the `ppoll` call will segfault before the syscall if you call it with bogus timeout or sigmask pointers, as glibc is dereferencing them.

> That's why you use sigaction(2) to register signal handlers --- your callback gets both a siginfo_t and a ucontext_t you can use to figure out whether your segfault came from a region of code you know about or some other random thing going wrong in your process.

It is true that you could try to figure out whether the faulting address is within a range you thought belonged to a particular thing, but I don't see any reason or benefit. However, this is fragile: Distinguishing between a page fault from a corrupt program state and a page fault in a valid program depends on the program state (specifically, inspecting memory that is likely foo), which in turn makes the output undefined. You won't get false negatives, but will get false positives. You could also try to establish a list of all program counter values (PIE/PIC caveats), and check if the signal originated from near those, but... Ugh.

Now, any other signal I could level with you, but not SIGSEGV. Not until I see some code where it's truly justified: a real, tangible benefit that can only be obtained in this fashion, justifying all the gymnastics and (in my opinion) nastiness of trying to handle a signal that should never be handled in the first place (not even for crash dumps, they're always worse than a proper core).

Re:macOS Sonoma 14.4 might break Java on your machine by quotemstr on Wednesday March 20, 2024 @07:50PM (#39773414)
> `ppoll(2)` is a C library function you call.

ppoll is a real system call on all Linux systems and has been for over a decade.

> It is true that you could try to figure out whether the faulting address is within a range you thought belonged to a particular thing,

Which programs do, reliably, all the time.

> You won't get false negatives, but will get false positives.

No, you won't. Once a program wanders into la-la land of memory corruption, all bets are off anyway.

Re:macOS Sonoma 14.4 might break Java on your machine by arghwhat on Monday March 18, 2024 @09:30AM (#39744215)
This did not improve performance, it was just an unnecessary hack. The authors agree, as they removed it realizing it was not an optimization.

UNIX signals are in no way or form direct translations of hardware CPU traps. The kernel handles hardware traps, which may or may not lead to UNIX signals. Heck, with userfaultfd, a different userspace process could be handling, or injecting, the fault! Not to mention VMs, where the the guest userspace is very far away from any real hardware traps.

There are basically two classes of UNIX signals: Signals that indicate that you might need to take some action (SIGTERM, SIGALRM, SIGUSR1, ...), and signals that indicate that your process did something illegal (SIGILL, SIGSEGV, SIGFP, ...). There is a very, very limited number of cases where handling these errors make sense, and trying to be "clever" to make (faulty and ill-advised) performance optimizations is not one of them.

Re:macOS Sonoma 14.4 might break Java on your machine by quotemstr on Monday March 18, 2024 @11:53AM (#39746175)
Please, knock it off with the value judgements. "Ill-advised" according to whom? You? Why should your opinion prevail?

> This did not improve performance, it was just an unnecessary hack

Well, the Android VM certainly uses a signal mechanism for safepoints and stack overflow checking, for a reason (https://android.googlesource.com/platform/art/+/master/runti...) (both latency and code size), so don't sit there and tell me that the VM running the world's most popular OS is pessimizing itself pointlessly.

> The kernel handles hardware traps, which may or may not lead to UNIX signals.

Not all traps result in signals, and not all signals are traps. Nevertheless, the POSIX signals API is the means through which Unix OSes provide user programs the ability to interact with CPU traps. (Windows does the same thing, morally, with SEH and vectored exception handling --- https://learn.microsoft.com/en-us/windows/win32/debug/vector...). Any decent OS should provide applications with the tools they need to make full use of the underlying hardware. All these anti-signals people are just arguing that programs be bigger and slower, because they can't make full use of all the hardware features of the system, out of, ultimately, an aesthetic objection to signal handling.

Re:macOS Sonoma 14.4 might break Java on your machine by arghwhat on Monday March 18, 2024 @08:15PM (#39751611)
> Please, knock it off with the value judgements. "Ill-advised" according to whom? You? Why should your opinion prevail?

Ill-advised as per the authors decision to remove said hack as it brought none of the intended benefits. Or are you suggesting that your opinion is more valuable than the authors whose code we're discussing?

Be careful with fallacies suggesting only one side of an argument is based on opinions. :)

Re:macOS Sonoma 14.4 might break Java on your machine by quotemstr on Tuesday March 19, 2024 @12:32AM (#39752966)
The actual text from https://bugs.java.com/bugdatabase/view_bug?bug_id=8327860 says this:

> We have been working on a patch that switches the jit protection mode to EXEC around these potential faulting memory accesses.

Yes, they're changing one aspect of signal handler use to work around this problem. They're not stopping the use of signal handlers in general. Hotspot continues to use signals for efficiency in general. See https://github.com/openjdk/jdk/blob/9059727df135dc90311bd476...

> Be careful with fallacies suggesting only one side of an argument is based on opinions. :)

The wonderful thing about choosing not to care about facts is having whatever opinions you want.

Re:macOS Sonoma 14.4 might break Java on your machine by arghwhat on Wednesday March 20, 2024 @07:06AM (#39764908)
> Yes, they're changing one aspect of signal handler use to work around this problem. They're not stopping the use of signal handlers in general. Hotspot continues to use signals for efficiency in general. See https://github.com/openjdk/jdk/blob/9059727df135dc90311bd476...

This whole thread is about SIGSEGV, and specifically their SIGSEGV handling. However, catching normal signals is not about efficiency.

Some of their exception handling is still odd: There is no reason for a program that receives SIGILL to ever attempt continuing. But others is fine, like catching SIGFPE to just forward an exception to the calling code.

(Sure, you could construct an argument to say that this is for efficiency if you considered the alternative to be implementing floating point in software so that all exceptions exist in user-space, but hardware floating point is the norm and such alternative would be wholly unreasonable.)

> The wonderful thing about choosing not to care about facts is having whatever opinions you want.

I appreciate the irony of you making such statement, proudly thinking that your opinion equals fact, and therefore any other opinion is not.

This discussion is nothing but subjective opinion vs. subjective opinion. Facts are (hopefully, as I can only speak for myself) inputs to both our opinions, but no opinion about "good" or "bad", "nasty" or not can ever be objective. Objective code quality does not exist.

Re:macOS Sonoma 14.4 might break Java on your machine by PhilipRoman on Monday March 18, 2024 @06:37AM (#39742258)
>There is very little you can safely do in a signal handler

You can actually do pretty much anything you want, it's just the C library that uses a lot of global state and internal memory allocations, which messes things up. The core syscall API and any reentrant code you write yourself are not affected.

>The memory state that a program receiving a SIGSEGV in is often undefined/garbage

That may be true for arbitrary segfaults caused by bugs, but the JIT has 100% control over what instructions to emit, it is not restricted by ABIs or platform-specific issues, so there is no problem to use SEGV as a signaling mechanism.

Re:macOS Sonoma 14.4 might break Java on your machine by zozbot234 on Monday March 18, 2024 @06:58AM (#39742403)
You can "do pretty much anything you want" as long as you carefully avoid doing anything non thread-safe (even indirectly) in both the main app and the handler itself? How reassuring!
Re:macOS Sonoma 14.4 might break Java on your machine by dwattttt on Monday March 18, 2024 @08:38AM (#39743499)
The HotSpot JVM is already multithreaded, they understand how to write thread-safe code (I'm not sure which JVM(s?) are reported to be impacted, HotSpot was the first I found a result for).
Re:macOS Sonoma 14.4 might break Java on your machine by sp1rit on Monday March 18, 2024 @05:20AM (#39741803)
>> As a normal part of the just-in-time compile and execute cycle

This means a workaround is running java with -Djava.compiler=NONE, no?

Re:macOS Sonoma 14.4 might break Java on your machine by przemelek on Monday March 18, 2024 @05:54AM (#39741996)
I was thinking more about -Xint, or in Docker, or x86 JVM, but my guess is that somebody already tested it ;-) Other thing is that one of developers in my team who is on M1 and 14.4 is able to run Java app, so...
Re:macOS Sonoma 14.4 might break Java on your machine by PhilipRoman on Monday March 18, 2024 @06:31AM (#39742204)
A better choice would be -Xrs which keeps optimizations enabled, but disables use of SEGV.
Re:macOS Sonoma 14.4 might break Java on your machine by zozbot234 on Monday March 18, 2024 @06:43AM (#39742294)
This disables use of all signal handlers, which means Java apps will also e.g. fail to quit cleanly in response to issuing SIGQUIT, or hitting ^C at the terminal. Better than "no workaround whatsoever" but far from ideal!
Re:macOS Sonoma 14.4 might break Java on your machine by josefx on Monday March 18, 2024 @06:58AM (#39742401)
I think it is used to avoid doing null checks on every pointer access.