vmi-core 0.7.0

Core VMI library
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use isr_macros::Field;
use zerocopy::{FromBytes, Immutable, IntoBytes};

use super::session::VmiSession;
use crate::{
    AccessContext, AddressContext, Architecture, Pa, Registers as _, Va, VcpuId, VmiCore, VmiError,
    VmiRead, VmiWrite,
    driver::VmiSetRegisters,
    os::{NoOS, VmiOs},
};

/// A VMI state.
///
/// The state combines access to a [`VmiSession`] with [`Architecture::Registers`]
/// to provide unified access to VMI operations in the context of a specific
/// virtual machine state.
pub struct VmiState<'a, Os>
where
    Os: VmiOs,
{
    /// The VMI session.
    session: VmiSession<'a, Os>,

    /// The CPU registers associated with the current VM state.
    registers: &'a <Os::Architecture as Architecture>::Registers,
}

impl<Os> Clone for VmiState<'_, Os>
where
    Os: VmiOs,
{
    fn clone(&self) -> Self {
        *self
    }
}

impl<Os> Copy for VmiState<'_, Os> where Os: VmiOs {}

impl<'a, Os> std::ops::Deref for VmiState<'a, Os>
where
    Os: VmiOs,
{
    type Target = VmiSession<'a, Os>;

    fn deref(&self) -> &Self::Target {
        &self.session
    }
}

impl<'a, Os> VmiState<'a, Os>
where
    Os: VmiOs,
{
    /// Creates a new VMI state.
    pub fn new(
        session: &'a VmiSession<'a, Os>,
        registers: &'a <Os::Architecture as Architecture>::Registers,
    ) -> Self {
        Self {
            session: *session,
            registers,
        }
    }

    /// Creates a new VMI state with the specified registers.
    pub fn with_registers(
        &'a self,
        registers: &'a <Os::Architecture as Architecture>::Registers,
    ) -> Self {
        Self {
            session: self.session,
            registers,
        }
    }

    /// Creates a new VMI state without an OS-specific implementation.
    pub fn without_os(&self) -> VmiState<'a, NoOS<Os::Driver>> {
        VmiState {
            session: self.session.without_os(),
            registers: self.registers,
        }
    }

    // Note that `core()` and `underlying_os()` are delegated to the `VmiSession`.

    /// Returns the VMI session.
    pub fn session(&self) -> &VmiSession<'a, Os> {
        &self.session
    }

    /// Returns the CPU registers associated with the current event.
    pub fn registers(&self) -> &'a <Os::Architecture as Architecture>::Registers {
        self.registers
    }

    /// Returns a wrapper providing access to OS-specific operations.
    pub fn os(&self) -> VmiOsState<'a, Os> {
        VmiOsState(*self)
    }

    /// Creates an address context for a given virtual address.
    pub fn access_context(&self, address: Va) -> AccessContext {
        self.registers().access_context(address)
    }

    /// Creates an address context for a given virtual address.
    pub fn address_context(&self, address: Va) -> AddressContext {
        self.registers().address_context(address)
    }

    /// Returns the physical address of the root of the current page table
    /// hierarchy for a given virtual address.
    pub fn translation_root(&self, va: Va) -> Pa {
        self.registers().translation_root(va)
    }
}

///////////////////////////////////////////////////////////////////////////////
// VmiRead
///////////////////////////////////////////////////////////////////////////////

