17a1b7cdfSAlex Crichton /**
27a1b7cdfSAlex Crichton  * \file wasmtime/module.h
37a1b7cdfSAlex Crichton  *
47a1b7cdfSAlex Crichton  * APIs for interacting with modules in Wasmtime
57a1b7cdfSAlex Crichton  */
67a1b7cdfSAlex Crichton 
77a1b7cdfSAlex Crichton #ifndef WASMTIME_MODULE_H
87a1b7cdfSAlex Crichton #define WASMTIME_MODULE_H
97a1b7cdfSAlex Crichton 
107a1b7cdfSAlex Crichton #include <wasm.h>
11*62f60665SAlex Crichton #include <wasmtime/conf.h>
127a1b7cdfSAlex Crichton #include <wasmtime/error.h>
137a1b7cdfSAlex Crichton 
147a1b7cdfSAlex Crichton #ifdef __cplusplus
157a1b7cdfSAlex Crichton extern "C" {
167a1b7cdfSAlex Crichton #endif
177a1b7cdfSAlex Crichton 
187a1b7cdfSAlex Crichton /**
197a1b7cdfSAlex Crichton  * \typedef wasmtime_module_t
207a1b7cdfSAlex Crichton  * \brief Convenience alias for #wasmtime_module
217a1b7cdfSAlex Crichton  *
227a1b7cdfSAlex Crichton  * \struct wasmtime_module
237a1b7cdfSAlex Crichton  * \brief A compiled Wasmtime module.
247a1b7cdfSAlex Crichton  *
257a1b7cdfSAlex Crichton  * This type represents a compiled WebAssembly module. The compiled module is
267a1b7cdfSAlex Crichton  * ready to be instantiated and can be inspected for imports/exports. It is safe
277a1b7cdfSAlex Crichton  * to use a module across multiple threads simultaneously.
287a1b7cdfSAlex Crichton  */
297a1b7cdfSAlex Crichton typedef struct wasmtime_module wasmtime_module_t;
307a1b7cdfSAlex Crichton 
31b7631976SAlex Crichton #ifdef WASMTIME_FEATURE_COMPILER
32b7631976SAlex Crichton 
337a1b7cdfSAlex Crichton /**
347a1b7cdfSAlex Crichton  * \brief Compiles a WebAssembly binary into a #wasmtime_module_t
357a1b7cdfSAlex Crichton  *
367a1b7cdfSAlex Crichton  * This function will compile a WebAssembly binary into an owned #wasm_module_t.
377a1b7cdfSAlex Crichton  * This performs the same as #wasm_module_new except that it returns a
387a1b7cdfSAlex Crichton  * #wasmtime_error_t type to get richer error information.
397a1b7cdfSAlex Crichton  *
407a1b7cdfSAlex Crichton  * On success the returned #wasmtime_error_t is `NULL` and the `ret` pointer is
417a1b7cdfSAlex Crichton  * filled in with a #wasm_module_t. On failure the #wasmtime_error_t is
427a1b7cdfSAlex Crichton  * non-`NULL` and the `ret` pointer is unmodified.
437a1b7cdfSAlex Crichton  *
447a1b7cdfSAlex Crichton  * This function does not take ownership of any of its arguments, but the
457a1b7cdfSAlex Crichton  * returned error and module are owned by the caller.
467a1b7cdfSAlex Crichton  */
47f8fee938STyler Rockwood WASM_API_EXTERN wasmtime_error_t *wasmtime_module_new(wasm_engine_t *engine,
487a1b7cdfSAlex Crichton                                                       const uint8_t *wasm,
497a1b7cdfSAlex Crichton                                                       size_t wasm_len,
50f8fee938STyler Rockwood                                                       wasmtime_module_t **ret);
517a1b7cdfSAlex Crichton 
52b7631976SAlex Crichton #endif // WASMTIME_FEATURE_COMPILER
53b7631976SAlex Crichton 
547a1b7cdfSAlex Crichton /**
557a1b7cdfSAlex Crichton  * \brief Deletes a module.
567a1b7cdfSAlex Crichton  */
577a1b7cdfSAlex Crichton WASM_API_EXTERN void wasmtime_module_delete(wasmtime_module_t *m);
587a1b7cdfSAlex Crichton 
597a1b7cdfSAlex Crichton /**
607a1b7cdfSAlex Crichton  * \brief Creates a shallow clone of the specified module, increasing the
617a1b7cdfSAlex Crichton  * internal reference count.
627a1b7cdfSAlex Crichton  */
637a1b7cdfSAlex Crichton WASM_API_EXTERN wasmtime_module_t *wasmtime_module_clone(wasmtime_module_t *m);
647a1b7cdfSAlex Crichton 
657a1b7cdfSAlex Crichton /**
6613ec5ff6SAlex Crichton  * \brief Same as #wasm_module_imports, but for #wasmtime_module_t.
6713ec5ff6SAlex Crichton  */
68f8fee938STyler Rockwood WASM_API_EXTERN void wasmtime_module_imports(const wasmtime_module_t *module,
69f8fee938STyler Rockwood                                              wasm_importtype_vec_t *out);
7013ec5ff6SAlex Crichton 
7113ec5ff6SAlex Crichton /**
7213ec5ff6SAlex Crichton  * \brief Same as #wasm_module_exports, but for #wasmtime_module_t.
7313ec5ff6SAlex Crichton  */
74f8fee938STyler Rockwood WASM_API_EXTERN void wasmtime_module_exports(const wasmtime_module_t *module,
75f8fee938STyler Rockwood                                              wasm_exporttype_vec_t *out);
7613ec5ff6SAlex Crichton 
77b7631976SAlex Crichton #ifdef WASMTIME_FEATURE_COMPILER
78b7631976SAlex Crichton 
7913ec5ff6SAlex Crichton /**
807a1b7cdfSAlex Crichton  * \brief Validate a WebAssembly binary.
817a1b7cdfSAlex Crichton  *
827a1b7cdfSAlex Crichton  * This function will validate the provided byte sequence to determine if it is
837a1b7cdfSAlex Crichton  * a valid WebAssembly binary within the context of the engine provided.
847a1b7cdfSAlex Crichton  *
857a1b7cdfSAlex Crichton  * This function does not take ownership of its arguments but the caller is
867a1b7cdfSAlex Crichton  * expected to deallocate the returned error if it is non-`NULL`.
877a1b7cdfSAlex Crichton  *
887a1b7cdfSAlex Crichton  * If the binary validates then `NULL` is returned, otherwise the error returned
897a1b7cdfSAlex Crichton  * describes why the binary did not validate.
907a1b7cdfSAlex Crichton  */
91f8fee938STyler Rockwood WASM_API_EXTERN wasmtime_error_t *
92f8fee938STyler Rockwood wasmtime_module_validate(wasm_engine_t *engine, const uint8_t *wasm,
93f8fee938STyler Rockwood                          size_t wasm_len);
947a1b7cdfSAlex Crichton 
957a1b7cdfSAlex Crichton /**
967a1b7cdfSAlex Crichton  * \brief This function serializes compiled module artifacts as blob data.
977a1b7cdfSAlex Crichton  *
987a1b7cdfSAlex Crichton  * \param module the module
99f8fee938STyler Rockwood  * \param ret if the conversion is successful, this byte vector is filled in
100f8fee938STyler Rockwood  * with the serialized compiled module.
1017a1b7cdfSAlex Crichton  *
1027a1b7cdfSAlex Crichton  * \return a non-null error if parsing fails, or returns `NULL`. If parsing
1037a1b7cdfSAlex Crichton  * fails then `ret` isn't touched.
1047a1b7cdfSAlex Crichton  *
1057a1b7cdfSAlex Crichton  * This function does not take ownership of `module`, and the caller is
1067a1b7cdfSAlex Crichton  * expected to deallocate the returned #wasmtime_error_t and #wasm_byte_vec_t.
1077a1b7cdfSAlex Crichton  */
108f8fee938STyler Rockwood WASM_API_EXTERN wasmtime_error_t *
109f8fee938STyler Rockwood wasmtime_module_serialize(wasmtime_module_t *module, wasm_byte_vec_t *ret);
1107a1b7cdfSAlex Crichton 
111b7631976SAlex Crichton #endif // WASMTIME_FEATURE_COMPILER
112b7631976SAlex Crichton 
1137a1b7cdfSAlex Crichton /**
1147a1b7cdfSAlex Crichton  * \brief Build a module from serialized data.
1157a1b7cdfSAlex Crichton  *
1167a1b7cdfSAlex Crichton  * This function does not take ownership of any of its arguments, but the
1177a1b7cdfSAlex Crichton  * returned error and module are owned by the caller.
1187a1b7cdfSAlex Crichton  *
1197a1b7cdfSAlex Crichton  * This function is not safe to receive arbitrary user input. See the Rust
1207a1b7cdfSAlex Crichton  * documentation for more information on what inputs are safe to pass in here
121b7631976SAlex Crichton  * (e.g. only that of `wasmtime_module_serialize`)
1227a1b7cdfSAlex Crichton  */
123f8fee938STyler Rockwood WASM_API_EXTERN wasmtime_error_t *
124f8fee938STyler Rockwood wasmtime_module_deserialize(wasm_engine_t *engine, const uint8_t *bytes,
125f8fee938STyler Rockwood                             size_t bytes_len, wasmtime_module_t **ret);
1267a1b7cdfSAlex Crichton 
1279e0c9100SAlex Crichton /**
1289e0c9100SAlex Crichton  * \brief Deserialize a module from an on-disk file.
1299e0c9100SAlex Crichton  *
1309e0c9100SAlex Crichton  * This function is the same as #wasmtime_module_deserialize except that it
1319e0c9100SAlex Crichton  * reads the data for the serialized module from the path on disk. This can be
1329e0c9100SAlex Crichton  * faster than the alternative which may require copying the data around.
1339e0c9100SAlex Crichton  *
1349e0c9100SAlex Crichton  * This function does not take ownership of any of its arguments, but the
1359e0c9100SAlex Crichton  * returned error and module are owned by the caller.
1369e0c9100SAlex Crichton  *
1379e0c9100SAlex Crichton  * This function is not safe to receive arbitrary user input. See the Rust
1389e0c9100SAlex Crichton  * documentation for more information on what inputs are safe to pass in here
139b7631976SAlex Crichton  * (e.g. only that of `wasmtime_module_serialize`)
1409e0c9100SAlex Crichton  */
141f8fee938STyler Rockwood WASM_API_EXTERN wasmtime_error_t *
142f8fee938STyler Rockwood wasmtime_module_deserialize_file(wasm_engine_t *engine, const char *path,
143f8fee938STyler Rockwood                                  wasmtime_module_t **ret);
1442d43a28fSTyler Rockwood 
1452d43a28fSTyler Rockwood /**
146f8fee938STyler Rockwood  * \brief Returns the range of bytes in memory where this module’s compilation
147f8fee938STyler Rockwood  * image resides.
1482d43a28fSTyler Rockwood  *
149f8fee938STyler Rockwood  * The compilation image for a module contains executable code, data, debug
150f8fee938STyler Rockwood  * information, etc. This is roughly the same as the wasmtime_module_serialize
151f8fee938STyler Rockwood  * but not the exact same.
1522d43a28fSTyler Rockwood  *
153f8fee938STyler Rockwood  * For more details see:
154f8fee938STyler Rockwood  * https://docs.wasmtime.dev/api/wasmtime/struct.Module.html#method.image_range
1552d43a28fSTyler Rockwood  */
156f8fee938STyler Rockwood WASM_API_EXTERN void
157120e6b23SAlex Crichton wasmtime_module_image_range(const wasmtime_module_t *module, void **start,
158120e6b23SAlex Crichton                             void **end);
1592d43a28fSTyler Rockwood 
1607a1b7cdfSAlex Crichton #ifdef __cplusplus
1617a1b7cdfSAlex Crichton } // extern "C"
1627a1b7cdfSAlex Crichton #endif
1637a1b7cdfSAlex Crichton 
1647a1b7cdfSAlex Crichton #endif // WASMTIME_MODULE_H
165