Skip to content

Commit

Permalink
fixup! Substitute identity hash codes with those that existed on the …
Browse files Browse the repository at this point in the history
…first object creation
  • Loading branch information
zhelenskiy committed Dec 10, 2024
1 parent c8128d6 commit cc8068d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
14 changes: 12 additions & 2 deletions bootstrap/src/sun/nio/ch/lincheck/Injections.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,21 @@ public static void beforeNewObjectCreation(String className) {
public static void afterNewObjectCreation(Object obj) {
getEventTracker().afterNewObjectCreation(obj);
}


/**
* Retrieves the next object id, used for identity hash code substitution, and then advances it by one.
*/
public static long getNextObjectId() {
return getEventTracker().getNextObjectId();
}


/**
* Ensures that for the same old id, previously received with {@code getNextObjectId},
* the counter after calling the function persists.
* <p>
* If for given {@code oldId} there is no saved {@code newId}, the function saves the current value.
* If for given {@code oldId} there is a saved {@code newId}, the function sets the counter to the {@code newId}.
*/
public static void advanceCurrentObjectIdWithKnownOldObjectId(long oldId) {
getEventTracker().advanceCurrentObjectIdWithKnownOldObjectId(oldId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import java.util.concurrent.atomic.AtomicLong


private typealias Id = Long

/**
* This class stores initial identity hashcodes of created objects.
* When the program is rerun the firstly calculated hashcodes are left as is and used to
Expand Down Expand Up @@ -47,43 +48,29 @@ internal class ObjectInitialHashCodes {
currentObjectId,
System.identityHashCode(obj)
)
if (isLoggingEnabled()) {
println(obj::class.simpleName)
println(currentObjectId)
println(initialIdentityHashCode)
}
// TODO: bizarre and crazy code below (might not work for all JVM implementations)
UnsafeHolder.UNSAFE.putInt(obj, IDENTITY_HASHCODE_OFFSET, initialIdentityHashCode)

return currentObjectId
}

private fun isLoggingEnabled(): Boolean = System.getProperty("lincheck.log", "false") == "true"

/**
* Resets ids numeration and starts them from 0.
*/
fun resetObjectIds() {
if (isLoggingEnabled()) {
println("Restart counter")
}
nextObjectId.set(0)
}


/**
* Ensures that for the same old id, previously received with {@code getNextObjectId},
* the counter after calling the function persists.
* <p>
* If for given {@code oldId} there is no saved {@code newId}, the function saves the current value.
* If for given {@code oldId} there is a saved {@code newId}, the function sets the counter to the {@code newId}.
*/
fun advanceCurrentObjectIdWithKnownOldObjectId(oldObjectId: Id) {
val newObjectId = nextObjectId.get()
val existingAdvance = objectIdAdvances.putIfAbsent(oldObjectId, newObjectId)
if (isLoggingEnabled()) {
print("Advancing counter: ")
print(oldObjectId)
print(" -> ")
if (existingAdvance != null) {
print("(reuse) ")
println(existingAdvance)
} else {
println(newObjectId)
}
}
if (existingAdvance != null) {
nextObjectId.set(existingAdvance)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import org.junit.Test
import java.io.ByteArrayOutputStream
import java.io.PrintStream

/**
* Checks for absence of non-determinism and absence (or existence) of exceptions.
*/
abstract class AbstractNativeCallTest {
private fun testTraceDebugger() {
val oldStdOut = System.out
Expand Down

0 comments on commit cc8068d

Please sign in to comment.