mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 11:04:34 +02:00
8025922: JNI access to Strings need to check if the value field is non-null
Reviewed-by: dholmes, dcubed
This commit is contained in:
parent
738af149a2
commit
dcbbb4d37b
2 changed files with 57 additions and 41 deletions
|
@ -3210,7 +3210,11 @@ JNI_QUICK_ENTRY(jsize, jni_GetStringLength(JNIEnv *env, jstring string))
|
||||||
HOTSPOT_JNI_GETSTRINGLENGTH_ENTRY(
|
HOTSPOT_JNI_GETSTRINGLENGTH_ENTRY(
|
||||||
env, string);
|
env, string);
|
||||||
#endif /* USDT2 */
|
#endif /* USDT2 */
|
||||||
jsize ret = java_lang_String::length(JNIHandles::resolve_non_null(string));
|
jsize ret = 0;
|
||||||
|
oop s = JNIHandles::resolve_non_null(string);
|
||||||
|
if (java_lang_String::value(s) != NULL) {
|
||||||
|
ret = java_lang_String::length(s);
|
||||||
|
}
|
||||||
#ifndef USDT2
|
#ifndef USDT2
|
||||||
DTRACE_PROBE1(hotspot_jni, GetStringLength__return, ret);
|
DTRACE_PROBE1(hotspot_jni, GetStringLength__return, ret);
|
||||||
#else /* USDT2 */
|
#else /* USDT2 */
|
||||||
|
@ -3230,11 +3234,13 @@ JNI_QUICK_ENTRY(const jchar*, jni_GetStringChars(
|
||||||
HOTSPOT_JNI_GETSTRINGCHARS_ENTRY(
|
HOTSPOT_JNI_GETSTRINGCHARS_ENTRY(
|
||||||
env, string, (uintptr_t *) isCopy);
|
env, string, (uintptr_t *) isCopy);
|
||||||
#endif /* USDT2 */
|
#endif /* USDT2 */
|
||||||
|
jchar* buf = NULL;
|
||||||
oop s = JNIHandles::resolve_non_null(string);
|
oop s = JNIHandles::resolve_non_null(string);
|
||||||
int s_len = java_lang_String::length(s);
|
|
||||||
typeArrayOop s_value = java_lang_String::value(s);
|
typeArrayOop s_value = java_lang_String::value(s);
|
||||||
|
if (s_value != NULL) {
|
||||||
|
int s_len = java_lang_String::length(s);
|
||||||
int s_offset = java_lang_String::offset(s);
|
int s_offset = java_lang_String::offset(s);
|
||||||
jchar* buf = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal); // add one for zero termination
|
buf = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal); // add one for zero termination
|
||||||
/* JNI Specification states return NULL on OOM */
|
/* JNI Specification states return NULL on OOM */
|
||||||
if (buf != NULL) {
|
if (buf != NULL) {
|
||||||
if (s_len > 0) {
|
if (s_len > 0) {
|
||||||
|
@ -3246,6 +3252,7 @@ JNI_QUICK_ENTRY(const jchar*, jni_GetStringChars(
|
||||||
*isCopy = JNI_TRUE;
|
*isCopy = JNI_TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#ifndef USDT2
|
#ifndef USDT2
|
||||||
DTRACE_PROBE1(hotspot_jni, GetStringChars__return, buf);
|
DTRACE_PROBE1(hotspot_jni, GetStringChars__return, buf);
|
||||||
#else /* USDT2 */
|
#else /* USDT2 */
|
||||||
|
@ -3313,7 +3320,11 @@ JNI_ENTRY(jsize, jni_GetStringUTFLength(JNIEnv *env, jstring string))
|
||||||
HOTSPOT_JNI_GETSTRINGUTFLENGTH_ENTRY(
|
HOTSPOT_JNI_GETSTRINGUTFLENGTH_ENTRY(
|
||||||
env, string);
|
env, string);
|
||||||
#endif /* USDT2 */
|
#endif /* USDT2 */
|
||||||
jsize ret = java_lang_String::utf8_length(JNIHandles::resolve_non_null(string));
|
jsize ret = 0;
|
||||||
|
oop java_string = JNIHandles::resolve_non_null(string);
|
||||||
|
if (java_lang_String::value(java_string) != NULL) {
|
||||||
|
ret = java_lang_String::utf8_length(java_string);
|
||||||
|
}
|
||||||
#ifndef USDT2
|
#ifndef USDT2
|
||||||
DTRACE_PROBE1(hotspot_jni, GetStringUTFLength__return, ret);
|
DTRACE_PROBE1(hotspot_jni, GetStringUTFLength__return, ret);
|
||||||
#else /* USDT2 */
|
#else /* USDT2 */
|
||||||
|
@ -3332,16 +3343,19 @@ JNI_ENTRY(const char*, jni_GetStringUTFChars(JNIEnv *env, jstring string, jboole
|
||||||
HOTSPOT_JNI_GETSTRINGUTFCHARS_ENTRY(
|
HOTSPOT_JNI_GETSTRINGUTFCHARS_ENTRY(
|
||||||
env, string, (uintptr_t *) isCopy);
|
env, string, (uintptr_t *) isCopy);
|
||||||
#endif /* USDT2 */
|
#endif /* USDT2 */
|
||||||
|
char* result = NULL;
|
||||||
oop java_string = JNIHandles::resolve_non_null(string);
|
oop java_string = JNIHandles::resolve_non_null(string);
|
||||||
|
if (java_lang_String::value(java_string) != NULL) {
|
||||||
size_t length = java_lang_String::utf8_length(java_string);
|
size_t length = java_lang_String::utf8_length(java_string);
|
||||||
/* JNI Specification states return NULL on OOM */
|
/* JNI Specification states return NULL on OOM */
|
||||||
char* result = AllocateHeap(length + 1, mtInternal, 0, AllocFailStrategy::RETURN_NULL);
|
result = AllocateHeap(length + 1, mtInternal, 0, AllocFailStrategy::RETURN_NULL);
|
||||||
if (result != NULL) {
|
if (result != NULL) {
|
||||||
java_lang_String::as_utf8_string(java_string, result, (int) length + 1);
|
java_lang_String::as_utf8_string(java_string, result, (int) length + 1);
|
||||||
if (isCopy != NULL) {
|
if (isCopy != NULL) {
|
||||||
*isCopy = JNI_TRUE;
|
*isCopy = JNI_TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#ifndef USDT2
|
#ifndef USDT2
|
||||||
DTRACE_PROBE1(hotspot_jni, GetStringUTFChars__return, result);
|
DTRACE_PROBE1(hotspot_jni, GetStringUTFChars__return, result);
|
||||||
#else /* USDT2 */
|
#else /* USDT2 */
|
||||||
|
|
|
@ -1324,18 +1324,19 @@ JNI_ENTRY_CHECKED(const jchar *,
|
||||||
IN_VM(
|
IN_VM(
|
||||||
checkString(thr, str);
|
checkString(thr, str);
|
||||||
)
|
)
|
||||||
|
jchar* newResult = NULL;
|
||||||
const jchar *result = UNCHECKED()->GetStringChars(env,str,isCopy);
|
const jchar *result = UNCHECKED()->GetStringChars(env,str,isCopy);
|
||||||
assert (isCopy == NULL || *isCopy == JNI_TRUE, "GetStringChars didn't return a copy as expected");
|
assert (isCopy == NULL || *isCopy == JNI_TRUE, "GetStringChars didn't return a copy as expected");
|
||||||
|
if (result != NULL) {
|
||||||
size_t len = UNCHECKED()->GetStringLength(env,str) + 1; // + 1 for NULL termination
|
size_t len = UNCHECKED()->GetStringLength(env,str) + 1; // + 1 for NULL termination
|
||||||
jint* tagLocation = (jint*) AllocateHeap(len * sizeof(jchar) + sizeof(jint), mtInternal);
|
jint* tagLocation = (jint*) AllocateHeap(len * sizeof(jchar) + sizeof(jint), mtInternal);
|
||||||
*tagLocation = STRING_TAG;
|
*tagLocation = STRING_TAG;
|
||||||
jchar* newResult = (jchar*) (tagLocation + 1);
|
newResult = (jchar*) (tagLocation + 1);
|
||||||
memcpy(newResult, result, len * sizeof(jchar));
|
memcpy(newResult, result, len * sizeof(jchar));
|
||||||
// Avoiding call to UNCHECKED()->ReleaseStringChars() since that will fire unexpected dtrace probes
|
// Avoiding call to UNCHECKED()->ReleaseStringChars() since that will fire unexpected dtrace probes
|
||||||
// Note that the dtrace arguments for the allocated memory will not match up with this solution.
|
// Note that the dtrace arguments for the allocated memory will not match up with this solution.
|
||||||
FreeHeap((char*)result);
|
FreeHeap((char*)result);
|
||||||
|
}
|
||||||
functionExit(env);
|
functionExit(env);
|
||||||
return newResult;
|
return newResult;
|
||||||
JNI_END
|
JNI_END
|
||||||
|
@ -1394,18 +1395,19 @@ JNI_ENTRY_CHECKED(const char *,
|
||||||
IN_VM(
|
IN_VM(
|
||||||
checkString(thr, str);
|
checkString(thr, str);
|
||||||
)
|
)
|
||||||
|
char* newResult = NULL;
|
||||||
const char *result = UNCHECKED()->GetStringUTFChars(env,str,isCopy);
|
const char *result = UNCHECKED()->GetStringUTFChars(env,str,isCopy);
|
||||||
assert (isCopy == NULL || *isCopy == JNI_TRUE, "GetStringUTFChars didn't return a copy as expected");
|
assert (isCopy == NULL || *isCopy == JNI_TRUE, "GetStringUTFChars didn't return a copy as expected");
|
||||||
|
if (result != NULL) {
|
||||||
size_t len = strlen(result) + 1; // + 1 for NULL termination
|
size_t len = strlen(result) + 1; // + 1 for NULL termination
|
||||||
jint* tagLocation = (jint*) AllocateHeap(len + sizeof(jint), mtInternal);
|
jint* tagLocation = (jint*) AllocateHeap(len + sizeof(jint), mtInternal);
|
||||||
*tagLocation = STRING_UTF_TAG;
|
*tagLocation = STRING_UTF_TAG;
|
||||||
char* newResult = (char*) (tagLocation + 1);
|
newResult = (char*) (tagLocation + 1);
|
||||||
strcpy(newResult, result);
|
strcpy(newResult, result);
|
||||||
// Avoiding call to UNCHECKED()->ReleaseStringUTFChars() since that will fire unexpected dtrace probes
|
// Avoiding call to UNCHECKED()->ReleaseStringUTFChars() since that will fire unexpected dtrace probes
|
||||||
// Note that the dtrace arguments for the allocated memory will not match up with this solution.
|
// Note that the dtrace arguments for the allocated memory will not match up with this solution.
|
||||||
FreeHeap((char*)result, mtInternal);
|
FreeHeap((char*)result, mtInternal);
|
||||||
|
}
|
||||||
functionExit(env);
|
functionExit(env);
|
||||||
return newResult;
|
return newResult;
|
||||||
JNI_END
|
JNI_END
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue