1 /*
   2  * Copyright (c) 2003, 2018, 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 <stdlib.h>
  25 #include <stdio.h>
  26 #include <stdarg.h>
  27 #include <string.h>
  28 #include <ctype.h>
  29 
  30 /*************************************************************/
  31 #if (defined(WIN32) || defined(_WIN32))
  32 #include <windows.h>
  33 #else
  34 #include <unistd.h>
  35 #include <sys/time.h>
  36 #endif
  37 /*************************************************************/
  38 
  39 #include "jni.h"
  40 
  41 /*************************************************************/
  42 
  43 #include "nsk_tools.h"
  44 #include "jni_tools.h"
  45 
  46 /*************************************************************/
  47 
  48 extern "C" {
  49 
  50 /*************************************************************/
  51 
  52 int nsk_jni_check_exception(JNIEnv* jni, const char file[], int line)
  53 {
  54     jthrowable throwable;
  55 
  56     NSK_TRACE(throwable = jni->ExceptionOccurred());
  57     if (throwable != NULL) {
  58         nsk_lcomplain(file, line, "Exception in JNI call (cleared):\n");
  59         NSK_TRACE(jni->ExceptionDescribe());
  60         NSK_TRACE(jni->ExceptionClear());
  61         return NSK_TRUE;
  62     }
  63     return NSK_FALSE;
  64 }
  65 
  66 int nsk_jni_lverify(int positive, JNIEnv* jni, int status,
  67                         const char file[], int line, const char format[], ...)
  68 {
  69     int failure=0;
  70     int negative = !positive;
  71     va_list ap;
  72     va_start(ap,format);
  73 
  74     nsk_lvtrace(NSK_TRACE_AFTER,file,line,format,ap);
  75     if (status == negative) {
  76         nsk_lvcomplain(file,line,format,ap);
  77         nsk_printf("#   verified JNI assertion is FALSE\n");
  78         failure=1;
  79     }
  80 
  81     failure = nsk_jni_check_exception(jni, file, line) || failure;
  82 
  83     va_end(ap);
  84     return !failure;
  85 }
  86 
  87 int nsk_jni_lverify_void(JNIEnv* jni, const char file[], int line,
  88                             const char format[], ...)
  89 {
  90     int failure=0;
  91     va_list ap;
  92     va_start(ap,format);
  93 
  94     nsk_lvtrace(NSK_TRACE_AFTER,file,line,format,ap);
  95     failure = nsk_jni_check_exception(jni, file, line);
  96 
  97     if (failure)
  98         nsk_lvcomplain(file,line,format,ap);
  99 
 100     va_end(ap);
 101     return !failure;
 102 }
 103 
 104 char *jlong_to_string(jlong value, char *string) {
 105     char buffer[32];
 106     char *pbuf, *pstr;
 107 
 108     pstr = string;
 109     if (value == 0) {
 110         *pstr++ = '0';
 111     } else {
 112         if (value < 0) {
 113             *pstr++ = '-';
 114             value = -value;
 115         }
 116         pbuf = buffer;
 117         while (value != 0) {
 118             *pbuf++ = '0' + (char)(value % 10);
 119             value = value / 10;
 120         }
 121         while (pbuf != buffer) {
 122             *pstr++ = *--pbuf;
 123         }
 124     }
 125     *pstr = '\0';
 126 
 127     return string;
 128 }
 129 
 130 char *julong_to_string(julong value, char *string) {
 131     char buffer[32];
 132     char *pbuf, *pstr;
 133 
 134     pstr = string;
 135     if (value == 0) {
 136         *pstr++ = '0';
 137     } else {
 138         pbuf = buffer;
 139         while (value != 0) {
 140             *pbuf++ = '0' + (char)(value % 10);
 141             value = value / 10;
 142         }
 143         while (pbuf != buffer) {
 144             *pstr++ = *--pbuf;
 145         }
 146     }
 147     *pstr = '\0';
 148 
 149     return string;
 150 }
 151 
 152 void mssleep(long millis) {
 153 #if (defined(WIN32) || defined(_WIN32))
 154    Sleep(millis);
 155 #else
 156    /* Using select for portable sleep */
 157    /* Not using usleep because of it's possible interaction with SIGALRM */
 158    struct timeval timeout;
 159    timeout.tv_sec = millis / 1000;
 160    timeout.tv_usec = (millis % 1000) * 1000;
 161    select(0, NULL, NULL, NULL, &timeout);
 162 #endif
 163 }
 164 
 165 void
 166 jni_print_vmargs(JavaVMInitArgs vmargs)
 167 {
 168    int i = 0;
 169 
 170    printf("JavaVMInitArgs:\n");
 171    printf(" version = %d\n", vmargs.version);
 172    printf(" ignoreUnrecognized = %d\n", vmargs.ignoreUnrecognized);
 173 
 174    printf(" vmargs.nOptions = %d\n", vmargs.nOptions);
 175    for (i = 0; i < vmargs.nOptions; i++) {
 176       printf("   options[%d].optionString = %s\n", i, vmargs.options[i].optionString);
 177       printf("   options[%d].extraInfo = %p\n", i, vmargs.options[i].extraInfo);
 178    }
 179 }
 180 
 181 JavaVMOption*
 182 jni_create_vmoptions(int size, char *args[], int argsCnt)
 183 {
 184    int i;
 185    JavaVMOption *options = NULL;
 186 
 187    if (size <= 0)
 188       return options;
 189 
 190    options = (JavaVMOption*)calloc(size, sizeof(JavaVMOption));
 191 
 192    for (i=0; i<argsCnt; i++)
 193       options[i].optionString = args[i];
 194 
 195    return options;
 196 }
 197 
 198 /*************************************************************/
 199 
 200 }