binaryninja/
type_parser.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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#![allow(unused)]
use binaryninjacore_sys::*;
use std::ffi::{c_char, c_void};
use std::fmt::Debug;
use std::ptr::NonNull;

use crate::platform::Platform;
use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Ref};
use crate::string::{raw_to_string, BnStrCompatible, BnString};
use crate::type_container::TypeContainer;
use crate::types::{QualifiedName, QualifiedNameAndType, Type};

pub type TypeParserErrorSeverity = BNTypeParserErrorSeverity;
pub type TypeParserOption = BNTypeParserOption;

/// Register a custom parser with the API
pub fn register_type_parser<S: BnStrCompatible, T: TypeParser>(
    name: S,
    parser: T,
) -> (&'static mut T, CoreTypeParser) {
    let parser = Box::leak(Box::new(parser));
    let mut callback = BNTypeParserCallbacks {
        context: parser as *mut _ as *mut c_void,
        getOptionText: Some(cb_get_option_text::<T>),
        preprocessSource: Some(cb_preprocess_source::<T>),
        parseTypesFromSource: Some(cb_parse_types_from_source::<T>),
        parseTypeString: Some(cb_parse_type_string::<T>),
        freeString: Some(cb_free_string),
        freeResult: Some(cb_free_result),
        freeErrorList: Some(cb_free_error_list),
    };
    let result = unsafe {
        BNRegisterTypeParser(
            name.into_bytes_with_nul().as_ref().as_ptr() as *const _,
            &mut callback,
        )
    };
    let core = unsafe { CoreTypeParser::from_raw(NonNull::new(result).unwrap()) };
    (parser, core)
}

#[repr(transparent)]
pub struct CoreTypeParser {
    handle: NonNull<BNTypeParser>,
}

impl CoreTypeParser {
    pub(crate) unsafe fn from_raw(handle: NonNull<BNTypeParser>) -> Self {
        Self { handle }
    }

    pub fn parsers() -> Array<CoreTypeParser> {
        let mut count = 0;
        let result = unsafe { BNGetTypeParserList(&mut count) };
        unsafe { Array::new(result, count, ()) }
    }

    pub fn parser_by_name<S: BnStrCompatible>(name: S) -> Option<CoreTypeParser> {
        let name_raw = name.into_bytes_with_nul();
        let result = unsafe { BNGetTypeParserByName(name_raw.as_ref().as_ptr() as *const c_char) };
        NonNull::new(result).map(|x| unsafe { Self::from_raw(x) })
    }

    pub fn name(&self) -> BnString {
        let result = unsafe { BNGetTypeParserName(self.handle.as_ptr()) };
        assert!(!result.is_null());
        unsafe { BnString::from_raw(result) }
    }
}

impl TypeParser for CoreTypeParser {
    fn get_option_text(&self, option: TypeParserOption, value: &str) -> Option<String> {
        let mut output = std::ptr::null_mut();
        let value_cstr = BnString::new(value);
        let result = unsafe {
            BNGetTypeParserOptionText(
                self.handle.as_ptr(),
                option,
                value_cstr.as_ptr(),
                &mut output,
            )
        };
        result.then(|| {
            assert!(!output.is_null());
            value_cstr.to_string()
        })
    }

    fn preprocess_source(
        &self,
        source: &str,
        file_name: &str,
        platform: &Platform,
        existing_types: &TypeContainer,
        options: &[String],
        include_dirs: &[String],
    ) -> Result<String, Vec<TypeParserError>> {
        let source_cstr = BnString::new(source);
        let file_name_cstr = BnString::new(file_name);
        let mut result = std::ptr::null_mut();
        let mut errors = std::ptr::null_mut();
        let mut error_count = 0;
        let success = unsafe {
            BNTypeParserPreprocessSource(
                self.handle.as_ptr(),
                source_cstr.as_ptr(),
                file_name_cstr.as_ptr(),
                platform.handle,
                existing_types.handle.as_ptr(),
                options.as_ptr() as *const *const c_char,
                options.len(),
                include_dirs.as_ptr() as *const *const c_char,
                include_dirs.len(),
                &mut result,
                &mut errors,
                &mut error_count,
            )
        };
        if success {
            assert!(!result.is_null());
            let bn_result = unsafe { BnString::from_raw(result) };
            Ok(bn_result.to_string())
        } else {
            let errors: Array<TypeParserError> = unsafe { Array::new(errors, error_count, ()) };
            Err(errors.to_vec())
        }
    }

