Skip to content

Commit 126b4f7

Browse files
authored
[wasm-dis] Restore features section in output (#7840)
This effectively restores pre-#7802 status, except for the case the input binary has the features section. If the binary does not have it, we restore the default features (mutable-global + sign-ext) + whatever is given in the command line before the printing. If the binary has the features section, we just restore it before the printing. This currently does not handle the case when there is the features section and the user gives more features flags in the command line, but in the current interface it is cumbersome to handle. And we were not handling that anyway pre-#7802, so it is not a regression. And we preserve the `FeaturesSet::All` setting at parsing time so @tlively's [concern](#7836 (comment)) will not be an issue. Because we preserve the features section, #7836 will be addressed too. The main motivation for #7802 was `call_indirect` could be incorrectly printed, and that was also separately addressed by #7843. `ModuleReader::getFeaturesSectionFeatures` was adopted from @tlively's commit: ed36a8c Because we don't set the features to `Features::All` at the printing time, `(type $..)` in `(func)` definitions and `(table ..)` in `call_indirect`s have been removed. Fixes #7836.
1 parent 969bf76 commit 126b4f7

38 files changed

+160
-140
lines changed

src/tools/wasm-dis.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// wasm2asm console tool
1919
//
2020

21+
#include "parsing.h"
2122
#include "source-map.h"
2223
#include "support/colors.h"
2324
#include "support/file.h"
@@ -67,10 +68,12 @@ int main(int argc, const char* argv[]) {
6768
}
6869
Module wasm;
6970
options.applyOptionsBeforeParse(wasm);
71+
auto enabledFeatures = wasm.features;
7072
wasm.features = FeatureSet::All;
7173

74+
auto moduleReader = ModuleReader();
7275
try {
73-
ModuleReader().readBinary(options.extra["infile"], wasm, sourceMapFilename);
76+
moduleReader.readBinary(options.extra["infile"], wasm, sourceMapFilename);
7477
} catch (ParseException& p) {
7578
p.dump(std::cerr);
7679
std::cerr << '\n';
@@ -87,7 +90,11 @@ int main(int argc, const char* argv[]) {
8790
}
8891

8992
options.applyOptionsAfterParse(wasm);
90-
93+
// If the features section is present, restore it. If not, restore the command
94+
// line + default features.
95+
wasm.features = wasm.hasFeaturesSection
96+
? moduleReader.getFeaturesSectionFeatures()
97+
: enabledFeatures;
9198
if (options.debug) {
9299
std::cerr << "Printing..." << std::endl;
93100
}

src/wasm-binary.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "parsing.h"
3131
#include "source-map.h"
3232
#include "wasm-builder.h"
33+
#include "wasm-features.h"
3334
#include "wasm-ir-builder.h"
3435
#include "wasm-traversal.h"
3536
#include "wasm-validator.h"
@@ -1730,12 +1731,19 @@ class WasmBinaryReader {
17301731
throw ParseException(text, 0, pos);
17311732
}
17321733

1734+
// Allow users to query the target features section features after parsing.
1735+
FeatureSet getFeaturesSectionFeatures() { return featuresSectionFeatures; }
1736+
17331737
private:
17341738
// In certain modes we need to note the locations of expressions, to match
17351739
// them against sections like DWARF or custom annotations. As this incurs
17361740
// overhead, we only note locations when we actually need to.
17371741
bool needCodeLocations = false;
17381742

1743+
// The features enabled by the target features section, which may be a subset
1744+
// of the features enabled for the module.
1745+
FeatureSet featuresSectionFeatures;
1746+
17391747
// Scans ahead in the binary to check certain conditions like
17401748
// needCodeLocations.
17411749
void preScan();

src/wasm-io.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#ifndef wasm_wasm_io_h
2222
#define wasm_wasm_io_h
2323

24-
#include "parsing.h"
2524
#include "pass.h"
2625
#include "support/file.h"
2726
#include "wasm.h"
@@ -67,13 +66,17 @@ class ModuleReader : public ModuleIOBase {
6766
// check whether a file is a wasm binary
6867
bool isBinaryFile(std::string filename);
6968

69+
FeatureSet getFeaturesSectionFeatures() { return featuresSectionFeatures; }
70+
7071
private:
7172
bool DWARF = false;
7273

7374
IRProfile profile = IRProfile::Normal;
7475

7576
bool skipFunctionBodies = false;
7677

78+
FeatureSet featuresSectionFeatures = FeatureSet::MVP;
79+
7780
void readStdin(Module& wasm, std::string sourceMapFilename);
7881

7982
void readBinaryData(std::vector<char>& input,

src/wasm/wasm-binary.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5262,12 +5262,14 @@ void WasmBinaryReader::readFeatures(size_t sectionPos, size_t payloadLen) {
52625262
<< " was enabled by the user, but disallowed in the features section.";
52635263
}
52645264
if (used) {
5265-
wasm.features.enable(feature);
5265+
featuresSectionFeatures.enable(feature);
52665266
}
52675267
}
52685268
if (pos != sectionPos + payloadLen) {
52695269
throwError("bad features section size");
52705270
}
5271+
5272+
wasm.features.enable(featuresSectionFeatures);
52715273
}
52725274

52735275
void WasmBinaryReader::readDylink(size_t payloadLen) {

src/wasm/wasm-io.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ void ModuleReader::readBinaryData(std::vector<char>& input,
6565
parser.setDWARF(DWARF);
6666
parser.setSkipFunctionBodies(skipFunctionBodies);
6767
parser.read();
68+
if (wasm.hasFeaturesSection) {
69+
featuresSectionFeatures = parser.getFeaturesSectionFeatures();
70+
}
6871
}
6972

7073
void ModuleReader::readBinary(std::string filename,

test/br_to_exit.wasm.fromBinary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $0 (func))
3-
(func $0 (type $0)
3+
(func $0
44
(block $label
55
(br $label)
66
)

test/br_to_try.wasm.fromBinary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $0 (func (param i32)))
33
(type $1 (func))
44
(tag $tag$0 (type $0) (param i32))
5-
(func $0 (type $1)
5+
(func $0
66
(block $label
77
(try
88
(do

test/break-to-return.wasm.fromBinary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $0 (func (param i32 i32) (result i32)))
33
(memory $0 256 256)
44
(export "add" (func $0))
5-
(func $0 (type $0) (param $0 i32) (param $1 i32) (result i32)
5+
(func $0 (param $0 i32) (param $1 i32) (result i32)
66
(block $label (result i32)
77
(br $label
88
(i32.add

test/break-within-catch.wasm.fromBinary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $0 (func (param i32)))
33
(type $1 (func))
44
(tag $tag$0 (type $0) (param i32))
5-
(func $0 (type $1)
5+
(func $0
66
(block $label
77
(try
88
(do

test/complexBinaryNames.wasm.fromBinary

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
(module
22
(type $0 (func))
33
(export "$zoo (.bar)" (func $1))
4-
(func $foo\20\28.bar\29 (type $0)
4+
(func $foo\20\28.bar\29
55
(nop)
66
)
7-
(func $1 (type $0)
7+
(func $1
88
(call $foo\20\28.bar\29)
99
)
1010
)

0 commit comments

Comments
 (0)