panda-re 0.49.0

The official library for interfacing with PANDA (Platform for Architecture-Neutral Dynamic Analysis)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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
//! Taint analysis API
//!
//! This module provides a series of helpers designed for using the [`taint2`] PANDA plugin in order
//! to perform [dynamic taint analysis] in order to help track
//!
//! [`taint2`]: https://github.com/panda-re/panda/tree/dev/panda/plugins/taint2
//! [dynamic taint analysis]: https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.681.4094&rep=rep1&type=pdf
//!
//! ## Example
//!
//! ```no_run
//! use panda::taint;
//! use panda::regs::Reg;
//!
//! // show all registers are untainted
//! for reg in [Reg::RAX, Reg::RBX, Reg::RCX, Reg::RDX] {
//!     println!("{:?} is tained? {:?}", reg, taint::check_reg(reg));
//! }
//!
//! println!("Tainting RAX...");
//! taint::label_reg(Reg::RAX, 1);
//!
//! // ...
//!
//! // show taint has propagated to any values effected by the opterations performed on RAX
//! for reg in [Reg::RAX, Reg::RBX, Reg::RCX, Reg::RDX] {
//!     println!("{:?} is tained? {:?}", reg, taint::check_reg(reg));
//! }
//! ```
//!
//! ([Full Example](https://github.com/panda-re/panda-rs/blob/master/panda-rs/examples/unicorn_taint.rs))

use crate::api::regs::Reg;
use crate::plugin_import;
use crate::sys::{target_ptr_t, CPUState};

use std::collections::HashSet;
use std::ops::Range;
use std::os::raw::{c_int, c_long, c_void};
use std::ptr;
use std::sync::Once;

plugin_import! {
    /// Direct access to the taint2 C API when direct use is needed
    static TAINT: Taint = extern "taint2" {
        fn taint2_enable_taint();
        fn taint2_enable_tainted_pointer();
        fn taint2_enabled() -> bool;

        fn taint2_label_addr(a: Addr, offset: c_int, label: u32);
        fn taint2_label_ram(ram_offset: u64, label: u32);
        fn taint2_label_reg(reg_num: c_int, offset: c_int, label: u32);
        fn taint2_label_io(ia: u64, label: u32);
        fn taint2_label_ram_additive(ram_offset: u64, label: u32);
        fn taint2_label_reg_additive(reg_num: c_int, offset: c_int, label: u32);
        fn taint2_label_io_additive(ia: u64, label: u32);
        fn taint2_add_taint_ram_pos(cpu: &mut CPUState, addr: u64, length: u32, start_label: u32);
        fn taint2_add_taint_ram_single_label(cpu: &mut CPUState, addr: u64, length: u32, label: c_long);

        fn taint2_delete_ram(ram_offset: u64);
        fn taint2_delete_reg(reg_num: c_int, offset: c_int);
        fn taint2_delete_io(ia: u64);

        fn taint2_query_pandalog(addr: Addr, offset: u32) -> *mut c_void;
        fn pandalog_taint_query_free(tq: *mut c_void);

        fn taint2_query(addr: Addr) -> u32;
        fn taint2_query_reg(reg_num: c_int, offset: c_int) -> u32;
        fn taint2_query_ram(ram_offset: u64) -> u32;
        fn taint2_query_laddr(la: u64, off: u64) -> u32;
        fn taint2_query_io(ia: u64) -> u32;
        fn taint2_query_llvm(reg_num: c_int, offset: c_int) -> u32;

        fn taint2_query_set_a(a: Addr, out: &mut *mut u32, outsz: &mut u32) -> u32;

        fn taint2_query_set(a: Addr, out: *mut u32);
        fn taint2_query_set_ram(ram_offset: u64, out: *mut u32);
        fn taint2_query_set_reg(reg_num: c_int, offset: c_int, out: *mut u32);
        fn taint2_query_set_io(ia: u64, out: *mut u32);

        fn taint2_query_tcn(a: Addr) -> u32;
        fn taint2_query_tcn_ram(ram_offset: u64) -> u32;
        fn taint2_query_tcn_reg(reg_num: c_int, offset: c_int) -> u32;
        fn taint2_query_tcn_io(ia: u64) -> u32;
        fn taint2_query_tcn_llvm(reg_num: c_int, offset: c_int) -> u32;

        fn taint2_query_cb_mask(a: Addr, size: u8) -> u64;

        fn taint2_labelset_addr_iter(addr: Addr, app: LabelSetVisitorRawFn, stuff: *mut c_void);
        fn taint2_labelset_ram_iter(ram_offset: u64, app: LabelSetVisitorRawFn, stuff: *mut c_void);
        fn taint2_labelset_reg_iter(reg_num: c_int, offset: c_int, app: LabelSetVisitorRawFn, stuff: *mut c_void);
        fn taint2_labelset_io_iter(ia: u64, app: LabelSetVisitorRawFn, stuff: *mut c_void);
        fn taint2_labelset_llvm_iter(reg_num: c_int, offset: c_int, app: LabelSetVisitorRawFn, stuff: *mut c_void);

        fn taint2_num_labels_applied() -> u32;

        fn taint2_track_taint_state();

        fn taint2_query_results_iter(qr: &mut QueryResult);
        fn taint2_query_result_next(qr: &mut QueryResult, done: &mut bool) -> u32;
        fn taint2_query_laddr_full(reg_num: u64, offset: u64, qr: &mut QueryResult);
        fn taint2_query_reg_full(reg_num: u32, offset: u32, qr: &mut QueryResult);
        fn taint2_query_ram_full(addr: u64, qr: &mut QueryResult);
    };
}

