提交 7095fdf5 编写于 作者: S sla

8025922: JNI access to Strings need to check if the value field is non-null

Reviewed-by: dholmes, dcubed
上级 776ca611
...@@ -3210,7 +3210,11 @@ JNI_QUICK_ENTRY(jsize, jni_GetStringLength(JNIEnv *env, jstring string)) ...@@ -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,20 +3234,23 @@ JNI_QUICK_ENTRY(const jchar*, jni_GetStringChars( ...@@ -3230,20 +3234,23 @@ 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);
int s_offset = java_lang_String::offset(s); if (s_value != NULL) {
jchar* buf = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal); // add one for zero termination int s_len = java_lang_String::length(s);
/* JNI Specification states return NULL on OOM */ int s_offset = java_lang_String::offset(s);
if (buf != NULL) { buf = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal); // add one for zero termination
if (s_len > 0) { /* JNI Specification states return NULL on OOM */
memcpy(buf, s_value->char_at_addr(s_offset), sizeof(jchar)*s_len); if (buf != NULL) {
} if (s_len > 0) {
buf[s_len] = 0; memcpy(buf, s_value->char_at_addr(s_offset), sizeof(jchar)*s_len);
//%note jni_5 }
if (isCopy != NULL) { buf[s_len] = 0;
*isCopy = JNI_TRUE; //%note jni_5
if (isCopy != NULL) {
*isCopy = JNI_TRUE;
}
} }
} }
#ifndef USDT2 #ifndef USDT2
...@@ -3313,7 +3320,11 @@ JNI_ENTRY(jsize, jni_GetStringUTFLength(JNIEnv *env, jstring string)) ...@@ -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,14 +3343,17 @@ JNI_ENTRY(const char*, jni_GetStringUTFChars(JNIEnv *env, jstring string, jboole ...@@ -3332,14 +3343,17 @@ 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);
size_t length = java_lang_String::utf8_length(java_string); if (java_lang_String::value(java_string) != NULL) {
/* JNI Specification states return NULL on OOM */ size_t length = java_lang_String::utf8_length(java_string);
char* result = AllocateHeap(length + 1, mtInternal, 0, AllocFailStrategy::RETURN_NULL); /* JNI Specification states return NULL on OOM */
if (result != NULL) { result = AllocateHeap(length + 1, mtInternal, 0, AllocFailStrategy::RETURN_NULL);
java_lang_String::as_utf8_string(java_string, result, (int) length + 1); if (result != NULL) {
if (isCopy != NULL) { java_lang_String::as_utf8_string(java_string, result, (int) length + 1);
*isCopy = JNI_TRUE; if (isCopy != NULL) {
*isCopy = JNI_TRUE;
}
} }
} }
#ifndef USDT2 #ifndef USDT2
......
...@@ -1324,18 +1324,19 @@ JNI_ENTRY_CHECKED(const jchar *, ...@@ -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 *, ...@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册