Skip to content

Commit c496d84

Browse files
committed
[lld][WebAssembly] Handle 4gb max memories
Summary: A previous change (53211a) had updated the argument parsing to handle large max memories, but 4294967296 would still wrap to zero after the options were parsed. This change updates the configuration to use a 64-bit integer to store the max memory to avoid that overflow. Reviewers: sbc100 Subscribers: dschuff, jgravelle-google, aheejin, sunfish, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D77437
1 parent 98b47f4 commit c496d84

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

lld/test/wasm/large-memory.test

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,23 @@ RUN: llc -filetype=obj %p/Inputs/start.ll -o %t.o
22

33
; Verify we can parse large integers such as when we ask for 2G of total
44
; memory.
5-
RUN: wasm-ld %t.o -o %t.wasm --max-memory=2147483648
5+
RUN: wasm-ld %t.o -o %t1.wasm --max-memory=2147483648
6+
RUN: obj2yaml %t1.wasm | FileCheck %s --check-prefixes=CHECK,CHECK-2G
7+
8+
; And also 4G of total memory
9+
RUN: wasm-ld %t.o -o %t2.wasm --max-memory=4294967296
10+
RUN: obj2yaml %t2.wasm | FileCheck %s --check-prefixes=CHECK,CHECK-4G
11+
12+
CHECK: - Type: MEMORY
13+
CHECK-NEXT: Memories:
14+
CHECK-NEXT: - Flags: [ HAS_MAX ]
15+
CHECK-NEXT: Initial: 0x00000002
16+
CHECK-2G-NEXT: Maximum: 0x00008000
17+
CHECK-4G-NEXT: Maximum: 0x00010000
18+
19+
; Test error for more than 4G of memory
20+
RUN: not wasm-ld %t.o -o %t3.wasm --initial-memory=4295032832 2>&1 | FileCheck %s --check-prefix INIT-ERROR
21+
RUN: not wasm-ld %t.o -o %t4.wasm --max-memory=4295032832 2>&1 | FileCheck %s --check-prefix MAX-ERROR
22+
23+
INIT-ERROR: initial memory too large, cannot be greater than 4294967296
24+
MAX-ERROR: maximum memory too large, cannot be greater than 4294967296

lld/wasm/Config.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ struct Configuration {
4646
bool stripDebug;
4747
bool stackFirst;
4848
bool trace;
49-
uint32_t globalBase;
50-
uint32_t initialMemory;
51-
uint32_t maxMemory;
52-
uint32_t zStackSize;
49+
uint64_t globalBase;
50+
uint64_t initialMemory;
51+
uint64_t maxMemory;
52+
uint64_t zStackSize;
5353
unsigned ltoPartitions;
5454
unsigned ltoo;
5555
unsigned optimize;

lld/wasm/Writer.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void Writer::writeSections() {
201201
// rather than overwriting global data, but also increases code size since all
202202
// static data loads and stores requires larger offsets.
203203
void Writer::layoutMemory() {
204-
uint32_t memoryPtr = 0;
204+
uint64_t memoryPtr = 0;
205205

206206
auto placeStack = [&]() {
207207
if (config->relocatable || config->isPic)
@@ -227,7 +227,7 @@ void Writer::layoutMemory() {
227227
if (WasmSym::globalBase)
228228
WasmSym::globalBase->setVirtualAddress(memoryPtr);
229229

230-
uint32_t dataStart = memoryPtr;
230+
uint64_t dataStart = memoryPtr;
231231

232232
// Arbitrarily set __dso_handle handle to point to the start of the data
233233
// segments.
@@ -286,8 +286,9 @@ void Writer::layoutMemory() {
286286
error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned");
287287
if (memoryPtr > config->initialMemory)
288288
error("initial memory too small, " + Twine(memoryPtr) + " bytes needed");
289-
else
290-
memoryPtr = config->initialMemory;
289+
if (config->initialMemory > (1ULL << 32))
290+
error("initial memory too large, cannot be greater than 4294967296");
291+
memoryPtr = config->initialMemory;
291292
}
292293
out.dylinkSec->memSize = memoryPtr;
293294
out.memorySec->numMemoryPages =
@@ -300,6 +301,8 @@ void Writer::layoutMemory() {
300301
error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned");
301302
if (memoryPtr > config->maxMemory)
302303
error("maximum memory too small, " + Twine(memoryPtr) + " bytes needed");
304+
if (config->maxMemory > (1ULL << 32))
305+
error("maximum memory too large, cannot be greater than 4294967296");
303306
out.memorySec->maxMemoryPages = config->maxMemory / WasmPageSize;
304307
log("mem: max pages = " + Twine(out.memorySec->maxMemoryPages));
305308
}

0 commit comments

Comments
 (0)