1 | /* $Id: tstLdrDisasmTest.cpp 96407 2022-08-22 17:43:14Z 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-2022 Oracle and/or its affiliates.
|
---|
13 | *
|
---|
14 | * This file is part of VirtualBox base platform packages, as
|
---|
15 | * available from https://www.virtualbox.org.
|
---|
16 | *
|
---|
17 | * This program is free software; you can redistribute it and/or
|
---|
18 | * modify it under the terms of the GNU General Public License
|
---|
19 | * as published by the Free Software Foundation, in version 3 of the
|
---|
20 | * License.
|
---|
21 | *
|
---|
22 | * This program is distributed in the hope that it will be useful, but
|
---|
23 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
25 | * General Public License for more details.
|
---|
26 | *
|
---|
27 | * You should have received a copy of the GNU General Public License
|
---|
28 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
29 | *
|
---|
30 | * The contents of this file may alternatively be used under the terms
|
---|
31 | * of the Common Development and Distribution License Version 1.0
|
---|
32 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
33 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
34 | * CDDL are applicable instead of those of the GPL.
|
---|
35 | *
|
---|
36 | * You may elect to license modified versions of this file under the
|
---|
37 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
38 | *
|
---|
39 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
40 | */
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 | /*********************************************************************************************************************************
|
---|
45 | * Header Files *
|
---|
46 | *********************************************************************************************************************************/
|
---|
47 | #include <VBox/dis.h>
|
---|
48 | #include <VBox/disopcode.h>
|
---|
49 | #include <VBox/sup.h>
|
---|
50 | #include <iprt/string.h>
|
---|
51 |
|
---|
52 | #if defined(IN_RING0)
|
---|
53 | # define MY_PRINTF(a) SUPR0Printf a
|
---|
54 | #else
|
---|
55 | # define MY_PRINTF(a) do {} while (0)
|
---|
56 | #endif
|
---|
57 |
|
---|
58 |
|
---|
59 | /*********************************************************************************************************************************
|
---|
60 | * Global Variables *
|
---|
61 | *********************************************************************************************************************************/
|
---|
62 |
|
---|
63 | /* 32-bit code */
|
---|
64 | static const uint8_t g_ab32BitCode[] =
|
---|
65 | {
|
---|
66 | 0x55, // 1000ab50 55 push ebp
|
---|
67 | 0x8b,0xec, // 1000ab51 8bec mov ebp,esp
|
---|
68 | 0x8b,0x45,0x08, // 1000ab53 8b4508 mov eax,dword ptr [ebp+8]
|
---|
69 | 0x81,0x38,0x07,0x07,// 1000ab56 813807076419 cmp dword ptr [eax],19640707h
|
---|
70 | 0x64,0x19,
|
---|
71 | 0x75,0x09, // 1000ab5c 7509 jne kLdr!kLdrModMap+0x17 (1000ab67)
|
---|
72 | 0x8b,0x4d,0x08, // 1000ab5e 8b4d08 mov ecx,dword ptr [ebp+8]
|
---|
73 | 0x83,0x79,0x2c,0x00,// 1000ab61 83792c00 cmp dword ptr [ecx+2Ch],0
|
---|
74 | 0x75,0x07, // 1000ab65 7507 jne kLdr!kLdrModMap+0x1e (1000ab6e)
|
---|
75 | 0xb8,0xc0,0x68,0x06,// 1000ab67 b8c0680600 mov eax,668C0h
|
---|
76 | 0x00,
|
---|
77 | 0xeb,0x14, // 1000ab6c eb14 jmp kLdr!kLdrModMap+0x32 (1000ab82)
|
---|
78 | 0x33,0xd2, // 1000ab6e 33d2 xor edx,edx
|
---|
79 | 0x75,0xe1, // 1000ab70 75e1 jne kLdr!kLdrModMap+0x3 (1000ab53)
|
---|
80 | 0x8b,0x45,0x08, // 1000ab72 8b4508 mov eax,dword ptr [ebp+8]
|
---|
81 | 0x50, // 1000ab75 50 push eax
|
---|
82 | 0x8b,0x4d,0x08, // 1000ab76 8b4d08 mov ecx,dword ptr [ebp+8]
|
---|
83 | 0x8b,0x51,0x2c, // 1000ab79 8b512c mov edx,dword ptr [ecx+2Ch]
|
---|
84 | 0xff,0x52,0x3c, // 1000ab7c ff523c call dword ptr [edx+3Ch]
|
---|
85 | 0x83,0xc4,0x04, // 1000ab7f 83c404 add esp,4
|
---|
86 | 0x5d, // 1000ab82 5d pop ebp
|
---|
87 | 0xc3, // 1000ab83 c3 ret
|
---|
88 | 0xcc
|
---|
89 | };
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * @callback_method_impl{FNDISREADBYTES}
|
---|
94 | */
|
---|
95 | static DECLCALLBACK(int) DisasmTest1ReadCode(PDISCPUSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
|
---|
96 | {
|
---|
97 | size_t cb = cbMaxRead;
|
---|
98 | if (cb + pDis->uInstrAddr + offInstr > sizeof(g_ab32BitCode))
|
---|
99 | cb = cbMinRead;
|
---|
100 | memcpy(&pDis->abInstr[offInstr], &g_ab32BitCode[pDis->uInstrAddr + offInstr], cb);
|
---|
101 | pDis->cbCachedInstr = offInstr + (uint8_t)cb;
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Use an inline function here just to test '__textcoal_nt' sections on darwin.
|
---|
108 | */
|
---|
109 | inline int MyDisasm(uintptr_t CodeIndex, PDISCPUSTATE pCpu, uint32_t *pcb)
|
---|
110 | {
|
---|
111 | uint32_t cb;
|
---|
112 | int rc = DISInstrWithReader(CodeIndex, DISCPUMODE_32BIT, DisasmTest1ReadCode, 0, pCpu, &cb);
|
---|
113 | *pcb = cb;
|
---|
114 | MY_PRINTF(("DISCoreOneEx -> rc=%d cb=%d Cpu: bOpCode=%#x pCurInstr=%p (42=%d)\n", \
|
---|
115 | rc, cb, pCpu->bOpCode, pCpu->pCurInstr, 42)); \
|
---|
116 | return rc;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | extern "C" DECLEXPORT(int) DisasmTest1(void)
|
---|
121 | {
|
---|
122 | DISCPUSTATE Cpu;
|
---|
123 | uintptr_t CodeIndex = 0;
|
---|
124 | uint32_t cb;
|
---|
125 | int rc;
|
---|
126 | MY_PRINTF(("DisasmTest1: %p\n", &DisasmTest1));
|
---|
127 |
|
---|
128 | #if defined(IN_RING0)
|
---|
129 | MY_PRINTF(("GIP: g_pSUPGlobalInfoPage=%p\n", g_pSUPGlobalInfoPage));
|
---|
130 | MY_PRINTF(("GIP: magic=%#x version=%#x mode=%d cCpus=%d\n", g_pSUPGlobalInfoPage->u32Magic, g_pSUPGlobalInfoPage->u32Version,
|
---|
131 | g_pSUPGlobalInfoPage->u32Mode, g_pSUPGlobalInfoPage->cCpus));
|
---|
132 | if (g_pSUPGlobalInfoPage->u32Magic != SUPGLOBALINFOPAGE_MAGIC)
|
---|
133 | return 0xc001;
|
---|
134 | if (g_pSUPGlobalInfoPage->u32Version != SUPGLOBALINFOPAGE_VERSION)
|
---|
135 | return 0xc002;
|
---|
136 | if (g_pSUPGlobalInfoPage->u32Mode != SUPGIPMODE_INVARIANT_TSC)
|
---|
137 | return 0xc003;
|
---|
138 | if (g_pSUPGlobalInfoPage->cCpus != 42)
|
---|
139 | return 0xc004;
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | memset(&Cpu, 0, sizeof(Cpu));
|
---|
143 |
|
---|
144 | #define DISAS_AND_CHECK(cbInstr, enmOp) \
|
---|
145 | do { \
|
---|
146 | rc = MyDisasm(CodeIndex, &Cpu, &cb); \
|
---|
147 | if (RT_FAILURE(rc)) \
|
---|
148 | return CodeIndex | 0xf000; \
|
---|
149 | if (Cpu.pCurInstr->uOpcode != (enmOp)) \
|
---|
150 | return CodeIndex| 0xe000; \
|
---|
151 | if (cb != (cbInstr)) \
|
---|
152 | return CodeIndex | 0xd000; \
|
---|
153 | CodeIndex += cb; \
|
---|
154 | } while (0)
|
---|
155 |
|
---|
156 | DISAS_AND_CHECK(1, OP_PUSH);
|
---|
157 | DISAS_AND_CHECK(2, OP_MOV);
|
---|
158 | DISAS_AND_CHECK(3, OP_MOV);
|
---|
159 | DISAS_AND_CHECK(6, OP_CMP);
|
---|
160 | DISAS_AND_CHECK(2, OP_JNE);
|
---|
161 | DISAS_AND_CHECK(3, OP_MOV);
|
---|
162 | DISAS_AND_CHECK(4, OP_CMP);
|
---|
163 | DISAS_AND_CHECK(2, OP_JNE);
|
---|
164 | DISAS_AND_CHECK(5, OP_MOV);
|
---|
165 | DISAS_AND_CHECK(2, OP_JMP);
|
---|
166 | DISAS_AND_CHECK(2, OP_XOR);
|
---|
167 | DISAS_AND_CHECK(2, OP_JNE);
|
---|
168 | DISAS_AND_CHECK(3, OP_MOV);
|
---|
169 | DISAS_AND_CHECK(1, OP_PUSH);
|
---|
170 | DISAS_AND_CHECK(3, OP_MOV);
|
---|
171 | DISAS_AND_CHECK(3, OP_MOV);
|
---|
172 | DISAS_AND_CHECK(3, OP_CALL);
|
---|
173 | DISAS_AND_CHECK(3, OP_ADD);
|
---|
174 | DISAS_AND_CHECK(1, OP_POP);
|
---|
175 | DISAS_AND_CHECK(1, OP_RETN);
|
---|
176 | DISAS_AND_CHECK(1, OP_INT3);
|
---|
177 |
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 |
|
---|