-
Notifications
You must be signed in to change notification settings - Fork 791
Expand file tree
/
Copy pathaot_stack_frame_test.cc
More file actions
231 lines (179 loc) · 6.77 KB
/
aot_stack_frame_test.cc
File metadata and controls
231 lines (179 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "gtest/gtest.h"
#include "bh_platform.h"
#include "wasm_runtime_common.h"
#include "aot_runtime.h"
#include "test_helper.h"
#ifndef __aligned
#define __aligned(n)
#endif
#include "wasm-apps/test_aot.h"
typedef struct MyAOTFrame {
uintptr_t func_index;
/* Instruction pointer: offset to the bytecode array */
uintptr_t ip_offset;
/* Operand stack top pointer of the current frame */
uint32 *sp;
#if WASM_ENABLE_GC != 0
/* Frame ref flags (GC only) */
uint8 *frame_ref;
#endif
uint32 lp[1];
} MyAOTFrame;
class AOTStackFrameTest : public testing::Test
{
protected:
virtual void SetUp()
{
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
ASSERT_EQ(wasm_runtime_full_init(&init_args), true);
}
virtual void TearDown()
{
DestroyFrames();
wasm_runtime_destroy();
}
public:
static void DestroyFrames()
{
if (my_frames) {
for (uint32 i = 0; i < my_frame_num; i++) {
if (my_frames[i])
wasm_runtime_free(my_frames[i]);
}
wasm_runtime_free(my_frames);
my_frames = NULL;
my_frame_num = 0;
}
}
public:
RuntimeInitArgs init_args;
wasm_module_t module = NULL;
wasm_module_inst_t module_inst = NULL;
wasm_function_inst_t func_inst = NULL;
wasm_exec_env_t exec_env = NULL;
static MyAOTFrame **my_frames;
static uint32 my_frame_num;
char error_buf[128];
char global_heap_buf[512 * 1024];
unsigned char test_aot_buf[16 * 1024];
unsigned argv[8];
};
MyAOTFrame **AOTStackFrameTest::my_frames = NULL;
uint32 AOTStackFrameTest::my_frame_num = 0;
extern "C" {
typedef void (*stack_frame_callback_t)(struct WASMExecEnv *exec_env);
void
aot_set_stack_frame_callback(stack_frame_callback_t callback);
void
aot_stack_frame_cb(struct WASMExecEnv *exec_env)
{
AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
AOTModule *module = (AOTModule *)module_inst->module;
AOTFrame *frame = (AOTFrame *)exec_env->cur_frame;
MyAOTFrame *my_frame, **my_frames;
uint32 all_cell_num, max_local_cell_num, max_stack_cell_num;
uint32 frame_size_old, frame_size, i, frame_num = 0, aot_func_idx;
AOTStackFrameTest::DestroyFrames();
while (frame) {
frame_num++;
frame = frame->prev_frame;
}
my_frames =
(MyAOTFrame **)wasm_runtime_malloc(sizeof(MyAOTFrame *) * frame_num);
bh_assert(my_frames);
frame = (AOTFrame *)exec_env->cur_frame;
for (i = 0; i < frame_num; i++) {
aot_func_idx = frame->func_index;
max_local_cell_num = module->max_local_cell_nums[aot_func_idx];
max_stack_cell_num = module->max_stack_cell_nums[aot_func_idx];
all_cell_num = max_local_cell_num + max_stack_cell_num;
frame_size_old = (uint32)offsetof(AOTFrame, lp) + all_cell_num * 4;
frame_size = (uint32)offsetof(MyAOTFrame, lp) + all_cell_num * 4;
my_frames[frame_num - 1 - i] = my_frame =
(MyAOTFrame *)wasm_runtime_malloc(frame_size);
my_frame->func_index = aot_func_idx;
my_frame->ip_offset = frame->ip_offset;
my_frame->sp = my_frame->lp + (frame->sp - frame->lp);
#if WASM_ENABLE_GC != 0
my_frame->frame_ref =
(uint8 *)my_frame->lp + (frame->frame_ref - (uint8 *)frame->lp);
#endif
bh_memcpy_s(my_frame->lp, all_cell_num * 4, frame->lp,
all_cell_num * 4);
frame = frame->prev_frame;
}
AOTStackFrameTest::my_frames = my_frames;
AOTStackFrameTest::my_frame_num = frame_num;
}
}
TEST_F(AOTStackFrameTest, test1)
{
MyAOTFrame *frame, **frames;
uint32 frame_num;
aot_set_stack_frame_callback(aot_stack_frame_cb);
bh_memcpy_s(test_aot_buf, sizeof(test_aot_buf), test_aot, sizeof(test_aot));
module = wasm_runtime_load(test_aot_buf, sizeof(test_aot), error_buf,
sizeof(error_buf));
ASSERT_TRUE(module != NULL);
module_inst = wasm_runtime_instantiate(module, 16384, 0, error_buf,
sizeof(error_buf));
ASSERT_TRUE(module_inst != NULL);
exec_env = wasm_runtime_create_exec_env(module_inst, 8 * 1024);
ASSERT_TRUE(exec_env != NULL);
func_inst = wasm_runtime_lookup_function(module_inst, "test2");
ASSERT_TRUE(func_inst != NULL);
argv[0] = 1234;
argv[1] = 5678;
wasm_runtime_call_wasm(exec_env, func_inst, 2, argv);
ASSERT_TRUE(wasm_runtime_get_exception(module_inst));
frames = AOTStackFrameTest::my_frames;
frame_num = AOTStackFrameTest::my_frame_num;
ASSERT_TRUE(frames != NULL);
ASSERT_TRUE(frame_num == 1);
ASSERT_TRUE(frames[0]->lp[0] == 1234);
ASSERT_TRUE(frames[0]->lp[1] == 5678);
ASSERT_TRUE(frames[0]->lp[2] == 0x11223344);
ASSERT_TRUE(*(uint64 *)(frames[0]->lp + 3) == 0x12345678ABCDEF99LL);
ASSERT_TRUE(*(float *)(frames[0]->lp + 5) == 5566.7788f);
ASSERT_TRUE(*(double *)(frames[0]->lp + 6) == 99887766.55443322);
}
TEST_F(AOTStackFrameTest, test2)
{
MyAOTFrame *frame, **frames;
uint32 frame_num;
aot_set_stack_frame_callback(aot_stack_frame_cb);
bh_memcpy_s(test_aot_buf, sizeof(test_aot_buf), test_aot, sizeof(test_aot));
module = wasm_runtime_load(test_aot_buf, sizeof(test_aot), error_buf,
sizeof(error_buf));
ASSERT_TRUE(module != NULL);
module_inst = wasm_runtime_instantiate(module, 16384, 0, error_buf,
sizeof(error_buf));
ASSERT_TRUE(module_inst != NULL);
exec_env = wasm_runtime_create_exec_env(module_inst, 8 * 1024);
ASSERT_TRUE(exec_env != NULL);
func_inst = wasm_runtime_lookup_function(module_inst, "test3");
ASSERT_TRUE(func_inst != NULL);
argv[0] = 1234;
argv[1] = 5678;
wasm_runtime_call_wasm(exec_env, func_inst, 2, argv);
ASSERT_TRUE(wasm_runtime_get_exception(module_inst));
frames = AOTStackFrameTest::my_frames;
frame_num = AOTStackFrameTest::my_frame_num;
ASSERT_TRUE(frames != NULL);
ASSERT_TRUE(frame_num == 2);
// 5(i32) + 1(i64) local variables, occupied 7 * 4 bytes
ASSERT_TRUE(frames[0]->sp - frames[0]->lp == 7);
// offset of ip from module load address
ASSERT_TRUE(frames[0]->ip_offset == 163);
ASSERT_TRUE(frames[0]->lp[0] == 1234);
ASSERT_TRUE(frames[0]->lp[1] == 5678);
ASSERT_TRUE(frames[0]->lp[2] == 0x11223344);
ASSERT_TRUE(*(uint64 *)(frames[0]->lp + 3) == 0x12345678ABCDEF99LL);
}