1 /*
   2  * Copyright (c) 2010, 2016, 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 /* This code tests the fact that we actually remove stack guard page when calling
  25  * JavaThread::exit() i.e. when detaching from current thread.
  26  * We overflow the stack and check that we get access error because of a guard page.
  27  * Than we detach from vm thread and overflow stack once again. This time we shouldn't
  28  * get access error because stack guard page is removed
  29  *
  30  * Notice: due a complicated interaction of signal handlers, the test may crash.
  31  * It's OK - don't file a bug.
  32  */
  33 
  34 #include <assert.h>
  35 #include <jni.h>
  36 #include <jvm.h>
  37 #include <alloca.h>
  38 #include <signal.h>
  39 #include <string.h>
  40 #include <sys/mman.h>
  41 #include <stdlib.h>
  42 #include <sys/ucontext.h>
  43 #include <setjmp.h>
  44 #include <unistd.h>
  45 #include <sys/syscall.h>
  46 #include <errno.h>
  47 
  48 #include <pthread.h>
  49 
  50 #define CLASS_PATH_OPT "-Djava.class.path="
  51 
  52 JavaVM* _jvm;
  53 
  54 static jmp_buf  context;
  55 
  56 static int _last_si_code = -1;
  57 static int _failures = 0;
  58 static int _rec_count = 0;
  59 static int _kp_rec_count = 0;
  60 
  61 pid_t gettid() {
  62   return (pid_t) syscall(SYS_gettid);
  63 }
  64 
  65 static void handler(int sig, siginfo_t *si, void *unused) {
  66   _last_si_code = si->si_code;
  67   printf("Got SIGSEGV(%d) at address: 0x%lx\n",si->si_code, (long) si->si_addr);
  68   longjmp(context, 1);
  69 }
  70 
  71 void set_signal_handler() {
  72   static char altstack[SIGSTKSZ];
  73 
  74   stack_t ss = {
  75     .ss_size = SIGSTKSZ,
  76     .ss_flags = 0,
  77     .ss_sp = altstack
  78   };
  79 
  80   struct sigaction sa = {
  81     .sa_sigaction = handler,
  82     .sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESETHAND
  83   };
  84 
  85   _last_si_code = -1;
  86 
  87   sigaltstack(&ss, 0);
  88   sigemptyset(&sa.sa_mask);
  89   if (sigaction(SIGSEGV, &sa, NULL) == -1) {
  90     fprintf(stderr, "Test ERROR. Can't set sigaction (%d)\n", errno);
  91     exit(7);
  92   }
  93 }
  94 
  95 int get_java_stacksize () {
  96   size_t stacksize;
  97   pthread_attr_t attr;
  98   JDK1_1InitArgs jdk_args;
  99 
 100   jdk_args.version = JNI_VERSION_1_1;
 101   JNI_GetDefaultJavaVMInitArgs(&jdk_args);
 102   if (jdk_args.javaStackSize <= 0) {
 103     fprintf(stderr, "Test ERROR. Can't get a valid value for the default stacksize.\n");
 104     exit(7);
 105   }
 106   return jdk_args.javaStackSize;
 107 }
 108 
 109 void *run_java_overflow (void *p) {
 110   JNIEnv *env;
 111   jclass class_id;
 112   jmethodID method_id;
 113   int res;
 114 
 115   res = (*_jvm)->AttachCurrentThread(_jvm, (void**)&env, NULL);
 116   if (res != JNI_OK) {
 117     fprintf(stderr, "Test ERROR. Can't attach to current thread\n");
 118     exit(7);
 119   }
 120 
 121   class_id = (*env)->FindClass (env, "DoOverflow");
 122   if (class_id == NULL) {
 123     fprintf(stderr, "Test ERROR. Can't load class DoOverflow\n");
 124     exit(7);
 125   }
 126 
 127   method_id = (*env)->GetStaticMethodID(env, class_id, "printIt", "()V");
 128   if (method_id == NULL) {
 129     fprintf(stderr, "Test ERROR. Can't find method DoOverflow.printIt\n");
 130     exit(7);
 131   }
 132 
 133   (*env)->CallStaticVoidMethod(env, class_id, method_id, NULL);
 134 
 135   res = (*_jvm)->DetachCurrentThread(_jvm);
 136   if (res != JNI_OK) {
 137     fprintf(stderr, "Test ERROR. Can't call detach from current thread\n");
 138     exit(7);
 139   }
 140   return NULL;
 141 }
 142 
 143 void do_overflow(){
 144   int *p = alloca(sizeof(int));
 145   if (_kp_rec_count == 0 || _rec_count < _kp_rec_count) {
 146       _rec_count ++;
 147       do_overflow();
 148   }
 149 }
 150 
 151 void *run_native_overflow(void *p) {
 152   // Test that stack guard page is correctly set for initial and non initial thread
 153   // and correctly removed for the initial thread
 154   JNIEnv *env;
 155   jclass class_id;
 156   jmethodID method_id;
 157   int res;
 158 
 159   printf("run_native_overflow %ld\n", (long) gettid());
 160 
 161   res = (*_jvm)->AttachCurrentThread(_jvm, (void **)&env, NULL);
 162   if (res != JNI_OK) {
 163     fprintf(stderr, "Test ERROR. Can't attach to current thread\n");
 164     exit(7);
 165   }
 166 
 167   class_id = (*env)->FindClass (env, "DoOverflow");
 168   if (class_id == NULL) {
 169     fprintf(stderr, "Test ERROR. Can't load class DoOverflow\n");
 170     exit(7);
 171   }
 172 
 173   method_id = (*env)->GetStaticMethodID (env, class_id, "printAlive", "()V");
 174   if (method_id == NULL) {
 175     fprintf(stderr, "Test ERROR. Can't find method DoOverflow.printAlive\n");
 176     exit(7);
 177   }
 178 
 179   (*env)->CallStaticVoidMethod (env, class_id, method_id, NULL);
 180 
 181   set_signal_handler();
 182   if (! setjmp(context)) {
 183     do_overflow();
 184   }
 185 
 186   if (_last_si_code == SEGV_ACCERR) {
 187     printf("Test PASSED. Got access violation accessing guard page at %d\n", _rec_count);
 188   }
 189 
 190   res = (*_jvm)->DetachCurrentThread(_jvm);
 191   if (res != JNI_OK) {
 192     fprintf(stderr, "Test ERROR. Can't call detach from current thread\n");
 193     exit(7);
 194   }
 195 
 196   if (getpid() != gettid()) {
 197     // For non-initial thread we don't unmap the region but call os::uncommit_memory and keep PROT_NONE
 198     // so if host has enough swap space we will get the same SEGV with code SEGV_ACCERR(2) trying
 199     // to access it as if the guard page is present.
 200     // We have no way to check this, so bail out, marking test as succeeded
 201     printf("Test PASSED. Not initial thread\n");
 202     return NULL;
 203   }
 204 
 205   // Limit depth of recursion for second run. It can't exceed one for first run.
 206   _kp_rec_count = _rec_count;
 207   _rec_count = 0;
 208 
 209   set_signal_handler();
 210   if (! setjmp(context)) {
 211     do_overflow();
 212   }
 213 
 214   if (_last_si_code == SEGV_ACCERR) {
 215       ++ _failures;
 216       fprintf(stderr,"Test FAILED. Stack guard page is still there at %d\n", _rec_count);
 217   } else if (_last_si_code == -1) {
 218       printf("Test PASSED. No stack guard page is present. Maximum recursion level reached at %d\n", _rec_count);
 219   }
 220   else{
 221       printf("Test PASSED. No stack guard page is present. SIGSEGV(%d) at %d\n", _last_si_code, _rec_count);
 222   }
 223 
 224   return NULL;
 225 }
 226 
 227 void usage() {
 228   fprintf(stderr, "Usage: invoke test_java_overflow\n");
 229   fprintf(stderr, "       invoke test_native_overflow\n");
 230 }
 231 
 232 
 233 int main (int argc, const char** argv) {
 234   JavaVMInitArgs vm_args;
 235   JavaVMOption options[3];
 236   JNIEnv* env;
 237   int optlen;
 238   char *javaclasspath = NULL;
 239   char javaclasspathopt[4096];
 240 
 241   printf("Test started with pid: %ld\n", (long) getpid());
 242 
 243   /* set the java class path so the DoOverflow class can be found */
 244   javaclasspath = getenv("CLASSPATH");
 245 
 246   if (javaclasspath == NULL) {
 247     fprintf(stderr, "Test ERROR. CLASSPATH is not set\n");
 248     exit(7);
 249   }
 250   optlen = strlen(CLASS_PATH_OPT) + strlen(javaclasspath) + 1;
 251   if (optlen > 4096) {
 252     fprintf(stderr, "Test ERROR. CLASSPATH is too long\n");
 253     exit(7);
 254   }
 255   snprintf(javaclasspathopt, sizeof(javaclasspathopt), "%s%s",
 256       CLASS_PATH_OPT, javaclasspath);
 257 
 258   options[0].optionString = "-Xint";
 259   options[1].optionString = "-Xss1M";
 260   options[2].optionString = javaclasspathopt;
 261 
 262   vm_args.version = JNI_VERSION_1_2;
 263   vm_args.ignoreUnrecognized = JNI_TRUE;
 264   vm_args.options = options;
 265   vm_args.nOptions = 3;
 266 
 267   if (JNI_CreateJavaVM (&_jvm, (void **)&env, &vm_args) < 0 ) {
 268     fprintf(stderr, "Test ERROR. Can't create JavaVM\n");
 269     exit(7);
 270   }
 271 
 272   int stack_size = get_java_stacksize();
 273   pthread_t thr;
 274   pthread_attr_t thread_attr;
 275 
 276   pthread_attr_init(&thread_attr);
 277   pthread_attr_setstacksize(&thread_attr, stack_size);
 278 
 279   if (argc > 1 && strcmp(argv[1], "test_java_overflow") == 0) {
 280     printf("\nTesting JAVA_OVERFLOW\n");
 281 
 282     printf("Testing stack guard page behaviour for other thread\n");
 283 
 284     pthread_create (&thr, &thread_attr, run_java_overflow, NULL);
 285     pthread_join (thr, NULL);
 286 
 287     printf("Testing stack guard page behaviour for initial thread\n");
 288     run_java_overflow(NULL);
 289     // This test crash on error
 290     exit(0);
 291   }
 292 
 293   if (argc > 1 && strcmp(argv[1], "test_native_overflow") == 0) {
 294     printf("\nTesting NATIVE_OVERFLOW\n");
 295 
 296     printf("Testing stack guard page behaviour for other thread\n");
 297     pthread_create (&thr, &thread_attr, run_native_overflow, NULL);
 298     pthread_join (thr, NULL);
 299 
 300     printf("Testing stack guard page behaviour for initial thread\n");
 301     run_native_overflow(NULL);
 302 
 303     exit((_failures > 0) ? 1 : 0);
 304   }
 305 
 306   fprintf(stderr, "Test ERROR. Unknown parameter %s\n", ((argc > 1) ? argv[1] : "none"));
 307   usage();
 308   exit(7);
 309 }