-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModRMTest.cs
204 lines (183 loc) · 7 KB
/
ModRMTest.cs
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
// This file is part of bugreport.
// Copyright (c) 2006-2009 The bugreport Developers.
// See AUTHORS.txt for details.
// Licensed under the GNU General Public License, Version 3 (GPLv3).
// See LICENSE.txt for details.
using System;
using NUnit.Framework;
namespace bugreport
{
[TestFixture]
public class ModRMTest
{
private Byte[] code;
[Test]
public void EaxEaxNoIndex()
{
// mov eax,DWORD PTR [eax]
code = new Byte[] {0x8b, 0x00};
Assert.AreEqual(RegisterName.EAX, ModRM.GetEvFor(code));
Assert.AreEqual(RegisterName.EAX, ModRM.GetGvFor(code));
Assert.IsFalse(ModRM.HasIndex(code));
Assert.IsTrue(ModRM.IsEffectiveAddressDereferenced(code));
}
[Test]
[ExpectedException(typeof(NotImplementedException))]
public void EaxEbpPlusDword12()
{
const int INDEX = 0x0c;
code = new Byte[] {0x8b, 0x85, INDEX, 0x00, 0x00, 0x00};
Assert.IsTrue(ModRM.HasIndex(code));
Assert.AreEqual(INDEX, ModRM.GetIndexFor(code));
}
[Test]
public void EaxEbpPlusSByte12()
{
// mov eax,DWORD PTR [ebp+index]
const int INDEX = 0x0c;
code = new Byte[] {0x8b, 0x45, INDEX};
Assert.AreEqual(RegisterName.EBP, ModRM.GetEvFor(code));
Assert.AreEqual(RegisterName.EAX, ModRM.GetGvFor(code));
Assert.AreEqual(INDEX, ModRM.GetIndexFor(code));
}
[Test]
public void EaxEbx4()
{
// mov eax,DWORD PTR [ebx-4]
code = new Byte[] {0x8b, 0x43, 0xfc};
Assert.AreEqual(RegisterName.EBX, ModRM.GetEvFor(code));
Assert.AreEqual(RegisterName.EAX, ModRM.GetGvFor(code));
Assert.AreEqual(0xfc, ModRM.GetIndexFor(code));
}
[Test]
public void EbxEbp12()
{
// mov ebx,DWORD PTR [ebp-12]
code = new Byte[] {0x8b, 0x5d, 0xf4};
Assert.AreEqual(RegisterName.EBP, ModRM.GetEvFor(code));
Assert.AreEqual(RegisterName.EBX, ModRM.GetGvFor(code));
Assert.IsFalse(ModRM.HasSIB(code));
Assert.AreEqual(0xf4, ModRM.GetIndexFor(code));
}
[Test]
public void EbxEsp0()
{
// mov ebx,DWORD PTR [esp]
code = new Byte[] {0x8b, 0x1c, 0x24};
//// Assert.AreEqual(RegisterName.ESP, ModRM.GetEvFor(code));
Assert.IsTrue(ModRM.HasSIB(code));
Assert.AreEqual(RegisterName.EBX, ModRM.GetGvFor(code));
Assert.IsFalse(ModRM.HasIndex(code));
}
[Test]
public void EdiEbp4()
{
// mov edi,DWORD PTR [ebp-4]
code = new Byte[] {0x8b, 0x7d, 0xfc};
Assert.AreEqual(RegisterName.EBP, ModRM.GetEvFor(code));
Assert.AreEqual(RegisterName.EDI, ModRM.GetGvFor(code));
Assert.AreEqual(0xfc, ModRM.GetIndexFor(code));
}
[Test]
public void EsiEbp8()
{
// mov esi,DWORD PTR [ebp-8]
code = new Byte[] {0x8b, 0x75, 0xf8};
Assert.AreEqual(RegisterName.EBP, ModRM.GetEvFor(code));
Assert.AreEqual(RegisterName.ESI, ModRM.GetGvFor(code));
Assert.AreEqual(0xf8, ModRM.GetIndexFor(code));
}
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void GetEvWithSIB()
{
code = new Byte[] {0xc7, 0x04, 0x24, 0x10, 0x00, 0x00, 0x00};
Assert.IsTrue(ModRM.HasSIB(code));
ModRM.GetEvFor(code);
}
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void GetIndexWithNoIndex()
{
code = new Byte[] {0x89, 0xe5};
Assert.IsFalse(ModRM.HasIndex(code));
ModRM.GetIndexFor(code);
}
[Test]
public void GvEbOffsetToRegister()
{
// movzx edx,BYTE PTR ds:0x80495e0
code = new Byte[] {0x0f, 0xb6, 0x15, 0xe0, 0x95, 0x04, 0x08};
Assert.AreEqual(RegisterName.EDX, ModRM.GetGvFor(code));
Assert.IsFalse(ModRM.HasIndex(code));
Assert.IsTrue(ModRM.IsEffectiveAddressDereferenced(code));
Assert.IsTrue(ModRM.HasOffset(code));
}
[Test]
public void GvMWithSIB()
{
code = new Byte[] {0x8d, 0x04, 0x02};
Assert.IsTrue(ModRM.HasSIB(code));
Assert.AreEqual(RegisterName.EAX, ModRM.GetGvFor(code));
}
[Test]
public void IsEvDwordFalse()
{
code = new Byte[] {0x31, 0x07};
Assert.IsFalse(ModRM.IsEvDword(code));
}
[Test]
public void IsEvDwordTrue()
{
code = new Byte[] {0x31, 0x05};
Assert.IsTrue(ModRM.IsEvDword(code));
}
[Test]
public void MovEbpEsp()
{
// mov ebp,esp
code = new Byte[] {0x89, 0xe5};
Assert.AreEqual(RegisterName.EBP, ModRM.GetEvFor(code));
Assert.AreEqual(RegisterName.ESP, ModRM.GetGvFor(code));
Assert.IsFalse(ModRM.HasIndex(code));
Assert.IsFalse(ModRM.IsEffectiveAddressDereferenced(code));
}
[Test]
public void MovEsp16()
{
// mov DWORD PTR [esp],0x10
code = new Byte[] {0xc7, 0x04, 0x24, 0x10, 0x00, 0x00, 0x00};
Assert.IsTrue(ModRM.HasSIB(code));
}
[Test]
public void TwoByteWithModRMNoIndex()
{
// movzx ebx,BYTE PTR [eax]
code = new Byte[] {0x0f, 0xb6, 0x18};
Assert.AreEqual(RegisterName.EAX, ModRM.GetEvFor(code));
Assert.AreEqual(RegisterName.EBX, ModRM.GetGvFor(code));
Assert.IsFalse(ModRM.HasIndex(code));
Assert.IsTrue(ModRM.IsEffectiveAddressDereferenced(code));
}
[Test]
public void TwoByteWithModRMWithIndex()
{
// movzx eax,BYTE PTR [ebp-5]
code = new Byte[] {0x0f, 0xb6, 0x45, 0xfb};
Assert.AreEqual(RegisterName.EBP, ModRM.GetEvFor(code));
Assert.AreEqual(RegisterName.EAX, ModRM.GetGvFor(code));
Assert.AreEqual(0xfb, ModRM.GetIndexFor(code));
Assert.IsTrue(ModRM.IsEffectiveAddressDereferenced(code));
}
[Test]
public void TwoByteWithModRMWithOnlyOffset()
{
// movzx edx,BYTE PTR ds:0x80495e0
code = new Byte[] {0x0f, 0xb6, 0x15, 0xe0, 0x95, 0x04, 0x08};
Assert.AreEqual(RegisterName.EDX, ModRM.GetGvFor(code));
Assert.IsFalse(ModRM.HasIndex(code));
Assert.IsTrue(ModRM.IsEffectiveAddressDereferenced(code));
Assert.IsTrue(ModRM.HasOffset(code));
}
}
}