Skip to content

Commit 23159ed

Browse files
committed
[𝘀𝗽𝗿] changes to main this commit is based on
Created using spr 1.3.4 [skip ci]
1 parent 3142dff commit 23159ed

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -288,26 +288,41 @@ bool SignalContext::IsStackOverflow() const {
288288

289289
#endif // SANITIZER_GO
290290

291+
static void SetNonBlock(int fd) {
292+
int res = fcntl(fd, F_GETFL, 0);
293+
CHECK(!internal_iserror(res, nullptr));
294+
295+
res |= O_NONBLOCK;
296+
res = fcntl(fd, F_SETFL, res);
297+
CHECK(!internal_iserror(res, nullptr));
298+
}
299+
291300
bool IsAccessibleMemoryRange(uptr beg, uptr size) {
292-
uptr page_size = GetPageSizeCached();
293-
// Checking too large memory ranges is slow.
294-
CHECK_LT(size, page_size * 10);
295-
int sock_pair[2];
296-
if (pipe(sock_pair))
297-
return false;
298-
uptr bytes_written =
299-
internal_write(sock_pair[1], reinterpret_cast<void *>(beg), size);
300-
int write_errno;
301-
bool result;
302-
if (internal_iserror(bytes_written, &write_errno)) {
303-
CHECK_EQ(EFAULT, write_errno);
304-
result = false;
305-
} else {
306-
result = (bytes_written == size);
301+
while (size) {
302+
// `read` from `sock_pair[0]` into a dummy buffer to free up the pipe buffer
303+
// for more `write` is slower than just recreating a pipe.
304+
int sock_pair[2];
305+
if (pipe(sock_pair))
306+
return false;
307+
308+
auto cleanup = at_scope_exit([&]() {
309+
internal_close(sock_pair[0]);
310+
internal_close(sock_pair[1]);
311+
});
312+
313+
SetNonBlock(sock_pair[1]);
314+
315+
int write_errno;
316+
uptr w = internal_write(sock_pair[1], reinterpret_cast<char *>(beg), size);
317+
if (internal_iserror(w, &write_errno)) {
318+
CHECK_EQ(EFAULT, write_errno);
319+
return false;
320+
}
321+
size -= w;
322+
beg += w;
307323
}
308-
internal_close(sock_pair[0]);
309-
internal_close(sock_pair[1]);
310-
return result;
324+
325+
return true;
311326
}
312327

313328
void PlatformPrepareForSandboxing(void *args) {

compiler-rt/lib/sanitizer_common/tests/sanitizer_posix_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ TEST(SanitizerCommon, IsAccessibleMemoryRange) {
8282
munmap((void *)mem, 3 * page_size);
8383
}
8484

85+
TEST(SanitizerCommon, IsAccessibleMemoryRangeLarge) {
86+
const int size = GetPageSize() * 10000;
87+
88+
uptr mem = (uptr)mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
89+
-1, 0);
90+
91+
EXPECT_TRUE(IsAccessibleMemoryRange(mem, size));
92+
93+
munmap((void *)mem, size);
94+
}
95+
8596
} // namespace __sanitizer
8697

8798
#endif // SANITIZER_POSIX

0 commit comments

Comments
 (0)