Skip to content

Return codes of libc functions are checked for errors only in debug mode. #34966

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

Closed
ghost opened this issue Jul 21, 2016 · 3 comments · Fixed by #77761
Closed

Return codes of libc functions are checked for errors only in debug mode. #34966

ghost opened this issue Jul 21, 2016 · 3 comments · Fixed by #77761
Labels
C-bug Category: This is a bug. C-enhancement Category: An issue proposing an enhancement or a PR with one. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@ghost
Copy link

ghost commented Jul 21, 2016

In libstd/sys/unix there are numerous cases where return code is checked for
error using following pattern:

let result = libc::pthread_mutexattr_init(&mut attr as *mut _);
debug_assert_eq!(result, 0);

That means, that error checking is performed only when compiled in
non-optimized mode or after explicitly enabling debug assertions. This is a
little bit worrying, especially when used on platforms where those functions
can indeed fail and break safety.

@eefriedman
Copy link
Contributor

(For anyone who was confused by the given example, pthread_mutexattr_init can fail with ENOMEM.)

@ghost
Copy link
Author

ghost commented Jul 22, 2016

On some platforms types like pthread_mutex_t, pthread_cond_t, etc., are "fat"
structures that have sufficient size to hold everything necessary and their
initialization will never fail (unless initialized incorrectly). But on others
they are opaque pointers to void* and require an additional memory
allocation. Search for those typedefs in libc crate, gives a rough
approximation of affected platforms.

Sadly this also affects static mutexes, so even lock / unlock could potentially
fail. Either way you could end up with a mutex which is not mutually exclusive at
all.

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 25, 2017
@tmiasko
Copy link
Contributor

tmiasko commented Oct 7, 2020

When mutex initialization fails this leads to non-functioning mutex and unsoundness.

The pthread_mutex_lock might also fail for recursive mutexes used in std::io and cause a mismatch in the number of locks / unlocks, but one needs to go out of the way to cause that.

@rustbot modify labels: C-bug I-unsound

@rustbot rustbot added the C-bug Category: This is a bug. label Oct 7, 2020
@Aaron1011 Aaron1011 added the I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label Oct 20, 2020
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Oct 20, 2020
@bors bors closed this as completed in b09ef11 Oct 20, 2020
@LeSeulArtichaut LeSeulArtichaut removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Oct 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. C-enhancement Category: An issue proposing an enhancement or a PR with one. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants