1 /*
   2  * Copyright (c) 1994, 2017, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <string.h>
  27 
  28 #include "jni.h"
  29 #include "jni_util.h"
  30 #include "jvm.h"
  31 #include "java_props.h"
  32 
  33 #include "java_lang_System.h"
  34 
  35 #define OBJ "Ljava/lang/Object;"
  36 
  37 /* Only register the performance-critical methods */
  38 static JNINativeMethod methods[] = {
  39     {"currentTimeMillis", "()J",              (void *)&JVM_CurrentTimeMillis},
  40     {"nanoTime",          "()J",              (void *)&JVM_NanoTime},
  41     {"arraycopy",     "(" OBJ "I" OBJ "II)V", (void *)&JVM_ArrayCopy},
  42 };
  43 
  44 #undef OBJ
  45 
  46 JNIEXPORT void JNICALL
  47 Java_java_lang_System_registerNatives(JNIEnv *env, jclass cls)
  48 {
  49     (*env)->RegisterNatives(env, cls,
  50                             methods, sizeof(methods)/sizeof(methods[0]));
  51 }
  52 
  53 JNIEXPORT jint JNICALL
  54 Java_java_lang_System_identityHashCode(JNIEnv *env, jobject this, jobject x)
  55 {
  56     return JVM_IHashCode(env, x);
  57 }
  58 
  59 #define PUTPROP(props, key, val)                                     \
  60     if (1) {                                                         \
  61         jstring jkey, jval;                                          \
  62         jobject r;                                                   \
  63         jkey = (*env)->NewStringUTF(env, key);                       \
  64         if (jkey == NULL) return NULL;                               \
  65         jval = (*env)->NewStringUTF(env, val);                       \
  66         if (jval == NULL) return NULL;                               \
  67         r = (*env)->CallObjectMethod(env, props, putID, jkey, jval); \
  68         if ((*env)->ExceptionOccurred(env)) return NULL;             \
  69         (*env)->DeleteLocalRef(env, jkey);                           \
  70         (*env)->DeleteLocalRef(env, jval);                           \
  71         (*env)->DeleteLocalRef(env, r);                              \
  72     } else ((void) 0)
  73 
  74 /*  "key" is a char type string with only ASCII character in it.
  75     "val" is a nchar (typedefed in java_props.h) type string  */
  76 
  77 #define PUTPROP_ForPlatformNString(props, key, val)                  \
  78     if (1) {                                                         \
  79         jstring jkey, jval;                                          \
  80         jobject r;                                                   \
  81         jkey = (*env)->NewStringUTF(env, key);                       \
  82         if (jkey == NULL) return NULL;                               \
  83         jval = GetStringPlatform(env, val);                          \
  84         if (jval == NULL) return NULL;                               \
  85         r = (*env)->CallObjectMethod(env, props, putID, jkey, jval); \
  86         if ((*env)->ExceptionOccurred(env)) return NULL;             \
  87         (*env)->DeleteLocalRef(env, jkey);                           \
  88         (*env)->DeleteLocalRef(env, jval);                           \
  89         (*env)->DeleteLocalRef(env, r);                              \
  90     } else ((void) 0)
  91 #define REMOVEPROP(props, key)                                    \
  92     if (1) {                                                      \
  93         jstring jkey;                                             \
  94         jobject r;                                                \
  95         jkey = JNU_NewStringPlatform(env, key);                   \
  96         if (jkey == NULL) return NULL;                            \
  97         r = (*env)->CallObjectMethod(env, props, removeID, jkey); \
  98         if ((*env)->ExceptionOccurred(env)) return NULL;          \
  99         (*env)->DeleteLocalRef(env, jkey);                        \
 100         (*env)->DeleteLocalRef(env, r);                           \
 101     } else ((void) 0)
 102 #define GETPROP(props, key, jret)                                     \
 103     if (1) {                                                          \
 104         jstring jkey = JNU_NewStringPlatform(env, key);               \
 105         if (jkey == NULL) return NULL;                                \
 106         jret = (*env)->CallObjectMethod(env, props, getPropID, jkey); \
 107         if ((*env)->ExceptionOccurred(env)) return NULL;              \
 108         (*env)->DeleteLocalRef(env, jkey);                            \
 109     } else ((void) 0)
 110 
 111 #ifndef VENDOR /* Third party may overwrite this. */
 112 #define VENDOR "Oracle Corporation"
 113 #define VENDOR_URL "http://java.oracle.com/"
 114 #define VENDOR_URL_BUG "http://bugreport.java.com/bugreport/"
 115 #endif
 116 
 117 #ifdef JAVA_SPECIFICATION_VENDOR /* Third party may NOT overwrite this. */
 118   #error "ERROR: No override of JAVA_SPECIFICATION_VENDOR is allowed"
 119 #else
 120   #define JAVA_SPECIFICATION_VENDOR "Oracle Corporation"
 121 #endif
 122 
 123 static int fmtdefault; // boolean value
 124 jobject fillI18nProps(JNIEnv *env, jobject props, char *baseKey,
 125                       char *platformDispVal, char *platformFmtVal,
 126                       jmethodID putID, jmethodID getPropID) {
 127     jstring jVMBaseVal = NULL;
 128 
 129     GETPROP(props, baseKey, jVMBaseVal);
 130     if (jVMBaseVal) {
 131         // user specified the base property.  there's nothing to do here.
 132         (*env)->DeleteLocalRef(env, jVMBaseVal);
 133     } else {
 134         char buf[64];
 135         jstring jVMVal = NULL;
 136         const char *baseVal = "";
 137 
 138         /* user.xxx base property */
 139         if (fmtdefault) {
 140             if (platformFmtVal) {
 141                 PUTPROP(props, baseKey, platformFmtVal);
 142                 baseVal = platformFmtVal;
 143             }
 144         } else {
 145             if (platformDispVal) {
 146                 PUTPROP(props, baseKey, platformDispVal);
 147                 baseVal = platformDispVal;
 148             }
 149         }
 150 
 151         /* user.xxx.display property */
 152         jio_snprintf(buf, sizeof(buf), "%s.display", baseKey);
 153         GETPROP(props, buf, jVMVal);
 154         if (jVMVal == NULL) {
 155             if (platformDispVal && (strcmp(baseVal, platformDispVal) != 0)) {
 156                 PUTPROP(props, buf, platformDispVal);
 157             }
 158         } else {
 159             (*env)->DeleteLocalRef(env, jVMVal);
 160         }
 161 
 162         /* user.xxx.format property */
 163         jio_snprintf(buf, sizeof(buf), "%s.format", baseKey);
 164         GETPROP(props, buf, jVMVal);
 165         if (jVMVal == NULL) {
 166             if (platformFmtVal && (strcmp(baseVal, platformFmtVal) != 0)) {
 167                 PUTPROP(props, buf, platformFmtVal);
 168             }
 169         } else {
 170             (*env)->DeleteLocalRef(env, jVMVal);
 171         }
 172     }
 173 
 174     return NULL;
 175 }
 176 
 177 JNIEXPORT jobject JNICALL
 178 Java_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props)
 179 {
 180     char buf[128];
 181     java_props_t *sprops;
 182     jmethodID putID, removeID, getPropID;
 183     jobject ret = NULL;
 184     jstring jVMVal = NULL;
 185 
 186     sprops = GetJavaProperties(env);
 187     CHECK_NULL_RETURN(sprops, NULL);
 188 
 189     putID = (*env)->GetMethodID(env,
 190                                 (*env)->GetObjectClass(env, props),
 191                                 "put",
 192             "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
 193     CHECK_NULL_RETURN(putID, NULL);
 194 
 195     removeID = (*env)->GetMethodID(env,
 196                                    (*env)->GetObjectClass(env, props),
 197                                    "remove",
 198             "(Ljava/lang/Object;)Ljava/lang/Object;");
 199     CHECK_NULL_RETURN(removeID, NULL);
 200 
 201     getPropID = (*env)->GetMethodID(env,
 202                                     (*env)->GetObjectClass(env, props),
 203                                     "getProperty",
 204             "(Ljava/lang/String;)Ljava/lang/String;");
 205     CHECK_NULL_RETURN(getPropID, NULL);
 206 
 207     PUTPROP(props, "java.specification.version",
 208             VERSION_SPECIFICATION);
 209     PUTPROP(props, "java.specification.name",
 210             "Java Platform API Specification");
 211     PUTPROP(props, "java.specification.vendor",
 212             JAVA_SPECIFICATION_VENDOR);
 213 
 214     PUTPROP(props, "java.version", VERSION_SHORT);
 215     PUTPROP(props, "java.vendor", VENDOR);
 216     PUTPROP(props, "java.vendor.url", VENDOR_URL);
 217     PUTPROP(props, "java.vendor.url.bug", VENDOR_URL_BUG);
 218 
 219     jio_snprintf(buf, sizeof(buf), "%d.%d", JVM_CLASSFILE_MAJOR_VERSION,
 220                                             JVM_CLASSFILE_MINOR_VERSION);
 221     PUTPROP(props, "java.class.version", buf);
 222 
 223     if (sprops->awt_toolkit) {
 224         PUTPROP(props, "awt.toolkit", sprops->awt_toolkit);
 225     }
 226 #ifdef MACOSX
 227     if (sprops->awt_headless) {
 228         PUTPROP(props, "java.awt.headless", sprops->awt_headless);
 229     }
 230 #endif
 231 
 232     /* os properties */
 233     PUTPROP(props, "os.name", sprops->os_name);
 234     PUTPROP(props, "os.version", sprops->os_version);
 235     PUTPROP(props, "os.arch", sprops->os_arch);
 236 
 237 #ifdef JDK_ARCH_ABI_PROP_NAME
 238     PUTPROP(props, "sun.arch.abi", sprops->sun_arch_abi);
 239 #endif
 240 
 241     /* file system properties */
 242     PUTPROP(props, "file.separator", sprops->file_separator);
 243     PUTPROP(props, "path.separator", sprops->path_separator);
 244     PUTPROP(props, "line.separator", sprops->line_separator);
 245 
 246     /*
 247      *  user.language
 248      *  user.script, user.country, user.variant (if user's environment specifies them)
 249      *  file.encoding
 250      *  file.encoding.pkg
 251      */
 252     PUTPROP(props, "user.language", sprops->language);
 253     if (sprops->script) {
 254         PUTPROP(props, "user.script", sprops->script);
 255     }
 256     if (sprops->country) {
 257         PUTPROP(props, "user.country", sprops->country);
 258     }
 259     if (sprops->variant) {
 260         PUTPROP(props, "user.variant", sprops->variant);
 261     }
 262     PUTPROP(props, "file.encoding", sprops->encoding);
 263     PUTPROP(props, "sun.jnu.encoding", sprops->sun_jnu_encoding);
 264     if (sprops->sun_stdout_encoding != NULL) {
 265         PUTPROP(props, "sun.stdout.encoding", sprops->sun_stdout_encoding);
 266     }
 267     if (sprops->sun_stderr_encoding != NULL) {
 268         PUTPROP(props, "sun.stderr.encoding", sprops->sun_stderr_encoding);
 269     }
 270     PUTPROP(props, "file.encoding.pkg", "sun.io");
 271 
 272     /* unicode_encoding specifies the default endianness */
 273     PUTPROP(props, "sun.io.unicode.encoding", sprops->unicode_encoding);
 274     PUTPROP(props, "sun.cpu.isalist",
 275             (sprops->cpu_isalist ? sprops->cpu_isalist : ""));
 276     PUTPROP(props, "sun.cpu.endian",  sprops->cpu_endian);
 277 
 278 
 279 #ifdef MACOSX
 280     /* Proxy setting properties */
 281     if (sprops->httpProxyEnabled) {
 282         PUTPROP(props, "http.proxyHost", sprops->httpHost);
 283         PUTPROP(props, "http.proxyPort", sprops->httpPort);
 284     }
 285 
 286     if (sprops->httpsProxyEnabled) {
 287         PUTPROP(props, "https.proxyHost", sprops->httpsHost);
 288         PUTPROP(props, "https.proxyPort", sprops->httpsPort);
 289     }
 290 
 291     if (sprops->ftpProxyEnabled) {
 292         PUTPROP(props, "ftp.proxyHost", sprops->ftpHost);
 293         PUTPROP(props, "ftp.proxyPort", sprops->ftpPort);
 294     }
 295 
 296     if (sprops->socksProxyEnabled) {
 297         PUTPROP(props, "socksProxyHost", sprops->socksHost);
 298         PUTPROP(props, "socksProxyPort", sprops->socksPort);
 299     }
 300 
 301     if (sprops->gopherProxyEnabled) {
 302         // The gopher client is different in that it expects an 'is this set?' flag that the others don't.
 303         PUTPROP(props, "gopherProxySet", "true");
 304         PUTPROP(props, "gopherProxyHost", sprops->gopherHost);
 305         PUTPROP(props, "gopherProxyPort", sprops->gopherPort);
 306     } else {
 307         PUTPROP(props, "gopherProxySet", "false");
 308     }
 309 
 310     // Mac OS X only has a single proxy exception list which applies
 311     // to all protocols
 312     if (sprops->exceptionList) {
 313         PUTPROP(props, "http.nonProxyHosts", sprops->exceptionList);
 314         PUTPROP(props, "ftp.nonProxyHosts", sprops->exceptionList);
 315         PUTPROP(props, "socksNonProxyHosts", sprops->exceptionList);
 316     }
 317 #endif
 318 
 319     /* !!! DO NOT call PUTPROP_ForPlatformNString before this line !!!
 320      * !!! I18n properties have not been set up yet !!!
 321      */
 322 
 323     /* Printing properties */
 324     /* Note: java.awt.printerjob is an implementation private property which
 325      * just happens to have a java.* name because it is referenced in
 326      * a java.awt class. It is the mechanism by which the implementation
 327      * finds the appropriate class in the JRE for the platform.
 328      * It is explicitly not designed to be overridden by clients as
 329      * a way of replacing the implementation class, and in any case
 330      * the mechanism by which the class is loaded is constrained to only
 331      * find and load classes that are part of the JRE.
 332      * This property may be removed if that mechanism is redesigned
 333      */
 334     PUTPROP(props, "java.awt.printerjob", sprops->printerJob);
 335 
 336     /* data model */
 337     if (sizeof(sprops) == 4) {
 338         sprops->data_model = "32";
 339     } else if (sizeof(sprops) == 8) {
 340         sprops->data_model = "64";
 341     } else {
 342         sprops->data_model = "unknown";
 343     }
 344     PUTPROP(props, "sun.arch.data.model",  \
 345                     sprops->data_model);
 346 
 347     /* patch level */
 348     PUTPROP(props, "sun.os.patch.level",  \
 349                     sprops->patch_level);
 350 
 351     /* Java2D properties */
 352     /* Note: java.awt.graphicsenv is an implementation private property which
 353      * just happens to have a java.* name because it is referenced in
 354      * a java.awt class. It is the mechanism by which the implementation
 355      * finds the appropriate class in the JRE for the platform.
 356      * It is explicitly not designed to be overridden by clients as
 357      * a way of replacing the implementation class, and in any case
 358      * the mechanism by which the class is loaded is constrained to only
 359      * find and load classes that are part of the JRE.
 360      * This property may be removed if that mechanism is redesigned
 361      */
 362     PUTPROP(props, "java.awt.graphicsenv", sprops->graphics_env);
 363     if (sprops->font_dir != NULL) {
 364         PUTPROP_ForPlatformNString(props,
 365                                    "sun.java2d.fontpath", sprops->font_dir);
 366     }
 367 
 368     PUTPROP_ForPlatformNString(props, "java.io.tmpdir", sprops->tmp_dir);
 369 
 370     PUTPROP_ForPlatformNString(props, "user.name", sprops->user_name);
 371     PUTPROP_ForPlatformNString(props, "user.home", sprops->user_home);
 372 
 373     PUTPROP(props, "user.timezone", sprops->timezone);
 374 
 375     PUTPROP_ForPlatformNString(props, "user.dir", sprops->user_dir);
 376 
 377     /* This is a sun. property as it is currently only set for Gnome and
 378      * Windows desktops.
 379      */
 380     if (sprops->desktop != NULL) {
 381         PUTPROP(props, "sun.desktop", sprops->desktop);
 382     }
 383 
 384     /*
 385      * unset "user.language", "user.script", "user.country", and "user.variant"
 386      * in order to tell whether the command line option "-DXXXX=YYYY" is
 387      * specified or not.  They will be reset in fillI18nProps() below.
 388      */
 389     REMOVEPROP(props, "user.language");
 390     REMOVEPROP(props, "user.script");
 391     REMOVEPROP(props, "user.country");
 392     REMOVEPROP(props, "user.variant");
 393     REMOVEPROP(props, "file.encoding");
 394 
 395     ret = JVM_InitProperties(env, props);
 396 
 397     /* Check the compatibility flag */
 398     GETPROP(props, "sun.locale.formatasdefault", jVMVal);
 399     if (jVMVal) {
 400         const char * val = (*env)->GetStringUTFChars(env, jVMVal, 0);
 401         CHECK_NULL_RETURN(val, NULL);
 402         fmtdefault = !strcmp(val, "true");
 403         (*env)->ReleaseStringUTFChars(env, jVMVal, val);
 404         (*env)->DeleteLocalRef(env, jVMVal);
 405     }
 406 
 407     /* reconstruct i18n related properties */
 408     fillI18nProps(env, props, "user.language", sprops->display_language,
 409         sprops->format_language, putID, getPropID);
 410     fillI18nProps(env, props, "user.script",
 411         sprops->display_script, sprops->format_script, putID, getPropID);
 412     fillI18nProps(env, props, "user.country",
 413         sprops->display_country, sprops->format_country, putID, getPropID);
 414     fillI18nProps(env, props, "user.variant",
 415         sprops->display_variant, sprops->format_variant, putID, getPropID);
 416     GETPROP(props, "file.encoding", jVMVal);
 417     if (jVMVal == NULL) {
 418 #ifdef MACOSX
 419         /*
 420          * Since sun_jnu_encoding is now hard-coded to UTF-8 on Mac, we don't
 421          * want to use it to overwrite file.encoding
 422          */
 423         PUTPROP(props, "file.encoding", sprops->encoding);
 424 #else
 425         if (fmtdefault) {
 426             PUTPROP(props, "file.encoding", sprops->encoding);
 427         } else {
 428             PUTPROP(props, "file.encoding", sprops->sun_jnu_encoding);
 429         }
 430 #endif
 431     } else {
 432         (*env)->DeleteLocalRef(env, jVMVal);
 433     }
 434 
 435     return ret;
 436 }
 437 
 438 /*
 439  * The following three functions implement setter methods for
 440  * java.lang.System.{in, out, err}. They are natively implemented
 441  * because they violate the semantics of the language (i.e. set final
 442  * variable).
 443  */
 444 JNIEXPORT void JNICALL
 445 Java_java_lang_System_setIn0(JNIEnv *env, jclass cla, jobject stream)
 446 {
 447     jfieldID fid =
 448         (*env)->GetStaticFieldID(env,cla,"in","Ljava/io/InputStream;");
 449     if (fid == 0)
 450         return;
 451     (*env)->SetStaticObjectField(env,cla,fid,stream);
 452 }
 453 
 454 JNIEXPORT void JNICALL
 455 Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)
 456 {
 457     jfieldID fid =
 458         (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");
 459     if (fid == 0)
 460         return;
 461     (*env)->SetStaticObjectField(env,cla,fid,stream);
 462 }
 463 
 464 JNIEXPORT void JNICALL
 465 Java_java_lang_System_setErr0(JNIEnv *env, jclass cla, jobject stream)
 466 {
 467     jfieldID fid =
 468         (*env)->GetStaticFieldID(env,cla,"err","Ljava/io/PrintStream;");
 469     if (fid == 0)
 470         return;
 471     (*env)->SetStaticObjectField(env,cla,fid,stream);
 472 }
 473 
 474 static void cpchars(jchar *dst, char *src, int n)
 475 {
 476     int i;
 477     for (i = 0; i < n; i++) {
 478         dst[i] = src[i];
 479     }
 480 }
 481 
 482 JNIEXPORT jstring JNICALL
 483 Java_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname)
 484 {
 485     int len;
 486     int prefix_len = (int) strlen(JNI_LIB_PREFIX);
 487     int suffix_len = (int) strlen(JNI_LIB_SUFFIX);
 488 
 489     jchar chars[256];
 490     if (libname == NULL) {
 491         JNU_ThrowNullPointerException(env, 0);
 492         return NULL;
 493     }
 494     len = (*env)->GetStringLength(env, libname);
 495     if (len > 240) {
 496         JNU_ThrowIllegalArgumentException(env, "name too long");
 497         return NULL;
 498     }
 499     cpchars(chars, JNI_LIB_PREFIX, prefix_len);
 500     (*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len);
 501     len += prefix_len;
 502     cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len);
 503     len += suffix_len;
 504 
 505     return (*env)->NewString(env, chars, len);
 506 }