This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add a platform independent buffered file IO data structure.
ClosedPublic

Authored by sivachandra on Feb 10 2022, 9:56 AM.

Diff Detail

Event Timeline

sivachandra created this revision.Feb 10 2022, 9:56 AM
sivachandra requested review of this revision.Feb 10 2022, 9:56 AM

This is still WiP. Especially wrt testing, I have only added one test. Will add more and update soon.

Add more tests.

I will be making another update with either a .md document or detailed inline comments. I don't intend to extend the functionality anymore in this patch.

lntue added inline comments.Feb 11 2022, 11:47 AM
libc/include/CMakeLists.txt
126–127

I just notice that we have both *-* and *_* here. We should update to use just 1 style at some point.

libc/src/__support/File/file.cpp
171

Shouldn't you only need:

if (own_buf) free(this->buf);
libc/src/__support/File/file.h
145

comment ended early.

libc/test/src/__support/File/file_test.cpp
21

I feel like this class will be useful beyond this test and could be factored to a standalone util. Feel free to ignore this suggestion.

michaelrj added inline comments.Feb 11 2022, 12:00 PM
libc/src/__support/File/file.h
68

ownded -> owned

89–90

I think

(mode & (static_cast<ModeFlags>(OpenMode::WRITE) | static_cast<ModeFlags>(OpenMode::APPEND)));

is clearer.

libc/test/src/__support/File/CMakeLists.txt
2

this needs to be conditioned on malloc being available, which currently means if(LLVM_LIBC_INCLUDE_SCUDO OR NOT LLVM_LIBC_FULL_BUILD). I will go look into making a single variable that can encompass that in a cleanup patch.

  • Address comments
  • Add inline documentation on how to use the File data structure
sivachandra marked 2 inline comments as done.Feb 14 2022, 7:56 AM
sivachandra added inline comments.
libc/include/CMakeLists.txt
126–127

Agreed. I will make another pass to fix this.

libc/src/__support/File/file.cpp
171

The memory for the File data structure itself will be created by a malloc call so it has to be free-d at closing time. But, thanks for pointing out about the buffer. I have now added what you have suggested.

libc/src/__support/File/file.h
145

This is not it the correct place anymore, but I think I have addressed this comment.

libc/test/src/__support/File/CMakeLists.txt
2

Not required anymore after your fix from last week.

libc/test/src/__support/File/file_test.cpp
21

SGTM. I will move it to a common place as we start reusing it in other tests.

lntue added inline comments.Feb 14 2022, 8:47 AM
libc/src/__support/File/file.cpp
168

Do we need to add a check at the beginning of all other accessing methods to make sure that close has not been called?

sivachandra added inline comments.Feb 14 2022, 9:07 AM
libc/src/__support/File/file.cpp
168

Couple of things:

  1. It is undefined behavior to perform file operations on closed files. So, user code should not try to close an already closed file.
  2. The File data structure is not available after a close, which means it is use after free to perform any checking.
lntue accepted this revision.Feb 14 2022, 2:43 PM
This revision is now accepted and ready to land.Feb 14 2022, 2:43 PM

Add the file_test to libc_support_unittests suite.

This revision was landed with ongoing or failed builds.Feb 14 2022, 9:34 PM
This revision was automatically updated to reflect the committed changes.