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
use std::collections::BTreeMap;

use crate::*;

/// WASI context
pub struct Wasi {
    /// wasi
    pub ctx: wasmtime_wasi::WasiCtx,

    /// wasi-nn
    #[cfg(feature = "nn")]
    pub nn: wasmtime_wasi_nn::WasiNnCtx,
}

/// Internal stores data that is available to the caller in PDK functions
pub struct Internal {
    /// Store
    pub store: *mut Store<Internal>,

    /// Linker
    pub linker: *mut wasmtime::Linker<Internal>,

    /// WASI context
    pub wasi: Option<Wasi>,

    /// Keep track of the status from the last HTTP request
    pub http_status: u16,

    /// Plugin variables
    pub vars: BTreeMap<String, Vec<u8>>,

    pub manifest: Manifest,

    pub available_pages: Option<u32>,

    pub(crate) memory_limiter: Option<MemoryLimiter>,
}

/// InternalExt provides a unified way of acessing `memory`, `store` and `internal` values
pub trait InternalExt {
    fn store(&self) -> &Store<Internal>;

    fn store_mut(&mut self) -> &mut Store<Internal>;

    fn linker(&self) -> &Linker<Internal>;

    fn linker_mut(&mut self) -> &mut Linker<Internal>;

    fn linker_and_store(&mut self) -> (&mut Linker<Internal>, &mut Store<Internal>);

    fn internal(&self) -> &Internal {
        self.store().data()
    }

    fn internal_mut(&mut self) -> &mut Internal {
        self.store_mut().data_mut()
    }

    fn memory_ptr(&mut self) -> *mut u8 {
        let (linker, mut store) = self.linker_and_store();
        if let Some(mem) = linker.get(&mut store, "env", "memory") {
            if let Some(mem) = mem.into_memory() {
                return mem.data_ptr(&mut store);
            }
        }

        std::ptr::null_mut()
    }

    fn memory(&mut self) -> &mut [u8] {
        let (linker, mut store) = self.linker_and_store();
        let mem = linker
            .get(&mut store, "env", "memory")
            .unwrap()
            .into_memory()
            .unwrap();
        let ptr = mem.data_ptr(&store);
        if ptr.is_null() {
            return &mut [];
        }
        let size = mem.data_size(&store);
        unsafe { std::slice::from_raw_parts_mut(ptr, size) }
    }

    fn memory_read(&mut self, offs: u64, len: Size) -> &[u8] {
        trace!("memory_read: {}, {}", offs, len);
        let offs = offs as usize;
        let len = len as usize;
        let mem = self.memory();
        &mem[offs..offs + len]
    }

    fn memory_read_str(&mut self, offs: u64) -> Result<&str, std::str::Utf8Error> {
        let len = self.memory_length(offs);
        std::str::from_utf8(self.memory_read(offs, len))
    }

    fn memory_write(&mut self, offs: u64, bytes: impl AsRef<[u8]>) {
        trace!("memory_write: {}", offs);
        let b = bytes.as_ref();
        let offs = offs as usize;
        let len = b.len();
        self.memory()[offs..offs + len].copy_from_slice(bytes.as_ref());
    }

    fn memory_alloc(&mut self, n: Size) -> Result<u64, Error> {
        if n == 0 {
            return Ok(0);
        }
        let (linker, mut store) = self.linker_and_store();
        let output = &mut [Val::I64(0)];
        linker
            .get(&mut store, "env", "extism_alloc")
            .unwrap()
            .into_func()
            .unwrap()
            .call(&mut store, &[Val::I64(n as i64)], output)?;
        let offs = output[0].unwrap_i64() as u64;
        if offs == 0 && n > 0 {
            anyhow::bail!("out of memory")
        }
        trace!("memory_alloc: {}, {}", offs, n);
        Ok(offs)
    }

    fn memory_alloc_bytes(&mut self, bytes: impl AsRef<[u8]>) -> Result<u64, Error> {
        let b = bytes.as_ref();
        let offs = self.memory_alloc(b.len() as Size)?;
        self.memory_write(offs, b);
        Ok(offs)
    }

    fn memory_free(&mut self, offs: u64) {
        let (linker, mut store) = self.linker_and_store();
        linker
            .get(&mut store, "env", "extism_free")
            .unwrap()
            .into_func()
            .unwrap()
            .call(&mut store, &[Val::I64(offs as i64)], &mut [])
            .unwrap();
    }

    fn memory_length(&mut self, offs: u64) -> u64 {
        let (linker, mut store) = self.linker_and_store();
        let output = &mut [Val::I64(0)];
        linker
            .get(&mut store, "env", "extism_length")
            .unwrap()
            .into_func()
            .unwrap()
            .call(&mut store, &[Val::I64(offs as i64)], output)
            .unwrap();
        let len = output[0].unwrap_i64() as u64;
        trace!("memory_length: {}, {}", offs, len);
        len
    }

    // A convenience method to set the plugin error and return a value
    fn error<E>(&mut self, e: impl std::fmt::Debug, x: E) -> E {
        let s = format!("{e:?}");
        debug!("Set error: {:?}", s);
        if let Ok(offs) = self.memory_alloc_bytes(&s) {
            let (linker, mut store) = self.linker_and_store();
            if let Some(f) = linker.get(&mut store, "env", "extism_error_set") {
                f.into_func()
                    .unwrap()
                    .call(&mut store, &[Val::I64(offs as i64)], &mut [])
                    .unwrap();
            }
        }
        x
    }

    fn clear_error(&mut self) {
        let (linker, mut store) = self.linker_and_store();
        if let Some(f) = linker.get(&mut store, "env", "extism_error_set") {
            f.into_func()
                .unwrap()
                .call(&mut store, &[Val::I64(0)], &mut [])
                .unwrap();
        }
    }

    fn has_error(&mut self) -> bool {
        let (linker, mut store) = self.linker_and_store();
        let output = &mut [Val::I64(0)];
        linker
            .get(&mut store, "env", "extism_error_get")
            .unwrap()
            .into_func()
            .unwrap()
            .call(&mut store, &[], output)
            .unwrap();
        output[0].unwrap_i64() != 0
    }

    fn get_error(&mut self) -> Option<&str> {
        let (linker, mut store) = self.linker_and_store();
        let output = &mut [Val::I64(0)];
        linker
            .get(&mut store, "env", "extism_error_get")
            .unwrap()
            .into_func()
            .unwrap()
            .call(&mut store, &[], output)
            .unwrap();
        let offs = output[0].unwrap_i64() as u64;
        if offs == 0 {
            return None;
        }

        let length = self.memory_length(offs);
        let data = self.memory_read(offs, length);
        let s = std::str::from_utf8(data);
        match s {
            Ok(s) => Some(s),
            Err(_) => None,
        }
    }
}

impl Internal {
    pub(crate) fn new(
        manifest: Manifest,
        wasi: bool,
        available_pages: Option<u32>,
    ) -> Result<Self, Error> {
        let wasi = if wasi {
            let auth = wasmtime_wasi::ambient_authority();
            let mut ctx = wasmtime_wasi::WasiCtxBuilder::new();
            for (k, v) in manifest.as_ref().config.iter() {
                ctx = ctx.env(k, v)?;
            }

            if let Some(a) = &manifest.as_ref().allowed_paths {
                for (k, v) in a.iter() {
                    let d = wasmtime_wasi::Dir::open_ambient_dir(k, auth)?;
                    ctx = ctx.preopened_dir(d, v)?;
                }
            }

            #[cfg(feature = "nn")]
            let nn = wasmtime_wasi_nn::WasiNnCtx::new()?;

            Some(Wasi {
                ctx: ctx.build(),
                #[cfg(feature = "nn")]
                nn,
            })
        } else {
            None
        };

        let memory_limiter = if let Some(pgs) = available_pages {
            let n = pgs as usize * 65536;
            Some(MemoryLimiter {
                max_bytes: n,
                bytes_left: n,
            })
        } else {
            None
        };

        Ok(Internal {
            wasi,
            manifest,
            http_status: 0,
            vars: BTreeMap::new(),
            linker: std::ptr::null_mut(),
            store: std::ptr::null_mut(),
            available_pages,
            memory_limiter,
        })
    }

    pub fn linker(&self) -> &wasmtime::Linker<Internal> {
        unsafe { &*self.linker }
    }

    pub fn linker_mut(&mut self) -> &mut wasmtime::Linker<Internal> {
        unsafe { &mut *self.linker }
    }
}

impl InternalExt for Internal {
    fn store(&self) -> &Store<Internal> {
        unsafe { &*self.store }
    }

    fn store_mut(&mut self) -> &mut Store<Internal> {
        unsafe { &mut *self.store }
    }

    fn linker(&self) -> &Linker<Internal> {
        unsafe { &*self.linker }
    }

    fn linker_mut(&mut self) -> &mut Linker<Internal> {
        unsafe { &mut *self.linker }
    }

    fn linker_and_store(&mut self) -> (&mut Linker<Internal>, &mut Store<Internal>) {
        unsafe { (&mut *self.linker, &mut *self.store) }
    }
}

pub(crate) struct MemoryLimiter {
    bytes_left: usize,
    max_bytes: usize,
}

impl MemoryLimiter {
    pub(crate) fn reset(&mut self) {
        self.bytes_left = self.max_bytes;
    }
}

impl wasmtime::ResourceLimiter for MemoryLimiter {
    fn memory_growing(
        &mut self,
        current: usize,
        desired: usize,
        maximum: Option<usize>,
    ) -> Result<bool> {
        if let Some(max) = maximum {
            if desired > max {
                return Ok(false);
            }
        }

        let d = desired - current;
        if d > self.bytes_left {
            return Ok(false);
        }

        self.bytes_left -= d;
        Ok(true)
    }

    fn table_growing(&mut self, _current: u32, desired: u32, maximum: Option<u32>) -> Result<bool> {
        if let Some(max) = maximum {
            return Ok(desired <= max);
        }

        Ok(true)
    }
}