    fn parse_types_from_source(
        &self,
        source: &str,
        file_name: &str,
        platform: &Platform,
        existing_types: &TypeContainer,
        options: &[String],
        include_dirs: &[String],
        auto_type_source: &str,
    ) -> Result<TypeParserResult, Vec<TypeParserError>> {
        let source_cstr = BnString::new(source);
        let file_name_cstr = BnString::new(file_name);
        let auto_type_source = BnString::new(auto_type_source);
        let mut raw_result = BNTypeParserResult::default();
        let mut errors = std::ptr::null_mut();
        let mut error_count = 0;
        let success = unsafe {
            BNTypeParserParseTypesFromSource(
                self.handle.as_ptr(),
                source_cstr.as_ptr(),
                file_name_cstr.as_ptr(),
                platform.handle,
                existing_types.handle.as_ptr(),
                options.as_ptr() as *const *const c_char,
                options.len(),
                include_dirs.as_ptr() as *const *const c_char,
                include_dirs.len(),
                auto_type_source.as_ptr(),
                &mut raw_result,
                &mut errors,
                &mut error_count,
            )
        };
        if success {
            let result = TypeParserResult::from_raw(&raw_result);
            // NOTE: This is safe because the core allocated the TypeParserResult
            TypeParserResult::free_raw(raw_result);
            Ok(result)
        } else {
            let errors: Array<TypeParserError> = unsafe { Array::new(errors, error_count, ()) };
            Err(errors.to_vec())
        }
    }

    fn parse_type_string(
        &self,
        source: &str,
        platform: &Platform,
        existing_types: &TypeContainer,
    ) -> Result<QualifiedNameAndType, Vec<TypeParserError>> {
        let source_cstr = BnString::new(source);
        let mut output = BNQualifiedNameAndType::default();
        let mut errors = std::ptr::null_mut();
        let mut error_count = 0;
        let result = unsafe {
            BNTypeParserParseTypeString(
                self.handle.as_ptr(),
                source_cstr.as_ptr(),
                platform.handle,
                existing_types.handle.as_ptr(),
                &mut output,
                &mut errors,
                &mut error_count,
            )
        };
        if result {
            Ok(QualifiedNameAndType::from_owned_raw(output))
        } else {
            let errors: Array<TypeParserError> = unsafe { Array::new(errors, error_count, ()) };
            Err(errors.to_vec())
        }
    }
}

impl Default for CoreTypeParser {
    fn default() -> Self {
        // TODO: This should return a ref
        unsafe { Self::from_raw(NonNull::new(BNGetDefaultTypeParser()).unwrap()) }
    }
}

// TODO: Impl this on platform.
pub trait TypeParser {
    /// Get the string representation of an option for passing to parse_type_*.
    /// Returns a string representing the option if the parser supports it,
    /// otherwise None
    ///
    /// * `option` - Option type
    /// * `value` - Option value
    fn get_option_text(&self, option: TypeParserOption, value: &str) -> Option<String>;

    /// Preprocess a block of source, returning the source that would be parsed
    ///
    /// * `source` - Source code to process
    /// * `file_name` - Name of the file containing the source (does not need to exist on disk)
    /// * `platform` - Platform to assume the source is relevant to
    /// * `existing_types` - Optional collection of all existing types to use for parsing context
    /// * `options` - Optional string arguments to pass as options, e.g. command line arguments
    /// * `include_dirs` - Optional list of directories to include in the header search path
    fn preprocess_source(
        &self,
        source: &str,
        file_name: &str,
        platform: &Platform,
        existing_types: &TypeContainer,
        options: &[String],
        include_dirs: &[String],
    ) -> Result<String, Vec<TypeParserError>>;

