summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/language-bindings/python/wasm-c-api/tests/test_advanced.py
blob: 2e1c285eabc64f20d077c6f4205613b3ceb95de5 (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
#
# Copyright (C) 2019 Intel Corporation.  All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
# pylint: disable=missing-module-docstring

import ctypes as c
import math
import unittest

import wamr.wasmcapi.ffi as ffi


# It is a module likes:
# (module
#   (import "mod" "g0" (global i32))
#   (import "mod" "f0" (func (param f32) (result f64)))
#
#   (func (export "f1") (param i32 i64))
#   (global (export "g1") (mut f32) (f32.const 3.14))
#   (memory (export "m1") 1 2)
#   (table (export "t1") 1 funcref)
#
#   (func (export "f2") (unreachable))
# )
MODULE_BINARY = (
    b"\x00asm\x01\x00\x00\x00\x01\x0e\x03`\x01}\x01|`\x02\x7f~\x00`\x00"
    b"\x00\x02\x14\x02\x03mod\x02g0\x03\x7f\x00\x03mod\x02f0\x00\x00\x03\x03"
    b"\x02\x01\x02\x04\x04\x01p\x00\x01\x05\x04\x01\x01\x01\x02\x06\t\x01}\x01C"
    b"\xc3\xf5H@\x0b\x07\x1a\x05\x02f1\x00\x01\x02g1\x03\x01\x02m1\x02\x00\x02t1"
    b"\x01\x00\x02f2\x00\x02\n\x08\x02\x02\x00\x0b\x03\x00\x00\x0b"
)

# False -> True when testing with a library enabling WAMR_BUILD_DUMP_CALL_STACK flag
TEST_WITH_WAMR_BUILD_DUMP_CALL_STACK = False


@ffi.wasm_func_cb_decl
def callback(args, results):
    args = ffi.dereference(args)
    results = ffi.dereference(results)

    arg_v = args.data[0]

    result_v = ffi.wasm_f64_val(arg_v.of.f32 * 2.0)
    ffi.wasm_val_copy(results.data[0], result_v)
    results.num_elems = 1

    print(f"\nIn callback: {arg_v} --> {result_v}\n")


@ffi.wasm_func_with_env_cb_decl
def callback_with_env(env, args, results):
    # pylint: disable=unused-argument
    print("summer")


class AdvancedTestSuite(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("Initializing...")
        cls._wasm_engine = ffi.wasm_engine_new()
        cls._wasm_store = ffi.wasm_store_new(cls._wasm_engine)

    def assertIsNullPointer(self, pointer):
        # pylint: disable=invalid-name
        if not ffi.is_null_pointer(pointer):
            self.fail("not a non-null pointer")

    def assertIsNotNullPointer(self, pointer):
        # pylint: disable=invalid-name
        if ffi.is_null_pointer(pointer):
            self.fail("not a non-null pointer")

    def load_binary(self, binary_string):
        print("Load binary...")
        binary = ffi.load_module_file(binary_string)
        binary = c.pointer(binary)
        self.assertIsNotNullPointer(binary)
        return binary

    def compile(self, binary):
        print("Compile...")
        module = ffi.wasm_module_new(self._wasm_store, binary)
        self.assertIsNotNullPointer(module)
        return module

    def prepare_imports_local(self):
        print("Prepare imports...")
        func_type = ffi.wasm_functype_new_1_1(
            ffi.wasm_valtype_new(ffi.WASM_F32),
            ffi.wasm_valtype_new(ffi.WASM_F64),
        )
        func = ffi.wasm_func_new(self._wasm_store, func_type, callback)
        self.assertIsNotNullPointer(func)
        ffi.wasm_functype_delete(func_type)

        glbl_type = ffi.wasm_globaltype_new(ffi.wasm_valtype_new(ffi.WASM_I32), True)
        init = ffi.wasm_i32_val(1024)
        glbl = ffi.wasm_global_new(self._wasm_store, glbl_type, init)
        self.assertIsNotNullPointer(glbl)
        ffi.wasm_globaltype_delete(glbl_type)

        imports = ffi.wasm_extern_vec_t()
        data = ffi.list_to_carray(
            c.POINTER(ffi.wasm_extern_t),
            ffi.wasm_func_as_extern(func),
            ffi.wasm_global_as_extern(glbl),
        )
        ffi.wasm_extern_vec_new(imports, 2, data)
        imports = c.pointer(imports)
        self.assertIsNotNullPointer(imports)
        return imports

    def instantiate(self, module, imports):
        print("Instantiate module...")
        instance = ffi.wasm_instance_new(
            self._wasm_store, module, imports, ffi.create_null_pointer(ffi.wasm_trap_t)
        )
        self.assertIsNotNone(instance)
        self.assertIsNotNullPointer(instance)
        return instance

    def extract_exports(self, instance):
        print("Extracting exports...")
        exports = ffi.wasm_extern_vec_t()
        ffi.wasm_instance_exports(instance, exports)
        exports = c.pointer(exports)
        self.assertIsNotNullPointer(exports)
        return exports

    def setUp(self):
        binary = self.load_binary(MODULE_BINARY)
        self.module = self.compile(binary)
        self.imports = self.prepare_imports_local()
        self.instance = self.instantiate(self.module, self.imports)
        self.exports = self.extract_exports(self.instance)

        ffi.wasm_byte_vec_delete(binary)

    def tearDown(self):
        if self.imports:
            ffi.wasm_extern_vec_delete(self.imports)

        if self.exports:
            ffi.wasm_extern_vec_delete(self.exports)

        ffi.wasm_instance_delete(self.instance)
        ffi.wasm_module_delete(self.module)

    def test_wasm_func_call_wasm(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        print(export_list)

        func = ffi.wasm_extern_as_func(export_list[0])
        self.assertIsNotNullPointer(func)

        # make a call
        params = ffi.wasm_val_vec_t()
        data = ffi.list_to_carray(
            ffi.wasm_val_t,
            ffi.wasm_i32_val(1024),
            ffi.wasm_i64_val(1024 * 1024),
        )
        ffi.wasm_val_vec_new(params, 2, data)

        results = ffi.wasm_val_vec_t()
        ffi.wasm_val_vec_new_empty(results)

        ffi.wasm_func_call(func, params, results)

    def test_wasm_func_call_native(self):
        import_list = ffi.wasm_vec_to_list(self.imports)

        func = ffi.wasm_extern_as_func(import_list[0])
        self.assertIsNotNullPointer(func)

        params = ffi.wasm_val_vec_t()
        ffi.wasm_val_vec_new(
            params, 1, ffi.list_to_carray(ffi.wasm_val_t, ffi.wasm_f32_val(3.14))
        )
        results = ffi.wasm_val_vec_t()
        ffi.wasm_val_vec_new_uninitialized(results, 1)
        ffi.wasm_func_call(func, params, results)
        self.assertEqual(params.data[0].of.f32 * 2, results.data[0].of.f64)

    def test_wasm_func_call_wrong_params(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        func = ffi.wasm_extern_as_func(export_list[0])
        # make a call
        params = ffi.wasm_val_vec_t()
        ffi.wasm_val_vec_new_empty(params)
        results = ffi.wasm_val_vec_t()
        ffi.wasm_val_vec_new_empty(results)
        trap = ffi.wasm_func_call(func, params, results)

        self.assertIsNotNullPointer(trap)

    def test_wasm_func_call_unlinked(self):
        ft = ffi.wasm_functype_new_0_0()
        func = ffi.wasm_func_new(self._wasm_store, ft, callback)
        params = ffi.wasm_val_vec_t()
        ffi.wasm_val_vec_new_empty(params)
        results = ffi.wasm_val_vec_t()
        ffi.wasm_val_vec_new_empty(results)
        trap = ffi.wasm_func_call(func, params, results)
        ffi.wasm_func_delete(func)

    def test_wasm_global_get_wasm(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        glb = ffi.wasm_extern_as_global(export_list[1])
        self.assertIsNotNullPointer(glb)

        # access the global
        val = ffi.wasm_val_t()
        ffi.wasm_global_get(glb, val)
        self.assertAlmostEqual(val.of.f32, 3.14, places=3)

    def test_wasm_global_get_native(self):
        import_list = ffi.wasm_vec_to_list(self.imports)

        glb = ffi.wasm_extern_as_global(import_list[1])
        self.assertIsNotNullPointer(glb)

        val = ffi.wasm_val_t()
        ffi.wasm_global_get(glb, val)
        self.assertEqual(val.of.i32, 1024)

    def test_wasm_global_get_unlinked(self):
        gt = ffi.wasm_globaltype_new(ffi.wasm_valtype_new(ffi.WASM_I32), True)
        init = ffi.wasm_i32_val(32)
        glbl = ffi.wasm_global_new(self._wasm_store, gt, init)
        val_ret = ffi.wasm_f32_val(3.14)
        ffi.wasm_global_get(glbl, val_ret)
        ffi.wasm_global_delete(glbl)

        # val_ret wasn't touched, keep the original value
        self.assertAlmostEqual(val_ret.of.f32, 3.14, 3)

    def test_wasm_global_get_null_val(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        glb = ffi.wasm_extern_as_global(export_list[1])
        ffi.wasm_global_get(glb, ffi.create_null_pointer(ffi.wasm_val_t))

    def test_wasm_global_get_null_global(self):
        val = ffi.wasm_val_t()
        ffi.wasm_global_get(ffi.create_null_pointer(ffi.wasm_global_t), val)

    def test_wasm_global_set_wasm(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        glb = ffi.wasm_extern_as_global(export_list[1])
        self.assertIsNotNullPointer(glb)

        # access the global
        new_val = ffi.wasm_f32_val(math.e)
        ffi.wasm_global_set(glb, new_val)

        val = ffi.wasm_val_t()
        ffi.wasm_global_get(glb, val)
        self.assertNotEqual(val.of.f32, 3.14)

    def test_wasm_global_set_native(self):
        import_list = ffi.wasm_vec_to_list(self.imports)

        glb = ffi.wasm_extern_as_global(import_list[1])
        self.assertIsNotNullPointer(glb)

        new_val = ffi.wasm_i32_val(2048)
        ffi.wasm_global_set(glb, new_val)

        val = ffi.wasm_val_t()
        ffi.wasm_global_get(glb, val)
        self.assertEqual(val, new_val)

    def test_wasm_global_set_unlinked(self):
        gt = ffi.wasm_globaltype_new(ffi.wasm_valtype_new(ffi.WASM_I32), True)
        init = ffi.wasm_i32_val(32)
        glbl = ffi.wasm_global_new(self._wasm_store, gt, init)
        val_ret = ffi.wasm_f32_val(3.14)
        ffi.wasm_global_set(glbl, val_ret)
        ffi.wasm_global_delete(glbl)

    def test_wasm_global_set_null_v(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        glb = ffi.wasm_extern_as_global(export_list[1])
        # access the global
        ffi.wasm_global_set(glb, ffi.create_null_pointer(ffi.wasm_val_t))

    def test_wasm_global_set_null_global(self):
        # access the global
        new_val = ffi.wasm_f32_val(math.e)
        ffi.wasm_global_set(ffi.create_null_pointer(ffi.wasm_global_t), new_val)

    def test_wasm_table_size(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        tbl = ffi.wasm_extern_as_table(export_list[3])
        self.assertIsNotNullPointer(tbl)

        tbl_sz = ffi.wasm_table_size(tbl)
        self.assertEqual(tbl_sz, 1)

    def test_wasm_table_size_unlink(self):
        vt = ffi.wasm_valtype_new(ffi.WASM_FUNCREF)
        limits = ffi.wasm_limits_new(10, 15)
        tt = ffi.wasm_tabletype_new(vt, limits)
        tbl = ffi.wasm_table_new(
            self._wasm_store, tt, ffi.create_null_pointer(ffi.wasm_ref_t)
        )
        tbl_sz = ffi.wasm_table_size(tbl)
        ffi.wasm_table_delete(tbl)

    def test_wasm_table_size_null_table(self):
        ffi.wasm_table_size(ffi.create_null_pointer(ffi.wasm_table_t))

    def test_wasm_table_get(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        tbl = ffi.wasm_extern_as_table(export_list[3])
        self.assertIsNotNullPointer(tbl)

        ref = ffi.wasm_table_get(tbl, 0)
        self.assertIsNullPointer(ref)

        ref = ffi.wasm_table_get(tbl, 4096)
        self.assertIsNullPointer(ref)

    def test_wasm_table_get_unlinked(self):
        vt = ffi.wasm_valtype_new(ffi.WASM_FUNCREF)
        limits = ffi.wasm_limits_new(10, 15)
        tt = ffi.wasm_tabletype_new(vt, limits)
        tbl = ffi.wasm_table_new(
            self._wasm_store, tt, ffi.create_null_pointer(ffi.wasm_ref_t)
        )
        ffi.wasm_table_get(tbl, 0)
        ffi.wasm_table_delete(tbl)

    def test_wasm_table_get_null_table(self):
        ffi.wasm_table_get(ffi.create_null_pointer(ffi.wasm_table_t), 0)

    def test_wasm_table_get_out_of_bounds(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        tbl = ffi.wasm_extern_as_table(export_list[3])
        ffi.wasm_table_get(tbl, 1_000_000_000)

    def test_wasm_ref(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        func = ffi.wasm_extern_as_func(export_list[0])
        self.assertIsNotNullPointer(func)

        ref = ffi.wasm_func_as_ref(func)
        self.assertIsNotNullPointer(ref)

        func_from_ref = ffi.wasm_ref_as_func(ref)
        self.assertEqual(
            ffi.dereference(ffi.wasm_func_type(func)),
            ffi.dereference(ffi.wasm_func_type(func_from_ref)),
        )

    def test_wasm_table_set(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        tbl = ffi.wasm_extern_as_table(export_list[3])
        self.assertIsNotNullPointer(tbl)

        func = ffi.wasm_extern_as_func(export_list[0])
        ref = ffi.wasm_func_as_ref(func)

        ffi.wasm_table_set(tbl, 0, ref)

        ref_ret = ffi.wasm_table_get(tbl, 0)
        self.assertIsNotNullPointer(ref_ret)
        func_ret = ffi.wasm_ref_as_func(ref_ret)
        self.assertEqual(
            ffi.dereference(ffi.wasm_func_type(func)),
            ffi.dereference(ffi.wasm_func_type(func_ret)),
        )

    def test_wasm_table_set_unlinked(self):
        vt = ffi.wasm_valtype_new(ffi.WASM_FUNCREF)
        limits = ffi.wasm_limits_new(10, 15)
        tt = ffi.wasm_tabletype_new(vt, limits)
        tbl = ffi.wasm_table_new(
            self._wasm_store, tt, ffi.create_null_pointer(ffi.wasm_ref_t)
        )
        export_list = ffi.wasm_vec_to_list(self.exports)
        func = ffi.wasm_extern_as_func(export_list[0])
        ref = ffi.wasm_func_as_ref(func)
        ffi.wasm_table_set(tbl, 0, ref)
        ffi.wasm_table_delete(tbl)

    def test_wasm_table_set_null_table(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        func = ffi.wasm_extern_as_func(export_list[0])
        ref = ffi.wasm_func_as_ref(func)
        ffi.wasm_table_set(ffi.create_null_pointer(ffi.wasm_table_t), 0, ref)

    def test_wasm_table_set_null_ref(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        tbl = ffi.wasm_extern_as_table(export_list[3])
        ffi.wasm_table_set(tbl, 0, ffi.create_null_pointer(ffi.wasm_ref_t))

    def test_wasm_table_set_out_of_bounds(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        tbl = ffi.wasm_extern_as_table(export_list[3])
        func = ffi.wasm_extern_as_func(export_list[0])
        ref = ffi.wasm_func_as_ref(func)
        ffi.wasm_table_set(tbl, 1_000_000_000, ref)

    def test_wasm_memory_size(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        mem = ffi.wasm_extern_as_memory(export_list[2])
        self.assertIsNotNullPointer(mem)

        pg_sz = ffi.wasm_memory_size(mem)
        self.assertEqual(pg_sz, 1)

    def test_wasm_memory_size_unlinked(self):
        limits = ffi.wasm_limits_new(10, 12)
        mt = ffi.wasm_memorytype_new(limits)
        mem = ffi.wasm_memory_new(self._wasm_store, mt)
        ffi.wasm_memory_size(mem)
        ffi.wasm_memory_delete(mem)

    def test_wasm_memory_data(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        mem = ffi.wasm_extern_as_memory(export_list[2])
        self.assertIsNotNullPointer(mem)

        data_base = ffi.wasm_memory_data(mem)
        self.assertIsNotNone(data_base)

    def test_wasm_memory_data_unlinked(self):
        limits = ffi.wasm_limits_new(10, 12)
        mt = ffi.wasm_memorytype_new(limits)
        mem = ffi.wasm_memory_new(self._wasm_store, mt)
        ffi.wasm_memory_data(mem)
        ffi.wasm_memory_delete(mem)

    def test_wasm_memory_data_size(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        mem = ffi.wasm_extern_as_memory(export_list[2])
        self.assertIsNotNullPointer(mem)

        mem_sz = ffi.wasm_memory_data_size(mem)
        self.assertGreater(mem_sz, 0)

    def test_wasm_memory_data_size_unlinked(self):
        limits = ffi.wasm_limits_new(10, 12)
        mt = ffi.wasm_memorytype_new(limits)
        mem = ffi.wasm_memory_new(self._wasm_store, mt)
        ffi.wasm_memory_data_size(mem)
        ffi.wasm_memory_delete(mem)

    def test_wasm_trap(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        func = ffi.wasm_extern_as_func(export_list[0])
        # make a call
        params = ffi.wasm_val_vec_t()
        ffi.wasm_val_vec_new_empty(params)
        results = ffi.wasm_val_vec_t()
        ffi.wasm_val_vec_new_empty(results)

        trap = ffi.wasm_func_call(func, params, results)
        self.assertIsNotNullPointer(trap)

        message = ffi.wasm_message_t()
        ffi.wasm_trap_message(trap, message)
        self.assertIsNotNullPointer(c.pointer(message))

        # not a function internal exception
        frame = ffi.wasm_trap_origin(trap)
        self.assertIsNullPointer(frame)

    @unittest.skipUnless(
        TEST_WITH_WAMR_BUILD_DUMP_CALL_STACK,
        "need to enable WAMR_BUILD_DUMP_CALL_STACK",
    )
    # assertions only works if enabling WAMR_BUILD_DUMP_CALL_STACK
    def test_wasm_frame(self):
        export_list = ffi.wasm_vec_to_list(self.exports)
        func = ffi.wasm_extern_as_func(export_list[4])
        # make a call
        params = ffi.wasm_val_vec_t()
        ffi.wasm_val_vec_new_empty(params)
        results = ffi.wasm_val_vec_t()
        ffi.wasm_val_vec_new_empty(results)

        print("Making a call...")
        trap = ffi.wasm_func_call(func, params, results)

        message = ffi.wasm_message_t()
        ffi.wasm_trap_message(trap, message)
        self.assertIsNotNullPointer(c.pointer(message))
        print(message)

        frame = ffi.wasm_trap_origin(trap)
        self.assertIsNotNullPointer(frame)
        print(ffi.dereference(frame))

        traces = ffi.wasm_frame_vec_t()
        ffi.wasm_trap_trace(trap, traces)
        self.assertIsNotNullPointer(c.pointer(frame))

        instance = ffi.wasm_frame_instance(frame)
        self.assertIsNotNullPointer(instance)

        module_offset = ffi.wasm_frame_module_offset(frame)

        func_index = ffi.wasm_frame_func_index(frame)
        self.assertEqual(func_index, 2)

        func_offset = ffi.wasm_frame_func_offset(frame)
        self.assertGreater(func_offset, 0)

    @classmethod
    def tearDownClass(cls):
        print("Shutting down...")
        ffi.wasm_store_delete(cls._wasm_store)
        ffi.wasm_engine_delete(cls._wasm_engine)


if __name__ == "__main__":
    unittest.main()