dotscope 0.6.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
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
//! Copy-on-Write memory page implementation.
//!
//! This module provides [`Page`], a memory page with transparent copy-on-write
//! semantics and interior mutability for thread-safe concurrent access.
//!
//! # CoW Semantics
//!
//! Each page has an immutable backing buffer (shared via `Arc`) and an optional
//! local buffer for modifications. On first write, the backing is copied to
//! the local buffer, and subsequent reads/writes use the local copy.
//!
//! # Thread Safety
//!
//! The page uses `RwLock` to allow concurrent reads and exclusive writes.
//! All operations take `&self` for interior mutability.
//!
//! # Fork Support
//!
//! When forking, the current state (backing + local) is consolidated into
//! a new backing, and the forked page starts with a fresh local buffer.

use std::sync::{Arc, RwLock, RwLockReadGuard};

use crate::emulation::engine::EmulationError;

/// Standard page size (4KB).
pub const PAGE_SIZE: usize = 4096;

/// A memory page with copy-on-write semantics.
///
/// Pages are the fundamental unit of memory management in the emulator.
/// They support transparent CoW - reads return data from either the local
/// copy (if modified) or the shared backing, while writes automatically
/// create a local copy on first modification.
///
/// # Thread Safety
///
/// All operations use interior mutability via `RwLock`. Multiple threads
/// can read concurrently, while writes acquire exclusive access.
///
/// # Example
///
/// ```rust,ignore
/// use dotscope::emulation::Page;
///
/// // Create a page with some data
/// let mut data = [0u8; 4096];
/// data[0] = 42;
/// let page = Page::new(data);
///
/// // Read returns the backing data
/// assert_eq!(page.read_byte(0)?, 42);
///
/// // First write triggers CoW
/// page.write_byte(0, 100)?;
/// assert_eq!(page.read_byte(0)?, 100);
///
/// // Fork creates an independent copy
/// let forked = page.fork()?;
/// forked.write_byte(0, 200)?;
/// assert_eq!(page.read_byte(0)?, 100); // Original unchanged
/// assert_eq!(forked.read_byte(0)?, 200);
/// ```
#[derive(Debug)]
pub struct Page {
    /// Immutable backing data (from parent or initial load).
    backing: Arc<[u8; PAGE_SIZE]>,
    /// Local copy for modifications (created on first write).
    /// Uses RwLock for concurrent read/write access.
    local: RwLock<Option<Box<[u8; PAGE_SIZE]>>>,
}

impl Page {
    /// Creates a new page with the given initial data.
    ///
    /// The data becomes the immutable backing, and no local copy exists yet.
    #[must_use]
    pub fn new(data: [u8; PAGE_SIZE]) -> Self {
        Self {
            backing: Arc::new(data),
            local: RwLock::new(None),
        }
    }

    /// Creates a new page initialized with zeros.
    #[must_use]
    pub fn zeroed() -> Self {
        Self::new([0u8; PAGE_SIZE])
    }

    /// Creates a page from a slice, padding with zeros if needed.
    ///
    /// If the slice is shorter than `PAGE_SIZE`, the rest is zero-filled.
    /// If longer, it is truncated.
    #[must_use]
    pub fn from_slice(data: &[u8]) -> Self {
        let mut page_data = [0u8; PAGE_SIZE];
        let copy_len = data.len().min(PAGE_SIZE);
        page_data[..copy_len].copy_from_slice(&data[..copy_len]);
        Self::new(page_data)
    }

    /// Reads a single byte at the given offset.
    ///
    /// Returns the byte from the local copy if it exists, otherwise from backing.
    ///
    /// # Errors
    ///
    /// Returns [`EmulationError::PageOutOfBounds`] if `offset >= PAGE_SIZE`.
    /// Returns [`EmulationError::LockPoisoned`] if the page lock is poisoned.
    pub fn read_byte(&self, offset: usize) -> Result<u8, EmulationError> {
        if offset >= PAGE_SIZE {
            return Err(EmulationError::PageOutOfBounds {
                offset,
                size: 1,
                page_size: PAGE_SIZE,
            });
        }
        let local = self
            .local
            .read()
            .map_err(|_| EmulationError::LockPoisoned {
                description: "page local buffer",
            })?;
        Ok(local
            .as_ref()
            .map_or(self.backing[offset], |data| data[offset]))
    }

    /// Reads a range of bytes into the provided buffer.
    ///
    /// # Arguments
    ///
    /// * `offset` - Starting offset within the page
    /// * `buf` - Buffer to read into
    ///
    /// # Errors
    ///
    /// Returns [`EmulationError::PageOutOfBounds`] if `offset + buf.len() > PAGE_SIZE`.
    /// Returns [`EmulationError::LockPoisoned`] if the page lock is poisoned.
    pub fn read(&self, offset: usize, buf: &mut [u8]) -> Result<(), EmulationError> {
        let end = offset.saturating_add(buf.len());
        if end > PAGE_SIZE || end < offset {
            return Err(EmulationError::PageOutOfBounds {
                offset,
                size: buf.len(),
                page_size: PAGE_SIZE,
            });
        }

        let local = self
            .local
            .read()
            .map_err(|_| EmulationError::LockPoisoned {
                description: "page local buffer",
            })?;
        let src = local
            .as_ref()
            .map_or(&self.backing[offset..end], |data| &data[offset..end]);
        buf.copy_from_slice(src);
        Ok(())
    }

