summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/wasm-c-api/src/callback_chain.c
blob: e4f5801dc1508bdc56d4486a0dd48d0cd177c08d (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
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "wasm_c_api.h"

#define own

static const byte_t *
get_memory_data(uint32_t offset, uint32_t length);

static bool
call_wasm_function(uint32_t export_id, const wasm_val_vec_t *args,
                   wasm_val_vec_t *results, const char *name);

/************************ IMPORTED FUNCTIONS **************************/

// (nil) -> i32
#define FUNCTION_TYPE_NIL_I32 wasm_functype_new_0_1(wasm_valtype_new_i32())
// (i32, i32) -> nil
#define FUNCTION_TYPE_I32X2_NIL \
    wasm_functype_new_2_0(wasm_valtype_new_i32(), wasm_valtype_new_i32())

/* IMPORT FUNCTION LIST */
#define IMPORT_FUNCTION_LIST(V)            \
    V(get_pairs, 0, FUNCTION_TYPE_NIL_I32) \
    V(log, 1, FUNCTION_TYPE_I32X2_NIL)

/* EXPORT FUNCTION LIST */
#define EXPORT_FUNCTION_LIST(V) \
    V(on_start)                 \
    V(on_stop)                  \
    V(malloc)                   \
    V(free)

enum EXPORT_ITEM_NAME {
#define DEFINE_ENUM(name) e_##name,
    EXPORT_FUNCTION_LIST(DEFINE_ENUM)
#undef DEFINE_ENUM
        e_MEMORY,
};

#define DEFINE_FUNCTION(name)                            \
    wasm_trap_t *STUB_##name(const wasm_val_vec_t *args, \
                             wasm_val_vec_t *results)

#define DEFINE_EMPTY_FUNCTION(name)                                 \
    DEFINE_FUNCTION(name)                                           \
    {                                                               \
        printf("[WASM -> NATIVE] calling back %s\n", __FUNCTION__); \
        return NULL;                                                \
    }
#undef DEFINE_EMPTY_FUNCTION

DEFINE_FUNCTION(get_pairs)
{
    wasm_val_vec_t as = { 0 };
    wasm_val_t data[1] = { WASM_I32_VAL(10) };
    wasm_val_vec_new(&as, 1, data);
    if (as.data == NULL) {
        printf("ERROR: create parameters failed\n");
        return NULL;
    }

    call_wasm_function(e_malloc, &as, results, "malloc");

    wasm_val_vec_delete(&as);
    return NULL;
}

DEFINE_FUNCTION(log)
{
    wasm_val_t offset = args->data[0];
    wasm_val_t length = args->data[1];
    const byte_t *data = NULL;

    printf("[WASM -> NATIVE] calling back %s\n", __FUNCTION__);

    if (offset.kind != WASM_I32 || length.kind != WASM_I32) {
        printf("> Error value type!\n");
    }

    if (!(data = get_memory_data(offset.of.i32, length.of.i32))) {
        return NULL;
    }

    if (data[length.of.i32 - 1]) {
        printf("> Error terminated character\n");
        return NULL;
    }

    printf("[WASM_LOG] %s\n", data);
    return NULL;
}

/**********************************************************************/
// all exportted wasm functions. check with "/opt/wabt/bin/wasm-objdump -x -j
// Export X.wasm" -1: memory 0-32: functions
static own wasm_extern_vec_t exports = { 0 };

static const byte_t *
get_memory_data(uint32_t offset, uint32_t length)
{
    wasm_memory_t *memory;

    if (!(memory = wasm_extern_as_memory(exports.data[e_MEMORY]))) {
        return NULL;
    }

    byte_t *base = wasm_memory_data(memory);
    size_t size = wasm_memory_data_size(memory);
    if (!base || offset + length > size) {
        return NULL;
    }

    printf("[NATIVE -> WASM] accessing the memory...\n");

    return base + offset;
}

static bool
call_wasm_function(uint32_t export_id, const wasm_val_vec_t *args,
                   wasm_val_vec_t *results, const char *name)
{
    const wasm_func_t *function;
    wasm_trap_t *trap;

    printf("[NATIVE -> WASM] calling func %s...\n", name);

    if (!(function = wasm_extern_as_func(exports.data[export_id]))) {
        printf("> Error get export function %u\n", export_id);
        return false;
    }

    if ((trap = wasm_func_call(function, args, results))) {
        own wasm_message_t message = { 0 };
        wasm_trap_message(trap, &message);

        if (message.data) {
            printf("> Error calling function %s\n", message.data);
        }
        else {
            printf("> Error calling function");
        }

        wasm_name_delete(&message);
        wasm_trap_delete(trap);
        return false;
    }
    return true;
}

