|
| 1 | +/* |
| 2 | + * Copyright 2025 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "ir/public-type-validator.h" |
| 18 | +#include "wasm-type.h" |
| 19 | + |
| 20 | +#include "gtest/gtest.h" |
| 21 | + |
| 22 | +using namespace wasm; |
| 23 | + |
| 24 | +class PublicTypeValidatorTest : public ::testing::Test { |
| 25 | +protected: |
| 26 | + HeapType EmptyStruct; |
| 27 | + HeapType RecursiveStruct; |
| 28 | + HeapType InvalidStruct; |
| 29 | + HeapType IndirectlyInvalidStruct; |
| 30 | + HeapType RecursiveInvalidStruct; |
| 31 | + HeapType EmptyStructInGroupWithInvalid; |
| 32 | + |
| 33 | + void SetUp() override { |
| 34 | + TypeBuilder builder(8); |
| 35 | + |
| 36 | + // Empty struct |
| 37 | + HeapType empty = builder[0]; |
| 38 | + builder[0] = Struct(); |
| 39 | + |
| 40 | + // Recursive struct |
| 41 | + HeapType recursive = builder[1]; |
| 42 | + builder[1] = Struct({Field(Type(recursive, Nullable), Mutable)}); |
| 43 | + |
| 44 | + // Invalid struct (when custom descriptors are disabled) |
| 45 | + HeapType invalid = builder[2]; |
| 46 | + builder[2] = Struct({Field(Type(empty, Nullable, Exact), Mutable)}); |
| 47 | + |
| 48 | + // Indirectly invalid struct |
| 49 | + builder[3] = Struct({Field(Type(invalid, Nullable), Mutable)}); |
| 50 | + |
| 51 | + // Mutually recursive, indirectly invalid struct |
| 52 | + builder[4] = Struct({Field(Type(builder[5], Nullable), Mutable)}); |
| 53 | + builder[5] = Struct({Field(Type(builder[4], Nullable, Exact), Mutable)}); |
| 54 | + builder.createRecGroup(4, 2); |
| 55 | + |
| 56 | + // Empty struct in group with invalid struct |
| 57 | + builder[6] = Struct(); |
| 58 | + builder[7] = Struct({Field(Type(invalid, Nullable), Mutable)}); |
| 59 | + builder.createRecGroup(6, 2); |
| 60 | + |
| 61 | + auto result = builder.build(); |
| 62 | + auto& built = *result; |
| 63 | + |
| 64 | + EmptyStruct = built[0]; |
| 65 | + RecursiveStruct = built[1]; |
| 66 | + InvalidStruct = built[2]; |
| 67 | + IndirectlyInvalidStruct = built[3]; |
| 68 | + RecursiveInvalidStruct = built[4]; |
| 69 | + EmptyStructInGroupWithInvalid = built[6]; |
| 70 | + } |
| 71 | + |
| 72 | + void TearDown() override { wasm::destroyAllTypesForTestingPurposesOnly(); } |
| 73 | +}; |
| 74 | + |
| 75 | +TEST_F(PublicTypeValidatorTest, CustomDescriptorsEnabled) { |
| 76 | + PublicTypeValidator validator(FeatureSet::CustomDescriptors); |
| 77 | + |
| 78 | + EXPECT_TRUE(validator.isValidPublicType(EmptyStruct)); |
| 79 | + EXPECT_TRUE(validator.isValidPublicType(RecursiveStruct)); |
| 80 | + EXPECT_TRUE(validator.isValidPublicType(InvalidStruct)); |
| 81 | + EXPECT_TRUE(validator.isValidPublicType(IndirectlyInvalidStruct)); |
| 82 | + EXPECT_TRUE(validator.isValidPublicType(RecursiveInvalidStruct)); |
| 83 | + EXPECT_TRUE(validator.isValidPublicType(EmptyStructInGroupWithInvalid)); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_F(PublicTypeValidatorTest, CustomDescriptorsDisabled) { |
| 87 | + PublicTypeValidator validator(FeatureSet::MVP); |
| 88 | + |
| 89 | + EXPECT_TRUE(validator.isValidPublicType(EmptyStruct)); |
| 90 | + EXPECT_TRUE(validator.isValidPublicType(RecursiveStruct)); |
| 91 | + EXPECT_FALSE(validator.isValidPublicType(InvalidStruct)); |
| 92 | + EXPECT_FALSE(validator.isValidPublicType(IndirectlyInvalidStruct)); |
| 93 | + EXPECT_FALSE(validator.isValidPublicType(RecursiveInvalidStruct)); |
| 94 | + EXPECT_FALSE(validator.isValidPublicType(EmptyStructInGroupWithInvalid)); |
| 95 | +} |
| 96 | + |
| 97 | +TEST_F(PublicTypeValidatorTest, CachedResult) { |
| 98 | + PublicTypeValidator validator(FeatureSet::MVP); |
| 99 | + |
| 100 | + // Check the indirectly invalid type first, then serve the query for the |
| 101 | + // directly invalid type from the cache. |
| 102 | + EXPECT_FALSE(validator.isValidPublicType(IndirectlyInvalidStruct)); |
| 103 | + EXPECT_FALSE(validator.isValidPublicType(InvalidStruct)); |
| 104 | + |
| 105 | + // We can serve repeated queries from the cache, too. |
| 106 | + EXPECT_FALSE(validator.isValidPublicType(IndirectlyInvalidStruct)); |
| 107 | +} |
| 108 | + |
| 109 | +TEST_F(PublicTypeValidatorTest, BasicHeapTypes) { |
| 110 | + PublicTypeValidator validator(FeatureSet::MVP); |
| 111 | + |
| 112 | + EXPECT_TRUE(validator.isValidPublicType(HeapTypes::any)); |
| 113 | + EXPECT_TRUE(validator.isValidPublicType(HeapTypes::eq)); |
| 114 | + EXPECT_TRUE(validator.isValidPublicType(HeapTypes::ext)); |
| 115 | + EXPECT_TRUE(validator.isValidPublicType(HeapTypes::func)); |
| 116 | + EXPECT_TRUE(validator.isValidPublicType(HeapTypes::none)); |
| 117 | +} |
| 118 | + |
| 119 | +TEST_F(PublicTypeValidatorTest, Types) { |
| 120 | + PublicTypeValidator validator(FeatureSet::MVP); |
| 121 | + |
| 122 | + EXPECT_TRUE(validator.isValidPublicType(Type::i32)); |
| 123 | + EXPECT_TRUE(validator.isValidPublicType(Type::i64)); |
| 124 | + EXPECT_TRUE(validator.isValidPublicType(Type::f32)); |
| 125 | + EXPECT_TRUE(validator.isValidPublicType(Type::f64)); |
| 126 | + EXPECT_TRUE(validator.isValidPublicType(Type::v128)); |
| 127 | + |
| 128 | + EXPECT_TRUE(validator.isValidPublicType(Type(HeapType::any, Nullable))); |
| 129 | + EXPECT_TRUE(validator.isValidPublicType(Type(EmptyStruct, Nullable))); |
| 130 | + EXPECT_FALSE(validator.isValidPublicType(Type(EmptyStruct, Nullable, Exact))); |
| 131 | + EXPECT_FALSE(validator.isValidPublicType(Type(InvalidStruct, Nullable))); |
| 132 | +} |
0 commit comments