VirtualBox

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

Last change on this file since 1321 was 1190, checked in by vboxsync, 18 years ago

Ported IPRT to ring-0 OS/2.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1/* $Id: tstLdrDisasmTest.cpp 1190 2007-03-04 20:42:13Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - 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 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
18 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
19 * distribution. VirtualBox OSE is distributed in the hope that it will
20 * be useful, but WITHOUT ANY WARRANTY of any kind.
21 *
22 * If you received this file as part of a commercial VirtualBox
23 * distribution, then only the terms of your commercial VirtualBox
24 * license agreement apply instead of the previous paragraph.
25 */
26
27
28
29/*******************************************************************************
30* Header Files *
31*******************************************************************************/
32#include <VBox/dis.h>
33#include <VBox/disopcode.h>
34#include <iprt/string.h>
35
36#if defined(IN_RING0) && !defined(__WIN__) /* Too lazy to make import libs. */
37extern "C" DECLIMPORT(int) MyPrintf(const char *pszFormat, ...);
38# define MY_PRINTF(a) MyPrintf a
39#else
40# define MY_PRINTF(a) do {} while (0)
41#endif
42
43
44/*******************************************************************************
45* Global Variables *
46*******************************************************************************/
47
48/* 32-bit code */
49static const uint8_t g_ab32BitCode[] =
50{
51 0x55, // 1000ab50 55 push ebp
52 0x8b,0xec, // 1000ab51 8bec mov ebp,esp
53 0x8b,0x45,0x08, // 1000ab53 8b4508 mov eax,dword ptr [ebp+8]
54 0x81,0x38,0x07,0x07,// 1000ab56 813807076419 cmp dword ptr [eax],19640707h
55 0x64,0x19,
56 0x75,0x09, // 1000ab5c 7509 jne kLdr!kLdrModMap+0x17 (1000ab67)
57 0x8b,0x4d,0x08, // 1000ab5e 8b4d08 mov ecx,dword ptr [ebp+8]
58 0x83,0x79,0x2c,0x00,// 1000ab61 83792c00 cmp dword ptr [ecx+2Ch],0
59 0x75,0x07, // 1000ab65 7507 jne kLdr!kLdrModMap+0x1e (1000ab6e)
60 0xb8,0xc0,0x68,0x06,// 1000ab67 b8c0680600 mov eax,668C0h
61 0x00,
62 0xeb,0x14, // 1000ab6c eb14 jmp kLdr!kLdrModMap+0x32 (1000ab82)
63 0x33,0xd2, // 1000ab6e 33d2 xor edx,edx
64 0x75,0xe1, // 1000ab70 75e1 jne kLdr!kLdrModMap+0x3 (1000ab53)
65 0x8b,0x45,0x08, // 1000ab72 8b4508 mov eax,dword ptr [ebp+8]
66 0x50, // 1000ab75 50 push eax
67 0x8b,0x4d,0x08, // 1000ab76 8b4d08 mov ecx,dword ptr [ebp+8]
68 0x8b,0x51,0x2c, // 1000ab79 8b512c mov edx,dword ptr [ecx+2Ch]
69 0xff,0x52,0x3c, // 1000ab7c ff523c call dword ptr [edx+3Ch]
70 0x83,0xc4,0x04, // 1000ab7f 83c404 add esp,4
71 0x5d, // 1000ab82 5d pop ebp
72 0xc3, // 1000ab83 c3 ret
73 0xcc
74};
75
76
77DECLCALLBACK(int32_t) DisasmTest1ReadCode(RTUINTPTR SrcAddr, uint8_t *pbDst, uint32_t cb, RTUINTPTR uUser)
78{
79 while (cb > 0)
80 {
81 *pbDst = g_ab32BitCode[SrcAddr];
82 /* next */
83 pbDst++;
84 SrcAddr++;
85 cb--;
86 }
87 return 0;
88}
89
90
91/*
92 * Use an inline function here just to test '__textcoal_nt' sections on darwin.
93 */
94inline int MyDisasm(uintptr_t CodeIndex, PDISCPUSTATE pCpu, uint32_t *pcb)
95{
96 uint32_t cb;
97 int rc = DISCoreOneEx(CodeIndex, CPUMODE_32BIT, DisasmTest1ReadCode, 0, pCpu, &cb);
98 *pcb = cb;
99 MY_PRINTF(("DISCoreOneEx -> rc=%d cb=%d Cpu: opcode=%#x pCurInstr=%p (42=%d)\n", \
100 rc, cb, pCpu->opcode, pCpu->pCurInstr, 42)); \
101 return rc;
102}
103
104
105extern "C" DECLEXPORT(int) DisasmTest1(void)
106{
107 DISCPUSTATE Cpu = {0};
108 uintptr_t CodeIndex = 0;
109 uint32_t cb;
110 int rc;
111 MY_PRINTF(("DisasmTest1: %p\n", &DisasmTest1));
112
113#define DISAS_AND_CHECK(cbInstr, enmOp) \
114 do { \
115 rc = MyDisasm(CodeIndex, &Cpu, &cb); \
116 if (RT_FAILURE(rc)) \
117 return CodeIndex | 0xf000; \
118 if (Cpu.pCurInstr->opcode != (enmOp)) \
119 return CodeIndex| 0xe000; \
120 if (cb != (cbInstr)) \
121 return CodeIndex | 0xd000; \
122 CodeIndex += cb; \
123 } while (0)
124
125 DISAS_AND_CHECK(1, OP_PUSH);
126 DISAS_AND_CHECK(2, OP_MOV);
127 DISAS_AND_CHECK(3, OP_MOV);
128 DISAS_AND_CHECK(6, OP_CMP);
129 DISAS_AND_CHECK(2, OP_JNE);
130 DISAS_AND_CHECK(3, OP_MOV);
131 DISAS_AND_CHECK(4, OP_CMP);
132 DISAS_AND_CHECK(2, OP_JNE);
133 DISAS_AND_CHECK(5, OP_MOV);
134 DISAS_AND_CHECK(2, OP_JMP);
135 DISAS_AND_CHECK(2, OP_XOR);
136 DISAS_AND_CHECK(2, OP_JNE);
137 DISAS_AND_CHECK(3, OP_MOV);
138 DISAS_AND_CHECK(1, OP_PUSH);
139 DISAS_AND_CHECK(3, OP_MOV);
140 DISAS_AND_CHECK(3, OP_MOV);
141 DISAS_AND_CHECK(3, OP_CALL);
142 DISAS_AND_CHECK(3, OP_ADD);
143 DISAS_AND_CHECK(1, OP_POP);
144 DISAS_AND_CHECK(1, OP_RETN);
145 DISAS_AND_CHECK(1, OP_INT3);
146
147 return rc;
148}
149
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