    /// Reads a range and returns a new Vec.
    ///
    /// # Errors
    ///
    /// Returns [`EmulationError::PageOutOfBounds`] if `offset + len > PAGE_SIZE`.
    /// Returns [`EmulationError::LockPoisoned`] if the page lock is poisoned.
    pub fn read_vec(&self, offset: usize, len: usize) -> Result<Vec<u8>, EmulationError> {
        let mut buf = vec![0u8; len];
        self.read(offset, &mut buf)?;
        Ok(buf)
    }

    /// Writes a single byte at the given offset.
    ///
    /// Triggers copy-on-write if no local copy exists yet.
    ///
    /// # Errors
    ///
    /// Returns [`EmulationError::PageOutOfBounds`] if `offset >= PAGE_SIZE`.
    /// Returns [`EmulationError::LockPoisoned`] if the page lock is poisoned.
    pub fn write_byte(&self, offset: usize, value: u8) -> Result<(), EmulationError> {
        if offset >= PAGE_SIZE {
            return Err(EmulationError::PageOutOfBounds {
                offset,
                size: 1,
                page_size: PAGE_SIZE,
            });
        }

        let mut local = self
            .local
            .write()
            .map_err(|_| EmulationError::LockPoisoned {
                description: "page local buffer",
            })?;
        let buf = local.get_or_insert_with(|| Box::new(*self.backing));
        buf[offset] = value;
        Ok(())
    }

    /// Writes bytes from the provided buffer.
    ///
    /// Triggers copy-on-write if no local copy exists yet.
    ///
    /// # Arguments
    ///
    /// * `offset` - Starting offset within the page
    /// * `data` - Data to write
    ///
    /// # Errors
    ///
    /// Returns [`EmulationError::PageOutOfBounds`] if `offset + data.len() > PAGE_SIZE`.
    /// Returns [`EmulationError::LockPoisoned`] if the page lock is poisoned.
    pub fn write(&self, offset: usize, data: &[u8]) -> Result<(), EmulationError> {
        let end = offset.saturating_add(data.len());
        if end > PAGE_SIZE || end < offset {
            return Err(EmulationError::PageOutOfBounds {
                offset,
                size: data.len(),
                page_size: PAGE_SIZE,
            });
        }

        let mut local = self
            .local
            .write()
            .map_err(|_| EmulationError::LockPoisoned {
                description: "page local buffer",
            })?;
        let buf = local.get_or_insert_with(|| Box::new(*self.backing));
        buf[offset..end].copy_from_slice(data);
        Ok(())
    }

    /// Returns `true` if this page has been modified (has a local copy).
    ///
    /// # Errors
    ///
    /// Returns [`EmulationError::LockPoisoned`] if the page lock is poisoned.
    pub fn is_modified(&self) -> Result<bool, EmulationError> {
        let local = self
            .local
            .read()
            .map_err(|_| EmulationError::LockPoisoned {
                description: "page local buffer",
            })?;
        Ok(local.is_some())
    }

    /// Forks this page, creating an independent copy with CoW semantics.
    ///
    /// The new page shares the current state as its backing:
    /// - If this page has local modifications, they become the new backing
    /// - Otherwise, the original backing is shared
    ///
    /// The forked page starts with no local modifications.
    ///
    /// # Errors
    ///
    /// Returns [`EmulationError::LockPoisoned`] if the page lock is poisoned.
    pub fn fork(&self) -> Result<Self, EmulationError> {
        let local = self
            .local
            .read()
            .map_err(|_| EmulationError::LockPoisoned {
                description: "page local buffer",
            })?;
        let new_backing = local.as_ref().map_or_else(
            || Arc::clone(&self.backing), // Share the original backing
            |data| Arc::new(**data),      // Local modifications become the new backing
        );

        Ok(Self {
            backing: new_backing,
            local: RwLock::new(None),
        })
    }

    /// Returns a reference to the current data (either local or backing).
    ///
    /// This acquires a read lock and returns a guard that provides access
    /// to the page data.
    ///
    /// # Errors
    ///
    /// Returns [`EmulationError::LockPoisoned`] if the page lock is poisoned.
    pub fn data(&self) -> Result<PageDataGuard<'_>, EmulationError> {
        let local = self
            .local
            .read()
            .map_err(|_| EmulationError::LockPoisoned {
                description: "page local buffer",
            })?;
        Ok(PageDataGuard { page: self, local })
    }
}

impl Clone for Page {
    fn clone(&self) -> Self {
        // Clone creates a copy with the current state as backing
        // Note: This will panic if the lock is poisoned - use fork() for fallible cloning
        self.fork().expect("page lock poisoned during clone")
    }
}

