Skip to content

volatile fields overwritten with 0 when .hashCode() or .equals() is called #475

@ghazel

Description

@ghazel

I have found a very strange bug. Using this simple Structure (and no associated C code, because no C calls are made):

public static class Bug extends Structure {
    public volatile long num;
    protected List<String> getFieldOrder() {
        return Arrays.asList(new String[] { "num" } );
    }
};

And this Java code:

Bug b = new Bug();
b.num = 0xABFFEEEE;
b.writeField("num");

Log.d(TAG, "bug pre: " + b);
b.hashCode();
Log.d(TAG, "bug post: " + b);

After b.hashCode() is called, the memory contents are 0:

bug pre: Mylib$Bug(auto-allocated@0xb50016f8 (8 bytes)) {
  long num@0=ffffffffabffeeee
}
memory dump
[eeeeffab]
[ffffffff]
bug post: Mylib$Bug(auto-allocated@0xb50016f8 (8 bytes)) {
  long num@0=ffffffffabffeeee
}
memory dump
[00000000]
[00000000]

Notice the last memory dump, all the bytes are zero.

The CPU abi here is armeabi-v7a (Android) and the JNA is built from the repo at 9f094c2. Does not happen if num is not volatile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions