polkavm_common/
abi.rs

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
416
417
418
419
420
421
422
//! Everything in this module affects the ABI of the guest programs, either by affecting
//! their observable behavior (no matter how obscure), or changing which programs are accepted by the VM.

use crate::utils::{align_to_next_page_u32, align_to_next_page_u64};
use core::ops::Range;

const ADDRESS_SPACE_SIZE: u64 = 0x100000000_u64;

/// The minimum page size of the VM.
pub const VM_MIN_PAGE_SIZE: u32 = 0x1000;

/// The maximum page size of the VM.
pub const VM_MAX_PAGE_SIZE: u32 = 0x10000;

static_assert!(VM_MIN_PAGE_SIZE <= VM_MAX_PAGE_SIZE);

/// The bottom of the accessible address space inside the VM.
const VM_ADDRESS_SPACE_BOTTOM: u32 = VM_MAX_PAGE_SIZE;

/// The top of the accessible address space inside the VM.
const VM_ADDRESS_SPACE_TOP: u32 = (ADDRESS_SPACE_SIZE - VM_MAX_PAGE_SIZE as u64) as u32;

/// The address which, when jumped to, will return to the host.
///
/// There isn't actually anything there; it's just a virtual address.
pub const VM_ADDR_RETURN_TO_HOST: u32 = 0xffff0000;
static_assert!(VM_ADDR_RETURN_TO_HOST & 0b11 == 0);

/// The maximum byte size of the code blob.
pub const VM_MAXIMUM_CODE_SIZE: u32 = 32 * 1024 * 1024;

/// The maximum number of entries in the jump table.
pub const VM_MAXIMUM_JUMP_TABLE_ENTRIES: u32 = 16 * 1024 * 1024;

/// The maximum number of functions the program can import.
pub const VM_MAXIMUM_IMPORT_COUNT: u32 = 1024;

/// The minimum required alignment of runtime code pointers.
pub const VM_CODE_ADDRESS_ALIGNMENT: u32 = 2;

#[derive(Clone)]
pub struct MemoryMapBuilder {
    page_size: u32,
    ro_data_size: u32,
    rw_data_size: u32,
    stack_size: u32,
    aux_data_size: u32,
}

impl MemoryMapBuilder {
    pub fn new(page_size: u32) -> Self {
        MemoryMapBuilder {
            page_size,
            ro_data_size: 0,
            rw_data_size: 0,
            stack_size: 0,
            aux_data_size: 0,
        }
    }

    pub fn ro_data_size(&mut self, value: u32) -> &mut Self {
        self.ro_data_size = value;
        self
    }

    pub fn rw_data_size(&mut self, value: u32) -> &mut Self {
        self.rw_data_size = value;
        self
    }

    pub fn stack_size(&mut self, value: u32) -> &mut Self {
        self.stack_size = value;
        self
    }

    pub fn aux_data_size(&mut self, value: u32) -> &mut Self {
        self.aux_data_size = value;
        self
    }

    pub fn build(&self) -> Result<MemoryMap, &'static str> {
        let MemoryMapBuilder {
            page_size,
            ro_data_size,
            rw_data_size,
            stack_size,
            aux_data_size,
        } = *self;

        if page_size < VM_MIN_PAGE_SIZE {
            return Err("invalid page size: page size is too small");
        }

        if page_size > VM_MAX_PAGE_SIZE {
            return Err("invalid page size: page size is too big");
        }

        if !page_size.is_power_of_two() {
            return Err("invalid page size: page size is not a power of two");
        }

        let Some(ro_data_address_space) = align_to_next_page_u64(u64::from(VM_MAX_PAGE_SIZE), u64::from(ro_data_size)) else {
            return Err("the size of read-only data is too big");
        };

        let Some(ro_data_size) = align_to_next_page_u32(page_size, ro_data_size) else {
            return Err("the size of read-only data is too big");
        };

        let Some(rw_data_address_space) = align_to_next_page_u64(u64::from(VM_MAX_PAGE_SIZE), u64::from(rw_data_size)) else {
            return Err("the size of read-write data is too big");
        };

        let original_rw_data_size = rw_data_size;
        let Some(rw_data_size) = align_to_next_page_u32(page_size, rw_data_size) else {
            return Err("the size of read-write data is too big");
        };

        let Some(stack_address_space) = align_to_next_page_u64(u64::from(VM_MAX_PAGE_SIZE), u64::from(stack_size)) else {
            return Err("the size of the stack is too big");
        };

        let Some(stack_size) = align_to_next_page_u32(page_size, stack_size) else {
            return Err("the size of the stack is too big");
        };

        let Some(aux_data_address_space) = align_to_next_page_u64(u64::from(VM_MAX_PAGE_SIZE), u64::from(aux_data_size)) else {
            return Err("the size of the aux data is too big");
        };

        let Some(aux_data_size) = align_to_next_page_u32(page_size, aux_data_size) else {
            return Err("the size of the aux data is too big");
        };

        let mut address_low: u64 = 0;

        address_low += u64::from(VM_ADDRESS_SPACE_BOTTOM);
        address_low += ro_data_address_space;
        address_low += u64::from(VM_MAX_PAGE_SIZE);

        let rw_data_address = address_low as u32;
        let heap_base = address_low + u64::from(original_rw_data_size);
        address_low += rw_data_address_space;
        let heap_slack = address_low - heap_base;
        address_low += u64::from(VM_MAX_PAGE_SIZE);

        let mut address_high: i64 = i64::from(VM_ADDRESS_SPACE_TOP);
        address_high -= aux_data_address_space as i64;
        let aux_data_address = address_high as u32;
        address_high -= i64::from(VM_MAX_PAGE_SIZE);
        let stack_address_high = address_high as u32;
        address_high -= stack_address_space as i64;

        if address_low as i64 > address_high {
            return Err("maximum memory size exceeded");
        }

        let max_heap_size = address_high as u64 - address_low + heap_slack;

        Ok(MemoryMap {
            page_size,
            ro_data_size,
            rw_data_address,
            rw_data_size,
            stack_address_high,
            stack_size,
            aux_data_address,
            aux_data_size,
            heap_base: heap_base as u32,
            max_heap_size: max_heap_size as u32,
        })
    }
}

/// The memory map of a given guest program.
#[derive(Clone)]
pub struct MemoryMap {
    page_size: u32,
    ro_data_size: u32,
    rw_data_address: u32,
    rw_data_size: u32,
    stack_address_high: u32,
    stack_size: u32,
    aux_data_address: u32,
    aux_data_size: u32,
    heap_base: u32,
    max_heap_size: u32,
}

impl MemoryMap {
    /// The page size of the program.
    #[inline]
    pub fn page_size(&self) -> u32 {
        self.page_size
    }

    /// The address at which the program's heap starts.
    #[inline]
    pub fn heap_base(&self) -> u32 {
        self.heap_base
    }

    /// The maximum size of the program's heap.
    #[inline]
    pub fn max_heap_size(&self) -> u32 {
        self.max_heap_size
    }

    /// The address at where the program's read-only data starts inside of the VM.
    #[inline]
    pub fn ro_data_address(&self) -> u32 {
        VM_ADDRESS_SPACE_BOTTOM
    }

    /// The size of the program's read-only data.
    #[inline]
    pub fn ro_data_size(&self) -> u32 {
        self.ro_data_size
    }

    /// The range of addresses where the program's read-only data is inside of the VM.
    #[inline]
    pub fn ro_data_range(&self) -> Range<u32> {
        self.ro_data_address()..self.ro_data_address() + self.ro_data_size()
    }

    /// The address at where the program's read-write data starts inside of the VM.
    #[inline]
    pub fn rw_data_address(&self) -> u32 {
        self.rw_data_address
    }

    /// The size of the program's read-write data.
    #[inline]
    pub fn rw_data_size(&self) -> u32 {
        self.rw_data_size
    }

    /// The range of addresses where the program's read-write data is inside of the VM.
    #[inline]
    pub fn rw_data_range(&self) -> Range<u32> {
        self.rw_data_address()..self.rw_data_address() + self.rw_data_size()
    }

    /// The address at where the program's stack starts inside of the VM.
    #[inline]
    pub fn stack_address_low(&self) -> u32 {
        self.stack_address_high() - self.stack_size
    }

    /// The address at where the program's stack ends inside of the VM.
    #[inline]
    pub fn stack_address_high(&self) -> u32 {
        self.stack_address_high
    }

    /// The size of the program's stack.
    #[inline]
    pub fn stack_size(&self) -> u32 {
        self.stack_size
    }

    /// The range of addresses where the program's stack is inside of the VM.
    #[inline]
    pub fn stack_range(&self) -> Range<u32> {
        self.stack_address_low()..self.stack_address_high()
    }

    #[inline]
    pub fn aux_data_address(&self) -> u32 {
        self.aux_data_address
    }

    #[inline]
    pub fn aux_data_size(&self) -> u32 {
        self.aux_data_size
    }

    #[inline]
    pub fn aux_data_range(&self) -> Range<u32> {
        self.aux_data_address()..self.aux_data_address() + self.aux_data_size()
    }
}

#[test]
fn test_memory_map() {
    {
        let map = MemoryMapBuilder::new(0x4000)
            .ro_data_size(1)
            .rw_data_size(1)
            .stack_size(1)
            .build()
            .unwrap();
        assert_eq!(map.ro_data_address(), 0x10000);
        assert_eq!(map.ro_data_size(), 0x4000);
        assert_eq!(map.rw_data_address(), 0x30000);
        assert_eq!(map.rw_data_size(), 0x4000);
        assert_eq!(map.stack_size(), 0x4000);
        assert_eq!(map.stack_address_high(), 0xfffe0000);
        assert_eq!(map.stack_address_low(), 0xfffdc000);

        assert_eq!(map.heap_base(), 0x30001);
        assert_eq!(
            u64::from(map.max_heap_size()),
            ADDRESS_SPACE_SIZE - u64::from(VM_MAX_PAGE_SIZE) * 4 - u64::from(map.heap_base())
        );
    }

    let max_size = (ADDRESS_SPACE_SIZE - u64::from(VM_MAX_PAGE_SIZE) * 5) as u32;

    {
        // Read-only data takes the whole address space.
        let map = MemoryMapBuilder::new(0x4000).ro_data_size(max_size).build().unwrap();
        assert_eq!(map.ro_data_address(), 0x10000);
        assert_eq!(map.ro_data_size(), max_size);
        assert_eq!(map.rw_data_address(), map.ro_data_address() + VM_MAX_PAGE_SIZE + max_size);
        assert_eq!(map.rw_data_size(), 0);
        assert_eq!(map.stack_address_high(), VM_ADDRESS_SPACE_TOP - VM_MAX_PAGE_SIZE);
        assert_eq!(map.stack_address_low(), VM_ADDRESS_SPACE_TOP - VM_MAX_PAGE_SIZE);
        assert_eq!(map.stack_size(), 0);

        assert_eq!(map.heap_base(), map.rw_data_address());
        assert_eq!(map.max_heap_size(), 0);
    }

    assert!(MemoryMapBuilder::new(0x4000).ro_data_size(max_size + 1).build().is_err());
    assert!(MemoryMapBuilder::new(0x4000)
        .ro_data_size(max_size)
        .rw_data_size(1)
        .build()
        .is_err());
    assert!(MemoryMapBuilder::new(0x4000).ro_data_size(max_size).stack_size(1).build().is_err());

    {
        // Read-write data takes the whole address space.
        let map = MemoryMapBuilder::new(0x4000).rw_data_size(max_size).build().unwrap();
        assert_eq!(map.ro_data_address(), VM_MAX_PAGE_SIZE);
        assert_eq!(map.ro_data_size(), 0);
        assert_eq!(map.rw_data_address(), VM_MAX_PAGE_SIZE * 2);
        assert_eq!(map.rw_data_size(), max_size);
        assert_eq!(map.stack_address_high(), VM_ADDRESS_SPACE_TOP - VM_MAX_PAGE_SIZE);
        assert_eq!(map.stack_address_low(), VM_ADDRESS_SPACE_TOP - VM_MAX_PAGE_SIZE);
        assert_eq!(map.stack_size(), 0);

        assert_eq!(map.heap_base(), map.rw_data_address() + map.rw_data_size());
        assert_eq!(map.max_heap_size(), 0);
    }

    {
        // Stack takes the whole address space.
        let map = MemoryMapBuilder::new(0x4000).stack_size(max_size).build().unwrap();
        assert_eq!(map.ro_data_address(), VM_MAX_PAGE_SIZE);
        assert_eq!(map.ro_data_size(), 0);
        assert_eq!(map.rw_data_address(), VM_MAX_PAGE_SIZE * 2);
        assert_eq!(map.rw_data_size(), 0);
        assert_eq!(map.stack_address_high(), VM_ADDRESS_SPACE_TOP - VM_MAX_PAGE_SIZE);
        assert_eq!(map.stack_address_low(), VM_ADDRESS_SPACE_TOP - VM_MAX_PAGE_SIZE - max_size);
        assert_eq!(map.stack_size(), max_size);

        assert_eq!(map.heap_base(), map.rw_data_address());
        assert_eq!(map.max_heap_size(), 0);
    }
}