pub type LabelSetVisitorRawFn = extern "C" fn(u32, *mut c_void) -> c_int;

#[derive(Clone, Copy)]
#[repr(C)]
pub union ValueUnion {
    pub ha: u64,
    pub ma: u64,
    pub ia: u64,
    pub pa: u64,
    pub la: u64,
    pub gr: u64,
    pub gs: u64,
    pub ua: u64,
    pub con: u64,
    pub ret: u64,
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(C)]
#[allow(non_camel_case_types)]
pub enum AddrType {
    HADDR,
    MADDR,
    IADDR,
    PADDR,
    LADDR,
    GREG,
    GSPEC,
    UNK,
    CONST,
    RET,
    ADDR_LAST,
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(C)]
#[allow(non_camel_case_types)]
pub enum AddrFlag {
    IRRELEVANT = 5,
    EXCEPTION = 1,
    READLOG,
    FUNCARG,
}

#[derive(Clone, Copy)]
#[repr(C)]
pub struct Addr {
    pub typ: AddrType,
    pub val: ValueUnion,
    pub off: u16,
    pub flag: AddrFlag,
}

static TAINT_ENABLE: Once = Once::new();

/// Ensure the taint system is enabled
///
/// Note: most functions call this internally, check the docs for individual helpers, as in most
/// cases you don't need to call this directly unless you want to enable the taint system earlier
/// than directly before using it.
///
/// On subsequent calls, this function will have the same performance characteristics as
/// [`Once::call_once`](https://doc.rust-lang.org/std/sync/struct.Once.html#method.call_once).
pub fn enable() {
    TAINT_ENABLE.call_once(|| {
        TAINT.taint2_enable_taint();
    })
}

/// Check if the taint system is enabled
pub fn is_enabled() -> bool {
    TAINT.taint2_enabled()
}

/// Enable pointer tainting rules. May result in overtainting.
pub fn enable_tainted_pointer() {
    TAINT.taint2_enable_tainted_pointer()
}

/// Apply a 32-bit taint label to a given register.
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
/// use panda::regs::Reg;
///
/// // Select register by enum for compile-time guarantees
/// taint::label_reg(Reg::RAX, 1);
///
/// // Select register by string when needed
/// taint::label_reg("rax", 1);
/// ```
///
/// If a register is not supported by the [`Reg`] API, either make an issue or use
/// [`taint2_label_reg`] directly. (example: `TAINT.taint2_label_reg(reg_num, 0, label)`)
///
/// [`taint2_label_reg`]: Taint::taint2_label_reg
///
/// **Note**: This will enable taint if not already enabled.
pub fn label_reg(register: impl Into<Reg>, label: u32) {
    let reg = register.into() as c_int;
    enable();
    for i in 0..std::mem::size_of::<target_ptr_t>() {
        TAINT.taint2_label_reg(reg, i as c_int, label);
    }
}

/// Add a 32-bit taint label to a given register. Any previous taint labels on the same register are not removed.
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
/// use panda::regs::Reg;
///
/// // Select register by enum for compile-time guarantees
/// taint::label_reg_additive(Reg::RAX, 1);
///
/// // Select register by string when needed
/// taint::label_reg_additive("rax", 1);
/// ```
///
/// If a register is not supported by the [`Reg`] API, either make an issue or use
/// [`taint2_label_reg_additive`] directly. (example: `TAINT.taint2_label_reg_additive(reg_num, 0, label)`)
///
/// [`taint2_label_reg_additive`]: Taint::taint2_label_reg_additive
///
/// **Note**: This will enable taint if not already enabled.
pub fn label_reg_additive(register: impl Into<Reg>, label: u32) {
    let reg = register.into() as c_int;
    enable();
    for i in 0..std::mem::size_of::<target_ptr_t>() {
        TAINT.taint2_label_reg_additive(reg, i as c_int, label);
    }
}

/// Apply a 32-bit taint label to a specific byte of a given register.
///
/// ## Panics
///
/// This function panics if `byte_offset` is greater than or equal to the size of the register.
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
/// use panda::regs::Reg;
///
/// // Select register by enum for compile-time guarantees
/// taint::label_reg_byte(Reg::RAX, 0, 1);
///
/// // Select register by string when needed
/// taint::label_reg_byte("rax", 0, 1);
/// ```
///
/// **Note**: This will enable taint if not already enabled.
pub fn label_reg_byte(register: impl Into<Reg>, byte_offset: usize, label: u32) {
    assert!(byte_offset < std::mem::size_of::<target_ptr_t>());

    let reg = register.into() as c_int;
    enable();
    TAINT.taint2_label_reg(reg, byte_offset as c_int, label);
}

/// Apply a 32-bit taint label to a specific byte of a given register. Any previous taint labels on the same register
/// byte are not removed.
///
/// ## Panics
///
/// This function panics if `byte_offset` is greater than or equal to the size of the register.
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
/// use panda::regs::Reg;
///
/// // Select register by enum for compile-time guarantees
/// taint::label_reg_byte_additive(Reg::RAX, 0, 1);
///
/// // Select register by string when needed
/// taint::label_reg_byte_additive("rax", 0, 1);
/// ```
///
/// **Note**: This will enable taint if not already enabled.
pub fn label_reg_byte_additive(register: impl Into<Reg>, byte_offset: usize, label: u32) {
    assert!(byte_offset < std::mem::size_of::<target_ptr_t>());

    let reg = register.into() as c_int;
    enable();
    TAINT.taint2_label_reg_additive(reg, byte_offset as c_int, label);
}

/// Apply a 32-bit taint label to a given byte in RAM.
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
///
/// // taint the byte at address 0xfffffff01c5 with a label of 4
/// taint::label_ram(0xfffffff01c5, 4);
/// ```
///
/// **Note**: This will enable taint if not already enabled.
pub fn label_ram(addr: target_ptr_t, label: u32) {
    enable();
    TAINT.taint2_label_ram(addr as u64, label)
}

/// Add a 32-bit taint label to a given byte in RAM. Any previous taint labels on the same byte are not removed.
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
///
/// // Add a new taint label `4` to the byte at address 0xfffffff01c5
/// taint::label_ram_additive(0xfffffff01c5, 4);
/// ```
///
/// **Note**: This will enable taint if not already enabled.
pub fn label_ram_additive(addr: target_ptr_t, label: u32) {
    enable();
    TAINT.taint2_label_ram_additive(addr as u64, label);
}

/// Apply a 32-bit taint label to a range of bytes in RAM.
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
/// use panda::prelude::*;
///
/// // Select register by enum for compile-time guarantees
/// let start = 0xfffffff01c4;
/// let end = start + std::mem::size_of::<target_ptr_t>();
/// taint::label_ram(start..end, 4);
/// ```
///
/// **Note**: This will enable taint if not already enabled.
pub fn label_ram_range(addr_range: Range<target_ptr_t>, label: u32) {
    enable();
    for addr in addr_range {
        TAINT.taint2_label_ram(addr as u64, label);
    }
}

/// Add a 32-bit taint label to a range of bytes in RAM. Any previous taint labels on the same range of bytes are not
/// removed.
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
/// use panda::prelude::*;
///
/// // Select register by enum for compile-time guarantees
/// let start = 0xfffffff01c4;
/// let end = start + std::mem::size_of::<target_ptr_t>();
/// taint::label_ram_range_additive(start..end, 4);
/// ```
///
/// **Note**: This will enable taint if not already enabled.
pub fn label_ram_range_additive(addr_range: Range<target_ptr_t>, label: u32) {
    enable();
    for addr in addr_range {
        TAINT.taint2_label_ram_additive(addr as u64, label);
    }
}

/// Removes all taint labels on all bytes of a given register.
///
/// This function effectively does nothing if taint is not enabled.
pub fn unlabel_reg(register: impl Into<Reg>) {
    if !TAINT_ENABLE.is_completed() {
        return;
    }

    let reg = register.into() as c_int;
    for i in 0..std::mem::size_of::<target_ptr_t>() {
        TAINT.taint2_delete_reg(reg, i as c_int);
    }
}

