Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 0d8454d

Browse files
committed
memory old value
1 parent 552a519 commit 0d8454d

File tree

4 files changed

+46
-134
lines changed

4 files changed

+46
-134
lines changed

src/emu.rs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,8 @@ impl Emu {
11031103
op: "write".to_string(),
11041104
bits: 32,
11051105
address: self.regs.get_esp(),
1106-
value: value as u64,
1106+
old_value: 0, // TODO
1107+
new_value: value as u64,
11071108
name: name.clone(),
11081109
};
11091110
self.memory_operations.push(memory_operation);
@@ -1163,7 +1164,8 @@ impl Emu {
11631164
op: "write".to_string(),
11641165
bits: 64,
11651166
address: self.regs.rsp,
1166-
value: value as u64,
1167+
old_value: 0, // TODO
1168+
new_value: value as u64,
11671169
name: name.clone(),
11681170
};
11691171
self.memory_operations.push(memory_operation);
@@ -1265,7 +1267,8 @@ impl Emu {
12651267
op: "read".to_string(),
12661268
bits: 32,
12671269
address: self.regs.get_esp(),
1268-
value: value as u64,
1270+
old_value: 0, // TODO
1271+
new_value: value as u64,
12691272
name: name.clone(),
12701273
};
12711274
self.memory_operations.push(memory_operation);
@@ -1327,7 +1330,8 @@ impl Emu {
13271330
op: "read".to_string(),
13281331
bits: 32,
13291332
address: self.regs.rsp,
1330-
value: value as u64,
1333+
old_value: 0, // TODO
1334+
new_value: value as u64,
13311335
name: name.clone(),
13321336
};
13331337
self.memory_operations.push(memory_operation);
@@ -1520,7 +1524,8 @@ impl Emu {
15201524
op: "read".to_string(),
15211525
bits: 64,
15221526
address: addr,
1523-
value: v as u64,
1527+
old_value: 0, // TODO
1528+
new_value: v as u64,
15241529
name: name.clone(),
15251530
};
15261531
self.memory_operations.push(memory_operation);
@@ -1543,7 +1548,8 @@ impl Emu {
15431548
op: "read".to_string(),
15441549
bits: 32,
15451550
address: addr,
1546-
value: v as u64,
1551+
old_value: 0, // TODO
1552+
new_value: v as u64,
15471553
name: name.clone(),
15481554
};
15491555
self.memory_operations.push(memory_operation);
@@ -1566,7 +1572,8 @@ impl Emu {
15661572
op: "read".to_string(),
15671573
bits: 16,
15681574
address: addr,
1569-
value: v as u64,
1575+
old_value: 0, // TODO
1576+
new_value: v as u64,
15701577
name: name.clone(),
15711578
};
15721579
self.memory_operations.push(memory_operation);
@@ -1589,7 +1596,8 @@ impl Emu {
15891596
op: "read".to_string(),
15901597
bits: 8,
15911598
address: addr,
1592-
value: v as u64,
1599+
old_value: 0, // TODO
1600+
new_value: v as u64,
15931601
name: name.clone(),
15941602
};
15951603
self.memory_operations.push(memory_operation);
@@ -1636,7 +1644,8 @@ impl Emu {
16361644
op: "write".to_string(),
16371645
bits: 32,
16381646
address: addr,
1639-
value: value as u64,
1647+
old_value: 0, // TODO
1648+
new_value: value as u64,
16401649
name: name.clone(),
16411650
};
16421651
self.memory_operations.push(memory_operation);
@@ -3590,7 +3599,8 @@ impl Emu {
35903599
op: "read".to_string(),
35913600
bits: sz,
35923601
address: mem_addr,
3593-
value: value,
3602+
old_value: 0, // TODO
3603+
new_value: value,
35943604
name: name.clone(),
35953605
};
35963606
self.memory_operations.push(memory_operation);
@@ -3755,7 +3765,8 @@ impl Emu {
37553765
op: "write".to_string(),
37563766
bits: sz,
37573767
address: mem_addr,
3758-
value: value2,
3768+
old_value: 0, // TODO
3769+
new_value: value2,
37593770
name: name.clone(),
37603771
};
37613772
self.memory_operations.push(memory_operation);
@@ -4183,9 +4194,11 @@ impl Emu {
41834194
self.post_op_flags = self.flags.clone();
41844195
}
41854196

