851 // this function used to report a zero scale factor, so that the user
852 // would know not to attempt to access sub-word array elements.
853 // // Code for unpacked fields:
854 // if (scale < wordSize) return 0;
855
856 // The following allows for a pretty general fieldOffset cookie scheme,
857 // but requires it to be linear in byte offset.
858 return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0);
859 UNSAFE_END
860
861
862 static inline void throw_new(JNIEnv *env, const char *ename) {
863 char buf[100];
864 strcpy(buf, "java/lang/");
865 strcat(buf, ename);
866 jclass cls = env->FindClass(buf);
867 char* msg = NULL;
868 env->ThrowNew(cls, msg);
869 }
870
871 static jclass Unsafe_DefineClass(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) {
872 {
873 // Code lifted from JDK 1.3 ClassLoader.c
874
875 jbyte *body;
876 char *utfName;
877 jclass result = 0;
878 char buf[128];
879
880 if (UsePerfData) {
881 ClassLoader::unsafe_defineClassCallCounter()->inc();
882 }
883
884 if (data == NULL) {
885 throw_new(env, "NullPointerException");
886 return 0;
887 }
888
889 /* Work around 4153825. malloc crashes on Solaris when passed a
890 * negative size.
891 */
922 //VerifyFixClassname(utfName);
923 for (uint i = 0; i < len; i++) {
924 if (utfName[i] == '.') utfName[i] = '/';
925 }
926 } else {
927 utfName = NULL;
928 }
929
930 result = JVM_DefineClass(env, utfName, loader, body, length, pd);
931
932 if (utfName && utfName != buf)
933 FREE_C_HEAP_ARRAY(char, utfName, mtInternal);
934
935 free_body:
936 FREE_C_HEAP_ARRAY(jbyte, body, mtInternal);
937 return result;
938 }
939 }
940
941
942 UNSAFE_ENTRY(jclass, Unsafe_DefineClass0(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length))
943 UnsafeWrapper("Unsafe_DefineClass");
944 {
945 ThreadToNativeFromVM ttnfv(thread);
946
947 int depthFromDefineClass0 = 1;
948 jclass caller = JVM_GetCallerClass(env, depthFromDefineClass0);
949 jobject loader = (caller == NULL) ? NULL : JVM_GetClassLoader(env, caller);
950 jobject pd = (caller == NULL) ? NULL : JVM_GetProtectionDomain(env, caller);
951
952 return Unsafe_DefineClass(env, name, data, offset, length, loader, pd);
953 }
954 UNSAFE_END
955
956
957 UNSAFE_ENTRY(jclass, Unsafe_DefineClass1(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd))
958 UnsafeWrapper("Unsafe_DefineClass");
959 {
960 ThreadToNativeFromVM ttnfv(thread);
961
962 return Unsafe_DefineClass(env, name, data, offset, length, loader, pd);
963 }
964 UNSAFE_END
965
966 #define DAC_Args CLS"[B["OBJ
967 // define a class but do not make it known to the class loader or system dictionary
968 // - host_class: supplies context for linkage, access control, protection domain, and class loader
969 // - data: bytes of a class file, a raw memory address (length gives the number of bytes)
970 // - cp_patches: where non-null entries exist, they replace corresponding CP entries in data
971
972 // When you load an anonymous class U, it works as if you changed its name just before loading,
973 // to a name that you will never use again. Since the name is lost, no other class can directly
974 // link to any member of U. Just after U is loaded, the only way to use it is reflectively,
975 // through java.lang.Class methods like Class.newInstance.
976
977 // Access checks for linkage sites within U continue to follow the same rules as for named classes.
978 // The package of an anonymous class is given by the package qualifier on the name under which it was loaded.
979 // An anonymous class also has special privileges to access any member of its host class.
980 // This is the main reason why this loading operation is unsafe. The purpose of this is to
981 // allow language implementations to simulate "open classes"; a host class in effect gets
982 // new code when an anonymous class is loaded alongside it. A less convenient but more
983 // standard way to do this is with reflection, which can also be set to ignore access
984 // restrictions.
985
1306 oop p = JNIHandles::resolve(obj);
1307 void* addr = index_oop_from_field_offset_long(p, 0);
1308 Prefetch::write(addr, (intx)offset);
1309 UNSAFE_END
1310
1311
1312 /// JVM_RegisterUnsafeMethods
1313
1314 #define ADR "J"
1315
1316 #define LANG "Ljava/lang/"
1317
1318 #define OBJ LANG"Object;"
1319 #define CLS LANG"Class;"
1320 #define CTR LANG"reflect/Constructor;"
1321 #define FLD LANG"reflect/Field;"
1322 #define MTH LANG"reflect/Method;"
1323 #define THR LANG"Throwable;"
1324
1325 #define DC0_Args LANG"String;[BII"
1326 #define DC1_Args DC0_Args LANG"ClassLoader;" "Ljava/security/ProtectionDomain;"
1327
1328 #define CC (char*) /*cast a literal from (const char*)*/
1329 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
1330
1331 // define deprecated accessors for compabitility with 1.4.0
1332 #define DECLARE_GETSETOOP_140(Boolean, Z) \
1333 {CC"get"#Boolean, CC"("OBJ"I)"#Z, FN_PTR(Unsafe_Get##Boolean##140)}, \
1334 {CC"put"#Boolean, CC"("OBJ"I"#Z")V", FN_PTR(Unsafe_Set##Boolean##140)}
1335
1336 // Note: In 1.4.1, getObject and kin take both int and long offsets.
1337 #define DECLARE_GETSETOOP_141(Boolean, Z) \
1338 {CC"get"#Boolean, CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean)}, \
1339 {CC"put"#Boolean, CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean)}
1340
1341 // Note: In 1.5.0, there are volatile versions too
1342 #define DECLARE_GETSETOOP(Boolean, Z) \
1343 {CC"get"#Boolean, CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean)}, \
1344 {CC"put"#Boolean, CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean)}, \
1345 {CC"get"#Boolean"Volatile", CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean##Volatile)}, \
1346 {CC"put"#Boolean"Volatile", CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean##Volatile)}
1347
1348
1349 #define DECLARE_GETSETNATIVE(Byte, B) \
1350 {CC"get"#Byte, CC"("ADR")"#B, FN_PTR(Unsafe_GetNative##Byte)}, \
1351 {CC"put"#Byte, CC"("ADR#B")V", FN_PTR(Unsafe_SetNative##Byte)}
1352
1353
1354
1355 // %%% These are temporarily supported until the SDK sources
1356 // contain the necessarily updated Unsafe.java.
1357 static JNINativeMethod methods_140[] = {
1358
1359 {CC"getObject", CC"("OBJ"I)"OBJ"", FN_PTR(Unsafe_GetObject140)},
1360 {CC"putObject", CC"("OBJ"I"OBJ")V", FN_PTR(Unsafe_SetObject140)},
1361
1362 DECLARE_GETSETOOP_140(Boolean, Z),
1363 DECLARE_GETSETOOP_140(Byte, B),
1364 DECLARE_GETSETOOP_140(Short, S),
1365 DECLARE_GETSETOOP_140(Char, C),
1366 DECLARE_GETSETOOP_140(Int, I),
1367 DECLARE_GETSETOOP_140(Long, J),
1368 DECLARE_GETSETOOP_140(Float, F),
1369 DECLARE_GETSETOOP_140(Double, D),
1370
1371 DECLARE_GETSETNATIVE(Byte, B),
1372 DECLARE_GETSETNATIVE(Short, S),
1373 DECLARE_GETSETNATIVE(Char, C),
1374 DECLARE_GETSETNATIVE(Int, I),
1375 DECLARE_GETSETNATIVE(Long, J),
1376 DECLARE_GETSETNATIVE(Float, F),
1377 DECLARE_GETSETNATIVE(Double, D),
1378
1379 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
1380 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
1381
1382 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
1383 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
1384 // {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)},
1385 // {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)},
1386 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
1387
1388 {CC"fieldOffset", CC"("FLD")I", FN_PTR(Unsafe_FieldOffset)}, //deprecated
1389 {CC"staticFieldBase", CC"("CLS")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromClass)}, //deprecated
1390 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
1391 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
1392 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
1393 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
1394 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
1395
1396 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
1397 {CC"defineClass", CC"("DC1_Args")"CLS, FN_PTR(Unsafe_DefineClass1)},
1398 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
1399 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
1400 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
1401 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)}
1402 };
1403
1404 // These are the old methods prior to the JSR 166 changes in 1.5.0
1405 static JNINativeMethod methods_141[] = {
1406
1407 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)},
1408 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)},
1409
1410 DECLARE_GETSETOOP_141(Boolean, Z),
1411 DECLARE_GETSETOOP_141(Byte, B),
1412 DECLARE_GETSETOOP_141(Short, S),
1413 DECLARE_GETSETOOP_141(Char, C),
1414 DECLARE_GETSETOOP_141(Int, I),
1415 DECLARE_GETSETOOP_141(Long, J),
1416 DECLARE_GETSETOOP_141(Float, F),
1417 DECLARE_GETSETOOP_141(Double, D),
1418
1419 DECLARE_GETSETNATIVE(Byte, B),
1420 DECLARE_GETSETNATIVE(Short, S),
1421 DECLARE_GETSETNATIVE(Char, C),
1422 DECLARE_GETSETNATIVE(Int, I),
1423 DECLARE_GETSETNATIVE(Long, J),
1424 DECLARE_GETSETNATIVE(Float, F),
1425 DECLARE_GETSETNATIVE(Double, D),
1426
1427 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
1428 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
1429
1430 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
1431 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
1432 // {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)},
1433 // {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)},
1434 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
1435
1436 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)},
1437 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)},
1438 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
1439 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
1440 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
1441 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
1442 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
1443 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
1444
1445 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
1446 {CC"defineClass", CC"("DC1_Args")"CLS, FN_PTR(Unsafe_DefineClass1)},
1447 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
1448 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
1449 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
1450 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)}
1451
1452 };
1453
1454 // These are the old methods prior to the JSR 166 changes in 1.6.0
1455 static JNINativeMethod methods_15[] = {
1456
1457 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)},
1458 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)},
1459 {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)},
1460 {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)},
1461
1462
1463 DECLARE_GETSETOOP(Boolean, Z),
1464 DECLARE_GETSETOOP(Byte, B),
1465 DECLARE_GETSETOOP(Short, S),
1466 DECLARE_GETSETOOP(Char, C),
1467 DECLARE_GETSETOOP(Int, I),
1468 DECLARE_GETSETOOP(Long, J),
1469 DECLARE_GETSETOOP(Float, F),
1470 DECLARE_GETSETOOP(Double, D),
1471
1472 DECLARE_GETSETNATIVE(Byte, B),
1473 DECLARE_GETSETNATIVE(Short, S),
1474 DECLARE_GETSETNATIVE(Char, C),
1475 DECLARE_GETSETNATIVE(Int, I),
1476 DECLARE_GETSETNATIVE(Long, J),
1477 DECLARE_GETSETNATIVE(Float, F),
1478 DECLARE_GETSETNATIVE(Double, D),
1479
1480 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
1481 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
1482
1483 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
1484 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
1485 // {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)},
1486 // {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)},
1487 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
1488
1489 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)},
1490 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)},
1491 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
1492 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
1493 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
1494 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
1495 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
1496 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
1497
1498 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
1499 {CC"defineClass", CC"("DC1_Args")"CLS, FN_PTR(Unsafe_DefineClass1)},
1500 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
1501 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
1502 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
1503 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)},
1504 {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
1505 {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)},
1506 {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)},
1507 {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)},
1508 {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)}
1509
1510 };
1511
1512 // These are the correct methods, moving forward:
1513 static JNINativeMethod methods[] = {
1514
1515 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)},
1516 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)},
1517 {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)},
1518 {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)},
1519
1520
1521 DECLARE_GETSETOOP(Boolean, Z),
1522 DECLARE_GETSETOOP(Byte, B),
1523 DECLARE_GETSETOOP(Short, S),
1524 DECLARE_GETSETOOP(Char, C),
1525 DECLARE_GETSETOOP(Int, I),
1526 DECLARE_GETSETOOP(Long, J),
1527 DECLARE_GETSETOOP(Float, F),
1528 DECLARE_GETSETOOP(Double, D),
1529
1530 DECLARE_GETSETNATIVE(Byte, B),
1531 DECLARE_GETSETNATIVE(Short, S),
1532 DECLARE_GETSETNATIVE(Char, C),
1533 DECLARE_GETSETNATIVE(Int, I),
1534 DECLARE_GETSETNATIVE(Long, J),
1535 DECLARE_GETSETNATIVE(Float, F),
1536 DECLARE_GETSETNATIVE(Double, D),
1537
1538 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
1539 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
1540
1541 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
1542 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
1543 // {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)},
1544 // {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)},
1545 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
1546
1547 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)},
1548 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)},
1549 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
1550 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
1551 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
1552 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
1553 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
1554 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
1555
1556 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
1557 {CC"defineClass", CC"("DC1_Args")"CLS, FN_PTR(Unsafe_DefineClass1)},
1558 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
1559 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
1560 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
1561 {CC"tryMonitorEnter", CC"("OBJ")Z", FN_PTR(Unsafe_TryMonitorEnter)},
1562 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)},
1563 {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
1564 {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)},
1565 {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)},
1566 {CC"putOrderedObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetOrderedObject)},
1567 {CC"putOrderedInt", CC"("OBJ"JI)V", FN_PTR(Unsafe_SetOrderedInt)},
1568 {CC"putOrderedLong", CC"("OBJ"JJ)V", FN_PTR(Unsafe_SetOrderedLong)},
1569 {CC"loadFence", CC"()V", FN_PTR(Unsafe_LoadFence)},
1570 {CC"storeFence", CC"()V", FN_PTR(Unsafe_StoreFence)},
1571 {CC"fullFence", CC"()V", FN_PTR(Unsafe_FullFence)},
1572 {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)},
1573 {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)}
1574
1575 // {CC"getLoadAverage", CC"([DI)I", FN_PTR(Unsafe_Loadavg)},
1576
1577 // {CC"prefetchRead", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)},
1578 // {CC"prefetchWrite", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)}
1579 // {CC"prefetchReadStatic", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)},
1580 // {CC"prefetchWriteStatic",CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)}
1581
1582 };
1583
1584 JNINativeMethod loadavg_method[] = {
1585 {CC"getLoadAverage", CC"([DI)I", FN_PTR(Unsafe_Loadavg)}
1586 };
1587
1588 JNINativeMethod prefetch_methods[] = {
1589 {CC"prefetchRead", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)},
1590 {CC"prefetchWrite", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)},
1591 {CC"prefetchReadStatic", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)},
1592 {CC"prefetchWriteStatic",CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)}
1593 };
1594
1595 JNINativeMethod memcopy_methods[] = {
1596 {CC"copyMemory", CC"("OBJ"J"OBJ"JJ)V", FN_PTR(Unsafe_CopyMemory2)},
1597 {CC"setMemory", CC"("OBJ"JJB)V", FN_PTR(Unsafe_SetMemory2)}
1598 };
1599
1600 JNINativeMethod memcopy_methods_15[] = {
1601 {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)},
1602 {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)}
1603 };
1604
1605 JNINativeMethod anonk_methods[] = {
1606 {CC"defineAnonymousClass", CC"("DAC_Args")"CLS, FN_PTR(Unsafe_DefineAnonymousClass)},
1607 };
1608
1609 JNINativeMethod lform_methods[] = {
1610 {CC"shouldBeInitialized",CC"("CLS")Z", FN_PTR(Unsafe_ShouldBeInitialized)},
1611 };
1612
1613 #undef CC
1614 #undef FN_PTR
1615
1616 #undef ADR
1617 #undef LANG
1618 #undef OBJ
1619 #undef CLS
1620 #undef CTR
1621 #undef FLD
1622 #undef MTH
1623 #undef THR
1624 #undef DC0_Args
1625 #undef DC1_Args
1626
1627 #undef DECLARE_GETSETOOP
1628 #undef DECLARE_GETSETNATIVE
1629
1630
1631 // This one function is exported, used by NativeLookup.
1632 // The Unsafe_xxx functions above are called only from the interpreter.
1633 // The optimizer looks at names and signatures to recognize
1634 // individual functions.
1635
1636 JVM_ENTRY(void, JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls))
1637 UnsafeWrapper("JVM_RegisterUnsafeMethods");
1638 {
1639 ThreadToNativeFromVM ttnfv(thread);
1640 {
1641 env->RegisterNatives(unsafecls, loadavg_method, sizeof(loadavg_method)/sizeof(JNINativeMethod));
1642 if (env->ExceptionOccurred()) {
1643 if (PrintMiscellaneous && (Verbose || WizardMode)) {
1644 tty->print_cr("Warning: SDK 1.6 Unsafe.loadavg not found.");
1645 }
1646 env->ExceptionClear();
1647 }
1648 }
1649 {
1650 env->RegisterNatives(unsafecls, prefetch_methods, sizeof(prefetch_methods)/sizeof(JNINativeMethod));
1651 if (env->ExceptionOccurred()) {
1652 if (PrintMiscellaneous && (Verbose || WizardMode)) {
1653 tty->print_cr("Warning: SDK 1.6 Unsafe.prefetchRead/Write not found.");
1654 }
1655 env->ExceptionClear();
1656 }
1657 }
1658 {
1659 env->RegisterNatives(unsafecls, memcopy_methods, sizeof(memcopy_methods)/sizeof(JNINativeMethod));
1660 if (env->ExceptionOccurred()) {
1661 if (PrintMiscellaneous && (Verbose || WizardMode)) {
1662 tty->print_cr("Warning: SDK 1.7 Unsafe.copyMemory not found.");
1663 }
1664 env->ExceptionClear();
1665 env->RegisterNatives(unsafecls, memcopy_methods_15, sizeof(memcopy_methods_15)/sizeof(JNINativeMethod));
1666 if (env->ExceptionOccurred()) {
1667 if (PrintMiscellaneous && (Verbose || WizardMode)) {
1668 tty->print_cr("Warning: SDK 1.5 Unsafe.copyMemory not found.");
1669 }
1670 env->ExceptionClear();
1671 }
1672 }
1673 }
1674 if (EnableInvokeDynamic) {
1675 env->RegisterNatives(unsafecls, anonk_methods, sizeof(anonk_methods)/sizeof(JNINativeMethod));
1676 if (env->ExceptionOccurred()) {
1677 if (PrintMiscellaneous && (Verbose || WizardMode)) {
1678 tty->print_cr("Warning: SDK 1.7 Unsafe.defineClass (anonymous version) not found.");
1679 }
1680 env->ExceptionClear();
1681 }
1682 }
1683 if (EnableInvokeDynamic) {
1684 env->RegisterNatives(unsafecls, lform_methods, sizeof(lform_methods)/sizeof(JNINativeMethod));
1685 if (env->ExceptionOccurred()) {
1686 if (PrintMiscellaneous && (Verbose || WizardMode)) {
1687 tty->print_cr("Warning: SDK 1.7 LambdaForm support in Unsafe not found.");
1688 }
1689 env->ExceptionClear();
1690 }
1691 }
1692 int status = env->RegisterNatives(unsafecls, methods, sizeof(methods)/sizeof(JNINativeMethod));
1693 if (env->ExceptionOccurred()) {
1694 if (PrintMiscellaneous && (Verbose || WizardMode)) {
1695 tty->print_cr("Warning: SDK 1.6 version of Unsafe not found.");
1696 }
1697 env->ExceptionClear();
1698 // %%% For now, be backward compatible with an older class:
1699 status = env->RegisterNatives(unsafecls, methods_15, sizeof(methods_15)/sizeof(JNINativeMethod));
1700 }
1701 if (env->ExceptionOccurred()) {
1702 if (PrintMiscellaneous && (Verbose || WizardMode)) {
1703 tty->print_cr("Warning: SDK 1.5 version of Unsafe not found.");
1704 }
1705 env->ExceptionClear();
1706 // %%% For now, be backward compatible with an older class:
1707 status = env->RegisterNatives(unsafecls, methods_141, sizeof(methods_141)/sizeof(JNINativeMethod));
1708 }
1709 if (env->ExceptionOccurred()) {
1710 if (PrintMiscellaneous && (Verbose || WizardMode)) {
1711 tty->print_cr("Warning: SDK 1.4.1 version of Unsafe not found.");
1712 }
1713 env->ExceptionClear();
1714 // %%% For now, be backward compatible with an older class:
1715 status = env->RegisterNatives(unsafecls, methods_140, sizeof(methods_140)/sizeof(JNINativeMethod));
1716 }
1717 guarantee(status == 0, "register unsafe natives");
1718 }
1719 JVM_END
|
851 // this function used to report a zero scale factor, so that the user
852 // would know not to attempt to access sub-word array elements.
853 // // Code for unpacked fields:
854 // if (scale < wordSize) return 0;
855
856 // The following allows for a pretty general fieldOffset cookie scheme,
857 // but requires it to be linear in byte offset.
858 return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0);
859 UNSAFE_END
860
861
862 static inline void throw_new(JNIEnv *env, const char *ename) {
863 char buf[100];
864 strcpy(buf, "java/lang/");
865 strcat(buf, ename);
866 jclass cls = env->FindClass(buf);
867 char* msg = NULL;
868 env->ThrowNew(cls, msg);
869 }
870
871 static jclass Unsafe_DefineClass_impl(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) {
872 {
873 // Code lifted from JDK 1.3 ClassLoader.c
874
875 jbyte *body;
876 char *utfName;
877 jclass result = 0;
878 char buf[128];
879
880 if (UsePerfData) {
881 ClassLoader::unsafe_defineClassCallCounter()->inc();
882 }
883
884 if (data == NULL) {
885 throw_new(env, "NullPointerException");
886 return 0;
887 }
888
889 /* Work around 4153825. malloc crashes on Solaris when passed a
890 * negative size.
891 */
922 //VerifyFixClassname(utfName);
923 for (uint i = 0; i < len; i++) {
924 if (utfName[i] == '.') utfName[i] = '/';
925 }
926 } else {
927 utfName = NULL;
928 }
929
930 result = JVM_DefineClass(env, utfName, loader, body, length, pd);
931
932 if (utfName && utfName != buf)
933 FREE_C_HEAP_ARRAY(char, utfName, mtInternal);
934
935 free_body:
936 FREE_C_HEAP_ARRAY(jbyte, body, mtInternal);
937 return result;
938 }
939 }
940
941
942 UNSAFE_ENTRY(jclass, Unsafe_DefineClass(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd))
943 UnsafeWrapper("Unsafe_DefineClass");
944 {
945 ThreadToNativeFromVM ttnfv(thread);
946 return Unsafe_DefineClass_impl(env, name, data, offset, length, loader, pd);
947 }
948 UNSAFE_END
949
950
951 UNSAFE_ENTRY(jclass, Unsafe_DefineClass0(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length))
952 UnsafeWrapper("Unsafe_DefineClass");
953 {
954 ThreadToNativeFromVM ttnfv(thread);
955
956 int depthFromDefineClass0 = 1;
957 jclass caller = JVM_GetCallerClass(env, depthFromDefineClass0);
958 jobject loader = (caller == NULL) ? NULL : JVM_GetClassLoader(env, caller);
959 jobject pd = (caller == NULL) ? NULL : JVM_GetProtectionDomain(env, caller);
960
961 return Unsafe_DefineClass_impl(env, name, data, offset, length, loader, pd);
962 }
963 UNSAFE_END
964
965
966 #define DAC_Args CLS"[B["OBJ
967 // define a class but do not make it known to the class loader or system dictionary
968 // - host_class: supplies context for linkage, access control, protection domain, and class loader
969 // - data: bytes of a class file, a raw memory address (length gives the number of bytes)
970 // - cp_patches: where non-null entries exist, they replace corresponding CP entries in data
971
972 // When you load an anonymous class U, it works as if you changed its name just before loading,
973 // to a name that you will never use again. Since the name is lost, no other class can directly
974 // link to any member of U. Just after U is loaded, the only way to use it is reflectively,
975 // through java.lang.Class methods like Class.newInstance.
976
977 // Access checks for linkage sites within U continue to follow the same rules as for named classes.
978 // The package of an anonymous class is given by the package qualifier on the name under which it was loaded.
979 // An anonymous class also has special privileges to access any member of its host class.
980 // This is the main reason why this loading operation is unsafe. The purpose of this is to
981 // allow language implementations to simulate "open classes"; a host class in effect gets
982 // new code when an anonymous class is loaded alongside it. A less convenient but more
983 // standard way to do this is with reflection, which can also be set to ignore access
984 // restrictions.
985
1306 oop p = JNIHandles::resolve(obj);
1307 void* addr = index_oop_from_field_offset_long(p, 0);
1308 Prefetch::write(addr, (intx)offset);
1309 UNSAFE_END
1310
1311
1312 /// JVM_RegisterUnsafeMethods
1313
1314 #define ADR "J"
1315
1316 #define LANG "Ljava/lang/"
1317
1318 #define OBJ LANG"Object;"
1319 #define CLS LANG"Class;"
1320 #define CTR LANG"reflect/Constructor;"
1321 #define FLD LANG"reflect/Field;"
1322 #define MTH LANG"reflect/Method;"
1323 #define THR LANG"Throwable;"
1324
1325 #define DC0_Args LANG"String;[BII"
1326 #define DC_Args DC0_Args LANG"ClassLoader;" "Ljava/security/ProtectionDomain;"
1327
1328 #define CC (char*) /*cast a literal from (const char*)*/
1329 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
1330
1331 // define deprecated accessors for compabitility with 1.4.0
1332 #define DECLARE_GETSETOOP_140(Boolean, Z) \
1333 {CC"get"#Boolean, CC"("OBJ"I)"#Z, FN_PTR(Unsafe_Get##Boolean##140)}, \
1334 {CC"put"#Boolean, CC"("OBJ"I"#Z")V", FN_PTR(Unsafe_Set##Boolean##140)}
1335
1336 // Note: In 1.4.1, getObject and kin take both int and long offsets.
1337 #define DECLARE_GETSETOOP_141(Boolean, Z) \
1338 {CC"get"#Boolean, CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean)}, \
1339 {CC"put"#Boolean, CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean)}
1340
1341 // Note: In 1.5.0, there are volatile versions too
1342 #define DECLARE_GETSETOOP(Boolean, Z) \
1343 {CC"get"#Boolean, CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean)}, \
1344 {CC"put"#Boolean, CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean)}, \
1345 {CC"get"#Boolean"Volatile", CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean##Volatile)}, \
1346 {CC"put"#Boolean"Volatile", CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean##Volatile)}
1347
1348
1349 #define DECLARE_GETSETNATIVE(Byte, B) \
1350 {CC"get"#Byte, CC"("ADR")"#B, FN_PTR(Unsafe_GetNative##Byte)}, \
1351 {CC"put"#Byte, CC"("ADR#B")V", FN_PTR(Unsafe_SetNative##Byte)}
1352
1353
1354
1355 // These are the methods for 1.4.0
1356 static JNINativeMethod methods_140[] = {
1357 {CC"getObject", CC"("OBJ"I)"OBJ"", FN_PTR(Unsafe_GetObject140)},
1358 {CC"putObject", CC"("OBJ"I"OBJ")V", FN_PTR(Unsafe_SetObject140)},
1359
1360 DECLARE_GETSETOOP_140(Boolean, Z),
1361 DECLARE_GETSETOOP_140(Byte, B),
1362 DECLARE_GETSETOOP_140(Short, S),
1363 DECLARE_GETSETOOP_140(Char, C),
1364 DECLARE_GETSETOOP_140(Int, I),
1365 DECLARE_GETSETOOP_140(Long, J),
1366 DECLARE_GETSETOOP_140(Float, F),
1367 DECLARE_GETSETOOP_140(Double, D),
1368
1369 DECLARE_GETSETNATIVE(Byte, B),
1370 DECLARE_GETSETNATIVE(Short, S),
1371 DECLARE_GETSETNATIVE(Char, C),
1372 DECLARE_GETSETNATIVE(Int, I),
1373 DECLARE_GETSETNATIVE(Long, J),
1374 DECLARE_GETSETNATIVE(Float, F),
1375 DECLARE_GETSETNATIVE(Double, D),
1376
1377 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
1378 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
1379
1380 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
1381 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
1382 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
1383
1384 {CC"fieldOffset", CC"("FLD")I", FN_PTR(Unsafe_FieldOffset)},
1385 {CC"staticFieldBase", CC"("CLS")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromClass)},
1386 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
1387 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
1388 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
1389 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
1390 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
1391
1392 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
1393 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)},
1394 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
1395 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
1396 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
1397 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)}
1398 };
1399
1400 // These are the methods prior to the JSR 166 changes in 1.5.0
1401 static JNINativeMethod methods_141[] = {
1402 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)},
1403 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)},
1404
1405 DECLARE_GETSETOOP_141(Boolean, Z),
1406 DECLARE_GETSETOOP_141(Byte, B),
1407 DECLARE_GETSETOOP_141(Short, S),
1408 DECLARE_GETSETOOP_141(Char, C),
1409 DECLARE_GETSETOOP_141(Int, I),
1410 DECLARE_GETSETOOP_141(Long, J),
1411 DECLARE_GETSETOOP_141(Float, F),
1412 DECLARE_GETSETOOP_141(Double, D),
1413
1414 DECLARE_GETSETNATIVE(Byte, B),
1415 DECLARE_GETSETNATIVE(Short, S),
1416 DECLARE_GETSETNATIVE(Char, C),
1417 DECLARE_GETSETNATIVE(Int, I),
1418 DECLARE_GETSETNATIVE(Long, J),
1419 DECLARE_GETSETNATIVE(Float, F),
1420 DECLARE_GETSETNATIVE(Double, D),
1421
1422 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
1423 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
1424
1425 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
1426 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
1427 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
1428
1429 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)},
1430 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)},
1431 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
1432 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
1433 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
1434 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
1435 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
1436 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
1437
1438 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
1439 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)},
1440 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
1441 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
1442 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
1443 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)}
1444
1445 };
1446
1447 // These are the methods prior to the JSR 166 changes in 1.6.0
1448 static JNINativeMethod methods_15[] = {
1449 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)},
1450 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)},
1451 {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)},
1452 {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)},
1453
1454
1455 DECLARE_GETSETOOP(Boolean, Z),
1456 DECLARE_GETSETOOP(Byte, B),
1457 DECLARE_GETSETOOP(Short, S),
1458 DECLARE_GETSETOOP(Char, C),
1459 DECLARE_GETSETOOP(Int, I),
1460 DECLARE_GETSETOOP(Long, J),
1461 DECLARE_GETSETOOP(Float, F),
1462 DECLARE_GETSETOOP(Double, D),
1463
1464 DECLARE_GETSETNATIVE(Byte, B),
1465 DECLARE_GETSETNATIVE(Short, S),
1466 DECLARE_GETSETNATIVE(Char, C),
1467 DECLARE_GETSETNATIVE(Int, I),
1468 DECLARE_GETSETNATIVE(Long, J),
1469 DECLARE_GETSETNATIVE(Float, F),
1470 DECLARE_GETSETNATIVE(Double, D),
1471
1472 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
1473 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
1474
1475 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
1476 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
1477 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
1478
1479 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)},
1480 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)},
1481 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
1482 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
1483 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
1484 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
1485 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
1486 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
1487
1488 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
1489 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)},
1490 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
1491 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
1492 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
1493 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)},
1494 {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
1495 {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)},
1496 {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)},
1497 {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)},
1498 {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)}
1499
1500 };
1501
1502 // These are the methods for 1.6.0 and 1.7.0
1503 static JNINativeMethod methods_16[] = {
1504 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)},
1505 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)},
1506 {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)},
1507 {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)},
1508
1509 DECLARE_GETSETOOP(Boolean, Z),
1510 DECLARE_GETSETOOP(Byte, B),
1511 DECLARE_GETSETOOP(Short, S),
1512 DECLARE_GETSETOOP(Char, C),
1513 DECLARE_GETSETOOP(Int, I),
1514 DECLARE_GETSETOOP(Long, J),
1515 DECLARE_GETSETOOP(Float, F),
1516 DECLARE_GETSETOOP(Double, D),
1517
1518 DECLARE_GETSETNATIVE(Byte, B),
1519 DECLARE_GETSETNATIVE(Short, S),
1520 DECLARE_GETSETNATIVE(Char, C),
1521 DECLARE_GETSETNATIVE(Int, I),
1522 DECLARE_GETSETNATIVE(Long, J),
1523 DECLARE_GETSETNATIVE(Float, F),
1524 DECLARE_GETSETNATIVE(Double, D),
1525
1526 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
1527 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
1528
1529 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
1530 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
1531 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
1532
1533 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)},
1534 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)},
1535 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
1536 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
1537 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
1538 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
1539 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
1540 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
1541
1542 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
1543 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)},
1544 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
1545 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
1546 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
1547 {CC"tryMonitorEnter", CC"("OBJ")Z", FN_PTR(Unsafe_TryMonitorEnter)},
1548 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)},
1549 {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
1550 {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)},
1551 {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)},
1552 {CC"putOrderedObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetOrderedObject)},
1553 {CC"putOrderedInt", CC"("OBJ"JI)V", FN_PTR(Unsafe_SetOrderedInt)},
1554 {CC"putOrderedLong", CC"("OBJ"JJ)V", FN_PTR(Unsafe_SetOrderedLong)},
1555 {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)},
1556 {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)}
1557 };
1558
1559 // These are the methods for 1.8.0
1560 static JNINativeMethod methods_18[] = {
1561 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)},
1562 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)},
1563 {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)},
1564 {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)},
1565
1566 DECLARE_GETSETOOP(Boolean, Z),
1567 DECLARE_GETSETOOP(Byte, B),
1568 DECLARE_GETSETOOP(Short, S),
1569 DECLARE_GETSETOOP(Char, C),
1570 DECLARE_GETSETOOP(Int, I),
1571 DECLARE_GETSETOOP(Long, J),
1572 DECLARE_GETSETOOP(Float, F),
1573 DECLARE_GETSETOOP(Double, D),
1574
1575 DECLARE_GETSETNATIVE(Byte, B),
1576 DECLARE_GETSETNATIVE(Short, S),
1577 DECLARE_GETSETNATIVE(Char, C),
1578 DECLARE_GETSETNATIVE(Int, I),
1579 DECLARE_GETSETNATIVE(Long, J),
1580 DECLARE_GETSETNATIVE(Float, F),
1581 DECLARE_GETSETNATIVE(Double, D),
1582
1583 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
1584 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
1585
1586 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
1587 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
1588 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
1589
1590 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)},
1591 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)},
1592 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
1593 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
1594 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
1595 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
1596 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
1597 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
1598
1599 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)},
1600 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
1601 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
1602 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
1603 {CC"tryMonitorEnter", CC"("OBJ")Z", FN_PTR(Unsafe_TryMonitorEnter)},
1604 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)},
1605 {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
1606 {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)},
1607 {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)},
1608 {CC"putOrderedObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetOrderedObject)},
1609 {CC"putOrderedInt", CC"("OBJ"JI)V", FN_PTR(Unsafe_SetOrderedInt)},
1610 {CC"putOrderedLong", CC"("OBJ"JJ)V", FN_PTR(Unsafe_SetOrderedLong)},
1611 {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)},
1612 {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)}
1613 };
1614
1615 JNINativeMethod loadavg_method[] = {
1616 {CC"getLoadAverage", CC"([DI)I", FN_PTR(Unsafe_Loadavg)}
1617 };
1618
1619 JNINativeMethod prefetch_methods[] = {
1620 {CC"prefetchRead", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)},
1621 {CC"prefetchWrite", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)},
1622 {CC"prefetchReadStatic", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)},
1623 {CC"prefetchWriteStatic",CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)}
1624 };
1625
1626 JNINativeMethod memcopy_methods_17[] = {
1627 {CC"copyMemory", CC"("OBJ"J"OBJ"JJ)V", FN_PTR(Unsafe_CopyMemory2)},
1628 {CC"setMemory", CC"("OBJ"JJB)V", FN_PTR(Unsafe_SetMemory2)}
1629 };
1630
1631 JNINativeMethod memcopy_methods_15[] = {
1632 {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)},
1633 {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)}
1634 };
1635
1636 JNINativeMethod anonk_methods[] = {
1637 {CC"defineAnonymousClass", CC"("DAC_Args")"CLS, FN_PTR(Unsafe_DefineAnonymousClass)},
1638 };
1639
1640 JNINativeMethod lform_methods[] = {
1641 {CC"shouldBeInitialized",CC"("CLS")Z", FN_PTR(Unsafe_ShouldBeInitialized)},
1642 };
1643
1644 JNINativeMethod fence_methods[] = {
1645 {CC"loadFence", CC"()V", FN_PTR(Unsafe_LoadFence)},
1646 {CC"storeFence", CC"()V", FN_PTR(Unsafe_StoreFence)},
1647 {CC"fullFence", CC"()V", FN_PTR(Unsafe_FullFence)},
1648 };
1649
1650 #undef CC
1651 #undef FN_PTR
1652
1653 #undef ADR
1654 #undef LANG
1655 #undef OBJ
1656 #undef CLS
1657 #undef CTR
1658 #undef FLD
1659 #undef MTH
1660 #undef THR
1661 #undef DC0_Args
1662 #undef DC_Args
1663
1664 #undef DECLARE_GETSETOOP
1665 #undef DECLARE_GETSETNATIVE
1666
1667
1668 /**
1669 * Helper method to register native methods.
1670 */
1671 static bool register_natives(const char* message, JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods) {
1672 int status = env->RegisterNatives(clazz, methods, nMethods);
1673 if (status < 0 || env->ExceptionOccurred()) {
1674 if (PrintMiscellaneous && (Verbose || WizardMode)) {
1675 tty->print_cr("Unsafe: failed registering %s", message);
1676 }
1677 env->ExceptionClear();
1678 return false;
1679 } else {
1680 if (PrintMiscellaneous && (Verbose || WizardMode)) {
1681 tty->print_cr("Unsafe: successfully registered %s", message);
1682 }
1683 return true;
1684 }
1685 }
1686
1687
1688 // This one function is exported, used by NativeLookup.
1689 // The Unsafe_xxx functions above are called only from the interpreter.
1690 // The optimizer looks at names and signatures to recognize
1691 // individual functions.
1692
1693 JVM_ENTRY(void, JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls))
1694 UnsafeWrapper("JVM_RegisterUnsafeMethods");
1695 {
1696 ThreadToNativeFromVM ttnfv(thread);
1697
1698 // Unsafe methods
1699 {
1700 bool success = false;
1701 // We need to register the 1.6 methods first because the 1.8 methods would register fine on 1.7 and 1.6
1702 if (!success) {
1703 success = register_natives("1.6 methods", env, unsafecls, methods_16, sizeof(methods_16)/sizeof(JNINativeMethod));
1704 }
1705 if (!success) {
1706 success = register_natives("1.8 methods", env, unsafecls, methods_18, sizeof(methods_18)/sizeof(JNINativeMethod));
1707 }
1708 if (!success) {
1709 success = register_natives("1.5 methods", env, unsafecls, methods_15, sizeof(methods_15)/sizeof(JNINativeMethod));
1710 }
1711 if (!success) {
1712 success = register_natives("1.4.1 methods", env, unsafecls, methods_141, sizeof(methods_141)/sizeof(JNINativeMethod));
1713 }
1714 if (!success) {
1715 success = register_natives("1.4.0 methods", env, unsafecls, methods_140, sizeof(methods_140)/sizeof(JNINativeMethod));
1716 }
1717 guarantee(success, "register unsafe natives");
1718 }
1719
1720 // Unsafe.getLoadAverage
1721 register_natives("1.6 loadavg method", env, unsafecls, loadavg_method, sizeof(loadavg_method)/sizeof(JNINativeMethod));
1722
1723 // Prefetch methods
1724 register_natives("1.6 prefetch methods", env, unsafecls, prefetch_methods, sizeof(prefetch_methods)/sizeof(JNINativeMethod));
1725
1726 // Memory copy methods
1727 {
1728 bool success = false;
1729 if (!success) {
1730 success = register_natives("1.7 memory copy methods", env, unsafecls, memcopy_methods_17, sizeof(memcopy_methods_17)/sizeof(JNINativeMethod));
1731 }
1732 if (!success) {
1733 success = register_natives("1.5 memory copy methods", env, unsafecls, memcopy_methods_15, sizeof(memcopy_methods_15)/sizeof(JNINativeMethod));
1734 }
1735 }
1736
1737 // Unsafe.defineAnonymousClass
1738 if (EnableInvokeDynamic) {
1739 register_natives("1.7 define anonymous class method", env, unsafecls, anonk_methods, sizeof(anonk_methods)/sizeof(JNINativeMethod));
1740 }
1741
1742 // Unsafe.shouldBeInitialized
1743 if (EnableInvokeDynamic) {
1744 register_natives("1.7 LambdaForm support", env, unsafecls, lform_methods, sizeof(lform_methods)/sizeof(JNINativeMethod));
1745 }
1746
1747 // Fence methods
1748 register_natives("1.8 fence methods", env, unsafecls, fence_methods, sizeof(fence_methods)/sizeof(JNINativeMethod));
1749 }
1750 JVM_END
|