/// Removes all taint labels on a specific byte of a given register.
///
/// This function effectively does nothing if taint is not enabled.
///
/// ## Panics
///
/// This function panics if `byte_offset` is greater than or equal to the size of the register.
pub fn unlabel_reg_byte(register: impl Into<Reg>, byte_offset: usize) {
    assert!(byte_offset < std::mem::size_of::<target_ptr_t>());

    if !TAINT_ENABLE.is_completed() {
        return;
    }

    let reg = register.into() as c_int;
    TAINT.taint2_delete_reg(reg, byte_offset as c_int);
}

/// Removes all taint labels on a given byte in RAM.
///
/// This function effectively does nothing if taint is not enabled.
pub fn unlabel_ram(addr: target_ptr_t) {
    if !TAINT_ENABLE.is_completed() {
        return;
    }

    TAINT.taint2_delete_ram(addr as u64);
}

/// Removes all taint labels on a range of bytes in RAM.
///
/// This function effectively does nothing if taint is not enabled.
pub fn unlabel_ram_range(addr_range: Range<target_ptr_t>) {
    if !TAINT_ENABLE.is_completed() {
        return;
    }

    for addr in addr_range {
        TAINT.taint2_delete_ram(addr as u64);
    }
}

/// Check if a register is tainted by any label
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
/// use panda::regs::Reg;
///
/// taint::label_reg(Reg::RAX);
///
/// if taint::check_reg(Reg::RAX) {
///     println!("RAX is tainted by some label");
/// }
/// ```
pub fn check_reg(reg: impl Into<Reg>) -> bool {
    let reg_num = reg.into() as c_int;
    check_reg_num(reg_num)
}

/// Check if a specific byte of a register is tainted by any label
///
/// ## Panics
///
/// This function panics if `byte_offset` is greater than or equal to the size of the register.
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
/// use panda::regs::Reg;
///
/// taint::label_reg_byte(Reg::RAX, 1, 1);
///
/// if taint::check_reg_byte(Reg::RAX, 1) {
///     println!("RAX[1] is tainted by some label");
/// }
/// ```
pub fn check_reg_byte(reg: impl Into<Reg>, byte_offset: usize) -> bool {
    assert!(byte_offset < std::mem::size_of::<target_ptr_t>());

    let reg_num = reg.into() as c_int;
    check_reg_num_byte(reg_num, byte_offset)
}

/// Check if a register is tainted by any label, by the register number
///
/// ### Notes
///
/// * When your given register is supported in the [`Reg`] API, use [`check_reg`]
/// * If taint has not been enabled by **your** plugin, this will return false
pub fn check_reg_num(reg_num: c_int) -> bool {
    TAINT_ENABLE.is_completed() && {
        let reg_size = std::mem::size_of::<target_ptr_t>();

        (0..reg_size).any(|offset| TAINT.taint2_query_reg(reg_num, offset as c_int) > 0)
    }
}

/// Check if a specific byte of a register is tainted by any label, by the register number
///
/// ## Panics
///
/// This function panics if `byte_offset` is greater than or equal to the size of the register.
///
/// ### Notes
///
/// * When your given register is supported in the [`Reg`] API, use [`check_reg_byte`]
/// * If taint has not been enabled by **your** plugin, this will return false
pub fn check_reg_num_byte(reg_num: c_int, byte_offset: usize) -> bool {
    assert!(byte_offset < std::mem::size_of::<target_ptr_t>());
    TAINT_ENABLE.is_completed() && TAINT.taint2_query_reg(reg_num, byte_offset as c_int) > 0
}

/// Check if a byte in RAM is tainted by any label
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
///
/// if taint::check_ram(0xffff_0034) {
///     println!("Variable at 0xffff_0034 is tainted")
/// }
/// ```
///
/// **Note:** If taint has not been enabled by **your** plugin, this will return false
pub fn check_ram(addr: target_ptr_t) -> bool {
    TAINT_ENABLE.is_completed() && TAINT.taint2_query_ram(addr as u64) > 0
}

/// Check if any of a range of bytes in RAM is tainted by any label
///
/// ## Example
///
/// ```no_run
/// use panda::taint;
///
/// if taint::check_ram_range(0xffff_0034..0xffff_0038) {
///     println!("Variable at 0xffff_0034 is tainted")
/// }
/// ```
///
/// **Note:** If taint has not been enabled by **your** plugin, this will return false
pub fn check_ram_range(mut addr_range: Range<target_ptr_t>) -> bool {
    TAINT_ENABLE.is_completed() && addr_range.any(|addr| TAINT.taint2_query_ram(addr as u64) > 0)
}

