diff options
author | Wouter van Oortmerssen <aardappel@gmail.com> | 2020-09-18 15:50:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-18 15:50:25 -0700 |
commit | 3b4cb935f83c7fabacbf61146e56dabc0d65a441 (patch) | |
tree | 9aaac74aad59fe2ff2b00ac6e6a77bb808b01747 /src | |
parent | 1a928bc3ff4b511e81b3f93db8aea872e88abaaf (diff) | |
download | binaryen-3b4cb935f83c7fabacbf61146e56dabc0d65a441.tar.gz binaryen-3b4cb935f83c7fabacbf61146e56dabc0d65a441.tar.bz2 binaryen-3b4cb935f83c7fabacbf61146e56dabc0d65a441.zip |
Initial implementation of "Memory64" proposal (#3130)
Also includes a lot of new spec tests that eventually need to go into the spec repo
Diffstat (limited to 'src')
-rw-r--r-- | src/binaryen-c.cpp | 2 | ||||
-rw-r--r-- | src/ir/ExpressionAnalyzer.cpp | 4 | ||||
-rw-r--r-- | src/literal.h | 15 | ||||
-rw-r--r-- | src/passes/AlignmentLowering.cpp | 52 | ||||
-rw-r--r-- | src/passes/AvoidReinterprets.cpp | 9 | ||||
-rw-r--r-- | src/passes/InstrumentMemory.cpp | 17 | ||||
-rw-r--r-- | src/passes/MemoryPacking.cpp | 22 | ||||
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 18 | ||||
-rw-r--r-- | src/passes/Print.cpp | 5 | ||||
-rw-r--r-- | src/passes/SafeHeap.cpp | 102 | ||||
-rw-r--r-- | src/passes/StackIR.cpp | 2 | ||||
-rw-r--r-- | src/tools/tool-options.h | 1 | ||||
-rw-r--r-- | src/tools/wasm2js.cpp | 2 | ||||
-rw-r--r-- | src/wasm-binary.h | 15 | ||||
-rw-r--r-- | src/wasm-builder.h | 15 | ||||
-rw-r--r-- | src/wasm-features.h | 6 | ||||
-rw-r--r-- | src/wasm-interpreter.h | 60 | ||||
-rw-r--r-- | src/wasm-s-parser.h | 2 | ||||
-rw-r--r-- | src/wasm-stack.h | 8 | ||||
-rw-r--r-- | src/wasm.h | 41 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 74 | ||||
-rw-r--r-- | src/wasm/wasm-s-parser.cpp | 75 | ||||
-rw-r--r-- | src/wasm/wasm-stack.cpp | 2 | ||||
-rw-r--r-- | src/wasm/wasm-validator.cpp | 128 | ||||
-rw-r--r-- | src/wasm/wasm.cpp | 7 | ||||
-rw-r--r-- | src/wasm2js.h | 4 |
26 files changed, 430 insertions, 258 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index e833692d5..1c938faab 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -3262,7 +3262,7 @@ void BinaryenSetMemory(BinaryenModuleRef module, uint8_t shared) { auto* wasm = (Module*)module; wasm->memory.initial = initial; - wasm->memory.max = maximum; + wasm->memory.max = int32_t(maximum); // Make sure -1 extends. wasm->memory.exists = true; wasm->memory.shared = shared; if (exportName) { diff --git a/src/ir/ExpressionAnalyzer.cpp b/src/ir/ExpressionAnalyzer.cpp index 37ba82e81..a8d9ce44a 100644 --- a/src/ir/ExpressionAnalyzer.cpp +++ b/src/ir/ExpressionAnalyzer.cpp @@ -475,13 +475,11 @@ size_t ExpressionAnalyzer::hash(Expression* curr) { void visitLiteral(Literal curr) { rehash(digest, curr); } void visitType(Type curr) { rehash(digest, curr.getID()); } void visitIndex(Index curr) { - static_assert(sizeof(Index) == sizeof(int32_t), + static_assert(sizeof(Index) == sizeof(uint32_t), "wasm64 will need changes here"); rehash(digest, curr); } void visitAddress(Address curr) { - static_assert(sizeof(Address) == sizeof(int32_t), - "wasm64 will need changes here"); rehash(digest, curr.addr); } }; diff --git a/src/literal.h b/src/literal.h index 981f5b6d4..c617f56f0 100644 --- a/src/literal.h +++ b/src/literal.h @@ -117,6 +117,21 @@ public: } } + static Literal makeFromUInt64(uint64_t x, Type type) { + switch (type.getBasic()) { + case Type::i32: + return Literal(int32_t(x)); + case Type::i64: + return Literal(int64_t(x)); + case Type::f32: + return Literal(float(x)); + case Type::f64: + return Literal(double(x)); + default: + WASM_UNREACHABLE("unexpected type"); + } + } + static Literals makeZero(Type type); static Literal makeSingleZero(Type type); diff --git a/src/passes/AlignmentLowering.cpp b/src/passes/AlignmentLowering.cpp index b12e49c4f..8cab58072 100644 --- a/src/passes/AlignmentLowering.cpp +++ b/src/passes/AlignmentLowering.cpp @@ -34,9 +34,10 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { if (curr->align == 0 || curr->align == curr->bytes) { return curr; } + auto indexType = getModule()->memory.indexType; Builder builder(*getModule()); assert(curr->type == Type::i32); - auto temp = builder.addVar(getFunction(), Type::i32); + auto temp = builder.addVar(getFunction(), indexType); Expression* ret; if (curr->bytes == 2) { ret = builder.makeBinary( @@ -45,7 +46,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { false, curr->offset, 1, - builder.makeLocalGet(temp, Type::i32), + builder.makeLocalGet(temp, indexType), Type::i32), builder.makeBinary( ShlInt32, @@ -53,7 +54,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { false, curr->offset + 1, 1, - builder.makeLocalGet(temp, Type::i32), + builder.makeLocalGet(temp, indexType), Type::i32), builder.makeConst(int32_t(8)))); if (curr->signed_) { @@ -69,7 +70,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { false, curr->offset, 1, - builder.makeLocalGet(temp, Type::i32), + builder.makeLocalGet(temp, indexType), Type::i32), builder.makeBinary( ShlInt32, @@ -77,7 +78,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { false, curr->offset + 1, 1, - builder.makeLocalGet(temp, Type::i32), + builder.makeLocalGet(temp, indexType), Type::i32), builder.makeConst(int32_t(8)))), builder.makeBinary( @@ -88,7 +89,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { false, curr->offset + 2, 1, - builder.makeLocalGet(temp, Type::i32), + builder.makeLocalGet(temp, indexType), Type::i32), builder.makeConst(int32_t(16))), builder.makeBinary( @@ -97,7 +98,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { false, curr->offset + 3, 1, - builder.makeLocalGet(temp, Type::i32), + builder.makeLocalGet(temp, indexType), Type::i32), builder.makeConst(int32_t(24))))); } else if (curr->align == 2) { @@ -107,7 +108,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { false, curr->offset, 2, - builder.makeLocalGet(temp, Type::i32), + builder.makeLocalGet(temp, indexType), Type::i32), builder.makeBinary( ShlInt32, @@ -115,7 +116,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { false, curr->offset + 2, 2, - builder.makeLocalGet(temp, Type::i32), + builder.makeLocalGet(temp, indexType), Type::i32), builder.makeConst(int32_t(16)))); } else { @@ -134,7 +135,8 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { } Builder builder(*getModule()); assert(curr->value->type == Type::i32); - auto tempPtr = builder.addVar(getFunction(), Type::i32); + auto indexType = getModule()->memory.indexType; + auto tempPtr = builder.addVar(getFunction(), indexType); auto tempValue = builder.addVar(getFunction(), Type::i32); auto* block = builder.makeBlock({builder.makeLocalSet(tempPtr, curr->ptr), @@ -144,14 +146,14 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { builder.makeStore(1, curr->offset, 1, - builder.makeLocalGet(tempPtr, Type::i32), + builder.makeLocalGet(tempPtr, indexType), builder.makeLocalGet(tempValue, Type::i32), Type::i32)); block->list.push_back(builder.makeStore( 1, curr->offset + 1, 1, - builder.makeLocalGet(tempPtr, Type::i32), + builder.makeLocalGet(tempPtr, indexType), builder.makeBinary(ShrUInt32, builder.makeLocalGet(tempValue, Type::i32), builder.makeConst(int32_t(8))), @@ -162,14 +164,14 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { builder.makeStore(1, curr->offset, 1, - builder.makeLocalGet(tempPtr, Type::i32), + builder.makeLocalGet(tempPtr, indexType), builder.makeLocalGet(tempValue, Type::i32), Type::i32)); block->list.push_back(builder.makeStore( 1, curr->offset + 1, 1, - builder.makeLocalGet(tempPtr, Type::i32), + builder.makeLocalGet(tempPtr, indexType), builder.makeBinary(ShrUInt32, builder.makeLocalGet(tempValue, Type::i32), builder.makeConst(int32_t(8))), @@ -178,7 +180,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { 1, curr->offset + 2, 1, - builder.makeLocalGet(tempPtr, Type::i32), + builder.makeLocalGet(tempPtr, indexType), builder.makeBinary(ShrUInt32, builder.makeLocalGet(tempValue, Type::i32), builder.makeConst(int32_t(16))), @@ -187,7 +189,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { 1, curr->offset + 3, 1, - builder.makeLocalGet(tempPtr, Type::i32), + builder.makeLocalGet(tempPtr, indexType), builder.makeBinary(ShrUInt32, builder.makeLocalGet(tempValue, Type::i32), builder.makeConst(int32_t(24))), @@ -197,14 +199,14 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { builder.makeStore(2, curr->offset, 2, - builder.makeLocalGet(tempPtr, Type::i32), + builder.makeLocalGet(tempPtr, indexType), builder.makeLocalGet(tempValue, Type::i32), Type::i32)); block->list.push_back(builder.makeStore( 2, curr->offset + 2, 2, - builder.makeLocalGet(tempPtr, Type::i32), + builder.makeLocalGet(tempPtr, indexType), builder.makeBinary(ShrUInt32, builder.makeLocalGet(tempValue, Type::i32), builder.makeConst(int32_t(16))), @@ -254,14 +256,15 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { break; } // Load two 32-bit pieces, and combine them. - auto temp = builder.addVar(getFunction(), Type::i32); + auto indexType = getModule()->memory.indexType; + auto temp = builder.addVar(getFunction(), indexType); auto* set = builder.makeLocalSet(temp, curr->ptr); Expression* low = lowerLoadI32(builder.makeLoad(4, false, curr->offset, curr->align, - builder.makeLocalGet(temp, Type::i32), + builder.makeLocalGet(temp, indexType), Type::i32)); low = builder.makeUnary(ExtendUInt32, low); // Note that the alignment is assumed to be the same here, even though @@ -273,7 +276,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { false, curr->offset + 4, curr->align, - builder.makeLocalGet(temp, Type::i32), + builder.makeLocalGet(temp, indexType), Type::i32)); high = builder.makeUnary(ExtendUInt32, high); high = @@ -332,7 +335,8 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { value = builder.makeUnary(ReinterpretFloat64, value); } // Store as two 32-bit pieces. - auto tempPtr = builder.addVar(getFunction(), Type::i32); + auto indexType = getModule()->memory.indexType; + auto tempPtr = builder.addVar(getFunction(), indexType); auto* setPtr = builder.makeLocalSet(tempPtr, curr->ptr); auto tempValue = builder.addVar(getFunction(), Type::i64); auto* setValue = builder.makeLocalSet(tempValue, value); @@ -342,7 +346,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { builder.makeStore(4, curr->offset, curr->align, - builder.makeLocalGet(tempPtr, Type::i32), + builder.makeLocalGet(tempPtr, indexType), low, Type::i32)); Expression* high = @@ -358,7 +362,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> { builder.makeStore(4, curr->offset + 4, curr->align, - builder.makeLocalGet(tempPtr, Type::i32), + builder.makeLocalGet(tempPtr, indexType), high, Type::i32)); replacement = builder.makeBlock({setPtr, setValue, low, high}); diff --git a/src/passes/AvoidReinterprets.cpp b/src/passes/AvoidReinterprets.cpp index 8668b9703..cc5871a2a 100644 --- a/src/passes/AvoidReinterprets.cpp +++ b/src/passes/AvoidReinterprets.cpp @@ -117,12 +117,13 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> { void optimize(Function* func) { std::set<Load*> unoptimizables; + auto indexType = getModule()->memory.indexType; for (auto& pair : infos) { auto* load = pair.first; auto& info = pair.second; if (info.reinterpreted && canReplaceWithReinterpret(load)) { // We should use another load here, to avoid reinterprets. - info.ptrLocal = Builder::addVar(func, Type::i32); + info.ptrLocal = Builder::addVar(func, indexType); info.reinterpretedLocal = Builder::addVar(func, load->type.reinterpret()); } else { @@ -176,7 +177,8 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> { auto& info = iter->second; Builder builder(*module); auto* ptr = curr->ptr; - curr->ptr = builder.makeLocalGet(info.ptrLocal, Type::i32); + auto indexType = getModule()->memory.indexType; + curr->ptr = builder.makeLocalGet(info.ptrLocal, indexType); // Note that the other load can have its sign set to false - if the // original were an integer, the other is a float anyhow; and if // original were a float, we don't know what sign to use. @@ -185,7 +187,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> { builder.makeLocalSet( info.reinterpretedLocal, makeReinterpretedLoad( - curr, builder.makeLocalGet(info.ptrLocal, Type::i32))), + curr, builder.makeLocalGet(info.ptrLocal, indexType))), curr})); } } @@ -201,6 +203,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> { } } finalOptimizer(infos, localGraph, getModule(), getPassOptions()); + finalOptimizer.setModule(getModule()); finalOptimizer.walk(func->body); } }; diff --git a/src/passes/InstrumentMemory.cpp b/src/passes/InstrumentMemory.cpp index fca3ff510..9e4788456 100644 --- a/src/passes/InstrumentMemory.cpp +++ b/src/passes/InstrumentMemory.cpp @@ -78,12 +78,14 @@ struct InstrumentMemory : public WalkerPass<PostWalker<InstrumentMemory>> { void visitLoad(Load* curr) { id++; Builder builder(*getModule()); + auto indexType = getModule()->memory.indexType; + auto offset = builder.makeConstPtr(curr->offset.addr); curr->ptr = builder.makeCall(load_ptr, {builder.makeConst(int32_t(id)), builder.makeConst(int32_t(curr->bytes)), - builder.makeConst(int32_t(curr->offset.addr)), + offset, curr->ptr}, - Type::i32); + indexType); Name target; switch (curr->type.getBasic()) { case Type::i32: @@ -108,12 +110,14 @@ struct InstrumentMemory : public WalkerPass<PostWalker<InstrumentMemory>> { void visitStore(Store* curr) { id++; Builder builder(*getModule()); + auto indexType = getModule()->memory.indexType; + auto offset = builder.makeConstPtr(curr->offset.addr); curr->ptr = builder.makeCall(store_ptr, {builder.makeConst(int32_t(id)), builder.makeConst(int32_t(curr->bytes)), - builder.makeConst(int32_t(curr->offset.addr)), + offset, curr->ptr}, - Type::i32); + indexType); Name target; switch (curr->value->type.getBasic()) { case Type::i32: @@ -136,14 +140,15 @@ struct InstrumentMemory : public WalkerPass<PostWalker<InstrumentMemory>> { } void visitModule(Module* curr) { + auto indexType = curr->memory.indexType; addImport( - curr, load_ptr, {Type::i32, Type::i32, Type::i32, Type::i32}, Type::i32); + curr, load_ptr, {Type::i32, Type::i32, indexType, indexType}, indexType); addImport(curr, load_val_i32, {Type::i32, Type::i32}, Type::i32); addImport(curr, load_val_i64, {Type::i32, Type::i64}, Type::i64); addImport(curr, load_val_f32, {Type::i32, Type::f32}, Type::f32); addImport(curr, load_val_f64, {Type::i32, Type::f64}, Type::f64); addImport( - curr, store_ptr, {Type::i32, Type::i32, Type::i32, Type::i32}, Type::i32); + curr, store_ptr, {Type::i32, Type::i32, indexType, indexType}, indexType); addImport(curr, store_val_i32, {Type::i32, Type::i32}, Type::i32); addImport(curr, store_val_i64, {Type::i32, Type::i64}, Type::i64); addImport(curr, store_val_f32, {Type::i32, Type::f32}, Type::f32); diff --git a/src/passes/MemoryPacking.cpp b/src/passes/MemoryPacking.cpp index eac7fb7f0..1018ce1c7 100644 --- a/src/passes/MemoryPacking.cpp +++ b/src/passes/MemoryPacking.cpp @@ -76,9 +76,14 @@ const size_t DATA_DROP_SIZE = 3; namespace { -Expression* makeShiftedMemorySize(Builder& builder) { +Expression* +makeGtShiftedMemorySize(Builder& builder, Module& module, MemoryInit* curr) { return builder.makeBinary( - ShlInt32, builder.makeMemorySize(), builder.makeConst(int32_t(16))); + module.memory.is64() ? GtUInt64 : GtUInt32, + curr->dest, + builder.makeBinary(module.memory.is64() ? ShlInt64 : ShlInt32, + builder.makeMemorySize(), + builder.makeConstPtr(16))); } } // anonymous namespace @@ -317,10 +322,9 @@ void MemoryPacking::optimizeBulkMemoryOps(PassRunner* runner, Module* module) { assert(!mustNop || !mustTrap); if (mustNop) { // Offset and size are 0, so just trap if dest > memory.size - replaceCurrent(builder.makeIf( - builder.makeBinary( - GtUInt32, curr->dest, makeShiftedMemorySize(builder)), - builder.makeUnreachable())); + replaceCurrent( + builder.makeIf(makeGtShiftedMemorySize(builder, *getModule(), curr), + builder.makeUnreachable())); } else if (mustTrap) { // Drop dest, offset, and size then trap replaceCurrent(builder.blockify(builder.makeDrop(curr->dest), @@ -333,8 +337,7 @@ void MemoryPacking::optimizeBulkMemoryOps(PassRunner* runner, Module* module) { replaceCurrent(builder.makeIf( builder.makeBinary( OrInt32, - builder.makeBinary( - GtUInt32, curr->dest, makeShiftedMemorySize(builder)), + makeGtShiftedMemorySize(builder, *getModule(), curr), builder.makeBinary(OrInt32, curr->offset, curr->size)), builder.makeUnreachable())); } @@ -527,8 +530,7 @@ void MemoryPacking::createReplacements(Module* module, Expression* result = builder.makeIf( builder.makeBinary( OrInt32, - builder.makeBinary( - GtUInt32, init->dest, makeShiftedMemorySize(builder)), + makeGtShiftedMemorySize(builder, *module, init), builder.makeGlobalGet(getDropStateGlobal(), Type::i32)), builder.makeUnreachable()); replacements[init] = [result](Function*) { return result; }; diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index e3d2b3a57..9cc16004d 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1107,14 +1107,20 @@ private: // as for readability. auto* last = ptr->dynCast<Const>(); if (last) { - // don't do this if it would wrap the pointer - uint64_t value64 = last->value.geti32(); + uint64_t value64 = last->value.getInteger(); uint64_t offset64 = offset; - if (value64 <= uint64_t(std::numeric_limits<int32_t>::max()) && - offset64 <= uint64_t(std::numeric_limits<int32_t>::max()) && - value64 + offset64 <= uint64_t(std::numeric_limits<int32_t>::max())) { - last->value = Literal(int32_t(value64 + offset64)); + if (getModule()->memory.is64()) { + last->value = Literal(int64_t(value64 + offset64)); offset = 0; + } else { + // don't do this if it would wrap the pointer + if (value64 <= uint64_t(std::numeric_limits<int32_t>::max()) && + offset64 <= uint64_t(std::numeric_limits<int32_t>::max()) && + value64 + offset64 <= + uint64_t(std::numeric_limits<int32_t>::max())) { + last->value = Literal(int32_t(value64 + offset64)); + offset = 0; + } } } } diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 916615e07..6955a7a5e 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -2298,6 +2298,9 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { o << '('; printMedium(o, "shared "); } + if (curr->is64()) { + o << "i64 "; + } o << curr->initial; if (curr->hasMax()) { o << ' ' << curr->max; @@ -2446,7 +2449,7 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> { << section.data.size(); bool isPrintable = true; for (auto c : section.data) { - if (!isprint(c)) { + if (!isprint(static_cast<unsigned char>(c))) { isPrintable = false; break; } diff --git a/src/passes/SafeHeap.cpp b/src/passes/SafeHeap.cpp index 108739d66..0335c174e 100644 --- a/src/passes/SafeHeap.cpp +++ b/src/passes/SafeHeap.cpp @@ -85,10 +85,7 @@ struct AccessInstrumenter : public WalkerPass<PostWalker<AccessInstrumenter>> { Builder builder(*getModule()); replaceCurrent( builder.makeCall(getLoadName(curr), - { - curr->ptr, - builder.makeConst(Literal(int32_t(curr->offset))), - }, + {curr->ptr, builder.makeConstPtr(curr->offset.addr)}, curr->type)); } @@ -97,14 +94,10 @@ struct AccessInstrumenter : public WalkerPass<PostWalker<AccessInstrumenter>> { return; } Builder builder(*getModule()); - replaceCurrent( - builder.makeCall(getStoreName(curr), - { - curr->ptr, - builder.makeConst(Literal(int32_t(curr->offset))), - curr->value, - }, - Type::none)); + replaceCurrent(builder.makeCall( + getStoreName(curr), + {curr->ptr, builder.makeConstPtr(curr->offset.addr), curr->value}, + Type::none)); } }; @@ -125,6 +118,7 @@ struct SafeHeap : public Pass { void addImports(Module* module) { ImportInfo info(*module); + auto indexType = module->memory.indexType; // Older emscripten imports env.DYNAMICTOP_PTR. // Newer emscripten imports or exports emscripten_get_sbrk_ptr(). if (auto* existing = info.getImportedGlobal(ENV, DYNAMICTOP_PTR_IMPORT)) { @@ -140,7 +134,7 @@ struct SafeHeap : public Pass { import->name = getSbrkPtr = GET_SBRK_PTR; import->module = ENV; import->base = GET_SBRK_PTR; - import->sig = Signature(Type::none, Type::i32); + import->sig = Signature(Type::none, indexType); module->addFunction(import); } if (auto* existing = info.getImportedFunction(ENV, SEGFAULT_IMPORT)) { @@ -251,25 +245,27 @@ struct SafeHeap : public Pass { auto* func = new Function; func->name = name; // pointer, offset - func->sig = Signature({Type::i32, Type::i32}, style.type); - func->vars.push_back(Type::i32); // pointer + offset + auto indexType = module->memory.indexType; + func->sig = Signature({indexType, indexType}, style.type); + func->vars.push_back(indexType); // pointer + offset Builder builder(*module); auto* block = builder.makeBlock(); block->list.push_back(builder.makeLocalSet( 2, - builder.makeBinary(AddInt32, - builder.makeLocalGet(0, Type::i32), - builder.makeLocalGet(1, Type::i32)))); + builder.makeBinary(module->memory.is64() ? AddInt64 : AddInt32, + builder.makeLocalGet(0, indexType), + builder.makeLocalGet(1, indexType)))); // check for reading past valid memory: if pointer + offset + bytes - block->list.push_back(makeBoundsCheck(style.type, builder, 2, style.bytes)); + block->list.push_back( + makeBoundsCheck(style.type, builder, 2, style.bytes, module)); // check proper alignment if (style.align > 1) { - block->list.push_back(makeAlignCheck(style.align, builder, 2)); + block->list.push_back(makeAlignCheck(style.align, builder, 2, module)); } // do the load auto* load = module->allocator.alloc<Load>(); *load = style; // basically the same as the template we are given! - load->ptr = builder.makeLocalGet(2, Type::i32); + load->ptr = builder.makeLocalGet(2, indexType); Expression* last = load; if (load->isAtomic && load->signed_) { // atomic loads cannot be signed, manually sign it @@ -291,26 +287,27 @@ struct SafeHeap : public Pass { auto* func = new Function; func->name = name; // pointer, offset, value - func->sig = Signature({Type::i32, Type::i32, style.valueType}, Type::none); - func->vars.push_back(Type::i32); // pointer + offset + auto indexType = module->memory.indexType; + func->sig = Signature({indexType, indexType, style.valueType}, Type::none); + func->vars.push_back(indexType); // pointer + offset Builder builder(*module); auto* block = builder.makeBlock(); block->list.push_back(builder.makeLocalSet( 3, - builder.makeBinary(AddInt32, - builder.makeLocalGet(0, Type::i32), - builder.makeLocalGet(1, Type::i32)))); + builder.makeBinary(module->memory.is64() ? AddInt64 : AddInt32, + builder.makeLocalGet(0, indexType), + builder.makeLocalGet(1, indexType)))); // check for reading past valid memory: if pointer + offset + bytes block->list.push_back( - makeBoundsCheck(style.valueType, builder, 3, style.bytes)); + makeBoundsCheck(style.valueType, builder, 3, style.bytes, module)); // check proper alignment if (style.align > 1) { - block->list.push_back(makeAlignCheck(style.align, builder, 3)); + block->list.push_back(makeAlignCheck(style.align, builder, 3, module)); } // do the store auto* store = module->allocator.alloc<Store>(); *store = style; // basically the same as the template we are given! - store->ptr = builder.makeLocalGet(3, Type::i32); + store->ptr = builder.makeLocalGet(3, indexType); store->value = builder.makeLocalGet(2, style.valueType); block->list.push_back(store); block->finalize(Type::none); @@ -318,42 +315,53 @@ struct SafeHeap : public Pass { module->addFunction(func); } - Expression* makeAlignCheck(Address align, Builder& builder, Index local) { + Expression* + makeAlignCheck(Address align, Builder& builder, Index local, Module* module) { + auto indexType = module->memory.indexType; + Expression* ptrBits = builder.makeLocalGet(local, indexType); + if (module->memory.is64()) { + ptrBits = builder.makeUnary(WrapInt64, ptrBits); + } return builder.makeIf( - builder.makeBinary(AndInt32, - builder.makeLocalGet(local, Type::i32), - builder.makeConst(Literal(int32_t(align - 1)))), + builder.makeBinary( + AndInt32, ptrBits, builder.makeConst(int32_t(align - 1))), builder.makeCall(alignfault, {}, Type::none)); } - Expression* - makeBoundsCheck(Type type, Builder& builder, Index local, Index bytes) { - auto upperOp = options.lowMemoryUnused ? LtUInt32 : EqInt32; + Expression* makeBoundsCheck( + Type type, Builder& builder, Index local, Index bytes, Module* module) { + auto indexType = module->memory.indexType; + auto upperOp = module->memory.is64() + ? options.lowMemoryUnused ? LtUInt64 : EqInt64 + : options.lowMemoryUnused ? LtUInt32 : EqInt32; auto upperBound = options.lowMemoryUnused ? PassOptions::LowMemoryBound : 0; Expression* brkLocation; if (sbrk.is()) { - brkLocation = builder.makeCall( - sbrk, {builder.makeConst(Literal(int32_t(0)))}, Type::i32); + brkLocation = + builder.makeCall(sbrk, {builder.makeConstPtr(0)}, indexType); } else { Expression* sbrkPtr; if (dynamicTopPtr.is()) { - sbrkPtr = builder.makeGlobalGet(dynamicTopPtr, Type::i32); + sbrkPtr = builder.makeGlobalGet(dynamicTopPtr, indexType); } else { - sbrkPtr = builder.makeCall(getSbrkPtr, {}, Type::i32); + sbrkPtr = builder.makeCall(getSbrkPtr, {}, indexType); } - brkLocation = builder.makeLoad(4, false, 0, 4, sbrkPtr, Type::i32); + auto size = module->memory.is64() ? 8 : 4; + brkLocation = builder.makeLoad(size, false, 0, size, sbrkPtr, indexType); } + auto gtuOp = module->memory.is64() ? GtUInt64 : GtUInt32; + auto addOp = module->memory.is64() ? AddInt64 : AddInt32; return builder.makeIf( builder.makeBinary( OrInt32, builder.makeBinary(upperOp, - builder.makeLocalGet(local, Type::i32), - builder.makeConst(Literal(int32_t(upperBound)))), + builder.makeLocalGet(local, indexType), + builder.makeConstPtr(upperBound)), builder.makeBinary( - GtUInt32, - builder.makeBinary(AddInt32, - builder.makeLocalGet(local, Type::i32), - builder.makeConst(Literal(int32_t(bytes)))), + gtuOp, + builder.makeBinary(addOp, + builder.makeLocalGet(local, indexType), + builder.makeConstPtr(bytes)), brkLocation)), builder.makeCall(segfault, {}, Type::none)); } diff --git a/src/passes/StackIR.cpp b/src/passes/StackIR.cpp index a628468fb..ee2062b3d 100644 --- a/src/passes/StackIR.cpp +++ b/src/passes/StackIR.cpp @@ -36,7 +36,7 @@ struct GenerateStackIR : public WalkerPass<PostWalker<GenerateStackIR>> { bool modifiesBinaryenIR() override { return false; } void doWalkFunction(Function* func) { - StackIRGenerator stackIRGen(getModule()->allocator, func); + StackIRGenerator stackIRGen(*getModule(), func); stackIRGen.write(); func->stackIR = make_unique<StackIR>(); func->stackIR->swap(stackIRGen.getStackIR()); diff --git a/src/tools/tool-options.h b/src/tools/tool-options.h index 2e7158b34..83d5b38d7 100644 --- a/src/tools/tool-options.h +++ b/src/tools/tool-options.h @@ -88,6 +88,7 @@ struct ToolOptions : public Options { .addFeature(FeatureSet::ReferenceTypes, "reference types") .addFeature(FeatureSet::Multivalue, "multivalue functions") .addFeature(FeatureSet::GC, "garbage collection") + .addFeature(FeatureSet::Memory64, "memory64") .add("--no-validation", "-n", "Disables validation, assumes inputs are correct", diff --git a/src/tools/wasm2js.cpp b/src/tools/wasm2js.cpp index 68334708f..95d93cdbd 100644 --- a/src/tools/wasm2js.cpp +++ b/src/tools/wasm2js.cpp @@ -810,7 +810,7 @@ void AssertionEmitter::emit() { } )"; - Builder wasmBuilder(sexpBuilder.getAllocator()); + Builder wasmBuilder(sexpBuilder.getAllocator(), sexpBuilder.getModule()); Name asmModule = std::string("ret") + ASM_FUNC.str; for (size_t i = 0; i < root.size(); ++i) { Element& e = *root[i]; diff --git a/src/wasm-binary.h b/src/wasm-binary.h index da2378993..7666fa842 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -331,10 +331,7 @@ enum Section { Event = 13 }; -enum SegmentFlag { - IsPassive = 0x01, - HasMemIndex = 0x02, -}; +enum SegmentFlag { IsPassive = 0x01, HasMemIndex = 0x02 }; enum EncodedType { // value_type @@ -384,6 +381,7 @@ extern const char* TailCallFeature; extern const char* ReferenceTypesFeature; extern const char* MultivalueFeature; extern const char* GCFeature; +extern const char* Memory64Feature; enum Subsection { NameModule = 0, @@ -938,7 +936,7 @@ enum MemoryAccess { NaturalAlignment = 0 }; -enum MemoryFlags { HasMaximum = 1 << 0, IsShared = 1 << 1 }; +enum MemoryFlags { HasMaximum = 1 << 0, IsShared = 1 << 1, Is64 = 1 << 2 }; enum FeaturePrefix { FeatureUsed = '+', @@ -1097,10 +1095,8 @@ public: void write(); void writeHeader(); int32_t writeU32LEBPlaceholder(); - void writeResizableLimits(Address initial, - Address maximum, - bool hasMaximum, - bool shared); + void writeResizableLimits( + Address initial, Address maximum, bool hasMaximum, bool shared, bool is64); template<typename T> int32_t startSection(T code); void finishSection(int32_t start); int32_t startSubsection(BinaryConsts::UserSections::Subsection code); @@ -1264,6 +1260,7 @@ public: void getResizableLimits(Address& initial, Address& max, bool& shared, + Type& indexType, Address defaultIfNoMax); void readImports(); diff --git a/src/wasm-builder.h b/src/wasm-builder.h index 762d89dbe..b35fde376 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -35,10 +35,12 @@ struct NameType { class Builder { MixedArena& allocator; + Module& wasm; public: - Builder(MixedArena& allocator) : allocator(allocator) {} - Builder(Module& wasm) : allocator(wasm.allocator) {} + Builder(MixedArena& allocator, Module& wasm) + : allocator(allocator), wasm(wasm) {} + Builder(Module& wasm) : allocator(wasm.allocator), wasm(wasm) {} // make* functions, other globals @@ -486,6 +488,9 @@ public: ret->finalize(); return ret; } + Const* makeConstPtr(uint64_t val) { + return makeConst(Literal::makeFromUInt64(val, wasm.memory.indexType)); + } Binary* makeBinary(BinaryOp op, Expression* left, Expression* right) { auto* ret = allocator.alloc<Binary>(); ret->op = op; @@ -521,11 +526,17 @@ public: } MemorySize* makeMemorySize() { auto* ret = allocator.alloc<MemorySize>(); + if (wasm.memory.is64()) { + ret->make64(); + } ret->finalize(); return ret; } MemoryGrow* makeMemoryGrow(Expression* delta) { auto* ret = allocator.alloc<MemoryGrow>(); + if (wasm.memory.is64()) { + ret->make64(); + } ret->delta = delta; ret->finalize(); return ret; diff --git a/src/wasm-features.h b/src/wasm-features.h index 56436bd6a..6e74e0b29 100644 --- a/src/wasm-features.h +++ b/src/wasm-features.h @@ -37,7 +37,8 @@ struct FeatureSet { ReferenceTypes = 1 << 8, Multivalue = 1 << 9, GC = 1 << 10, - All = (1 << 11) - 1 + Memory64 = 1 << 11, + All = (1 << 12) - 1 }; static std::string toString(Feature f) { @@ -64,6 +65,8 @@ struct FeatureSet { return "multivalue"; case GC: return "gc"; + case Memory64: + return "memory64"; default: WASM_UNREACHABLE("unexpected feature"); } @@ -105,6 +108,7 @@ struct FeatureSet { void setReferenceTypes(bool v = true) { set(ReferenceTypes, v); } void setMultivalue(bool v = true) { set(Multivalue, v); } void setGC(bool v = true) { set(GC, v); } + void setMemory64(bool v = true) { set(Memory64, v); } void setAll(bool v = true) { features = v ? All : MVP; } void enable(const FeatureSet& other) { features |= other.features; } diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index 8fc595034..51cc24d01 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -2295,31 +2295,34 @@ private: } Flow visitMemorySize(MemorySize* curr) { NOTE_ENTER("MemorySize"); - return Literal(int32_t(instance.memorySize)); + return Literal::makeFromUInt64(instance.memorySize, + instance.wasm.memory.indexType); } Flow visitMemoryGrow(MemoryGrow* curr) { NOTE_ENTER("MemoryGrow"); - auto fail = Literal(int32_t(-1)); + auto indexType = instance.wasm.memory.indexType; + auto fail = Literal::makeFromUInt64(-1, indexType); Flow flow = this->visit(curr->delta); if (flow.breaking()) { return flow; } - int32_t ret = instance.memorySize; - uint32_t delta = flow.getSingleValue().geti32(); - if (delta > uint32_t(-1) / Memory::kPageSize) { + Flow ret = Literal::makeFromUInt64(instance.memorySize, indexType); + uint64_t delta = flow.getSingleValue().getInteger(); + if (delta > uint32_t(-1) / Memory::kPageSize && indexType == Type::i32) { return fail; } - if (instance.memorySize >= uint32_t(-1) - delta) { + if (instance.memorySize >= uint32_t(-1) - delta && + indexType == Type::i32) { return fail; } - uint32_t newSize = instance.memorySize + delta; + auto newSize = instance.memorySize + delta; if (newSize > instance.wasm.memory.max) { return fail; } instance.externalInterface->growMemory( instance.memorySize * Memory::kPageSize, newSize * Memory::kPageSize); instance.memorySize = newSize; - return Literal(int32_t(ret)); + return ret; } Flow visitMemoryInit(MemoryInit* curr) { NOTE_ENTER("MemoryInit"); @@ -2342,7 +2345,7 @@ private: assert(curr->segment < instance.wasm.memory.segments.size()); Memory::Segment& segment = instance.wasm.memory.segments[curr->segment]; - Address destVal(uint32_t(dest.getSingleValue().geti32())); + Address destVal(dest.getSingleValue().getInteger()); Address offsetVal(uint32_t(offset.getSingleValue().geti32())); Address sizeVal(uint32_t(size.getSingleValue().geti32())); @@ -2353,12 +2356,11 @@ private: if ((uint64_t)offsetVal + sizeVal > segment.data.size()) { trap("out of bounds segment access in memory.init"); } - if ((uint64_t)destVal + sizeVal > - (uint64_t)instance.memorySize * Memory::kPageSize) { + if (destVal + sizeVal > instance.memorySize * Memory::kPageSize) { trap("out of bounds memory access in memory.init"); } for (size_t i = 0; i < sizeVal; ++i) { - Literal addr(uint32_t(destVal + i)); + Literal addr(destVal + i); instance.externalInterface->store8( instance.getFinalAddressWithoutOffset(addr, 1), segment.data[offsetVal + i]); @@ -2387,14 +2389,15 @@ private: NOTE_EVAL1(dest); NOTE_EVAL1(source); NOTE_EVAL1(size); - Address destVal(uint32_t(dest.getSingleValue().geti32())); - Address sourceVal(uint32_t(source.getSingleValue().geti32())); - Address sizeVal(uint32_t(size.getSingleValue().geti32())); - - if ((uint64_t)sourceVal + sizeVal > - (uint64_t)instance.memorySize * Memory::kPageSize || - (uint64_t)destVal + sizeVal > - (uint64_t)instance.memorySize * Memory::kPageSize) { + Address destVal(dest.getSingleValue().getInteger()); + Address sourceVal(source.getSingleValue().getInteger()); + Address sizeVal(size.getSingleValue().getInteger()); + + if (sourceVal + sizeVal > instance.memorySize * Memory::kPageSize || + destVal + sizeVal > instance.memorySize * Memory::kPageSize || + // FIXME: better/cheaper way to detect wrapping? + sourceVal + sizeVal < sourceVal || sourceVal + sizeVal < sizeVal || + destVal + sizeVal < destVal || destVal + sizeVal < sizeVal) { trap("out of bounds segment access in memory.copy"); } @@ -2409,11 +2412,9 @@ private: } for (int64_t i = start; i != end; i += step) { instance.externalInterface->store8( - instance.getFinalAddressWithoutOffset(Literal(uint32_t(destVal + i)), - 1), + instance.getFinalAddressWithoutOffset(Literal(destVal + i), 1), instance.externalInterface->load8s( - instance.getFinalAddressWithoutOffset( - Literal(uint32_t(sourceVal + i)), 1))); + instance.getFinalAddressWithoutOffset(Literal(sourceVal + i), 1))); } return {}; } @@ -2434,19 +2435,16 @@ private: NOTE_EVAL1(dest); NOTE_EVAL1(value); NOTE_EVAL1(size); - Address destVal(uint32_t(dest.getSingleValue().geti32())); - Address sizeVal(uint32_t(size.getSingleValue().geti32())); + Address destVal(dest.getSingleValue().getInteger()); + Address sizeVal(size.getSingleValue().getInteger()); - if ((uint64_t)destVal + sizeVal > - (uint64_t)instance.memorySize * Memory::kPageSize) { + if (destVal + sizeVal > instance.memorySize * Memory::kPageSize) { trap("out of bounds memory access in memory.fill"); } uint8_t val(value.getSingleValue().geti32()); for (size_t i = 0; i < sizeVal; ++i) { instance.externalInterface->store8( - instance.getFinalAddressWithoutOffset(Literal(uint32_t(destVal + i)), - 1), - val); + instance.getFinalAddressWithoutOffset(Literal(destVal + i), 1), val); } return {}; } diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index 68baf7b08..f413ab5d4 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -174,6 +174,7 @@ public: Expression* parseExpression(Element& s); MixedArena& getAllocator() { return allocator; } + Module& getModule() { return wasm; } private: Expression* makeExpression(Element& s); @@ -246,6 +247,7 @@ private: // Helper functions Type parseOptionalResultType(Element& s, Index& i); Index parseMemoryLimits(Element& s, Index i); + Index parseMemoryIndex(Element& s, Index i); std::vector<Type> parseParamOrLocal(Element& s); std::vector<NameType> parseParamOrLocal(Element& s, size_t& localIndex); std::vector<Type> parseResults(Element& s); diff --git a/src/wasm-stack.h b/src/wasm-stack.h index 6a926eddf..13cce6d6f 100644 --- a/src/wasm-stack.h +++ b/src/wasm-stack.h @@ -429,8 +429,8 @@ private: // Queues the expressions linearly in Stack IR (SIR) class StackIRGenerator : public BinaryenIRWriter<StackIRGenerator> { public: - StackIRGenerator(MixedArena& allocator, Function* func) - : BinaryenIRWriter<StackIRGenerator>(func), allocator(allocator) {} + StackIRGenerator(Module& module, Function* func) + : BinaryenIRWriter<StackIRGenerator>(func), module(module) {} void emit(Expression* curr); void emitScopeEnd(Expression* curr); @@ -443,7 +443,7 @@ public: } void emitFunctionEnd() {} void emitUnreachable() { - stackIR.push_back(makeStackInst(Builder(allocator).makeUnreachable())); + stackIR.push_back(makeStackInst(Builder(module).makeUnreachable())); } void emitDebugLocation(Expression* curr) {} @@ -455,7 +455,7 @@ private: return makeStackInst(StackInst::Basic, origin); } - MixedArena& allocator; + Module& module; StackIR stackIR; // filled in write() }; diff --git a/src/wasm.h b/src/wasm.h index 419e02875..9f61fac37 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -42,20 +42,18 @@ namespace wasm { // An index in a wasm module typedef uint32_t Index; -// An address in linear memory. For now only wasm32 +// An address in linear memory. struct Address { - typedef uint32_t address_t; - address_t addr; + typedef uint32_t address32_t; + typedef uint64_t address64_t; + address64_t addr; Address() : addr(0) {} - Address(uint64_t a) : addr(static_cast<address_t>(a)) { - assert(a <= std::numeric_limits<address_t>::max()); - } + Address(uint64_t a) : addr(a) {} Address& operator=(uint64_t a) { - assert(a <= std::numeric_limits<address_t>::max()); - addr = static_cast<address_t>(a); + addr = a; return *this; } - operator address_t() const { return addr; } + operator address64_t() const { return addr; } Address& operator++() { ++addr; return *this; @@ -1070,6 +1068,9 @@ public: MemorySize() { type = Type::i32; } MemorySize(MixedArena& allocator) : MemorySize() {} + Type ptrType = Type::i32; + + void make64(); void finalize(); }; @@ -1079,7 +1080,9 @@ public: MemoryGrow(MixedArena& allocator) : MemoryGrow() {} Expression* delta = nullptr; + Type ptrType = Type::i32; + void make64(); void finalize(); }; @@ -1361,9 +1364,9 @@ public: class Table : public Importable { public: - static const Address::address_t kPageSize = 1; + static const Address::address32_t kPageSize = 1; static const Index kUnlimitedSize = Index(-1); - // In wasm32, the maximum table size is limited by a 32-bit pointer: 4GB + // In wasm32/64, the maximum table size is limited by a 32-bit pointer: 4GB static const Index kMaxSize = Index(-1); struct Segment { @@ -1398,12 +1401,11 @@ public: class Memory : public Importable { public: - static const Address::address_t kPageSize = 64 * 1024; - static const Address::address_t kUnlimitedSize = Address::address_t(-1); + static const Address::address32_t kPageSize = 64 * 1024; + static const Address::address64_t kUnlimitedSize = Address::address64_t(-1); // In wasm32, the maximum memory size is limited by a 32-bit pointer: 4GB - static const Address::address_t kMaxSize = + static const Address::address32_t kMaxSize32 = (uint64_t(4) * 1024 * 1024 * 1024) / kPageSize; - static const Address::address_t kPageMask = ~(kPageSize - 1); struct Segment { bool isPassive = false; @@ -1429,21 +1431,24 @@ public: bool exists = false; Name name; Address initial = 0; // sizes are in pages - Address max = kMaxSize; + Address max = kMaxSize32; std::vector<Segment> segments; // See comment in Table. bool shared = false; + Type indexType = Type::i32; Memory() { name = Name::fromInt(0); } bool hasMax() { return max != kUnlimitedSize; } + bool is64() { return indexType == Type::i64; } void clear() { exists = false; name = ""; initial = 0; - max = kMaxSize; + max = kMaxSize32; segments.clear(); shared = false; + indexType = Type::i32; } }; @@ -1568,7 +1573,7 @@ public: namespace std { template<> struct hash<wasm::Address> { size_t operator()(const wasm::Address a) const { - return std::hash<wasm::Address::address_t>()(a.addr); + return std::hash<wasm::Address::address64_t>()(a.addr); } }; } // namespace std diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index e958dca6e..18a66fb2c 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -99,12 +99,11 @@ int32_t WasmBinaryWriter::writeU32LEBPlaceholder() { return ret; } -void WasmBinaryWriter::writeResizableLimits(Address initial, - Address maximum, - bool hasMaximum, - bool shared) { +void WasmBinaryWriter::writeResizableLimits( + Address initial, Address maximum, bool hasMaximum, bool shared, bool is64) { uint32_t flags = (hasMaximum ? (uint32_t)BinaryConsts::HasMaximum : 0U) | - (shared ? (uint32_t)BinaryConsts::IsShared : 0U); + (shared ? (uint32_t)BinaryConsts::IsShared : 0U) | + (is64 ? (uint32_t)BinaryConsts::Is64 : 0U); o << U32LEB(flags); o << U32LEB(initial); if (hasMaximum) { @@ -203,7 +202,8 @@ void WasmBinaryWriter::writeMemory() { writeResizableLimits(wasm->memory.initial, wasm->memory.max, wasm->memory.hasMax(), - wasm->memory.shared); + wasm->memory.shared, + wasm->memory.is64()); finishSection(start); } @@ -267,7 +267,8 @@ void WasmBinaryWriter::writeImports() { writeResizableLimits(wasm->memory.initial, wasm->memory.max, wasm->memory.hasMax(), - wasm->memory.shared); + wasm->memory.shared, + wasm->memory.is64()); } if (wasm->table.imported()) { BYN_TRACE("write one table\n"); @@ -277,7 +278,8 @@ void WasmBinaryWriter::writeImports() { writeResizableLimits(wasm->table.initial, wasm->table.max, wasm->table.hasMax(), - /*shared=*/false); + /*shared=*/false, + /*is64*/ false); } finishSection(start); } @@ -505,7 +507,8 @@ void WasmBinaryWriter::writeFunctionTableDeclaration() { writeResizableLimits(wasm->table.initial, wasm->table.max, wasm->table.hasMax(), - /*shared=*/false); + /*shared=*/false, + /*is64*/ false); finishSection(start); } @@ -752,6 +755,8 @@ void WasmBinaryWriter::writeFeaturesSection() { return BinaryConsts::UserSections::MultivalueFeature; case FeatureSet::GC: return BinaryConsts::UserSections::GCFeature; + case FeatureSet::Memory64: + return BinaryConsts::UserSections::Memory64Feature; default: WASM_UNREACHABLE("unexpected feature flag"); } @@ -1317,6 +1322,7 @@ void WasmBinaryBuilder::readMemory() { getResizableLimits(wasm.memory.initial, wasm.memory.max, wasm.memory.shared, + wasm.memory.indexType, Memory::kUnlimitedSize); } @@ -1370,15 +1376,18 @@ Name WasmBinaryBuilder::getEventName(Index index) { void WasmBinaryBuilder::getResizableLimits(Address& initial, Address& max, bool& shared, + Type& indexType, Address defaultIfNoMax) { auto flags = getU32LEB(); initial = getU32LEB(); bool hasMax = (flags & BinaryConsts::HasMaximum) != 0; bool isShared = (flags & BinaryConsts::IsShared) != 0; + bool is64 = (flags & BinaryConsts::Is64) != 0; if (isShared && !hasMax) { throwError("shared memory must have max size"); } shared = isShared; + indexType = is64 ? Type::i64 : Type::i32; if (hasMax) { max = getU32LEB(); } else { @@ -1425,11 +1434,18 @@ void WasmBinaryBuilder::readImports() { } wasm.table.exists = true; bool is_shared; - getResizableLimits( - wasm.table.initial, wasm.table.max, is_shared, Table::kUnlimitedSize); + Type indexType; + getResizableLimits(wasm.table.initial, + wasm.table.max, + is_shared, + indexType, + Table::kUnlimitedSize); if (is_shared) { throwError("Tables may not be shared"); } + if (indexType == Type::i64) { + throwError("Tables may not be 64-bit"); + } break; } case ExternalKind::Memory: { @@ -1440,6 +1456,7 @@ void WasmBinaryBuilder::readImports() { getResizableLimits(wasm.memory.initial, wasm.memory.max, wasm.memory.shared, + wasm.memory.indexType, Memory::kUnlimitedSize); break; } @@ -2096,11 +2113,18 @@ void WasmBinaryBuilder::readFunctionTableDeclaration() { throwError("ElementType must be funcref in MVP"); } bool is_shared; - getResizableLimits( - wasm.table.initial, wasm.table.max, is_shared, Table::kUnlimitedSize); + Type indexType; + getResizableLimits(wasm.table.initial, + wasm.table.max, + is_shared, + indexType, + Table::kUnlimitedSize); if (is_shared) { throwError("Tables may not be shared"); } + if (indexType == Type::i64) { + throwError("Tables may not be 64-bit"); + } } void WasmBinaryBuilder::readTableElements() { @@ -2309,6 +2333,8 @@ void WasmBinaryBuilder::readFeatures(size_t payloadLen) { wasm.features.setMultivalue(); } else if (name == BinaryConsts::UserSections::GCFeature) { wasm.features.setGC(); + } else if (name == BinaryConsts::UserSections::Memory64Feature) { + wasm.features.setMemory64(); } } } @@ -2452,14 +2478,24 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) { case BinaryConsts::BrOnExn: visitBrOnExn((curr = allocator.alloc<BrOnExn>())->cast<BrOnExn>()); break; - case BinaryConsts::MemorySize: - visitMemorySize( - (curr = allocator.alloc<MemorySize>())->cast<MemorySize>()); + case BinaryConsts::MemorySize: { + auto size = allocator.alloc<MemorySize>(); + if (wasm.memory.is64()) { + size->make64(); + } + curr = size; + visitMemorySize(size); break; - case BinaryConsts::MemoryGrow: - visitMemoryGrow( - (curr = allocator.alloc<MemoryGrow>())->cast<MemoryGrow>()); + } + case BinaryConsts::MemoryGrow: { + auto grow = allocator.alloc<MemoryGrow>(); + if (wasm.memory.is64()) { + grow->make64(); + } + curr = grow; + visitMemoryGrow(grow); break; + } case BinaryConsts::AtomicPrefix: { code = static_cast<uint8_t>(getU32LEB()); if (maybeVisitLoad(curr, code, /*isAtomic=*/true)) { diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 2883519f2..8bbcc9ac0 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -50,12 +50,13 @@ int unhex(char c) { namespace wasm { -static Address getCheckedAddress(const Element* s, const char* errorText) { - uint64_t num = atoll(s->c_str()); - if (num > std::numeric_limits<Address::address_t>::max()) { - throw ParseException(errorText, s->line, s->col); +static Address getAddress(const Element* s) { return atoll(s->c_str()); } + +static void +checkAddress(Address a, const char* errorText, const Element* errorElem) { + if (a > std::numeric_limits<Address::address32_t>::max()) { + throw ParseException(errorText, errorElem->line, errorElem->col); } - return num; } static bool elementStartsWith(Element& s, IString str) { @@ -1023,12 +1024,18 @@ Expression* SExpressionWasmBuilder::makeDrop(Element& s) { Expression* SExpressionWasmBuilder::makeMemorySize(Element& s) { auto ret = allocator.alloc<MemorySize>(); + if (wasm.memory.is64()) { + ret->make64(); + } ret->finalize(); return ret; } Expression* SExpressionWasmBuilder::makeMemoryGrow(Element& s) { auto ret = allocator.alloc<MemoryGrow>(); + if (wasm.memory.is64()) { + ret->make64(); + } ret->delta = parseExpression(s[1]); ret->finalize(); return ret; @@ -2007,18 +2014,40 @@ void SExpressionWasmBuilder::stringToBinary(const char* input, data.resize(actual); } +Index SExpressionWasmBuilder::parseMemoryIndex(Element& s, Index i) { + if (i < s.size() && s[i]->isStr()) { + if (s[i]->str() == "i64") { + i++; + wasm.memory.indexType = Type::i64; + } else if (s[i]->str() == "i32") { + i++; + wasm.memory.indexType = Type::i32; + } + } + return i; +} + Index SExpressionWasmBuilder::parseMemoryLimits(Element& s, Index i) { - wasm.memory.initial = getCheckedAddress(s[i++], "excessive memory init"); + i = parseMemoryIndex(s, i); if (i == s.size()) { - wasm.memory.max = Memory::kUnlimitedSize; - return i; + throw ParseException("missing memory limits", s.line, s.col); } - uint64_t max = atoll(s[i]->c_str()); - if (max > Memory::kMaxSize) { - throw ParseException("total memory must be <= 4GB", s[i]->line, s[i]->col); + auto initElem = s[i++]; + wasm.memory.initial = getAddress(initElem); + if (!wasm.memory.is64()) { + checkAddress(wasm.memory.initial, "excessive memory init", initElem); } - wasm.memory.max = max; - return ++i; + if (i == s.size()) { + wasm.memory.max = Memory::kUnlimitedSize; + } else { + auto maxElem = s[i++]; + wasm.memory.max = getAddress(maxElem); + if (!wasm.memory.is64() && wasm.memory.max > Memory::kMaxSize32) { + throw ParseException( + "total memory must be <= 4GB", maxElem->line, maxElem->col); + } + } + return i; } void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) { @@ -2031,6 +2060,7 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) { if (s[i]->dollared()) { wasm.memory.name = s[i++]->str(); } + i = parseMemoryIndex(s, i); Name importModule, importBase; if (s[i]->isList()) { auto& inner = *s[i]; @@ -2057,8 +2087,9 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) { throw ParseException("bad import ending", inner.line, inner.col); } // (memory (data ..)) format + auto j = parseMemoryIndex(inner, 1); auto offset = allocator.alloc<Const>()->set(Literal(int32_t(0))); - parseInnerData(*s[i], 1, offset, false); + parseInnerData(inner, j, offset, false); wasm.memory.initial = wasm.memory.segments[0].data.size(); return; } @@ -2075,7 +2106,11 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) { if (elementStartsWith(curr, DATA)) { offsetValue = 0; } else { - offsetValue = getCheckedAddress(curr[j++], "excessive memory offset"); + auto offsetElem = curr[j++]; + offsetValue = getAddress(offsetElem); + if (!wasm.memory.is64()) { + checkAddress(offsetValue, "excessive memory offset", offsetElem); + } } const char* input = curr[j]->c_str(); auto* offset = allocator.alloc<Const>(); @@ -2263,12 +2298,14 @@ void SExpressionWasmBuilder::parseImport(Element& s) { wasm.table.module = module; wasm.table.base = base; if (j < inner.size() - 1) { - wasm.table.initial = - getCheckedAddress(inner[j++], "excessive table init size"); + auto initElem = inner[j++]; + wasm.table.initial = getAddress(initElem); + checkAddress(wasm.table.initial, "excessive table init size", initElem); } if (j < inner.size() - 1) { - wasm.table.max = - getCheckedAddress(inner[j++], "excessive table max size"); + auto maxElem = inner[j++]; + wasm.table.max = getAddress(maxElem); + checkAddress(wasm.table.max, "excessive table max size", maxElem); } else { wasm.table.max = Table::kUnlimitedSize; } diff --git a/src/wasm/wasm-stack.cpp b/src/wasm/wasm-stack.cpp index edfdf31d1..30816546a 100644 --- a/src/wasm/wasm-stack.cpp +++ b/src/wasm/wasm-stack.cpp @@ -1907,7 +1907,7 @@ void StackIRGenerator::emitScopeEnd(Expression* curr) { StackInst* StackIRGenerator::makeStackInst(StackInst::Op op, Expression* origin) { - auto* ret = allocator.alloc<StackInst>(); + auto* ret = module.allocator.alloc<StackInst>(); ret->op = op; ret->origin = origin; auto stackType = origin->type; diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp index c141fdd6f..505753c88 100644 --- a/src/wasm/wasm-validator.cpp +++ b/src/wasm/wasm-validator.cpp @@ -388,6 +388,8 @@ private: void validateAlignment( size_t align, Type type, Index bytes, bool isAtomic, Expression* curr); void validateMemBytes(uint8_t bytes, Type type, Expression* curr); + + Type indexType() { return getModule()->memory.indexType; } }; void FunctionValidator::noteLabelName(Name name) { @@ -931,7 +933,10 @@ void FunctionValidator::visitLoad(Load* curr) { validateMemBytes(curr->bytes, curr->type, curr); validateAlignment(curr->align, curr->type, curr->bytes, curr->isAtomic, curr); shouldBeEqualOrFirstIsUnreachable( - curr->ptr->type, Type(Type::i32), curr, "load pointer type must be i32"); + curr->ptr->type, + indexType(), + curr, + "load pointer type must match memory index type"); if (curr->isAtomic) { shouldBeFalse(curr->signed_, curr, "atomic loads must be unsigned"); shouldBeIntOrUnreachable( @@ -963,7 +968,10 @@ void FunctionValidator::visitStore(Store* curr) { validateAlignment( curr->align, curr->valueType, curr->bytes, curr->isAtomic, curr); shouldBeEqualOrFirstIsUnreachable( - curr->ptr->type, Type(Type::i32), curr, "store pointer type must be i32"); + curr->ptr->type, + indexType(), + curr, + "store pointer must match memory index type"); shouldBeUnequal(curr->value->type, Type(Type::none), curr, @@ -986,10 +994,11 @@ void FunctionValidator::visitAtomicRMW(AtomicRMW* curr) { curr, "Atomic operation with non-shared memory"); validateMemBytes(curr->bytes, curr->type, curr); - shouldBeEqualOrFirstIsUnreachable(curr->ptr->type, - Type(Type::i32), - curr, - "AtomicRMW pointer type must be i32"); + shouldBeEqualOrFirstIsUnreachable( + curr->ptr->type, + indexType(), + curr, + "AtomicRMW pointer type must match memory index type"); shouldBeEqualOrFirstIsUnreachable(curr->type, curr->value->type, curr, @@ -1009,7 +1018,10 @@ void FunctionValidator::visitAtomicCmpxchg(AtomicCmpxchg* curr) { "Atomic operation with non-shared memory"); validateMemBytes(curr->bytes, curr->type, curr); shouldBeEqualOrFirstIsUnreachable( - curr->ptr->type, Type(Type::i32), curr, "cmpxchg pointer type must be i32"); + curr->ptr->type, + indexType(), + curr, + "cmpxchg pointer must match memory index type"); if (curr->expected->type != Type::unreachable && curr->replacement->type != Type::unreachable) { shouldBeEqual(curr->expected->type, @@ -1042,10 +1054,11 @@ void FunctionValidator::visitAtomicWait(AtomicWait* curr) { "Atomic operation with non-shared memory"); shouldBeEqualOrFirstIsUnreachable( curr->type, Type(Type::i32), curr, "AtomicWait must have type i32"); - shouldBeEqualOrFirstIsUnreachable(curr->ptr->type, - Type(Type::i32), - curr, - "AtomicWait pointer type must be i32"); + shouldBeEqualOrFirstIsUnreachable( + curr->ptr->type, + indexType(), + curr, + "AtomicWait pointer must match memory index type"); shouldBeIntOrUnreachable( curr->expected->type, curr, "AtomicWait expected type must be int"); shouldBeEqualOrFirstIsUnreachable( @@ -1070,10 +1083,11 @@ void FunctionValidator::visitAtomicNotify(AtomicNotify* curr) { "Atomic operation with non-shared memory"); shouldBeEqualOrFirstIsUnreachable( curr->type, Type(Type::i32), curr, "AtomicNotify must have type i32"); - shouldBeEqualOrFirstIsUnreachable(curr->ptr->type, - Type(Type::i32), - curr, - "AtomicNotify pointer type must be i32"); + shouldBeEqualOrFirstIsUnreachable( + curr->ptr->type, + indexType(), + curr, + "AtomicNotify pointer must match memory index type"); shouldBeEqualOrFirstIsUnreachable( curr->notifyCount->type, Type(Type::i32), @@ -1230,10 +1244,11 @@ void FunctionValidator::visitSIMDLoad(SIMDLoad* curr) { getModule()->features.hasSIMD(), curr, "SIMD operation (SIMD is disabled)"); shouldBeEqualOrFirstIsUnreachable( curr->type, Type(Type::v128), curr, "load_splat must have type v128"); - shouldBeEqualOrFirstIsUnreachable(curr->ptr->type, - Type(Type::i32), - curr, - "load_splat address must have type i32"); + shouldBeEqualOrFirstIsUnreachable( + curr->ptr->type, + indexType(), + curr, + "load_splat address must match memory index type"); Type memAlignType = Type::none; switch (curr->op) { case LoadSplatVec8x16: @@ -1264,7 +1279,10 @@ void FunctionValidator::visitMemoryInit(MemoryInit* curr) { shouldBeEqualOrFirstIsUnreachable( curr->type, Type(Type::none), curr, "memory.init must have type none"); shouldBeEqualOrFirstIsUnreachable( - curr->dest->type, Type(Type::i32), curr, "memory.init dest must be an i32"); + curr->dest->type, + indexType(), + curr, + "memory.init dest must match memory index type"); shouldBeEqualOrFirstIsUnreachable(curr->offset->type, Type(Type::i32), curr, @@ -1304,13 +1322,20 @@ void FunctionValidator::visitMemoryCopy(MemoryCopy* curr) { shouldBeEqualOrFirstIsUnreachable( curr->type, Type(Type::none), curr, "memory.copy must have type none"); shouldBeEqualOrFirstIsUnreachable( - curr->dest->type, Type(Type::i32), curr, "memory.copy dest must be an i32"); - shouldBeEqualOrFirstIsUnreachable(curr->source->type, - Type(Type::i32), - curr, - "memory.copy source must be an i32"); + curr->dest->type, + indexType(), + curr, + "memory.copy dest must match memory index type"); + shouldBeEqualOrFirstIsUnreachable( + curr->source->type, + indexType(), + curr, + "memory.copy source must match memory index type"); shouldBeEqualOrFirstIsUnreachable( - curr->size->type, Type(Type::i32), curr, "memory.copy size must be an i32"); + curr->size->type, + indexType(), + curr, + "memory.copy size must match memory index type"); shouldBeTrue( getModule()->memory.exists, curr, "Memory operations require a memory"); } @@ -1322,13 +1347,19 @@ void FunctionValidator::visitMemoryFill(MemoryFill* curr) { shouldBeEqualOrFirstIsUnreachable( curr->type, Type(Type::none), curr, "memory.fill must have type none"); shouldBeEqualOrFirstIsUnreachable( - curr->dest->type, Type(Type::i32), curr, "memory.fill dest must be an i32"); + curr->dest->type, + indexType(), + curr, + "memory.fill dest must match memory index type"); shouldBeEqualOrFirstIsUnreachable(curr->value->type, Type(Type::i32), curr, "memory.fill value must be an i32"); shouldBeEqualOrFirstIsUnreachable( - curr->size->type, Type(Type::i32), curr, "memory.fill size must be an i32"); + curr->size->type, + indexType(), + curr, + "memory.fill size must match memory index type"); shouldBeTrue( getModule()->memory.exists, curr, "Memory operations require a memory"); } @@ -1907,9 +1938,9 @@ void FunctionValidator::visitMemoryGrow(MemoryGrow* curr) { shouldBeTrue( getModule()->memory.exists, curr, "Memory operations require a memory"); shouldBeEqualOrFirstIsUnreachable(curr->delta->type, - Type(Type::i32), + indexType(), curr, - "memory.grow must have i32 operand"); + "memory.grow must match memory index type"); } void FunctionValidator::visitRefIsNull(RefIsNull* curr) { @@ -2113,7 +2144,7 @@ void FunctionValidator::visitFunction(Function* curr) { } } -static bool checkOffset(Expression* curr, Address add, Address max) { +static bool checkSegmentOffset(Expression* curr, Address add, Address max) { if (curr->is<GlobalGet>()) { return true; } @@ -2122,10 +2153,10 @@ static bool checkOffset(Expression* curr, Address add, Address max) { return false; } uint64_t raw = c->value.getInteger(); - if (raw > std::numeric_limits<Address::address_t>::max()) { + if (raw > std::numeric_limits<Address::address32_t>::max()) { return false; } - if (raw + uint64_t(add) > std::numeric_limits<Address::address_t>::max()) { + if (raw + uint64_t(add) > std::numeric_limits<Address::address32_t>::max()) { return false; } Address offset = raw; @@ -2354,12 +2385,14 @@ static void validateMemory(Module& module, ValidationInfo& info) { auto& curr = module.memory; info.shouldBeFalse( curr.initial > curr.max, "memory", "memory max >= initial"); - info.shouldBeTrue(curr.initial <= Memory::kMaxSize, - "memory", - "initial memory must be <= 4GB"); - info.shouldBeTrue(!curr.hasMax() || curr.max <= Memory::kMaxSize, - "memory", - "max memory must be <= 4GB, or unlimited"); + if (!curr.is64()) { + info.shouldBeTrue(curr.initial <= Memory::kMaxSize32, + "memory", + "initial memory must be <= 4GB"); + info.shouldBeTrue(!curr.hasMax() || curr.max <= Memory::kMaxSize32, + "memory", + "max memory must be <= 4GB, or unlimited"); + } info.shouldBeTrue(!curr.shared || curr.hasMax(), "memory", "shared memory must have max size"); @@ -2385,9 +2418,9 @@ static void validateMemory(Module& module, ValidationInfo& info) { "segment offset should be i32")) { continue; } - info.shouldBeTrue(checkOffset(segment.offset, - segment.data.size(), - curr.initial * Memory::kPageSize), + info.shouldBeTrue(checkSegmentOffset(segment.offset, + segment.data.size(), + curr.initial * Memory::kPageSize), segment.offset, "segment offset should be reasonable"); if (segment.offset->is<Const>()) { @@ -2416,11 +2449,12 @@ static void validateTable(Module& module, ValidationInfo& info) { Type(Type::i32), segment.offset, "segment offset should be i32"); - info.shouldBeTrue(checkOffset(segment.offset, - segment.data.size(), - module.table.initial * Table::kPageSize), - segment.offset, - "segment offset should be reasonable"); + info.shouldBeTrue( + checkSegmentOffset(segment.offset, + segment.data.size(), + module.table.initial * Table::kPageSize), + segment.offset, + "segment offset should be reasonable"); for (auto name : segment.data) { info.shouldBeTrue( module.getFunctionOrNull(name), name, "segment name should be valid"); diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 87488cfaf..6cc61a8a1 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -46,6 +46,7 @@ const char* TailCallFeature = "tail-call"; const char* ReferenceTypesFeature = "reference-types"; const char* MultivalueFeature = "multivalue"; const char* GCFeature = "gc"; +const char* Memory64Feature = "memory64"; } // namespace UserSections } // namespace BinaryConsts @@ -881,13 +882,15 @@ void Drop::finalize() { } } -void MemorySize::finalize() { type = Type::i32; } +void MemorySize::make64() { type = ptrType = Type::i64; } +void MemorySize::finalize() { type = ptrType; } +void MemoryGrow::make64() { type = ptrType = Type::i64; } void MemoryGrow::finalize() { if (delta->type == Type::unreachable) { type = Type::unreachable; } else { - type = Type::i32; + type = ptrType; } } diff --git a/src/wasm2js.h b/src/wasm2js.h index 727e87379..9350b38d6 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -423,7 +423,7 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { asmFunc[3]->push_back(processFunction(wasm, func)); }); if (generateFetchHighBits) { - Builder builder(allocator); + Builder builder(allocator, *wasm); asmFunc[3]->push_back( processFunction(wasm, wasm->addFunction(builder.makeFunction( @@ -2122,7 +2122,7 @@ void Wasm2JSBuilder::addMemoryGrowthFuncs(Ref ast, Module* wasm) { IString("&&"), ValueBuilder::makeBinary(ValueBuilder::makeName(IString("newPages")), LT, - ValueBuilder::makeInt(Memory::kMaxSize))), + ValueBuilder::makeInt(Memory::kMaxSize32))), block, NULL)); |