Implicit unlock in synchronized method relies on local variable 0

Description

Synchronized method performs implicit lock and unlock of the 'this' object. At method entry, Jikes fetches the 'this' reference from local variable 0 and acquire a lock on that object. Before each return instruction, Jikes fetches the reference from local variable 0 again, and release the lock on that object.

Although at method entry, the 'this' reference is always located in local variable 0 (see [1]), it is wrong to expect the 'this' reference to be still available in variable 0 when the method is about to return.

The Java VM Spec does not forbid the java compiler to reuse the variable 0 to store other references than 'this' (see [2]). Moreover the Spec states "Note that the monitor entry and exit automatically performed by the Java virtual machine when invoking a synchronized method are considered to occur during the calling method's invocation." (see [3]).

The issue have been discovered and discussed before because Soot is producing bytecode that reuse variable 0 (see [4],[5]). Note that the OpenJDK is able to handle such bytecode correctly.

[1] http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.6.1
[2] http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.2
[3] http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.11.10
[4] http://old.nabble.com/-rvm-research--Biased-unlocking-exception-when-using-soot-generated-classes-tt27772333.html#a27772333
[5] http://www.sable.mcgill.ca/pipermail/soot-list/2010-July/003145.html

Environment

None

Status

Assignee

Unassigned

Reporter

quentin sabah

Labels

None

Fix versions

Affects versions

Priority

Medium
Configure