2929#include " wabt/opcode.h"
3030#include " wabt/type-checker.h"
3131
32- #include " wabt/binary-reader.h" // For TypeMut.
33-
3432namespace wabt {
3533
3634struct ValidateOptions {
@@ -48,7 +46,11 @@ enum class TableImportStatus {
4846class SharedValidator {
4947 public:
5048 WABT_DISALLOW_COPY_AND_ASSIGN (SharedValidator);
49+ using TypeEntry = TypeChecker::TypeEntry;
5150 using FuncType = TypeChecker::FuncType;
51+ using StructType = TypeChecker::StructType;
52+ using ArrayType = TypeChecker::ArrayType;
53+ using RecGroup = TypeChecker::RecGroup;
5254 SharedValidator (Errors*, const ValidateOptions& options);
5355
5456 // TODO: Move into SharedValidator?
@@ -68,16 +70,28 @@ class SharedValidator {
6870
6971 Index GetLocalCount () const ;
7072
73+ // The canonical index of a type is the index of the first type,
74+ // which is equal to the original type. The canonical index is
75+ // always less or equal than type_index.
76+ Index GetCanonicalTypeIndex (Index type_index);
77+
7178 Result EndModule ();
7279
80+ Result OnRecursiveGroup (Index first_type_index, Index type_count);
7381 Result OnFuncType (const Location&,
7482 Index param_count,
7583 const Type* param_types,
7684 Index result_count,
7785 const Type* result_types,
78- Index type_index);
79- Result OnStructType (const Location&, Index field_count, TypeMut* fields);
80- Result OnArrayType (const Location&, TypeMut field);
86+ Index type_index,
87+ SupertypesInfo* supertypes);
88+ Result OnStructType (const Location&,
89+ Index field_count,
90+ TypeMut* fields,
91+ SupertypesInfo* supertypes);
92+ Result OnArrayType (const Location&,
93+ TypeMut field,
94+ SupertypesInfo* supertypes);
8195
8296 Result OnFunction (const Location&, Var sig_var);
8397 Result OnTable (const Location&, Type elem_type, const Limits&, TableImportStatus import_status, TableInitExprStatus init_provided);
@@ -231,20 +245,6 @@ class SharedValidator {
231245 Result OnUnreachable (const Location&);
232246
233247 private:
234- struct StructType {
235- StructType () = default ;
236- StructType (const TypeMutVector& fields) : fields(fields) {}
237-
238- TypeMutVector fields;
239- };
240-
241- struct ArrayType {
242- ArrayType () = default ;
243- ArrayType (TypeMut field) : field(field) {}
244-
245- TypeMut field;
246- };
247-
248248 struct TableType {
249249 TableType () = default ;
250250 TableType (Type element, Limits limits) : element(element), limits(limits) {}
@@ -300,7 +300,11 @@ class SharedValidator {
300300 Type actual,
301301 Type expected,
302302 const char * desc);
303- Result CheckReferenceType (const Location&, Type type, const char * desc);
303+ Result CheckReferenceType (const Location&,
304+ Type type,
305+ Index end_index,
306+ const char * desc);
307+ Result CheckSupertypes (const Location&, SupertypesInfo* supertypes);
304308 Result CheckLimits (const Location&,
305309 const Limits&,
306310 uint64_t absolute_max,
@@ -345,17 +349,16 @@ class SharedValidator {
345349 void RestoreLocalRefs (Result result);
346350 void IgnoreLocalRefs ();
347351
352+ Index GetEndIndex ();
353+
348354 ValidateOptions options_;
349355 Errors* errors_;
350356 TypeChecker typechecker_; // TODO: Move into SharedValidator.
351357 // Cached for access by OnTypecheckerError.
352358 Location expr_loc_ = Location(kInvalidOffset );
353359 bool in_init_expr_ = false ;
354360
355- Index num_types_ = 0 ;
356- std::map<Index, FuncType> func_types_;
357- std::map<Index, StructType> struct_types_;
358- std::map<Index, ArrayType> array_types_;
361+ TypeChecker::TypeFields type_fields_;
359362
360363 std::vector<FuncType> funcs_; // Includes imported and defined.
361364 std::vector<TableType> tables_; // Includes imported and defined.
@@ -366,6 +369,9 @@ class SharedValidator {
366369 Index starts_ = 0 ;
367370 Index num_imported_globals_ = 0 ;
368371 Index data_segments_ = 0 ;
372+ Index last_rec_type_end_ = 0 ;
373+ // Recursive type checks may enter to infinite loop for invalid values.
374+ Result type_validation_result_ = Result::Ok;
369375
370376 // Includes parameters, since this is only used for validating
371377 // local.{get,set,tee} instructions.
0 commit comments