    /// Parse an entire block of source into types, variables, and functions
    ///
    /// * `source` - Source code to parse
    /// * `file_name` - Name of the file containing the source (optional: exists on disk)
    /// * `platform` - Platform to assume the types are relevant to
    /// * `existing_types` - Optional container of all existing types to use for parsing context
    /// * `options` - Optional string arguments to pass as options, e.g. command line arguments
    /// * `include_dirs` - Optional list of directories to include in the header search path
    /// * `auto_type_source` - Optional source of types if used for automatically generated types
    fn parse_types_from_source(
        &self,
        source: &str,
        file_name: &str,
        platform: &Platform,
        existing_types: &TypeContainer,
        options: &[String],
        include_dirs: &[String],
        auto_type_source: &str,
    ) -> Result<TypeParserResult, Vec<TypeParserError>>;

    /// Parse a single type and name from a string containing their definition.
    ///
    /// * `source` - Source code to parse
    /// * `platform` - Platform to assume the types are relevant to
    /// * `existing_types` - Optional container of all existing types to use for parsing context
    fn parse_type_string(
        &self,
        source: &str,
        platform: &Platform,
        existing_types: &TypeContainer,
    ) -> Result<QualifiedNameAndType, Vec<TypeParserError>>;
}

impl CoreArrayProvider for CoreTypeParser {
    type Raw = *mut BNTypeParser;
    type Context = ();
    type Wrapped<'a> = Self;
}

unsafe impl CoreArrayProviderInner for CoreTypeParser {
    unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
        BNFreeTypeParserList(raw)
    }

    unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
        // TODO: Because handle is a NonNull we should prob make Self::Raw that as well...
        let handle = NonNull::new(*raw).unwrap();
        CoreTypeParser::from_raw(handle)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TypeParserError {
    pub severity: TypeParserErrorSeverity,
    pub message: String,
    pub file_name: String,
    pub line: u64,
    pub column: u64,
}

impl TypeParserError {
    pub(crate) fn from_raw(value: &BNTypeParserError) -> Self {
        Self {
            severity: value.severity,
            message: raw_to_string(value.message).unwrap(),
            file_name: raw_to_string(value.fileName).unwrap(),
            line: value.line,
            column: value.column,
        }
    }

    pub(crate) fn from_owned_raw(value: BNTypeParserError) -> Self {
        let owned = Self::from_raw(&value);
        Self::free_raw(value);
        owned
    }

    pub(crate) fn into_raw(value: Self) -> BNTypeParserError {
        BNTypeParserError {
            severity: value.severity,
            message: BnString::into_raw(BnString::new(value.message)),
            fileName: BnString::into_raw(BnString::new(value.file_name)),
            line: value.line,
            column: value.column,
        }
    }

    pub(crate) fn free_raw(value: BNTypeParserError) {
        let _ = unsafe { BnString::from_raw(value.message) };
        let _ = unsafe { BnString::from_raw(value.fileName) };
    }

    pub fn new(
        severity: TypeParserErrorSeverity,
        message: String,
        file_name: String,
        line: u64,
        column: u64,
    ) -> Self {
        Self {
            severity,
            message,
            file_name,
            line,
            column,
        }
    }
}

impl CoreArrayProvider for TypeParserError {
    type Raw = BNTypeParserError;
    type Context = ();
    type Wrapped<'a> = Self;
}

unsafe impl CoreArrayProviderInner for TypeParserError {
    unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
        unsafe { BNFreeTypeParserErrors(raw, count) }
    }

    unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
        Self::from_raw(raw)
    }
}

#[derive(Debug, Eq, PartialEq, Default)]
pub struct TypeParserResult {
    pub types: Vec<ParsedType>,
    pub variables: Vec<ParsedType>,
    pub functions: Vec<ParsedType>,
}

