Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,26 +288,43 @@ bool SignalContext::IsStackOverflow() const {

#endif // SANITIZER_GO

static void SetNonBlock(int fd) {
int res = fcntl(fd, F_GETFL, 0);
CHECK(!internal_iserror(res, nullptr));

res |= O_NONBLOCK;
res = fcntl(fd, F_SETFL, res);
CHECK(!internal_iserror(res, nullptr));
}

bool IsAccessibleMemoryRange(uptr beg, uptr size) {
uptr page_size = GetPageSizeCached();
// Checking too large memory ranges is slow.
CHECK_LT(size, page_size * 10);
int sock_pair[2];
if (pipe(sock_pair))
return false;
uptr bytes_written =
internal_write(sock_pair[1], reinterpret_cast<void *>(beg), size);
int write_errno;
bool result;
if (internal_iserror(bytes_written, &write_errno)) {
CHECK_EQ(EFAULT, write_errno);
result = false;
} else {
result = (bytes_written == size);
while (size) {
// `read` from `fds[0]` into a dummy buffer to free up the pipe buffer for
// more `write` is slower than just recreating a pipe.
int fds[2];
if (pipe(fds))
return false;

auto cleanup = at_scope_exit([&]() {
internal_close(fds[0]);
internal_close(fds[1]);
});

SetNonBlock(fds[1]);

int write_errno;
uptr w = internal_write(fds[1], reinterpret_cast<char *>(beg), size);
if (internal_iserror(w, &write_errno)) {
if (write_errno == EINTR)
continue;
CHECK_EQ(EFAULT, write_errno);
return false;
}
size -= w;
beg += w;
}
internal_close(sock_pair[0]);
internal_close(sock_pair[1]);
return result;

return true;
}

void PlatformPrepareForSandboxing(void *args) {
Expand Down
11 changes: 11 additions & 0 deletions compiler-rt/lib/sanitizer_common/tests/sanitizer_posix_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ TEST(SanitizerCommon, IsAccessibleMemoryRange) {
munmap((void *)mem, 3 * page_size);
}

TEST(SanitizerCommon, IsAccessibleMemoryRangeLarge) {
const int size = GetPageSize() * 10000;

uptr mem = (uptr)mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
-1, 0);

EXPECT_TRUE(IsAccessibleMemoryRange(mem, size));

munmap((void *)mem, size);
}

} // namespace __sanitizer

#endif // SANITIZER_POSIX
Loading