Skip to content

Commit db7cf80

Browse files
[GR-67902] Fix segfaults caused by graph scheduling.
PullRequest: graal/21566
2 parents 5c6c04a + cb351b0 commit db7cf80

File tree

3 files changed

+59
-17
lines changed

3 files changed

+59
-17
lines changed

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixSubstrateSigprofHandler.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.graalvm.nativeimage.c.function.CodePointer;
3434
import org.graalvm.word.Pointer;
3535

36+
import com.oracle.svm.core.NeverInline;
3637
import com.oracle.svm.core.RegisterDumper;
3738
import com.oracle.svm.core.SubstrateOptions;
3839
import com.oracle.svm.core.Uninterruptible;
@@ -81,15 +82,21 @@ private static void dispatch(@SuppressWarnings("unused") int signalNumber, @Supp
8182
int savedErrno = LibC.errno();
8283
try {
8384
if (tryEnterIsolate()) {
84-
CodePointer ip = (CodePointer) RegisterDumper.singleton().getIP(uContext);
85-
Pointer sp = (Pointer) RegisterDumper.singleton().getSP(uContext);
86-
tryUninterruptibleStackWalk(ip, sp, true);
85+
dispatch0(uContext);
8786
}
8887
} finally {
8988
LibC.setErrno(savedErrno);
9089
}
9190
}
9291

