< prev index next >

src/share/vm/prims/jni.cpp

Print this page


   1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.


  25 
  26 #include "precompiled.hpp"
  27 #include "ci/ciReplay.hpp"
  28 #include "classfile/altHashing.hpp"
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/javaClasses.hpp"
  31 #include "classfile/symbolTable.hpp"
  32 #include "classfile/systemDictionary.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "interpreter/linkResolver.hpp"
  35 #include "jfr/jfrEvents.hpp"
  36 #include "jfr/support/jfrThreadId.hpp"
  37 #include "utilities/macros.hpp"
  38 #include "utilities/ostream.hpp"
  39 #if INCLUDE_ALL_GCS
  40 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
  41 #endif // INCLUDE_ALL_GCS
  42 #include "memory/allocation.hpp"
  43 #include "memory/allocation.inline.hpp"
  44 #include "memory/gcLocker.inline.hpp"

  45 #include "memory/oopFactory.hpp"
  46 #include "memory/universe.inline.hpp"
  47 #include "oops/instanceKlass.hpp"
  48 #include "oops/instanceOop.hpp"
  49 #include "oops/markOop.hpp"
  50 #include "oops/method.hpp"
  51 #include "oops/objArrayKlass.hpp"
  52 #include "oops/objArrayOop.hpp"
  53 #include "oops/oop.inline.hpp"
  54 #include "oops/symbol.hpp"
  55 #include "oops/typeArrayKlass.hpp"
  56 #include "oops/typeArrayOop.hpp"
  57 #include "prims/jni.h"
  58 #include "prims/jniCheck.hpp"
  59 #include "prims/jniExport.hpp"
  60 #include "prims/jniFastGetField.hpp"
  61 #include "prims/jvm.h"
  62 #include "prims/jvm_misc.hpp"
  63 #include "prims/jvmtiExport.hpp"
  64 #include "prims/jvmtiThreadState.hpp"


4301 
4302 
4303 JNI_ENTRY(void, jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode))
4304   JNIWrapper("ReleasePrimitiveArrayCritical");
4305 #ifndef USDT2
4306   DTRACE_PROBE4(hotspot_jni, ReleasePrimitiveArrayCritical__entry, env, array, carray, mode);
4307 #else /* USDT2 */
4308   HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_ENTRY(
4309                                                   env, array, carray, mode);
4310 #endif /* USDT2 */
4311   // The array, carray and mode arguments are ignored
4312   unlock_gc_or_unpin_object(thread, array);
4313 #ifndef USDT2
4314   DTRACE_PROBE(hotspot_jni, ReleasePrimitiveArrayCritical__return);
4315 #else /* USDT2 */
4316 HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_RETURN(
4317 );
4318 #endif /* USDT2 */
4319 JNI_END
4320 



























4321 
4322 JNI_ENTRY(const jchar*, jni_GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy))
4323   JNIWrapper("GetStringCritical");
4324 #ifndef USDT2
4325   DTRACE_PROBE3(hotspot_jni, GetStringCritical__entry, env, string, isCopy);
4326 #else /* USDT2 */
4327   HOTSPOT_JNI_GETSTRINGCRITICAL_ENTRY(
4328                                       env, string, (uintptr_t *) isCopy);
4329 #endif /* USDT2 */
4330   if (isCopy != NULL) {
4331     *isCopy = JNI_FALSE;
4332   }
4333   oop s = lock_gc_or_pin_object(thread, string);
4334   int s_len = java_lang_String::length(s);
4335   typeArrayOop s_value = java_lang_String::value(s);
4336   int s_offset = java_lang_String::offset(s);
4337   const jchar* ret;
4338   if (s_len > 0) {
4339     ret = s_value->char_at_addr(s_offset);


4340   } else {

4341     ret = (jchar*) s_value->base(T_CHAR);

4342   }
4343 #ifndef USDT2
4344   DTRACE_PROBE1(hotspot_jni, GetStringCritical__return, ret);
4345 #else /* USDT2 */
4346  HOTSPOT_JNI_GETSTRINGCRITICAL_RETURN(
4347                                       (uint16_t *) ret);
4348 #endif /* USDT2 */
4349   return ret;
4350 JNI_END
4351 
4352 
4353 JNI_ENTRY(void, jni_ReleaseStringCritical(JNIEnv *env, jstring str, const jchar *chars))
4354   JNIWrapper("ReleaseStringCritical");
4355 #ifndef USDT2
4356   DTRACE_PROBE3(hotspot_jni, ReleaseStringCritical__entry, env, str, chars);
4357 #else /* USDT2 */
4358   HOTSPOT_JNI_RELEASESTRINGCRITICAL_ENTRY(
4359                                           env, str, (uint16_t *) chars);
4360 #endif /* USDT2 */
4361   // The str and chars arguments are ignored
4362   unlock_gc_or_unpin_object(thread, str);