pub fn check_laddr(addr: u64, offset: u64) -> bool {
    TAINT_ENABLE.is_completed() && TAINT.taint2_query_laddr(addr, offset) > 0
}

/// Get a list of all taint labels applied to a register, excluding duplicates across bytes
pub fn get_reg(reg: impl Into<Reg>) -> Vec<u32> {
    let labels: HashSet<u32> = iter_reg_labels(reg).collect();

    labels.into_iter().collect()
}

/// Get a list of all taint labels applied to a specific byte of a register
///
/// ## Panics
///
/// This function panics if `byte_offset` is greater than or equal to the size of the register.
pub fn get_reg_byte(reg: impl Into<Reg>, byte_offset: usize) -> Vec<u32> {
    assert!(byte_offset < std::mem::size_of::<target_ptr_t>());
    iter_reg_byte_labels(reg, byte_offset).collect()
}

/// Get a list of all taint labels applied to a byte of memory
pub fn get_ram(addr: target_ptr_t) -> Vec<u32> {
    let mut query_result = QueryResult::empty();
    TAINT.taint2_query_ram_full(addr as u64, &mut query_result);

    if check_ram(addr) {
        LabelIter {
            done: query_result.num_labels == 0,
            query_result,
        }
        .collect()
    } else {
        Vec::with_capacity(0)
    }
}

/// Get a unique list of all taint labels applied to a segment of memory
pub fn get_ram_range(addr_range: Range<target_ptr_t>) -> Vec<u32> {
    let labels: HashSet<u32> = iter_ram_labels(addr_range).collect();

    labels.into_iter().collect()
}

/// Iterate over all the taint labels applied to a given register
///
/// **NOTE**: this will repeat labels if they are applied to multiple bytes in
/// the register. For automatic deduplication behavior, try [`get_reg`].
pub fn iter_reg_labels(reg: impl Into<Reg>) -> impl Iterator<Item = u32> {
    let reg_size = std::mem::size_of::<target_ptr_t>();

    let reg = reg.into();
    (0..reg_size)
        .map(move |i| iter_reg_byte_labels(reg, i))
        .flatten()
}

/// Iterate over all the taint labels applied to a specific byte of a given register
///
/// ## Panics
///
/// This function panics if `byte_offset` is greater than or equal to the size of the register.
pub fn iter_reg_byte_labels(reg: impl Into<Reg>, byte_offset: usize) -> impl Iterator<Item = u32> {
    assert!(byte_offset < std::mem::size_of::<target_ptr_t>());

    let reg = reg.into();

    let mut query_result = QueryResult::empty();
    TAINT.taint2_query_reg_full(reg as u32, byte_offset as u32, &mut query_result);

    if TAINT.taint2_query_reg(reg as c_int, byte_offset as c_int) > 0 {
        LabelIter {
            done: query_result.is_empty_or_invalid(),
            query_result,
        }
    } else {
        LabelIter {
            done: true,
            query_result,
        }
    }
}

/// Iterate over all the taint labels applied to a segment of memory
///
/// **NOTE**: this will repeat labels if they are applied to multiple bytes in
/// the memory range. For automatic deduplication behavior, try [`get_ram_range`].
pub fn iter_ram_labels(addr_range: Range<target_ptr_t>) -> impl Iterator<Item = u32> {
    addr_range
        .map(move |addr| {
            let mut query_result = QueryResult::empty();
            TAINT.taint2_query_ram_full(addr as u64, &mut query_result);

            if check_ram(addr) {
                LabelIter {
                    done: query_result.is_empty_or_invalid(),
                    query_result,
                }
            } else {
                LabelIter {
                    done: true,
                    query_result,
                }
            }
        })
        .flatten()
}

#[repr(C)]
pub struct QueryResult {
    num_labels: u32,
    ls: *mut c_void,
    it_end: *mut c_void,
    it_curr: *mut c_void,
    tcn: u32,
    cb_mask: u8,
}

impl QueryResult {
    fn empty() -> Self {
        Self {
            num_labels: 0,
            ls: ptr::null_mut(),
            it_end: ptr::null_mut(),
            it_curr: ptr::null_mut(),
            tcn: 0,
            cb_mask: 0,
        }
    }

    fn is_empty_or_invalid(&self) -> bool {
        self.num_labels == 0 || self.it_end.is_null() || self.it_curr.is_null()
    }
}

pub struct LabelIter {
    query_result: QueryResult,
    done: bool,
}

impl Iterator for LabelIter {
    type Item = u32;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            None
        } else {
            Some(TAINT.taint2_query_result_next(&mut self.query_result, &mut self.done))
        }
    }
}

// TODO: sym_enable, sym_label_ram, sym_label_reg