1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <unistd.h>
#include "POSIXPrivate.h"
#include "WAVM/Inline/Assert.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/Errors.h"
#include "WAVM/Platform/Intrinsic.h"
#include "WAVM/Platform/Memory.h"
#include "WAVM/Platform/Mutex.h"
#ifdef __APPLE__
#define MAP_ANONYMOUS MAP_ANON
#endif
using namespace WAVM;
using namespace WAVM::Platform;
static Uptr internalGetPreferredVirtualPageSizeLog2()
{
U32 preferredVirtualPageSize = sysconf(_SC_PAGESIZE);
// Verify our assumption that the virtual page size is a power of two.
WAVM_ASSERT(!(preferredVirtualPageSize & (preferredVirtualPageSize - 1)));
return floorLogTwo(preferredVirtualPageSize);
}
Uptr Platform::getBytesPerPageLog2()
{
static Uptr preferredVirtualPageSizeLog2 = internalGetPreferredVirtualPageSizeLog2();
return preferredVirtualPageSizeLog2;
}
static U32 memoryAccessAsPOSIXFlag(MemoryAccess access)
{
switch(access)
{
default:
case MemoryAccess::none: return PROT_NONE;
case MemoryAccess::readOnly: return PROT_READ;
case MemoryAccess::readWrite: return PROT_READ | PROT_WRITE;
case MemoryAccess::readExecute: return PROT_READ | PROT_EXEC;
case MemoryAccess::readWriteExecute: return PROT_EXEC | PROT_READ | PROT_WRITE;
}
}
static bool isPageAligned(U8* address)
{
const Uptr addressBits = reinterpret_cast<Uptr>(address);
return (addressBits & (getBytesPerPage() - 1)) == 0;
}
U8* Platform::allocateVirtualPages(Uptr numPages)
{
Uptr numBytes = numPages << getBytesPerPageLog2();
void* result = mmap(nullptr, numBytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(result == MAP_FAILED)
{
if(errno != ENOMEM)
{
fprintf(stderr,
"mmap(0, %" WAVM_PRIuPTR
", PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) failed! errno=%s\n",
numBytes,
strerror(errno));
dumpErrorCallStack(0);
}
return nullptr;
}
return (U8*)result;
}
U8* Platform::allocateAlignedVirtualPages(Uptr numPages,
Uptr alignmentLog2,
U8*& outUnalignedBaseAddress)
{
const Uptr pageSizeLog2 = getBytesPerPageLog2();
const Uptr numBytes = numPages << pageSizeLog2;
if(alignmentLog2 > pageSizeLog2)
{
// Call mmap with enough padding added to the size to align the allocation within the
// unaligned mapping.
const Uptr alignmentBytes = 1ull << alignmentLog2;
U8* unalignedBaseAddress = (U8*)mmap(
nullptr, numBytes + alignmentBytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(unalignedBaseAddress == MAP_FAILED)
{
if(errno != ENOMEM)
{
fprintf(stderr,
"mmap(0, %" WAVM_PRIuPTR
", PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) failed! errno=%s\n",
numBytes + alignmentBytes,
strerror(errno));
dumpErrorCallStack(0);
}
return nullptr;
}
const Uptr address = reinterpret_cast<Uptr>(unalignedBaseAddress);
const Uptr alignedAddress = (address + alignmentBytes - 1) & ~(alignmentBytes - 1);
U8* result = reinterpret_cast<U8*>(alignedAddress);
// Unmap the start and end of the unaligned mapping, leaving the aligned mapping in the
// middle.
const Uptr numHeadPaddingBytes = alignedAddress - address;
if(numHeadPaddingBytes > 0)
{ WAVM_ERROR_UNLESS(!munmap(unalignedBaseAddress, numHeadPaddingBytes)); }
const Uptr numTailPaddingBytes = alignmentBytes - (alignedAddress - address);
if(numTailPaddingBytes > 0)
{ WAVM_ERROR_UNLESS(!munmap(result + (numPages << pageSizeLog2), numTailPaddingBytes)); }
outUnalignedBaseAddress = result;
return result;
}
else
{
outUnalignedBaseAddress = allocateVirtualPages(numPages);
return outUnalignedBaseAddress;
}
}
bool Platform::commitVirtualPages(U8* baseVirtualAddress, Uptr numPages, MemoryAccess access)
{
WAVM_ERROR_UNLESS(isPageAligned(baseVirtualAddress));
int result = mprotect(
baseVirtualAddress, numPages << getBytesPerPageLog2(), memoryAccessAsPOSIXFlag(access));
if(result != 0)
{
fprintf(stderr,
"mprotect(0x%" WAVM_PRIxPTR ", %" WAVM_PRIuPTR ", %u) failed: %s\n",
reinterpret_cast<Uptr>(baseVirtualAddress),
numPages << getBytesPerPageLog2(),
memoryAccessAsPOSIXFlag(access),
strerror(errno));
dumpErrorCallStack(0);
}
return result == 0;
}
bool Platform::setVirtualPageAccess(U8* baseVirtualAddress, Uptr numPages, MemoryAccess access)
{
WAVM_ERROR_UNLESS(isPageAligned(baseVirtualAddress));
int result = mprotect(
baseVirtualAddress, numPages << getBytesPerPageLog2(), memoryAccessAsPOSIXFlag(access));
if(result != 0)
{
fprintf(stderr,
"mprotect(0x%" WAVM_PRIxPTR ", %" WAVM_PRIuPTR ", %u) failed: %s\n",
reinterpret_cast<Uptr>(baseVirtualAddress),
numPages << getBytesPerPageLog2(),
memoryAccessAsPOSIXFlag(access),
strerror(errno));
dumpErrorCallStack(0);
}
return result == 0;
}
void Platform::decommitVirtualPages(U8* baseVirtualAddress, Uptr numPages)
{
WAVM_ERROR_UNLESS(isPageAligned(baseVirtualAddress));
auto numBytes = numPages << getBytesPerPageLog2();
if(mmap(baseVirtualAddress, numBytes, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
== MAP_FAILED)
{
Errors::fatalf("mmap(0x%" WAVM_PRIxPTR ", %" WAVM_PRIuPTR
", PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) failed: %s",
reinterpret_cast<Uptr>(baseVirtualAddress),
numBytes,
strerror(errno));
}
}
void Platform::freeVirtualPages(U8* baseVirtualAddress, Uptr numPages)
{
WAVM_ERROR_UNLESS(isPageAligned(baseVirtualAddress));
if(munmap(baseVirtualAddress, numPages << getBytesPerPageLog2()))
{
Errors::fatalf("munmap(0x%" WAVM_PRIxPTR ", %u) failed: %s",
reinterpret_cast<Uptr>(baseVirtualAddress),
numPages << getBytesPerPageLog2(),
strerror(errno));
}
}
void Platform::freeAlignedVirtualPages(U8* unalignedBaseAddress, Uptr numPages, Uptr alignmentLog2)
{
WAVM_ERROR_UNLESS(isPageAligned(unalignedBaseAddress));
if(munmap(unalignedBaseAddress, numPages << getBytesPerPageLog2()))
{
Errors::fatalf("munmap(0x%" WAVM_PRIxPTR ", %u) failed: %s",
reinterpret_cast<Uptr>(unalignedBaseAddress),
numPages << getBytesPerPageLog2(),
strerror(errno));
}
}
Uptr Platform::getPeakMemoryUsageBytes()
{
struct rusage ru;
WAVM_ERROR_UNLESS(!getrusage(RUSAGE_SELF, &ru));
#ifdef __APPLE__
// POSIX and even the Mac OS X docs say this is in KB, but it's actually in bytes.
return Uptr(ru.ru_maxrss);
#else
return Uptr(ru.ru_maxrss) * 1024;
#endif
}
|