impl TypeParserResult {
    pub(crate) fn from_raw(value: &BNTypeParserResult) -> Self {
        let raw_types = unsafe { std::slice::from_raw_parts(value.types, value.typeCount) };
        let types = raw_types.iter().map(ParsedType::from_raw).collect();
        let raw_variables =
            unsafe { std::slice::from_raw_parts(value.variables, value.variableCount) };
        let variables = raw_variables.iter().map(ParsedType::from_raw).collect();
        let raw_functions =
            unsafe { std::slice::from_raw_parts(value.functions, value.functionCount) };
        let functions = raw_functions.iter().map(ParsedType::from_raw).collect();
        TypeParserResult {
            types,
            variables,
            functions,
        }
    }

    /// Return a rust allocated type parser result, free using [`Self::free_owned_raw`].
    ///
    /// Under no circumstance should you call [`Self::free_raw`] on the returned result.
    pub(crate) fn into_raw(value: Self) -> BNTypeParserResult {
        let boxed_raw_types: Box<[BNParsedType]> = value
            .types
            .into_iter()
            // NOTE: Freed with [`Self::free_owned_raw`].
            .map(ParsedType::into_raw)
            .collect();
        let boxed_raw_variables: Box<[BNParsedType]> = value
            .variables
            .into_iter()
            // NOTE: Freed with [`Self::free_owned_raw`].
            .map(ParsedType::into_raw)
            .collect();
        let boxed_raw_functions: Box<[BNParsedType]> = value
            .functions
            .into_iter()
            // NOTE: Freed with [`Self::free_owned_raw`].
            .map(ParsedType::into_raw)
            .collect();
        BNTypeParserResult {
            typeCount: boxed_raw_types.len(),
            // NOTE: Freed with [`Self::free_owned_raw`].
            types: Box::leak(boxed_raw_types).as_mut_ptr(),
            variableCount: boxed_raw_variables.len(),
            // NOTE: Freed with [`Self::free_owned_raw`].
            variables: Box::leak(boxed_raw_variables).as_mut_ptr(),
            functionCount: boxed_raw_functions.len(),
            // NOTE: Freed with [`Self::free_owned_raw`].
            functions: Box::leak(boxed_raw_functions).as_mut_ptr(),
        }
    }

    pub(crate) fn free_raw(mut value: BNTypeParserResult) {
        // SAFETY: `value` must be a properly initialized BNTypeParserResult.
        // SAFETY: `value` must be core allocated.
        unsafe { BNFreeTypeParserResult(&mut value) };
    }

