ghidra 0.0.2

Typed Rust bindings for an embedded Ghidra JVM
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
use std::collections::BTreeMap;

use thiserror::Error;

use crate::{
    ProgramFunctionSignature, ProgramMetadata, ProgramParameter, ProgramType, ProgramTypeComponent,
    ProgramTypeDetails,
};

/// Error returned when building or traversing a program type index.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum ProgramTypeIndexError {
    #[error("program type id `{id}` appears more than once")]
    DuplicateId { id: String },
    #[error("program type id `{id}` is not present in the type table")]
    MissingType { id: String },
    #[error("program type id `{id}` has kind `{actual}`, expected {expected}")]
    UnexpectedKind {
        id: String,
        expected: &'static str,
        actual: &'static str,
    },
}

/// Borrowed index for resolving `ProgramType` records by ID.
#[derive(Debug, Clone)]
pub struct ProgramTypeIndex<'a> {
    by_id: BTreeMap<&'a str, &'a ProgramType>,
}

impl<'a> ProgramTypeIndex<'a> {
    /// Builds an index over a program type table.
    pub fn new(types: &'a [ProgramType]) -> Result<Self, ProgramTypeIndexError> {
        let mut by_id = BTreeMap::new();
        for type_ref in types {
            if by_id.insert(type_ref.id.as_str(), type_ref).is_some() {
                return Err(ProgramTypeIndexError::DuplicateId {
                    id: type_ref.id.clone(),
                });
            }
        }
        Ok(Self { by_id })
    }

    /// Builds an index over the type table in program metadata.
    pub fn from_metadata(metadata: &'a ProgramMetadata) -> Result<Self, ProgramTypeIndexError> {
        Self::new(&metadata.types)
    }

    /// Returns the number of indexed types.
    pub fn len(&self) -> usize {
        self.by_id.len()
    }

    /// Returns whether the index is empty.
    pub fn is_empty(&self) -> bool {
        self.by_id.is_empty()
    }

    /// Iterates indexed types in ID order.
    pub fn iter(&self) -> impl DoubleEndedIterator<Item = &'a ProgramType> + '_ {
        self.by_id.values().copied()
    }

    /// Returns a type by ID.
    pub fn get(&self, id: &str) -> Option<&'a ProgramType> {
        self.by_id.get(id).copied()
    }

    /// Returns a type by ID or reports a missing reference.
    pub fn require(&self, id: &str) -> Result<&'a ProgramType, ProgramTypeIndexError> {
        self.get(id)
            .ok_or_else(|| ProgramTypeIndexError::MissingType { id: id.to_string() })
    }

    /// Resolves the pointee of a pointer type.
    pub fn pointee(&self, id: &str) -> Result<Option<&'a ProgramType>, ProgramTypeIndexError> {
        let type_ref = self.require(id)?;
        match &type_ref.details {
            ProgramTypeDetails::Pointer { pointee_type_id } => self.optional_type(pointee_type_id),
            details => Err(unexpected_kind(type_ref, "pointer", details)),
        }
    }

    /// Resolves the element type of an array type.
    pub fn array_element(
        &self,
        id: &str,
    ) -> Result<Option<&'a ProgramType>, ProgramTypeIndexError> {
        let type_ref = self.require(id)?;
        match &type_ref.details {
            ProgramTypeDetails::Array {
                element_type_id, ..
            } => self.optional_type(element_type_id),
            details => Err(unexpected_kind(type_ref, "array", details)),
        }
    }

    /// Resolves the base type of a typedef.
    pub fn typedef_base(&self, id: &str) -> Result<Option<&'a ProgramType>, ProgramTypeIndexError> {
        let type_ref = self.require(id)?;
        match &type_ref.details {
            ProgramTypeDetails::Typedef { base_type_id } => self.optional_type(base_type_id),
            details => Err(unexpected_kind(type_ref, "typedef", details)),
        }
    }

    /// Resolves the base type of a bitfield.
    pub fn bitfield_base(
        &self,
        id: &str,
    ) -> Result<Option<&'a ProgramType>, ProgramTypeIndexError> {
        let type_ref = self.require(id)?;
        match &type_ref.details {
            ProgramTypeDetails::Bitfield { base_type_id, .. } => self.optional_type(base_type_id),
            details => Err(unexpected_kind(type_ref, "bitfield", details)),
        }
    }

    /// Resolves a structure or union component type.
    pub fn component_type(
        &self,
        component: &ProgramTypeComponent,
    ) -> Result<Option<&'a ProgramType>, ProgramTypeIndexError> {
        self.optional_type(&component.type_id)
    }

    /// Resolves a function signature return type.
    pub fn signature_return_type(
        &self,
        signature: &ProgramFunctionSignature,
    ) -> Result<Option<&'a ProgramType>, ProgramTypeIndexError> {
        self.optional_type(&signature.return_type_id)
    }

    /// Resolves a function parameter type.
    pub fn parameter_type(
        &self,
        parameter: &ProgramParameter,
    ) -> Result<Option<&'a ProgramType>, ProgramTypeIndexError> {
        self.optional_type(&parameter.type_id)
    }

    /// Validates that every nested type reference resolves.
    pub fn validate_references(&self) -> Result<(), ProgramTypeIndexError> {
        for type_ref in self.iter() {
            match &type_ref.details {
                ProgramTypeDetails::Builtin | ProgramTypeDetails::Unknown => {}
                ProgramTypeDetails::Pointer { pointee_type_id } => {
                    self.require_optional(pointee_type_id)?;
                }
                ProgramTypeDetails::Array {
                    element_type_id, ..
                } => {
                    self.require_optional(element_type_id)?;
                }
                ProgramTypeDetails::Structure { components }
                | ProgramTypeDetails::Union { components } => {
                    for component in components {
                        self.component_type(component)?;
                    }
                }
                ProgramTypeDetails::Enum { .. } => {}
                ProgramTypeDetails::Typedef { base_type_id } => {
                    self.require_optional(base_type_id)?;
                }
                ProgramTypeDetails::FunctionDefinition { signature } => {
                    self.signature_return_type(signature)?;
                    for parameter in &signature.parameters {
                        self.parameter_type(parameter)?;
                    }
                }
                ProgramTypeDetails::Bitfield { base_type_id, .. } => {
                    self.require_optional(base_type_id)?;
                }
            }
        }
        Ok(())
    }

    fn optional_type(
        &self,
        id: &Option<String>,
    ) -> Result<Option<&'a ProgramType>, ProgramTypeIndexError> {
        id.as_deref().map(|id| self.require(id)).transpose()
    }

    fn require_optional(&self, id: &Option<String>) -> Result<(), ProgramTypeIndexError> {
        self.optional_type(id).map(|_| ())
    }
}

