< prev index next >

test/hotspot/jtreg/runtime/StackGuardPages/exeinvoke.c

Print this page




  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 <alloca.h>
  37 #include <signal.h>
  38 #include <string.h>
  39 #include <sys/mman.h>
  40 #include <stdlib.h>
  41 #include <sys/ucontext.h>
  42 #include <setjmp.h>
  43 #include <unistd.h>
  44 #include <sys/syscall.h>
  45 #include <errno.h>
  46 
  47 #include <pthread.h>
  48 
  49 #define CLASS_PATH_OPT "-Djava.class.path="
  50 
  51 JavaVM* _jvm;
  52 
  53 static jmp_buf  context;
  54 
  55 static int _last_si_code = -1;


  74     .ss_size = SIGSTKSZ,
  75     .ss_flags = 0,
  76     .ss_sp = altstack
  77   };
  78 
  79   struct sigaction sa = {
  80     .sa_sigaction = handler,
  81     .sa_flags = SA_ONSTACK | SA_SIGINFO | SA_RESETHAND
  82   };
  83 
  84   _last_si_code = -1;
  85 
  86   sigaltstack(&ss, 0);
  87   sigemptyset(&sa.sa_mask);
  88   if (sigaction(SIGSEGV, &sa, NULL) == -1) {
  89     fprintf(stderr, "Test ERROR. Can't set sigaction (%d)\n", errno);
  90     exit(7);
  91   }
  92 }
  93 














  94 void *run_java_overflow (void *p) {
  95   JNIEnv *env;
  96   jclass class_id;
  97   jmethodID method_id;
  98   int res;
  99 
 100   res = (*_jvm)->AttachCurrentThread(_jvm, (void**)&env, NULL);
 101   if (res != JNI_OK) {
 102     fprintf(stderr, "Test ERROR. Can't attach to current thread\n");
 103     exit(7);
 104   }
 105 
 106   class_id = (*env)->FindClass (env, "DoOverflow");
 107   if (class_id == NULL) {
 108     fprintf(stderr, "Test ERROR. Can't load class DoOverflow\n");
 109     exit(7);
 110   }
 111 
 112   method_id = (*env)->GetStaticMethodID(env, class_id, "printIt", "()V");
 113   if (method_id == NULL) {


 237     fprintf(stderr, "Test ERROR. CLASSPATH is too long\n");
 238     exit(7);
 239   }
 240   snprintf(javaclasspathopt, sizeof(javaclasspathopt), "%s%s",
 241       CLASS_PATH_OPT, javaclasspath);
 242 
 243   options[0].optionString = "-Xint";
 244   options[1].optionString = "-Xss1M";
 245   options[2].optionString = javaclasspathopt;
 246 
 247   vm_args.version = JNI_VERSION_1_2;
 248   vm_args.ignoreUnrecognized = JNI_TRUE;
 249   vm_args.options = options;
 250   vm_args.nOptions = 3;
 251 
 252   if (JNI_CreateJavaVM (&_jvm, (void **)&env, &vm_args) < 0 ) {
 253     fprintf(stderr, "Test ERROR. Can't create JavaVM\n");
 254     exit(7);
 255   }
 256 

 257   pthread_t thr;




 258 
 259   if (argc > 1 && strcmp(argv[1], "test_java_overflow") == 0) {
 260     printf("\nTesting JAVA_OVERFLOW\n");
 261 
 262     printf("Testing stack guard page behaviour for other thread\n");
 263     pthread_create (&thr, NULL, run_java_overflow, NULL);

 264     pthread_join (thr, NULL);
 265 
 266     printf("Testing stack guard page behaviour for initial thread\n");
 267     run_java_overflow(NULL);
 268     // This test crash on error
 269     exit(0);
 270   }
 271 
 272   if (argc > 1 && strcmp(argv[1], "test_native_overflow") == 0) {
 273     printf("\nTesting NATIVE_OVERFLOW\n");
 274 
 275     printf("Testing stack guard page behaviour for other thread\n");
 276     pthread_create (&thr, NULL, run_native_overflow, NULL);
 277     pthread_join (thr, NULL);
 278 
 279     printf("Testing stack guard page behaviour for initial thread\n");
 280     run_native_overflow(NULL);
 281 
 282     exit((_failures > 0) ? 1 : 0);
 283   }
 284 
 285   fprintf(stderr, "Test ERROR. Unknown parameter %s\n", ((argc > 1) ? argv[1] : "none"));
 286   usage();
 287   exit(7);
 288 }


  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;


  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) {


 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 }
< prev index next >