8017498: JVM crashes when native code calls sigaction(sig) where sig>=0x20

Wed, 17 Jul 2013 12:22:57 -0700

author
ccheung
date
Wed, 17 Jul 2013 12:22:57 -0700
changeset 5420
732af649bc3a
parent 5419
e619a2766bcc
child 5422
6388dbc4b7ca

8017498: JVM crashes when native code calls sigaction(sig) where sig>=0x20
Summary: Added (sig < MAXSIGNUM) check in jsig.c
Reviewed-by: dholmes, acorn

src/os/linux/vm/jsig.c file | annotate | diff | comparison | revisions
test/runtime/jsig/Test8017498.sh file | annotate | diff | comparison | revisions
test/runtime/jsig/TestJNI.c file | annotate | diff | comparison | revisions
test/runtime/jsig/TestJNI.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/os/linux/vm/jsig.c	Wed Jun 12 11:17:39 2013 +0200
     1.2 +++ b/src/os/linux/vm/jsig.c	Wed Jul 17 12:22:57 2013 -0700
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -107,7 +107,7 @@
    1.11  
    1.12    signal_lock();
    1.13  
    1.14 -  sigused = (MASK(sig) & jvmsigs) != 0;
    1.15 +  sigused = (sig < MAXSIGNUM) && ((MASK(sig) & jvmsigs) != 0);
    1.16    if (jvm_signal_installed && sigused) {
    1.17      /* jvm has installed its signal handler for this signal. */
    1.18      /* Save the handler. Don't really install it. */
    1.19 @@ -116,7 +116,7 @@
    1.20  
    1.21      signal_unlock();
    1.22      return oldhandler;
    1.23 -  } else if (jvm_signal_installing) {
    1.24 +  } else if (sig < MAXSIGNUM && jvm_signal_installing) {
    1.25      /* jvm is installing its signal handlers. Install the new
    1.26       * handlers and save the old ones. jvm uses sigaction().
    1.27       * Leave the piece here just in case. */
    1.28 @@ -165,7 +165,7 @@
    1.29  
    1.30    signal_lock();
    1.31  
    1.32 -  sigused = (MASK(sig) & jvmsigs) != 0;
    1.33 +  sigused = (sig < MAXSIGNUM) && ((MASK(sig) & jvmsigs) != 0);
    1.34    if (jvm_signal_installed && sigused) {
    1.35      /* jvm has installed its signal handler for this signal. */
    1.36      /* Save the handler. Don't really install it. */
    1.37 @@ -178,7 +178,7 @@
    1.38  
    1.39      signal_unlock();
    1.40      return 0;
    1.41 -  } else if (jvm_signal_installing) {
    1.42 +  } else if (sig < MAXSIGNUM && jvm_signal_installing) {
    1.43      /* jvm is installing its signal handlers. Install the new
    1.44       * handlers and save the old ones. */
    1.45      res = call_os_sigaction(sig, act, &oldAct);
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/runtime/jsig/Test8017498.sh	Wed Jul 17 12:22:57 2013 -0700
     2.3 @@ -0,0 +1,95 @@
     2.4 +#!/bin/sh
     2.5 +
     2.6 +#
     2.7 +#  Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2.8 +#  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.9 +#
    2.10 +#  This code is free software; you can redistribute it and/or modify it
    2.11 +#  under the terms of the GNU General Public License version 2 only, as
    2.12 +#  published by the Free Software Foundation.
    2.13 +#
    2.14 +#  This code is distributed in the hope that it will be useful, but WITHOUT
    2.15 +#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.16 +#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.17 +#  version 2 for more details (a copy is included in the LICENSE file that
    2.18 +#  accompanied this code).
    2.19 +#
    2.20 +#  You should have received a copy of the GNU General Public License version
    2.21 +#  2 along with this work; if not, write to the Free Software Foundation,
    2.22 +#  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.23 +#
    2.24 +#  Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.25 +#  or visit www.oracle.com if you need additional information or have any
    2.26 +#  questions.
    2.27 +#
    2.28 +
    2.29 +##
    2.30 +## @test Test8017498.sh
    2.31 +## @bug 8017498
    2.32 +## @summary sigaction(sig) results in process hang/timed-out if sig is much greater than SIGRTMAX
    2.33 +## @run shell Test8017498.sh
    2.34 +##
    2.35 +
    2.36 +if [ "${TESTSRC}" = "" ]
    2.37 +then
    2.38 +  TESTSRC=${PWD}
    2.39 +  echo "TESTSRC not set.  Using "${TESTSRC}" as default"
    2.40 +fi
    2.41 +echo "TESTSRC=${TESTSRC}"
    2.42 +## Adding common setup Variables for running shell tests.
    2.43 +. ${TESTSRC}/../../test_env.sh
    2.44 +
    2.45 +# set platform-dependent variables
    2.46 +OS=`uname -s`
    2.47 +case "$OS" in
    2.48 +  Linux)
    2.49 +    echo "Testing on Linux"
    2.50 +    if [ "$VM_BITS" = "64" ]
    2.51 +    then
    2.52 +        LD_PRELOAD=${TESTJAVA}${FS}jre${FS}lib${FS}amd64${FS}libjsig.so
    2.53 +    else
    2.54 +        LD_PRELOAD=${TESTJAVA}${FS}jre${FS}lib${FS}i386${FS}libjsig.so
    2.55 +    fi
    2.56 +    echo LD_PRELOAD = ${LD_PRELOAD}
    2.57 +    export LD_PRELOAD=${LD_PRELOAD}
    2.58 +    ;;
    2.59 +  *)
    2.60 +    NULL=NUL
    2.61 +    PS=";"
    2.62 +    FS="\\"
    2.63 +    echo "Test passed; only valid for Linux"
    2.64 +    exit 0;
    2.65 +    ;;
    2.66 +esac
    2.67 +
    2.68 +THIS_DIR=.
    2.69 +
    2.70 +cp ${TESTSRC}${FS}*.java ${THIS_DIR}
    2.71 +${TESTJAVA}${FS}bin${FS}javac *.java
    2.72 +
    2.73 +gcc -fPIC -shared -o ${TESTSRC}${FS}libTestJNI.so -I${TESTJAVA}${FS}include -I${TESTJAVA}${FS}include${FS}linux ${TESTSRC}${FS}TestJNI.c
    2.74 +
    2.75 +# run the java test in the background
    2.76 +echo ${TESTJAVA}${FS}bin${FS}java -Djava.library.path=${TESTSRC}${FS} -server TestJNI 100 > test.out 2>&1 &
    2.77 +${TESTJAVA}${FS}bin${FS}java -Djava.library.path=${TESTSRC}${FS} -server TestJNI 100 > test.out 2>&1 &
    2.78 +
    2.79 +# obtain the process id
    2.80 +C_PID=$!
    2.81 +
    2.82 +# sleep for 1s
    2.83 +sleep 1
    2.84 +
    2.85 +# reset LD_PRELOAD
    2.86 +unset LD_PRELOAD
    2.87 +
    2.88 +# check the output file (test.out)
    2.89 +grep "old handler" test.out > ${NULL}
    2.90 +if [ $? = 0 ]
    2.91 +then
    2.92 +    echo "Test Passed"
    2.93 +    exit 0
    2.94 +else
    2.95 +    kill -9 ${C_PID}
    2.96 +    echo "Test Failed"
    2.97 +    exit 1
    2.98 +fi
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/runtime/jsig/TestJNI.c	Wed Jul 17 12:22:57 2013 -0700
     3.3 @@ -0,0 +1,61 @@
     3.4 +/*
     3.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +#include <stdio.h>
    3.28 +#include <jni.h>
    3.29 +#define __USE_GNU
    3.30 +#include <signal.h>
    3.31 +#include <sys/ucontext.h>
    3.32 +
    3.33 +#ifdef __cplusplus
    3.34 +extern "C" {
    3.35 +#endif
    3.36 +
    3.37 +void sig_handler(int sig, siginfo_t *info, ucontext_t *context) {
    3.38 +    int thrNum;
    3.39 +
    3.40 +    printf( " HANDLER (1) " );
    3.41 +    // Move forward RIP to skip failing instruction
    3.42 +    context->uc_mcontext.gregs[REG_RIP] += 6;
    3.43 +}
    3.44 +
    3.45 +JNIEXPORT void JNICALL Java_TestJNI_doSomething(JNIEnv *env, jclass klass, jint val) {
    3.46 +    struct sigaction act;
    3.47 +    struct sigaction oact;
    3.48 +    pthread_attr_t attr;
    3.49 +    stack_t stack;
    3.50 +
    3.51 +    act.sa_flags = SA_ONSTACK|SA_RESTART|SA_SIGINFO;
    3.52 +    sigfillset(&act.sa_mask);
    3.53 +    act.sa_handler = SIG_DFL;
    3.54 +    act.sa_sigaction = (void (*)())sig_handler;
    3.55 +    sigaction(0x20+val, &act, &oact);
    3.56 +
    3.57 +    printf( " doSomething(%d) " , val);
    3.58 +    printf( " old handler = %p " , oact.sa_handler);
    3.59 +}
    3.60 +
    3.61 +#ifdef __cplusplus
    3.62 +}
    3.63 +#endif
    3.64 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/runtime/jsig/TestJNI.java	Wed Jul 17 12:22:57 2013 -0700
     4.3 @@ -0,0 +1,42 @@
     4.4 +/*
     4.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + *
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.
    4.11 + *
    4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.15 + * version 2 for more details (a copy is included in the LICENSE file that
    4.16 + * accompanied this code).
    4.17 + *
    4.18 + * You should have received a copy of the GNU General Public License version
    4.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.21 + *
    4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.23 + * or visit www.oracle.com if you need additional information or have any
    4.24 + * questions.
    4.25 + */
    4.26 +
    4.27 +public class TestJNI {
    4.28 +    static {
    4.29 +        System.loadLibrary("TestJNI");
    4.30 +    }
    4.31 +    public static native void doSomething(int val);
    4.32 +    public static void main(String[] args) {
    4.33 +        int intArg = 43;
    4.34 +        if (args.length > 0) {
    4.35 +            try {
    4.36 +                intArg = Integer.parseInt(args[0]);
    4.37 +            } catch (NumberFormatException e) {
    4.38 +                System.err.println("arg " + args[0] + " must be an integer");
    4.39 +                System.exit(1);
    4.40 +            }
    4.41 +        }
    4.42 +        TestJNI.doSomething(intArg);
    4.43 +    }
    4.44 +}
    4.45 +

mercurial