int
main(int argc, const char *argv[])
{
    // Initialize.
    printf("Initializing...\n");
    wasm_engine_t *engine = wasm_engine_new();
    wasm_store_t *store = wasm_store_new(engine);

    // Load binary.
    printf("Loading binary...\n");
#if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
    FILE *file = fopen("callback_chain.aot", "rb");
#else
    FILE *file = fopen("callback_chain.wasm", "rb");
#endif
    if (!file) {
        printf("> Error loading module!\n");
        return 1;
    }

    int ret = fseek(file, 0L, SEEK_END);
    if (ret == -1) {
        printf("> Error loading module!\n");
        fclose(file);
        return 1;
    }

    long file_size = ftell(file);
    if (file_size == -1) {
        printf("> Error loading module!\n");
        fclose(file);
        return 1;
    }

    ret = fseek(file, 0L, SEEK_SET);
    if (ret == -1) {
        printf("> Error loading module!\n");
        fclose(file);
        return 1;
    }

    wasm_byte_vec_t binary;
    wasm_byte_vec_new_uninitialized(&binary, file_size);
    if (fread(binary.data, file_size, 1, file) != 1) {
        printf("> Error loading module!\n");
        fclose(file);
        return 1;
    }
    fclose(file);

    // Compile.
    printf("Compiling module...\n");
    own wasm_module_t *module = wasm_module_new(store, &binary);
    if (!module) {
        printf("> Error compiling module!\n");
        return 1;
    }

    wasm_byte_vec_delete(&binary);

    // Instantiate.
    printf("Instantiating module...\n");

    // Create external functions.
    printf("Creating callback...\n");
#define IMPORT_FUNCTION_VARIABLE_NAME(name, ...) \
    own wasm_func_t *function_##name = NULL;
    IMPORT_FUNCTION_LIST(IMPORT_FUNCTION_VARIABLE_NAME)
#undef IMPORT_FUNCTION_VARIABLE_NAME

#define CREATE_WASM_FUNCTION(name, index, CREATE_FUNC_TYPE)                 \
    {                                                                       \
        own wasm_functype_t *type = CREATE_FUNC_TYPE;                       \
        if (!(function_##name = wasm_func_new(store, type, STUB_##name))) { \
            printf("> Error creating new function\n");                      \
            return 1;                                                       \
        }                                                                   \
        wasm_functype_delete(type);                                         \
    }
    IMPORT_FUNCTION_LIST(CREATE_WASM_FUNCTION)
#undef CREATE_WASM_FUNCTION

    wasm_extern_t *fs[2] = { 0 };
#define ADD_TO_FUNCTION_LIST(name, index, ...) \
    fs[index] = wasm_func_as_extern(function_##name);
    IMPORT_FUNCTION_LIST(ADD_TO_FUNCTION_LIST)
#undef ADD_TO_FUNCTION_LIST

    wasm_extern_vec_t imports = WASM_ARRAY_VEC(fs);
    own wasm_instance_t *instance =
        wasm_instance_new(store, module, &imports, NULL);
    if (!instance) {
        printf("> Error instantiating module!\n");
        return 1;
    }

#define DESTROY_WASM_FUNCITON(name, index, ...) \
    wasm_func_delete(function_##name);
    IMPORT_FUNCTION_LIST(DESTROY_WASM_FUNCITON)
#undef DESTROY_WASM_FUNCITON

    // Extract export.
    printf("Extracting export...\n");
    wasm_instance_exports(instance, &exports);
    if (!exports.size) {
        printf("> Error accessing exports!\n");
        return 1;
    }

    wasm_module_delete(module);
    wasm_instance_delete(instance);

    // Call.
    printf("Calling export...\n");

    if (!call_wasm_function(e_on_start, NULL, NULL, "on_start")) {
        printf("> Error calling on_start\n");
        return 1;
    }

    if (!call_wasm_function(e_on_stop, NULL, NULL, "on_stop")) {
        printf("> Error calling on_stop\n");
        return 1;
    }

    wasm_extern_vec_delete(&exports);

    // Shut down.
    printf("Shutting down...\n");
    wasm_store_delete(store);
    wasm_engine_delete(engine);

    // All done.
    printf("Done.\n");
    return 0;
}