libmwemu 0.24.1

x86 32/64bits and system internals emulator, for securely emulating malware and other stuff.
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
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
use crate::*;
use std::convert::TryInto;
const DATA_ADDR: u64 = 0x7000;

// Comprehensive tests for PUSHF/POPF/PUSHFQ/POPFQ instructions
//
// PUSHF - Push FLAGS (16-bit) onto stack
// POPF - Pop FLAGS (16-bit) from stack
// PUSHFQ - Push RFLAGS (64-bit) onto stack
// POPFQ - Pop RFLAGS (64-bit) from stack
//
// These instructions save and restore the processor flags

// ============================================================================
// PUSHFQ - Push RFLAGS (64-bit)
// Opcode: 9C (REX.W prefix for 64-bit)
// ============================================================================

#[test]
fn test_pushfq_basic() {
    let code = [
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rsp, 0x0FF8, "RSP decremented by 8");

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(0x0FF8, stack_val.len()).try_into().unwrap();
    let pushed_flags = u64::from_le_bytes(stack_val);
    assert_ne!(pushed_flags, 0, "Flags should be pushed");
}

#[test]
fn test_pushfq_with_carry_set() {
    let code = [
        0xf9, // STC (set carry)
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(0x0FF8, stack_val.len()).try_into().unwrap();
    let pushed_flags = u64::from_le_bytes(stack_val);
    assert_ne!(pushed_flags & 0x01, 0, "CF should be set in pushed flags");
}

#[test]
fn test_pushfq_with_zero_set() {
    let code = [
        0x48, 0x31, 0xc0, // XOR RAX, RAX (sets ZF)
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(0x0FF8, stack_val.len()).try_into().unwrap();
    let pushed_flags = u64::from_le_bytes(stack_val);
    assert_ne!(pushed_flags & 0x40, 0, "ZF should be set in pushed flags");
}

#[test]
fn test_pushfq_with_sign_set() {
    let code = [
        0x48, 0xc7, 0xc0, 0xff, 0xff, 0xff, 0xff, // MOV RAX, -1
        0x48, 0x85, 0xc0, // TEST RAX, RAX (sets SF)
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(0x0FF8, stack_val.len()).try_into().unwrap();
    let pushed_flags = u64::from_le_bytes(stack_val);
    assert_ne!(pushed_flags & 0x80, 0, "SF should be set in pushed flags");
}

#[test]
fn test_pushfq_multiple_flags() {
    let code = [
        // XOR clears CF, so do XOR first to set ZF, then STC to set CF
        0x48, 0x31, 0xc0, // XOR RAX, RAX (sets ZF, PF, clears CF/OF)
        0xf9, // STC (set carry)
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(0x0FF8, stack_val.len()).try_into().unwrap();
    let pushed_flags = u64::from_le_bytes(stack_val);
    assert_ne!(pushed_flags & 0x01, 0, "CF should be set");
    assert_ne!(pushed_flags & 0x40, 0, "ZF should be set");
}

#[test]
fn test_pushfq_preserves_registers() {
    let code = [
        0x48, 0xc7, 0xc0, 0x42, 0x00, 0x00, 0x00, // MOV RAX, 0x42
        0x48, 0xc7, 0xc3, 0x99, 0x00, 0x00, 0x00, // MOV RBX, 0x99
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax, 0x42, "RAX unchanged");
    assert_eq!(emu.regs().rbx, 0x99, "RBX unchanged");
}

#[test]
fn test_pushfq_multiple_times() {
    let code = [
        0xf9, // STC
        0x48, 0x9c, // PUSHFQ (with CF)
        0xf8, // CLC
        0x48, 0x9c, // PUSHFQ (without CF)
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rsp, 0x1000 - 16, "RSP decremented twice");

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(0x0FF8, stack_val.len()).try_into().unwrap();
    let first_flags = u64::from_le_bytes(stack_val);
    stack_val = emu.maps.read_bytes(0x0FF0, stack_val.len()).try_into().unwrap();
    let second_flags = u64::from_le_bytes(stack_val);

    assert_ne!(first_flags & 0x01, 0, "First PUSHFQ has CF set");
    assert_eq!(second_flags & 0x01, 0, "Second PUSHFQ has CF clear");
}

// ============================================================================
// POPFQ - Pop RFLAGS (64-bit)
// Opcode: 9D (REX.W prefix for 64-bit)
// ============================================================================

#[test]
fn test_popfq_basic() {
    let code = [
        0x48, 0x9c, // PUSHFQ
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rsp, 0x1000, "RSP restored");
}

#[test]
fn test_popfq_restore_carry() {
    let code = [
        0xf9, // STC (set carry)
        0x48, 0x9c, // PUSHFQ
        0xf8, // CLC (clear carry)
        0x48, 0x9d, // POPFQ (restore carry)
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_cf, "CF should be restored");
}

#[test]
fn test_popfq_restore_zero() {
    let code = [
        0x48, 0x31, 0xc0, // XOR RAX, RAX (sets ZF)
        0x48, 0x9c, // PUSHFQ
        0x48, 0xc7, 0xc0, 0x01, 0x00, 0x00, 0x00, // MOV RAX, 1
        0x48, 0x85, 0xc0, // TEST RAX, RAX (clears ZF)
        0x48, 0x9d, // POPFQ (restore ZF)
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_zf, "ZF should be restored");
}

#[test]
fn test_popfq_restore_sign() {
    let code = [
        0x48, 0xc7, 0xc0, 0xff, 0xff, 0xff, 0xff, // MOV RAX, -1
        0x48, 0x85, 0xc0, // TEST RAX, RAX (sets SF)
        0x48, 0x9c, // PUSHFQ
        0x48, 0x31, 0xc0, // XOR RAX, RAX (clears SF)
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_sf, "SF should be restored");
}

#[test]
fn test_popfq_restore_all_flags() {
    let code = [
        // XOR clears CF, so do XOR first to set ZF, then STC to set CF
        0x48, 0x31, 0xc0, // XOR RAX, RAX (sets ZF, PF, clears CF)
        0xf9, // STC (set CF after XOR)
        0x48, 0x9c, // PUSHFQ
        0xf8, // CLC
        0x48, 0xc7, 0xc0, 0x01, 0x00, 0x00, 0x00, // MOV RAX, 1
        0x48, 0x85, 0xc0, // TEST RAX, RAX (clears ZF)
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_cf, "CF should be restored");
    assert!(emu.flags().f_zf, "ZF should be restored");
}

#[test]
fn test_popfq_preserves_registers() {
    let code = [
        0x48, 0xc7, 0xc0, 0x42, 0x00, 0x00, 0x00, // MOV RAX, 0x42
        0x48, 0xc7, 0xc3, 0x99, 0x00, 0x00, 0x00, // MOV RBX, 0x99
        0x48, 0x9c, // PUSHFQ
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rax, 0x42, "RAX unchanged");
    assert_eq!(emu.regs().rbx, 0x99, "RBX unchanged");
}

// ============================================================================
// PUSHF/POPF Roundtrip Tests
// ============================================================================

#[test]
fn test_pushfq_popfq_roundtrip() {
    let code = [
        0xf9, // STC
        0x48, 0x9c, // PUSHFQ
        0xf8, // CLC
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_cf, "CF should be restored by POPFQ");
    assert_eq!(emu.regs().rsp, 0x1000, "RSP should be balanced");
}

#[test]
fn test_pushfq_popfq_nested() {
    let code = [
        0xf9, // STC
        0x48, 0x9c, // PUSHFQ (save flags with CF)
        0xf8, // CLC
        0x48, 0x9c, // PUSHFQ (save flags without CF)
        0x48, 0x9d, // POPFQ (restore no CF)
        0x48, 0x9d, // POPFQ (restore CF)
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_cf, "CF should be restored from first PUSHFQ");
    assert_eq!(emu.regs().rsp, 0x1000, "Stack should be balanced");
}

#[test]
fn test_pushfq_popfq_with_arithmetic() {
    let code = [
        0x48, 0xc7, 0xc0, 0xff, 0xff, 0xff, 0xff, // MOV RAX, -1
        0x48, 0x83, 0xc0, 0x01, // ADD RAX, 1 (sets ZF, CF)
        0x48, 0x9c, // PUSHFQ
        // Do some other arithmetic
        0x48, 0xc7, 0xc0, 0x10, 0x00, 0x00, 0x00, // MOV RAX, 0x10
        0x48, 0x83, 0xc0, 0x01, // ADD RAX, 1 (clears ZF, CF)
        0x48, 0x9d, // POPFQ (restore original flags)
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_zf, "ZF should be restored");
    assert!(emu.flags().f_cf, "CF should be restored");
}

#[test]
fn test_pushfq_modify_on_stack() {
    let code = [
        0x48, 0x9c, // PUSHFQ
        // Modify CF bit on stack
        0x48, 0x8b, 0x04, 0x24, // MOV RAX, [RSP]
        0x48, 0x83, 0xc8, 0x01, // OR RAX, 0x01 (set CF bit)
        0x48, 0x89, 0x04, 0x24, // MOV [RSP], RAX
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_cf, "CF should be set from modified stack value");
}

#[test]
fn test_multiple_pushfq_popfq() {
    let code = [
        0xf9, // STC
        0x48, 0x9c, // PUSHFQ
        0x48, 0x9c, // PUSHFQ
        0x48, 0x9c, // PUSHFQ
        0x48, 0x9d, // POPFQ
        0x48, 0x9d, // POPFQ
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rsp, 0x1000, "Stack should be balanced");
    assert!(emu.flags().f_cf, "CF should still be set");
}

// ============================================================================
// 16-bit PUSHF/POPF (without REX.W prefix)
// ============================================================================

#[test]
fn test_pushf_16bit() {
    let code = [
        0x66, 0x9c, // PUSHF with 66H prefix = 16-bit push
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rsp, 0x0FFE, "RSP decremented by 2");
}

#[test]
fn test_popf_16bit() {
    let code = [
        0x66, 0x9c, // PUSHF
        0x66, 0x9d, // POPF
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rsp, 0x1000, "RSP should be balanced");
}

// ============================================================================
// Edge Cases and Special Scenarios
// ============================================================================

#[test]
fn test_pushfq_at_stack_boundary() {
    let code = [
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x08-(0x08 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x08; // Near bottom of memory
    emu.run(None).unwrap();

    assert_eq!(emu.regs().rsp, 0x00, "RSP decremented");
}

#[test]
fn test_popfq_from_prepared_stack() {
    let code = [
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x3000-(0x3000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x3000;

    let flags_with_cf = 0x0001u64;
    let flags_bytes = flags_with_cf.to_le_bytes();
    emu.maps.write_bytes_slice(0x3000, &flags_bytes);

    emu.run(None).unwrap();
    assert!(emu.flags().f_cf, "CF should be set from stack");
}

#[test]
fn test_pushfq_popfq_preserves_reserved_bits() {
    let code = [
        0x48, 0x9c, // PUSHFQ
        0x48, 0x9d, // POPFQ
        0x48, 0x9c, // PUSHFQ again
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(emu.regs().rsp, stack_val.len()).try_into().unwrap();
    let pushed_flags = u64::from_le_bytes(stack_val);

    assert_ne!(pushed_flags & 0x02, 0, "Reserved bit 1 should be set");
}

#[test]
fn test_pushfq_with_overflow() {
    let code = [
        // Use 32-bit operand size to trigger signed overflow
        // 0x7FFFFFFF + 1 = 0x80000000 (positive + positive = negative in 32-bit)
        0xb8, 0xff, 0xff, 0xff, 0x7f, // MOV EAX, 0x7FFFFFFF (32-bit, zero-extends)
        0x83, 0xc0, 0x01, // ADD EAX, 1 (32-bit add, sets OF)
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(0x0FF8, stack_val.len()).try_into().unwrap();
    let pushed_flags = u64::from_le_bytes(stack_val);
    assert_ne!(pushed_flags & 0x800, 0, "OF should be set in pushed flags");
}

#[test]
fn test_popfq_restore_overflow() {
    let code = [
        // Use 32-bit operand size to trigger signed overflow
        0xb8, 0xff, 0xff, 0xff, 0x7f, // MOV EAX, 0x7FFFFFFF (32-bit)
        0x83, 0xc0, 0x01, // ADD EAX, 1 (32-bit add, sets OF)
        0x48, 0x9c, // PUSHFQ
        0x48, 0x31, 0xc0, // XOR RAX, RAX (clears OF)
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_of, "OF should be restored");
}

#[test]
fn test_pushfq_with_direction_flag() {
    let code = [
        0xfd, // STD (set direction flag)
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(0x0FF8, stack_val.len()).try_into().unwrap();
    let pushed_flags = u64::from_le_bytes(stack_val);
    assert_ne!(pushed_flags & 0x400, 0, "DF should be set in pushed flags");
}

#[test]
fn test_popfq_restore_direction_flag() {
    let code = [
        0xfd, // STD
        0x48, 0x9c, // PUSHFQ
        0xfc, // CLD (clear direction flag)
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_df, "DF should be restored");
}

#[test]
fn test_pushfq_popfq_with_all_status_flags() {
    let code = [
        // Set up complex flag state
        // Use 32-bit ADD to set OF (64-bit won't overflow with 0x7FFFFFFF+1)
        0xb8, 0xff, 0xff, 0xff, 0x7f, // MOV EAX, 0x7FFFFFFF
        0x83, 0xc0, 0x01, // ADD EAX, 1 (sets OF, clears CF)
        // Now set CF and DF (STC/STD only modify their specific flags)
        0xf9, // STC
        0xfd, // STD
        0x48, 0x9c, // PUSHFQ
        // Clear all flags
        0xf8, // CLC
        0xfc, // CLD
        0x48, 0x31, 0xc0, // XOR RAX, RAX
        // Restore flags
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_cf, "CF should be restored");
    assert!(emu.flags().f_df, "DF should be restored");
    assert!(emu.flags().f_of, "OF should be restored");
}

#[test]
fn test_pushfq_after_comparison() {
    let code = [
        0x48, 0xc7, 0xc0, 0x0a, 0x00, 0x00, 0x00, // MOV RAX, 10
        0x48, 0xc7, 0xc3, 0x05, 0x00, 0x00, 0x00, // MOV RBX, 5
        0x48, 0x39, 0xd8, // CMP RAX, RBX (10 vs 5, clears CF, ZF, SF)
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(0x0FF8, stack_val.len()).try_into().unwrap();
    let pushed_flags = u64::from_le_bytes(stack_val);
    assert_eq!(pushed_flags & 0x01, 0, "CF should be clear (10 >= 5)");
    assert_eq!(pushed_flags & 0x40, 0, "ZF should be clear (10 != 5)");
    assert_eq!(pushed_flags & 0x80, 0, "SF should be clear (positive result)");
}

#[test]
fn test_popfq_after_comparison() {
    let code = [
        0x48, 0xc7, 0xc0, 0x05, 0x00, 0x00, 0x00, // MOV RAX, 5
        0x48, 0xc7, 0xc3, 0x0a, 0x00, 0x00, 0x00, // MOV RBX, 10
        0x48, 0x39, 0xd8, // CMP RAX, RBX (5 vs 10, sets CF)
        0x48, 0x9c, // PUSHFQ
        // Do another comparison
        0x48, 0x39, 0xc3, // CMP RBX, RAX (10 vs 5, clears CF)
        // Restore original comparison flags
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_cf, "CF should be restored from first comparison");
}

#[test]
fn test_pushfq_popfq_in_loop_simulation() {
    let code = [
        0xf9, // STC
        // Save flags
        0x48, 0x9c, // PUSHFQ
        // Clear flags
        0xf8, // CLC
        0x48, 0x31, 0xc0, // XOR RAX, RAX
        // Restore flags
        0x48, 0x9d, // POPFQ
        // Save again
        0x48, 0x9c, // PUSHFQ
        // Clear again
        0xf8, // CLC
        // Restore again
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_cf, "CF should be preserved through multiple save/restore");
    assert_eq!(emu.regs().rsp, 0x1000, "Stack should be balanced");
}

#[test]
fn test_pushfq_with_parity_flag() {
    let code = [
        0x48, 0xc7, 0xc0, 0x03, 0x00, 0x00, 0x00, // MOV RAX, 3 (0b11, even parity)
        0x48, 0x85, 0xc0, // TEST RAX, RAX (sets PF)
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(0x0FF8, stack_val.len()).try_into().unwrap();
    let pushed_flags = u64::from_le_bytes(stack_val);
    assert_ne!(pushed_flags & 0x04, 0, "PF should be set in pushed flags");
}

#[test]
fn test_popfq_restore_parity() {
    let code = [
        0x48, 0xc7, 0xc0, 0x03, 0x00, 0x00, 0x00, // MOV RAX, 3
        0x48, 0x85, 0xc0, // TEST RAX, RAX (sets PF)
        0x48, 0x9c, // PUSHFQ
        0x48, 0xc7, 0xc0, 0x07, 0x00, 0x00, 0x00, // MOV RAX, 7 (odd parity)
        0x48, 0x85, 0xc0, // TEST RAX, RAX (clears PF)
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_pf, "PF should be restored");
}

#[test]
fn test_pushfq_with_auxiliary_carry() {
    let code = [
        0x48, 0xc7, 0xc0, 0x0f, 0x00, 0x00, 0x00, // MOV RAX, 0x0F
        0x48, 0x83, 0xc0, 0x01, // ADD RAX, 1 (sets AF)
        0x48, 0x9c, // PUSHFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    let mut stack_val = [0u8; 8];
    stack_val = emu.maps.read_bytes(0x0FF8, stack_val.len()).try_into().unwrap();
    let pushed_flags = u64::from_le_bytes(stack_val);
    assert_ne!(pushed_flags & 0x10, 0, "AF should be set in pushed flags");
}

#[test]
fn test_popfq_restore_auxiliary_carry() {
    let code = [
        0x48, 0xc7, 0xc0, 0x0f, 0x00, 0x00, 0x00, // MOV RAX, 0x0F
        0x48, 0x83, 0xc0, 0x01, // ADD RAX, 1 (sets AF)
        0x48, 0x9c, // PUSHFQ
        0x48, 0x31, 0xc0, // XOR RAX, RAX (clears AF)
        0x48, 0x9d, // POPFQ
        0xf4, // HLT
    ];
    let mut emu = emu64();
    emu.load_code_bytes(&code);
    emu.maps.create_map("stack_test", 0x1000-(0x1000 / 2), 0x1000, crate::maps::mem64::Permission::READ_WRITE_EXECUTE).unwrap();
    emu.regs_mut().rsp = 0x1000;
    emu.run(None).unwrap();

    assert!(emu.flags().f_af, "AF should be restored");
}