Skip to content

Commit ab1d636

Browse files
committed
Validate tag attribute field and empty tag result type.
Throw on tag import and export identifiers if exception handling is disabled.
1 parent 0587fd0 commit ab1d636

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

wasm/src/org.graalvm.wasm.test/src/org/graalvm/wasm/test/suites/validation/ValidationSuite.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public static Collection<Object[]> data() {
205205
Failure.Type.MALFORMED),
206206
binaryCase(
207207
"Global - invalid modified",
208-
"Invalid mutability flag: 2",
208+
"Invalid mutability flag: 0x02",
209209
"00 61 73 6D 01 00 00 00 06 06 01 7F 02 41 00 0B",
210210
Failure.Type.MALFORMED),
211211
binaryCase(
@@ -993,7 +993,24 @@ public static Collection<Object[]> data() {
993993
"Data Count Section - not supported",
994994
"invalid section ID: 12",
995995
"00 61 73 6D 01 00 00 00 0C 00",
996-
Failure.Type.MALFORMED));
996+
Failure.Type.MALFORMED),
997+
998+
// Tag section
999+
binaryCase(
1000+
"Tag section - invalid attribute",
1001+
"Invalid tag attribute: 0x01",
1002+
"00 61 73 6d 01 00 00 00 " +
1003+
"01 04 01 60 00 00 " + // type section
1004+
"0d 03 01 01 00", // tag section
1005+
Failure.Type.MALFORMED),
1006+
binaryCase(
1007+
"Tag section - non-empty result type",
1008+
"non-empty tag result type: 1 should = 0",
1009+
// (module (tag (result i32)))
1010+
"00 61 73 6d 01 00 00 00 " +
1011+
"01 05 01 60 00 01 7f " + // type section
1012+
"0d 03 01 00 00", // tag section
1013+
Failure.Type.INVALID));
9971014
}
9981015

9991016
private final String expectedErrorMessage;

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryParser.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,17 @@ private void readImportSection() {
450450
break;
451451
}
452452
case ImportIdentifier.TAG: {
453-
final byte attribute = read1();
453+
if (!exceptions) {
454+
fail(Failure.MALFORMED_IMPORT_KIND, "Invalid import type identifier: 0x%02x", importType);
455+
}
456+
final byte attribute = readTagAttribute();
454457
final int typeIndex = readTypeIndex();
455458
final int tagIndex = module.symbolTable().tagCount();
456459
module.symbolTable().importTag(moduleName, memberName, tagIndex, attribute, typeIndex);
457460
break;
458461
}
459462
default: {
460-
fail(Failure.MALFORMED_IMPORT_KIND, "Invalid import type identifier: 0x%02X", importType);
463+
fail(Failure.MALFORMED_IMPORT_KIND, "Invalid import type identifier: 0x%02x", importType);
461464
}
462465
}
463466
}
@@ -2932,12 +2935,15 @@ private void readExportSection() {
29322935
break;
29332936
}
29342937
case ExportIdentifier.TAG: {
2938+
if (!exceptions) {
2939+
fail(Failure.UNSPECIFIED_MALFORMED, "Invalid export type identifier: 0x%02x", exportType);
2940+
}
29352941
final int index = readTagIndex();
29362942
module.symbolTable().exportTag(index, exportName);
29372943
break;
29382944
}
29392945
default: {
2940-
fail(Failure.UNSPECIFIED_MALFORMED, "Invalid export type identifier: 0x%02X", exportType);
2946+
fail(Failure.UNSPECIFIED_MALFORMED, "Invalid export type identifier: 0x%02x", exportType);
29412947
}
29422948
}
29432949
}
@@ -2950,8 +2956,7 @@ private void readTagSection() {
29502956
for (int tagIndex = startingTagIndex; tagIndex != startingTagIndex + tagCount; tagIndex++) {
29512957
assertTrue(!isEOF(), Failure.LENGTH_OUT_OF_BOUNDS);
29522958
// 0x00 means exception
2953-
final byte attribute = read1();
2954-
assertByteEqual(attribute, (byte) WasmTag.Attribute.EXCEPTION, Failure.MALFORMED_TAG_ATTRIBUTE);
2959+
final byte attribute = readTagAttribute();
29552960
final int type = readTypeIndex();
29562961

29572962
module.symbolTable().allocateTag(tagIndex, attribute, type);

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryStreamParser.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,25 @@ protected byte peekMutability() {
204204
} else if (mut == GlobalModifier.MUTABLE) {
205205
return mut;
206206
} else {
207-
throw Assert.fail(Failure.MALFORMED_MUTABILITY, "Invalid mutability flag: " + mut);
207+
throw Assert.fail(Failure.MALFORMED_MUTABILITY, "Invalid mutability flag: 0x%02x", mut);
208+
}
209+
}
210+
211+
/**
212+
* Reads the attribute of a tag (uint8).
213+
*/
214+
protected byte readTagAttribute() {
215+
final byte attribute = peekTagAttribute();
216+
offset++;
217+
return attribute;
218+
}
219+
220+
protected byte peekTagAttribute() {
221+
final byte attribute = peek1();
222+
if (attribute == WasmTag.Attribute.EXCEPTION) {
223+
return attribute;
224+
} else {
225+
throw Assert.fail(Failure.MALFORMED_TAG_ATTRIBUTE, "Invalid tag attribute: 0x%02x", attribute);
208226
}
209227
}
210228

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/SymbolTable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ public void importTag(String moduleName, String tagName, int index, byte attribu
11841184
}
11851185

11861186
void addTag(int index, byte attribute, int typeIndex) {
1187+
assertIntEqual(functionTypeResultCount(typeIndex), 0, Failure.NON_EMPTY_TAG_RESULT_TYPE);
11871188
ensureTagCapacity(index);
11881189
final TagInfo tag = new TagInfo(attribute, typeIndex);
11891190
tags[index] = tag;

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/exception/Failure.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public enum Failure {
7676
UNSPECIFIED_INVALID(Type.INVALID, "unspecified"),
7777
TYPE_MISMATCH(Type.INVALID, "type mismatch"),
7878
INVALID_RESULT_ARITY(Type.INVALID, "invalid result arity"),
79+
NON_EMPTY_TAG_RESULT_TYPE(Type.INVALID, "non-empty tag result type"),
7980
MULTIPLE_MEMORIES(Type.INVALID, "multiple memories"),
8081
MULTIPLE_TABLES(Type.INVALID, "multiple tables"),
8182
LOOP_INPUT(Type.INVALID, "non-empty loop input type"),

0 commit comments

Comments
 (0)