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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
#include "WAVM/Platform/Memory.h"
#include <stdint.h>
#include <string.h>
#include <algorithm>
#include <atomic>
#include <memory>
#include <vector>
#include "RuntimePrivate.h"
#include "WAVM/IR/IR.h"
#include "WAVM/IR/Types.h"
#include "WAVM/IR/Value.h"
#include "WAVM/Inline/Assert.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Platform/Intrinsic.h"
#include "WAVM/Platform/RWMutex.h"
#include "WAVM/Runtime/Runtime.h"
#include "WAVM/RuntimeABI/RuntimeABI.h"
using namespace WAVM;
using namespace WAVM::Runtime;
namespace WAVM { namespace Runtime {
WAVM_DEFINE_INTRINSIC_MODULE(wavmIntrinsicsMemory)
}}
// Global lists of memories; used to query whether an address is reserved by one of them.
static Platform::RWMutex memoriesMutex;
static std::vector<Memory*> memories;
static thread_local Memory* CurrentMemory = nullptr;
__attribute__((__noinline__)) void Memory::setCurrentMemory(Memory* memory)
{
CurrentMemory = memory;
}
__attribute__((__noinline__)) Memory* Memory::getCurrentMemory()
{
return CurrentMemory;
}
static constexpr U64 maxMemory64WASMPages =
#if WAVM_ENABLE_TSAN
(U64(8) * 1024 * 1024 * 1024) >> IR::numBytesPerPageLog2; // 8GB
#else
(U64(1) * 1024 * 1024 * 1024 * 1024) >> IR::numBytesPerPageLog2; // 1TB
#endif
static Uptr getPlatformPagesPerWebAssemblyPageLog2()
{
WAVM_ERROR_UNLESS(Platform::getBytesPerPageLog2() <= IR::numBytesPerPageLog2);
return IR::numBytesPerPageLog2 - Platform::getBytesPerPageLog2();
}
static Memory* createMemoryImpl(Compartment* compartment,
IR::MemoryType type,
std::string&& debugName,
ResourceQuotaRefParam resourceQuota)
{
Memory* memory = new Memory(compartment, type, std::move(debugName), resourceQuota);
if(type.indexType == IR::IndexType::i32)
{
static_assert(sizeof(Uptr) == 8, "WAVM's runtime requires a 64-bit host");
}
// Grow the memory to the type's minimum size.
if(growMemory(memory, type.size.min) != GrowResult::success)
{
delete memory;
return nullptr;
}
// Add the memory to the global array.
{
Platform::RWMutex::ExclusiveLock memoriesLock(memoriesMutex);
memories.push_back(memory);
}
return memory;
}
Memory* Runtime::createMemory(Compartment* compartment,
IR::MemoryType type,
std::string&& debugName,
ResourceQuotaRefParam resourceQuota)
{
WAVM_ASSERT(type.size.min <= UINTPTR_MAX);
Memory* memory = createMemoryImpl(compartment, type, std::move(debugName), resourceQuota);
if(!memory) { return nullptr; }
// Add the memory to the compartment's memories IndexMap.
{
Platform::RWMutex::ExclusiveLock compartmentLock(compartment->mutex);
memory->id = compartment->memories.add(UINTPTR_MAX, memory);
if(memory->id == UINTPTR_MAX)
{
delete memory;
return nullptr;
}
MemoryRuntimeData& runtimeData = compartment->runtimeData->memories[memory->id];
runtimeData.base = memory->getBaseAddress();
runtimeData.endAddress = memory->getNumReservedBytes();
runtimeData.numPages.store(memory->numPages.load(std::memory_order_acquire),
std::memory_order_release);
}
return memory;
}
Memory* Runtime::cloneMemory(Memory* memory, Compartment* newCompartment)
{
Platform::RWMutex::ExclusiveLock resizingLock(memory->resizingMutex);
const IR::MemoryType memoryType = getMemoryType(memory);
std::string debugName = memory->debugName;
Memory* newMemory
= createMemoryImpl(newCompartment, memoryType, std::move(debugName), memory->resourceQuota);
if(!newMemory) { return nullptr; }
// Copy the memory contents to the new memory.
memcpy(newMemory->getBaseAddress(), memory->getBaseAddress(), memoryType.size.min * IR::numBytesPerPage);
resizingLock.unlock();
// Insert the memory in the new compartment's memories array with the same index as it had in
// the original compartment's memories IndexMap.
{
Platform::RWMutex::ExclusiveLock compartmentLock(newCompartment->mutex);
newMemory->id = memory->id;
newCompartment->memories.insertOrFail(newMemory->id, newMemory);
MemoryRuntimeData& runtimeData = newCompartment->runtimeData->memories[newMemory->id];
runtimeData.base = newMemory->getBaseAddress();
runtimeData.numPages.store(newMemory->numPages, std::memory_order_release);
runtimeData.endAddress = newMemory->getNumReservedBytes();
}
return newMemory;
}
U8* Runtime::Memory::getBaseAddress()
{
return static_cast<U8*>(data.getData());
}
size_t Runtime::Memory::getNumReservedBytes()
{
return data.getNumReservedBytes();
}
Runtime::Memory::~Memory()
{
if(id != UINTPTR_MAX)
{
WAVM_ASSERT_RWMUTEX_IS_EXCLUSIVELY_LOCKED_BY_CURRENT_THREAD(compartment->mutex);
WAVM_ASSERT(compartment->memories[id] == this);
compartment->memories.removeOrFail(id);
MemoryRuntimeData& runtimeData = compartment->runtimeData->memories[id];
WAVM_ASSERT(runtimeData.base == getBaseAddress());
runtimeData.base = nullptr;
runtimeData.numPages.store(0, std::memory_order_release);
runtimeData.endAddress = 0;
}
// Remove the memory from the global array.
{
Platform::RWMutex::ExclusiveLock memoriesLock(memoriesMutex);
for(Uptr memoryIndex = 0; memoryIndex < memories.size(); ++memoryIndex)
{
if(memories[memoryIndex] == this)
{
memories.erase(memories.begin() + memoryIndex);
break;
}
}
}
// Free the allocated quota.
if(resourceQuota) { resourceQuota->memoryPages.free(numPages); }
}
bool Runtime::isAddressOwnedByMemory(U8* address, Memory*& outMemory, Uptr& outMemoryAddress)
{
// Iterate over all memories and check if the address is within the reserved address space for
// each.
Platform::RWMutex::ShareableLock memoriesLock(memoriesMutex);
for(auto memory : memories)
{
U8* startAddress = memory->getBaseAddress();
U8* endAddress = memory->getBaseAddress() + memory->getNumReservedBytes() + Memory::getNumGuardBytes();
if(address >= startAddress && address < endAddress)
{
outMemory = memory;
outMemoryAddress = address - startAddress;
return true;
}
}
return false;
}
Uptr Runtime::getMemoryNumPages(const Memory* memory)
{
return memory->numPages.load(std::memory_order_seq_cst);
}
IR::MemoryType Runtime::getMemoryType(const Memory* memory)
{
return IR::MemoryType(memory->isShared,
memory->indexType,
IR::SizeConstraints{getMemoryNumPages(memory), memory->maxPages});
}
GrowResult Runtime::growMemory(Memory* memory, Uptr numPagesToGrow, Uptr* outOldNumPages)
{
Uptr oldNumPages;
if(numPagesToGrow == 0) { oldNumPages = memory->numPages.load(std::memory_order_seq_cst); }
else
{
// Check the memory page quota.
if(memory->resourceQuota && !memory->resourceQuota->memoryPages.allocate(numPagesToGrow))
{ return GrowResult::outOfQuota; }
Platform::RWMutex::ExclusiveLock resizingLock(memory->resizingMutex);
oldNumPages = memory->numPages.load(std::memory_order_acquire);
// If the number of pages to grow would cause the memory's size to exceed its maximum,
// return GrowResult::outOfMaxSize.
const U64 maxMemoryPages = memory->indexType == IR::IndexType::i32
? IR::maxMemory32Pages
: std::min(maxMemory64WASMPages, IR::maxMemory64Pages);
if(numPagesToGrow > memory->maxPages || oldNumPages > memory->maxPages - numPagesToGrow
|| numPagesToGrow > maxMemoryPages || oldNumPages > maxMemoryPages - numPagesToGrow)
{
if(memory->resourceQuota) { memory->resourceQuota->memoryPages.free(numPagesToGrow); }
return GrowResult::outOfMaxSize;
}
memory->data.grow(numPagesToGrow);
const Uptr newNumPages = oldNumPages + numPagesToGrow;
memory->numPages.store(newNumPages, std::memory_order_release);
if(memory->id != UINTPTR_MAX)
{
memory->compartment->runtimeData->memories[memory->id].base = memory->getBaseAddress();
memory->compartment->runtimeData->memories[memory->id].endAddress = memory->getNumReservedBytes();
memory->compartment->runtimeData->memories[memory->id].numPages.store(
newNumPages, std::memory_order_release);
}
}
if(outOldNumPages) { *outOldNumPages = oldNumPages; }
return GrowResult::success;
}
void Runtime::unmapMemoryPages(Memory* memory, Uptr pageIndex, Uptr numPages)
{
WAVM_ASSERT(pageIndex + numPages > pageIndex);
WAVM_ASSERT((pageIndex + numPages) * IR::numBytesPerPage <= memory->getNumReservedBytes());
// Decommit the pages.
Platform::decommitVirtualPages(memory->getBaseAddress() + pageIndex * IR::numBytesPerPage,
numPages << getPlatformPagesPerWebAssemblyPageLog2());
Platform::deregisterVirtualAllocation(numPages << getPlatformPagesPerWebAssemblyPageLog2());
}
U8* Runtime::getMemoryBaseAddress(Memory* memory) { return memory->getBaseAddress(); }
static U8* getValidatedMemoryOffsetRangeImpl(Memory* memory,
U8* memoryBase,
Uptr memoryNumBytes,
Uptr address,
Uptr numBytes)
{
if(address + numBytes > memoryNumBytes || address + numBytes < address)
{
throwException(
ExceptionTypes::outOfBoundsMemoryAccess,
{asObject(memory), U64(address > memoryNumBytes ? address : memoryNumBytes)});
}
WAVM_ASSERT(memoryBase);
numBytes = branchlessMin(numBytes, memoryNumBytes);
return memoryBase + branchlessMin(address, memoryNumBytes - numBytes);
}
U8* Runtime::getReservedMemoryOffsetRange(Memory* memory, Uptr address, Uptr numBytes)
{
WAVM_ASSERT(memory);
// Validate that the range [offset..offset+numBytes) is contained by the memory's reserved
// pages.
return ::getValidatedMemoryOffsetRangeImpl(
memory, memory->getBaseAddress(), memory->getNumReservedBytes(), address, numBytes);
}
U8* Runtime::getValidatedMemoryOffsetRange(Memory* memory, Uptr address, Uptr numBytes)
{
WAVM_ASSERT(memory);
// Validate that the range [offset..offset+numBytes) is contained by the memory's committed
// pages.
return ::getValidatedMemoryOffsetRangeImpl(
memory,
memory->getBaseAddress(),
memory->numPages.load(std::memory_order_acquire) * IR::numBytesPerPage,
address,
numBytes);
}
void Runtime::initDataSegment(Instance* instance,
Uptr dataSegmentIndex,
const std::vector<U8>* dataVector,
Memory* memory,
Uptr destAddress,
Uptr sourceOffset,
Uptr numBytes)
{
U8* destPointer = getValidatedMemoryOffsetRange(memory, destAddress, numBytes);
if(sourceOffset + numBytes > dataVector->size() || sourceOffset + numBytes < sourceOffset)
{
throwException(
ExceptionTypes::outOfBoundsDataSegmentAccess,
{asObject(instance),
U64(dataSegmentIndex),
U64(sourceOffset > dataVector->size() ? sourceOffset : dataVector->size())});
}
else
{
Runtime::unwindSignalsAsExceptions([destPointer, sourceOffset, numBytes, dataVector] {
bytewiseMemCopy(destPointer, dataVector->data() + sourceOffset, numBytes);
});
}
}
WAVM_DEFINE_INTRINSIC_FUNCTION(wavmIntrinsicsMemory,
"memory.grow",
Iptr,
memory_grow,
Uptr deltaPages,
Uptr memoryId)
{
Memory* memory = getMemoryFromRuntimeData(contextRuntimeData, memoryId);
Uptr oldNumPages = 0;
if(growMemory(memory, deltaPages, &oldNumPages) != GrowResult::success) { return -1; }
WAVM_ASSERT(oldNumPages <= INTPTR_MAX);
return Iptr(oldNumPages);
}
WAVM_DEFINE_INTRINSIC_FUNCTION(wavmIntrinsicsMemory,
"memory.init",
void,
memory_init,
Uptr destAddress,
Uptr sourceOffset,
Uptr numBytes,
Uptr instanceId,
Uptr memoryId,
Uptr dataSegmentIndex)
{
Instance* instance = getInstanceFromRuntimeData(contextRuntimeData, instanceId);
Memory* memory = getMemoryFromRuntimeData(contextRuntimeData, memoryId);
Platform::RWMutex::ShareableLock dataSegmentsLock(instance->dataSegmentsMutex);
if(!instance->dataSegments[dataSegmentIndex])
{
if(sourceOffset != 0 || numBytes != 0)
{
throwException(ExceptionTypes::outOfBoundsDataSegmentAccess,
{instance, dataSegmentIndex, sourceOffset});
}
}
else
{
// Make a copy of the shared_ptr to the data and unlock the data segments mutex.
std::shared_ptr<std::vector<U8>> dataVector = instance->dataSegments[dataSegmentIndex];
dataSegmentsLock.unlock();
initDataSegment(instance,
dataSegmentIndex,
dataVector.get(),
memory,
destAddress,
sourceOffset,
numBytes);
}
}
WAVM_DEFINE_INTRINSIC_FUNCTION(wavmIntrinsicsMemory,
"data.drop",
void,
data_drop,
Uptr instanceId,
Uptr dataSegmentIndex)
{
Instance* instance = getInstanceFromRuntimeData(contextRuntimeData, instanceId);
Platform::RWMutex::ExclusiveLock dataSegmentsLock(instance->dataSegmentsMutex);
if(instance->dataSegments[dataSegmentIndex])
{ instance->dataSegments[dataSegmentIndex].reset(); }
}
WAVM_DEFINE_INTRINSIC_FUNCTION(wavmIntrinsics,
"memoryOutOfBoundsTrap",
void,
outOfBoundsMemoryTrap,
Uptr address,
Uptr numBytes,
Uptr memoryNumBytes,
Uptr memoryId)
{
Compartment* compartment = getCompartmentFromContextRuntimeData(contextRuntimeData);
Platform::RWMutex::ShareableLock compartmentLock(compartment->mutex);
Memory* memory = compartment->memories[memoryId];
compartmentLock.unlock();
const U64 outOfBoundsAddress = U64(address) > memoryNumBytes ? U64(address) : memoryNumBytes;
throwException(ExceptionTypes::outOfBoundsMemoryAccess, {memory, outOfBoundsAddress});
}
|