impl<'a, Os> VmiState<'a, Os>
where
    Os: VmiOs,
    Os::Driver: VmiRead,
{
    /// Returns the return address from the current stack frame.
    pub fn return_address(&self) -> Result<Va, VmiError> {
        self.registers().return_address(self.core())
    }

    /// Translates a virtual address to a physical address.
    pub fn translate_address(&self, va: Va) -> Result<Pa, VmiError> {
        self.core().translate_address(self.address_context(va))
    }

    // region: Read

    /// Reads memory from the virtual machine.
    pub fn read(&self, address: Va, buffer: &mut [u8]) -> Result<(), VmiError> {
        self.read_in(self.access_context(address), buffer)
    }

    /// Reads a single byte from the virtual machine.
    pub fn read_u8(&self, address: Va) -> Result<u8, VmiError> {
        self.read_u8_in(self.access_context(address))
    }

    /// Reads a 16-bit unsigned integer from the virtual machine.
    pub fn read_u16(&self, address: Va) -> Result<u16, VmiError> {
        self.read_u16_in(self.access_context(address))
    }

    /// Reads a 32-bit unsigned integer from the virtual machine.
    pub fn read_u32(&self, address: Va) -> Result<u32, VmiError> {
        self.read_u32_in(self.access_context(address))
    }

    /// Reads a 64-bit unsigned integer from the virtual machine.
    pub fn read_u64(&self, address: Va) -> Result<u64, VmiError> {
        self.read_u64_in(self.access_context(address))
    }

    /// Reads an unsigned integer of the specified size from the virtual machine.
    ///
    /// This method reads an unsigned integer of the specified size (in bytes)
    /// from the virtual machine. Note that the size must be 1, 2, 4, or 8.
    ///
    /// The result is returned as a [`u64`] to accommodate the widest possible
    /// integer size.
    pub fn read_uint(&self, address: Va, size: usize) -> Result<u64, VmiError> {
        self.read_uint_in(self.access_context(address), size)
    }

    /// Reads a field of a structure from the virtual machine.
    ///
    /// This method reads a field from the virtual machine. The field is
    /// defined by the provided [`Field`] structure, which specifies the
    /// offset and size of the field within the memory region.
    ///
    /// The result is returned as a [`u64`] to accommodate the widest possible
    /// integer size.
    pub fn read_field(&self, base_address: Va, field: &Field) -> Result<u64, VmiError> {
        self.read_field_in(self.access_context(base_address), field)
    }

    /// Reads an address-sized unsigned integer from the virtual machine.
    pub fn read_address(&self, address: Va) -> Result<u64, VmiError> {
        self.read_address_in(self.access_context(address))
    }

    /// Reads an address-sized unsigned integer from the virtual machine.
    pub fn read_address_native(&self, address: Va) -> Result<u64, VmiError> {
        self.read_address_native_in(self.access_context(address))
    }

    /// Reads a 32-bit address from the virtual machine.
    pub fn read_address32(&self, address: Va) -> Result<u64, VmiError> {
        self.read_address32_in(self.access_context(address))
    }

    /// Reads a 64-bit address from the virtual machine.
    pub fn read_address64(&self, address: Va) -> Result<u64, VmiError> {
        self.read_address64_in(self.access_context(address))
    }

    /// Reads a virtual address from the virtual machine.
    pub fn read_va(&self, address: Va) -> Result<Va, VmiError> {
        self.read_va_in(self.access_context(address))
    }

    /// Reads a virtual address from the virtual machine.
    pub fn read_va_native(&self, address: Va) -> Result<Va, VmiError> {
        self.read_va_native_in(self.access_context(address))
    }

    /// Reads a 32-bit virtual address from the virtual machine.
    pub fn read_va32(&self, address: Va) -> Result<Va, VmiError> {
        self.read_va32_in(self.access_context(address))
    }

    /// Reads a 64-bit virtual address from the virtual machine.
    pub fn read_va64(&self, address: Va) -> Result<Va, VmiError> {
        self.read_va64_in(self.access_context(address))
    }

    /// Reads a null-terminated string of bytes from the virtual machine with a
    /// specified limit.
    pub fn read_string_bytes_limited(
        &self,
        address: Va,
        limit: usize,
    ) -> Result<Vec<u8>, VmiError> {
        self.read_string_bytes_limited_in(self.access_context(address), limit)
    }

    /// Reads a null-terminated string of bytes from the virtual machine.
    pub fn read_string_bytes(&self, address: Va) -> Result<Vec<u8>, VmiError> {
        self.read_string_bytes_in(self.access_context(address))
    }

    /// Reads a null-terminated wide string (UTF-16) from the virtual machine
    /// with a specified limit.
    pub fn read_string_utf16_bytes_limited(
        &self,
        address: Va,
        limit: usize,
    ) -> Result<Vec<u16>, VmiError> {
        self.read_string_utf16_bytes_limited_in(self.access_context(address), limit)
    }

    /// Reads a null-terminated wide string (UTF-16) from the virtual machine.
    pub fn read_string_utf16_bytes(&self, address: Va) -> Result<Vec<u16>, VmiError> {
        self.read_string_utf16_bytes_in(self.access_context(address))
    }

    /// Reads a null-terminated string from the virtual machine with a specified
    /// limit.
    pub fn read_string_limited(&self, address: Va, limit: usize) -> Result<String, VmiError> {
        self.read_string_limited_in(self.access_context(address), limit)
    }

    /// Reads a null-terminated string from the virtual machine.
    pub fn read_string(&self, address: Va) -> Result<String, VmiError> {
        self.read_string_in(self.access_context(address))
    }

    /// Reads a null-terminated wide string (UTF-16) from the virtual machine
    /// with a specified limit.
    pub fn read_string_utf16_limited(&self, address: Va, limit: usize) -> Result<String, VmiError> {
        self.read_string_utf16_limited_in(self.access_context(address), limit)
    }

    /// Reads a null-terminated wide string (UTF-16) from the virtual machine.
    pub fn read_string_utf16(&self, address: Va) -> Result<String, VmiError> {
        self.read_string_utf16_in(self.access_context(address))
    }

    /// Reads a struct from the virtual machine.
    pub fn read_struct<T>(&self, address: Va) -> Result<T, VmiError>
    where
        T: IntoBytes + FromBytes,
    {
        self.read_struct_in(self.access_context(address))
    }

    // endregion: Read

    // region: Read in

    /// Reads memory from the virtual machine.
    pub fn read_in(
        &self,
        ctx: impl Into<AccessContext>,
        buffer: &mut [u8],
    ) -> Result<(), VmiError> {
        self.core().read(ctx, buffer)
    }

    /// Reads a single byte from the virtual machine.
    pub fn read_u8_in(&self, ctx: impl Into<AccessContext>) -> Result<u8, VmiError> {
        self.core().read_u8(ctx)
    }

    /// Reads a 16-bit unsigned integer from the virtual machine.
    pub fn read_u16_in(&self, ctx: impl Into<AccessContext>) -> Result<u16, VmiError> {
        self.core().read_u16(ctx)
    }

    /// Reads a 32-bit unsigned integer from the virtual machine.
    pub fn read_u32_in(&self, ctx: impl Into<AccessContext>) -> Result<u32, VmiError> {
        self.core().read_u32(ctx)
    }

    /// Reads a 64-bit unsigned integer from the virtual machine.
    pub fn read_u64_in(&self, ctx: impl Into<AccessContext>) -> Result<u64, VmiError> {
        self.core().read_u64(ctx)
    }

    /// Reads an unsigned integer of the specified size from the virtual machine.
    ///
    /// This method reads an unsigned integer of the specified size (in bytes)
    /// from the virtual machine. Note that the size must be 1, 2, 4, or 8.
    ///
    /// The result is returned as a [`u64`] to accommodate the widest possible
    /// integer size.
    pub fn read_uint_in(
        &self,
        ctx: impl Into<AccessContext>,
        size: usize,
    ) -> Result<u64, VmiError> {
        self.core().read_uint(ctx, size)
    }

    /// Reads a field of a structure from the virtual machine.
    ///
    /// This method reads a field from the virtual machine. The field is
    /// defined by the provided [`Field`] structure, which specifies the
    /// offset and size of the field within the memory region.
    ///
    /// The result is returned as a [`u64`] to accommodate the widest possible
    /// integer size.
    pub fn read_field_in(
        &self,
        ctx: impl Into<AccessContext>,
        field: &Field,
    ) -> Result<u64, VmiError> {
        self.core().read_field(ctx, field)
    }

    /// Reads an address-sized unsigned integer from the virtual machine.
    pub fn read_address_in(&self, ctx: impl Into<AccessContext>) -> Result<u64, VmiError> {
        self.core()
            .read_address(ctx, self.registers().effective_address_width())
    }

    /// Reads an address-sized unsigned integer from the virtual machine.
    pub fn read_address_native_in(&self, ctx: impl Into<AccessContext>) -> Result<u64, VmiError> {
        self.core()
            .read_address(ctx, self.registers().address_width())
    }

    /// Reads a 32-bit address from the virtual machine.
    pub fn read_address32_in(&self, ctx: impl Into<AccessContext>) -> Result<u64, VmiError> {
        self.core().read_address32(ctx)
    }

    /// Reads a 64-bit address from the virtual machine.
    pub fn read_address64_in(&self, ctx: impl Into<AccessContext>) -> Result<u64, VmiError> {
        self.core().read_address64(ctx)
    }

    /// Reads a virtual address from the virtual machine.
    pub fn read_va_in(&self, ctx: impl Into<AccessContext>) -> Result<Va, VmiError> {
        self.core()
            .read_va(ctx, self.registers().effective_address_width())
    }

    /// Reads a virtual address from the virtual machine.
    pub fn read_va_native_in(&self, ctx: impl Into<AccessContext>) -> Result<Va, VmiError> {
        self.core().read_va(ctx, self.registers().address_width())
    }

    /// Reads a 32-bit virtual address from the virtual machine.
    pub fn read_va32_in(&self, ctx: impl Into<AccessContext>) -> Result<Va, VmiError> {
        self.core().read_va32(ctx)
    }

    /// Reads a 64-bit virtual address from the virtual machine.
    pub fn read_va64_in(&self, ctx: impl Into<AccessContext>) -> Result<Va, VmiError> {
        self.core().read_va64(ctx)
    }

    /// Reads a null-terminated string of bytes from the virtual machine with a
    /// specified limit.
    pub fn read_string_bytes_limited_in(
        &self,
        ctx: impl Into<AccessContext>,
        limit: usize,
    ) -> Result<Vec<u8>, VmiError> {
        self.core().read_string_bytes_limited(ctx, limit)
    }

    /// Reads a null-terminated string of bytes from the virtual machine.
    pub fn read_string_bytes_in(&self, ctx: impl Into<AccessContext>) -> Result<Vec<u8>, VmiError> {
        self.core().read_string_bytes(ctx)
    }

    /// Reads a null-terminated wide string (UTF-16) from the virtual machine
    /// with a specified limit.
    pub fn read_string_utf16_bytes_limited_in(
        &self,
        ctx: impl Into<AccessContext>,
        limit: usize,
    ) -> Result<Vec<u16>, VmiError> {
        self.core().read_string_utf16_bytes_limited(ctx, limit)
    }

    /// Reads a null-terminated wide string (UTF-16) from the virtual machine.
    pub fn read_string_utf16_bytes_in(
        &self,
        ctx: impl Into<AccessContext>,
    ) -> Result<Vec<u16>, VmiError> {
        self.core().read_string_utf16_bytes(ctx)
    }

    /// Reads a null-terminated string from the virtual machine with a specified
    /// limit.
    pub fn read_string_limited_in(
        &self,
        ctx: impl Into<AccessContext>,
        limit: usize,
    ) -> Result<String, VmiError> {
        self.core().read_string_limited(ctx, limit)
    }

    /// Reads a null-terminated string from the virtual machine.
    pub fn read_string_in(&self, ctx: impl Into<AccessContext>) -> Result<String, VmiError> {
        self.core().read_string(ctx)
    }

    /// Reads a null-terminated wide string (UTF-16) from the virtual machine
    /// with a specified limit.
    pub fn read_string_utf16_limited_in(
        &self,
        ctx: impl Into<AccessContext>,
        limit: usize,
    ) -> Result<String, VmiError> {
        self.core().read_string_utf16_limited(ctx, limit)
    }

    /// Reads a null-terminated wide string (UTF-16) from the virtual machine.
    pub fn read_string_utf16_in(&self, ctx: impl Into<AccessContext>) -> Result<String, VmiError> {
        self.core().read_string_utf16(ctx)
    }

    /// Reads a struct from the virtual machine.
    pub fn read_struct_in<T>(&self, ctx: impl Into<AccessContext>) -> Result<T, VmiError>
    where
        T: IntoBytes + FromBytes,
    {
        self.core().read_struct(ctx)
    }

    // endregion: Read in
}