impl ProgramMetadata {
    /// Builds a borrowed index for this metadata's type table.
    pub fn type_index(&self) -> Result<ProgramTypeIndex<'_>, ProgramTypeIndexError> {
        ProgramTypeIndex::from_metadata(self)
    }
}

fn unexpected_kind(
    type_ref: &ProgramType,
    expected: &'static str,
    actual: &ProgramTypeDetails,
) -> ProgramTypeIndexError {
    ProgramTypeIndexError::UnexpectedKind {
        id: type_ref.id.clone(),
        expected,
        actual: kind_name(actual),
    }
}

fn kind_name(details: &ProgramTypeDetails) -> &'static str {
    match details {
        ProgramTypeDetails::Builtin => "builtin",
        ProgramTypeDetails::Unknown => "unknown",
        ProgramTypeDetails::Pointer { .. } => "pointer",
        ProgramTypeDetails::Array { .. } => "array",
        ProgramTypeDetails::Structure { .. } => "structure",
        ProgramTypeDetails::Union { .. } => "union",
        ProgramTypeDetails::Enum { .. } => "enum",
        ProgramTypeDetails::Typedef { .. } => "typedef",
        ProgramTypeDetails::FunctionDefinition { .. } => "function_definition",
        ProgramTypeDetails::Bitfield { .. } => "bitfield",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ProgramFunctionSignature, ProgramParameter};

    #[test]
    fn rejects_duplicate_type_ids() {
        let types = vec![builtin("type:int"), builtin("type:int")];

        assert_eq!(
            ProgramTypeIndex::new(&types).expect_err("duplicate id is rejected"),
            ProgramTypeIndexError::DuplicateId {
                id: "type:int".to_string()
            }
        );
    }

    #[test]
    fn resolves_common_nested_type_references() {
        let types = vec![
            builtin("type:int"),
            ProgramType {
                id: "type:int_ptr".to_string(),
                name: "int *".to_string(),
                display_name: "int *".to_string(),
                size: 8,
                alignment: 8,
                category_path: None,
                details: ProgramTypeDetails::Pointer {
                    pointee_type_id: Some("type:int".to_string()),
                },
            },
            ProgramType {
                id: "type:int_array".to_string(),
                name: "int[4]".to_string(),
                display_name: "int[4]".to_string(),
                size: 16,
                alignment: 4,
                category_path: None,
                details: ProgramTypeDetails::Array {
                    element_type_id: Some("type:int".to_string()),
                    element_count: 4,
                    element_size: 4,
                },
            },
            ProgramType {
                id: "type:int_alias".to_string(),
                name: "int_alias".to_string(),
                display_name: "int_alias".to_string(),
                size: 4,
                alignment: 4,
                category_path: None,
                details: ProgramTypeDetails::Typedef {
                    base_type_id: Some("type:int".to_string()),
                },
            },
            ProgramType {
                id: "type:int_bit".to_string(),
                name: "int:3".to_string(),
                display_name: "int:3".to_string(),
                size: 1,
                alignment: 1,
                category_path: None,
                details: ProgramTypeDetails::Bitfield {
                    base_type_id: Some("type:int".to_string()),
                    bit_size: 3,
                    bit_offset: 0,
                    storage_size: 1,
                },
            },
            ProgramType {
                id: "type:record".to_string(),
                name: "Record".to_string(),
                display_name: "Record".to_string(),
                size: 4,
                alignment: 4,
                category_path: None,
                details: ProgramTypeDetails::Structure {
                    components: vec![component("value", "type:int")],
                },
            },
            ProgramType {
                id: "type:callback".to_string(),
                name: "callback".to_string(),
                display_name: "callback".to_string(),
                size: 1,
                alignment: 1,
                category_path: None,
                details: ProgramTypeDetails::FunctionDefinition {
                    signature: signature(),
                },
            },
        ];
        let index = ProgramTypeIndex::new(&types).expect("index builds");

        assert_eq!(index.len(), types.len());
        assert_eq!(
            index.pointee("type:int_ptr").unwrap().unwrap().id,
            "type:int"
        );
        assert_eq!(
            index.array_element("type:int_array").unwrap().unwrap().id,
            "type:int"
        );
        assert_eq!(
            index.typedef_base("type:int_alias").unwrap().unwrap().id,
            "type:int"
        );
        assert_eq!(
            index.bitfield_base("type:int_bit").unwrap().unwrap().id,
            "type:int"
        );
        let components = match &index.require("type:record").unwrap().details {
            ProgramTypeDetails::Structure { components } => components,
            _ => panic!("expected structure"),
        };
        assert_eq!(
            index.component_type(&components[0]).unwrap().unwrap().id,
            "type:int"
        );
        let signature = match &index.require("type:callback").unwrap().details {
            ProgramTypeDetails::FunctionDefinition { signature } => signature,
            _ => panic!("expected function definition"),
        };
        assert_eq!(
            index.signature_return_type(signature).unwrap().unwrap().id,
            "type:int"
        );
        assert_eq!(
            index
                .parameter_type(&signature.parameters[0])
                .unwrap()
                .unwrap()
                .id,
            "type:int"
        );
        index.validate_references().unwrap();
    }

    #[test]
    fn reports_missing_nested_references() {
        let types = vec![ProgramType {
            id: "type:int_ptr".to_string(),
            name: "int *".to_string(),
            display_name: "int *".to_string(),
            size: 8,
            alignment: 8,
            category_path: None,
            details: ProgramTypeDetails::Pointer {
                pointee_type_id: Some("type:missing".to_string()),
            },
        }];
        let index = ProgramTypeIndex::new(&types).expect("index builds");

        assert_eq!(
            index.validate_references(),
            Err(ProgramTypeIndexError::MissingType {
                id: "type:missing".to_string()
            })
        );
    }

    #[test]
    fn reports_unexpected_kind_for_specific_traversal() {
        let types = vec![builtin("type:int")];
        let index = ProgramTypeIndex::new(&types).expect("index builds");

        assert_eq!(
            index.pointee("type:int"),
            Err(ProgramTypeIndexError::UnexpectedKind {
                id: "type:int".to_string(),
                expected: "pointer",
                actual: "builtin"
            })
        );
    }

    #[test]
    fn builds_from_metadata() {
        let metadata = ProgramMetadata {
            symbols: Vec::new(),
            functions: Vec::new(),
            types: vec![builtin("type:int")],
        };

        assert_eq!(metadata.type_index().unwrap().len(), 1);
    }

    fn builtin(id: &str) -> ProgramType {
        ProgramType {
            id: id.to_string(),
            name: id.to_string(),
            display_name: id.to_string(),
            size: 4,
            alignment: 4,
            category_path: None,
            details: ProgramTypeDetails::Builtin,
        }
    }

    fn component(name: &str, type_id: &str) -> ProgramTypeComponent {
        ProgramTypeComponent {
            ordinal: 0,
            name: name.to_string(),
            offset: 0,
            length: 4,
            type_id: Some(type_id.to_string()),
            bit_size: None,
            bit_offset: None,
            comment: None,
        }
    }

    fn signature() -> ProgramFunctionSignature {
        ProgramFunctionSignature {
            display: "int callback(int value)".to_string(),
            calling_convention: "__stdcall".to_string(),
            return_type_id: Some("type:int".to_string()),
            parameters: vec![ProgramParameter {
                ordinal: 0,
                name: "value".to_string(),
                type_id: Some("type:int".to_string()),
                storage: "unknown".to_string(),
            }],
            varargs: false,
            no_return: false,
        }
    }
}