Skip to content

Commit af318ba

Browse files
wenyonghChris Woodsermlerttrenner
authored
Implement Exception Handling for classic interpreter (#3096)
This PR adds the initial support for WASM exception handling: * Inside the classic interpreter only: * Initial handling of Tags * Initial handling of Exceptions based on W3C Exception Proposal * Import and Export of Exceptions and Tags * Add `cmake -DWAMR_BUILD_EXCE_HANDLING=1/0` option to enable/disable the feature, and by default it is disabled * Update the wamr-test-suites scripts to test the feature * Additional CI/CD changes to validate the exception spec proposal cases Refer to: #1884 587513f 8bebfe9 59bccdf Signed-off-by: Ricardo Aguilar <ricardoaguilar@siemens.com> Co-authored-by: Chris Woods <chris.woods@siemens.com> Co-authored-by: Rene Ermler <rene.ermler@siemens.com> Co-authored-by: Trenner Thomas <trenner.thomas@siemens.com>
1 parent 7e65f9a commit af318ba

File tree

16 files changed

+1620
-39
lines changed

16 files changed

+1620
-39
lines changed

build-scripts/config_common.cmake

+5
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ if (WAMR_BUILD_REF_TYPES EQUAL 1)
335335
else ()
336336
message (" Reference types disabled")
337337
endif ()
338+
if (WAMR_BUILD_EXCE_HANDLING EQUAL 1)
339+
add_definitions (-DWASM_ENABLE_EXCE_HANDLING=1)
340+
add_definitions (-DWASM_ENABLE_TAGS=1)
341+
message (" Exception Handling enabled")
342+
endif ()
338343
if (DEFINED WAMR_BH_VPRINTF)
339344
add_definitions (-DBH_VPRINTF=${WAMR_BH_VPRINTF})
340345
endif ()

core/config.h

+8
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,14 @@
457457
#define WASM_ENABLE_REF_TYPES 0
458458
#endif
459459

460+
#ifndef WASM_ENABLE_EXCE_HANDLING
461+
#define WASM_ENABLE_EXCE_HANDLING 0
462+
#endif
463+
464+
#ifndef WASM_ENABLE_TAGS
465+
#define WASM_ENABLE_TAGS 0
466+
#endif
467+
460468
#ifndef WASM_ENABLE_SGX_IPFS
461469
#define WASM_ENABLE_SGX_IPFS 0
462470
#endif

core/iwasm/interpreter/wasm.h

+67
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ extern "C" {
6565
#if WASM_ENABLE_BULK_MEMORY != 0
6666
#define SECTION_TYPE_DATACOUNT 12
6767
#endif
68+
#if WASM_ENABLE_TAGS != 0
69+
#define SECTION_TYPE_TAG 13
70+
#endif
6871

6972
#define SUB_SECTION_TYPE_MODULE 0
7073
#define SUB_SECTION_TYPE_FUNC 1
@@ -74,20 +77,34 @@ extern "C" {
7477
#define IMPORT_KIND_TABLE 1
7578
#define IMPORT_KIND_MEMORY 2
7679
#define IMPORT_KIND_GLOBAL 3
80+
#if WASM_ENABLE_TAGS != 0
81+
#define IMPORT_KIND_TAG 4
82+
#endif
7783

7884
#define EXPORT_KIND_FUNC 0
7985
#define EXPORT_KIND_TABLE 1
8086
#define EXPORT_KIND_MEMORY 2
8187
#define EXPORT_KIND_GLOBAL 3
88+
#if WASM_ENABLE_TAGS != 0
89+
#define EXPORT_KIND_TAG 4
90+
#endif
8291

8392
#define LABEL_TYPE_BLOCK 0
8493
#define LABEL_TYPE_LOOP 1
8594
#define LABEL_TYPE_IF 2
8695
#define LABEL_TYPE_FUNCTION 3
96+
#if WASM_ENABLE_EXCE_HANDLING != 0
97+
#define LABEL_TYPE_TRY 4
98+
#define LABEL_TYPE_CATCH 5
99+
#define LABEL_TYPE_CATCH_ALL 6
100+
#endif
87101

88102
typedef struct WASMModule WASMModule;
89103
typedef struct WASMFunction WASMFunction;
90104
typedef struct WASMGlobal WASMGlobal;
105+
#if WASM_ENABLE_TAGS != 0
106+
typedef struct WASMTag WASMTag;
107+
#endif
91108

92109
typedef union V128 {
93110
int8 i8x16[16];
@@ -201,6 +218,24 @@ typedef struct WASMFunctionImport {
201218
bool call_conv_wasm_c_api;
202219
} WASMFunctionImport;
203220

221+
#if WASM_ENABLE_TAGS != 0
222+
typedef struct WASMTagImport {
223+
char *module_name;
224+
char *field_name;
225+
uint8 attribute; /* the type of the tag (numerical) */
226+
uint32 type; /* the type of the catch function (numerical)*/
227+
WASMType *tag_type;
228+
void *tag_ptr_linked;
229+
230+
#if WASM_ENABLE_MULTI_MODULE != 0
231+
/* imported tag pointer after linked */
232+
WASMModule *import_module;
233+
WASMTag *import_tag_linked;
234+
uint32 import_tag_index_linked;
235+
#endif
236+
} WASMTagImport;
237+
#endif
238+
204239
typedef struct WASMGlobalImport {
205240
char *module_name;
206241
char *field_name;
@@ -227,6 +262,9 @@ typedef struct WASMImport {
227262
WASMFunctionImport function;
228263
WASMTableImport table;
229264
WASMMemoryImport memory;
265+
#if WASM_ENABLE_TAGS != 0
266+
WASMTagImport tag;
267+
#endif
230268
WASMGlobalImport global;
231269
struct {
232270
char *module_name;
@@ -265,6 +303,10 @@ struct WASMFunction {
265303
uint32 const_cell_num;
266304
#endif
267305

306+
#if WASM_ENABLE_EXCE_HANDLING != 0
307+
uint32 exception_handler_count;
308+
#endif
309+
268310
#if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
269311
|| WASM_ENABLE_WAMR_COMPILER != 0
270312
/* Whether function has opcode memory.grow */
@@ -294,6 +336,14 @@ struct WASMFunction {
294336
#endif
295337
};
296338

339+
#if WASM_ENABLE_TAGS != 0
340+
struct WASMTag {
341+
uint8 attribute; /* the attribute property of the tag (expected to be 0) */
342+
uint32 type; /* the type of the tag (expected valid inden in type table) */
343+
WASMType *tag_type;
344+
};
345+
#endif
346+
297347
struct WASMGlobal {
298348
uint8 type;
299349
bool is_mutable;
@@ -420,6 +470,9 @@ struct WASMModule {
420470
uint32 function_count;
421471
uint32 table_count;
422472
uint32 memory_count;
473+
#if WASM_ENABLE_TAGS != 0
474+
uint32 tag_count;
475+
#endif
423476
uint32 global_count;
424477
uint32 export_count;
425478
uint32 table_seg_count;
@@ -433,18 +486,27 @@ struct WASMModule {
433486
uint32 import_function_count;
434487
uint32 import_table_count;
435488
uint32 import_memory_count;
489+
#if WASM_ENABLE_TAGS != 0
490+
uint32 import_tag_count;
491+
#endif
436492
uint32 import_global_count;
437493

438494
WASMImport *import_functions;
439495
WASMImport *import_tables;
440496
WASMImport *import_memories;
497+
#if WASM_ENABLE_TAGS != 0
498+
WASMImport *import_tags;
499+
#endif
441500
WASMImport *import_globals;
442501

443502
WASMType **types;
444503
WASMImport *imports;
445504
WASMFunction **functions;
446505
WASMTable *tables;
447506
WASMMemory *memories;
507+
#if WASM_ENABLE_TAGS != 0
508+
WASMTag **tags;
509+
#endif
448510
WASMGlobal *globals;
449511
WASMExport *exports;
450512
WASMTableSeg *table_segments;
@@ -628,6 +690,11 @@ typedef struct WASMBranchBlock {
628690
uint8 *target_addr;
629691
uint32 *frame_sp;
630692
uint32 cell_num;
693+
#if WASM_ENABLE_EXCE_HANDLING != 0
694+
/* in exception handling, label_type needs to be stored to lookup exception
695+
* handlers */
696+
uint8 label_type;
697+
#endif
631698
} WASMBranchBlock;
632699

633700
/**

core/iwasm/interpreter/wasm_interp.h

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ typedef struct WASMInterpFrame {
3434
uint64 time_started;
3535
#endif
3636

37+
#if WASM_ENABLE_EXCE_HANDLING != 0
38+
/* set to true if the callee returns an exception rather than
39+
* result values on the stack
40+
*/
41+
bool exception_raised;
42+
uint32 tag_index;
43+
#endif
44+
3745
#if WASM_ENABLE_FAST_INTERP != 0
3846
/* Return offset of the first return value of current frame,
3947
the callee will put return values here continuously */

0 commit comments

Comments
 (0)