xref: /wasmtime-44.0.1/examples/fuel.c (revision dfc4358d)
1 /*
2 Example of instantiating of the WebAssembly module and invoking its exported
3 function.
4 
5 You can compile and run this example on Linux with:
6 
7    cargo build --release -p wasmtime-c-api
8    cc examples/fuel.c \
9        -I crates/c-api/include \
10        target/release/libwasmtime.a \
11        -lpthread -ldl -lm \
12        -o fuel
13    ./fuel
14 
15 Note that on Windows and macOS the command will be similar, but you'll need
16 to tweak the `-lpthread` and such annotations.
17 
18 You can also build using cmake:
19 
20 mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-fuel
21 */
22 
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <wasm.h>
27 #include <wasmtime.h>
28 
29 static void exit_with_error(const char *message, wasmtime_error_t *error,
30                             wasm_trap_t *trap);
31 
32 int main() {
33   wasmtime_error_t *error = NULL;
34 
35   wasm_config_t *config = wasm_config_new();
36   assert(config != NULL);
37   wasmtime_config_consume_fuel_set(config, true);
38 
39   // Create an *engine*, which is a compilation context, with our configured
40   // options.
41   wasm_engine_t *engine = wasm_engine_new_with_config(config);
42   assert(engine != NULL);
43   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
44   assert(store != NULL);
45   wasmtime_context_t *context = wasmtime_store_context(store);
46 
47   error = wasmtime_context_set_fuel(context, 10000);
48   if (error != NULL)
49     exit_with_error("failed to set fuel", error, NULL);
50 
51   // Load our input file to parse it next
52   FILE *file = fopen("examples/fuel.wat", "r");
53   if (!file) {
54     printf("> Error loading file!\n");
55     return 1;
56   }
57   fseek(file, 0L, SEEK_END);
58   size_t file_size = ftell(file);
59   fseek(file, 0L, SEEK_SET);
60   wasm_byte_vec_t wat;
61   wasm_byte_vec_new_uninitialized(&wat, file_size);
62   if (fread(wat.data, file_size, 1, file) != 1) {
63     printf("> Error loading module!\n");
64     return 1;
65   }
66   fclose(file);
67 
68   // Parse the wat into the binary wasm format
69   wasm_byte_vec_t wasm;
70   error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
71   if (error != NULL)
72     exit_with_error("failed to parse wat", error, NULL);
73   wasm_byte_vec_delete(&wat);
74 
75   // Compile and instantiate our module
76   wasmtime_module_t *module = NULL;
77   error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
78   if (module == NULL)
79     exit_with_error("failed to compile module", error, NULL);
80   wasm_byte_vec_delete(&wasm);
81 
82   wasm_trap_t *trap = NULL;
83   wasmtime_instance_t instance;
84   error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
85   if (error != NULL || trap != NULL)
86     exit_with_error("failed to instantiate", error, trap);
87 
88   // Lookup our `fibonacci` export function
89   wasmtime_extern_t fib;
90   bool ok = wasmtime_instance_export_get(context, &instance, "fibonacci",
91                                          strlen("fibonacci"), &fib);
92   assert(ok);
93   assert(fib.kind == WASMTIME_EXTERN_FUNC);
94 
95   // Call it repeatedly until it fails
96   for (int n = 1;; n++) {
97     uint64_t fuel_before;
98     wasmtime_context_get_fuel(context, &fuel_before);
99     wasmtime_val_t params[1];
100     params[0].kind = WASMTIME_I32;
101     params[0].of.i32 = n;
102     wasmtime_val_t results[1];
103     error =
104         wasmtime_func_call(context, &fib.of.func, params, 1, results, 1, &trap);
105     if (error != NULL || trap != NULL) {
106       if (trap != NULL) {
107         wasmtime_trap_code_t code;
108         assert(wasmtime_trap_code(trap, &code));
109         assert(code == WASMTIME_TRAP_CODE_OUT_OF_FUEL);
110       }
111       printf("Exhausted fuel computing fib(%d)\n", n);
112       break;
113     }
114 
115     uint64_t fuel_after;
116     wasmtime_context_get_fuel(context, &fuel_after);
117     assert(results[0].kind == WASMTIME_I32);
118     printf("fib(%d) = %d [consumed %lu fuel]\n", n, results[0].of.i32,
119            fuel_after - fuel_before);
120 
121     error = wasmtime_context_set_fuel(context, 10000);
122     if (error != NULL)
123       exit_with_error("failed to set fuel", error, NULL);
124   }
125 
126   // Clean up after ourselves at this point
127   wasmtime_module_delete(module);
128   wasmtime_store_delete(store);
129   wasm_engine_delete(engine);
130   return 0;
131 }
132 
133 static void exit_with_error(const char *message, wasmtime_error_t *error,
134                             wasm_trap_t *trap) {
135   fprintf(stderr, "error: %s\n", message);
136   wasm_byte_vec_t error_message;
137   if (error != NULL) {
138     wasmtime_error_message(error, &error_message);
139   } else {
140     wasm_trap_message(trap, &error_message);
141   }
142   fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
143   wasm_byte_vec_delete(&error_message);
144   exit(1);
145 }
146