This is an archive of the discontinued LLVM Phabricator instance.

Call FlushFileBuffers on readwrite file mappings.
ClosedPublic

Authored by zturner on Feb 5 2018, 11:37 AM.

Details

Summary

Due to a Windows kernel bug, calling FlushFileBuffers is necessary to ensure that subsequent processes can correctly read the data we write. More information is available in this connect issue. The Windows Kernel team is also aware and from my understanding they are investigating. Until we have more clear guidance about what versions of Windows this affects, I'm adding this call to FlushFileBuffers for all readwrite mappings on all versions of Windows.

Diff Detail

Repository
rL LLVM

Event Timeline

zturner created this revision.Feb 5 2018, 11:37 AM
amccarth added inline comments.
llvm/lib/Support/Windows/Path.inc
900 ↗(On Diff #132872)

How about including the URL from the patch description directly in the comment?

zturner added inline comments.Feb 5 2018, 1:04 PM
llvm/lib/Support/Windows/Path.inc
900 ↗(On Diff #132872)

I thought about it, but it would wrap past 80 characters (not a huge deal, admittedly). More importantly though, it doesn't contain any kind of workaround or acknowledgement from Microsoft other than "we did the same thing in link.exe". I was hoping that if / when we get more concrete information from someone on the kernel team that I could update it with that, since that would actually be some additional info besides what's in this comment. That said, if anyone feels strongly, I can add it here in the meantime (although it doesn't really say much that this comment doesn't already say).

amccarth added inline comments.Feb 5 2018, 1:07 PM
llvm/lib/Support/Windows/Path.inc
900 ↗(On Diff #132872)

SGTM.

rnk added a comment.Feb 5 2018, 3:18 PM

One concern I have is that this is a common pattern on Linux:

int fd = open(...);
void *mem = mmap(fd, ...);
close(fd);
// use mem

Are we reasonably confident that LLVM doesn't have this pattern anywhere in it? i.e. we never store the FD and then try to use it after closing it?

In D42925#998354, @rnk wrote:

One concern I have is that this is a common pattern on Linux:

int fd = open(...);
void *mem = mmap(fd, ...);
close(fd);
// use mem

Are we reasonably confident that LLVM doesn't have this pattern anywhere in it? i.e. we never store the FD and then try to use it after closing it?

We don't do that anywhere *at the moment* that I can find, but I suppose it's a concern for future proofing this code. Someone could come along and add code like this after the fact. Currently the only place where we open a mapped_file_region for readwrite is in OnDiskBuffer, which stores a TempFile and then renames it atomically to the final path. We could put the flush logic inside of TempFile::keep() so that it flushes it before renaming. That might be a better option, although it wouldn't catch the case where someone comes along later and tries to use a mapped_file_region for something else. Perhaps it's ok for the time being though?

rnk accepted this revision.Feb 5 2018, 3:40 PM
In D42925#998354, @rnk wrote:

One concern I have is that this is a common pattern on Linux:

int fd = open(...);
void *mem = mmap(fd, ...);
close(fd);
// use mem

Are we reasonably confident that LLVM doesn't have this pattern anywhere in it? i.e. we never store the FD and then try to use it after closing it?

We don't do that anywhere *at the moment* that I can find, but I suppose it's a concern for future proofing this code. Someone could come along and add code like this after the fact. Currently the only place where we open a mapped_file_region for readwrite is in OnDiskBuffer, which stores a TempFile and then renames it atomically to the final path. We could put the flush logic inside of TempFile::keep() so that it flushes it before renaming. That might be a better option, although it wouldn't catch the case where someone comes along later and tries to use a mapped_file_region for something else. Perhaps it's ok for the time being though?

It's probably fine. I tried to look for this pattern, but got lazy and punted it to you.

This revision is now accepted and ready to land.Feb 5 2018, 3:40 PM
thakis added a subscriber: thakis.Feb 15 2018, 7:07 AM

Did this land?

This revision was automatically updated to reflect the committed changes.