///////////////////////////////////////////////////////////////////////////////
// VmiRead + VmiWrite
///////////////////////////////////////////////////////////////////////////////

impl<'a, Os> VmiState<'a, Os>
where
    Os: VmiOs,
    Os::Driver: VmiRead + VmiWrite,
{
    /// Writes memory to the virtual machine.
    pub fn write(&self, address: Va, buffer: &[u8]) -> Result<(), VmiError> {
        self.write_in(self.access_context(address), buffer)
    }

    /// Writes memory to the virtual machine.
    pub fn write_in(&self, ctx: impl Into<AccessContext>, buffer: &[u8]) -> Result<(), VmiError> {
        self.core().write(ctx, buffer)
    }

    /// Writes a single byte to the virtual machine.
    pub fn write_u8(&self, address: Va, value: u8) -> Result<(), VmiError> {
        self.core().write_u8(self.access_context(address), value)
    }

    /// Writes a 16-bit unsigned integer to the virtual machine.
    pub fn write_u16(&self, address: Va, value: u16) -> Result<(), VmiError> {
        self.core().write_u16(self.access_context(address), value)
    }

    /// Writes a 32-bit unsigned integer to the virtual machine.
    pub fn write_u32(&self, address: Va, value: u32) -> Result<(), VmiError> {
        self.core().write_u32(self.access_context(address), value)
    }

    /// Writes a 64-bit unsigned integer to the virtual machine.
    pub fn write_u64(&self, address: Va, value: u64) -> Result<(), VmiError> {
        self.core().write_u64(self.access_context(address), value)
    }

    /// Writes a struct to the virtual machine.
    pub fn write_struct<T>(&self, address: Va, value: T) -> Result<(), VmiError>
    where
        T: FromBytes + IntoBytes + Immutable,
    {
        self.core()
            .write_struct(self.access_context(address), value)
    }
}

