Skip to content

Conversation

@qwwdfsad
Copy link
Member

@qwwdfsad qwwdfsad commented Aug 9, 2023

It has a benign JVM data-race on the hashCode field, but on K/N LLVM has no guarantees it will read default or written value (e.g. OoTA is one of the possibilities). The recommendation from K/N folks is to use @volatile here

Fixes #190

It has a benign JVM data-race on the hashCode field, but on K/N LLVM has no guarantees it will read default or written value (e.g. OoTA is one of the possibilities).
The recommendation from K/N folks is to use @volatile here

Fixes #190
@fzhinkin
Copy link
Collaborator

OoTA is one of the possibilities

I thought it shouldn't, but:

If there is a race on a given memory location, loads from that location return undef.

https://llvm.org/docs/Atomics.html#notatomic

public constructor(data: ByteArray, startIndex: Int = 0, endIndex: Int = data.size) :
this(data.copyOfRange(startIndex, endIndex), null)

@Volatile
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to add an expect annotation like VolatileInNative and actualize it with Volatile only in native?

Like in this patch

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nicest alternative would be to somehow perform load/store w/ relaxed memory ordering, though:
https://godbolt.org/z/rEdcobsW3

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a slightly different version, please check it out.

The nicest alternative would be

Yep. I agree we ruled it out as "no use cases" during the initial design 😅

@qwwdfsad qwwdfsad requested a review from fzhinkin August 10, 2023 10:07
Copy link
Collaborator

@fzhinkin fzhinkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the small typo, everything LGTM. Thanks for resolving the issue!

Co-authored-by: Filipp Zhinkin <filipp.zhinkin@jetbrains.com>
@qwwdfsad qwwdfsad merged commit 8836876 into develop Aug 10, 2023
@qwwdfsad qwwdfsad deleted the fix-kn-race branch August 10, 2023 11:53
@fzhinkin fzhinkin linked an issue Sep 13, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible UB in ByteString hash function

3 participants