isr_macros/
offsets.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
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
use crate::Error;

/// A field within a structure.
///
/// `Field` encapsulates the offset and size of a field, enabling type-safe
/// access to structure members. It's primarily used with the [`offsets!`] macro
/// for defining structure layouts and accessing their fields.
///
/// [`offsets!`]: crate::offsets
#[derive(Debug, Clone, Copy)]
pub struct Field {
    /// The offset of the field from the beginning of the structure, in bytes.
    pub offset: u64,

    /// The size of the field, in bytes.
    pub size: u64,
}

/// A bitfield within a structure.
///
/// `Bitfield` provides information about the offset, size, bit position, and
/// bit length of a bitfield member. It extends the functionality of [`Field`]
/// by allowing access to individual bits within a field.
#[derive(Debug, Clone, Copy)]
pub struct Bitfield {
    /// The offset of the bitfield from the beginning of the structure, in bytes.
    pub offset: u64,

    /// The size of the underlying field containing the bitfield, in bytes.
    pub size: u64,

    /// The starting bit position of the bitfield within the underlying field.
    pub bit_position: u64,

    /// The length of the bitfield, in bits.
    pub bit_length: u64,
}

impl Bitfield {
    /// Extracts the bitfield value from a given integer.
    ///
    /// This method performs bitwise operations to isolate and return the
    /// value represented by the bitfield within the provided integer.
    pub fn value_from(&self, value: u64) -> u64 {
        let result = value >> self.bit_position;
        let result = result & ((1 << self.bit_length) - 1);

        #[expect(clippy::let_and_return)]
        result
    }
}

/// A field descriptor.
///
/// This descriptor can be either a [`Field`] or a [`Bitfield`].
#[derive(Debug, Clone)]
pub enum FieldDescriptor {
    /// Represents a regular field.
    Field(Field),

    /// Represents a bitfield.
    Bitfield(Bitfield),
}

impl FieldDescriptor {
    /// Returns the offset of the field or bitfield, in bytes.
    pub fn offset(&self) -> u64 {
        match self {
            FieldDescriptor::Field(field) => field.offset,
            FieldDescriptor::Bitfield(bitfield) => bitfield.offset,
        }
    }

    /// Returns the size of the field or bitfield, in bytes.
    pub fn size(&self) -> u64 {
        match self {
            FieldDescriptor::Field(field) => field.size,
            FieldDescriptor::Bitfield(bitfield) => bitfield.size,
        }
    }
}

impl TryFrom<FieldDescriptor> for u64 {
    type Error = Error;

    fn try_from(value: FieldDescriptor) -> Result<Self, Self::Error> {
        match value {
            FieldDescriptor::Field(field) => Ok(field.offset),
            FieldDescriptor::Bitfield(bitfield) => Ok(bitfield.offset),
        }
    }
}

impl TryFrom<FieldDescriptor> for Field {
    type Error = Error;

    fn try_from(value: FieldDescriptor) -> Result<Self, Self::Error> {
        match value {
            FieldDescriptor::Field(field) => Ok(field),
            FieldDescriptor::Bitfield(_) => {
                Err(Error::Conversion("expected field, found bitfield"))
            }
        }
    }
}

impl TryFrom<FieldDescriptor> for Bitfield {
    type Error = Error;

    fn try_from(value: FieldDescriptor) -> Result<Self, Self::Error> {
        match value {
            FieldDescriptor::Field(_) => Err(Error::Conversion("expected bitfield, found field")),
            FieldDescriptor::Bitfield(bitfield) => Ok(bitfield),
        }
    }
}

//
//
//

pub trait IntoField<T> {
    type Error;

    fn into_field(self) -> Result<T, Error>;
}

impl IntoField<u64> for Result<FieldDescriptor, Error> {
    type Error = Error;

    fn into_field(self) -> Result<u64, Error> {
        self?.try_into()
    }
}

impl IntoField<Field> for Result<FieldDescriptor, Error> {
    type Error = Error;

    fn into_field(self) -> Result<Field, Error> {
        self?.try_into()
    }
}

impl IntoField<Bitfield> for Result<FieldDescriptor, Error> {
    type Error = Error;

    fn into_field(self) -> Result<Bitfield, Error> {
        self?.try_into()
    }
}

impl IntoField<Option<u64>> for Result<FieldDescriptor, Error> {
    type Error = Error;

