Skip to content

Remove check on strong_count < 1. #32

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

Merged
merged 1 commit into from
Feb 3, 2021
Merged

Remove check on strong_count < 1. #32

merged 1 commit into from
Feb 3, 2021

Conversation

jsha
Copy link
Collaborator

@jsha jsha commented Jan 30, 2021

Previously, we tried to check against the possibility that C code calls
rustls_client_config_free twice. We did this by doing Arc::from_raw,
then checking if strong_count is < 1.

However, this was undefined behavior: deferencing a dangling pointer.
https://doc.rust-lang.org/reference/behavior-considered-undefined.html

If strong_count went to zero on some previous call, Arc would have
dropped its contents. That means the pointed-to memory is no longer
valid to access, and its contents are undefined. So we might see
strong_count of 1,000,000, or -1,000,000, or any other value; or monkeys
could fly out of our noses.

The C caller can still invoke undefined behavior by calling
rustls_client_config_free twice, but the previous change tried to
detect undefined behavior by invoking undefined behavior, which doesn't
work.

Previously, we tried to check against the possibility that C code calls
rustls_client_config_free twice. We did this by doing `Arc::from_raw`,
then checking if strong_count is < 1.

However, this was undefined behavior: deferencing a dangling pointer.
https://doc.rust-lang.org/reference/behavior-considered-undefined.html

If strong_count went to zero on some previous call, `Arc` would have
dropped its contents.  That means the pointed-to memory is no longer
valid to access, and its contents are undefined. So we might see
strong_count of 1,000,000, or -1,000,000, or any other value; or monkeys
could fly out of our noses.

The C caller can still invoke undefined behavior by calling
`rustls_client_config_free` twice, but the previous change tried to
detect undefined behavior by invoking undefined behavior, which doesn't
work.
@jsha
Copy link
Collaborator Author

jsha commented Jan 30, 2021

Thanks to @alex for pointing this out in #1 (comment).

@jsha jsha changed the title Remove safety check on strong_count < 1. Remove check on strong_count < 1. Jan 30, 2021
@jsha jsha requested a review from tgeoghegan January 30, 2021 02:20
@jsha jsha merged commit eddcb09 into main Feb 3, 2021
@jsha jsha deleted the remove-strongcount-check branch February 3, 2021 00:08
This was referenced Mar 10, 2021
jsha added a commit that referenced this pull request Mar 12, 2021
Order imports as: std, third-party, crate.

Use `try_ref_from_ptr!` to turn `out_n`'s into refs. This moves
unsafe blocks from later in the function to earlier in the function,
closer to where their safety properties are checked. Also, remove
pre-emptive zeroing of `out_n`'s. According to the conventions listed
in our README, there are no partial successes or partial failures,
so we shouldn't write to `out_n` if we early-return.

Remove `strong_count < 1` checks on Arc. Follow-up from #32.

Add some missing comments and fix some formatting.
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.

2 participants