Skip to content

Commit d1a3c84

Browse files
author
Zoltan Herczeg
committed
Implement full reference support for call_ref, ref_null and variables
Support named references for globals, locals, tables, elems Support named references for call_ref, ref_null
1 parent 96dfd60 commit d1a3c84

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1480
-506
lines changed

include/wabt/binary-reader-logging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
181181
Result OnCatchExpr(Index tag_index) override;
182182
Result OnCatchAllExpr() override;
183183
Result OnCallIndirectExpr(Index sig_index, Index table_index) override;
184-
Result OnCallRefExpr() override;
184+
Result OnCallRefExpr(Type sig_type) override;
185185
Result OnCompareExpr(Opcode opcode) override;
186186
Result OnConvertExpr(Opcode opcode) override;
187187
Result OnDelegateExpr(Index depth) override;

include/wabt/binary-reader-nop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class BinaryReaderNop : public BinaryReaderDelegate {
250250
Result OnCallIndirectExpr(Index sig_index, Index table_index) override {
251251
return Result::Ok;
252252
}
253-
Result OnCallRefExpr() override { return Result::Ok; }
253+
Result OnCallRefExpr(Type sig_type) override { return Result::Ok; }
254254
Result OnCatchExpr(Index tag_index) override { return Result::Ok; }
255255
Result OnCatchAllExpr() override { return Result::Ok; }
256256
Result OnCompareExpr(Opcode opcode) override { return Result::Ok; }

include/wabt/binary-reader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class BinaryReaderDelegate {
255255
Index default_target_depth) = 0;
256256
virtual Result OnCallExpr(Index func_index) = 0;
257257
virtual Result OnCallIndirectExpr(Index sig_index, Index table_index) = 0;
258-
virtual Result OnCallRefExpr() = 0;
258+
virtual Result OnCallRefExpr(Type sig_type) = 0;
259259
virtual Result OnCatchExpr(Index tag_index) = 0;
260260
virtual Result OnCatchAllExpr() = 0;
261261
virtual Result OnCompareExpr(Opcode opcode) = 0;

include/wabt/ir.h

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,41 @@ namespace wabt {
3636

3737
struct Module;
3838

39-
enum class VarType {
39+
enum class VarType : uint16_t {
4040
Index,
4141
Name,
4242
};
4343

4444
struct Var {
45+
// Var can represent variables or types.
46+
47+
// Represent a variable:
48+
// has_opt_type() is false
49+
// Only used by wast-parser
50+
51+
// Represent a type:
52+
// has_opt_type() is true, is_index() is true
53+
// type can be get by to_type()
54+
// Binary reader only constructs this variant
55+
56+
// Represent both a variable and a type:
57+
// has_opt_type() is true, is_name() is true
58+
// A reference, which index is unknown
59+
// Only used by wast-parser
60+
4561
explicit Var();
4662
explicit Var(Index index, const Location& loc);
4763
explicit Var(std::string_view name, const Location& loc);
64+
explicit Var(Type type, const Location& loc);
4865
Var(Var&&);
4966
Var(const Var&);
5067
Var& operator=(const Var&);
5168
Var& operator=(Var&&);
5269
~Var();
5370

54-
VarType type() const { return type_; }
5571
bool is_index() const { return type_ == VarType::Index; }
5672
bool is_name() const { return type_ == VarType::Name; }
73+
bool has_opt_type() const { return opt_type_ < 0; }
5774

5875
Index index() const {
5976
assert(is_index());
@@ -63,17 +80,25 @@ struct Var {
6380
assert(is_name());
6481
return name_;
6582
}
83+
Type::Enum opt_type() const {
84+
assert(has_opt_type());
85+
return static_cast<Type::Enum>(opt_type_);
86+
}
6687

6788
void set_index(Index);
6889
void set_name(std::string&&);
6990
void set_name(std::string_view);
91+
void set_opt_type(Type::Enum);
92+
Type to_type() const;
7093

7194
Location loc;
7295

7396
private:
7497
void Destroy();
7598

7699
VarType type_;
100+
// Can be set to Type::Enum types, Type::Any represent no optional type.
101+
int16_t opt_type_;
77102
union {
78103
Index index_;
79104
std::string name_;
@@ -155,6 +180,7 @@ struct Const {
155180
}
156181
void set_funcref() { From<uintptr_t>(Type::FuncRef, 0); }
157182
void set_externref(uintptr_t x) { From(Type::ExternRef, x); }
183+
void set_extern(uintptr_t x) { From(Type(Type::ExternRef, Type::ReferenceNonNull), x); }
158184
void set_null(Type type) { From<uintptr_t>(type, kRefNullBits); }
159185

160186
bool is_expected_nan(int lane = 0) const {
@@ -537,10 +563,10 @@ using MemoryCopyExpr = MemoryBinaryExpr<ExprType::MemoryCopy>;
537563
template <ExprType TypeEnum>
538564
class RefTypeExpr : public ExprMixin<TypeEnum> {
539565
public:
540-
RefTypeExpr(Type type, const Location& loc = Location())
566+
RefTypeExpr(Var type, const Location& loc = Location())
541567
: ExprMixin<TypeEnum>(loc), type(type) {}
542568

543-
Type type;
569+
Var type;
544570
};
545571

546572
using RefNullExpr = RefTypeExpr<ExprType::RefNull>;
@@ -662,8 +688,8 @@ using MemoryInitExpr = MemoryVarExpr<ExprType::MemoryInit>;
662688

663689
class SelectExpr : public ExprMixin<ExprType::Select> {
664690
public:
665-
SelectExpr(TypeVector type, const Location& loc = Location())
666-
: ExprMixin<ExprType::Select>(loc), result_type(type) {}
691+
SelectExpr(const Location& loc = Location())
692+
: ExprMixin<ExprType::Select>(loc) {}
667693
TypeVector result_type;
668694
};
669695

@@ -727,9 +753,7 @@ class CallRefExpr : public ExprMixin<ExprType::CallRef> {
727753
explicit CallRefExpr(const Location& loc = Location())
728754
: ExprMixin<ExprType::CallRef>(loc) {}
729755

730-
// This field is setup only during Validate phase,
731-
// so keep that in mind when you use it.
732-
Var function_type_index;
756+
Var sig_type;
733757
};
734758

735759
template <ExprType TypeEnum>

include/wabt/leb128.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void WriteS32Leb128(Stream* stream, T value, const char* desc) {
6363
size_t ReadU32Leb128(const uint8_t* p, const uint8_t* end, uint32_t* out_value);
6464
size_t ReadU64Leb128(const uint8_t* p, const uint8_t* end, uint64_t* out_value);
6565
size_t ReadS32Leb128(const uint8_t* p, const uint8_t* end, uint32_t* out_value);
66+
size_t ReadS33Leb128(const uint8_t* p, const uint8_t* end, uint64_t* out_value);
6667
size_t ReadS64Leb128(const uint8_t* p, const uint8_t* end, uint64_t* out_value);
6768

6869
} // namespace wabt

include/wabt/shared-validator.h

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct ValidateOptions {
4343
class SharedValidator {
4444
public:
4545
WABT_DISALLOW_COPY_AND_ASSIGN(SharedValidator);
46+
using FuncType = TypeChecker::FuncType;
4647
SharedValidator(Errors*, const ValidateOptions& options);
4748

4849
// TODO: Move into SharedValidator?
@@ -141,7 +142,7 @@ class SharedValidator {
141142
Result EndBrTable(const Location&);
142143
Result OnCall(const Location&, Var func_var);
143144
Result OnCallIndirect(const Location&, Var sig_var, Var table_var);
144-
Result OnCallRef(const Location&, Index* function_type_index);
145+
Result OnCallRef(const Location&, Var function_type_var);
145146
Result OnCatch(const Location&, Var tag_var, bool is_catch_all);
146147
Result OnCompare(const Location&, Opcode);
147148
Result OnConst(const Location&, Type);
@@ -178,7 +179,7 @@ class SharedValidator {
178179
Result OnNop(const Location&);
179180
Result OnRefFunc(const Location&, Var func_var);
180181
Result OnRefIsNull(const Location&);
181-
Result OnRefNull(const Location&, Type type);
182+
Result OnRefNull(const Location&, Var func_type_var);
182183
Result OnRethrow(const Location&, Var depth);
183184
Result OnReturnCall(const Location&, Var func_var);
184185
Result OnReturnCallIndirect(const Location&, Var sig_var, Var table_var);
@@ -221,18 +222,6 @@ class SharedValidator {
221222
Result OnUnreachable(const Location&);
222223

223224
private:
224-
struct FuncType {
225-
FuncType() = default;
226-
FuncType(const TypeVector& params,
227-
const TypeVector& results,
228-
Index type_index)
229-
: params(params), results(results), type_index(type_index) {}
230-
231-
TypeVector params;
232-
TypeVector results;
233-
Index type_index;
234-
};
235-
236225
struct StructType {
237226
StructType() = default;
238227
StructType(const TypeMutVector& fields) : fields(fields) {}
@@ -289,6 +278,25 @@ class SharedValidator {
289278
Index end;
290279
};
291280

281+
struct LocalReferenceMap {
282+
Type type;
283+
Index bit_index;
284+
};
285+
286+
struct RecursionDetector {
287+
RecursionDetector(SharedValidator *validator)
288+
: validator(validator) {}
289+
290+
SharedValidator *validator;
291+
bool recursion_found = false;
292+
Index iteration = 0;
293+
std::map<Index, Index> processed_func_types;
294+
std::vector<Index> visited_func_types;
295+
296+
Result CheckRecursion(Type type,
297+
const char* desc);
298+
};
299+
292300
bool ValidInitOpcode(Opcode opcode) const;
293301
Result CheckInstr(Opcode opcode, const Location& loc);
294302
Result CheckType(const Location&,
@@ -336,6 +344,10 @@ class SharedValidator {
336344

337345
TypeVector ToTypeVector(Index count, const Type* types);
338346

347+
void SaveLocalRefs();
348+
void RestoreLocalRefs(Result result);
349+
void IgnoreLocalRefs();
350+
339351
ValidateOptions options_;
340352
Errors* errors_;
341353
TypeChecker typechecker_; // TODO: Move into SharedValidator.
@@ -361,6 +373,8 @@ class SharedValidator {
361373
// Includes parameters, since this is only used for validating
362374
// local.{get,set,tee} instructions.
363375
std::vector<LocalDecl> locals_;
376+
std::map<Index, LocalReferenceMap> local_refs_map_;
377+
std::vector<bool> local_ref_is_set_;
364378

365379
std::set<std::string> export_names_; // Used to check for duplicates.
366380
std::set<Index> declared_funcs_; // TODO: optimize?

include/wabt/token.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ WABT_TOKEN(Module, "module")
5555
WABT_TOKEN(Mut, "mut")
5656
WABT_TOKEN(NanArithmetic, "nan:arithmetic")
5757
WABT_TOKEN(NanCanonical, "nan:canonical")
58+
WABT_TOKEN(Null, "null")
5859
WABT_TOKEN(Offset, "offset")
5960
WABT_TOKEN(Output, "output")
6061
WABT_TOKEN(PageSize, "pagesize")

include/wabt/type-checker.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <functional>
2121
#include <type_traits>
2222
#include <vector>
23+
#include <map>
2324

2425
#include "wabt/common.h"
2526
#include "wabt/feature.h"
@@ -31,6 +32,18 @@ class TypeChecker {
3132
public:
3233
using ErrorCallback = std::function<void(const char* msg)>;
3334

35+
struct FuncType {
36+
FuncType() = default;
37+
FuncType(const TypeVector& params,
38+
const TypeVector& results,
39+
Index type_index)
40+
: params(params), results(results), type_index(type_index) {}
41+
42+
TypeVector params;
43+
TypeVector results;
44+
Index type_index;
45+
};
46+
3447
struct Label {
3548
Label(LabelType,
3649
const TypeVector& param_types,
@@ -46,9 +59,11 @@ class TypeChecker {
4659
TypeVector result_types;
4760
size_t type_stack_limit;
4861
bool unreachable;
62+
std::vector<bool> local_ref_is_set_;
4963
};
5064

51-
explicit TypeChecker(const Features& features) : features_(features) {}
65+
explicit TypeChecker(const Features& features, std::map<Index, FuncType>& func_types)
66+
: features_(features), func_types_(func_types) {}
5267

5368
void set_error_callback(const ErrorCallback& error_callback) {
5469
error_callback_ = error_callback;
@@ -80,7 +95,7 @@ class TypeChecker {
8095
Result OnCallIndirect(const TypeVector& param_types,
8196
const TypeVector& result_types,
8297
const Limits& table_limits);
83-
Result OnIndexedFuncRef(Index* out_index);
98+
Result OnCallRef(Type);
8499
Result OnReturnCall(const TypeVector& param_types,
85100
const TypeVector& result_types);
86101
Result OnReturnCallIndirect(const TypeVector& param_types,
@@ -141,7 +156,7 @@ class TypeChecker {
141156
Result BeginInitExpr(Type type);
142157
Result EndInitExpr();
143158

144-
static Result CheckType(Type actual, Type expected);
159+
Result CheckType(Type actual, Type expected);
145160

146161
private:
147162
void WABT_PRINTF_FORMAT(2, 3) PrintError(const char* fmt, ...);
@@ -210,6 +225,7 @@ class TypeChecker {
210225
// to represent "any".
211226
TypeVector* br_table_sig_ = nullptr;
212227
Features features_;
228+
std::map<Index, FuncType>& func_types_;
213229
};
214230

215231
} // namespace wabt

0 commit comments

Comments
 (0)