llvm-native-core-ext 0.1.0

Extended modules for llvm-native-core: analysis passes, transforms, codegen extras, bitcode, linker, JIT, utilities. Part of the llvm-native workspace (https://crates.io/crates/llvm-native).
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
//! Hardware Address Sanitizer (HWASan) — complete implementation.
//!
//! HWASan uses ARM TBI (Top Byte Ignore) for fast memory tagging.
//! Features:
//! - Kernel HWASan: KASAN integration for Linux kernel
//! - TBI (Top Byte Ignore) usage on AArch64
//! - MTE (Memory Tagging Extension) hardware acceleration
//! - Stack tagging: instrument function prologue/epilogue
//! - Heap tagging: instrument malloc/free
//! - Global tagging: instrument global variable access
//! - Short granule support: tag individual bytes within 16-byte granule
//! - Tag mismatch report: detailed report with allocation traces
//!
//! Clean-room behavioral reconstruction from the HWASan specification
//! and ARM Architecture Reference Manual. No LLVM source consulted.

use std::collections::HashMap;
use std::sync::atomic::{AtomicU8, Ordering};

// ============================================================================
// Constants
// ============================================================================

/// Granule size in bytes: memory is tagged at 16-byte granularity.
pub const HWASAN_GRANULE_SIZE: u32 = 16;

/// Number of bits in a tag (4 bits = 16 tag values).
pub const HWASAN_TAG_BITS: u8 = 4;

/// Mask for extracting the tag from a pointer (top byte, TBI).
pub const HWASAN_TAG_MASK: u64 = 0xFF00000000000000;

/// Shift amount for the tag in a 64-bit pointer.
pub const HWASAN_TAG_SHIFT: u32 = 56;

/// Maximum tag value (0-15).
pub const HWASAN_MAX_TAG: u8 = 0x0F;

/// Tag value for untagged memory.
pub const HWASAN_UNTAGGED: u8 = 0;

/// Mask for extracting address bits from a tagged pointer.
pub const HWASAN_ADDR_MASK: u64 = 0x00FFFFFFFFFFFFFF;

/// Short granule mask (bits per byte within a 16-byte granule).
pub const HWASAN_SHORT_GRANULE_MASK: u16 = 0xFFFF;

/// MTE tag granule size (16 bytes).
pub const HWASAN_MTE_GRANULE_SIZE: u32 = 16;

/// MTE tag check mask.
pub const HWASAN_MTE_TAG_MASK: u64 = 0xF;

/// Shadow memory offset for kernel HWASan (KASAN).
pub const HWASAN_KERNEL_SHADOW_OFFSET: u64 = 0xFFFF000000000000;

/// Kernel shadow scale.
pub const HWASAN_KERNEL_SHADOW_SCALE: u32 = 16;

// ============================================================================
// Tag Generation
// ============================================================================

/// Hardware-based random tag generator using ARM MTE or software PRNG.
#[derive(Debug, Clone)]
pub struct TagGenerator {
    /// Seed for software PRNG (when MTE is unavailable).
    pub seed: u64,
    /// Whether hardware MTE is available.
    pub has_mte: bool,
    /// Simple LCG state.
    lcg_state: u64,
}

impl TagGenerator {
    pub fn new(seed: u64) -> Self {
        TagGenerator {
            seed,
            has_mte: false,
            lcg_state: seed,
        }
    }

    /// Generate a random 4-bit tag (1-15, exclude 0).
    pub fn generate_tag(&mut self) -> u8 {
        if self.has_mte {
            // Hardware tag generation via IRG instruction
            self.mte_generate_tag()
        } else {
            // Software PRNG
            let tag = self.lcg_next() as u8 % HWASAN_MAX_TAG;
            if tag == 0 { 1 } else { tag }
        }
    }

    /// Simulate MTE IRG (Insert Random Tag) instruction.
    fn mte_generate_tag(&mut self) -> u8 {
        let tag = self.lcg_next() as u8 & HWASAN_MAX_TAG;
        if tag == 0 { 1 } else { tag }
    }

    /// Simple LCG PRNG.
    fn lcg_next(&mut self) -> u64 {
        self.lcg_state = self.lcg_state.wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        self.lcg_state
    }

    /// Generate a tag for a specific allocation.
    pub fn allocate_tag(&mut self) -> u8 {
        let tag = self.generate_tag();
        tag
    }

    /// Mark a tag as freed (for use-after-free detection).
    pub fn free_tag(&mut self, _tag: u8) {
        // In real HWASan, freed tags are tracked to detect UAF
    }
}

// ============================================================================
// Pointer Tagging
// ============================================================================

/// Operations on tagged pointers.
#[derive(Debug, Clone)]
pub struct TaggedPointer;

impl TaggedPointer {
    /// Create a tagged pointer: place tag in top byte (TBI).
    pub fn tag_pointer(ptr: u64, tag: u8) -> u64 {
        (ptr & HWASAN_ADDR_MASK) | ((tag as u64) << HWASAN_TAG_SHIFT)
    }

    /// Extract the tag from a pointer.
    pub fn get_tag(ptr: u64) -> u8 {
        ((ptr & HWASAN_TAG_MASK) >> HWASAN_TAG_SHIFT) as u8
    }

    /// Strip the tag, returning the untagged address.
    pub fn strip_tag(ptr: u64) -> u64 {
        ptr & HWASAN_ADDR_MASK
    }

    /// Check if a pointer is tagged (has non-zero top byte).
    pub fn is_tagged(ptr: u64) -> bool {
        (ptr & HWASAN_TAG_MASK) != 0
    }

    /// Compare tags of two pointers.
    pub fn tags_match(p1: u64, p2: u64) -> bool {
        Self::get_tag(p1) == Self::get_tag(p2)
    }

    /// Sign a pointer with a tag (AArch64 PAC/TBI compatible).
    pub fn sign_pointer(ptr: u64, modifier: u64) -> u64 {
        let tag = Self::get_tag(ptr);
        let addr = Self::strip_tag(ptr);
        // Simplified PAC: XOR the truncated address with modifier
        let pac = (addr & 0xFFFFFFFF) ^ modifier;
        Self::tag_pointer(addr | (pac << 32), tag)
    }
}

// ============================================================================
// Memory Tagging
// ============================================================================

/// Memory tagging tables for heap, stack, and globals.
#[derive(Debug, Clone)]
pub struct MemoryTagTable {
    /// Maps untagged address → tag (for heap/stack/global memory).
    pub tags: HashMap<u64, u8>,
    /// Maps untagged address → allocation stack trace ID.
    pub alloc_trace: HashMap<u64, u32>,
    /// Maps untagged address → deallocation stack trace ID.
    pub free_trace: HashMap<u64, u32>,
    /// Short granule tags: per-byte tags within a 16-byte granule.
    pub short_granules: HashMap<u64, u16>,
    /// Whether to use short granule support.
    pub use_short_granules: bool,
}

impl MemoryTagTable {
    pub fn new() -> Self {
        MemoryTagTable {
            tags: HashMap::new(),
            alloc_trace: HashMap::new(),
            free_trace: HashMap::new(),
            short_granules: HashMap::new(),
            use_short_granules: false,
        }
    }

    /// Set a tag for a memory region.
    pub fn set_memory_tag(&mut self, addr: u64, size: usize, tag: u8) {
        let granularity = HWASAN_GRANULE_SIZE as u64;
        let start = addr & !(granularity - 1);
        let end = (addr + size as u64 + granularity - 1) & !(granularity - 1);

        for granule_addr in (start..end).step_by(granularity as usize) {
            self.tags.insert(granule_addr, tag);
        }
    }

    /// Set short granule tags for individual bytes within a 16-byte granule.
    pub fn set_short_granule_tags(&mut self, granule_addr: u64, byte_tags: &[u8]) {
        if !self.use_short_granules { return; }

        let mut mask: u16 = 0;
        // Each byte gets encoded as: bit i = 1 if byte i has same tag as granule
        for (i, &bt) in byte_tags.iter().enumerate().take(16) {
            let granule_tag = self.tags.get(&granule_addr).copied().unwrap_or(0);
            if bt == granule_tag {
                mask |= 1u16 << i;
            }
        }
        self.short_granules.insert(granule_addr, mask);
    }

    /// Get the tag for a specific address.
    pub fn get_memory_tag(&self, addr: u64) -> u8 {
        let granule_addr = addr & !(HWASAN_GRANULE_SIZE as u64 - 1);
        self.tags.get(&granule_addr).copied().unwrap_or(HWASAN_UNTAGGED)
    }

    /// Set allocation trace.
    pub fn set_alloc_trace(&mut self, addr: u64, trace_id: u32) {
        let granule_addr = addr & !(HWASAN_GRANULE_SIZE as u64 - 1);
        self.alloc_trace.insert(granule_addr, trace_id);
    }

    /// Set free trace.
    pub fn set_free_trace(&mut self, addr: u64, trace_id: u32) {
        let granule_addr = addr & !(HWASAN_GRANULE_SIZE as u64 - 1);
        self.free_trace.insert(granule_addr, trace_id);
    }

    /// Check if a short-granule access is valid.
    pub fn check_short_granule(&self, addr: u64) -> bool {
        if !self.use_short_granules { return true; }

        let granule_addr = addr & !(HWASAN_GRANULE_SIZE as u64 - 1);
        let byte_offset = (addr - granule_addr) as usize;
        let mask = self.short_granules.get(&granule_addr).copied().unwrap_or(0);
        (mask & (1u16 << byte_offset)) != 0
    }
}

impl Default for MemoryTagTable {
    fn default() -> Self {
        MemoryTagTable::new()
    }
}

// ============================================================================
// Stack Tagging
// ============================================================================

/// Stack frame tagging for function prologue/epilogue.
#[derive(Debug, Clone)]
pub struct StackTagging {
    /// Tag to use for stack variables.
    pub stack_tag: u8,
    /// Whether to tag function prologues/epilogues.
    pub enabled: bool,
    /// Tag generator for stack allocations.
    pub tag_generator: TagGenerator,
}

impl StackTagging {
    pub fn new(seed: u64) -> Self {
        StackTagging {
            stack_tag: HWASAN_UNTAGGED,
            enabled: false,
            tag_generator: TagGenerator::new(seed),
        }
    }

    /// Generate stack prologue instrumentation.
    pub fn emit_prologue(&mut self, func_name: &str, stack_size: u64) -> Vec<String> {
        if !self.enabled { return Vec::new(); }

        self.stack_tag = self.tag_generator.generate_tag();

        vec![
            format!("  ; HWASan stack prologue for {}", func_name),
            format!("  ; Stack size: {} bytes", stack_size),
            format!("  ; Stack tag: {}", self.stack_tag),
            "  ; tag stack frame memory".to_string(),
            "  ;   stg %sp, [%sp]           // store tag to stack granule 0".to_string(),
            "  ;   addg %tmp, %sp, #16, #1  // add tag to SP+16".to_string(),
            "  ;   stg %tmp, [%sp, #16]     // store tag to stack granule 1".to_string(),
            format!("  ; Total granules: {}", (stack_size + 15) / 16),
        ]
    }

    /// Generate stack epilogue instrumentation.
    pub fn emit_epilogue(&mut self, func_name: &str) -> Vec<String> {
        if !self.enabled { return Vec::new(); }

        vec![
            format!("  ; HWASan stack epilogue for {}", func_name),
            "  ; untag stack frame (set to 0 for use-after-return detection)".to_string(),
        ]
    }

    /// Emit LLVM IR for stack tagging.
    pub fn emit_llvm_stack_prologue(&self, func_name: &str, sp_reg: &str, frame_size: u64) -> Vec<String> {
        vec![
            format!("  ; HWASan stack tag: {} frame {} bytes", func_name, frame_size),
            format!("  ; IRG: insert random tag into stack pointer"),
            format!("  %tagged_sp = call i64 @llvm.aarch64.irg(i64 {}, i64 0)", sp_reg),
            format!("  ; STG: store allocation tag for each granule"),
            format!("  call void @llvm.aarch64.stg(i64 %tagged_sp, i64 %tagged_sp, i64 {})", frame_size),
        ]
    }
}

// ============================================================================
// Heap Tagging
// ============================================================================

/// Heap allocation tagging (malloc/free instrumentation).
#[derive(Debug, Clone)]
pub struct HeapTagging {
    pub tag_generator: TagGenerator,
    pub tag_table: MemoryTagTable,
    pub enabled: bool,
    /// Trace ID counter for allocation stack traces.
    pub next_trace_id: u32,
}

impl HeapTagging {
    pub fn new(seed: u64) -> Self {
        HeapTagging {
            tag_generator: TagGenerator::new(seed),
            tag_table: MemoryTagTable::new(),
            enabled: false,
            next_trace_id: 1,
        }
    }

    /// Instrument a malloc call: allocate with random tag.
    pub fn instrument_malloc(&mut self, size: usize) -> (u64, u8) {
        let addr = 0x10000000u64; // Placeholder
        let tag = self.tag_generator.allocate_tag();
        let tagged_ptr = TaggedPointer::tag_pointer(addr, tag);

        self.tag_table.set_memory_tag(addr, size, tag);
        let trace_id = self.next_trace_id;
        self.next_trace_id += 1;
        self.tag_table.set_alloc_trace(addr, trace_id);

        (tagged_ptr, tag)
    }

    /// Instrument a free call: check tag before freeing.
    pub fn instrument_free(&mut self, ptr: u64) -> Option<String> {
        let tag = TaggedPointer::get_tag(ptr);
        let addr = TaggedPointer::strip_tag(ptr);
        let mem_tag = self.tag_table.get_memory_tag(addr);

        if tag != mem_tag && mem_tag != HWASAN_UNTAGGED {
            // Tag mismatch: possible use-after-free or buffer overflow
            return Some(format!(
                "HWASan: tag mismatch on free: ptr_tag={}, mem_tag={} at 0x{:x}",
                tag, mem_tag, addr
            ));
        }

        // Mark as freed with a different tag
        let free_tag = self.tag_generator.generate_tag();
        self.tag_table.set_memory_tag(addr, 1, free_tag);
        self.tag_table.set_free_trace(addr, self.next_trace_id);
        self.next_trace_id += 1;

        None
    }

    /// Emit LLVM IR for heap allocation tagging.
    pub fn emit_llvm_malloc_instrument(&self, size_reg: &str, result_reg: &str) -> Vec<String> {
        vec![
            "  ; HWASan heap alloc tag".to_string(),
            format!("  ; Call malloc with size {}", size_reg),
            format!("  %{}_tag = call i32 @__hwasan_generate_tag()", result_reg),
            format!("  ; IRG: tag the pointer"),
            format!("  %{}_tagged = call i64 @llvm.aarch64.irg(i64 %{}, i64 %{}_tag)", result_reg, result_reg, result_reg),
            format!("  ; STG: tag the allocated memory"),
            format!("  call void @__hwasan_tag_memory(i64 %{}_tagged, i64 {}, i32 %{}_tag)", result_reg, size_reg, result_reg),
        ]
    }

    /// Emit LLVM IR for heap free check.
    pub fn emit_llvm_free_check(&self, ptr_reg: &str) -> Vec<String> {
        vec![
            "  ; HWASan heap free check".to_string(),
            format!(
                "  call void @__hwasan_check_free(ptr {})",
                ptr_reg
            ),
        ]
    }
}

// ============================================================================
// Global Tagging
// ============================================================================

/// Global variable tagging.
#[derive(Debug, Clone)]
pub struct GlobalTagging {
    pub tag_generator: TagGenerator,
    pub global_tags: HashMap<String, (u64, u8)>,
    pub enabled: bool,
}

impl GlobalTagging {
    pub fn new(seed: u64) -> Self {
        GlobalTagging {
            tag_generator: TagGenerator::new(seed),
            global_tags: HashMap::new(),
            enabled: false,
        }
    }

    /// Register a global variable with a random tag.
    pub fn register_global(&mut self, name: &str, addr: u64, size: usize) -> u8 {
        let tag = self.tag_generator.allocate_tag();
        self.global_tags.insert(name.to_string(), (addr, tag));
        tag
    }

    /// Emit LLVM IR for global access check.
    pub fn emit_llvm_global_check(&self, global_name: &str, access_ptr: &str) -> Vec<String> {
        if let Some(&(_addr, tag)) = self.global_tags.get(global_name) {
            vec![
                format!("  ; HWASan global access check for {}", global_name),
                format!("  ; Expected tag: {}", tag),
                format!("  %ptr_tag = lshr i64 {}, 56", access_ptr),
                format!("  %expected_tag = zext i8 {} to i64", tag),
                format!("  %tag_ok = icmp eq i64 %ptr_tag, %expected_tag"),
                "  br i1 %tag_ok, label %global_ok, label %hwasan_mismatch".to_string(),
            ]
        } else {
            vec![]
        }
    }
}

// ============================================================================
// Tag Mismatch Report
// ============================================================================

/// Comprehensive tag mismatch report.
#[derive(Debug, Clone)]
pub struct TagMismatchReport {
    /// Address where mismatch occurred.
    pub address: u64,
    /// Expected tag (from pointer).
    pub pointer_tag: u8,
    /// Actual tag (from memory).
    pub memory_tag: u8,
    /// Access type (load/store).
    pub access_type: String,
    /// Access size in bytes.
    pub access_size: usize,
    /// Source location.
    pub location: String,
    /// Thread ID.
    pub thread_id: u64,
    /// Allocation stack trace ID.
    pub alloc_trace_id: Option<u32>,
    /// Deallocation stack trace ID (for use-after-free).
    pub free_trace_id: Option<u32>,
    /// Whether this looks like a use-after-free.
    pub likely_uaf: bool,
    /// Whether this looks like a buffer overflow.
    pub likely_buffer_overflow: bool,
}

impl TagMismatchReport {
    pub fn new(
        address: u64,
        ptr_tag: u8,
        mem_tag: u8,
        access_type: &str,
        access_size: usize,
        location: &str,
    ) -> Self {
        TagMismatchReport {
            address,
            pointer_tag: ptr_tag,
            memory_tag: mem_tag,
            access_type: access_type.to_string(),
            access_size,
            location: location.to_string(),
            thread_id: 0,
            alloc_trace_id: None,
            free_trace_id: None,
            likely_uaf: false,
            likely_buffer_overflow: false,
        }
    }

    /// Format the report as a human-readable string.
    pub fn format(&self) -> String {
        let mut report = String::new();
        report.push_str("=================================================================\n");
        report.push_str("HWASan: tag-mismatch on address 0x");
        report.push_str(&format!("{:016x}\n", self.address));
        report.push_str(&format!(
            "  {} of size {} at {}\n",
            self.access_type, self.access_size, self.location
        ));
        report.push_str(&format!(
            "  pointer tag:  0x{:x}\n",
            self.pointer_tag
        ));
        report.push_str(&format!(
            "  memory tag:   0x{:x}\n",
            self.memory_tag
        ));

        if let Some(tid) = self.alloc_trace_id {
            report.push_str(&format!("  alloc trace:  #{}\n", tid));
        }
        if let Some(tid) = self.free_trace_id {
            report.push_str(&format!("  free trace:   #{}\n", tid));
        }

        if self.likely_uaf {
            report.push_str("  Likely use-after-free detected.\n");
            if let Some(tid) = self.free_trace_id {
                report.push_str(&format!(
                    "  Memory was freed at trace #{}\n",
                    tid
                ));
            }
        }

        if self.likely_buffer_overflow {
            report.push_str("  Likely buffer overflow detected.\n");
        }

        report.push_str("=================================================================\n");
        report
    }

    /// Emit LLVM IR for the tag mismatch handler.
    pub fn emit_llvm_handler() -> Vec<String> {
        vec![
            "hwasan_mismatch:".to_string(),
            "  ; Tag mismatch handler".to_string(),
            "  call void @__hwasan_tag_mismatch4(i64 %addr, i8 %ptr_tag, i8 %mem_tag, i64 %access_info)".to_string(),
            "  ; Halt or continue based on HWASan flags".to_string(),
            "  call void @__hwasan_handle_tag_mismatch()".to_string(),
            "  unreachable".to_string(),
        ]
    }
}

// ============================================================================
// MTE (Memory Tagging Extension) Hardware Acceleration
// ============================================================================

/// ARM MTE hardware acceleration support.
#[derive(Debug, Clone)]
pub struct MTESupport {
    /// Whether MTE is available on this CPU.
    pub available: bool,
    /// Whether synchronous MTE checks are enabled.
    pub sync_mode: bool,
    /// Whether asymmetric MTE checks are enabled.
    pub async_mode: bool,
}

impl MTESupport {
    pub fn new() -> Self {
        MTESupport {
            available: false,
            sync_mode: false,
            async_mode: false,
        }
    }

    /// Check if MTE is supported by probing CPU features.
    pub fn probe() -> Self {
        // In real HWASan, this would check ID_AA64PFR1_EL1.MTE
        MTESupport {
            available: false,
            sync_mode: false,
            async_mode: false,
        }
    }

    /// Emit MTE-specific LLVM IR.
    pub fn emit_mte_instructions(&self) -> Vec<String> {
        if !self.available { return Vec::new(); }

        let mut ops = vec![
            "  ; ARM MTE instructions".to_string(),
            "  ; IRG Xd, Xn: Insert Random Tag".to_string(),
            "  ; STG Xt, [Xn]: Store Allocation Tag".to_string(),
            "  ; LDG Xt, [Xn]: Load Allocation Tag".to_string(),
            "  ; STZG Xt, [Xn]: Store Zero Tag (for deallocation)".to_string(),
        ];

        if self.sync_mode {
            ops.push("  ; MTE sync: tag check fault on mismatch".to_string());
        }
        if self.async_mode {
            ops.push("  ; MTE async: tag check logged, no immediate fault".to_string());
        }

        ops
    }

    /// Emit LLVM intrinsics for MTE operations.
    pub fn emit_mte_intrinsics() -> Vec<String> {
        vec![
            "declare i64 @llvm.aarch64.irg(i64, i64)".to_string(),
            "declare void @llvm.aarch64.stg(i64, i64, i64)".to_string(),
            "declare void @llvm.aarch64.stzg(i64, i64, i64)".to_string(),
            "declare i64 @llvm.aarch64.ldg(i64)".to_string(),
            "declare void @llvm.aarch64.addg(i64, i32)".to_string(),
            "declare void @llvm.aarch64.subp(i64, i64)".to_string(),
            "declare i64 @llvm.aarch64.gmi(i64, i64)".to_string(),
        ]
    }
}

impl Default for MTESupport {
    fn default() -> Self {
        MTESupport::new()
    }
}

// ============================================================================
// Kernel HWASan (KASAN)
// ============================================================================

/// Kernel Address Sanitizer using hardware tagging (KASAN_HW).
#[derive(Debug, Clone)]
pub struct KernelHWASan {
    /// Shadow memory offset for kernel address space.
    pub shadow_offset: u64,
    /// Shadow memory scale.
    pub shadow_scale: u32,
    /// Whether to tag slab allocations.
    pub tag_slab: bool,
    /// Whether to tag page allocations.
    pub tag_page_alloc: bool,
    /// Whether to tag vmalloc allocations.
    pub tag_vmalloc: bool,
    /// Whether to tag stack.
    pub tag_stack: bool,
    /// Whether to tag globals.
    pub tag_globals: bool,
    /// Memory tag table.
    pub tags: MemoryTagTable,
}

impl KernelHWASan {
    pub fn new() -> Self {
        KernelHWASan {
            shadow_offset: HWASAN_KERNEL_SHADOW_OFFSET,
            shadow_scale: HWASAN_KERNEL_SHADOW_SCALE,
            tag_slab: true,
            tag_page_alloc: true,
            tag_vmalloc: true,
            tag_stack: true,
            tag_globals: true,
            tags: MemoryTagTable::new(),
        }
    }

    /// Convert kernel virtual address to shadow address.
    pub fn virt_to_shadow(&self, virt_addr: u64) -> u64 {
        ((virt_addr >> self.shadow_scale) + self.shadow_offset) & !(HWASAN_GRANULE_SIZE as u64 - 1)
    }

    /// Tag a kernel slab allocation.
    pub fn tag_slab_allocation(&mut self, addr: u64, size: usize) -> u8 {
        if !self.tag_slab { return HWASAN_UNTAGGED; }
        let tag = (addr >> 4) as u8 & HWASAN_MAX_TAG;
        let tag = if tag == 0 { 1 } else { tag };
        self.tags.set_memory_tag(addr, size, tag);
        tag
    }

    /// Emit LLVM IR for kernel KASAN check.
    pub fn emit_llvm_kernel_check(&self, ptr_reg: &str, size: usize) -> Vec<String> {
        vec![
            format!("  ; KASAN HW check: {} bytes at {}", size, ptr_reg),
            format!("  %kas_ptr_tag = lshr i64 {}, 56", ptr_reg),
            format!("  %kas_mem = and i64 {}, {:#x}", ptr_reg, (1u64 << 56) - 1),
            format!(
                "  %kas_shadow = lshr i64 %kas_mem, {}",
                self.shadow_scale
            ),
            "  %kas_shadow_addr = add i64 %kas_shadow, %shadow_offset".to_string(),
            "  %kas_mem_tag = load i8, ptr %kas_shadow_addr".to_string(),
            "  %kas_tag_ok = icmp eq i64 %kas_ptr_tag, %kas_mem_tag".to_string(),
            "  br i1 %kas_tag_ok, label %kas_ok, label %kas_mismatch".to_string(),
        ]
    }

    /// Emit kernel boot-time initialization.
    pub fn emit_boot_init() -> Vec<String> {
        vec![
            "  ; KASAN HW boot initialization".to_string(),
            "  ; 1. Enable TBI in TCR_EL1".to_string(),
            "  ; 2. Set up shadow memory region".to_string(),
            "  ; 3. Tag kernel image and data sections".to_string(),
            "  ; 4. Initialize tag generation seed".to_string(),
            "  call void @__hwasan_kernel_init()".to_string(),
        ]
    }
}

impl Default for KernelHWASan {
    fn default() -> Self {
        KernelHWASan::new()
    }
}

// ============================================================================
// HWASan Configuration
// ============================================================================

/// Full HWASan configuration.
#[derive(Debug, Clone)]
pub struct HWASanConfig {
    /// Whether HWASan is enabled.
    pub enabled: bool,
    /// Use hardware MTE if available.
    pub use_mte: bool,
    /// Tag heap allocations.
    pub tag_heap: bool,
    /// Tag stack allocations.
    pub tag_stack: bool,
    /// Tag global variables.
    pub tag_globals: bool,
    /// Use short granules.
    pub use_short_granules: bool,
    /// Halt on first error.
    pub halt_on_error: bool,
    /// Print stack trace on error.
    pub print_stacktrace: bool,
    /// Kernel mode.
    pub kernel_mode: bool,
    /// Random seed for tag generation.
    pub random_seed: u64,
    /// Files to exclude.
    pub blacklist: Vec<String>,
}

impl Default for HWASanConfig {
    fn default() -> Self {
        HWASanConfig {
            enabled: true,
            use_mte: false,
            tag_heap: true,
            tag_stack: true,
            tag_globals: true,
            use_short_granules: false,
            halt_on_error: true,
            print_stacktrace: true,
            kernel_mode: false,
            random_seed: 12345,
            blacklist: Vec::new(),
        }
    }
}

// ============================================================================
// HWASan Runtime
// ============================================================================

/// HWASan runtime state.
#[derive(Debug)]
pub struct HWASanRuntime {
    pub config: HWASanConfig,
    pub tag_generator: TagGenerator,
    pub memory_tags: MemoryTagTable,
    pub stack_tagging: StackTagging,
    pub heap_tagging: HeapTagging,
    pub global_tagging: GlobalTagging,
    pub mte_support: MTESupport,
    pub kernel_hwasan: Option<KernelHWASan>,
    pub mismatch_reports: Vec<TagMismatchReport>,
    pub initialized: bool,
}

impl HWASanRuntime {
    pub fn new(config: HWASanConfig) -> Self {
        let seed = config.random_seed;
        HWASanRuntime {
            stack_tagging: StackTagging::new(seed),
            heap_tagging: HeapTagging::new(seed),
            global_tagging: GlobalTagging::new(seed),
            tag_generator: TagGenerator::new(seed),
            memory_tags: MemoryTagTable::new(),
            mte_support: if config.use_mte { MTESupport::probe() } else { MTESupport::new() },
            kernel_hwasan: if config.kernel_mode { Some(KernelHWASan::new()) } else { None },
            mismatch_reports: Vec::new(),
            initialized: false,
            config,
        }
    }

    /// Initialize HWASan runtime.
    pub fn initialize(&mut self) {
        if self.initialized { return; }
        self.initialized = true;

        if self.config.tag_heap {
            self.heap_tagging.enabled = true;
        }
        if self.config.tag_stack {
            self.stack_tagging.enabled = true;
        }
        if self.config.tag_globals {
            self.global_tagging.enabled = true;
        }
        self.memory_tags.use_short_granules = self.config.use_short_granules;
    }

    /// Check a tagged memory access.
    pub fn check_memory_access(
        &mut self,
        ptr: u64,
        access_type: &str,
        size: usize,
        location: &str,
    ) -> Option<TagMismatchReport> {
        let ptr_tag = TaggedPointer::get_tag(ptr);
        let addr = TaggedPointer::strip_tag(ptr);
        let mem_tag = self.memory_tags.get_memory_tag(addr);

        if ptr_tag == mem_tag || mem_tag == HWASAN_UNTAGGED {
            return None;
        }

        // Tag mismatch detected
        let mut report = TagMismatchReport::new(
            addr, ptr_tag, mem_tag, access_type, size, location,
        );

        // Analyze: is this use-after-free?
        if self.memory_tags.free_trace.contains_key(&(addr & !15)) {
            report.likely_uaf = true;
            report.free_trace_id = self.memory_tags
                .free_trace
                .get(&(addr & !15))
                .copied();
        } else {
            report.likely_buffer_overflow = true;
        }

        report.alloc_trace_id = self.memory_tags
            .alloc_trace
            .get(&(addr & !15))
            .copied();

        self.mismatch_reports.push(report.clone());
        Some(report)
    }

    /// Emit all HWASan runtime declarations.
    pub fn emit_runtime_declarations() -> Vec<String> {
        vec![
            "declare void @__hwasan_init()".to_string(),
            "declare void @__hwasan_tag_memory(i64, i64, i32)".to_string(),
            "declare i32 @__hwasan_generate_tag()".to_string(),
            "declare void @__hwasan_check_load(i64, i64)".to_string(),
            "declare void @__hwasan_check_store(i64, i64)".to_string(),
            "declare void @__hwasan_tag_mismatch4(i64, i8, i8, i64)".to_string(),
            "declare void @__hwasan_handle_tag_mismatch()".to_string(),
            "declare void @__hwasan_check_free(ptr)".to_string(),
            "declare void @__hwasan_kernel_init()".to_string(),
        ]
    }

    /// Emit LLVM IR for a complete HWASan-instrumented module.
    pub fn emit_llvm_module(&self, module_name: &str) -> String {
        let mut out = String::new();
        out.push_str(&format!("; HWASan instrumented module: {}\n", module_name));
        out.push_str(&format!("; MTE: {}\n", self.mte_support.available));
        out.push_str(&format!("; Kernel mode: {}\n", self.config.kernel_mode));
        out.push('\n');

        out.push_str("; MTE intrinsics\n");
        for decl in MTESupport::emit_mte_intrinsics() {
            out.push_str(&decl);
            out.push('\n');
        }

        out.push_str("\n; Runtime declarations\n");
        for decl in HWASanRuntime::emit_runtime_declarations() {
            out.push_str(&decl);
            out.push('\n');
        }

        if self.config.tag_stack {
            out.push_str("\n; Stack tagging callbacks\n");
            out.push_str("declare void @__hwasan_stack_tag(i64, i64)\n");
        }

        out
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tagged_pointer_set_get() {
        let ptr = 0x123456789ABCDEFu64;
        let tagged = TaggedPointer::tag_pointer(ptr, 0x0A);
        assert_eq!(TaggedPointer::get_tag(tagged), 0x0A);
        assert_eq!(TaggedPointer::strip_tag(tagged), ptr);
    }

    #[test]
    fn test_tagged_pointer_strip() {
        let tagged = TaggedPointer::tag_pointer(0xFFFF, 0x0F);
        assert_eq!(TaggedPointer::strip_tag(tagged), 0xFFFF);
    }

    #[test]
    fn test_tagged_pointer_is_tagged() {
        assert!(!TaggedPointer::is_tagged(0x1000));
        assert!(TaggedPointer::is_tagged(
            TaggedPointer::tag_pointer(0x1000, 0x01)
        ));
    }

    #[test]
    fn test_tagged_pointer_tags_match() {
        let p1 = TaggedPointer::tag_pointer(0x1000, 5);
        let p2 = TaggedPointer::tag_pointer(0x2000, 5);
        let p3 = TaggedPointer::tag_pointer(0x3000, 6);
        assert!(TaggedPointer::tags_match(p1, p2));
        assert!(!TaggedPointer::tags_match(p1, p3));
    }

    #[test]
    fn test_tag_generator_produces_valid_tag() {
        let mut gen = TagGenerator::new(42);
        for _ in 0..100 {
            let tag = gen.generate_tag();
            assert!(tag >= 1 && tag <= HWASAN_MAX_TAG);
        }
    }

    #[test]
    fn test_tag_generator_no_zero_tag() {
        let mut gen = TagGenerator::new(123);
        for _ in 0..200 {
            let tag = gen.generate_tag();
            assert_ne!(tag, 0);
        }
    }

    #[test]
    fn test_memory_tag_table_set_get() {
        let mut table = MemoryTagTable::new();
        table.set_memory_tag(0x1000, 32, 7);
        assert_eq!(table.get_memory_tag(0x1000), 7);
        assert_eq!(table.get_memory_tag(0x100F), 7);
        assert_eq!(table.get_memory_tag(0x1020), 7);
    }

    #[test]
    fn test_memory_tag_table_neighbor() {
        let mut table = MemoryTagTable::new();
        table.set_memory_tag(0x1000, 16, 5);
        // Next granule not tagged
        assert_eq!(table.get_memory_tag(0x1010), 0);
    }

    #[test]
    fn test_memory_tag_table_alloc_free_trace() {
        let mut table = MemoryTagTable::new();
        table.set_alloc_trace(0x1000, 42);
        table.set_free_trace(0x1000, 99);
        assert_eq!(table.alloc_trace.get(&0x1000), Some(&42));
        assert_eq!(table.free_trace.get(&0x1000), Some(&99));
    }

    #[test]
    fn test_tag_mismatch_report_format() {
        let report = TagMismatchReport::new(
            0x1234, 3, 7, "load", 4, "main.c:42",
        );
        let formatted = report.format();
        assert!(formatted.contains("tag-mismatch"));
        assert!(formatted.contains("0x1234"));
        assert!(formatted.contains("pointer tag:  0x3"));
        assert!(formatted.contains("memory tag:   0x7"));
    }

    #[test]
    fn test_tag_mismatch_report_uaf() {
        let mut report = TagMismatchReport::new(
            0x5678, 1, 2, "store", 8, "main.c:100",
        );
        report.likely_uaf = true;
        report.free_trace_id = Some(5);
        let formatted = report.format();
        assert!(formatted.contains("use-after-free"));
    }

    #[test]
    fn test_mte_intrinsics() {
        let intrinsics = MTESupport::emit_mte_intrinsics();
        assert!(intrinsics.iter().any(|s| s.contains("llvm.aarch64.irg")));
        assert!(intrinsics.iter().any(|s| s.contains("llvm.aarch64.stg")));
        assert!(intrinsics.iter().any(|s| s.contains("llvm.aarch64.ldg")));
    }

    #[test]
    fn test_hwasan_runtime_init() {
        let config = HWASanConfig::default();
        let mut rt = HWASanRuntime::new(config);
        rt.initialize();
        assert!(rt.initialized);
        assert!(rt.heap_tagging.enabled);
        assert!(rt.stack_tagging.enabled);
    }

    #[test]
    fn test_hwasan_check_access_clean() {
        let config = HWASanConfig::default();
        let mut rt = HWASanRuntime::new(config);
        rt.memory_tags.set_memory_tag(0x1000, 16, 5);
        let tagged_ptr = TaggedPointer::tag_pointer(0x1000, 5);
        let result = rt.check_memory_access(tagged_ptr, "load", 4, "test.c:1");
        assert!(result.is_none());
    }

    #[test]
    fn test_hwasan_check_access_mismatch() {
        let config = HWASanConfig::default();
        let mut rt = HWASanRuntime::new(config);
        rt.memory_tags.set_memory_tag(0x1000, 16, 5);
        let tagged_ptr = TaggedPointer::tag_pointer(0x1000, 3); // Wrong tag
        let result = rt.check_memory_access(tagged_ptr, "load", 4, "test.c:1");
        assert!(result.is_some());
        assert_eq!(rt.mismatch_reports.len(), 1);
    }

    #[test]
    fn test_stack_tagging_prologue() {
        let mut st = StackTagging::new(42);
        st.enabled = true;
        let ops = st.emit_prologue("test_func", 64);
        assert!(!ops.is_empty());
        assert!(ops.iter().any(|s| s.contains("test_func")));
    }

    #[test]
    fn test_stack_tagging_disabled() {
        let mut st = StackTagging::new(42);
        let ops = st.emit_prologue("test_func", 64);
        assert!(ops.is_empty());
    }

    #[test]
    fn test_heap_tagging_malloc() {
        let mut ht = HeapTagging::new(42);
        ht.enabled = true;
        let (_ptr, tag) = ht.instrument_malloc(128);
        assert_ne!(tag, 0);
    }

    #[test]
    fn test_heap_tagging_free_mismatch() {
        let mut ht = HeapTagging::new(42);
        ht.enabled = true;
        let (ptr, _tag) = ht.instrument_malloc(128);
        let wrong_ptr = TaggedPointer::tag_pointer(
            TaggedPointer::strip_tag(ptr), 7  // Wrong tag
        );
        let result = ht.instrument_free(wrong_ptr);
        assert!(result.is_some());
        assert!(result.unwrap().contains("tag mismatch"));
    }

    #[test]
    fn test_kernel_virt_to_shadow() {
        let kasan = KernelHWASan::new();
        let shadow = kasan.virt_to_shadow(0xFFFF000000001000);
        assert!(shadow > 0);
    }

    #[test]
    fn test_kernel_slab_tagging() {
        let mut kasan = KernelHWASan::new();
        let tag = kasan.tag_slab_allocation(0xFFFF000000001000, 64);
        assert_ne!(tag, 0);
    }

    #[test]
    fn test_short_granule() {
        let mut table = MemoryTagTable::new();
        table.use_short_granules = true;
        table.set_memory_tag(0x1000, 16, 5);
        // Set byte 3 and 7 as matching the granule tag
        table.set_short_granule_tags(0x1000, &[0,0,0,5,0,0,0,5,0,0,0,0,0,0,0,0]);
        assert!(table.check_short_granule(0x1003)); // Byte 3 matches
        assert!(!table.check_short_granule(0x1001)); // Byte 1 doesn't
    }

    #[test]
    fn test_runtime_declarations() {
        let decls = HWASanRuntime::emit_runtime_declarations();
        assert!(decls.iter().any(|d| d.contains("__hwasan_init")));
        assert!(decls.iter().any(|d| d.contains("__hwasan_tag_mismatch4")));
    }

    #[test]
    fn test_emit_llvm_module() {
        let config = HWASanConfig::default();
        let rt = HWASanRuntime::new(config);
        let module = rt.emit_llvm_module("test");
        assert!(module.contains("HWASan instrumented module"));
        assert!(module.contains("llvm.aarch64.irg"));
    }

    #[test]
    fn test_kernel_mode() {
        let mut config = HWASanConfig::default();
        config.kernel_mode = true;
        let rt = HWASanRuntime::new(config);
        assert!(rt.kernel_hwasan.is_some());
    }
}