#[cfg(kani)]
mod kani {
    use super::VM_MAX_PAGE_SIZE;
    use crate::utils::align_to_next_page_u64;

    #[kani::proof]
    fn memory_map() {
        let page_size: u32 = kani::any();
        let ro_data_size: u32 = kani::any();
        let rw_data_size: u32 = kani::any();
        let stack_size: u32 = kani::any();
        let aux_data_size: u32 = kani::any();
        kani::assume(page_size >= super::VM_MIN_PAGE_SIZE);
        kani::assume(page_size <= super::VM_MAX_PAGE_SIZE);
        kani::assume(page_size.is_power_of_two());

        let map = super::MemoryMapBuilder::new(page_size)
            .ro_data_size(ro_data_size)
            .rw_data_size(rw_data_size)
            .stack_size(stack_size)
            .aux_data_size(aux_data_size)
            .build();

        if let Ok(ref map) = map {
            assert_eq!(map.ro_data_address() % VM_MAX_PAGE_SIZE, 0);
            assert_eq!(map.rw_data_address() % VM_MAX_PAGE_SIZE, 0);
            assert_eq!(map.stack_address_high() % VM_MAX_PAGE_SIZE, 0);
            assert_eq!(map.aux_data_address() % VM_MAX_PAGE_SIZE, 0);

            assert_eq!(map.ro_data_address() % page_size, 0);
            assert_eq!(map.ro_data_range().end % page_size, 0);
            assert_eq!(map.rw_data_address() % page_size, 0);
            assert_eq!(map.rw_data_range().end % page_size, 0);
            assert_eq!(map.stack_address_high() % page_size, 0);
            assert_eq!(map.stack_address_low() % page_size, 0);
            assert_eq!(map.aux_data_address() % page_size, 0);
            assert_eq!(map.aux_data_range().end % page_size, 0);

            assert!(map.ro_data_address() < map.rw_data_address());
            assert!(map.rw_data_address() < map.stack_address_low());
            assert!(map.stack_address_low() <= map.stack_address_high());
            assert!(map.stack_address_high() < map.aux_data_address());

            assert!(map.rw_data_address() - map.ro_data_range().end >= VM_MAX_PAGE_SIZE);
            assert!(map.stack_address_low() - map.rw_data_range().end >= VM_MAX_PAGE_SIZE);
            assert!(map.aux_data_address() - map.stack_address_high() >= VM_MAX_PAGE_SIZE);
        }

        let total_size = align_to_next_page_u64(u64::from(VM_MAX_PAGE_SIZE), ro_data_size as u64).unwrap()
            + align_to_next_page_u64(u64::from(VM_MAX_PAGE_SIZE), rw_data_size as u64).unwrap()
            + align_to_next_page_u64(u64::from(VM_MAX_PAGE_SIZE), stack_size as u64).unwrap()
            + align_to_next_page_u64(u64::from(VM_MAX_PAGE_SIZE), aux_data_size as u64).unwrap();

        // [guard] ro_data [guard] rw_data [guard] stack [guard] aux [guard]
        let max_size = 0x100000000 - u64::from(VM_MAX_PAGE_SIZE) * 5;
        assert_eq!(map.is_err(), total_size > max_size);
    }
}