all repos — gemini-redirect @ 486db3039b7901d089b299bf3514850923c82002

content/blog/woce-4.md (view raw)

  1+++
  2title = "Writing our own Cheat Engine: Floating points"
  3date = 2021-02-28
  4updated = 2021-02-28
  5[taxonomies]
  6category = ["sw"]
  7tags = ["windows", "rust", "hacking"]
  8+++
  9
 10This is part 4 on the *Writing our own Cheat Engine* series:
 11
 12* [Part 1: Introduction](/blog/woce-1) (start here if you're new to the series!)
 13* [Part 2: Exact Value scanning](/blog/woce-2)
 14* [Part 3: Unknown initial value](/blog/woce-3)
 15* Part 4: Floating points
 16* [Part 5: Code finder](/blog/woce-5)
 17
 18In part 3 we did a fair amount of plumbing in order to support scan modes beyond the trivial "exact value scan". As a result, we have abstracted away the `Scan`, `CandidateLocations` and `Value` types as a separate `enum` each. Scanning for changed memory regions in an opened process can now be achieved with three lines of code:
 19
 20```rust
 21let regions = process.memory_regions();
 22let first_scan = process.scan_regions(&regions, Scan::InRange(0, 500));
 23let second_scan = process.rescan_regions(&first_scan, Scan::DecreasedBy(7));
 24```
 25
 26How's that for programmability? No need to fire up Cheat Engine's GUI anymore!
 27
 28The `first_scan` in the example above remembers all the found `Value` within the range specified by `Scan`. Up until now, we have only worked with `i32`, so that's the type the scans expect and what they work with.
 29
 30Now it's time to introduce support for different types, like `f32`, `i64`, or even more atypical ones, like arbitrary sequences of bytes (think of strings) or even numbers in big-endian.
 31
 32Tighten your belt, because this post is quite the ride. Let's get right into it!
 33
 34## Floating points
 35
 36<details open><summary>Cheat Engine Tutorial: Step 4</summary>
 37
 38> In the previous tutorial we used bytes to scan, but some games store information in so called 'floating point' notations.
 39> (probably to prevent simple memory scanners from finding it the easy way). A floating point is a value with some digits behind the point. (like 5.12 or 11321.1)
 40>
 41> Below you see your health and ammo. Both are stored as Floating point notations, but health is stored as a float and ammo is stored as a double.
 42> Click on hit me to lose some health, and on shoot to decrease your ammo with 0.5
 43>
 44> You have to set BOTH values to 5000 or higher to proceed.
 45>
 46> Exact value scan will work fine here, but you may want to experiment with other types too.
 47>
 48> Hint: It is recommended to disable "Fast Scan" for type double
 49
 50</details>
 51
 52## Generic values
 53
 54The `Value` enumeration holds scanned values, and is currently hardcoded to store `i32`. The `Scan` type also holds a value, the value we want to scan for. Changing it to support other types is trivial:
 55
 56```rust
 57pub enum Scan<T> {
 58    Exact(T),
 59    Unknown,
 60    Decreased,
 61    // ...other variants...
 62}
 63
 64pub enum Value<T> {
 65    Exact(T),
 66    AnyWithin(Vec<u8>),
 67}
 68```
 69
 70`AnyWithin` is the raw memory, and `T` can be interpreted from any sequence of bytes thanks to our friend [`mem::transmute`][transmute]. This change alone is enough to store an arbitrary `T`! So we're done now? Not really, no.
 71
 72First of all, we need to update all the places where `Scan` or `Value` are used. Our first stop is the scanned `Region`, which holds the found `Value`:
 73
 74```rust
 75pub struct Region<T> {
 76    pub info: MEMORY_BASIC_INFORMATION,
 77    pub locations: CandidateLocations,
 78    pub value: Value<T>,
 79}
 80```
 81
 82Then, we need to update everywhere `Region` is used, and on and on… All in all this process is just repeating `cargo check`, letting the compiler vent on you, and taking good care of it by fixing the errors. It's quite reassuring to know you will not miss a single place. Thank you, compiler!
 83
 84But wait, how could scanning for a decreased value work for any `T`? The type is not `Ord`, we should add some trait bounds. And also, what happens if the type is not `Copy`? It could implement `Drop`[^1], and we will be transmuting from raw bytes, which would trigger the `Drop` implementation when we're done with the value! Not memory safe at all! And how could we possibly cast raw memory to the type without knowing its siz– oh nevermind, [`T` is already `Sized` by default][sized-default]. But anyway, we need the other bounds.
 85
 86In order to not repeat ourselves, we will implement a new `trait`, let's say `Scannable`, which requires all other bounds:
 87
 88```rust
 89pub trait Scannable: Copy + PartialEq + PartialOrd {}
 90
 91impl<T: Copy + PartialEq + PartialOrd> Scannable for T {}
 92```
 93
 94And fix our definitions:
 95
 96```rust
 97pub enum Scan<T: Scannable> { ... }
 98pub enum Value<T: Scannable> { ... }
 99pub struct Region<T: Scannable> { ... }
100
101// ...and the many other places referring to T
102```
103
104Every type which is `Copy`, `PartialEq` and `PartialOrd` can be scanned over[^2], because we `impl Scan for T` where the bounds are met. Unfortunately, we cannot require `Eq` or `Ord` because the floating point types do not implement it.
105
106## Transmuting memory
107
108Also known as reinterpreting a bunch of bytes as something else, or perhaps it stands for "summoning the demon":
109
110> `transmute` is **incredibly** unsafe. There are a vast number of ways to cause [undefined behavior][ub] with this function. `transmute` should be the absolute last resort.
111
112Types like `i32` define methods such as [`from_ne_bytes`][fromne] and [`to_ne_bytes`][tone] which convert raw bytes from and into its native representation. This is all really nice, but unfortunately, there's no standard trait in the Rust's standard library to "interpret a type `T` as the byte sequence of its native representation". `transmute`, however, does exist, and similar to any other `unsafe` function, it's safe to call **as long as we respect its invariants**. What are these invariants[^3]?
113
114> Both types must have the same size
115
116Okay, we can just assert that the window length matches the type's length. What else?
117
118> Neither the original, nor the result, may be an [invalid value][inv-val].
119
120What's an invalid value?
121
122> * a `bool` that isn't 0 or 1
123> * an `enum` with an invalid discriminant
124> * a null `fn` pointer
125> * a `char` outside the ranges [0x0, 0xD7FF] and [0xE000, 0x10FFFF]
126> * a `!` (all values are invalid for this type)
127> * an integer (`i*`/`u*`), floating point value (`f*`), or raw pointer read from uninitialized memory, or uninitialized memory in a `str`.
128> * a reference/`Box` that is dangling, unaligned, or points to an invalid value.
129> * a wide reference, `Box`, or raw pointer that has invalid metadata:
130>   * `dyn Trait` metadata is invalid if it is not a pointer to a vtable for `Trait` that matches the actual dynamic trait the pointer or reference points to
131>   * slice metadata is invalid if the length is not a valid `usize` (i.e., it must not be read from uninitialized memory)
132> * a type with custom invalid values that is one of those values, such as a `NonNull` that is null. (Requesting custom invalid values is an unstable feature, but some stable libstd types, like `NonNull`, make use of it.)
133
134Okay, that's actually an awful lot. Types like `bool` implement all the trait bounds we defined, and it would be insta-UB to ever try to cast them from arbitrary bytes. The same goes for `char`, and all `enum` are out of our control, too. At least we're safe on the "memory is initialized" front.
135
136Dang it, I really wanted to use `transmute`! But if we were to use it for arbitrary types, it would trigger undefined behaviour sooner than later.
137
138We have several options here:
139
140* Make it an `unsafe trait`. Implementors will be responsible for ensuring that the type they're implementing it for can be safely transmuted from and into.
141* [Seal the `trait`][seal] and implement it only for types we know are safe[^4], like `i32`.
142* Add methods to the `trait` definition that do the conversion of the type into its native representation.
143
144We will go with the first option[^5], because I really want to use `transmute`, and I want users to be able to implement the trait on their own types.
145
146In any case, we need to change our `impl` to something more specific, in order to prevent it from automatically implementing the trait for types for which their memory representation has invalid values. So we get rid of this:
147
148```rust
149pub trait Scannable: Copy + PartialEq + PartialOrd {}
150
151impl<T: Copy + PartialEq + PartialOrd> Scannable for T {}
152```
153
154And replace it with this:
155
156```rust
157pub unsafe trait Scannable: Copy + PartialEq + PartialOrd {}
158
159macro_rules! impl_many {
160    ( unsafe impl $trait:tt for $( $ty:ty ),* ) => {
161        $( unsafe impl $trait for $ty {} )*
162    };
163}
164
165// SAFETY: all these types respect `Scannable` invariants.
166impl_many!(unsafe impl Scannable for i8, u8, i16, u16, i32, u32, i64, u64, f32, f64);
167```
168
169Making a small macro for things like these is super useful. You could of course write `unsafe impl Scannable for T` for all ten `T` as well, but that introduces even more `unsafe` to read. Last but not least, let's replace the hardcoded `i32::from_ne_bytes` and `i32::to_ne_bytes` with `mem::transmute`.
170
171All the `windows(4)` need to be replaced with `windows(mem::size_of::<T>())` because the size may no longer be `4`. All the `i32::from_ne_bytes(...)` need to be replaced with `mem::transmute::<_, T>(...)`. We explicitly write out `T` to make sure the compiler doesn't accidentally infer something we didn't intend.
172
173And… it doesn't work at all. We're working with byte slices of arbitrary length. We cannot transmute a `&[]` type, which is 16 bytes (8 for the pointer and 8 for the length), to our `T`. My plan to use transmute can't possibly work here. Sigh.
174
175## Not quite transmuting memory
176
177Okay, we can't transmute, because we don't have a sized value, we only have a slice of bytes pointing somewhere else. What we *could* do is reinterpret the pointer to those bytes as a different type, and then dereference it! This is still a form of "transmutation", just without using `transmute`.
178
179```rust
180let value = unsafe { *(window.as_ptr() as *const T) };
181```
182
183Woop! You can compile this and test it out on the step 2 and 3 of the tutorial, using `i32`, and it will still work! Something troubles me, though. Can you see what it is?
184
185When we talked about invalid values, it had a note about unaligned references:
186
187> a reference/`Box` that is dangling, unaligned, or points to an invalid value.
188
189Our `window` is essentially a reference to `T`. The only difference is we're working at the pointer level, but they're pretty much references. Let's see what the documentation for [`pointer`][pointer] has to say as well, since we're dereferencing pointers:
190
191> when a raw pointer is dereferenced (using the `*` operator), it must be non-null and aligned.
192
193It must be aligned. The only reason why our data is aligned is because we are also performing a "fast scan", so we only look at aligned locations. This is a time bomb waiting to blow up. Is there any other way to [`read`][ptr-read] from a pointer which is safer?
194
195> `src` must be properly aligned. Use [`read_unaligned`][ptr-readun] if this is not the case.
196
197Bingo! Both `read` and `read_unaligned`, unlike dereferencing the pointer, will perform a copy, but if it can make the code less prone to blowing up, I'll take it[^6]. Let's change the code one more time:
198
199```rust
200let current = unsafe { window.as_ptr().cast::<T>().read_unaligned() };
201```
202
203I prefer to avoid type annotations in variables where possible, which is why I use the [turbofish] so often. You can get rid of the cast and use a type annotation instead, but make sure the type is known, otherwise it will think it's `u8` because `window` is a `&[u8]`.
204
205Now, this is all cool and good. You can replace `i32` with `f32` for `T` and you'll be able to get halfway done with the step 4 of Cheat Engine's tutorial. Unfortunately, as it is, this code is not enough to complete step 4 with exact scans[^7]. You see, comparing floating point values is not as simple as checking for bitwise equality. We were actually really lucky that the `f32` part works! But the values in the `f64` part are not as precise as our inputs, so our exact scan fails.
206
207Using a fixed type parameter is pretty limiting as well. On the one hand, it is nice that, if you scan for `i32`, the compiler statically guarantees that subsequent scans will also happen on `i32` and thus be compatible. On the other, this requires us to know the type at compile time, which for an interactive program, is not possible. While we *could* create different methods for each supported type and, at runtime, decide to which we should jump, I am not satisfied with that solution. It also means we can't switch from scanning an `u32` to an `i32`, for whatever reason.
208
209So we need to work around this once more.
210
211## Rethinking the scans
212
213What does our scanning function need, really? It needs a way to compare two chunks of memory as being equal or not (as we have seen, this isn't trivial with types such as floating point numbers) and, for other types of scans, it needs to be able to produce an ordering, or calculate a difference.
214
215Instead of having a our trait require the bounds `PartialEq` and `PartialOrd`, we can define our own methods to compare `Self` with `&[u8]`. It still should be `Clone`, so we can pass it around without worrying about lifetimes:
216
217```rust
218// Callers must `assert_eq!(memory.len(), mem::size_of::<Self>())`.
219unsafe fn eq(&self, memory: &[u8]) -> bool;
220unsafe fn cmp(&self, memory: &[u8]) -> Ordering;
221```
222
223This can be trivially implemented for all integer types:
224
225```rust
226macro_rules! impl_scannable_for_int {
227    ( $( $ty:ty ),* ) => {
228        $(
229            // SAFETY: caller is responsible to `assert_eq!(memory.len(), mem::size_of::<T>())`
230            impl Scannable for $ty {
231                unsafe fn eq(&self, memory: &[u8]) -> bool {
232                    let other = unsafe { memory.as_ptr().cast::<$ty>().read_unaligned() };
233                    *self == other
234                }
235
236                unsafe fn cmp(&self, memory: &[u8]) -> Ordering {
237                    let other = unsafe { memory.as_ptr().cast::<$ty>().read_unaligned() };
238                    <$ty as Ord>::cmp(self, &other)
239                }
240            }
241        )*
242    };
243}
244
245impl_scannable_for_int!(i8, u8, i16, u16, i32, u32, i64, u64);
246```
247
248The funny `<$ty as Ord>` is because I decided to call the method `Scannable::cmp`, so I have to disambiguate between it and `Ord::cmp`. We can go ahead and update the code using `Scannable` to use these new functions instead.
249
250Now, you may have noticed I only implemented it for the integer types. That's because floats need some extra care. Unfortunately, floating point types do not have any form of "precision" embedded in them, so we can't accurately say "compare these floats to the precision level the user specified". What we can do, however, is drop a few bits from the mantissa, so "relatively close" quantities are considered equal. It's definitely not as good as comparing floats to the user's precision, but it will get the job done.
251
252I'm going to arbitrarily say that we are okay comparing with "half" the precision. We can achieve that by masking half of the bits from the mantissa to zero:
253
254```rust
255
256macro_rules! impl_scannable_for_float {
257    ( $( $ty:ty : $int_ty:ty ),* ) => {
258        $(
259            #[allow(unused_unsafe)] // mind you, it is necessary
260            impl Scannable for $ty {
261                unsafe fn eq(&self, memory: &[u8]) -> bool {
262                    const MASK: $int_ty = !((1 << (<$ty>::MANTISSA_DIGITS / 2)) - 1);
263
264                    // SAFETY: caller is responsible to `assert_eq!(memory.len(), mem::size_of::<T>())`
265                    let other = unsafe { memory.as_ptr().cast::<$ty>().read_unaligned() };
266                    let left = <$ty>::from_bits(self.to_bits() & MASK);
267                    let right = <$ty>::from_bits(other.to_bits() & MASK);
268                    left == right
269                }
270
271                ...
272            }
273        )*
274    };
275}
276
277impl_scannable_for_float!(f32: u32, f64: u64);
278```
279
280You may be wondering what's up with that weird `MASK`. Let's visualize it with a [`f16`][f16]. This type has 16 bits, 1 for sign, 5 for exponent, and 10 for the mantissa:
281
282```
283S EEEEE MMMMMMMMMM
284```
285
286If we substitute the constant with the numeric value and operate:
287
288```rust
289!((1 << (10 / 2)) - 1)
290!((1 << 5) - 1)
291!(0b00000000_00100000 - 1)
292!(0b00000000_00011111)
2930b11111111_11100000
294```
295
296So effectively, half of the mantisssa bit will be masked to 0. For the `f16` example, this makes us lose 5 bits of precision. Comparing two floating point values with their last five bits truncated is equivalent to checking if they are "roughly equal"!
297
298When Cheat Engine scans for floating point values, several additional settings show, and one such option is "truncated". I do not know if it behaves like this, but it might.
299
300Let's try this out:
301
302```rust
303#[test]
304fn f32_roughly_eq() {
305    let left = 0.25f32;
306    let right = 0.25000123f32;
307    let memory = unsafe { mem::transmute::<_, [u8; 4]>(right) };
308    assert_ne!(left, right);
309    assert!(unsafe { Scannable::eq(&left, &memory) });
310}
311```
312
313```
314>cargo test f32_roughly_eq
315
316running 1 test
317test scan::candidate_location_tests::f32_roughly_eq ... ok
318```
319
320Huzzah! The `assert_ne!` makes sure that a normal comparision would fail, and then we `assert!` that our custom one passes the test. When the user performs an exact scan, the code will be more tolerant to the user's less precise inputs, which overall should result in a nicer experience.
321
322## Dynamically sized scans
323
324The second problem we need to solve is the possibility of the size not being known at compile time[^8]. While we can go as far as scanning over strings of a known length, this is rather limiting, because we need to know the length at compile time[^9]. Heap allocated objects are another problem, because we don't want to compare the memory representation of the stack object, but likely the memory where they point to (such as `String`).
325
326Instead of using `mem::size_of`, we can add a new method to our `Scannable`, `size`, which will tell us the size required of the memory view we're comparing against:
327
328```rust
329unsafe impl Scannable {
330    ...
331
332    fn size(&self) -> usize;
333}
334```
335
336It is `unsafe` to implement, because we are relying on the returned value to be truthful and unchanging. It should be safe to call, because it cannot have any invariants. Unfortunately, signaling "unsafe to implement" is done by marking the entire trait as `unsafe`, since "unsafe to call" is reserved for `unsafe fn`, and even though the rest of methods are not necessarily unsafe to implement, they're treated as such.
337
338At the moment, `Scannable` cannot be made into a trait object because it is [not object safe][objectsafe]. This is caused by the `Clone` requirement on all `Scannable` object, which in turn needs the types to be `Sized` because `clone` returns `Self`. Because of this, the size must be known.
339
340However, we *can* move the `Clone` requirement to the methods that need it! This way, `Scannable` can remain object safe, enabling us to do the following:
341
342```rust
343unsafe impl<T: AsRef<dyn Scannable> + AsMut<dyn Scannable>> Scannable for T {
344    unsafe fn eq(&self, memory: &[u8]) -> bool {
345        self.as_ref().eq(memory)
346    }
347
348    unsafe fn cmp(&self, memory: &[u8]) -> Ordering {
349        self.as_ref().cmp(memory)
350    }
351
352    fn mem_view(&self) -> &[u8] {
353        self.as_ref().mem_view()
354    }
355
356    fn size(&self) -> usize {
357        self.as_ref().size()
358    }
359}
360```
361
362Any type which can be interpreted as a reference to `Scannable` is also a scannable! This enables us to perform scans over `Box<dyn i32>`, where the type is known at runtime! Or rather, it would, if `Box<dyn T>` implemented `Clone`, which it can't[^10] because that's what prompted this entire issue. Dang it! I can't catch a breath today!
363
364Okay, let's step back. Why did we need our scannables to be clone in the first place? When we perform exact scans, we store the original value in the region, which we don't own, so we clone it. But what if we *did* own the value? Instead of taking the `Scan` by reference, which holds `T: Scannable`, we could take it by value. If we get rid of all the `Clone` bounds and update `Scan::run` to take `self`, along with updating all the things that take a `Region` to take them by value as well, it should all work out.
365
366But it does not. If we take `Scan` by value, with it not being `Clone`, we simply can't use it to scan over multiple regions. After the first region, we have lost the `Scan`.
367
368Let's take a second step back. We are scanning memory, and we want to compare memory, but we want to treat the memory with different semantics (for example, if we treat it as `f32`, we want to check for rough equality). Instead of storing the *value* itself, we could store its *memory representation*, and when we compare memory representations, we can do so under certain semantics.
369
370First off, let's revert getting rid of all `Clone`. Wherever we stored a `T`, we will now store a `Vec<u8>`. We will still use a type parameter to represent the "implementations of `Scannable`". For this to work, our definitions need to use `T` somewhere, or else the compiler refuses to compile the code with error [E0392]. For this, I will stick a [`PhantomData`][phantom] in the `Exact` variant. It's a bit pointless to include it in all variants, and `Exact` seems the most appropriated:
371
372```rust
373pub enum Scan<T: Scannable> {
374    Exact(Vec<u8>, PhantomData<T>),
375    Unknown,
376    ...
377}
378```
379
380This keeps in line with `Value`:
381
382```rust
383pub enum Value<T: Scannable> {
384    Exact(Vec<u8>, PhantomData<T>),
385    ...
386}
387```
388
389Our `Scannable` will no longer work on `T` and `&[u8]`. Instead, it will work on two `&[u8]`. We will also need a way to interpret a `T` as `&[u8]`, which we can achieve with a new method, `mem_view`. This method interprets the raw memory representation of `self` as its raw bytes. It also lets us get rid of `size`, because we can simply do `mem_view().len()`. It's still `unsafe` to implement, because it should return the same length every time:
390
391```rust
392pub unsafe trait Scannable {
393    // Callers must `assert_eq!(left.len(), right.len(), self.mem_view().len())`.
394    unsafe fn eq(left: &[u8], right: &[u8]) -> bool;
395    unsafe fn cmp(left: &[u8], right: &[u8]) -> Ordering;
396    fn mem_view(&self) -> &[u8];
397}
398```
399
400But now we can't use it in trait object, so the following no longer works:
401
402```rust
403unsafe impl<T: AsRef<dyn Scannable> + AsMut<dyn Scannable>> Scannable for T {
404    ...
405}
406```
407
408Ugh! Well, to be fair, we no longer have a "scannable" at this point. It's more like a scan mode that tells us how memory should be compared according to a certain type. Let's split the trait into two: one for the scan mode, and other for "things which are scannable":
409
410```rust
411pub trait ScanMode {
412    unsafe fn eq(left: &[u8], right: &[u8]) -> bool;
413    unsafe fn cmp(left: &[u8], right: &[u8]) -> Ordering;
414}
415
416pub unsafe trait Scannable {
417    type Mode: ScanMode;
418
419    fn mem_view(&self) -> &[u8];
420}
421```
422
423Note that we have an associated `type Mode` which contains the corresponding `ScanMode`. If we used a trait bound such as `Scannable: ScanMode`, we'd be back to square one: it would inherit the method definitions that don't use `&self` and thus cannot be used as trait objects.
424
425With these changes, it is possible to implement `Scannable` for any `dyn Scannable`:
426
427```rust
428unsafe impl<T: ScanMode + AsRef<dyn Scannable<Mode = Self>>> Scannable for T {
429    type Mode = Self;
430
431    fn mem_view(&self) -> &[u8] {
432        self.as_ref().mem_view()
433    }
434}
435```
436
437We do have to adjust a few places of the code to account for both `Scannable` and `ScanMode`, but all in all, it's pretty straightforward. Things like `Value` don't need to store the `Scannable` anymore, just a `Vec<u8>`. It also doesn't need the `ScanMode`, because it's not going to be scanning anything on its own. This applies transitively to `Region` which was holding a `Value`.
438
439`Value` *does* need to be updated to store the size of the region we are scanning for, however, because we need that information when running a subsequent scan. For all `Scan` that don't have a explicit thing to scan for (like `Decreased`), the `size` also needs to be stored in them.
440
441Despite all our efforts, we're still unable to return an `Scannable` chosen at runtime.
442
443```rust
444fn prompt_user_for_scan() -> Scan<Box<dyn Scannable<Mode = ???>>> {
445    todo!()
446}
447```
448
449As far as I can tell, there's simply no way to specify that type. We want to return a type which is scannable, which has itself (which is also a `ScanMode`) as the corresponding mode. Even if we just tried to return the mode, we simply can't, because it's not object-safe. Is this the end of the road?
450
451## Specifying the scan mode
452
453We need a way to pass an arbitrary scan mode to our `Scan`. This scan mode should go in tandem with `Scannable` types, because it would be unsafe otherwise. We've seen that using a type just doesn't cut it. What else can we do?
454
455Using an enumeration is a no-go, because I want users to be able to extend it further. I also would like to avoid having to update the `enum` and all the matches every time I come up with a different type combination. And it could get pretty complicated if I ever built something dynamically, such as letting the user combine different scans in one pass.
456
457So what if we make `Scannable` return a value that implements the functions we need?
458
459```rust
460pub struct ScanMode {
461    eq: unsafe fn(left: &[u8], right: &[u8]) -> bool,
462    cmp: unsafe fn(left: &[u8], right: &[u8]) -> Ordering,
463}
464```
465
466It's definitely… non-conventional. But hey, now we're left with the `Scannable` trait, which is object-safe, and does not have any type parameters!
467
468```rust
469pub unsafe trait Scannable {
470    fn mem_view(&self) -> &[u8];
471    fn scan_mode(&self) -> ScanMode;
472}
473```
474
475It is a bit weird, but defining local functions and using those in the returned value is a nice way to keep things properly scoped:
476
477```rust
478macro_rules! impl_scannable_for_int {
479    ( $( $ty:ty ),* ) => {
480        $(
481            unsafe impl Scannable for $ty {
482                fn mem_view(&self) -> &[u8] {
483                    unsafe { std::slice::from_raw_parts(self as *const _ as *const u8, mem::size_of::<$ty>()) }
484                }
485
486                fn scan_mode(&self) -> ScanMode {
487                    unsafe fn eq(left: &[u8], right: &[u8]) -> bool {
488                        ...
489                    }
490
491                    unsafe fn cmp(left: &[u8], right: &[u8]) -> Ordering {
492                        ...
493                    }
494
495                    ScanMode { eq, cmp }
496                }
497            }
498        )*
499    };
500}
501```
502
503Our `Scan` needs to store the `Scannable` type, and not just the memory, once again. For variants that don't need any value, they can store the `ScanMode` and size instead.
504
505Does this solution work? Yes! It's possible to return a `Box<dyn Scannable>` from a function, and underneath, it may be using any type which is `Scannable`. Is this the best solution? Well, that's hard to say. This is *one* of the possible solutions.
506
507We have been going around in circles for quite some time now, so I'll leave it there. It's a solution, which may not be pretty, but it works. With these changes, the code is capable of completing all of the steps in the Cheat Engine tutorial up until point!
508
509## Finale
510
511If there's one lesson to learn from this post, it's that there is often no single correct solution to a problem. We could have approached the scan types in many, many ways (and we tried quite a few!), but in the end, choosing one option or the other comes down to your (sometimes self-imposed) requirements.
512
513You may [obtain the code for this post][code] over at my GitHub. You can run `git checkout step4` after cloning the repository to get the right version of the code. The code has gone through a lot of iterations, and I'd still like to polish it a bit more, so it might slightly differ from the code presented in this entry.
514
515If you feel adventurous, Cheat Engine has different options for scanning floating point types: "rounded (default)", "rounded (extreme)", and truncated. Optionally, it can scan for "simple values only". You could go ahead and toy around with these!
516
517We didn't touch on types with different lengths, such as strings. You could support UTF-8, UTF-16, or arbitrary byte sequences. This post also didn't cover scanning for multiple things at once, known as "groupscan commands", although from what I can tell, these are just a nice way to scan for arbitrary byte sequences.
518
519We also didn't look into supporting different the same scan with different alignments. All these things may be worth exploring depending on your requirements. You could even get rid of such genericity and go with something way simpler. Supporting `i32`, `f32` and `f64` is enough to complete the Cheat Engine tutorial. But I wanted something more powerful, although my solution currently can't scan for a sequence such as "exact type, unknown, exact matching the unknown". So yeah.
520
521In the [next post](/blog/woce-5), we'll tackle the fifth step of the tutorial: Code finder. Cheat Engine attaches its debugger to the process for this one, and then replaces the instruction that performs the write with a different no-op so that nothing is written anymore. This will be quite the challenge!
522
523### Footnotes
524
525[^1]: [`Copy` and `Drop` are exclusive][copy-drop]. See also [E0184].
526
527[^2]: If you added more scan types that require additional bounds, make sure to add them too. For example, the "decreased by" scan requires the type to `impl Sub`.
528
529[^3]: This is a good time to remind you to read the documentation. It is of special importance when dealing with `unsafe` methods; I recommend reading it a couple times.
530
531[^4]: Even with this option, it would not be a bad idea to make the trait `unsafe`.
532
533[^5]: Not for long. As we will find out later, this approach has its limitations.
534
535[^6]: We can still perform the pointer dereference when we know it's aligned. This would likely be an optimization, although it would definitely complicate the code more.
536
537[^7]: It *would* work if you scanned for unknown values and then checked for decreased values repeatedly. But we can't just leave exact scan broken!
538
539[^8]: Unfortunately, this makes some optimizations harder or even impossible to perform. Providing specialized functions for types where the size is known at compile time could be worth doing. Programming is all tradeoffs.
540
541[^9]: [Rust 1.51][rust151], which was not out at the time of writing, would make it a lot easier to allow scanning for fixed-length sequences of bytes, thanks to const generics.
542
543[^10]: Workarounds do exist, such as [dtolnay's `dyn-clone`][dynclone]. But I would rather not go that route.
544
545[transmute]: https://doc.rust-lang.org/stable/std/mem/fn.transmute.html
546[ub]: https://doc.rust-lang.org/stable/reference/behavior-considered-undefined.html
547[code]: https://github.com/lonami/memo
548[sized-default]: https://doc.rust-lang.org/stable/std/marker/trait.Sized.html
549[fromne]: https://doc.rust-lang.org/stable/std/primitive.i32.html#method.from_ne_bytes
550[tone]: https://doc.rust-lang.org/stable/std/primitive.i32.html#method.to_ne_bytes
551[inv-val]: https://doc.rust-lang.org/nomicon/what-unsafe-does.html
552[seal]: https://rust-lang.github.io/api-guidelines/future-proofing.html
553[pointer]: https://doc.rust-lang.org/std/primitive.pointer.html
554[ptr-read]: https://doc.rust-lang.org/std/ptr/fn.read.html
555[ptr-readun]: https://doc.rust-lang.org/std/ptr/fn.read_unaligned.html
556[turbofish]: https://www.reddit.com/r/rust/comments/3fimgp/why_double_colon_rather_that_dot/ctozkd0/
557[f16]: https://en.wikipedia.org/wiki/Bfloat16_floating-point_format
558[objectsafe]: https://doc.rust-lang.org/stable/error-index.html#E0038
559[copy-drop]: https://doc.rust-lang.org/stable/std/ops/trait.Drop.html#copy-and-drop-are-exclusive
560[E0184]: https://doc.rust-lang.org/stable/error-index.html#E0184
561[E0392]: https://doc.rust-lang.org/stable/error-index.html#E0392
562[phantom]: https://doc.rust-lang.org/stable/std/marker/struct.PhantomData.html
563[rust151]: https://blog.rust-lang.org/2021/02/26/const-generics-mvp-beta.html
564[dynclone]: https://crates.io/crates/dyn-clone