Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array.{get*,set} validation #31

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/valid/validate_instruction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,7 @@ bool ArrayNewDefaultWithRtt(Context& context,
}

bool ArrayGet(Context& context, Location loc, const At<Index>& immediate) {
bool valid = PopType(context, loc, StackType::I32());
auto [stack_type, array_type] = PopArrayReference(context, loc, immediate);
if (!array_type) {
return false;
Expand All @@ -1703,12 +1704,13 @@ bool ArrayGet(Context& context, Location loc, const At<Index>& immediate) {
}

PushType(context, StackType{*value_type});
return true;
return valid;
}

bool ArrayGetPacked(Context& context,
Location loc,
const At<Index>& immediate) {
bool valid = PopType(context, loc, StackType::I32());
auto [stack_type, array_type] = PopArrayReference(context, loc, immediate);
if (!array_type) {
return false;
Expand All @@ -1720,7 +1722,7 @@ bool ArrayGetPacked(Context& context,
}

PushType(context, StackType::I32());
return true;
return valid;
}

bool ArraySet(Context& context, Location loc, const At<Index>& immediate) {
Expand All @@ -1736,9 +1738,12 @@ bool ArraySet(Context& context, Location loc, const At<Index>& immediate) {
valid = false;
}

StackTypeList stack_types{StackType{ValueType{ReferenceType{
RefType{HeapType{immediate}, Null::Yes}}}},
ToStackType(array_type->field->type)};
StackTypeList stack_types{
StackType{
ValueType{ReferenceType{RefType{HeapType{immediate}, Null::Yes}}}},
StackType::I32(),
ToStackType(array_type->field->type),
};
return AllTrue(valid, PopTypes(context, loc, stack_types));
}

Expand Down
22 changes: 11 additions & 11 deletions test/valid/validate_instruction_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3382,16 +3382,16 @@ TEST_F(ValidateInstructionTest, ArrayGet) {
ValueType VT_RefNull_index = MakeValueTypeRef(index, Null::Yes);
ValueType VT_Ref_index = MakeValueTypeRef(index, Null::No);

TestSignature(I{O::ArrayGet, index}, {VT_RefNull_index}, {VT_F32});
TestSignature(I{O::ArrayGet, index}, {VT_Ref_index}, {VT_F32});
TestSignature(I{O::ArrayGet, index}, {VT_RefNull_index, VT_I32}, {VT_F32});
TestSignature(I{O::ArrayGet, index}, {VT_Ref_index, VT_I32}, {VT_F32});
}

TEST_F(ValidateInstructionTest, ArrayGet_FailPacked) {
auto index = AddArrayType(
ArrayType{FieldType{StorageType{PackedType::I8}, Mutability::Const}});
ValueType VT_RefNull_index = MakeValueTypeRef(index, Null::Yes);

FailWithTypeStack(I{O::ArrayGet, index}, {VT_RefNull_index});
FailWithTypeStack(I{O::ArrayGet, index}, {VT_RefNull_index, VT_I32});
}

TEST_F(ValidateInstructionTest, ArrayGetPacked) {
Expand All @@ -3401,21 +3401,21 @@ TEST_F(ValidateInstructionTest, ArrayGetPacked) {
ValueType VT_Ref_index = MakeValueTypeRef(index, Null::No);

// ArrayGetS
TestSignature(I{O::ArrayGetS, index}, {VT_RefNull_index}, {VT_I32});
TestSignature(I{O::ArrayGetS, index}, {VT_Ref_index}, {VT_I32});
TestSignature(I{O::ArrayGetS, index}, {VT_RefNull_index, VT_I32}, {VT_I32});
TestSignature(I{O::ArrayGetS, index}, {VT_Ref_index, VT_I32}, {VT_I32});

// ArrayGetU
TestSignature(I{O::ArrayGetU, index}, {VT_RefNull_index}, {VT_I32});
TestSignature(I{O::ArrayGetU, index}, {VT_Ref_index}, {VT_I32});
TestSignature(I{O::ArrayGetU, index}, {VT_RefNull_index, VT_I32}, {VT_I32});
TestSignature(I{O::ArrayGetU, index}, {VT_Ref_index, VT_I32}, {VT_I32});
}

TEST_F(ValidateInstructionTest, ArrayGetPacked_FailUnpacked) {
auto index = AddArrayType(
ArrayType{FieldType{StorageType{VT_F32}, Mutability::Const}});
ValueType VT_RefNull_index = MakeValueTypeRef(index, Null::Yes);

FailWithTypeStack(I{O::ArrayGetS, index}, {VT_RefNull_index});
FailWithTypeStack(I{O::ArrayGetU, index}, {VT_RefNull_index});
FailWithTypeStack(I{O::ArrayGetS, index}, {VT_RefNull_index, VT_I32});
FailWithTypeStack(I{O::ArrayGetU, index}, {VT_RefNull_index, VT_I32});
}

TEST_F(ValidateInstructionTest, ArraySet) {
Expand All @@ -3424,8 +3424,8 @@ TEST_F(ValidateInstructionTest, ArraySet) {
ValueType VT_RefNull_index = MakeValueTypeRef(index, Null::Yes);
ValueType VT_Ref_index = MakeValueTypeRef(index, Null::No);

TestSignature(I{O::ArraySet, index}, {VT_RefNull_index, VT_F32}, {});
TestSignature(I{O::ArraySet, index}, {VT_Ref_index, VT_F32}, {});
TestSignature(I{O::ArraySet, index}, {VT_RefNull_index, VT_I32, VT_F32}, {});
TestSignature(I{O::ArraySet, index}, {VT_Ref_index, VT_I32, VT_F32}, {});
}

TEST_F(ValidateInstructionTest, ArrayLen) {
Expand Down