1#![cfg_attr(not(feature = "cache"), allow(unused_imports))]
4
5use crate::{handle_result, wasm_memorytype_t, wasmtime_error_t};
6use std::ops::Range;
7use std::os::raw::c_char;
8use std::ptr;
9use std::{ffi::CStr, sync::Arc};
10use wasmtime::{
11 Config, LinearMemory, MemoryCreator, OptLevel, ProfilingStrategy, Result, Strategy,
12};
13
14#[repr(C)]
15#[derive(Clone)]
16pub struct wasm_config_t {
17 pub(crate) config: Config,
18}
19
20wasmtime_c_api_macros::declare_own!(wasm_config_t);
21
22#[repr(u8)]
23#[derive(Clone)]
24pub enum wasmtime_strategy_t {
25 WASMTIME_STRATEGY_AUTO,
26 WASMTIME_STRATEGY_CRANELIFT,
27}
28
29#[repr(u8)]
30#[derive(Clone)]
31pub enum wasmtime_opt_level_t {
32 WASMTIME_OPT_LEVEL_NONE,
33 WASMTIME_OPT_LEVEL_SPEED,
34 WASMTIME_OPT_LEVEL_SPEED_AND_SIZE,
35}
36
37#[repr(u8)]
38#[derive(Clone)]
39pub enum wasmtime_profiling_strategy_t {
40 WASMTIME_PROFILING_STRATEGY_NONE,
41 WASMTIME_PROFILING_STRATEGY_JITDUMP,
42 WASMTIME_PROFILING_STRATEGY_VTUNE,
43 WASMTIME_PROFILING_STRATEGY_PERFMAP,
44}
45
46#[no_mangle]
47pub extern "C" fn wasm_config_new() -> Box<wasm_config_t> {
48 Box::new(wasm_config_t {
49 config: Config::default(),
50 })
51}
52
53#[no_mangle]
54pub extern "C" fn wasmtime_config_debug_info_set(c: &mut wasm_config_t, enable: bool) {
55 c.config.debug_info(enable);
56}
57
58#[no_mangle]
59pub extern "C" fn wasmtime_config_consume_fuel_set(c: &mut wasm_config_t, enable: bool) {
60 c.config.consume_fuel(enable);
61}
62
63#[no_mangle]
64pub extern "C" fn wasmtime_config_epoch_interruption_set(c: &mut wasm_config_t, enable: bool) {
65 c.config.epoch_interruption(enable);
66}
67
68#[no_mangle]
69pub extern "C" fn wasmtime_config_max_wasm_stack_set(c: &mut wasm_config_t, size: usize) {
70 c.config.max_wasm_stack(size);
71}
72
73#[no_mangle]
74#[cfg(feature = "threads")]
75pub extern "C" fn wasmtime_config_wasm_threads_set(c: &mut wasm_config_t, enable: bool) {
76 c.config.wasm_threads(enable);
77}
78
79#[no_mangle]
80pub extern "C" fn wasmtime_config_wasm_tail_call_set(c: &mut wasm_config_t, enable: bool) {
81 c.config.wasm_tail_call(enable);
82}
83
84#[no_mangle]
85pub extern "C" fn wasmtime_config_wasm_reference_types_set(c: &mut wasm_config_t, enable: bool) {
86 c.config.wasm_reference_types(enable);
87}
88
89#[no_mangle]
90pub extern "C" fn wasmtime_config_wasm_function_references_set(
91 c: &mut wasm_config_t,
92 enable: bool,
93) {
94 c.config.wasm_function_references(enable);
95}
96
97#[no_mangle]
98pub extern "C" fn wasmtime_config_wasm_gc_set(c: &mut wasm_config_t, enable: bool) {
99 c.config.wasm_gc(enable);
100}
101
102#[no_mangle]
103pub extern "C" fn wasmtime_config_wasm_simd_set(c: &mut wasm_config_t, enable: bool) {
104 c.config.wasm_simd(enable);
105}
106
107#[no_mangle]
108pub extern "C" fn wasmtime_config_wasm_relaxed_simd_set(c: &mut wasm_config_t, enable: bool) {
109 c.config.wasm_relaxed_simd(enable);
110}
111
112#[no_mangle]
113pub extern "C" fn wasmtime_config_wasm_relaxed_simd_deterministic_set(
114 c: &mut wasm_config_t,
115 enable: bool,
116) {
117 c.config.relaxed_simd_deterministic(enable);
118}
119
120#[no_mangle]
121pub extern "C" fn wasmtime_config_wasm_bulk_memory_set(c: &mut wasm_config_t, enable: bool) {
122 c.config.wasm_bulk_memory(enable);
123}
124
125#[no_mangle]
126pub extern "C" fn wasmtime_config_wasm_multi_value_set(c: &mut wasm_config_t, enable: bool) {
127 c.config.wasm_multi_value(enable);
128}
129
130#[no_mangle]
131pub extern "C" fn wasmtime_config_wasm_multi_memory_set(c: &mut wasm_config_t, enable: bool) {
132 c.config.wasm_multi_memory(enable);
133}
134
135#[no_mangle]
136pub extern "C" fn wasmtime_config_wasm_memory64_set(c: &mut wasm_config_t, enable: bool) {
137 c.config.wasm_memory64(enable);
138}
139
140#[no_mangle]
141#[cfg(any(feature = "cranelift", feature = "winch"))]
142pub extern "C" fn wasmtime_config_strategy_set(
143 c: &mut wasm_config_t,
144 strategy: wasmtime_strategy_t,
145) {
146 use wasmtime_strategy_t::*;
147 c.config.strategy(match strategy {
148 WASMTIME_STRATEGY_AUTO => Strategy::Auto,
149 WASMTIME_STRATEGY_CRANELIFT => Strategy::Cranelift,
150 });
151}
152
153#[no_mangle]
154#[cfg(feature = "parallel-compilation")]
155pub extern "C" fn wasmtime_config_parallel_compilation_set(c: &mut wasm_config_t, enable: bool) {
156 c.config.parallel_compilation(enable);
157}
158
159#[no_mangle]
160#[cfg(any(feature = "cranelift", feature = "winch"))]
161pub extern "C" fn wasmtime_config_cranelift_debug_verifier_set(
162 c: &mut wasm_config_t,
163 enable: bool,
164) {
165 c.config.cranelift_debug_verifier(enable);
166}
167
168#[no_mangle]
169#[cfg(any(feature = "cranelift", feature = "winch"))]
170pub extern "C" fn wasmtime_config_cranelift_nan_canonicalization_set(
171 c: &mut wasm_config_t,
172 enable: bool,
173) {
174 c.config.cranelift_nan_canonicalization(enable);
175}
176
177#[no_mangle]
178#[cfg(any(feature = "cranelift", feature = "winch"))]
179pub extern "C" fn wasmtime_config_cranelift_opt_level_set(
180 c: &mut wasm_config_t,
181 opt_level: wasmtime_opt_level_t,
182) {
183 use wasmtime_opt_level_t::*;
184 c.config.cranelift_opt_level(match opt_level {
185 WASMTIME_OPT_LEVEL_NONE => OptLevel::None,
186 WASMTIME_OPT_LEVEL_SPEED => OptLevel::Speed,
187 WASMTIME_OPT_LEVEL_SPEED_AND_SIZE => OptLevel::SpeedAndSize,
188 });
189}
190
191#[no_mangle]
192pub extern "C" fn wasmtime_config_profiler_set(
193 c: &mut wasm_config_t,
194 strategy: wasmtime_profiling_strategy_t,
195) {
196 use wasmtime_profiling_strategy_t::*;
197 c.config.profiler(match strategy {
198 WASMTIME_PROFILING_STRATEGY_NONE => ProfilingStrategy::None,
199 WASMTIME_PROFILING_STRATEGY_JITDUMP => ProfilingStrategy::JitDump,
200 WASMTIME_PROFILING_STRATEGY_VTUNE => ProfilingStrategy::VTune,
201 WASMTIME_PROFILING_STRATEGY_PERFMAP => ProfilingStrategy::PerfMap,
202 });
203}
204
205#[no_mangle]
206#[cfg(feature = "cache")]
207pub unsafe extern "C" fn wasmtime_config_cache_config_load(
208 c: &mut wasm_config_t,
209 filename: *const c_char,
210) -> Option<Box<wasmtime_error_t>> {
211 handle_result(
212 if filename.is_null() {
213 c.config.cache_config_load_default()
214 } else {
215 match CStr::from_ptr(filename).to_str() {
216 Ok(s) => c.config.cache_config_load(s),
217 Err(e) => Err(e.into()),
218 }
219 },
220 |_cfg| {},
221 )
222}
223
224#[no_mangle]
225pub extern "C" fn wasmtime_config_static_memory_forced_set(c: &mut wasm_config_t, enable: bool) {
226 c.config.static_memory_forced(enable);
227}
228
229#[no_mangle]
230pub extern "C" fn wasmtime_config_static_memory_maximum_size_set(c: &mut wasm_config_t, size: u64) {
231 c.config.static_memory_maximum_size(size);
232}
233
234#[no_mangle]
235pub extern "C" fn wasmtime_config_static_memory_guard_size_set(c: &mut wasm_config_t, size: u64) {
236 c.config.static_memory_guard_size(size);
237}
238
239#[no_mangle]
240pub extern "C" fn wasmtime_config_dynamic_memory_guard_size_set(c: &mut wasm_config_t, size: u64) {
241 c.config.dynamic_memory_guard_size(size);
242}
243
244#[no_mangle]
245pub extern "C" fn wasmtime_config_dynamic_memory_reserved_for_growth_set(
246 c: &mut wasm_config_t,
247 size: u64,
248) {
249 c.config.dynamic_memory_reserved_for_growth(size);
250}
251
252#[no_mangle]
253pub extern "C" fn wasmtime_config_native_unwind_info_set(c: &mut wasm_config_t, enabled: bool) {
254 c.config.native_unwind_info(enabled);
255}
256
257#[no_mangle]
258#[cfg(any(feature = "cranelift", feature = "winch"))]
259pub unsafe extern "C" fn wasmtime_config_target_set(
260 c: &mut wasm_config_t,
261 target: *const c_char,
262) -> Option<Box<wasmtime_error_t>> {
263 let target = CStr::from_ptr(target).to_str().expect("not valid utf-8");
264 handle_result(c.config.target(target), |_cfg| {})
265}
266
267#[no_mangle]
268pub extern "C" fn wasmtime_config_macos_use_mach_ports_set(c: &mut wasm_config_t, enabled: bool) {
269 c.config.macos_use_mach_ports(enabled);
270}
271
272#[no_mangle]
273#[cfg(any(feature = "cranelift", feature = "winch"))]
274pub unsafe extern "C" fn wasmtime_config_cranelift_flag_enable(
275 c: &mut wasm_config_t,
276 flag: *const c_char,
277) {
278 let flag = CStr::from_ptr(flag).to_str().expect("not valid utf-8");
279 c.config.cranelift_flag_enable(flag);
280}
281
282#[no_mangle]
283#[cfg(any(feature = "cranelift", feature = "winch"))]
284pub unsafe extern "C" fn wasmtime_config_cranelift_flag_set(
285 c: &mut wasm_config_t,
286 flag: *const c_char,
287 value: *const c_char,
288) {
289 let flag = CStr::from_ptr(flag).to_str().expect("not valid utf-8");
290 let value = CStr::from_ptr(value).to_str().expect("not valid utf-8");
291 c.config.cranelift_flag_set(flag, value);
292}
293
294pub type wasmtime_memory_get_callback_t = extern "C" fn(
295 env: *mut std::ffi::c_void,
296 byte_size: &mut usize,
297 maximum_byte_size: &mut usize,
298) -> *mut u8;
299
300pub type wasmtime_memory_grow_callback_t =
301 extern "C" fn(env: *mut std::ffi::c_void, new_size: usize) -> Option<Box<wasmtime_error_t>>;
302
303#[repr(C)]
304pub struct wasmtime_linear_memory_t {
305 env: *mut std::ffi::c_void,
306 get_memory: wasmtime_memory_get_callback_t,
307 grow_memory: wasmtime_memory_grow_callback_t,
308 finalizer: Option<extern "C" fn(arg1: *mut std::ffi::c_void)>,
309}
310
311pub type wasmtime_new_memory_callback_t = extern "C" fn(
312 env: *mut std::ffi::c_void,
313 ty: &wasm_memorytype_t,
314 minimum: usize,
315 maximum: usize,
316 reserved_size_in_bytes: usize,
317 guard_size_in_bytes: usize,
318 memory_ret: *mut wasmtime_linear_memory_t,
319) -> Option<Box<wasmtime_error_t>>;
320
321struct CHostLinearMemory {
322 foreign: crate::ForeignData,
323 get_memory: wasmtime_memory_get_callback_t,
324 grow_memory: wasmtime_memory_grow_callback_t,
325}
326
327unsafe impl LinearMemory for CHostLinearMemory {
328 fn byte_size(&self) -> usize {
329 let mut byte_size = 0;
330 let mut maximum_byte_size = 0;
331 let cb = self.get_memory;
332 cb(self.foreign.data, &mut byte_size, &mut maximum_byte_size);
333 return byte_size;
334 }
335 fn maximum_byte_size(&self) -> Option<usize> {
336 let mut byte_size = 0;
337 let mut maximum_byte_size = 0;
338 let cb = self.get_memory;
339 cb(self.foreign.data, &mut byte_size, &mut maximum_byte_size);
340 if maximum_byte_size == 0 {
341 None
342 } else {
343 Some(maximum_byte_size)
344 }
345 }
346 fn as_ptr(&self) -> *mut u8 {
347 let mut byte_size = 0;
348 let mut maximum_byte_size = 0;
349 let cb = self.get_memory;
350 cb(self.foreign.data, &mut byte_size, &mut maximum_byte_size)
351 }
352 fn wasm_accessible(&self) -> Range<usize> {
353 let mut byte_size = 0;
354 let mut maximum_byte_size = 0;
355 let cb = self.get_memory;
356 let ptr = cb(self.foreign.data, &mut byte_size, &mut maximum_byte_size);
357 Range {
358 start: ptr as usize,
359 end: ptr as usize + byte_size,
360 }
361 }
362 fn grow_to(&mut self, new_size: usize) -> Result<()> {
363 let cb = self.grow_memory;
364 let error = cb(self.foreign.data, new_size);
365 if let Some(err) = error {
366 Err((*err).into())
367 } else {
368 Ok(())
369 }
370 }
371}
372
373#[repr(C)]
374pub struct wasmtime_memory_creator_t {
375 env: *mut std::ffi::c_void,
376 new_memory: wasmtime_new_memory_callback_t,
377 finalizer: Option<extern "C" fn(arg1: *mut std::ffi::c_void)>,
378}
379
380struct CHostMemoryCreator {
381 foreign: crate::ForeignData,
382 new_memory: wasmtime_new_memory_callback_t,
383}
384unsafe impl Send for CHostMemoryCreator {}
385unsafe impl Sync for CHostMemoryCreator {}
386
387unsafe impl MemoryCreator for CHostMemoryCreator {
388 fn new_memory(
389 &self,
390 ty: wasmtime::MemoryType,
391 minimum: usize,
392 maximum: Option<usize>,
393 reserved_size_in_bytes: Option<usize>,
394 guard_size_in_bytes: usize,
395 ) -> Result<Box<dyn wasmtime::LinearMemory>, String> {
396 extern "C" fn panic_get_callback(
397 _env: *mut std::ffi::c_void,
398 _byte_size: &mut usize,
399 _maximum_byte_size: &mut usize,
400 ) -> *mut u8 {
401 panic!("a callback must be set");
402 }
403 extern "C" fn panic_grow_callback(
404 _env: *mut std::ffi::c_void,
405 _size: usize,
406 ) -> Option<Box<wasmtime_error_t>> {
407 panic!("a callback must be set");
408 }
409 let mut memory = wasmtime_linear_memory_t {
410 env: ptr::null_mut(),
411 get_memory: panic_get_callback,
412 grow_memory: panic_grow_callback,
413 finalizer: None,
414 };
415 let cb = self.new_memory;
416 let error = cb(
417 self.foreign.data,
418 &wasm_memorytype_t::new(ty),
419 minimum,
420 maximum.unwrap_or(usize::MAX),
421 reserved_size_in_bytes.unwrap_or(0),
422 guard_size_in_bytes,
423 &mut memory,
424 );
425 match error {
426 None => {
427 let foreign = crate::ForeignData {
428 data: memory.env,
429 finalizer: memory.finalizer,
430 };
431 Ok(Box::new(CHostLinearMemory {
432 foreign,
433 get_memory: memory.get_memory,
434 grow_memory: memory.grow_memory,
435 }))
436 }
437 Some(err) => {
438 let err: anyhow::Error = (*err).into();
439 Err(format!("{}", err))
440 }
441 }
442 }
443}
444
445#[no_mangle]
446pub unsafe extern "C" fn wasmtime_config_host_memory_creator_set(
447 c: &mut wasm_config_t,
448 creator: &wasmtime_memory_creator_t,
449) {
450 c.config.with_host_memory(Arc::new(CHostMemoryCreator {
451 foreign: crate::ForeignData {
452 data: creator.env,
453 finalizer: creator.finalizer,
454 },
455 new_memory: creator.new_memory,
456 }));
457}
458
459#[no_mangle]
460pub extern "C" fn wasmtime_config_memory_init_cow_set(c: &mut wasm_config_t, enable: bool) {
461 c.config.memory_init_cow(enable);
462}