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

Strengthen memory_order for refcount_down #433

Merged
merged 3 commits into from
Sep 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions include/aws/cryptosdk/materials.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ struct aws_cryptosdk_dec_materials {
*/
AWS_CRYPTOSDK_STATIC_INLINE bool aws_cryptosdk_private_refcount_down(struct aws_atomic_var *refcount) {
/*
* Memory ordering note: We must use release memory order here. Otherwise, we have the following race:
* Memory ordering note: We must use release_acquire memory order here. Otherwise, we have the following race:
*
* Program order:
*
Expand All @@ -246,11 +246,12 @@ AWS_CRYPTOSDK_STATIC_INLINE bool aws_cryptosdk_private_refcount_down(struct aws_
* Thread A: free(obj)
* Thread B: obj->foo = 1
*
* To prevent this we use release order, which forbids any memory accesses coming before this point
* from being reordered later. This prevents thread B from reordering the obj->foo access to come after
* the down().
* To prevent this we use release_acquire order. The release forbids any memory accesses sequenced-before the
* atomic decrement of down() from being reordered later. This prevents thread B from reordering the obj->foo
* access to come after the down(). The acquire ensures that the atomic decrements of down() calls correctly
* synchronize-with one-another.
*/
size_t old_count = aws_atomic_fetch_sub_explicit(refcount, 1, aws_memory_order_release);
size_t old_count = aws_atomic_fetch_sub_explicit(refcount, 1, aws_memory_order_acq_rel);

assert(old_count != 0);

Expand Down