impl Default for Page {
    fn default() -> Self {
        Self::zeroed()
    }
}

/// Guard providing access to page data.
///
/// This holds the read lock on the page's local buffer and provides
/// access to either the local or backing data.
pub struct PageDataGuard<'a> {
    page: &'a Page,
    local: RwLockReadGuard<'a, Option<Box<[u8; PAGE_SIZE]>>>,
}

impl PageDataGuard<'_> {
    /// Returns a reference to the page data.
    #[must_use]
    pub fn as_slice(&self) -> &[u8; PAGE_SIZE] {
        self.local.as_ref().map_or(&self.page.backing, |data| data)
    }
}

impl std::ops::Deref for PageDataGuard<'_> {
    type Target = [u8; PAGE_SIZE];

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

#[cfg(test)]
mod tests {
    use std::{sync::Arc, thread};

    use crate::emulation::memory::page::{Page, PAGE_SIZE};

    #[test]
    fn test_page_new() {
        let mut data = [0u8; PAGE_SIZE];
        data[0] = 42;
        data[PAGE_SIZE - 1] = 99;
        let page = Page::new(data);

        assert_eq!(page.read_byte(0).unwrap(), 42);
        assert_eq!(page.read_byte(PAGE_SIZE - 1).unwrap(), 99);
        assert!(!page.is_modified().unwrap());
    }

    #[test]
    fn test_page_write_cow() {
        let page = Page::new([0u8; PAGE_SIZE]);
        assert!(!page.is_modified().unwrap());

        page.write_byte(100, 0xFF).unwrap();
        assert!(page.is_modified().unwrap());
        assert_eq!(page.read_byte(100).unwrap(), 0xFF);
        assert_eq!(page.read_byte(0).unwrap(), 0); // Other bytes unchanged
    }

    #[test]
    fn test_page_fork() {
        let page = Page::new([42u8; PAGE_SIZE]);
        page.write_byte(0, 100).unwrap();

        let forked = page.fork().unwrap();

        // Fork inherits current state
        assert_eq!(forked.read_byte(0).unwrap(), 100);
        assert!(!forked.is_modified().unwrap()); // Fresh local state

        // Modifications are independent
        forked.write_byte(0, 200).unwrap();
        assert_eq!(page.read_byte(0).unwrap(), 100);
        assert_eq!(forked.read_byte(0).unwrap(), 200);
    }

    #[test]
    fn test_page_read_write_range() {
        let page = Page::zeroed();

        let data = [1, 2, 3, 4, 5];
        page.write(10, &data).unwrap();

        let mut buf = [0u8; 5];
        page.read(10, &mut buf).unwrap();
        assert_eq!(buf, [1, 2, 3, 4, 5]);

        // Also test read_vec
        let vec = page.read_vec(10, 5).unwrap();
        assert_eq!(vec, vec![1, 2, 3, 4, 5]);
    }

    #[test]
    fn test_page_from_slice() {
        let data = vec![1, 2, 3, 4, 5];
        let page = Page::from_slice(&data);

        assert_eq!(page.read_byte(0).unwrap(), 1);
        assert_eq!(page.read_byte(4).unwrap(), 5);
        assert_eq!(page.read_byte(5).unwrap(), 0); // Zero-padded
    }

    #[test]
    fn test_page_data_guard() {
        let mut data = [0u8; PAGE_SIZE];
        data[0] = 42;
        let page = Page::new(data);

        let guard = page.data().unwrap();
        assert_eq!(guard[0], 42);
    }

    #[test]
    fn test_page_concurrent_reads() {
        let page = Arc::new(Page::new([42u8; PAGE_SIZE]));

        let handles: Vec<_> = (0..4)
            .map(|_| {
                let p = Arc::clone(&page);
                thread::spawn(move || {
                    for _ in 0..1000 {
                        assert_eq!(p.read_byte(0).unwrap(), 42);
                    }
                })
            })
            .collect();

        for h in handles {
            h.join().unwrap();
        }
    }

    #[test]
    fn test_page_out_of_bounds_read_byte() {
        let page = Page::zeroed();
        let result = page.read_byte(PAGE_SIZE);
        assert!(result.is_err());
    }

    #[test]
    fn test_page_out_of_bounds_write_byte() {
        let page = Page::zeroed();
        let result = page.write_byte(PAGE_SIZE, 0xFF);
        assert!(result.is_err());
    }

    #[test]
    fn test_page_out_of_bounds_read_range() {
        let page = Page::zeroed();
        let mut buf = [0u8; 10];
        let result = page.read(PAGE_SIZE - 5, &mut buf);
        assert!(result.is_err());
    }

    #[test]
    fn test_page_out_of_bounds_write_range() {
        let page = Page::zeroed();
        let data = [0u8; 10];
        let result = page.write(PAGE_SIZE - 5, &data);
        assert!(result.is_err());
    }
}