92+
@Uninterruptible(reason = "The method executes during signal handling.", callerMustBe = true)
93+
@NeverInline("Base registers are set in caller, prevent reads from floating before that.")
94+
private static void dispatch0(Signal.ucontext_t uContext) {
95+
CodePointer ip = (CodePointer) RegisterDumper.singleton().getIP(uContext);
96+
Pointer sp = (Pointer) RegisterDumper.singleton().getSP(uContext);
97+
tryUninterruptibleStackWalk(ip, sp, true);
98+
}
99+
93100
@Override
94101
protected void installSignalHandler() {
95102
PosixSignalHandlerSupport.installNativeSignalHandler(Signal.SignalEnum.SIGPROF, advancedSignalDispatcher.getFunctionPointer(), Signal.SA_RESTART(),

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateSegfaultHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ public static void dump(PointerBase signalInfo, RegisterDumper.Context context)
259259
}
260260

261261
@Uninterruptible(reason = "Must be uninterruptible until we get immune to safepoints.")
262+
@NeverInline("Base registers are set in caller, prevent reads from floating before that.")
262263
public static void dump(PointerBase signalInfo, RegisterDumper.Context context, boolean inSVMSegfaultHandler) {
263264
Pointer sp = (Pointer) RegisterDumper.singleton().getSP(context);
264265
CodePointer ip = (CodePointer) RegisterDumper.singleton().getIP(context);

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/CEntryPointSnippets.java

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,13 @@ public static void initBaseRegisters(PointerBase heapBase, PointerBase codeBase)
256256
@Snippet(allowMissingProbabilities = true)
257257
public static int createIsolateSnippet(CEntryPointCreateIsolateParameters parameters) {
258258
writeCurrentVMThread(Word.nullPointer());
259+
259260
int result = runtimeCall(CREATE_ISOLATE, parameters);
260261
if (result != CEntryPointErrors.NO_ERROR) {
261262
return result;
262263
}
263-
ThreadStatusTransition.fromNativeToJava(false);
264264

265+
ThreadStatusTransition.fromNativeToJava(false);
265266
return runtimeCallInitializeIsolate(INITIALIZE_ISOLATE, parameters);
266267
}
267268

@@ -310,14 +311,15 @@ private static int createIsolate(CEntryPointCreateIsolateParameters providedPara
310311
IsolateArgumentParser.singleton().tearDown(arguments);
311312
return error;
312313
}
314+
313315
Isolate isolate = isolatePtr.read();
314316
initBaseRegisters(Isolates.getHeapBase(isolate));
315317

316318
return createIsolate0(isolate, arguments);
317319
}
318320

319321
@Uninterruptible(reason = "Thread state not yet set up.")
320-
@NeverInline(value = "Ensure this code cannot rise above where heap base is set.")
322+
@NeverInline("Base registers are set in caller, prevent reads from floating before that.")
321323
private static int createIsolate0(Isolate isolate, IsolateArguments arguments) {
322324
assert Heap.getHeap().verifyImageHeapMapping();
323325
IsolateArgumentParser.singleton().persistOptions(arguments);
@@ -327,10 +329,12 @@ private static int createIsolate0(Isolate isolate, IsolateArguments arguments) {
327329
if (!VMThreads.ensureInitialized()) {
328330
return CEntryPointErrors.THREADING_INITIALIZATION_FAILED;
329331
}
332+
330333
int error = enterAttachThread0(isolate, false, true);
331334
if (error != CEntryPointErrors.NO_ERROR) {
332335
return error;
333336
}
337+
334338
PlatformThreads.singleton().assignMainThread();
335339
return CEntryPointErrors.NO_ERROR;
336340
}
@@ -504,13 +508,13 @@ private static int initializeIsolateInterruptibly1(CEntryPointCreateIsolateParam
504508
@Snippet(allowMissingProbabilities = true)
505509
public static int attachThreadSnippet(Isolate isolate, boolean startedByIsolate, boolean ensureJavaThread) {
506510
writeCurrentVMThread(Word.nullPointer());
511+
507512
int error = runtimeCall(ATTACH_THREAD, isolate, startedByIsolate, ensureJavaThread);
508513
if (error != CEntryPointErrors.NO_ERROR) {
509514
return error;
510515
}
511516

512517
ThreadStatusTransition.fromNativeToJava(false);
513-
514518
if (ensureJavaThread) {
515519
return runtimeCallEnsureJavaThread(ENSURE_JAVA_THREAD);
516520
}
@@ -534,7 +538,14 @@ private static int enterAttachThread0(Isolate isolate, boolean startedByIsolate,
534538
if (error != CEntryPointErrors.NO_ERROR) {
535539
return error;
536540
}
541+
537542
initBaseRegisters(Isolates.getHeapBase(isolate));
543+
return enterAttachThread1(isolate, startedByIsolate, ensureJavaThread, allowAttach, inCrashHandler);
544+
}
545+
546+
@Uninterruptible(reason = "Thread state not set up yet.")
547+
@NeverInline("Base registers are set in caller, prevent reads from floating before that.")
548+
private static int enterAttachThread1(Isolate isolate, boolean startedByIsolate, boolean ensureJavaThread, boolean allowAttach, boolean inCrashHandler) {
538549
if (!VMThreads.isInitialized()) {
539550
return CEntryPointErrors.UNINITIALIZED_ISOLATE;
540551
}
@@ -567,6 +578,7 @@ private static int attachUnattachedThread(Isolate isolate, boolean startedByIsol
567578
if (thread.isNull()) {
568579
return CEntryPointErrors.ALLOCATION_FAILED;
569580
}
581+
570582
writeCurrentVMThread(thread);
571583
if (!StackOverflowCheck.singleton().initialize()) {
572584
return CEntryPointErrors.UNKNOWN_STACK_BOUNDARIES;
@@ -601,8 +613,13 @@ public static int enterFromCrashHandler(Isolate isolate) {
601613
@Uninterruptible(reason = "Thread state not yet set up.")
602614
public static void initializeIsolateThreadForCrashHandler(Isolate isolate, IsolateThread thread) {
603615
initBaseRegisters(Isolates.getHeapBase(isolate));
604-
605616
writeCurrentVMThread(thread);
617+
initializeIsolateThreadForCrashHandler0(isolate, thread);
618+
}
619+
620+
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE)
621+
@NeverInline("Base registers are set in caller, prevent reads from floating before that.")
622+
private static void initializeIsolateThreadForCrashHandler0(Isolate isolate, IsolateThread thread) {
606623
VMThreads.StatusSupport.setStatusNative();
607624
VMThreads.IsolateTL.set(thread, isolate);
608625
SubstrateDiagnostics.setOnlyAttachedForCrashHandler(thread);
@@ -713,13 +730,11 @@ private static boolean initiateTearDownIsolateInterruptibly() {
713730

714731
@Snippet(allowMissingProbabilities = true)
715732
public static int enterByIsolateSnippet(Isolate isolate) {
716-
int result;
717733
writeCurrentVMThread(Word.nullPointer());
718-
result = runtimeCall(ENTER_BY_ISOLATE, isolate);
719-
if (result == CEntryPointErrors.NO_ERROR) {
720-
if (VMThreads.StatusSupport.isStatusNativeOrSafepoint()) {
721-
ThreadStatusTransition.fromNativeToJava(false);
722-
}
734+
735+
int result = runtimeCall(ENTER_BY_ISOLATE, isolate);
736+
if (result == CEntryPointErrors.NO_ERROR && VMThreads.StatusSupport.isStatusNativeOrSafepoint()) {
737+
ThreadStatusTransition.fromNativeToJava(false);
723738
}
724739
return result;
725740
}
@@ -731,14 +746,23 @@ private static int enterByIsolate(Isolate isolate) {
731746
if (error != CEntryPointErrors.NO_ERROR) {
732747
return error;
733748
}
749+
734750
initBaseRegisters(Isolates.getHeapBase(isolate));
751+
return enterByIsolate0();
752+
}
753+
754+
@Uninterruptible(reason = "Thread state not set up yet.")
755+
@NeverInline("Base registers are set in caller, prevent reads from floating before that.")
756+
private static int enterByIsolate0() {
735757
if (!VMThreads.isInitialized()) {
736758
return CEntryPointErrors.UNINITIALIZED_ISOLATE;
737759
}
760+
738761
IsolateThread thread = VMThreads.singleton().findIsolateThreadForCurrentOSThread(false);
739762
if (thread.isNull()) {
740763
return CEntryPointErrors.UNATTACHED_THREAD;
741764
}
765+
742766
writeCurrentVMThread(thread);
743767
return CEntryPointErrors.NO_ERROR;
744768
}
@@ -748,9 +772,11 @@ public static int enterSnippet(IsolateThread thread) {
748772
if (thread.isNull()) {
749773
return CEntryPointErrors.NULL_ARGUMENT;
750774
}
775+
751776
writeCurrentVMThread(thread);
752777
Isolate isolate = VMThreads.IsolateTL.get(thread);
753778
initBaseRegisters(Isolates.getHeapBase(isolate));
779+
754780
if (runtimeAssertionsEnabled() || SubstrateOptions.CheckIsolateThreadAtEntry.getValue()) {
755781
/*
756782
* Verification must happen before the thread state transition. It locks the raw
@@ -759,9 +785,7 @@ public static int enterSnippet(IsolateThread thread) {
759785
runtimeCall(VERIFY_ISOLATE_THREAD, thread);
760786
}
761787
ThreadStatusTransition.fromNativeToJava(false);
762-
763788
return CEntryPointErrors.NO_ERROR;
764-
765789
}
766790

767791
@Fold
@@ -804,7 +828,7 @@ private static int reportException(Throwable exception) {
804828

805829
private static void reportExceptionInterruptibly(Throwable exception) {
806830
logException(exception);
807-
VMError.shouldNotReachHere("Unhandled exception");
831+
throw VMError.shouldNotReachHere("Unhandled exception");
808832
}
809833

810834
@RestrictHeapAccess(access = NO_ALLOCATION, reason = "Must not allocate in fatal error handling.")
@@ -825,13 +849,23 @@ public static int returnFromJavaToCSnippet() {
825849

826850
@Snippet(allowMissingProbabilities = true)
827851
public static boolean isAttachedSnippet(Isolate isolate) {
828-
return Isolates.checkIsolate(isolate) == CEntryPointErrors.NO_ERROR && runtimeCallIsAttached(IS_ATTACHED, isolate);
852+
return runtimeCallIsAttached(IS_ATTACHED, isolate);
829853
}
830854

831855
@Uninterruptible(reason = "Thread state not yet set up.")
832856
@SubstrateForeignCallTarget(stubCallingConvention = false)
833857
private static boolean isAttached(Isolate isolate) {
858+
if (Isolates.checkIsolate(isolate) != CEntryPointErrors.NO_ERROR) {
859+
return false;
860+
}
861+
834862
initBaseRegisters(Isolates.getHeapBase(isolate));
863+
return isAttached0();
864+
}
865+
866+
@Uninterruptible(reason = "Thread state not yet set up.")
867+
@NeverInline("Base registers are set in caller, prevent reads from floating before that.")
868+
private static boolean isAttached0() {
835869
return VMThreads.isInitialized() && VMThreads.singleton().findIsolateThreadForCurrentOSThread(false).isNonNull();
836870
}
837871

0 commit comments

Comments
 (0)