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 /* Third party may overwrite these values. */
 112 #ifndef VENDOR
 113 #define VENDOR "Oracle Corporation"
 114 #endif
 115 #ifndef VENDOR_URL
 116 #define VENDOR_URL "http://java.oracle.com/"
 117 #endif
 118 #ifndef VENDOR_URL_BUG
 119 #define VENDOR_URL_BUG "http://bugreport.java.com/bugreport/"
 120 #endif
 121 
 122 #ifdef JAVA_SPECIFICATION_VENDOR /* Third party may NOT overwrite this. */
 123   #error "ERROR: No override of JAVA_SPECIFICATION_VENDOR is allowed"
 124 #else
 125   #define JAVA_SPECIFICATION_VENDOR "Oracle Corporation"
 126 #endif
 127 
 128 jobject fillI18nProps(JNIEnv *env, jobject props, char *baseKey,
 129                       char *platformDispVal, char *platformFmtVal,
 130                       jmethodID putID, jmethodID getPropID) {
 131     jstring jVMBaseVal = NULL;
 132 
 133     GETPROP(props, baseKey, jVMBaseVal);
 134     if (jVMBaseVal) {
 135         // user specified the base property.  there's nothing to do here.
 136         (*env)->DeleteLocalRef(env, jVMBaseVal);
 137     } else {
 138         char buf[64];
 139         jstring jVMVal = NULL;
 140         const char *baseVal = "";
 141 
 142         /* user.xxx base property */
 143         if (platformDispVal) {
 144             PUTPROP(props, baseKey, platformDispVal);
 145             baseVal = platformDispVal;
 146         }
 147 
 148         /* user.xxx.display property */
 149         jio_snprintf(buf, sizeof(buf), "%s.display", baseKey);
 150         GETPROP(props, buf, jVMVal);
 151         if (jVMVal == NULL) {
 152             if (platformDispVal && (strcmp(baseVal, platformDispVal) != 0)) {
 153                 PUTPROP(props, buf, platformDispVal);
 154             }
 155         } else {
 156             (*env)->DeleteLocalRef(env, jVMVal);
 157         }
 158 
 159         /* user.xxx.format property */
 160         jio_snprintf(buf, sizeof(buf), "%s.format", baseKey);
 161         GETPROP(props, buf, jVMVal);
 162         if (jVMVal == NULL) {
 163             if (platformFmtVal && (strcmp(baseVal, platformFmtVal) != 0)) {
 164                 PUTPROP(props, buf, platformFmtVal);
 165             }
 166         } else {
 167             (*env)->DeleteLocalRef(env, jVMVal);
 168         }
 169     }
 170 
 171     return NULL;
 172 }
 173 
 174 JNIEXPORT jobject JNICALL
 175 Java_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props)
 176 {
 177     char buf[128];
 178     java_props_t *sprops;
 179     jmethodID putID, removeID, getPropID;
 180     jobject ret = NULL;
 181     jstring jVMVal = NULL;
 182 
 183     if ((*env)->EnsureLocalCapacity(env, 50) < 0) {
 184         return 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      */
 251     PUTPROP(props, "user.language", sprops->language);
 252     if (sprops->script) {
 253         PUTPROP(props, "user.script", sprops->script);
 254     }
 255     if (sprops->country) {
 256         PUTPROP(props, "user.country", sprops->country);
 257     }
 258     if (sprops->variant) {
 259         PUTPROP(props, "user.variant", sprops->variant);
 260     }
 261     PUTPROP(props, "file.encoding", sprops->encoding);
 262     if (sprops->sun_stdout_encoding != NULL) {
 263         PUTPROP(props, "sun.stdout.encoding", sprops->sun_stdout_encoding);
 264     }
 265     if (sprops->sun_stderr_encoding != NULL) {
 266         PUTPROP(props, "sun.stderr.encoding", sprops->sun_stderr_encoding);
 267     }
 268 
 269     /* unicode_encoding specifies the default endianness */
 270     PUTPROP(props, "sun.io.unicode.encoding", sprops->unicode_encoding);
 271     PUTPROP(props, "sun.cpu.isalist",
 272             (sprops->cpu_isalist ? sprops->cpu_isalist : ""));
 273     PUTPROP(props, "sun.cpu.endian",  sprops->cpu_endian);
 274 
 275 
 276 #ifdef MACOSX
 277     /* Proxy setting properties */
 278     if (sprops->httpProxyEnabled) {
 279         PUTPROP(props, "http.proxyHost", sprops->httpHost);
 280         PUTPROP(props, "http.proxyPort", sprops->httpPort);
 281     }
 282 
 283     if (sprops->httpsProxyEnabled) {
 284         PUTPROP(props, "https.proxyHost", sprops->httpsHost);
 285         PUTPROP(props, "https.proxyPort", sprops->httpsPort);
 286     }
 287 
 288     if (sprops->ftpProxyEnabled) {
 289         PUTPROP(props, "ftp.proxyHost", sprops->ftpHost);
 290         PUTPROP(props, "ftp.proxyPort", sprops->ftpPort);
 291     }
 292 
 293     if (sprops->socksProxyEnabled) {
 294         PUTPROP(props, "socksProxyHost", sprops->socksHost);
 295         PUTPROP(props, "socksProxyPort", sprops->socksPort);
 296     }
 297 
 298     if (sprops->gopherProxyEnabled) {
 299         // The gopher client is different in that it expects an 'is this set?' flag that the others don't.
 300         PUTPROP(props, "gopherProxySet", "true");
 301         PUTPROP(props, "gopherProxyHost", sprops->gopherHost);
 302         PUTPROP(props, "gopherProxyPort", sprops->gopherPort);
 303     } else {
 304         PUTPROP(props, "gopherProxySet", "false");
 305     }
 306 
 307     // Mac OS X only has a single proxy exception list which applies
 308     // to all protocols
 309     if (sprops->exceptionList) {
 310         PUTPROP(props, "http.nonProxyHosts", sprops->exceptionList);
 311         PUTPROP(props, "ftp.nonProxyHosts", sprops->exceptionList);
 312         PUTPROP(props, "socksNonProxyHosts", sprops->exceptionList);
 313     }
 314 #endif
 315 
 316     /* !!! DO NOT call PUTPROP_ForPlatformNString before this line !!!
 317      * !!! I18n properties have not been set up yet !!!
 318      */
 319     InitializeEncoding(env, sprops->sun_jnu_encoding);
 320 
 321     /* Printing properties */
 322     /* Note: java.awt.printerjob is an implementation private property which
 323      * just happens to have a java.* name because it is referenced in
 324      * a java.awt class. It is the mechanism by which the implementation
 325      * finds the appropriate class in the JRE for the platform.
 326      * It is explicitly not designed to be overridden by clients as
 327      * a way of replacing the implementation class, and in any case
 328      * the mechanism by which the class is loaded is constrained to only
 329      * find and load classes that are part of the JRE.
 330      * This property may be removed if that mechanism is redesigned
 331      */
 332     PUTPROP(props, "java.awt.printerjob", sprops->printerJob);
 333 
 334     /* data model */
 335     if (sizeof(sprops) == 4) {
 336         sprops->data_model = "32";
 337     } else if (sizeof(sprops) == 8) {
 338         sprops->data_model = "64";
 339     } else {
 340         sprops->data_model = "unknown";
 341     }
 342     PUTPROP(props, "sun.arch.data.model",  \
 343                     sprops->data_model);
 344 
 345     /* patch level */
 346     PUTPROP(props, "sun.os.patch.level",  \
 347                     sprops->patch_level);
 348 
 349     /* Java2D properties */
 350     /* Note: java.awt.graphicsenv is an implementation private property which
 351      * just happens to have a java.* name because it is referenced in
 352      * a java.awt class. It is the mechanism by which the implementation
 353      * finds the appropriate class in the JRE for the platform.
 354      * It is explicitly not designed to be overridden by clients as
 355      * a way of replacing the implementation class, and in any case
 356      * the mechanism by which the class is loaded is constrained to only
 357      * find and load classes that are part of the JRE.
 358      * This property may be removed if that mechanism is redesigned
 359      */
 360     PUTPROP(props, "java.awt.graphicsenv", sprops->graphics_env);
 361     if (sprops->font_dir != NULL) {
 362         PUTPROP_ForPlatformNString(props,
 363                                    "sun.java2d.fontpath", sprops->font_dir);
 364     }
 365 
 366     PUTPROP_ForPlatformNString(props, "java.io.tmpdir", sprops->tmp_dir);
 367 
 368     PUTPROP_ForPlatformNString(props, "user.name", sprops->user_name);
 369     PUTPROP_ForPlatformNString(props, "user.home", sprops->user_home);
 370 
 371     PUTPROP(props, "user.timezone", sprops->timezone);
 372 
 373     PUTPROP_ForPlatformNString(props, "user.dir", sprops->user_dir);
 374 
 375     /* This is a sun. property as it is currently only set for Gnome and
 376      * Windows desktops.
 377      */
 378     if (sprops->desktop != NULL) {
 379         PUTPROP(props, "sun.desktop", sprops->desktop);
 380     }
 381 
 382     /*
 383      * unset "user.language", "user.script", "user.country", and "user.variant"
 384      * in order to tell whether the command line option "-DXXXX=YYYY" is
 385      * specified or not.  They will be reset in fillI18nProps() below.
 386      */
 387     REMOVEPROP(props, "user.language");
 388     REMOVEPROP(props, "user.script");
 389     REMOVEPROP(props, "user.country");
 390     REMOVEPROP(props, "user.variant");
 391     REMOVEPROP(props, "file.encoding");
 392 
 393     ret = JVM_InitProperties(env, props);
 394 
 395     /* reconstruct i18n related properties */
 396     fillI18nProps(env, props, "user.language", sprops->display_language,
 397         sprops->format_language, putID, getPropID);
 398     fillI18nProps(env, props, "user.script",
 399         sprops->display_script, sprops->format_script, putID, getPropID);
 400     fillI18nProps(env, props, "user.country",
 401         sprops->display_country, sprops->format_country, putID, getPropID);
 402     fillI18nProps(env, props, "user.variant",
 403         sprops->display_variant, sprops->format_variant, putID, getPropID);
 404     GETPROP(props, "file.encoding", jVMVal);
 405     if (jVMVal == NULL) {
 406 #ifdef MACOSX
 407         /*
 408          * Since sun_jnu_encoding is now hard-coded to UTF-8 on Mac, we don't
 409          * want to use it to overwrite file.encoding
 410          */
 411         PUTPROP(props, "file.encoding", sprops->encoding);
 412 #else
 413         PUTPROP(props, "file.encoding", sprops->sun_jnu_encoding);
 414 #endif
 415     } else {
 416         (*env)->DeleteLocalRef(env, jVMVal);
 417     }
 418 
 419     // Platform defined encoding properties override any on the command line
 420     PUTPROP(props, "sun.jnu.encoding", sprops->sun_jnu_encoding);
 421 
 422     return ret;
 423 }
 424 
 425 /*
 426  * The following three functions implement setter methods for
 427  * java.lang.System.{in, out, err}. They are natively implemented
 428  * because they violate the semantics of the language (i.e. set final
 429  * variable).
 430  */
 431 JNIEXPORT void JNICALL
 432 Java_java_lang_System_setIn0(JNIEnv *env, jclass cla, jobject stream)
 433 {
 434     jfieldID fid =
 435         (*env)->GetStaticFieldID(env,cla,"in","Ljava/io/InputStream;");
 436     if (fid == 0)
 437         return;
 438     (*env)->SetStaticObjectField(env,cla,fid,stream);
 439 }
 440 
 441 JNIEXPORT void JNICALL
 442 Java_java_lang_System_setOut0(JNIEnv *env, jclass cla, jobject stream)
 443 {
 444     jfieldID fid =
 445         (*env)->GetStaticFieldID(env,cla,"out","Ljava/io/PrintStream;");
 446     if (fid == 0)
 447         return;
 448     (*env)->SetStaticObjectField(env,cla,fid,stream);
 449 }
 450 
 451 JNIEXPORT void JNICALL
 452 Java_java_lang_System_setErr0(JNIEnv *env, jclass cla, jobject stream)
 453 {
 454     jfieldID fid =
 455         (*env)->GetStaticFieldID(env,cla,"err","Ljava/io/PrintStream;");
 456     if (fid == 0)
 457         return;
 458     (*env)->SetStaticObjectField(env,cla,fid,stream);
 459 }
 460 
 461 static void cpchars(jchar *dst, char *src, int n)
 462 {
 463     int i;
 464     for (i = 0; i < n; i++) {
 465         dst[i] = src[i];
 466     }
 467 }
 468 
 469 JNIEXPORT jstring JNICALL
 470 Java_java_lang_System_mapLibraryName(JNIEnv *env, jclass ign, jstring libname)
 471 {
 472     int len;
 473     int prefix_len = (int) strlen(JNI_LIB_PREFIX);
 474     int suffix_len = (int) strlen(JNI_LIB_SUFFIX);
 475 
 476     jchar chars[256];
 477     if (libname == NULL) {
 478         JNU_ThrowNullPointerException(env, 0);
 479         return NULL;
 480     }
 481     len = (*env)->GetStringLength(env, libname);
 482     if (len > 240) {
 483         JNU_ThrowIllegalArgumentException(env, "name too long");
 484         return NULL;
 485     }
 486     cpchars(chars, JNI_LIB_PREFIX, prefix_len);
 487     (*env)->GetStringRegion(env, libname, 0, len, chars + prefix_len);
 488     len += prefix_len;
 489     cpchars(chars + len, JNI_LIB_SUFFIX, suffix_len);
 490     len += suffix_len;
 491 
 492     return (*env)->NewString(env, chars, len);
 493 }