This is an archive of the discontinued LLVM Phabricator instance.

File::SetDescriptor() should require options
ClosedPublic

Authored by lawrence_danna on Sep 19 2019, 9:23 PM.

Details

Summary

lvm_private::File::GetStream() can fail if m_options == 0

It's not clear from the header a File created with a descriptor will be
not be usable by many parts of LLDB unless SetOptions is also called,
but it is.

This is because those parts of LLDB rely on GetStream() to use the
file, and that in turn relies on calling fdopen on the descriptor. When
calling fdopen, GetStream relies on m_options to determine the access
mode. If m_options has never been set, GetStream() will fail.

This patch adds options as a required argument to File::SetDescriptor
and the corresponding constructor.

Event Timeline

lawrence_danna created this revision.Sep 19 2019, 9:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 19 2019, 9:23 PM
labath requested changes to this revision.Sep 20 2019, 5:36 AM
labath added a subscriber: labath.

It doesn't look like this will work on windows (I find no trace of fcntl in the windows docs). Even if we find a way to guess the open mode on windows somehow, I am not convinced that is the right way forward here. I would rather we find a way to fix the api so that it is harder to use incorrectly, then trying to guess what should happen when the api is used in a potentially incorrect way.

What is original reason you needed this patch for? Could we just return a null FILE* if the user didn't call SetOptions?

This revision now requires changes to proceed.Sep 20 2019, 5:36 AM

It doesn't look like this will work on windows (I find no trace of fcntl in the windows docs). Even if we find a way to guess the open mode on windows somehow, I am not convinced that is the right way forward here. I would rather we find a way to fix the api so that it is harder to use incorrectly, then trying to guess what should happen when the api is used in a potentially incorrect way.

What is original reason you needed this patch for? Could we just return a null FILE* if the user didn't call SetOptions?

While I was writing tests for my python files branch, I first tested the simple case of creating a SBFile with a descriptor, and found to my surprise that almost nothing in LLDB could actually write to that file, because everything expected a FILE* stream and it couldn't make one.

Should I just add an options argument to SetDescriptor instead?

Added options to SetDescriptor instead of using ::fcntl

lawrence_danna edited the summary of this revision. (Show Details)Sep 20 2019, 9:20 AM
lawrence_danna retitled this revision from bugfix: llvm_private::File::GetStream() can fail if m_options == 0 to File::SetDescriptor() should require options.
lawrence_danna edited the summary of this revision. (Show Details)
JDevlieghere accepted this revision.Sep 20 2019, 10:54 AM

Do we still need SetOptions after this? Are there cases where the value needs to change after construction? If not I would consider removing that function. Otherwise this LGTM if Pavel doesn't have any other concerns.

lawrence_danna edited the summary of this revision. (Show Details)

removed SetOptions()

labath accepted this revision.Sep 23 2019, 5:09 AM

This looks much better to me, but maybe you could add some unit tests for the use cases this fixes (I guess that's the use case of creating a File object with an fd, and the writing(?) to it).

This revision is now accepted and ready to land.Sep 23 2019, 5:09 AM

added unit test

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptSep 23 2019, 1:37 PM