VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstLdrDisasmTest.cpp@ 14534

Last change on this file since 14534 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1/* $Id: tstLdrDisasmTest.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
2/** @file
3 * IPRT - RTLdr test object.
4 *
5 * We use precompiled versions of this object for testing all the loaders.
6 *
7 * This is not supposed to be pretty or usable code, just something which
8 * make life difficult for the loader.
9 */
10
11/*
12 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
13 *
14 * This file is part of VirtualBox Open Source Edition (OSE), as
15 * available from http://www.virtualbox.org. This file is free software;
16 * you can redistribute it and/or modify it under the terms of the GNU
17 * General Public License (GPL) as published by the Free Software
18 * Foundation, in version 2 as it comes in the "COPYING" file of the
19 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
20 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
21 *
22 * The contents of this file may alternatively be used under the terms
23 * of the Common Development and Distribution License Version 1.0
24 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
25 * VirtualBox OSE distribution, in which case the provisions of the
26 * CDDL are applicable instead of those of the GPL.
27 *
28 * You may elect to license modified versions of this file under the
29 * terms and conditions of either the GPL or the CDDL or both.
30 *
31 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
32 * Clara, CA 95054 USA or visit http://www.sun.com if you need
33 * additional information or have any questions.
34 */
35
36
37
38/*******************************************************************************
39* Header Files *
40*******************************************************************************/
41#include <VBox/dis.h>
42#include <VBox/disopcode.h>
43#include <iprt/string.h>
44
45#if defined(IN_RING0) && !defined(RT_OS_WINDOWS) /* Too lazy to make import libs. */
46extern "C" DECLIMPORT(int) MyPrintf(const char *pszFormat, ...);
47# define MY_PRINTF(a) MyPrintf a
48#else
49# define MY_PRINTF(a) do {} while (0)
50#endif
51
52
53/*******************************************************************************
54* Global Variables *
55*******************************************************************************/
56
57/* 32-bit code */
58static const uint8_t g_ab32BitCode[] =
59{
60 0x55, // 1000ab50 55 push ebp
61 0x8b,0xec, // 1000ab51 8bec mov ebp,esp
62 0x8b,0x45,0x08, // 1000ab53 8b4508 mov eax,dword ptr [ebp+8]
63 0x81,0x38,0x07,0x07,// 1000ab56 813807076419 cmp dword ptr [eax],19640707h
64 0x64,0x19,
65 0x75,0x09, // 1000ab5c 7509 jne kLdr!kLdrModMap+0x17 (1000ab67)
66 0x8b,0x4d,0x08, // 1000ab5e 8b4d08 mov ecx,dword ptr [ebp+8]
67 0x83,0x79,0x2c,0x00,// 1000ab61 83792c00 cmp dword ptr [ecx+2Ch],0
68 0x75,0x07, // 1000ab65 7507 jne kLdr!kLdrModMap+0x1e (1000ab6e)
69 0xb8,0xc0,0x68,0x06,// 1000ab67 b8c0680600 mov eax,668C0h
70 0x00,
71 0xeb,0x14, // 1000ab6c eb14 jmp kLdr!kLdrModMap+0x32 (1000ab82)
72 0x33,0xd2, // 1000ab6e 33d2 xor edx,edx
73 0x75,0xe1, // 1000ab70 75e1 jne kLdr!kLdrModMap+0x3 (1000ab53)
74 0x8b,0x45,0x08, // 1000ab72 8b4508 mov eax,dword ptr [ebp+8]
75 0x50, // 1000ab75 50 push eax
76 0x8b,0x4d,0x08, // 1000ab76 8b4d08 mov ecx,dword ptr [ebp+8]
77 0x8b,0x51,0x2c, // 1000ab79 8b512c mov edx,dword ptr [ecx+2Ch]
78 0xff,0x52,0x3c, // 1000ab7c ff523c call dword ptr [edx+3Ch]
79 0x83,0xc4,0x04, // 1000ab7f 83c404 add esp,4
80 0x5d, // 1000ab82 5d pop ebp
81 0xc3, // 1000ab83 c3 ret
82 0xcc
83};
84
85
86DECLCALLBACK(int) DisasmTest1ReadCode(RTUINTPTR SrcAddr, uint8_t *pbDst, uint32_t cb, void *pvUser)
87{
88 while (cb > 0)
89 {
90 *pbDst = g_ab32BitCode[SrcAddr];
91 /* next */
92 pbDst++;
93 SrcAddr++;
94 cb--;
95 }
96 return 0;
97}
98
99
100/*
101 * Use an inline function here just to test '__textcoal_nt' sections on darwin.
102 */
103inline int MyDisasm(uintptr_t CodeIndex, PDISCPUSTATE pCpu, uint32_t *pcb)
104{
105 uint32_t cb;
106 int rc = DISCoreOneEx(CodeIndex, CPUMODE_32BIT, DisasmTest1ReadCode, 0, pCpu, &cb);
107 *pcb = cb;
108 MY_PRINTF(("DISCoreOneEx -> rc=%d cb=%d Cpu: opcode=%#x pCurInstr=%p (42=%d)\n", \
109 rc, cb, pCpu->opcode, pCpu->pCurInstr, 42)); \
110 return rc;
111}
112
113
114extern "C" DECLEXPORT(int) DisasmTest1(void)
115{
116 DISCPUSTATE Cpu;
117 uintptr_t CodeIndex = 0;
118 uint32_t cb;
119 int rc;
120 MY_PRINTF(("DisasmTest1: %p\n", &DisasmTest1));
121
122 memset(&Cpu, 0, sizeof(Cpu));
123 Cpu.mode = CPUMODE_32BIT;
124
125#define DISAS_AND_CHECK(cbInstr, enmOp) \
126 do { \
127 rc = MyDisasm(CodeIndex, &Cpu, &cb); \
128 if (RT_FAILURE(rc)) \
129 return CodeIndex | 0xf000; \
130 if (Cpu.pCurInstr->opcode != (enmOp)) \
131 return CodeIndex| 0xe000; \
132 if (cb != (cbInstr)) \
133 return CodeIndex | 0xd000; \
134 CodeIndex += cb; \
135 } while (0)
136
137 DISAS_AND_CHECK(1, OP_PUSH);
138 DISAS_AND_CHECK(2, OP_MOV);
139 DISAS_AND_CHECK(3, OP_MOV);
140 DISAS_AND_CHECK(6, OP_CMP);
141 DISAS_AND_CHECK(2, OP_JNE);
142 DISAS_AND_CHECK(3, OP_MOV);
143 DISAS_AND_CHECK(4, OP_CMP);
144 DISAS_AND_CHECK(2, OP_JNE);
145 DISAS_AND_CHECK(5, OP_MOV);
146 DISAS_AND_CHECK(2, OP_JMP);
147 DISAS_AND_CHECK(2, OP_XOR);
148 DISAS_AND_CHECK(2, OP_JNE);
149 DISAS_AND_CHECK(3, OP_MOV);
150 DISAS_AND_CHECK(1, OP_PUSH);
151 DISAS_AND_CHECK(3, OP_MOV);
152 DISAS_AND_CHECK(3, OP_MOV);
153 DISAS_AND_CHECK(3, OP_CALL);
154 DISAS_AND_CHECK(3, OP_ADD);
155 DISAS_AND_CHECK(1, OP_POP);
156 DISAS_AND_CHECK(1, OP_RETN);
157 DISAS_AND_CHECK(1, OP_INT3);
158
159 return rc;
160}
161
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette