Skip to content

Commit a354b41

Browse files
committed
[WIP] Windows (alt approach)
1 parent 4702b7e commit a354b41

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

llvm/include/llvm/Support/FileSystem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/Support/ErrorHandling.h"
3737
#include "llvm/Support/ErrorOr.h"
3838
#include "llvm/Support/FileSystem/UniqueID.h"
39+
#include "llvm/Support/IOSandbox.h"
3940
#include "llvm/Support/MD5.h"
4041
#include <cassert>
4142
#include <cstdint>
@@ -1447,6 +1448,8 @@ class directory_iterator {
14471448
explicit directory_iterator(const Twine &path, std::error_code &ec,
14481449
bool follow_symlinks = true)
14491450
: FollowSymlinks(follow_symlinks) {
1451+
sandbox::violationIfEnabled();
1452+
14501453
State = std::make_shared<detail::DirIterState>();
14511454
SmallString<128> path_storage;
14521455
ec = detail::directory_iterator_construct(
@@ -1456,6 +1459,8 @@ class directory_iterator {
14561459
explicit directory_iterator(const directory_entry &de, std::error_code &ec,
14571460
bool follow_symlinks = true)
14581461
: FollowSymlinks(follow_symlinks) {
1462+
sandbox::violationIfEnabled();
1463+
14591464
State = std::make_shared<detail::DirIterState>();
14601465
ec = detail::directory_iterator_construct(
14611466
*State, de.path(), FollowSymlinks);
@@ -1466,6 +1471,8 @@ class directory_iterator {
14661471

14671472
// No operator++ because we need error_code.
14681473
directory_iterator &increment(std::error_code &ec) {
1474+
sandbox::violationIfEnabled();
1475+
14691476
ec = directory_iterator_increment(*State);
14701477
return *this;
14711478
}

llvm/lib/Support/Windows/Path.inc

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ TimePoint<> basic_file_status::getLastModificationTime() const {
198198
uint32_t file_status::getLinkCount() const { return NumLinks; }
199199

200200
std::error_code current_path(SmallVectorImpl<char> &result) {
201+
sandbox::violationIfEnabled();
202+
201203
SmallVector<wchar_t, MAX_PATH> cur_path;
202204
DWORD len = MAX_PATH;
203205

@@ -226,6 +228,8 @@ std::error_code current_path(SmallVectorImpl<char> &result) {
226228
}
227229

228230
std::error_code set_current_path(const Twine &path) {
231+
sandbox::violationIfEnabled();
232+
229233
// Convert to utf-16.
230234
SmallVector<wchar_t, 128> wide_path;
231235
if (std::error_code ec = widenPath(path, wide_path))
@@ -350,6 +354,8 @@ static std::error_code is_local_internal(SmallVectorImpl<wchar_t> &Path,
350354
}
351355

352356
std::error_code is_local(const Twine &path, bool &result) {
357+
sandbox::violationIfEnabled();
358+
353359
if (!llvm::sys::fs::exists(path) || !llvm::sys::path::has_root_path(path))
354360
return make_error_code(errc::no_such_file_or_directory);
355361

@@ -413,6 +419,8 @@ static std::error_code realPathFromHandle(HANDLE H,
413419
}
414420

415421
std::error_code is_local(int FD, bool &Result) {
422+
sandbox::violationIfEnabled();
423+
416424
SmallVector<wchar_t, 128> FinalPath;
417425
HANDLE Handle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
418426

@@ -635,6 +643,8 @@ std::error_code resize_file_sparse(int FD, uint64_t Size) {
635643
}
636644

637645
std::error_code access(const Twine &Path, AccessMode Mode) {
646+
sandbox::violationIfEnabled();
647+
638648
SmallVector<wchar_t, 128> PathUtf16;
639649

640650
if (std::error_code EC = widenPath(Path, PathUtf16))
@@ -674,6 +684,8 @@ bool equivalent(file_status A, file_status B) {
674684
}
675685

676686
std::error_code equivalent(const Twine &A, const Twine &B, bool &result) {
687+
sandbox::violationIfEnabled();
688+
677689
file_status fsA, fsB;
678690
if (std::error_code ec = status(A, fsA))
679691
return ec;
@@ -789,6 +801,8 @@ handle_status_error:
789801
}
790802

791803
std::error_code status(const Twine &path, file_status &result, bool Follow) {
804+
sandbox::violationIfEnabled();
805+
792806
SmallString<128> path_storage;
793807
SmallVector<wchar_t, 128> path_utf16;
794808

@@ -823,11 +837,15 @@ std::error_code status(const Twine &path, file_status &result, bool Follow) {
823837
}
824838

825839
std::error_code status(int FD, file_status &Result) {
840+
sandbox::violationIfEnabled();
841+
826842
HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
827843
return getStatus(FileHandle, Result);
828844
}
829845

830846
std::error_code status(file_t FileHandle, file_status &Result) {
847+
sandbox::violationIfEnabled();
848+
831849
return getStatus(FileHandle, Result);
832850
}
833851

@@ -1242,6 +1260,8 @@ static std::error_code openNativeFileInternal(const Twine &Name,
12421260
Expected<file_t> openNativeFile(const Twine &Name, CreationDisposition Disp,
12431261
FileAccess Access, OpenFlags Flags,
12441262
unsigned Mode) {
1263+
sandbox::violationIfEnabled();
1264+
12451265
// Verify that we don't have both "append" and "excl".
12461266
assert((!(Disp == CD_CreateNew) || !(Flags & OF_Append)) &&
12471267
"Cannot specify both 'CreateNew' and 'Append' file creation flags!");
@@ -1277,6 +1297,8 @@ Expected<file_t> openNativeFile(const Twine &Name, CreationDisposition Disp,
12771297
std::error_code openFile(const Twine &Name, int &ResultFD,
12781298
CreationDisposition Disp, FileAccess Access,
12791299
OpenFlags Flags, unsigned int Mode) {
1300+
sandbox::violationIfEnabled();
1301+
12801302
Expected<file_t> Result = openNativeFile(Name, Disp, Access, Flags);
12811303
if (!Result)
12821304
return errorToErrorCode(Result.takeError());
@@ -1300,12 +1322,16 @@ static std::error_code directoryRealPath(const Twine &Name,
13001322
std::error_code openFileForRead(const Twine &Name, int &ResultFD,
13011323
OpenFlags Flags,
13021324
SmallVectorImpl<char> *RealPath) {
1325+
sandbox::violationIfEnabled();
1326+
13031327
Expected<HANDLE> NativeFile = openNativeFileForRead(Name, Flags, RealPath);
13041328
return nativeFileToFd(std::move(NativeFile), ResultFD, OF_None);
13051329
}
13061330

13071331
Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags,
13081332
SmallVectorImpl<char> *RealPath) {
1333+
sandbox::violationIfEnabled();
1334+
13091335
Expected<file_t> Result =
13101336
openNativeFile(Name, CD_OpenExisting, FA_Read, Flags);
13111337

@@ -1324,9 +1350,9 @@ file_t getStdinHandle() { return ::GetStdHandle(STD_INPUT_HANDLE); }
13241350
file_t getStdoutHandle() { return ::GetStdHandle(STD_OUTPUT_HANDLE); }
13251351
file_t getStderrHandle() { return ::GetStdHandle(STD_ERROR_HANDLE); }
13261352

1327-
Expected<size_t> readNativeFileImpl(file_t FileHandle,
1328-
MutableArrayRef<char> Buf,
1329-
OVERLAPPED *Overlap) {
1353+
static Expected<size_t> readNativeFileImpl(file_t FileHandle,
1354+
MutableArrayRef<char> Buf,
1355+
OVERLAPPED *Overlap) {
13301356
// ReadFile can only read 2GB at a time. The caller should check the number of
13311357
// bytes and read in a loop until termination.
13321358
DWORD BytesToRead =
@@ -1342,12 +1368,16 @@ Expected<size_t> readNativeFileImpl(file_t FileHandle,
13421368
}
13431369

13441370
Expected<size_t> readNativeFile(file_t FileHandle, MutableArrayRef<char> Buf) {
1371+
sandbox::violationIfEnabled();
1372+
13451373
return readNativeFileImpl(FileHandle, Buf, /*Overlap=*/nullptr);
13461374
}
13471375

13481376
Expected<size_t> readNativeFileSlice(file_t FileHandle,
13491377
MutableArrayRef<char> Buf,
13501378
uint64_t Offset) {
1379+
sandbox::violationIfEnabled();
1380+
13511381
OVERLAPPED Overlapped = {};
13521382
Overlapped.Offset = uint32_t(Offset);
13531383
Overlapped.OffsetHigh = uint32_t(Offset >> 32);
@@ -1492,6 +1522,8 @@ void expand_tilde(const Twine &path, SmallVectorImpl<char> &dest) {
14921522

14931523
std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest,
14941524
bool expand_tilde) {
1525+
sandbox::violationIfEnabled();
1526+
14951527
dest.clear();
14961528
if (path.isTriviallyEmpty())
14971529
return std::error_code();

0 commit comments

Comments
 (0)