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 segfault if pthread_getattr_np fails #76521

Merged
merged 2 commits into from
Sep 21, 2020

Conversation

tavianator
Copy link
Contributor

@tavianator tavianator commented Sep 9, 2020

glibc destroys the passed pthread_attr_t if pthread_getattr_np()
fails. Destroying it again leads to a segfault. Fix it by only
destroying it on success for glibc.

glibc destroys[1] the passed pthread_attr_t if pthread_getattr_np()
fails.  Destroying it again leads to a segfault.  Fix it by only
destroying it on success for glibc.

[1]: https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=ce437205e41dc05653e435f6188768cccdd91c99;hb=HEAD#l205
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @LukasKalbertodt (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 9, 2020
@tmiasko
Copy link
Contributor

tmiasko commented Sep 9, 2020

Looking at documentation and implementation of pthread_getattr_np in glibc, musl, and NetBSD, the behaviour seems consistent. The function is responsible for attr initialization, so that pthread_attr_init is unnecessary, and attributes should be only destroyed on success.

On the other hand the pthread_attr_get_np expects already initialized attributes (FreeBSD man page contains somewhat unusual comment: "It is HIGHLY RECOMMENDED to use pthread_attr_init(3) function to allocate attribute storage.").

@tavianator
Copy link
Contributor Author

Ah that makes sense, I'll refactor the code to do that. I initially thought this was just a glibc oddity since musl doesn't call _destroy(), but the musl version just never fails so it doesn't need to.

…ibcs

The calling convention of pthread_getattr_np() is to initialize the
pthread_attr_t, so _destroy() is only necessary on success (and _init()
isn't necessary beforehand).  On the other hand, FreeBSD wants the
attr_t to be initialized before pthread_attr_get_np(), and therefore it
should always be destroyed afterwards.
@LukasKalbertodt
Copy link
Member

I'm not qualified to review this PR. @rust-lang/libs can anyone of you take this? Sorry :<

@Amanieu
Copy link
Member

Amanieu commented Sep 19, 2020

@bors r+

@bors
Copy link
Contributor

bors commented Sep 19, 2020

📌 Commit a684153 has been approved by Amanieu

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 19, 2020
RalfJung added a commit to RalfJung/rust that referenced this pull request Sep 19, 2020
…roy, r=Amanieu

Fix segfault if pthread_getattr_np fails

glibc [destroys][1] the passed pthread_attr_t if pthread_getattr_np()
fails.  Destroying it again leads to a segfault.  Fix it by only
destroying it on success for glibc.

[1]: https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=ce437205e41dc05653e435f6188768cccdd91c99;hb=HEAD#l205
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Sep 19, 2020
…roy, r=Amanieu

Fix segfault if pthread_getattr_np fails

glibc [destroys][1] the passed pthread_attr_t if pthread_getattr_np()
fails.  Destroying it again leads to a segfault.  Fix it by only
destroying it on success for glibc.

[1]: https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=ce437205e41dc05653e435f6188768cccdd91c99;hb=HEAD#l205
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Sep 19, 2020
…roy, r=Amanieu

Fix segfault if pthread_getattr_np fails

glibc [destroys][1] the passed pthread_attr_t if pthread_getattr_np()
fails.  Destroying it again leads to a segfault.  Fix it by only
destroying it on success for glibc.

[1]: https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=ce437205e41dc05653e435f6188768cccdd91c99;hb=HEAD#l205
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Sep 19, 2020
…roy, r=Amanieu

Fix segfault if pthread_getattr_np fails

glibc [destroys][1] the passed pthread_attr_t if pthread_getattr_np()
fails.  Destroying it again leads to a segfault.  Fix it by only
destroying it on success for glibc.

[1]: https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=ce437205e41dc05653e435f6188768cccdd91c99;hb=HEAD#l205
RalfJung added a commit to RalfJung/rust that referenced this pull request Sep 20, 2020
…roy, r=Amanieu

Fix segfault if pthread_getattr_np fails

glibc [destroys][1] the passed pthread_attr_t if pthread_getattr_np()
fails.  Destroying it again leads to a segfault.  Fix it by only
destroying it on success for glibc.

[1]: https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=ce437205e41dc05653e435f6188768cccdd91c99;hb=HEAD#l205
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Sep 21, 2020
…roy, r=Amanieu

Fix segfault if pthread_getattr_np fails

glibc [destroys][1] the passed pthread_attr_t if pthread_getattr_np()
fails.  Destroying it again leads to a segfault.  Fix it by only
destroying it on success for glibc.

[1]: https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=ce437205e41dc05653e435f6188768cccdd91c99;hb=HEAD#l205
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Sep 21, 2020
…roy, r=Amanieu

Fix segfault if pthread_getattr_np fails

glibc [destroys][1] the passed pthread_attr_t if pthread_getattr_np()
fails.  Destroying it again leads to a segfault.  Fix it by only
destroying it on success for glibc.

[1]: https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=ce437205e41dc05653e435f6188768cccdd91c99;hb=HEAD#l205
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 21, 2020
Rollup of 10 pull requests

Successful merges:

 - rust-lang#76439 (Add error explanation for E0755)
 - rust-lang#76521 (Fix segfault if pthread_getattr_np fails)
 - rust-lang#76835 (make replace_prefix only take &str as arguments )
 - rust-lang#76967 (Revert adding Atomic::from_mut.)
 - rust-lang#76977 (Add a regression test for copy propagation miscompilation)
 - rust-lang#76981 (liballoc bench use imported path Bencher)
 - rust-lang#76983 (BTreeMap: extra testing & fixed comments)
 - rust-lang#76996 (Fix typo in rustc_lexer docs)
 - rust-lang#77009 (Dogfood total_cmp in the test crate)
 - rust-lang#77012 (update Miri for another bugfix)

Failed merges:

 - rust-lang#76489 (Add explanation for E0756)

r? `@ghost`
@bors bors merged commit ae4b677 into rust-lang:master Sep 21, 2020
@rustbot rustbot added this to the 1.48.0 milestone Sep 21, 2020
@tavianator tavianator deleted the fix-pthread-getattr-destroy branch October 8, 2020 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants