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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Offsets and sizes of various structs in wasmtime-execute's vmcontext
//! module.

use cranelift_codegen::ir;
use cranelift_wasm::{
    DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, GlobalIndex, MemoryIndex,
    TableIndex,
};

/// This class computes offsets to fields within `VMContext` and other
/// related structs that JIT code accesses directly.
pub struct VMOffsets {
    pointer_size: u8,
}

impl VMOffsets {
    /// Return a new `VMOffsets` instance, for a given pointer size.
    pub fn new(pointer_size: u8) -> Self {
        Self { pointer_size }
    }
}

/// Offsets for `VMFunctionImport`.
impl VMOffsets {
    /// The offset of the `body` field.
    #[allow(clippy::erasing_op)]
    pub fn vmfunction_import_body(&self) -> u8 {
        0 * self.pointer_size
    }

    /// The offset of the `vmctx` field.
    #[allow(clippy::identity_op)]
    pub fn vmfunction_import_vmctx(&self) -> u8 {
        1 * self.pointer_size
    }

    /// Return the size of `VMFunctionImport`.
    pub fn size_of_vmfunction_import(&self) -> u8 {
        2 * self.pointer_size
    }
}

/// Offsets for `*const VMFunctionBody`.
impl VMOffsets {
    /// The size of the `current_elements` field.
    #[allow(clippy::identity_op)]
    pub fn size_of_vmfunction_body_ptr(&self) -> u8 {
        1 * self.pointer_size
    }
}

/// Offsets for `VMTableImport`.
impl VMOffsets {
    /// The offset of the `from` field.
    #[allow(clippy::erasing_op)]
    pub fn vmtable_import_from(&self) -> u8 {
        0 * self.pointer_size
    }

    /// The offset of the `vmctx` field.
    #[allow(clippy::identity_op)]
    pub fn vmtable_import_vmctx(&self) -> u8 {
        1 * self.pointer_size
    }

    /// Return the size of `VMTableImport`.
    pub fn size_of_vmtable_import(&self) -> u8 {
        2 * self.pointer_size
    }
}

/// Offsets for `VMTableDefinition`.
impl VMOffsets {
    /// The offset of the `base` field.
    #[allow(clippy::erasing_op)]
    pub fn vmtable_definition_base(&self) -> u8 {
        0 * self.pointer_size
    }

    /// The offset of the `current_elements` field.
    #[allow(clippy::identity_op)]
    pub fn vmtable_definition_current_elements(&self) -> u8 {
        1 * self.pointer_size
    }

    /// The size of the `current_elements` field.
    pub fn size_of_vmtable_definition_current_elements(&self) -> u8 {
        4
    }

    /// Return the size of `VMTableDefinition`.
    pub fn size_of_vmtable_definition(&self) -> u8 {
        2 * self.pointer_size
    }

    /// The type of the `current_elements` field.
    pub fn type_of_vmtable_definition_current_elements(&self) -> ir::Type {
        ir::Type::int(u16::from(self.size_of_vmtable_definition_current_elements()) * 8).unwrap()
    }
}

/// Offsets for `VMMemoryImport`.
impl VMOffsets {
    /// The offset of the `from` field.
    #[allow(clippy::erasing_op)]
    pub fn vmmemory_import_from(&self) -> u8 {
        0 * self.pointer_size
    }

    /// The offset of the `vmctx` field.
    #[allow(clippy::identity_op)]
    pub fn vmmemory_import_vmctx(&self) -> u8 {
        1 * self.pointer_size
    }

    /// Return the size of `VMMemoryImport`.
    pub fn size_of_vmmemory_import(&self) -> u8 {
        2 * self.pointer_size
    }
}

/// Offsets for `VMMemoryDefinition`.
impl VMOffsets {
    /// The offset of the `base` field.
    #[allow(clippy::erasing_op)]
    pub fn vmmemory_definition_base(&self) -> u8 {
        0 * self.pointer_size
    }

    /// The offset of the `current_length` field.
    #[allow(clippy::identity_op)]
    pub fn vmmemory_definition_current_length(&self) -> u8 {
        1 * self.pointer_size
    }

    /// The size of the `current_length` field.
    pub fn size_of_vmmemory_definition_current_length(&self) -> u8 {
        4
    }

    /// Return the size of `VMMemoryDefinition`.
    pub fn size_of_vmmemory_definition(&self) -> u8 {
        2 * self.pointer_size
    }

    /// The type of the `current_length` field.
    pub fn type_of_vmmemory_definition_current_length(&self) -> ir::Type {
        ir::Type::int(u16::from(self.size_of_vmmemory_definition_current_length()) * 8).unwrap()
    }
}

