blib/lib/Games/Hack/Patch/x86_64.pm

filter: Statementstmt 100% 20 / 20 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod 100% 1 / 1 totaltotal 100% 34 / 34 CC 7 SCAR 19.5
CC
7
Cov
100%
GetNOP
7.0
CRAP 7.0
↑ Summary
1 #!/usr/bin/perl
2 # vim: set sw=2 expandtab : #
3 # Master version is at https://developer.berlios.de/projects/games-hack/
4  
5 package Games::Hack::Patch::x86_64;
6  
7 1 $VERSION=0.13;
8  
9 1 require Exporter;
10 1 @ISA = qw(Exporter);
11 1 @EXPORT = qw(GetNOP);
12  
13  
14  
15 sub GetNOP
16 8 ▶{
Subroutine GetNOP: called CC=7 SCAR=19.5
Pod documented
17 8 my($adr_start, $adr_end, $disass)=@_;
18 8 my($binary, $diff);
19  
20 8 $binary="";
21  
22 ### Floating point store? Stores always %ST(0).
23 8 ▶ if ($disass =~ m#^f[bi]?st(p)?s? #)
Branch: 3 branches
BranchTrueFalseTotal
if ($disass =~ /^f[bi]?st(p)?s? /) { } 3 5 8
elsif ($disass =~ /^pop/) { } 1 4 5
elsif ($disass =~ /^(mov|or|and|add|sub|xor)/) { } 2 2 4
24 {
25 3 ▶ if ($1)
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 2 1 3
26 {
27 # If the "Pop" flag is given in the "fst" ("floating point store")
28 # operation, we need to clean the floating point stack.
29 # We use the "ffreep st(0)" instruction, .
30 2 $binary .= "\xdf\xc0";
31  
32 2 $adr_start += 2;
33 }
34 # Rest gets done by jump.
35 }
36 # Popping values from the stack
37 elsif ($disass =~ m#^pop#)
38 {
39 # increment esp; rest done by jump.
40 # is there a 64bit pop?
41 1 $binary .= "\x83\xc4\x04";
42 1 $adr_start += 3;
43 }
44 ### Integer move or direct modification
45 elsif ($disass =~ m#^(mov|or|and|add|sub|xor)#)
46 {
47 # done by jump.
48 }
49 else
50 {
51 2 warn "Unknown instruction '" . $disass . "', patching unsafe!";
52 2 return undef;
53 }
54  
55  
56 ### short jump
57 6 ▶ if (($diff=$adr_end-$adr_start) >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (($diff = $adr_end - $adr_start) >= 2) 4 2 6
58 {
59 # The opcode needs two bytes; these are already consumed, and so the jump
60 # distance has to be reduced.
61 4 $binary .= "\xeb" . pack("C", $diff-2);
62 4 $adr_end=$adr_start;
63 }
64  
65 ### NOP.
66 # That should never be needed ... all instructions involving memory access
67 # (even via a register) have at least 2 bytes.
68 # (If some value would be persistently stored in a register, we wouldn't
69 # find it by looking at the memory contents.)
70 6 ▶ $binary .= "\x90" x $diff
Branch: 1 branch
BranchTrueFalseTotal
if ($diff = $adr_end - $adr_start) >= 1 2 4 6
71 if ($diff=$adr_end-$adr_start) >= 1;
72  
73 6 return $binary;
74 }
75  
76 __END__
↑ Summary