| 1 |
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 |
/* Copyright (c) 2011, Google Inc. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* * Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* * Redistributions in binary form must reproduce the above |
| 12 |
* copyright notice, this list of conditions and the following disclaimer |
| 13 |
* in the documentation and/or other materials provided with the |
| 14 |
* distribution. |
| 15 |
* * Neither the name of Google Inc. nor the names of its |
| 16 |
* contributors may be used to endorse or promote products derived from |
| 17 |
* this software without specific prior written permission. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
* |
| 31 |
* --- |
| 32 |
* Author: Joi Sigurdsson |
| 33 |
* Author: Scott Francis |
| 34 |
* |
| 35 |
* Unit tests for PreamblePatcher |
| 36 |
*/ |
| 37 |
|
| 38 |
#include "config_for_unittests.h" |
| 39 |
#include "preamble_patcher.h" |
| 40 |
#include "mini_disassembler.h" |
| 41 |
#pragma warning(push) |
| 42 |
#pragma warning(disable:4553) |
| 43 |
#include "auto_testing_hook.h" |
| 44 |
#pragma warning(pop) |
| 45 |
|
| 46 |
#define WIN32_LEAN_AND_MEAN |
| 47 |
#include <windows.h> |
| 48 |
#include <tchar.h> |
| 49 |
|
| 50 |
// Turning off all optimizations for this file, since the official build's |
| 51 |
// "Whole program optimization" seems to cause the TestPatchUsingDynamicStub |
| 52 |
// test to crash with an access violation. We debugged this and found |
| 53 |
// that the optimized access a register that is changed by a call to the hook |
| 54 |
// function. |
| 55 |
#pragma optimize("", off) |
| 56 |
|
| 57 |
// A convenience macro to avoid a lot of casting in the tests. |
| 58 |
// I tried to make this a templated function, but windows complained: |
| 59 |
// error C2782: 'sidestep::SideStepError `anonymous-namespace'::Unpatch(T,T,T *)' : template parameter 'T' is ambiguous |
| 60 |
// could be 'int (int)' |
| 61 |
// or 'int (__cdecl *)(int)' |
| 62 |
// My life isn't long enough to try to figure out how to fix this. |
| 63 |
#define UNPATCH(target_function, replacement_function, original_function_stub) \ |
| 64 |
sidestep::PreamblePatcher::Unpatch((void*)(target_function), \ |
| 65 |
(void*)(replacement_function), \ |
| 66 |
(void*)(original_function)) |
| 67 |
|
| 68 |
namespace { |
| 69 |
|
| 70 |
// Function for testing - this is what we patch |
| 71 |
// |
| 72 |
// NOTE: Because of the way the compiler optimizes this function in |
| 73 |
// release builds, we need to use a different input value every time we |
| 74 |
// call it within a function, otherwise the compiler will just reuse the |
| 75 |
// last calculated incremented value. |
| 76 |
int __declspec(noinline) IncrementNumber(int i) { |
| 77 |
#ifdef _M_X64 |
| 78 |
__int64 i2 = i + 1; |
| 79 |
return (int) i2; |
| 80 |
#else |
| 81 |
return i + 1; |
| 82 |
#endif |
| 83 |
} |
| 84 |
|
| 85 |
extern "C" int TooShortFunction(int); |
| 86 |
|
| 87 |
extern "C" int JumpShortCondFunction(int); |
| 88 |
|
| 89 |
extern "C" int JumpNearCondFunction(int); |
| 90 |
|
| 91 |
extern "C" int JumpAbsoluteFunction(int); |
| 92 |
|
| 93 |
extern "C" int CallNearRelativeFunction(int); |
| 94 |
|
| 95 |
typedef int (*IncrementingFunc)(int); |
| 96 |
IncrementingFunc original_function = NULL; |
| 97 |
|
| 98 |
int HookIncrementNumber(int i) { |
| 99 |
SIDESTEP_ASSERT(original_function != NULL); |
| 100 |
int incremented_once = original_function(i); |
| 101 |
return incremented_once + 1; |
| 102 |
} |
| 103 |
|
| 104 |
// For the AutoTestingHook test, we can't use original_function, because |
| 105 |
// all that is encapsulated. |
| 106 |
// This function "increments" by 10, just to set it apart from the other |
| 107 |
// functions. |
| 108 |
int __declspec(noinline) AutoHookIncrementNumber(int i) { |
| 109 |
return i + 10; |
| 110 |
} |
| 111 |
|
| 112 |
}; // namespace |
| 113 |
|
| 114 |
namespace sidestep { |
| 115 |
|
| 116 |
bool TestDisassembler() { |
| 117 |
unsigned int instruction_size = 0; |
| 118 |
sidestep::MiniDisassembler disassembler; |
| 119 |
void * target = reinterpret_cast<unsigned char *>(IncrementNumber); |
| 120 |
void * new_target = PreamblePatcher::ResolveTarget(target); |
| 121 |
if (target != new_target) |
| 122 |
target = new_target; |
| 123 |
|
| 124 |
while (1) { |
| 125 |
sidestep::InstructionType instructionType = disassembler.Disassemble( |
| 126 |
reinterpret_cast<unsigned char *>(target) + instruction_size, |
| 127 |
instruction_size); |
| 128 |
if (sidestep::IT_RETURN == instructionType) { |
| 129 |
return true; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
bool TestPatchWithLongJump() { |
| 135 |
original_function = NULL; |
| 136 |
void *p = ::VirtualAlloc(reinterpret_cast<void *>(0x0000020000000000), 4096, |
| 137 |
MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); |
| 138 |
SIDESTEP_EXPECT_TRUE(p != NULL); |
| 139 |
memset(p, 0xcc, 4096); |
| 140 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 141 |
sidestep::PreamblePatcher::Patch(IncrementNumber, |
| 142 |
(IncrementingFunc) p, |
| 143 |
&original_function)); |
| 144 |
SIDESTEP_ASSERT((*original_function)(1) == 2); |
| 145 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 146 |
UNPATCH(IncrementNumber, |
| 147 |
(IncrementingFunc)p, |
| 148 |
original_function)); |
| 149 |
::VirtualFree(p, 0, MEM_RELEASE); |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
bool TestPatchWithPreambleShortCondJump() { |
| 154 |
original_function = NULL; |
| 155 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 156 |
sidestep::PreamblePatcher::Patch(JumpShortCondFunction, |
| 157 |
HookIncrementNumber, |
| 158 |
&original_function)); |
| 159 |
(*original_function)(1); |
| 160 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 161 |
UNPATCH(JumpShortCondFunction, |
| 162 |
(void*)HookIncrementNumber, |
| 163 |
original_function)); |
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
bool TestPatchWithPreambleNearRelativeCondJump() { |
| 168 |
original_function = NULL; |
| 169 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 170 |
sidestep::PreamblePatcher::Patch(JumpNearCondFunction, |
| 171 |
HookIncrementNumber, |
| 172 |
&original_function)); |
| 173 |
(*original_function)(0); |
| 174 |
(*original_function)(1); |
| 175 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 176 |
UNPATCH(JumpNearCondFunction, |
| 177 |
HookIncrementNumber, |
| 178 |
original_function)); |
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
bool TestPatchWithPreambleAbsoluteJump() { |
| 183 |
original_function = NULL; |
| 184 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 185 |
sidestep::PreamblePatcher::Patch(JumpAbsoluteFunction, |
| 186 |
HookIncrementNumber, |
| 187 |
&original_function)); |
| 188 |
(*original_function)(0); |
| 189 |
(*original_function)(1); |
| 190 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 191 |
UNPATCH(JumpAbsoluteFunction, |
| 192 |
HookIncrementNumber, |
| 193 |
original_function)); |
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
bool TestPatchWithPreambleNearRelativeCall() { |
| 198 |
original_function = NULL; |
| 199 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 200 |
sidestep::PreamblePatcher::Patch( |
| 201 |
CallNearRelativeFunction, |
| 202 |
HookIncrementNumber, |
| 203 |
&original_function)); |
| 204 |
(*original_function)(0); |
| 205 |
(*original_function)(1); |
| 206 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 207 |
UNPATCH(CallNearRelativeFunction, |
| 208 |
HookIncrementNumber, |
| 209 |
original_function)); |
| 210 |
return true; |
| 211 |
} |
| 212 |
|
| 213 |
bool TestPatchUsingDynamicStub() { |
| 214 |
original_function = NULL; |
| 215 |
SIDESTEP_EXPECT_TRUE(IncrementNumber(1) == 2); |
| 216 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 217 |
sidestep::PreamblePatcher::Patch(IncrementNumber, |
| 218 |
HookIncrementNumber, |
| 219 |
&original_function)); |
| 220 |
SIDESTEP_EXPECT_TRUE(original_function); |
| 221 |
SIDESTEP_EXPECT_TRUE(IncrementNumber(2) == 4); |
| 222 |
SIDESTEP_EXPECT_TRUE(original_function(3) == 4); |
| 223 |
|
| 224 |
// Clearbox test to see that the function has been patched. |
| 225 |
sidestep::MiniDisassembler disassembler; |
| 226 |
unsigned int instruction_size = 0; |
| 227 |
SIDESTEP_EXPECT_TRUE(sidestep::IT_JUMP == disassembler.Disassemble( |
| 228 |
reinterpret_cast<unsigned char*>(IncrementNumber), |
| 229 |
instruction_size)); |
| 230 |
|
| 231 |
// Since we patched IncrementNumber, its first statement is a |
| 232 |
// jmp to the hook function. So verify that we now can not patch |
| 233 |
// IncrementNumber because it starts with a jump. |
| 234 |
#if 0 |
| 235 |
IncrementingFunc dummy = NULL; |
| 236 |
// TODO([email protected]): restore this test once flag is added to |
| 237 |
// disable JMP following |
| 238 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_JUMP_INSTRUCTION == |
| 239 |
sidestep::PreamblePatcher::Patch(IncrementNumber, |
| 240 |
HookIncrementNumber, |
| 241 |
&dummy)); |
| 242 |
|
| 243 |
// This test disabled because code in preamble_patcher_with_stub.cc |
| 244 |
// asserts before returning the error code -- so there is no way |
| 245 |
// to get an error code here, in debug build. |
| 246 |
dummy = NULL; |
| 247 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_FUNCTION_TOO_SMALL == |
| 248 |
sidestep::PreamblePatcher::Patch(TooShortFunction, |
| 249 |
HookIncrementNumber, |
| 250 |
&dummy)); |
| 251 |
#endif |
| 252 |
|
| 253 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 254 |
UNPATCH(IncrementNumber, |
| 255 |
HookIncrementNumber, |
| 256 |
original_function)); |
| 257 |
return true; |
| 258 |
} |
| 259 |
|
| 260 |
bool PatchThenUnpatch() { |
| 261 |
original_function = NULL; |
| 262 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 263 |
sidestep::PreamblePatcher::Patch(IncrementNumber, |
| 264 |
HookIncrementNumber, |
| 265 |
&original_function)); |
| 266 |
SIDESTEP_EXPECT_TRUE(original_function); |
| 267 |
SIDESTEP_EXPECT_TRUE(IncrementNumber(1) == 3); |
| 268 |
SIDESTEP_EXPECT_TRUE(original_function(2) == 3); |
| 269 |
|
| 270 |
SIDESTEP_EXPECT_TRUE(sidestep::SIDESTEP_SUCCESS == |
| 271 |
UNPATCH(IncrementNumber, |
| 272 |
HookIncrementNumber, |
| 273 |
original_function)); |
| 274 |
original_function = NULL; |
| 275 |
SIDESTEP_EXPECT_TRUE(IncrementNumber(3) == 4); |
| 276 |
|
| 277 |
return true; |
| 278 |
} |
| 279 |
|
| 280 |
bool AutoTestingHookTest() { |
| 281 |
SIDESTEP_EXPECT_TRUE(IncrementNumber(1) == 2); |
| 282 |
|
| 283 |
// Inner scope, so we can test what happens when the AutoTestingHook |
| 284 |
// goes out of scope |
| 285 |
{ |
| 286 |
AutoTestingHook hook = MakeTestingHook(IncrementNumber, |
| 287 |
AutoHookIncrementNumber); |
| 288 |
(void) hook; |
| 289 |
SIDESTEP_EXPECT_TRUE(IncrementNumber(2) == 12); |
| 290 |
} |
| 291 |
SIDESTEP_EXPECT_TRUE(IncrementNumber(3) == 4); |
| 292 |
|
| 293 |
return true; |
| 294 |
} |
| 295 |
|
| 296 |
bool AutoTestingHookInContainerTest() { |
| 297 |
SIDESTEP_EXPECT_TRUE(IncrementNumber(1) == 2); |
| 298 |
|
| 299 |
// Inner scope, so we can test what happens when the AutoTestingHook |
| 300 |
// goes out of scope |
| 301 |
{ |
| 302 |
AutoTestingHookHolder hook(MakeTestingHookHolder(IncrementNumber, |
| 303 |
AutoHookIncrementNumber)); |
| 304 |
(void) hook; |
| 305 |
SIDESTEP_EXPECT_TRUE(IncrementNumber(2) == 12); |
| 306 |
} |
| 307 |
SIDESTEP_EXPECT_TRUE(IncrementNumber(3) == 4); |
| 308 |
|
| 309 |
return true; |
| 310 |
} |
| 311 |
|
| 312 |
bool TestPreambleAllocation() { |
| 313 |
__int64 diff = 0; |
| 314 |
void* p1 = reinterpret_cast<void*>(0x110000000); |
| 315 |
void* p2 = reinterpret_cast<void*>(0x810000000); |
| 316 |
unsigned char* b1 = PreamblePatcher::AllocPreambleBlockNear(p1); |
| 317 |
SIDESTEP_EXPECT_TRUE(b1 != NULL); |
| 318 |
diff = reinterpret_cast<__int64>(p1) - reinterpret_cast<__int64>(b1); |
| 319 |
// Ensure blocks are within 2GB |
| 320 |
SIDESTEP_EXPECT_TRUE(diff <= INT_MAX && diff >= INT_MIN); |
| 321 |
unsigned char* b2 = PreamblePatcher::AllocPreambleBlockNear(p2); |
| 322 |
SIDESTEP_EXPECT_TRUE(b2 != NULL); |
| 323 |
diff = reinterpret_cast<__int64>(p2) - reinterpret_cast<__int64>(b2); |
| 324 |
SIDESTEP_EXPECT_TRUE(diff <= INT_MAX && diff >= INT_MIN); |
| 325 |
|
| 326 |
// Ensure we're reusing free blocks |
| 327 |
unsigned char* b3 = b1; |
| 328 |
unsigned char* b4 = b2; |
| 329 |
PreamblePatcher::FreePreambleBlock(b1); |
| 330 |
PreamblePatcher::FreePreambleBlock(b2); |
| 331 |
b1 = PreamblePatcher::AllocPreambleBlockNear(p1); |
| 332 |
SIDESTEP_EXPECT_TRUE(b1 == b3); |
| 333 |
b2 = PreamblePatcher::AllocPreambleBlockNear(p2); |
| 334 |
SIDESTEP_EXPECT_TRUE(b2 == b4); |
| 335 |
PreamblePatcher::FreePreambleBlock(b1); |
| 336 |
PreamblePatcher::FreePreambleBlock(b2); |
| 337 |
|
| 338 |
return true; |
| 339 |
} |
| 340 |
|
| 341 |
bool UnitTests() { |
| 342 |
return TestPatchWithPreambleNearRelativeCall() && |
| 343 |
TestPatchWithPreambleAbsoluteJump() && |
| 344 |
TestPatchWithPreambleNearRelativeCondJump() && |
| 345 |
TestPatchWithPreambleShortCondJump() && |
| 346 |
TestDisassembler() && TestPatchWithLongJump() && |
| 347 |
TestPatchUsingDynamicStub() && PatchThenUnpatch() && |
| 348 |
AutoTestingHookTest() && AutoTestingHookInContainerTest() && |
| 349 |
TestPreambleAllocation(); |
| 350 |
} |
| 351 |
|
| 352 |
}; // namespace sidestep |
| 353 |
|
| 354 |
int safe_vsnprintf(char *str, size_t size, const char *format, va_list ap) { |
| 355 |
if (size == 0) // not even room for a \0? |
| 356 |
return -1; // not what C99 says to do, but what windows does |
| 357 |
str[size-1] = '\0'; |
| 358 |
return _vsnprintf(str, size-1, format, ap); |
| 359 |
} |
| 360 |
|
| 361 |
int _tmain(int argc, _TCHAR* argv[]) |
| 362 |
{ |
| 363 |
bool ret = sidestep::UnitTests(); |
| 364 |
printf("%s\n", ret ? "PASS" : "FAIL"); |
| 365 |
return ret ? 0 : -1; |
| 366 |
} |
| 367 |
|
| 368 |
#pragma optimize("", on) |