4363 #ifndef USDT2
4364   DTRACE_PROBE(hotspot_jni, ReleaseStringCritical__return);
4365 #else /* USDT2 */
4366 HOTSPOT_JNI_RELEASESTRINGCRITICAL_RETURN(
4367 );
4368 #endif /* USDT2 */
4369 JNI_END
4370 
4371 
4372 JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref))
4373   JNIWrapper("jni_NewWeakGlobalRef");
4374 #ifndef USDT2
4375   DTRACE_PROBE2(hotspot_jni, NewWeakGlobalRef__entry, env, ref);
4376 #else /* USDT2 */
4377  HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(
4378                                     env, ref);
4379 #endif /* USDT2 */
4380   Handle ref_handle(thread, JNIHandles::resolve(ref));
4381   jweak ret = JNIHandles::make_weak_global(ref_handle);
4382 #ifndef USDT2


   1 /*
   2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.


  25 
  26 #include "precompiled.hpp"
  27 #include "ci/ciReplay.hpp"
  28 #include "classfile/altHashing.hpp"
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/javaClasses.hpp"
  31 #include "classfile/symbolTable.hpp"
  32 #include "classfile/systemDictionary.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "interpreter/linkResolver.hpp"
  35 #include "jfr/jfrEvents.hpp"
  36 #include "jfr/support/jfrThreadId.hpp"
  37 #include "utilities/macros.hpp"
  38 #include "utilities/ostream.hpp"
  39 #if INCLUDE_ALL_GCS
  40 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
  41 #endif // INCLUDE_ALL_GCS
  42 #include "memory/allocation.hpp"
  43 #include "memory/allocation.inline.hpp"
  44 #include "memory/gcLocker.inline.hpp"
  45 #include "gc_implementation/shenandoah/shenandoahStringDedup.hpp"
  46 #include "memory/oopFactory.hpp"
  47 #include "memory/universe.inline.hpp"
  48 #include "oops/instanceKlass.hpp"
  49 #include "oops/instanceOop.hpp"
  50 #include "oops/markOop.hpp"
  51 #include "oops/method.hpp"
  52 #include "oops/objArrayKlass.hpp"
  53 #include "oops/objArrayOop.hpp"
  54 #include "oops/oop.inline.hpp"
  55 #include "oops/symbol.hpp"
  56 #include "oops/typeArrayKlass.hpp"
  57 #include "oops/typeArrayOop.hpp"
  58 #include "prims/jni.h"
  59 #include "prims/jniCheck.hpp"
  60 #include "prims/jniExport.hpp"
  61 #include "prims/jniFastGetField.hpp"
  62 #include "prims/jvm.h"
  63 #include "prims/jvm_misc.hpp"
  64 #include "prims/jvmtiExport.hpp"
  65 #include "prims/jvmtiThreadState.hpp"


4302 
4303 
4304 JNI_ENTRY(void, jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode))
4305   JNIWrapper("ReleasePrimitiveArrayCritical");
4306 #ifndef USDT2
4307   DTRACE_PROBE4(hotspot_jni, ReleasePrimitiveArrayCritical__entry, env, array, carray, mode);
4308 #else /* USDT2 */
4309   HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_ENTRY(
4310                                                   env, array, carray, mode);
4311 #endif /* USDT2 */
4312   // The array, carray and mode arguments are ignored
4313   unlock_gc_or_unpin_object(thread, array);
4314 #ifndef USDT2
4315   DTRACE_PROBE(hotspot_jni, ReleasePrimitiveArrayCritical__return);
4316 #else /* USDT2 */
4317 HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_RETURN(
4318 );
4319 #endif /* USDT2 */
4320 JNI_END
4321 
4322 // If a copy of string value should be returned instead
4323 static bool should_copy_string_value() {
4324   // To prevent deduplication from replacing the value array while setting up or in
4325   // the critical section. That would lead to the release operation
4326   // unpinning the wrong object.
4327   return Universe::heap()->supports_object_pinning() && ShenandoahStringDedup::is_enabled();
4328 }
4329 
4330 static typeArrayOop lock_gc_or_pin_string_value(JavaThread* thread, oop str) {
4331   if (Universe::heap()->supports_object_pinning()) {
4332     typeArrayOop s_value = java_lang_String::value(str);
4333     return (typeArrayOop) Universe::heap()->pin_object(thread, s_value);
4334   } else {
4335     Handle h(thread, str);      // Handlize across potential safepoint.
4336     GC_locker::lock_critical(thread);
4337     return java_lang_String::value(h());
4338   }
4339 }
4340 
4341 static void unlock_gc_or_unpin_string_value(JavaThread* thread, oop str) {
4342   if (Universe::heap()->supports_object_pinning()) {
4343     typeArrayOop s_value = java_lang_String::value(str);
4344     Universe::heap()->unpin_object(thread, s_value);
4345   } else {
4346     GC_locker::unlock_critical(thread);
4347   }
4348 }
4349 
4350 JNI_ENTRY(const jchar*, jni_GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy))
4351   JNIWrapper("GetStringCritical");
4352 #ifndef USDT2
4353   DTRACE_PROBE3(hotspot_jni, GetStringCritical__entry, env, string, isCopy);
4354 #else /* USDT2 */
4355   HOTSPOT_JNI_GETSTRINGCRITICAL_ENTRY(
4356                                       env, string, (uintptr_t *) isCopy);
4357 #endif /* USDT2 */
4358   oop s = JNIHandles::resolve_non_null(string);
4359   jchar* ret;
4360   if (should_copy_string_value()) {
4361     typeArrayOop s_value = java_lang_String::value(s);
4362     int s_len = java_lang_String::length(s);
4363     ret = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal);  // add one for zero termination
4364     /* JNI Specification states return NULL on OOM */
4365     if (ret != NULL) {
4366       memcpy(ret, s_value->char_at_addr(0), s_len * sizeof(jchar));
4367       ret[s_len] = 0;
4368     }
4369     if (isCopy != NULL) *isCopy = JNI_TRUE;
4370   } else {
4371     typeArrayOop s_value = lock_gc_or_pin_string_value(thread, s);
4372     ret = (jchar*) s_value->base(T_CHAR);
4373     if (isCopy != NULL) *isCopy = JNI_FALSE;
4374   }
4375 #ifndef USDT2
4376   DTRACE_PROBE1(hotspot_jni, GetStringCritical__return, ret);
4377 #else /* USDT2 */
4378  HOTSPOT_JNI_GETSTRINGCRITICAL_RETURN(
4379                                       (uint16_t *) ret);
4380 #endif /* USDT2 */
4381   return ret;
4382 JNI_END
4383 
4384 
4385 JNI_ENTRY(void, jni_ReleaseStringCritical(JNIEnv *env, jstring str, const jchar *chars))
4386   JNIWrapper("ReleaseStringCritical");
4387 #ifndef USDT2
4388   DTRACE_PROBE3(hotspot_jni, ReleaseStringCritical__entry, env, str, chars);
4389 #else /* USDT2 */
4390   HOTSPOT_JNI_RELEASESTRINGCRITICAL_ENTRY(
4391                                           env, str, (uint16_t *) chars);
4392 #endif /* USDT2 */
4393   if (should_copy_string_value()) {
4394     // For copied string value, free jchar array allocated by earlier call to GetStringCritical.
4395     // This assumes that ReleaseStringCritical bookends GetStringCritical.
4396     FREE_C_HEAP_ARRAY(jchar, chars, mtInternal);
4397   } else {
4398     oop s = JNIHandles::resolve_non_null(str);
4399     // For not copied string value, drop the associated gc-locker/pin.
4400     unlock_gc_or_unpin_string_value(thread, s);
4401   }
4402 #ifndef USDT2
4403   DTRACE_PROBE(hotspot_jni, ReleaseStringCritical__return);
4404 #else /* USDT2 */
4405 HOTSPOT_JNI_RELEASESTRINGCRITICAL_RETURN(
4406 );
4407 #endif /* USDT2 */
4408 JNI_END
4409 
4410 
4411 JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref))
4412   JNIWrapper("jni_NewWeakGlobalRef");
4413 #ifndef USDT2
4414   DTRACE_PROBE2(hotspot_jni, NewWeakGlobalRef__entry, env, ref);
4415 #else /* USDT2 */
4416  HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(
4417                                     env, ref);
4418 #endif /* USDT2 */
4419   Handle ref_handle(thread, JNIHandles::resolve(ref));
4420   jweak ret = JNIHandles::make_weak_global(ref_handle);
4421 #ifndef USDT2


< prev index next >