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 OnRecursiveType (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);
@@ -230,20 +244,6 @@ class SharedValidator {
230244 Result OnUnreachable (const Location&);
231245
232246 private:
233- struct StructType {
234- StructType () = default ;
235- StructType (const TypeMutVector& fields) : fields(fields) {}
236-
237- TypeMutVector fields;
238- };
239-
240- struct ArrayType {
241- ArrayType () = default ;
242- ArrayType (TypeMut field) : field(field) {}
243-
244- TypeMut field;
245- };
246-
247247 struct TableType {
248248 TableType () = default ;
249249 TableType (Type element, Limits limits) : element(element), limits(limits) {}
@@ -299,7 +299,11 @@ class SharedValidator {
299299 Type actual,
300300 Type expected,
301301 const char * desc);
302- Result CheckReferenceType (const Location&, Type type, const char * desc);
302+ Result CheckReferenceType (const Location&,
303+ Type type,
304+ Index end_index,
305+ const char * desc);
306+ Result CheckSupertypes (const Location&, SupertypesInfo* supertypes);
303307 Result CheckLimits (const Location&,
304308 const Limits&,
305309 uint64_t absolute_max,
@@ -344,17 +348,16 @@ class SharedValidator {
344348 void RestoreLocalRefs (Result result);
345349 void IgnoreLocalRefs ();
346350
351+ Index GetEndIndex ();
352+
347353 ValidateOptions options_;
348354 Errors* errors_;
349355 TypeChecker typechecker_; // TODO: Move into SharedValidator.
350356 // Cached for access by OnTypecheckerError.
351357 Location expr_loc_ = Location(kInvalidOffset );
352358 bool in_init_expr_ = false ;
353359
354- Index num_types_ = 0 ;
355- std::map<Index, FuncType> func_types_;
356- std::map<Index, StructType> struct_types_;
357- std::map<Index, ArrayType> array_types_;
360+ TypeChecker::TypeFields type_fields_;
358361
359362 std::vector<FuncType> funcs_; // Includes imported and defined.
360363 std::vector<TableType> tables_; // Includes imported and defined.
@@ -365,6 +368,9 @@ class SharedValidator {
365368 Index starts_ = 0 ;
366369 Index num_imported_globals_ = 0 ;
367370 Index data_segments_ = 0 ;
371+ Index last_rec_type_end_ = 0 ;
372+ // Recursive type checks may enter to infinite loop for invalid values.
373+ Result type_validation_result_ = Result::Ok;
368374
369375 // Includes parameters, since this is only used for validating
370376 // local.{get,set,tee} instructions.
0 commit comments