|
26 | 26 | import jdk.vm.ci.code.TargetDescription; |
27 | 27 | import jdk.vm.ci.meta.ResolvedJavaMethod; |
28 | 28 | import org.graalvm.compiler.api.test.Graal; |
| 29 | +import org.graalvm.compiler.core.phases.HighTier; |
| 30 | +import org.graalvm.compiler.options.OptionValues; |
29 | 31 | import org.graalvm.compiler.replacements.test.MethodSubstitutionTest; |
30 | 32 | import org.graalvm.compiler.runtime.RuntimeProvider; |
31 | 33 | import org.graalvm.compiler.test.AddExports; |
32 | 34 | import org.junit.Test; |
33 | 35 |
|
| 36 | +import java.lang.reflect.Field; |
| 37 | + |
34 | 38 | @AddExports("java.base/jdk.internal.misc") |
35 | 39 | public class UnsafeReplacementsTest extends MethodSubstitutionTest { |
36 | 40 |
|
@@ -310,4 +314,200 @@ public void testGetAndSet() { |
310 | 314 | test("unsafeGetAndSetLong"); |
311 | 315 | test("unsafeGetAndSetObject"); |
312 | 316 | } |
| 317 | + |
| 318 | + public static void fieldInstance() { |
| 319 | + JdkInternalMiscUnsafeAccessTestBoolean.testFieldInstance(); |
| 320 | + } |
| 321 | + |
| 322 | + @Test |
| 323 | + public void testFieldInstance() { |
| 324 | + test(new OptionValues(getInitialOptions(), HighTier.Options.Inline, false), "fieldInstance"); |
| 325 | + } |
| 326 | + |
| 327 | + public static void array() { |
| 328 | + JdkInternalMiscUnsafeAccessTestBoolean.testArray(); |
| 329 | + } |
| 330 | + |
| 331 | + @Test |
| 332 | + public void testArray() { |
| 333 | + test(new OptionValues(getInitialOptions(), HighTier.Options.Inline, false), "array"); |
| 334 | + } |
| 335 | + |
| 336 | + public static void fieldStatic() { |
| 337 | + JdkInternalMiscUnsafeAccessTestBoolean.testFieldStatic(); |
| 338 | + } |
| 339 | + |
| 340 | + @Test |
| 341 | + public void testFieldStatic() { |
| 342 | + test(new OptionValues(getInitialOptions(), HighTier.Options.Inline, false), "fieldStatic"); |
| 343 | + } |
| 344 | + |
| 345 | + public static class JdkInternalMiscUnsafeAccessTestBoolean { |
| 346 | + static final int ITERATIONS = 100000; |
| 347 | + |
| 348 | + static final int WEAK_ATTEMPTS = 10; |
| 349 | + |
| 350 | + static final long V_OFFSET; |
| 351 | + |
| 352 | + static final Object STATIC_V_BASE; |
| 353 | + |
| 354 | + static final long STATIC_V_OFFSET; |
| 355 | + |
| 356 | + static final int ARRAY_OFFSET; |
| 357 | + |
| 358 | + static final int ARRAY_SHIFT; |
| 359 | + |
| 360 | + static { |
| 361 | + try { |
| 362 | + Field staticVField = UnsafeReplacementsTest.JdkInternalMiscUnsafeAccessTestBoolean.class.getDeclaredField("staticV"); |
| 363 | + STATIC_V_BASE = unsafe.staticFieldBase(staticVField); |
| 364 | + STATIC_V_OFFSET = unsafe.staticFieldOffset(staticVField); |
| 365 | + } catch (Exception e) { |
| 366 | + throw new RuntimeException(e); |
| 367 | + } |
| 368 | + |
| 369 | + try { |
| 370 | + Field vField = UnsafeReplacementsTest.JdkInternalMiscUnsafeAccessTestBoolean.class.getDeclaredField("v"); |
| 371 | + V_OFFSET = unsafe.objectFieldOffset(vField); |
| 372 | + } catch (Exception e) { |
| 373 | + throw new RuntimeException(e); |
| 374 | + } |
| 375 | + |
| 376 | + ARRAY_OFFSET = unsafe.arrayBaseOffset(boolean[].class); |
| 377 | + int ascale = unsafe.arrayIndexScale(boolean[].class); |
| 378 | + ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale); |
| 379 | + } |
| 380 | + |
| 381 | + static boolean staticV; |
| 382 | + |
| 383 | + boolean v; |
| 384 | + |
| 385 | + @BytecodeParserForceInline |
| 386 | + public static void testFieldInstance() { |
| 387 | + JdkInternalMiscUnsafeAccessTestBoolean t = new JdkInternalMiscUnsafeAccessTestBoolean(); |
| 388 | + for (int c = 0; c < ITERATIONS; c++) { |
| 389 | + testAccess(t, V_OFFSET); |
| 390 | + } |
| 391 | + } |
| 392 | + |
| 393 | + public static void testFieldStatic() { |
| 394 | + for (int c = 0; c < ITERATIONS; c++) { |
| 395 | + testAccess(STATIC_V_BASE, STATIC_V_OFFSET); |
| 396 | + } |
| 397 | + } |
| 398 | + |
| 399 | + public static void testArray() { |
| 400 | + boolean[] array = new boolean[10]; |
| 401 | + for (int c = 0; c < ITERATIONS; c++) { |
| 402 | + for (int i = 0; i < array.length; i++) { |
| 403 | + testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET); |
| 404 | + } |
| 405 | + } |
| 406 | + } |
| 407 | + |
| 408 | + public static void assertEquals(Object seen, Object expected, String message) { |
| 409 | + if (seen != expected) { |
| 410 | + throw new AssertionError(message + " - seen: " + seen + ", expected: " + expected); |
| 411 | + } |
| 412 | + } |
| 413 | + |
| 414 | + // Checkstyle: stop |
| 415 | + @BytecodeParserForceInline |
| 416 | + public static void testAccess(Object base, long offset) { |
| 417 | + // Advanced compare |
| 418 | + { |
| 419 | + boolean r = unsafe.compareAndExchangeBoolean(base, offset, false, true); |
| 420 | + assertEquals(r, false, "success compareAndExchange boolean"); |
| 421 | + boolean x = unsafe.getBoolean(base, offset); |
| 422 | + assertEquals(x, true, "success compareAndExchange boolean value"); |
| 423 | + } |
| 424 | + |
| 425 | + { |
| 426 | + boolean r = unsafe.compareAndExchangeBoolean(base, offset, false, false); |
| 427 | + assertEquals(r, true, "failing compareAndExchange boolean"); |
| 428 | + boolean x = unsafe.getBoolean(base, offset); |
| 429 | + assertEquals(x, true, "failing compareAndExchange boolean value"); |
| 430 | + } |
| 431 | + |
| 432 | + { |
| 433 | + boolean r = unsafe.compareAndExchangeBooleanAcquire(base, offset, true, false); |
| 434 | + assertEquals(r, true, "success compareAndExchangeAcquire boolean"); |
| 435 | + boolean x = unsafe.getBoolean(base, offset); |
| 436 | + assertEquals(x, false, "success compareAndExchangeAcquire boolean value"); |
| 437 | + } |
| 438 | + |
| 439 | + { |
| 440 | + boolean r = unsafe.compareAndExchangeBooleanAcquire(base, offset, true, false); |
| 441 | + assertEquals(r, false, "failing compareAndExchangeAcquire boolean"); |
| 442 | + boolean x = unsafe.getBoolean(base, offset); |
| 443 | + assertEquals(x, false, "failing compareAndExchangeAcquire boolean value"); |
| 444 | + } |
| 445 | + |
| 446 | + { |
| 447 | + boolean r = unsafe.compareAndExchangeBooleanRelease(base, offset, false, true); |
| 448 | + assertEquals(r, false, "success compareAndExchangeRelease boolean"); |
| 449 | + boolean x = unsafe.getBoolean(base, offset); |
| 450 | + assertEquals(x, true, "success compareAndExchangeRelease boolean value"); |
| 451 | + } |
| 452 | + |
| 453 | + { |
| 454 | + boolean r = unsafe.compareAndExchangeBooleanRelease(base, offset, false, false); |
| 455 | + assertEquals(r, true, "failing compareAndExchangeRelease boolean"); |
| 456 | + boolean x = unsafe.getBoolean(base, offset); |
| 457 | + assertEquals(x, true, "failing compareAndExchangeRelease boolean value"); |
| 458 | + } |
| 459 | + |
| 460 | + { |
| 461 | + boolean success = false; |
| 462 | + for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 463 | + success = unsafe.weakCompareAndSetBooleanPlain(base, offset, true, false); |
| 464 | + } |
| 465 | + assertEquals(success, true, "weakCompareAndSetPlain boolean"); |
| 466 | + boolean x = unsafe.getBoolean(base, offset); |
| 467 | + assertEquals(x, false, "weakCompareAndSetPlain boolean value"); |
| 468 | + } |
| 469 | + |
| 470 | + { |
| 471 | + boolean success = false; |
| 472 | + for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 473 | + success = unsafe.weakCompareAndSetBooleanAcquire(base, offset, false, true); |
| 474 | + } |
| 475 | + assertEquals(success, true, "weakCompareAndSetAcquire boolean"); |
| 476 | + boolean x = unsafe.getBoolean(base, offset); |
| 477 | + assertEquals(x, true, "weakCompareAndSetAcquire boolean"); |
| 478 | + } |
| 479 | + |
| 480 | + { |
| 481 | + boolean success = false; |
| 482 | + for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 483 | + success = unsafe.weakCompareAndSetBooleanRelease(base, offset, true, false); |
| 484 | + } |
| 485 | + assertEquals(success, true, "weakCompareAndSetRelease boolean"); |
| 486 | + boolean x = unsafe.getBoolean(base, offset); |
| 487 | + assertEquals(x, false, "weakCompareAndSetRelease boolean"); |
| 488 | + } |
| 489 | + |
| 490 | + { |
| 491 | + boolean success = false; |
| 492 | + for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 493 | + success = unsafe.weakCompareAndSetBoolean(base, offset, false, true); |
| 494 | + } |
| 495 | + assertEquals(success, true, "weakCompareAndSet boolean"); |
| 496 | + boolean x = unsafe.getBoolean(base, offset); |
| 497 | + assertEquals(x, true, "weakCompareAndSet boolean"); |
| 498 | + } |
| 499 | + |
| 500 | + unsafe.putBoolean(base, offset, false); |
| 501 | + |
| 502 | + // Compare set and get |
| 503 | + { |
| 504 | + boolean o = unsafe.getAndSetBoolean(base, offset, true); |
| 505 | + assertEquals(o, false, "getAndSet boolean"); |
| 506 | + boolean x = unsafe.getBoolean(base, offset); |
| 507 | + assertEquals(x, true, "getAndSet boolean value"); |
| 508 | + } |
| 509 | + |
| 510 | + } |
| 511 | + // Checkstyle: resume |
| 512 | + } |
313 | 513 | } |
0 commit comments