jdk
1/*
2* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*
5* This code is free software; you can redistribute it and/or modify it
6* under the terms of the GNU General Public License version 2 only, as
7* published by the Free Software Foundation.
8*
9* This code is distributed in the hope that it will be useful, but WITHOUT
10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12* version 2 for more details (a copy is included in the LICENSE file that
13* accompanied this code).
14*
15* You should have received a copy of the GNU General Public License version
16* 2 along with this work; if not, write to the Free Software Foundation,
17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18*
19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20* or visit www.oracle.com if you need additional information or have any
21* questions.
22*/
23
24#include "precompiled.hpp"25#ifndef _WIN3226#include "runtime/os.hpp"27#include "utilities/ostream.hpp"28#include "unittest.hpp"29
30#include <errno.h>31#include <signal.h>32#include <stdio.h>33#include <sys/ucontext.h>34#include <string.h>35
36extern "C" {37static void sig_handler(int sig) {38printf( " HANDLER (1) " );39}40}
41
42class PosixSignalTest : public ::testing::Test {43public:44
45static void check_handlers() {46struct sigaction act, old_SIGFPE_act, old_SIGILL_act;47act.sa_handler = sig_handler;48sigemptyset(&act.sa_mask);49act.sa_flags = 0;50ASSERT_NE(sigaction(SIGFPE, &act, &old_SIGFPE_act), -1) << "Setting SIGFPE handler failed: " << os::strerror(errno) << " (" << errno << ")";51ASSERT_NE(sigaction(SIGILL, &act, &old_SIGILL_act), -1) << "Setting SIGILL handler failed: " << os::strerror(errno) << " (" << errno << ")";52
53// Use local stringStream to capture output from run_periodic_checks() calls to54// print_signal_handlers().55stringStream st;56os::run_periodic_checks(&st);57char* res = (char *)st.base(); // res can't be const because some strstr()'s have non-const first args.58
59// Restore signal handlers.60ASSERT_NE(sigaction(SIGFPE, &act, &old_SIGFPE_act), -1) << "Restoring SIGFPE handler failed: " << os::strerror(errno) << " (" << errno << ")";61ASSERT_NE(sigaction(SIGILL, &act, &old_SIGILL_act), -1) << "Restoring SIGILL handler failed: " << os::strerror(errno) << " (" << errno << ")";62
63// Check that "Handler was modified" occurs exactly twice in the tty output.64char* modified = strstr(res, "Handler was modified!");65ASSERT_NE(modified, nullptr) << "No message found";66
67modified = strstr(modified + 1, "Handler was modified!");68ASSERT_NE(modified, nullptr) << "Only one message found";69ASSERT_EQ(strstr(modified + 1, "Handler was modified!"), nullptr) << "Too many messages found";70}71};72
73// This tests the fix for JDK-8285792.
74TEST_OTHER_VM(PosixSignalTest, check_handlers) {75PosixSignalTest::check_handlers();76}
77
78#endif79