/// Offsets for `VMGlobalImport`.
impl VMOffsets {
    /// The offset of the `from` field.
    #[allow(clippy::erasing_op)]
    pub fn vmglobal_import_from(&self) -> u8 {
        0 * self.pointer_size
    }

    /// Return the size of `VMGlobalImport`.
    #[allow(clippy::identity_op)]
    pub fn size_of_vmglobal_import(&self) -> u8 {
        1 * self.pointer_size
    }
}

/// Offsets for `VMGlobalDefinition`.
impl VMOffsets {
    /// Return the size of `VMGlobalDefinition`.
    pub fn size_of_vmglobal_definition(&self) -> u8 {
        8
    }
}

/// Offsets for `VMSharedSignatureIndex`.
impl VMOffsets {
    /// Return the size of `VMSharedSignatureIndex`.
    pub fn size_of_vmshared_signature_index(&self) -> u8 {
        4
    }
}

/// Offsets for `VMCallerCheckedAnyfunc`.
impl VMOffsets {
    /// The offset of the `func_ptr` field.
    #[allow(clippy::erasing_op)]
    pub fn vmcaller_checked_anyfunc_func_ptr(&self) -> u8 {
        0 * self.pointer_size
    }

    /// The offset of the `type_index` field.
    #[allow(clippy::identity_op)]
    pub fn vmcaller_checked_anyfunc_type_index(&self) -> u8 {
        1 * self.pointer_size
    }

    /// The offset of the `vmctx` field.
    pub fn vmcaller_checked_anyfunc_vmctx(&self) -> u8 {
        2 * self.pointer_size
    }

    /// Return the size of `VMCallerCheckedAnyfunc`.
    pub fn size_of_vmcaller_checked_anyfunc(&self) -> u8 {
        3 * self.pointer_size
    }
}

/// Offsets for `VMContext`.
impl VMOffsets {
    /// The offset of the `tables` field.
    #[allow(clippy::erasing_op)]
    pub fn vmctx_imported_functions(&self) -> u8 {
        0 * self.pointer_size
    }

    /// The offset of the `tables` field.
    #[allow(clippy::identity_op)]
    pub fn vmctx_imported_tables(&self) -> u8 {
        1 * self.pointer_size
    }

    /// The offset of the `memories` field.
    pub fn vmctx_imported_memories(&self) -> u8 {
        2 * self.pointer_size
    }

    /// The offset of the `globals` field.
    pub fn vmctx_imported_globals(&self) -> u8 {
        3 * self.pointer_size
    }

    /// The offset of the `tables` field.
    pub fn vmctx_tables(&self) -> u8 {
        4 * self.pointer_size
    }

    /// The offset of the `memories` field.
    pub fn vmctx_memories(&self) -> u8 {
        5 * self.pointer_size
    }

    /// The offset of the `globals` field.
    pub fn vmctx_globals(&self) -> u8 {
        6 * self.pointer_size
    }

    /// The offset of the `signature_ids` field.
    pub fn vmctx_signature_ids(&self) -> u8 {
        7 * self.pointer_size
    }

    /// Return the size of `VMContext`.
    #[allow(dead_code)]
    pub fn size_of_vmctx(&self) -> u8 {
        8 * self.pointer_size
    }

    /// Return the offset from the `imported_functions` pointer to `VMFunctionImport` index `index`.
    fn index_vmfunction_import(&self, index: FuncIndex) -> i32 {
        cast::i32(
            index
                .as_u32()
                .checked_mul(u32::from(self.size_of_vmfunction_import()))
                .unwrap(),
        )
        .unwrap()
    }

    /// Return the offset from the `imported_tables` pointer to `VMTableImport` index `index`.
    fn index_vmtable_import(&self, index: TableIndex) -> i32 {
        cast::i32(
            index
                .as_u32()
                .checked_mul(u32::from(self.size_of_vmtable_import()))
                .unwrap(),
        )
        .unwrap()
    }

    /// Return the offset from the `tables` pointer to `VMTableDefinition` index `index`.
    fn index_vmtable_definition(&self, index: DefinedTableIndex) -> i32 {
        cast::i32(
            index
                .as_u32()
                .checked_mul(u32::from(self.size_of_vmtable_definition()))
                .unwrap(),
        )
        .unwrap()
    }

    /// Return the offset from the `imported_memories` pointer to `VMMemoryImport` index `index`.
    fn index_vmmemory_import(&self, index: MemoryIndex) -> i32 {
        cast::i32(
            index
                .as_u32()
                .checked_mul(u32::from(self.size_of_vmmemory_import()))
                .unwrap(),
        )
        .unwrap()
    }

