Skip to content

Commit a05743a

Browse files
eholkCommit Bot
authored andcommitted
Stop allocating RW memory in AllocateGuarded
AllocateGuarded previously fell back on Allocate and then called Guard to set the protection to PROT_NONE. Linux commits RW memory, but the important thing here is to reserve the address space without committing it. This change adds a new variant of Allocate that takes explicit permission bits so that AllocateGuarded allocates non-RW memory from the beginning. Bug: v8:6320 Change-Id: I7962acbed09938951bf3bb4af2d1f302adba2547 Reviewed-on: https://chromium-review.googlesource.com/491928 Commit-Queue: Eric Holk <eholk@chromium.org> Reviewed-by: Mircea Trofin <mtrofin@chromium.org> Reviewed-by: Jochen Eisinger <jochen@chromium.org> Cr-Commit-Position: refs/heads/master@{#45075}
1 parent bf74d43 commit a05743a

13 files changed

Lines changed: 183 additions & 39 deletions

src/base/platform/platform-aix.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ double AIXTimezoneCache::LocalTimeOffset() {
7171

7272
TimezoneCache* OS::CreateTimezoneCache() { return new AIXTimezoneCache(); }
7373

74-
void* OS::Allocate(const size_t requested, size_t* allocated, bool executable) {
74+
void* OS::Allocate(const size_t requested, size_t* allocated,
75+
OS::MemoryPermission access) {
7576
const size_t msize = RoundUp(requested, getpagesize());
76-
int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0);
77+
int prot = GetProtectionFromMemoryPermission(access);
7778
void* mbase = mmapHelper(msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
7879

7980
if (mbase == MAP_FAILED) return NULL;

src/base/platform/platform-cygwin.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,10 @@ double CygwinTimezoneCache::LocalTimeOffset() {
5555
(loc->tm_isdst > 0 ? 3600 * msPerSecond : 0));
5656
}
5757

58-
59-
void* OS::Allocate(const size_t requested,
60-
size_t* allocated,
61-
bool is_executable) {
58+
void* OS::Allocate(const size_t requested, size_t* allocated,
59+
OS::MemoryPermission access) {
6260
const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE));
63-
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
61+
int prot = GetProtectionFromMemoryPermission(access);
6462
void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
6563
if (mbase == MAP_FAILED) return NULL;
6664
*allocated = msize;

src/base/platform/platform-freebsd.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ namespace base {
3737

3838
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
3939

40-
void* OS::Allocate(const size_t requested,
41-
size_t* allocated,
42-
bool executable) {
40+
void* OS::Allocate(const size_t requested, size_t* allocated,
41+
OS::MemoryPermission access) {
4342
const size_t msize = RoundUp(requested, getpagesize());
44-
int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0);
43+
int prot = GetProtectionFromMemoryPermission(access);
4544
void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
4645

4746
if (mbase == MAP_FAILED) return NULL;

src/base/platform/platform-linux.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ bool OS::ArmUsingHardFloat() {
9595
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
9696

9797
void* OS::Allocate(const size_t requested, size_t* allocated,
98-
bool is_executable) {
98+
OS::MemoryPermission access) {
9999
const size_t msize = RoundUp(requested, AllocateAlignment());
100-
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
100+
int prot = GetProtectionFromMemoryPermission(access);
101101
void* addr = OS::GetRandomMmapAddr();
102102
void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
103103
if (mbase == MAP_FAILED) return NULL;

src/base/platform/platform-macos.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,10 @@ namespace base {
5151
static const int kMmapFd = VM_MAKE_TAG(255);
5252
static const off_t kMmapFdOffset = 0;
5353

54-
55-
void* OS::Allocate(const size_t requested,
56-
size_t* allocated,
57-
bool is_executable) {
54+
void* OS::Allocate(const size_t requested, size_t* allocated,
55+
OS::MemoryPermission access) {
5856
const size_t msize = RoundUp(requested, getpagesize());
59-
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
57+
int prot = GetProtectionFromMemoryPermission(access);
6058
void* mbase = mmap(OS::GetRandomMmapAddr(),
6159
msize,
6260
prot,

src/base/platform/platform-openbsd.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ namespace base {
3535

3636
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
3737

38-
void* OS::Allocate(const size_t requested,
39-
size_t* allocated,
40-
bool is_executable) {
38+
void* OS::Allocate(const size_t requested, size_t* allocated,
39+
OS::MemoryPermission access) {
4140
const size_t msize = RoundUp(requested, AllocateAlignment());
42-
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
41+
int prot = GetProtectionFromMemoryPermission(access);
4342
void* addr = OS::GetRandomMmapAddr();
4443
void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
4544
if (mbase == MAP_FAILED) return NULL;

src/base/platform/platform-posix.cc

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,24 @@ intptr_t OS::CommitPageSize() {
101101
return page_size;
102102
}
103103

104+
void* OS::Allocate(const size_t requested, size_t* allocated,
105+
bool is_executable) {
106+
return OS::Allocate(requested, allocated,
107+
is_executable ? OS::MemoryPermission::kReadWriteExecute
108+
: OS::MemoryPermission::kReadWrite);
109+
}
110+
104111
void* OS::AllocateGuarded(const size_t requested) {
105112
size_t allocated = 0;
106-
const bool is_executable = false;
107-
void* mbase = OS::Allocate(requested, &allocated, is_executable);
113+
void* mbase =
114+
OS::Allocate(requested, &allocated, OS::MemoryPermission::kNoAccess);
108115
if (allocated != requested) {
109116
OS::Free(mbase, allocated);
110117
return nullptr;
111118
}
112119
if (mbase == nullptr) {
113120
return nullptr;
114121
}
115-
OS::Guard(mbase, requested);
116122
return mbase;
117123
}
118124

@@ -776,5 +782,17 @@ void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
776782
USE(result);
777783
}
778784

785+
int GetProtectionFromMemoryPermission(OS::MemoryPermission access) {
786+
switch (access) {
787+
case OS::MemoryPermission::kNoAccess:
788+
return PROT_NONE;
789+
case OS::MemoryPermission::kReadWrite:
790+
return PROT_READ | PROT_WRITE;
791+
case OS::MemoryPermission::kReadWriteExecute:
792+
return PROT_READ | PROT_WRITE | PROT_EXEC;
793+
}
794+
UNREACHABLE();
795+
}
796+
779797
} // namespace base
780798
} // namespace v8

src/base/platform/platform-posix.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef V8_BASE_PLATFORM_PLATFORM_POSIX_H_
66
#define V8_BASE_PLATFORM_PLATFORM_POSIX_H_
77

8+
#include "src/base/platform/platform.h"
89
#include "src/base/timezone-cache.h"
910

1011
namespace v8 {
@@ -22,6 +23,8 @@ class PosixTimezoneCache : public TimezoneCache {
2223
static const int msPerSecond = 1000;
2324
};
2425

26+
int GetProtectionFromMemoryPermission(OS::MemoryPermission access);
27+
2528
} // namespace base
2629
} // namespace v8
2730

src/base/platform/platform-qnx.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,10 @@ bool OS::ArmUsingHardFloat() {
8686

8787
TimezoneCache* OS::CreateTimezoneCache() { return new PosixTimezoneCache(); }
8888

89-
void* OS::Allocate(const size_t requested,
90-
size_t* allocated,
91-
bool is_executable) {
89+
void* OS::Allocate(const size_t requested, size_t* allocated,
90+
OS::MemoryPermission access) {
9291
const size_t msize = RoundUp(requested, AllocateAlignment());
93-
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
92+
int prot = GetProtectionFromMemoryPermission(access);
9493
void* addr = OS::GetRandomMmapAddr();
9594
void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
9695
if (mbase == MAP_FAILED) return NULL;

src/base/platform/platform-solaris.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ double SolarisTimezoneCache::LocalTimeOffset() {
5858

5959
TimezoneCache* OS::CreateTimezoneCache() { return new SolarisTimezoneCache(); }
6060

61-
void* OS::Allocate(const size_t requested,
62-
size_t* allocated,
63-
bool is_executable) {
61+
void* OS::Allocate(const size_t requested, size_t* allocated,
62+
OS::MemoryPermission access) {
6463
const size_t msize = RoundUp(requested, getpagesize());
65-
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
64+
int prot = GetProtectionFromMemoryPermission(access);
6665
void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
6766

6867
if (mbase == MAP_FAILED) return NULL;

0 commit comments

Comments
 (0)