    fn into_field(self) -> Result<Option<u64>, Error> {
        match self {
            Ok(descriptor) => Ok(Some(descriptor.try_into()?)),
            Err(_) => Ok(None),
        }
    }
}

impl IntoField<Option<Field>> for Result<FieldDescriptor, Error> {
    type Error = Error;

    fn into_field(self) -> Result<Option<Field>, Error> {
        match self {
            Ok(descriptor) => Ok(Some(descriptor.try_into()?)),
            Err(_) => Ok(None),
        }
    }
}

impl IntoField<Option<Bitfield>> for Result<FieldDescriptor, Error> {
    type Error = Error;

    fn into_field(self) -> Result<Option<Bitfield>, Error> {
        match self {
            Ok(descriptor) => Ok(Some(descriptor.try_into()?)),
            Err(_) => Ok(None),
        }
    }
}

/// Defines offsets for members within a structure.
///
/// This macro facilitates type-safe access to structure members in the ISR
/// framework, automatically calculating field offsets and sizes based on
/// provided profile data.
///
/// # Usage
///
/// ```rust
/// # use isr::{
/// #     cache::{Codec as _, JsonCodec},
/// #     macros::{offsets, Bitfield, Field},
/// # };
/// #
/// offsets! {
///     // Defined attributes are applied to each substucture.
///     #[derive(Debug)]
///     pub struct Offsets {
///         struct _EX_FAST_REF {
///             RefCnt: Bitfield,
///             Value: Field,
///         }
///
///         struct _EPROCESS {
///             UniqueProcessId: Field,
///
///             // Define an alternative name for a field.
///             #[isr(alias = "Wow64Process")]
///             WoW64Process: Field,
///
///             // We can even define field names that are present
///             // in the nested structures.
///             Affinity: Field,  // Defined in _KPROCESS
///         }
///     }
/// }
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Use the profile of a Windows 10.0.18362.356 kernel.
/// # let profile = JsonCodec::decode(include_bytes!("../../../tests/assets/cache/windows/ntkrnlmp.pdb/ce7ffb00c20b87500211456b3e905c471/profile.json"))?;
/// let offsets = Offsets::new(&profile)?;
///
/// let refcnt = offsets._EX_FAST_REF.RefCnt.value_from(0x1234567890abcdef);
/// assert_eq!(offsets._EX_FAST_REF.RefCnt.bit_position, 0);
/// assert_eq!(offsets._EX_FAST_REF.RefCnt.bit_length, 4);
/// assert_eq!(refcnt, 0xf);
///
/// assert!(!offsets._EPROCESS.is_empty());
/// assert_eq!(offsets._EPROCESS.len(), 2176);
///
/// // The field with the largest offset + size in the `Offset` struct
/// // is `WoW64Process` (offset 1064, size 8), so the effective length
/// // of the structure is 1072 bytes.
/// assert_eq!(offsets._EPROCESS.effective_len(), 1072);
///
/// assert_eq!(offsets._EPROCESS.UniqueProcessId.offset, 744);
/// assert_eq!(offsets._EPROCESS.UniqueProcessId.size, 8);
///
/// assert_eq!(offsets._EPROCESS.WoW64Process.offset, 1064);
/// assert_eq!(offsets._EPROCESS.WoW64Process.size, 8);
///
/// assert_eq!(offsets._EPROCESS.Affinity.offset, 80);
/// assert_eq!(offsets._EPROCESS.Affinity.size, 168);
/// # Ok(())
/// # }
/// ```
///
/// # Attributes
///
/// - `#[isr(alias = "alternative_name")]`: Specifies an alternative name for a field.
///   This is useful if a field might have a different name across OS builds or
///   kernel versions.
///
/// - `#[isr(alias = ["name1", "name2", ...])]`: Specifies multiple alternative names for
///   a field.
///
/// The generated struct provides a `new` method that takes a reference to an
/// [`Profile`] and returns a [`Result`] containing the populated struct or an
/// error if any fields or structures are not found. Each inner struct also
/// implements the following convenience methods:
/// - `is_empty()`: Returns `true` if the structure has zero size.
/// - `len()`: Returns the size of the structure in bytes.
/// - `effective_len()`: Returns the offset of the last defined field plus its size.
///
/// [`Profile`]: isr_core::Profile
#[macro_export]
macro_rules! offsets {
    (
        $(#[$meta:meta])*
        $vis:vis struct $name:ident {
            $($rest:tt)*
        }
    ) => {
        $crate::offsets!(@outer
            $vis,
            [ $(#[$meta])* ],
            struct $name {
                $($rest)*
            }
        );

        $crate::offsets!(@inner
            $vis,
            [ $(#[$meta])* ],
            $($rest)*
        );
    };

    (@outer
        $vis:vis,
        [$($meta:tt)*],
        struct $name:ident {
            $(
                struct $iname:ident {
                    $(
                        $(#[isr($($fattr:tt)*)])?
                        $fname:ident: $ftype:ty
                    ),* $(,)?
                }
            )+
        }
    ) => {
        #[allow(non_camel_case_types, non_snake_case, missing_docs)]
        $($meta)*
        $vis struct $name {
            $(
                $vis $iname: $iname,
            )*
        }

        impl $name {
            /// Creates a new offsets instance.
            $vis fn new(profile: &$crate::__private::Profile) -> Result<Self, $crate::Error> {
                Ok(Self {
                    $(
                        $iname: $iname::new(profile)?,
                    )+
                })
            }
        }
    };

    (@inner
        $vis:vis,
        [$($meta:tt)*],
        struct $iname:ident {
            $(
                $(#[isr($($fattr:tt)*)])?
                $fname:ident: $ftype:ty
            ),* $(,)?
        }

        $($rest:tt)*
    ) => {
        #[allow(non_camel_case_types, non_snake_case, missing_docs)]
        $($meta)*
        $vis struct $iname {
            $(
                pub $fname: $ftype,
            )*
            __len: usize,
            __effective_len: usize,
        }

        impl $iname {
            #[doc = concat!("Creates a new `", stringify!($iname), "` instance.")]
            $vis fn new(profile: &$crate::__private::Profile) -> Result<Self, $crate::Error> {
                use $crate::__private::IntoField as _;

                let len = profile
                    .struct_size(stringify!($iname))
                    .ok_or($crate::Error::type_not_found(stringify!($iname)))?;
                let mut effective_len: u64 = 0;

                $(
                    effective_len = u64::max(
                        effective_len,
                        match $crate::offsets!(@assign
                            profile,
                            $iname,
                            $fname,
                            [$($($fattr)*)?]
                        ) {
                            Ok(descriptor) => descriptor.size() + descriptor.offset(),
                            Err(_) => 0,
                        }
                    );
                )*

                Ok(Self {
                    $(
                        $fname: $crate::offsets!(@assign
                            profile,
                            $iname,
                            $fname,
                            [$($($fattr)*)?]
                        ).into_field()?,
                    )*
                    __len: len as usize,
                    __effective_len: effective_len as usize,
                })
            }

            /// Returns `true` if the structure does not contain any fields.
            $vis fn is_empty(&self) -> bool {
                self.__len == 0
            }

            /// Returns the size of the structure in bytes.
            $vis fn len(&self) -> usize {
                self.__len
            }

            /// Returns the effective size of the structure in bytes.
            ///
            /// The effective size is the offset of the last defined field plus its size.
            $vis fn effective_len(&self) -> usize {
                self.__effective_len
            }
        }

        $crate::offsets!(@inner
            $vis,
            [$($meta)*],
            $($rest)*
        );
    };

    (@inner
        $vis:vis,
        [$($meta:tt)*],
    ) => {};

    (@assign
        $profile:ident,
        $iname:ident,
        $fname:ident,
        []
    ) => {{
        use $crate::__private::ProfileExt as _;

        $profile
            .find_field_descriptor(stringify!($iname), stringify!($fname))
    }};

    (@assign
        $profile:ident,
        $iname:ident,
        $fname:ident,
        [alias = $alias:literal]
    ) => {{
        use $crate::__private::ProfileExt as _;

        $profile
            .find_field_descriptor(stringify!($iname), stringify!($fname))
            .or_else(|_| $profile
                .find_field_descriptor(stringify!($iname), $alias)
            )
    }};

    (@assign
        $profile:ident,
        $iname:ident,
        $fname:ident,
        [alias = [$($alias:literal),+ $(,)?]]
    ) => {{
        use $crate::__private::ProfileExt as _;

        $profile
            .find_field_descriptor(stringify!($iname), stringify!($fname))
            $(
                .or_else(|_| $profile
                    .find_field_descriptor(stringify!($iname), $alias)
                )
            )+
    }};
}