    /// Return the offset from the `memories` pointer to `VMMemoryDefinition` index `index`.
    fn index_vmmemory_definition(&self, index: DefinedMemoryIndex) -> i32 {
        cast::i32(
            index
                .as_u32()
                .checked_mul(u32::from(self.size_of_vmmemory_definition()))
                .unwrap(),
        )
        .unwrap()
    }

    /// Return the offset from the `imported_globals` pointer to `VMGlobalImport` index `index`.
    fn index_vmglobal_import(&self, index: GlobalIndex) -> i32 {
        cast::i32(
            index
                .as_u32()
                .checked_mul(u32::from(self.size_of_vmglobal_import()))
                .unwrap(),
        )
        .unwrap()
    }

    /// Return the offset from the `imported_functions` pointer to the
    /// `body` field in `*const VMFunctionBody` index `index`.
    pub fn index_vmfunction_import_body(&self, index: FuncIndex) -> i32 {
        self.index_vmfunction_import(index)
            .checked_add(i32::from(self.vmfunction_import_body()))
            .unwrap()
    }

    /// Return the offset from the `imported_functions` pointer to the
    /// `vmctx` field in `*const VMFunctionBody` index `index`.
    pub fn index_vmfunction_import_vmctx(&self, index: FuncIndex) -> i32 {
        self.index_vmfunction_import(index)
            .checked_add(i32::from(self.vmfunction_import_vmctx()))
            .unwrap()
    }

    /// Return the offset from the `tables` pointer to the `from` field in
    /// `VMTableImport` index `index`.
    pub fn index_vmtable_import_from(&self, index: TableIndex) -> i32 {
        self.index_vmtable_import(index)
            .checked_add(i32::from(self.vmtable_import_from()))
            .unwrap()
    }

    /// Return the offset from the `tables` pointer to the `base` field in
    /// `VMTableDefinition` index `index`.
    pub fn index_vmtable_definition_base(&self, index: DefinedTableIndex) -> i32 {
        self.index_vmtable_definition(index)
            .checked_add(i32::from(self.vmtable_definition_base()))
            .unwrap()
    }

    /// Return the offset from the `tables` pointer to the `current_elements` field in
    /// `VMTableDefinition` index `index`.
    pub fn index_vmtable_definition_current_elements(&self, index: DefinedTableIndex) -> i32 {
        self.index_vmtable_definition(index)
            .checked_add(i32::from(self.vmtable_definition_current_elements()))
            .unwrap()
    }

    /// Return the offset from the `memories` pointer to the `from` field in
    /// `VMMemoryImport` index `index`.
    pub fn index_vmmemory_import_from(&self, index: MemoryIndex) -> i32 {
        self.index_vmmemory_import(index)
            .checked_add(i32::from(self.vmmemory_import_from()))
            .unwrap()
    }

    /// Return the offset from the `memories` pointer to the `vmctx` field in
    /// `VMMemoryImport` index `index`.
    pub fn index_vmmemory_import_vmctx(&self, index: MemoryIndex) -> i32 {
        self.index_vmmemory_import(index)
            .checked_add(i32::from(self.vmmemory_import_vmctx()))
            .unwrap()
    }

    /// Return the offset from the `memories` pointer to the `base` field in
    /// `VMMemoryDefinition` index `index`.
    pub fn index_vmmemory_definition_base(&self, index: DefinedMemoryIndex) -> i32 {
        self.index_vmmemory_definition(index)
            .checked_add(i32::from(self.vmmemory_definition_base()))
            .unwrap()
    }

    /// Return the offset from the `memories` pointer to the `current_length` field in
    /// `VMMemoryDefinition` index `index`.
    pub fn index_vmmemory_definition_current_length(&self, index: DefinedMemoryIndex) -> i32 {
        self.index_vmmemory_definition(index)
            .checked_add(i32::from(self.vmmemory_definition_current_length()))
            .unwrap()
    }

    /// Return the offset from the `imported_globals` pointer to the `from` field in
    /// `VMGlobalImport` index `index`.
    pub fn index_vmglobal_import_from(&self, index: GlobalIndex) -> i32 {
        self.index_vmglobal_import(index)
            .checked_add(i32::from(self.vmglobal_import_from()))
            .unwrap()
    }

    /// Return the offset from the `globals` pointer to the `VMGlobalDefinition`
    /// index `index`.
    pub fn index_vmglobal_definition(&self, index: DefinedGlobalIndex) -> i32 {
        cast::i32(
            index
                .as_u32()
                .checked_mul(u32::from(self.size_of_vmglobal_definition()))
                .unwrap(),
        )
        .unwrap()
    }
}