This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Check for and use ptsname_r if available
ClosedPublic

Authored by labath on Oct 2 2020, 4:49 AM.

Details

Summary

ptsname is not thread-safe. ptsname_r is available on most (but not all)
systems -- use it preferentially.

In the patch I also improve the thread-safety of the ptsname fallback
path by wrapping it in a mutex. This should guarantee the safety of a
typical ptsname implementation using a single static buffer, as long as
all callers go through this function.

I also remove the error arguments, as the only way this function can
fail is if the "primary" fd is not valid. This is a programmer error as
this requirement is documented, and all callers ensure that is the case.

Diff Detail

Event Timeline

labath created this revision.Oct 2 2020, 4:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 2 2020, 4:49 AM
Herald added a subscriber: emaste. · View Herald Transcript
labath requested review of this revision.Oct 2 2020, 4:49 AM
mgorny added inline comments.Oct 2 2020, 5:58 AM
lldb/source/Host/common/PseudoTerminal.cpp
149

I would really feel better with a real error handling here. It shouldn't be hard to use ErrorOr here.

labath added inline comments.Oct 2 2020, 6:30 AM
lldb/source/Host/common/PseudoTerminal.cpp
149

Yeah, but what are you going to do with that value? Pass it to the caller? The existing callers are ignoring the error return anyway, and I don't want to add error handling everywhere as afaict, this function can't fail unless the user messes up the master state (which is not something I want to support).

mgorny added inline comments.Oct 2 2020, 6:48 AM
lldb/source/Host/common/PseudoTerminal.cpp
149

I get your point but I've literally wasted days because of missing error handling, so I'd really preferred if we wouldn't make it even worse. Though I guess assert is good enough.

labath added inline comments.Oct 2 2020, 7:01 AM
lldb/source/Host/common/PseudoTerminal.cpp
149

In some ways it's even better because it will point you straight to the place where the assumption is violated, whereas a propagated logic error can manifest itself much farther away (or not at all). :)

MaskRay added inline comments.Oct 2 2020, 8:53 AM
lldb/source/Host/common/PseudoTerminal.cpp
149

If ptsname/ptsname_r fails, buf will be uninitialized and trigger a use-of-uninitialized-value error.

149

... in a -DLLVM_ENABLE_ASSERTIONS=off build.

This probably still needs some protection.

labath added inline comments.Oct 5 2020, 4:35 AM
lldb/source/Host/common/PseudoTerminal.cpp
149

What kind of protection did you have it mind? Initialize the buffer to an empty string?

MaskRay accepted this revision.Oct 6 2020, 4:48 PM

LGTM. Worth mentioning that this will be in POSIX issue 8 https://www.austingroupbugs.net/bug_view_page.php?bug_id=508

lldb/source/Host/common/PseudoTerminal.cpp
149

In the case of a non-zero return value, buf[0] = '\0' is probably sufficient to avoid an uninitialized value.

This revision is now accepted and ready to land.Oct 6 2020, 4:48 PM
labath added a comment.Oct 7 2020, 6:24 AM

LGTM. Worth mentioning that this will be in POSIX issue 8 https://www.austingroupbugs.net/bug_view_page.php?bug_id=508

Oh, cool. I didn't know posix is still being worked on :P

lldb/source/Host/common/PseudoTerminal.cpp
149

sounds good.

This revision was landed with ongoing or failed builds.Oct 7 2020, 6:29 AM
This revision was automatically updated to reflect the committed changes.