From 2e6156b452c2cf64cec90e5140b26b7688e54756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandar=20Pejovi=C4=87?= Date: Tue, 24 Mar 2020 20:38:26 +0100 Subject: [PATCH] Refactor SubstrateSegfaultHandler --- ...ava => PosixSubstrateSegfaultHandler.java} | 47 +++++--------- .../svm/core/SubstrateSegfaultHandler.java | 63 +++++++++++++++++++ 2 files changed, 79 insertions(+), 31 deletions(-) rename substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/{SegfaultHandlerFeature.java => PosixSubstrateSegfaultHandler.java} (81%) create mode 100644 substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateSegfaultHandler.java diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/SegfaultHandlerFeature.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixSubstrateSegfaultHandler.java similarity index 81% rename from substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/SegfaultHandlerFeature.java rename to substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixSubstrateSegfaultHandler.java index 96a320388814..ed2a4a93a3b2 100644 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/SegfaultHandlerFeature.java +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixSubstrateSegfaultHandler.java @@ -24,8 +24,6 @@ */ package com.oracle.svm.core.posix; -import org.graalvm.compiler.options.Option; -import org.graalvm.nativeimage.ImageInfo; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.Isolate; import org.graalvm.nativeimage.IsolateThread; @@ -42,6 +40,7 @@ import com.oracle.svm.core.Isolates; import com.oracle.svm.core.SubstrateOptions; +import com.oracle.svm.core.SubstrateSegfaultHandler; import com.oracle.svm.core.annotate.AutomaticFeature; import com.oracle.svm.core.annotate.RestrictHeapAccess; import com.oracle.svm.core.annotate.Uninterruptible; @@ -56,9 +55,7 @@ import com.oracle.svm.core.graal.nodes.WriteCurrentVMThreadNode; import com.oracle.svm.core.graal.nodes.WriteHeapBaseNode; import com.oracle.svm.core.graal.snippets.CEntryPointSnippets.IsolateCreationWatcher; -import com.oracle.svm.core.jdk.RuntimeSupport; import com.oracle.svm.core.log.Log; -import com.oracle.svm.core.option.RuntimeOptionKey; import com.oracle.svm.core.posix.headers.LibC; import com.oracle.svm.core.posix.headers.Signal; import com.oracle.svm.core.posix.headers.Signal.AdvancedSignalDispatcher; @@ -69,27 +66,17 @@ import com.oracle.svm.core.thread.VMThreads; @AutomaticFeature -public class SegfaultHandlerFeature implements Feature { - +class PosixSubstrateSegfaultHandlerFeature implements Feature { @Override public void afterRegistration(AfterRegistrationAccess access) { + ImageSingletons.add(SubstrateSegfaultHandler.class, new PosixSubstrateSegfaultHandler()); if (SubstrateOptions.useLLVMBackend()) { - ImageSingletons.add(IsolateCreationWatcher.class, new SubstrateSegfaultHandler.SingleIsolateSegfaultIsolateSetup()); + ImageSingletons.add(IsolateCreationWatcher.class, new PosixSubstrateSegfaultHandler.SingleIsolateSegfaultIsolateSetup()); } } - - @Override - public void beforeAnalysis(BeforeAnalysisAccess access) { - RuntimeSupport.getRuntimeSupport().addStartupHook(SubstrateSegfaultHandler::install); - } } -class SubstrateSegfaultHandler { - - public static class Options { - @Option(help = "Install segfault handler that prints register contents and full Java stacktrace. Default: enabled for an executable, disabled for a shared library.")// - static final RuntimeOptionKey InstallSegfaultHandler = new RuntimeOptionKey<>(null); - } +class PosixSubstrateSegfaultHandler extends SubstrateSegfaultHandler { private static volatile boolean dispatchInProgress = false; @@ -148,20 +135,18 @@ private static void dump(int signalNumber, ucontext_t uContext) { } /** The address of the signal handler for signals handled by Java code, above. */ - private static final CEntryPointLiteral advancedSignalDispatcher = CEntryPointLiteral.create(SubstrateSegfaultHandler.class, "dispatch", int.class, siginfo_t.class, - ucontext_t.class); + private static final CEntryPointLiteral advancedSignalDispatcher = CEntryPointLiteral.create(PosixSubstrateSegfaultHandler.class, + "dispatch", int.class, siginfo_t.class, ucontext_t.class); - static void install() { - Boolean optionValue = Options.InstallSegfaultHandler.getValue(); - if (optionValue == Boolean.TRUE || (optionValue == null && ImageInfo.isExecutable())) { - int structSigActionSize = SizeOf.get(sigaction.class); - sigaction structSigAction = StackValue.get(structSigActionSize); - LibC.memset(structSigAction, WordFactory.signed(0), WordFactory.unsigned(structSigActionSize)); - /* Register sa_sigaction signal handler */ - structSigAction.sa_flags(Signal.SA_SIGINFO()); - structSigAction.sa_sigaction(advancedSignalDispatcher.getFunctionPointer()); - Signal.sigaction(Signal.SignalEnum.SIGSEGV, structSigAction, WordFactory.nullPointer()); - } + @Override + protected void install() { + int structSigActionSize = SizeOf.get(sigaction.class); + sigaction structSigAction = StackValue.get(structSigActionSize); + LibC.memset(structSigAction, WordFactory.signed(0), WordFactory.unsigned(structSigActionSize)); + /* Register sa_sigaction signal handler */ + structSigAction.sa_flags(Signal.SA_SIGINFO()); + structSigAction.sa_sigaction(advancedSignalDispatcher.getFunctionPointer()); + Signal.sigaction(Signal.SignalEnum.SIGSEGV, structSigAction, WordFactory.nullPointer()); } static class SingleIsolateSegfaultIsolateSetup implements IsolateCreationWatcher { diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateSegfaultHandler.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateSegfaultHandler.java new file mode 100644 index 000000000000..e1936ba8c026 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateSegfaultHandler.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.svm.core; + +import org.graalvm.compiler.options.Option; +import org.graalvm.nativeimage.ImageInfo; +import org.graalvm.nativeimage.ImageSingletons; +import org.graalvm.nativeimage.hosted.Feature; + +import com.oracle.svm.core.annotate.AutomaticFeature; +import com.oracle.svm.core.jdk.RuntimeSupport; +import com.oracle.svm.core.option.RuntimeOptionKey; + +@AutomaticFeature +class SubstrateSegfaultHandlerFeature implements Feature { + @Override + public void beforeAnalysis(Feature.BeforeAnalysisAccess access) { + if (!ImageSingletons.contains(SubstrateSegfaultHandler.class)) { + return; /* No segfault handler. */ + } + RuntimeSupport.getRuntimeSupport().addStartupHook(SubstrateSegfaultHandler::startupHook); + } +} + +public abstract class SubstrateSegfaultHandler { + + public static class Options { + @Option(help = "Install segfault handler that prints register contents and full Java stacktrace. Default: enabled for an executable, disabled for a shared library.")// + static final RuntimeOptionKey InstallSegfaultHandler = new RuntimeOptionKey<>(null); + } + + static void startupHook() { + Boolean optionValue = Options.InstallSegfaultHandler.getValue(); + if (optionValue == Boolean.TRUE || (optionValue == null && ImageInfo.isExecutable())) { + ImageSingletons.lookup(SubstrateSegfaultHandler.class).install(); + } + } + + /** Installs the platform dependent segfault handler. */ + protected abstract void install(); +}