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*,
5355 std::string_view filename,
5456 const ValidateOptions& options);
@@ -70,16 +72,28 @@ class SharedValidator {
7072
7173 Index GetLocalCount () const ;
7274
75+ // The canonical index is the lowest index, which represents
76+ // the same type as the type_index. The canonical index is
77+ // always less or equal than type_index.
78+ Index GetCanonicalTypeIndex (Index type_index);
79+
7380 Result EndModule ();
7481
82+ Result OnRecursiveGroup (Index first_type_index, Index type_count);
7583 Result OnFuncType (const Location&,
7684 Index param_count,
7785 const Type* param_types,
7886 Index result_count,
7987 const Type* result_types,
80- Index type_index);
81- Result OnStructType (const Location&, Index field_count, TypeMut* fields);
82- Result OnArrayType (const Location&, TypeMut field);
88+ Index type_index,
89+ SupertypesInfo* supertypes);
90+ Result OnStructType (const Location&,
91+ Index field_count,
92+ TypeMut* fields,
93+ SupertypesInfo* supertypes);
94+ Result OnArrayType (const Location&,
95+ TypeMut field,
96+ SupertypesInfo* supertypes);
8397
8498 Result OnFunction (const Location&, Var sig_var);
8599 Result OnTable (const Location&, Type elem_type, const Limits&, TableImportStatus import_status, TableInitExprStatus init_provided);
@@ -233,20 +247,6 @@ class SharedValidator {
233247 Result OnUnreachable (const Location&);
234248
235249 private:
236- struct StructType {
237- StructType () = default ;
238- StructType (const TypeMutVector& fields) : fields(fields) {}
239-
240- TypeMutVector fields;
241- };
242-
243- struct ArrayType {
244- ArrayType () = default ;
245- ArrayType (TypeMut field) : field(field) {}
246-
247- TypeMut field;
248- };
249-
250250 struct TableType {
251251 TableType () = default ;
252252 TableType (Type element, Limits limits) : element(element), limits(limits) {}
@@ -302,7 +302,11 @@ class SharedValidator {
302302 Type actual,
303303 Type expected,
304304 const char * desc);
305- Result CheckReferenceType (const Location&, Type type, const char * desc);
305+ Result CheckReferenceType (const Location&,
306+ Type type,
307+ Index end_index,
308+ const char * desc);
309+ Result CheckSupertypes (const Location&, SupertypesInfo* supertypes);
306310 Result CheckLimits (const Location&,
307311 const Limits&,
308312 uint64_t absolute_max,
@@ -347,6 +351,8 @@ class SharedValidator {
347351 void RestoreLocalRefs (Result result);
348352 void IgnoreLocalRefs ();
349353
354+ Index GetRecGroupEnd ();
355+
350356 ValidateOptions options_;
351357 Errors* errors_;
352358 std::string_view filename_;
@@ -355,10 +361,7 @@ class SharedValidator {
355361 Location expr_loc_ = Location(kInvalidOffset );
356362 bool in_init_expr_ = false ;
357363
358- Index num_types_ = 0 ;
359- std::map<Index, FuncType> func_types_;
360- std::map<Index, StructType> struct_types_;
361- std::map<Index, ArrayType> array_types_;
364+ TypeChecker::TypeFields type_fields_;
362365
363366 std::vector<FuncType> funcs_; // Includes imported and defined.
364367 std::vector<TableType> tables_; // Includes imported and defined.
@@ -369,6 +372,9 @@ class SharedValidator {
369372 Index starts_ = 0 ;
370373 Index num_imported_globals_ = 0 ;
371374 Index data_segments_ = 0 ;
375+ Index last_rec_type_end_ = 0 ;
376+ // Recursive type checks may enter to infinite loop for invalid values.
377+ Result type_validation_result_ = Result::Ok;
372378
373379 // Includes parameters, since this is only used for validating
374380 // local.{get,set,tee} instructions.
0 commit comments