Skip to content
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

Fix potential UB in ByteString.hashCode #208

Merged
merged 3 commits into from
Aug 10, 2023
Merged

Fix potential UB in ByteString.hashCode #208

merged 3 commits into from
Aug 10, 2023

Conversation

qwwdfsad
Copy link
Collaborator

@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

@@ -69,6 +70,7 @@ public class ByteString private constructor(
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
Collaborator 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!

bytestring/common/src/-Platform.kt Outdated Show resolved Hide resolved
@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
2 participants