-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
volatile fields overwritten with 0 when .hashCode() or .equals() is called #475
Comments
The bug seems to come from these lines: https://github.com/twall/jna/blob/50fe79d660e1991adcf31fffd1fd59e5202d62e0/src/com/sun/jna/Structure.java#L1484 They clear the struct and attempt to re-write the fields, but (as an aside, that's not exactly how I would expect volatile to work -- I would expect it to always write through to the |
By design, fields marked “volatile” are not written by default (see javadoc in Structure.java:71) and you must use “writeField()” to write the Java side to native memory. JNA uses this as a way of tagging areas of memory that may be updated on the native side independently of the Java code. It provides more fine-grained control than “setAutoWrite(false)”. Are you saying that the native memory is overwritten in cases other than calling “writeField()”?
|
Right. Native memory is being zeroed by
|
It’s not as much of an issue if the Structure memory is allocated by JNA. I suppose that memory could be passed off to native for ongoing use, but that’s an uncommon pattern. The intent was to capture the nature of native blocks of memory being updated asynchronously, like in a device driver, where you want the native side to have sole write access to a particular field.
|
JNA allocated structure is just an example. In my application I get the structure by calling a function like: Bug Mylib.getSomeStruct(); Using that structure in a hash map, or array, or any comparison zeros it out, as demonstrated, so later when I try use the structure it's zero: void Mylib.useBugForThings(Bug b); |
Why do equals and hashCode clear the native memory at all? |
Probably to prevent padding bytes from affecting a byte by byte comparison. It was a long time ago and I don't think I fully defined the intent of those functions. The general idea was to have structs with identical field values compare equals, but in practice I've never actually used that "feature" Sent from my iPhone
|
That's a pretty deep equals, which I wouldn't expect from memcmp() or in general, coming from C. Trying to write automatic Java-style object equality seems pretty hard -- and it'd probably want to run .equals() on each of the field members, no? In that case you wouldn't need to clear the memory at all. |
I think I was intending a memcmp, and cleared the memory prior to writing the Java fields to native memory in order to get a “clean” comparison. The intent was not to do object-by-object comparison. That said, it might be worth it to drop the hashCode/equals with side effects and go for a simpler model of “is this the same struct/memory” rather than “are these structs equivalent”.
|
I expected "is this the same struct/memory". If I did hope for “are these structs equivalent”, I would expect to either suffer the potential byte padding differences, or be required to implement .equals() myself (likely in C to begin with). |
As a matter of fact, the discussion around the issue here (http://stackoverflow.com/questions/141720/how-do-you-compare-structs-for-equality-in-c) would imply that JNA isn’t really doing anyone much benefit by providing a struct comparator.
|
Motivation: We are behind in terms of netty version. Modifications: Upgrade to latest version Result: Use latest netty release
I have found a very strange bug. Using this simple Structure (and no associated C code, because no C calls are made):
And this Java code:
After
b.hashCode()
is called, the memory contents are 0: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 ifnum
is notvolatile
.The text was updated successfully, but these errors were encountered: