diff options
-rw-r--r-- | sigchainlib/sigchain.cc | 56 | ||||
-rw-r--r-- | test/004-SignalTest/signaltest.cc | 4 | ||||
-rw-r--r-- | test/004-SignalTest2/expected.txt | 5 | ||||
-rw-r--r-- | test/004-SignalTest2/info.txt | 1 | ||||
-rw-r--r-- | test/004-SignalTest2/signaltest.cc | 53 | ||||
-rw-r--r-- | test/004-SignalTest2/src/Main.java | 52 | ||||
-rw-r--r-- | test/Android.libarttest.mk | 1 | ||||
-rw-r--r-- | test/Android.run-test.mk | 1 |
8 files changed, 167 insertions, 6 deletions
diff --git a/sigchainlib/sigchain.cc b/sigchainlib/sigchain.cc index c655226..50bfe70 100644 --- a/sigchainlib/sigchain.cc +++ b/sigchainlib/sigchain.cc @@ -34,7 +34,7 @@ namespace art { class SignalAction { public: - SignalAction() : claimed_(false) { + SignalAction() : claimed_(false), uses_old_style_(false) { } // Claim the signal and keep the action specified. @@ -60,13 +60,22 @@ class SignalAction { } // Change the recorded action to that specified. - void SetAction(const struct sigaction& action) { + // If oldstyle is true then this action is from an older style signal() + // call as opposed to sigaction(). In this case the sa_handler is + // used when invoking the user's handler. + void SetAction(const struct sigaction& action, bool oldstyle) { action_ = action; + uses_old_style_ = oldstyle; + } + + bool OldStyle() const { + return uses_old_style_; } private: struct sigaction action_; // Action to be performed. bool claimed_; // Whether signal is claimed or not. + bool uses_old_style_; // Action is created using signal(). Use sa_handler. }; // User's signal handlers @@ -115,7 +124,7 @@ void InvokeUserSignalHandler(int sig, siginfo_t* info, void* context) { } const struct sigaction& action = user_sigactions[sig].GetAction(); - if ((action.sa_flags & SA_SIGINFO) == 0) { + if (user_sigactions[sig].OldStyle()) { if (action.sa_handler != NULL) { action.sa_handler(sig); } else { @@ -145,7 +154,7 @@ int sigaction(int signal, const struct sigaction* new_action, struct sigaction* *old_action = user_sigactions[signal].GetAction(); } if (new_action != NULL) { - user_sigactions[signal].SetAction(*new_action); + user_sigactions[signal].SetAction(*new_action, false); } return 0; } @@ -168,6 +177,45 @@ int sigaction(int signal, const struct sigaction* new_action, struct sigaction* return linked_sigaction(signal, new_action, old_action); } +sighandler_t signal(int signal, sighandler_t handler) { + struct sigaction sa; + sigemptyset(&sa.sa_mask); + sa.sa_handler = handler; + sa.sa_flags = SA_RESTART; + sighandler_t oldhandler; + + // If this signal has been claimed as a signal chain, record the user's + // action but don't pass it on to the kernel. + // Note that we check that the signal number is in range here. An out of range signal + // number should behave exactly as the libc sigaction. + if (signal > 0 && signal < _NSIG && user_sigactions[signal].IsClaimed()) { + oldhandler = reinterpret_cast<sighandler_t>(user_sigactions[signal].GetAction().sa_handler); + user_sigactions[signal].SetAction(sa, true); + return oldhandler; + } + + // Will only get here if the signal chain has not been claimed. We want + // to pass the sigaction on to the kernel via the real sigaction in libc. + + void* linked_sigaction_sym = dlsym(RTLD_NEXT, "sigaction"); + if (linked_sigaction_sym == nullptr) { + linked_sigaction_sym = dlsym(RTLD_DEFAULT, "sigaction"); + if (linked_sigaction_sym == nullptr || + linked_sigaction_sym == reinterpret_cast<void*>(sigaction)) { + log("Unable to find next sigaction in signal chain"); + abort(); + } + } + + typedef int (*SigAction)(int, const struct sigaction*, struct sigaction*); + SigAction linked_sigaction = reinterpret_cast<SigAction>(linked_sigaction_sym); + if (linked_sigaction(signal, &sa, &sa) == -1) { + return SIG_ERR; + } + + return reinterpret_cast<sighandler_t>(sa.sa_handler); +} + int sigprocmask(int how, const sigset_t* bionic_new_set, sigset_t* bionic_old_set) { const sigset_t* new_set_ptr = bionic_new_set; sigset_t tmpset; diff --git a/test/004-SignalTest/signaltest.cc b/test/004-SignalTest/signaltest.cc index f09fc26..a6d9b66 100644 --- a/test/004-SignalTest/signaltest.cc +++ b/test/004-SignalTest/signaltest.cc @@ -87,12 +87,12 @@ extern "C" JNIEXPORT void JNICALL Java_Main_terminateSignalTest(JNIEnv*, jclass) // Prevent the compiler being a smart-alec and optimizing out the assignment // to nullptr. -char *p = nullptr; +char *go_away_compiler = nullptr; extern "C" JNIEXPORT jint JNICALL Java_Main_testSignal(JNIEnv*, jclass) { #if defined(__arm__) || defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) // On supported architectures we cause a real SEGV. - *p = 'a'; + *go_away_compiler = 'a'; #else // On other architectures we simulate SEGV. kill(getpid(), SIGSEGV); diff --git a/test/004-SignalTest2/expected.txt b/test/004-SignalTest2/expected.txt new file mode 100644 index 0000000..fd5ec00 --- /dev/null +++ b/test/004-SignalTest2/expected.txt @@ -0,0 +1,5 @@ +init signal test +Caught NullPointerException +Caught StackOverflowError +signal caught +Signal test OK diff --git a/test/004-SignalTest2/info.txt b/test/004-SignalTest2/info.txt new file mode 100644 index 0000000..00b0d9a --- /dev/null +++ b/test/004-SignalTest2/info.txt @@ -0,0 +1 @@ +Imported from oat tests. diff --git a/test/004-SignalTest2/signaltest.cc b/test/004-SignalTest2/signaltest.cc new file mode 100644 index 0000000..47b1eb6 --- /dev/null +++ b/test/004-SignalTest2/signaltest.cc @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "jni.h" + +#include <sys/ucontext.h> + +static int signal_count; +static const int kMaxSignal = 2; + +static void signalhandler(int sig) { + printf("signal caught\n"); + ++signal_count; + if (signal_count > kMaxSignal) { + printf("too many signals\n"); + abort(); + } + printf("Signal test OK\n"); + exit(0); +} + +sighandler_t oldsignal; + +extern "C" JNIEXPORT void JNICALL Java_Main_initSignalTest2(JNIEnv*, jclass) { + oldsignal = signal(SIGSEGV, reinterpret_cast<sighandler_t>(signalhandler)); +} + +// Prevent the compiler being a smart-alec and optimizing out the assignment +// to nullptr. +char *go_away_compiler2 = nullptr; + +extern "C" JNIEXPORT void JNICALL Java_Main_testSignal2(JNIEnv*, jclass) { + *go_away_compiler2 = 'a'; +} + diff --git a/test/004-SignalTest2/src/Main.java b/test/004-SignalTest2/src/Main.java new file mode 100644 index 0000000..7799e8c --- /dev/null +++ b/test/004-SignalTest2/src/Main.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +public class Main { + private static native void initSignalTest2(); + private static native void testSignal2(); + + private static void stackOverflow() { + stackOverflow(); + } + + public static void main(String[] args) { + System.loadLibrary("arttest"); + + System.out.println("init signal test"); + initSignalTest2(); + try { + Object o = null; + int hash = o.hashCode(); + + // Should never get here. + System.out.println("hash: " + hash); + throw new AssertionError(); + } catch (NullPointerException e) { + System.out.println("Caught NullPointerException"); + } + try { + stackOverflow(); + + // Should never get here. + throw new AssertionError(); + } catch (StackOverflowError e) { + System.out.println("Caught StackOverflowError"); + } + + // Test that a signal in native code works. + testSignal2(); + } +} diff --git a/test/Android.libarttest.mk b/test/Android.libarttest.mk index 2d139a6..1054203 100644 --- a/test/Android.libarttest.mk +++ b/test/Android.libarttest.mk @@ -21,6 +21,7 @@ include art/build/Android.common_build.mk LIBARTTEST_COMMON_SRC_FILES := \ 004-JniTest/jni_test.cc \ 004-SignalTest/signaltest.cc \ + 004-SignalTest2/signaltest.cc \ 004-ReferenceMap/stack_walk_refmap_jni.cc \ 004-StackWalk/stack_walk_jni.cc \ 004-UnsafeTest/unsafe_test.cc \ diff --git a/test/Android.run-test.mk b/test/Android.run-test.mk index e4ee4a7..0b6b27a 100644 --- a/test/Android.run-test.mk +++ b/test/Android.run-test.mk @@ -189,6 +189,7 @@ TEST_ART_BROKEN_NO_PREBUILD_TESTS := # Tests that are broken with tracing. TEST_ART_BROKEN_TRACE_RUN_TESTS := \ 004-SignalTest \ + 004-SignalTest2 \ 018-stack-overflow \ 097-duplicate-method \ 107-int-math2 |