    pub(crate) fn free_owned_raw(value: BNTypeParserResult) {
        let raw_types = std::ptr::slice_from_raw_parts_mut(value.types, value.typeCount);
        // Free the rust allocated types list
        let boxed_types = unsafe { Box::from_raw(raw_types) };
        for parsed_type in boxed_types {
            ParsedType::free_raw(parsed_type);
        }
        let raw_variables =
            std::ptr::slice_from_raw_parts_mut(value.variables, value.variableCount);
        // Free the rust allocated variables list
        let boxed_variables = unsafe { Box::from_raw(raw_variables) };
        for parsed_type in boxed_variables {
            ParsedType::free_raw(parsed_type);
        }
        let raw_functions =
            std::ptr::slice_from_raw_parts_mut(value.functions, value.functionCount);
        // Free the rust allocated functions list
        let boxed_functions = unsafe { Box::from_raw(raw_functions) };
        for parsed_type in boxed_functions {
            ParsedType::free_raw(parsed_type);
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ParsedType {
    name: QualifiedName,
    ty: Ref<Type>,
    user: bool,
}

impl ParsedType {
    pub(crate) fn from_raw(value: &BNParsedType) -> Self {
        Self {
            name: QualifiedName::from_raw(&value.name),
            ty: unsafe { Type::from_raw(value.type_).to_owned() },
            user: value.isUser,
        }
    }

    pub(crate) fn from_owned_raw(value: BNParsedType) -> Self {
        let owned = Self::from_raw(&value);
        Self::free_raw(value);
        owned
    }

    pub(crate) fn into_raw(value: Self) -> BNParsedType {
        BNParsedType {
            name: QualifiedName::into_raw(value.name),
            type_: unsafe { Ref::into_raw(value.ty) }.handle,
            isUser: value.user,
        }
    }

    pub(crate) fn free_raw(value: BNParsedType) {
        QualifiedName::free_raw(value.name);
        let _ = unsafe { Type::ref_from_raw(value.type_) };
    }

    pub fn new(name: QualifiedName, ty: Ref<Type>, user: bool) -> Self {
        Self { name, ty, user }
    }
}

impl CoreArrayProvider for ParsedType {
    type Raw = BNParsedType;
    type Context = ();
    type Wrapped<'b> = Self;
}

unsafe impl CoreArrayProviderInner for ParsedType {
    unsafe fn free(_raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {
        // Expected to be freed with BNFreeTypeParserResult
        // TODO ^ because of the above, we should not provide an array provider for this
    }

    unsafe fn wrap_raw<'b>(raw: &'b Self::Raw, _context: &'b Self::Context) -> Self::Wrapped<'b> {
        ParsedType::from_raw(raw)
    }
}

unsafe extern "C" fn cb_get_option_text<T: TypeParser>(
    ctxt: *mut ::std::os::raw::c_void,
    option: BNTypeParserOption,
    value: *const c_char,
    result: *mut *mut c_char,
) -> bool {
    let ctxt: &mut T = &mut *(ctxt as *mut T);
    if let Some(inner_result) = ctxt.get_option_text(option, &raw_to_string(value).unwrap()) {
        let bn_inner_result = BnString::new(inner_result);
        // NOTE: Dropped by `cb_free_string`
        *result = BnString::into_raw(bn_inner_result);
        true
    } else {
        *result = std::ptr::null_mut();
        false
    }
}

unsafe extern "C" fn cb_preprocess_source<T: TypeParser>(
    ctxt: *mut c_void,
    source: *const c_char,
    file_name: *const c_char,
    platform: *mut BNPlatform,
    existing_types: *mut BNTypeContainer,
    options: *const *const c_char,
    option_count: usize,
    include_dirs: *const *const c_char,
    include_dir_count: usize,
    result: *mut *mut c_char,
    errors: *mut *mut BNTypeParserError,
    error_count: *mut usize,
) -> bool {
    let ctxt: &mut T = &mut *(ctxt as *mut T);
    let platform = Platform { handle: platform };
    let existing_types_ptr = NonNull::new(existing_types).unwrap();
    let existing_types = TypeContainer::from_raw(existing_types_ptr);
    let options_raw = unsafe { std::slice::from_raw_parts(options, option_count) };
    let options: Vec<_> = options_raw
        .iter()
        .filter_map(|&r| raw_to_string(r))
        .collect();
    let includes_raw = unsafe { std::slice::from_raw_parts(include_dirs, include_dir_count) };
    let includes: Vec<_> = includes_raw
        .iter()
        .filter_map(|&r| raw_to_string(r))
        .collect();
    match ctxt.preprocess_source(
        &raw_to_string(source).unwrap(),
        &raw_to_string(file_name).unwrap(),
        &platform,
        &existing_types,
        &options,
        &includes,
    ) {
        Ok(inner_result) => {
            let bn_inner_result = BnString::new(inner_result);
            // NOTE: Dropped by `cb_free_string`
            *result = BnString::into_raw(bn_inner_result);
            *errors = std::ptr::null_mut();
            *error_count = 0;
            true
        }
        Err(inner_errors) => {
            *result = std::ptr::null_mut();
            *error_count = inner_errors.len();
            // NOTE: Leaking errors here, dropped by `cb_free_error_list`.
            let inner_errors: Box<[_]> = inner_errors
                .into_iter()
                .map(TypeParserError::into_raw)
                .collect();
            // NOTE: Dropped by `cb_free_error_list`
            *errors = Box::leak(inner_errors).as_mut_ptr();
            false
        }
    }
}

unsafe extern "C" fn cb_parse_types_from_source<T: TypeParser>(
    ctxt: *mut c_void,
    source: *const c_char,
    file_name: *const c_char,
    platform: *mut BNPlatform,
    existing_types: *mut BNTypeContainer,
    options: *const *const c_char,
    option_count: usize,
    include_dirs: *const *const c_char,
    include_dir_count: usize,
    auto_type_source: *const c_char,
    result: *mut BNTypeParserResult,
    errors: *mut *mut BNTypeParserError,
    error_count: *mut usize,
) -> bool {
    let ctxt: &mut T = &mut *(ctxt as *mut T);
    let platform = Platform { handle: platform };
    let existing_types_ptr = NonNull::new(existing_types).unwrap();
    let existing_types = TypeContainer::from_raw(existing_types_ptr);
    let options_raw = unsafe { std::slice::from_raw_parts(options, option_count) };
    let options: Vec<_> = options_raw
        .iter()
        .filter_map(|&r| raw_to_string(r))
        .collect();
    let includes_raw = unsafe { std::slice::from_raw_parts(include_dirs, include_dir_count) };
    let includes: Vec<_> = includes_raw
        .iter()
        .filter_map(|&r| raw_to_string(r))
        .collect();
    match ctxt.parse_types_from_source(
        &raw_to_string(source).unwrap(),
        &raw_to_string(file_name).unwrap(),
        &platform,
        &existing_types,
        &options,
        &includes,
        &raw_to_string(auto_type_source).unwrap(),
    ) {
        Ok(type_parser_result) => {
            *result = TypeParserResult::into_raw(type_parser_result);
            *errors = std::ptr::null_mut();
            *error_count = 0;
            true
        }
        Err(inner_errors) => {
            *error_count = inner_errors.len();
            let inner_errors: Box<[_]> = inner_errors
                .into_iter()
                .map(TypeParserError::into_raw)
                .collect();
            *result = Default::default();
            // NOTE: Dropped by cb_free_error_list
            *errors = Box::leak(inner_errors).as_mut_ptr();
            false
        }
    }
}

unsafe extern "C" fn cb_parse_type_string<T: TypeParser>(
    ctxt: *mut c_void,
    source: *const c_char,
    platform: *mut BNPlatform,
    existing_types: *mut BNTypeContainer,
    result: *mut BNQualifiedNameAndType,
    errors: *mut *mut BNTypeParserError,
    error_count: *mut usize,
) -> bool {
    let ctxt: &mut T = &mut *(ctxt as *mut T);
    let platform = Platform { handle: platform };
    let existing_types_ptr = NonNull::new(existing_types).unwrap();
    let existing_types = TypeContainer::from_raw(existing_types_ptr);
    match ctxt.parse_type_string(&raw_to_string(source).unwrap(), &platform, &existing_types) {
        Ok(inner_result) => {
            *result = QualifiedNameAndType::into_raw(inner_result);
            *errors = std::ptr::null_mut();
            *error_count = 0;
            true
        }
        Err(inner_errors) => {
            *error_count = inner_errors.len();
            let inner_errors: Box<[_]> = inner_errors
                .into_iter()
                .map(TypeParserError::into_raw)
                .collect();
            *result = Default::default();
            // NOTE: Dropped by cb_free_error_list
            *errors = Box::leak(inner_errors).as_mut_ptr();
            false
        }
    }
}

unsafe extern "C" fn cb_free_string(_ctxt: *mut c_void, string: *mut c_char) {
    // SAFETY: The returned string is just BnString
    let _ = BnString::from_raw(string);
}

unsafe extern "C" fn cb_free_result(_ctxt: *mut c_void, result: *mut BNTypeParserResult) {
    TypeParserResult::free_owned_raw(*result);
}

unsafe extern "C" fn cb_free_error_list(
    _ctxt: *mut c_void,
    errors: *mut BNTypeParserError,
    error_count: usize,
) {
    let errors = std::ptr::slice_from_raw_parts_mut(errors, error_count);
    let boxed_errors = Box::from_raw(errors);
    for error in boxed_errors {
        TypeParserError::free_raw(error);
    }
}