///////////////////////////////////////////////////////////////////////////////
// VmiSetRegisters
///////////////////////////////////////////////////////////////////////////////

impl<'a, Os> VmiState<'a, Os>
where
    Os: VmiOs,
    Os::Driver: VmiSetRegisters,
{
    /// Sets the registers of a virtual CPU.
    pub fn set_registers(
        &self,
        vcpu: VcpuId,
        registers: <Os::Architecture as Architecture>::Registers,
    ) -> Result<(), VmiError> {
        self.core().set_registers(vcpu, registers)
    }
}

/// Wrapper providing access to OS-specific operations.
pub struct VmiOsState<'a, Os>(VmiState<'a, Os>)
where
    Os: VmiOs;

impl<'a, Os> VmiOsState<'a, Os>
where
    Os: VmiOs,
{
    /// Returns the VMI core.
    pub fn core(&self) -> &'a VmiCore<Os::Driver> {
        self.0.core()
    }

    /// Returns the underlying OS-specific implementation.
    pub fn underlying_os(&self) -> &'a Os {
        self.0.underlying_os()
    }

    /// Returns the VMI session.
    pub fn session(&self) -> &VmiSession<'a, Os> {
        self.0.session()
    }

    /// Returns the VMI state.
    pub fn state(&self) -> VmiState<'a, Os> {
        self.0
    }

    /// Returns the CPU registers associated with the current event.
    pub fn registers(&self) -> &<Os::Architecture as Architecture>::Registers {
        self.0.registers()
    }

    /// Retrieves a specific function argument according to the calling
    /// convention of the operating system.
    pub fn function_argument_for_registers(
        &self,
        registers: &<Os::Architecture as Architecture>::Registers,
        index: u64,
    ) -> Result<u64, VmiError> {
        Os::function_argument(self.0.with_registers(registers), index)
    }

    /// Retrieves the return value of a function.
    pub fn function_return_value_for_registers(
        &self,
        registers: &<Os::Architecture as Architecture>::Registers,
    ) -> Result<u64, VmiError> {
        Os::function_return_value(self.0.with_registers(registers))
    }
}