src/share/vm/prims/jniCheck.cpp

changeset 5418
39deebbc90b3
parent 4278
070d523b96a7
child 5423
c29568b733d2
equal deleted inserted replaced
5417:33c52908bcdb 5418:39deebbc90b3
124 static const char * fatal_class_not_a_throwable_class = "JNI Throw or ThrowNew received a class argument that is not a Throwable or Throwable subclass"; 124 static const char * fatal_class_not_a_throwable_class = "JNI Throw or ThrowNew received a class argument that is not a Throwable or Throwable subclass";
125 static const char * fatal_wrong_class_or_method = "Wrong object class or methodID passed to JNI call"; 125 static const char * fatal_wrong_class_or_method = "Wrong object class or methodID passed to JNI call";
126 static const char * fatal_non_weak_method = "non-weak methodID passed to JNI call"; 126 static const char * fatal_non_weak_method = "non-weak methodID passed to JNI call";
127 static const char * fatal_unknown_array_object = "Unknown array object passed to JNI array operations"; 127 static const char * fatal_unknown_array_object = "Unknown array object passed to JNI array operations";
128 static const char * fatal_object_array_expected = "Object array expected but not received for JNI array operation"; 128 static const char * fatal_object_array_expected = "Object array expected but not received for JNI array operation";
129 static const char * fatal_prim_type_array_expected = "Primitive type array expected but not received for JNI array operation";
129 static const char * fatal_non_array = "Non-array passed to JNI array operations"; 130 static const char * fatal_non_array = "Non-array passed to JNI array operations";
130 static const char * fatal_element_type_mismatch = "Array element type mismatch in JNI"; 131 static const char * fatal_element_type_mismatch = "Array element type mismatch in JNI";
131 static const char * fatal_should_be_static = "Non-static field ID passed to JNI"; 132 static const char * fatal_should_be_static = "Non-static field ID passed to JNI";
132 static const char * fatal_wrong_static_field = "Wrong static field ID passed to JNI"; 133 static const char * fatal_wrong_static_field = "Wrong static field ID passed to JNI";
133 static const char * fatal_static_field_not_found = "Static field not found in JNI get/set field operations"; 134 static const char * fatal_static_field_not_found = "Static field not found in JNI get/set field operations";
276 oop s = jniCheck::validate_object(thr, js); 277 oop s = jniCheck::validate_object(thr, js);
277 if (!s || !java_lang_String::is_instance(s)) 278 if (!s || !java_lang_String::is_instance(s))
278 ReportJNIFatalError(thr, fatal_non_string); 279 ReportJNIFatalError(thr, fatal_non_string);
279 } 280 }
280 281
281 static inline void 282 static inline arrayOop
282 checkArray(JavaThread* thr, jarray jArray, int elementType) 283 check_is_array(JavaThread* thr, jarray jArray)
283 { 284 {
284 ASSERT_OOPS_ALLOWED; 285 ASSERT_OOPS_ALLOWED;
285 arrayOop aOop; 286 arrayOop aOop;
286 287
287 aOop = (arrayOop)jniCheck::validate_object(thr, jArray); 288 aOop = (arrayOop)jniCheck::validate_object(thr, jArray);
288 if (aOop == NULL || !aOop->is_array()) 289 if (aOop == NULL || !aOop->is_array()) {
289 ReportJNIFatalError(thr, fatal_non_array); 290 ReportJNIFatalError(thr, fatal_non_array);
290
291 if (elementType != -1) {
292 if (aOop->is_typeArray()) {
293 BasicType array_type = TypeArrayKlass::cast(aOop->klass())->element_type();
294 if (array_type != elementType)
295 ReportJNIFatalError(thr, fatal_element_type_mismatch);
296 } else if (aOop->is_objArray()) {
297 if ( T_OBJECT != elementType)
298 ReportJNIFatalError(thr, fatal_object_array_expected);
299 } else {
300 ReportJNIFatalError(thr, fatal_unknown_array_object);
301 }
302 } 291 }
303 } 292 return aOop;
304 293 }
294
295 static inline arrayOop
296 check_is_primitive_array(JavaThread* thr, jarray jArray) {
297 arrayOop aOop = check_is_array(thr, jArray);
298
299 if (!aOop->is_typeArray()) {
300 ReportJNIFatalError(thr, fatal_prim_type_array_expected);
301 }
302 return aOop;
303 }
304
305 static inline void
306 check_primitive_array_type(JavaThread* thr, jarray jArray, BasicType elementType)
307 {
308 BasicType array_type;
309 arrayOop aOop;
310
311 aOop = check_is_primitive_array(thr, jArray);
312 array_type = TypeArrayKlass::cast(aOop->klass())->element_type();
313 if (array_type != elementType) {
314 ReportJNIFatalError(thr, fatal_element_type_mismatch);
315 }
316 }
317
318 static inline void
319 check_is_obj_array(JavaThread* thr, jarray jArray) {
320 BasicType array_type;
321 arrayOop aOop;
322
323 aOop = check_is_array(thr, jArray);
324 array_type = TypeArrayKlass::cast(aOop->klass())->element_type();
325 if (array_type != T_OBJECT) {
326 ReportJNIFatalError(thr, fatal_object_array_expected);
327 }
328 }
305 329
306 oop jniCheck::validate_handle(JavaThread* thr, jobject obj) { 330 oop jniCheck::validate_handle(JavaThread* thr, jobject obj) {
307 if (JNIHandles::is_frame_handle(thr, obj) || 331 if (JNIHandles::is_frame_handle(thr, obj) ||
308 JNIHandles::is_local_handle(thr, obj) || 332 JNIHandles::is_local_handle(thr, obj) ||
309 JNIHandles::is_global_handle(obj) || 333 JNIHandles::is_global_handle(obj) ||
1415 JNI_ENTRY_CHECKED(jsize, 1439 JNI_ENTRY_CHECKED(jsize,
1416 checked_jni_GetArrayLength(JNIEnv *env, 1440 checked_jni_GetArrayLength(JNIEnv *env,
1417 jarray array)) 1441 jarray array))
1418 functionEnter(thr); 1442 functionEnter(thr);
1419 IN_VM( 1443 IN_VM(
1420 checkArray(thr, array, -1); 1444 check_is_array(thr, array);
1421 ) 1445 )
1422 jsize result = UNCHECKED()->GetArrayLength(env,array); 1446 jsize result = UNCHECKED()->GetArrayLength(env,array);
1423 functionExit(env); 1447 functionExit(env);
1424 return result; 1448 return result;
1425 JNI_END 1449 JNI_END
1439 checked_jni_GetObjectArrayElement(JNIEnv *env, 1463 checked_jni_GetObjectArrayElement(JNIEnv *env,
1440 jobjectArray array, 1464 jobjectArray array,
1441 jsize index)) 1465 jsize index))
1442 functionEnter(thr); 1466 functionEnter(thr);
1443 IN_VM( 1467 IN_VM(
1444 checkArray(thr, array, T_OBJECT); 1468 check_is_obj_array(thr, array);
1445 ) 1469 )
1446 jobject result = UNCHECKED()->GetObjectArrayElement(env,array,index); 1470 jobject result = UNCHECKED()->GetObjectArrayElement(env,array,index);
1447 functionExit(env); 1471 functionExit(env);
1448 return result; 1472 return result;
1449 JNI_END 1473 JNI_END
1453 jobjectArray array, 1477 jobjectArray array,
1454 jsize index, 1478 jsize index,
1455 jobject val)) 1479 jobject val))
1456 functionEnter(thr); 1480 functionEnter(thr);
1457 IN_VM( 1481 IN_VM(
1458 checkArray(thr, array, T_OBJECT); 1482 check_is_obj_array(thr, array);
1459 ) 1483 )
1460 UNCHECKED()->SetObjectArrayElement(env,array,index,val); 1484 UNCHECKED()->SetObjectArrayElement(env,array,index,val);
1461 functionExit(env); 1485 functionExit(env);
1462 JNI_END 1486 JNI_END
1463 1487
1485 checked_jni_Get##Result##ArrayElements(JNIEnv *env, \ 1509 checked_jni_Get##Result##ArrayElements(JNIEnv *env, \
1486 ElementType##Array array, \ 1510 ElementType##Array array, \
1487 jboolean *isCopy)) \ 1511 jboolean *isCopy)) \
1488 functionEnter(thr); \ 1512 functionEnter(thr); \
1489 IN_VM( \ 1513 IN_VM( \
1490 checkArray(thr, array, ElementTag); \ 1514 check_primitive_array_type(thr, array, ElementTag); \
1491 ) \ 1515 ) \
1492 ElementType *result = UNCHECKED()->Get##Result##ArrayElements(env, \ 1516 ElementType *result = UNCHECKED()->Get##Result##ArrayElements(env, \
1493 array, \ 1517 array, \
1494 isCopy); \ 1518 isCopy); \
1495 functionExit(env); \ 1519 functionExit(env); \
1511 ElementType##Array array, \ 1535 ElementType##Array array, \
1512 ElementType *elems, \ 1536 ElementType *elems, \
1513 jint mode)) \ 1537 jint mode)) \
1514 functionEnterExceptionAllowed(thr); \ 1538 functionEnterExceptionAllowed(thr); \
1515 IN_VM( \ 1539 IN_VM( \
1516 checkArray(thr, array, ElementTag); \ 1540 check_primitive_array_type(thr, array, ElementTag); \
1517 ASSERT_OOPS_ALLOWED; \ 1541 ASSERT_OOPS_ALLOWED; \
1518 typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \ 1542 typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
1519 /* cannot check validity of copy, unless every request is logged by 1543 /* cannot check validity of copy, unless every request is logged by
1520 * checking code. Implementation of this check is deferred until a 1544 * checking code. Implementation of this check is deferred until a
1521 * subsequent release. 1545 * subsequent release.
1541 jsize start, \ 1565 jsize start, \
1542 jsize len, \ 1566 jsize len, \
1543 ElementType *buf)) \ 1567 ElementType *buf)) \
1544 functionEnter(thr); \ 1568 functionEnter(thr); \
1545 IN_VM( \ 1569 IN_VM( \
1546 checkArray(thr, array, ElementTag); \ 1570 check_primitive_array_type(thr, array, ElementTag); \
1547 ) \ 1571 ) \
1548 UNCHECKED()->Get##Result##ArrayRegion(env,array,start,len,buf); \ 1572 UNCHECKED()->Get##Result##ArrayRegion(env,array,start,len,buf); \
1549 functionExit(env); \ 1573 functionExit(env); \
1550 JNI_END 1574 JNI_END
1551 1575
1565 jsize start, \ 1589 jsize start, \
1566 jsize len, \ 1590 jsize len, \
1567 const ElementType *buf)) \ 1591 const ElementType *buf)) \
1568 functionEnter(thr); \ 1592 functionEnter(thr); \
1569 IN_VM( \ 1593 IN_VM( \
1570 checkArray(thr, array, ElementTag); \ 1594 check_primitive_array_type(thr, array, ElementTag); \
1571 ) \ 1595 ) \
1572 UNCHECKED()->Set##Result##ArrayRegion(env,array,start,len,buf); \ 1596 UNCHECKED()->Set##Result##ArrayRegion(env,array,start,len,buf); \
1573 functionExit(env); \ 1597 functionExit(env); \
1574 JNI_END 1598 JNI_END
1575 1599
1667 checked_jni_GetPrimitiveArrayCritical(JNIEnv *env, 1691 checked_jni_GetPrimitiveArrayCritical(JNIEnv *env,
1668 jarray array, 1692 jarray array,
1669 jboolean *isCopy)) 1693 jboolean *isCopy))
1670 functionEnterCritical(thr); 1694 functionEnterCritical(thr);
1671 IN_VM( 1695 IN_VM(
1672 checkArray(thr, array, -1); 1696 check_is_primitive_array(thr, array);
1673 ) 1697 )
1674 void *result = UNCHECKED()->GetPrimitiveArrayCritical(env, array, isCopy); 1698 void *result = UNCHECKED()->GetPrimitiveArrayCritical(env, array, isCopy);
1675 functionExit(env); 1699 functionExit(env);
1676 return result; 1700 return result;
1677 JNI_END 1701 JNI_END
1681 jarray array, 1705 jarray array,
1682 void *carray, 1706 void *carray,
1683 jint mode)) 1707 jint mode))
1684 functionEnterCriticalExceptionAllowed(thr); 1708 functionEnterCriticalExceptionAllowed(thr);
1685 IN_VM( 1709 IN_VM(
1686 checkArray(thr, array, -1); 1710 check_is_primitive_array(thr, array);
1687 ) 1711 )
1688 /* The Hotspot JNI code does not use the parameters, so just check the 1712 /* The Hotspot JNI code does not use the parameters, so just check the
1689 * array parameter as a minor sanity check 1713 * array parameter as a minor sanity check
1690 */ 1714 */
1691 UNCHECKED()->ReleasePrimitiveArrayCritical(env, array, carray, mode); 1715 UNCHECKED()->ReleasePrimitiveArrayCritical(env, array, carray, mode);

mercurial