4186-
pub fn diff_pre_op_post_op(&mut self) {
4197+
pub fn write_to_trace_file(&mut self) {
41874198
// 00,00007FFBEF4E5FF0,EB 08,jmp 7FFBEF4E5FFA,rax: 7FFBEF4E5FF0-> 7FFBEF4E5FF0 rbx: 7FFE0385-> 7FFE0385 rcx: 7FFBEE4B0000-> 7FFBEE4B0000 rdx: 1-> 1 rsp: 98EB5DDFF8-> 98EB5DDFF8 rbp: 98EB5DE338-> 98EB5DE338 rsi: 1-> 1 rdi: 7FFE0384-> 7FFE0384 r8: 0-> 0 r9: 0-> 0 r10: A440AE23305F3A70-> A440AE23305F3A70 r11: 98EB5DE068-> 98EB5DE068 r12: 7FFBEF4E5FF0-> 7FFBEF4E5FF0 r13: 1FC18C72DC0-> 1FC18C72DC0 r14: 7FFBEE4B0000-> 7FFBEE4B0000 r15: 0-> 0 rflags: 344-> 246,,OptionalHeader.AddressOfEntryPoint
41884199
// 01,00007FFBEF4E5FFA,50,push rax,rsp: 98EB5DDFF8-> 98EB5DDFF0,00000098EB5DDFF0: 7FFC65FF8B8F-> 7FFBEF4E5FF0,rax:GetMsgProc+102D07D
4200+
let index = self.pos - 1;
4201+
41894202
let instruction = self.instruction.unwrap();
41904203
let instruction_bytes = &self.instruction_bytes;
41914204

@@ -4203,22 +4216,30 @@ impl Emu {
42034216
);
42044217

42054218
let mut memory = String::new();
4206-
for op in self.memory_operations.iter() {
4207-
memory += &format!("{:?}", op);
4219+
for memory_op in self.memory_operations.iter() {
4220+
if memory_op.op == "read" {
4221+
continue;
4222+
}
4223+
// 00000098EB5DDFF0: 7FFC65FF8B8F-> 7FFBEF4E5FF0
4224+
memory = format!("{} {:x}: {:x}-> {:x}", memory, memory_op.address, memory_op.old_value, memory_op.new_value);
42084225
}
42094226

42104227
let mut trace_file = self.cfg.trace_file.as_ref().unwrap();
42114228
writeln!(
42124229
trace_file,
42134230
"{index:02X},{address:016X},{bytes:02x?},{disassembly},{registers},{memory},{comments}",
4214-
index = self.pos - 1,
4231+
index = index,
42154232
address = self.pre_op_regs.rip,
42164233
bytes = instruction_bytes,
42174234
disassembly = self.out,
42184235
registers = format!("{} {}", registers, flags),
42194236
memory = memory,
42204237
comments = ""
4221-
);
4238+
).expect("failed to write to trace file");
4239+
4240+
if index > 10 {
4241+
panic!("OUT");
4242+
}
42224243
}
42234244

42244245
fn trace_registers_64bit(&mut self) {
@@ -4556,7 +4577,7 @@ impl Emu {
45564577

45574578
if self.cfg.trace_file.is_some() {
45584579
self.capture_post_op();
4559-
self.diff_pre_op_post_op();
4580+
self.write_to_trace_file();
45604581
}
45614582

45624583
if !emulation_ok {

src/emu/flags.rs

Lines changed: 3 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -87,120 +87,9 @@ impl Flags {
8787
}
8888

8989
pub fn diff(rip: u64, pos: u64, a: Flags, b: Flags) -> String {
90-
let mut output = format!(
91-
"\tdiff_flags: pos = {} rip = {:x} in = {:x} out = {:x} ",
92-
pos,
93-
rip,
94-
a.dump(),
95-
b.dump()
96-
);
97-
if a.f_cf != b.f_cf {
98-
output = format!(
99-
"{}{} {:x} -> {:x}; ",
100-
output, "f_cf", a.f_cf as u8, b.f_cf as u8
101-
);
102-
}
103-
if a.f_pf != b.f_pf {
104-
output = format!(
105-
"{}{} {:x} -> {:x}; ",
106-
output, "f_pf", a.f_pf as u8, b.f_pf as u8
107-
);
108-
}
109-
if a.f_af != b.f_af {
110-
output = format!(
111-
"{}{} {:x} -> {:x}; ",
112-
output, "f_af", a.f_af as u8, b.f_af as u8
113-
);
114-
}
115-
if a.f_zf != b.f_zf {
116-
output = format!(
117-
"{}{} {:x} -> {:x}; ",
118-
output, "f_zf", a.f_zf as u8, b.f_zf as u8
119-
);
120-
}
121-
if a.f_sf != b.f_sf {
122-
output = format!(
123-
"{}{} {:x} -> {:x}; ",
124-
output, "f_sf", a.f_sf as u8, b.f_sf as u8
125-
);
126-
}
127-
if a.f_tf != b.f_tf {
128-
output = format!(
129-
"{}{} {:x} -> {:x}; ",
130-
output, "f_tf", a.f_tf as u8, b.f_tf as u8
131-
);
132-
}
133-
if a.f_if != b.f_if {
134-
output = format!(
135-
"{}{} {:x} -> {:x}; ",
136-
output, "f_if", a.f_if as u8, b.f_if as u8
137-
);
138-
}
139-
if a.f_df != b.f_df {
140-
output = format!(
141-
"{}{} {:x} -> {:x}; ",
142-
output, "f_df", a.f_df as u8, b.f_df as u8
143-
);
144-
}
145-
if a.f_of != b.f_of {
146-
output = format!(
147-
"{}{} {:x} -> {:x}; ",
148-
output, "f_of", a.f_of as u8, b.f_of as u8
149-
);
150-
}
151-
if a.f_iopl1 != b.f_iopl1 {
152-
output = format!(
153-
"{}{} {:x} -> {:x}; ",
154-
output, "f_iopl1", a.f_iopl1 as u8, b.f_iopl1 as u8
155-
);
156-
}
157-
if a.f_iopl2 != b.f_iopl2 {
158-
output = format!(
159-
"{}{} {:x} -> {:x}; ",
160-
output, "f_iopl2", a.f_iopl2 as u8, b.f_iopl2 as u8
161-
);
162-
}
163-
if a.f_nt != b.f_nt {
164-
output = format!(
165-
"{}{} {:x} -> {:x}; ",
166-
output, "f_nt", a.f_nt as u8, b.f_nt as u8
167-
);
168-
}
169-
if a.f_rf != b.f_rf {
170-
output = format!(
171-
"{}{} {:x} -> {:x}; ",
172-
output, "f_rf", a.f_rf as u8, b.f_rf as u8
173-
);
174-
}
175-
if a.f_vm != b.f_vm {
176-
output = format!(
177-
"{}{} {:x} -> {:x}; ",
178-
output, "f_vm", a.f_vm as u8, b.f_vm as u8
179-
);
180-
}
181-
if a.f_ac != b.f_ac {
182-
output = format!(
183-
"{}{} {:x} -> {:x}; ",
184-
output, "f_ac", a.f_ac as u8, b.f_ac as u8
185-
);
186-
}
187-
if a.f_vif != b.f_vif {
188-
output = format!(
189-
"{}{} {:x} -> {:x}; ",
190-
output, "f_vif", a.f_vif as u8, b.f_vif as u8
191-
);
192-
}
193-
if a.f_vip != b.f_vip {
194-
output = format!(
195-
"{}{} {:x} -> {:x}; ",
196-
output, "f_vip", a.f_vip as u8, b.f_vip as u8
197-
);
198-
}
199-
if a.f_id != b.f_id {
200-
output = format!(
201-
"{}{} {:x} -> {:x}; ",
202-
output, "f_id", a.f_id as u8, b.f_id as u8
203-
);
90+
let mut output = String::new();
91+
if a.dump() != b.dump() {
92+
output = format!("rflags: {:x} -> {:x}; ", a.dump(), b.dump());
20493
}
20594
output
20695
}

src/emu/regs64.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl Regs64 {
269269
}
270270

271271
pub fn diff(rip: u64, pos: u64, a: Regs64, b: Regs64) -> String {
272-
let mut output = format!("\tdiff_reg: pos = {} rip = {:x} ", pos, rip);
272+
let mut output = String::new();
273273
if a.dr0 != b.dr0 {
274274
output = format!("{}{} {:x} -> {:x}; ", output, "dr0", a.dr0, b.dr0);
275275
}

src/emu/structures.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,8 +2055,10 @@ pub struct MemoryOperation {
20552055
pub bits: u32,
20562056
/// Memory address being accessed
20572057
pub address: u64,
2058-
/// Value being read or written
2059-
pub value: u64,
2058+
/// Old value before the operation
2059+
pub old_value: u64,
2060+
/// New value after the operation
2061+
pub new_value: u64,
20602062
/// Name of the memory region being accessed
20612063
pub name: String,
20622064
}

0 commit comments

Comments
 (0)