1 | /* $Id: tstLdrDisasmTest.cpp 93115 2022-01-01 11:31:46Z 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 Corporation
|
---|
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 |
|
---|
32 |
|
---|
33 |
|
---|
34 | /*********************************************************************************************************************************
|
---|
35 | * Header Files *
|
---|
36 | *********************************************************************************************************************************/
|
---|
37 | #include <VBox/dis.h>
|
---|
38 | #include <VBox/disopcode.h>
|
---|
39 | #include <VBox/sup.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 |
|
---|
42 | #if defined(IN_RING0)
|
---|
43 | # define MY_PRINTF(a) SUPR0Printf a
|
---|
44 | #else
|
---|
45 | # define MY_PRINTF(a) do {} while (0)
|
---|
46 | #endif
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Global Variables *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 |
|
---|
53 | /* 32-bit code */
|
---|
54 | static const uint8_t g_ab32BitCode[] =
|
---|
55 | {
|
---|
56 | 0x55, // 1000ab50 55 push ebp
|
---|
57 | 0x8b,0xec, // 1000ab51 8bec mov ebp,esp
|
---|
58 | 0x8b,0x45,0x08, // 1000ab53 8b4508 mov eax,dword ptr [ebp+8]
|
---|
59 | 0x81,0x38,0x07,0x07,// 1000ab56 813807076419 cmp dword ptr [eax],19640707h
|
---|
60 | 0x64,0x19,
|
---|
61 | 0x75,0x09, // 1000ab5c 7509 jne kLdr!kLdrModMap+0x17 (1000ab67)
|
---|
62 | 0x8b,0x4d,0x08, // 1000ab5e 8b4d08 mov ecx,dword ptr [ebp+8]
|
---|
63 | 0x83,0x79,0x2c,0x00,// 1000ab61 83792c00 cmp dword ptr [ecx+2Ch],0
|
---|
64 | 0x75,0x07, // 1000ab65 7507 jne kLdr!kLdrModMap+0x1e (1000ab6e)
|
---|
65 | 0xb8,0xc0,0x68,0x06,// 1000ab67 b8c0680600 mov eax,668C0h
|
---|
66 | 0x00,
|
---|
67 | 0xeb,0x14, // 1000ab6c eb14 jmp kLdr!kLdrModMap+0x32 (1000ab82)
|
---|
68 | 0x33,0xd2, // 1000ab6e 33d2 xor edx,edx
|
---|
69 | 0x75,0xe1, // 1000ab70 75e1 jne kLdr!kLdrModMap+0x3 (1000ab53)
|
---|
70 | 0x8b,0x45,0x08, // 1000ab72 8b4508 mov eax,dword ptr [ebp+8]
|
---|
71 | 0x50, // 1000ab75 50 push eax
|
---|
72 | 0x8b,0x4d,0x08, // 1000ab76 8b4d08 mov ecx,dword ptr [ebp+8]
|
---|
73 | 0x8b,0x51,0x2c, // 1000ab79 8b512c mov edx,dword ptr [ecx+2Ch]
|
---|
74 | 0xff,0x52,0x3c, // 1000ab7c ff523c call dword ptr [edx+3Ch]
|
---|
75 | 0x83,0xc4,0x04, // 1000ab7f 83c404 add esp,4
|
---|
76 | 0x5d, // 1000ab82 5d pop ebp
|
---|
77 | 0xc3, // 1000ab83 c3 ret
|
---|
78 | 0xcc
|
---|
79 | };
|
---|
80 |
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * @callback_method_impl{FNDISREADBYTES}
|
---|
84 | */
|
---|
85 | static DECLCALLBACK(int) DisasmTest1ReadCode(PDISCPUSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
|
---|
86 | {
|
---|
87 | size_t cb = cbMaxRead;
|
---|
88 | if (cb + pDis->uInstrAddr + offInstr > sizeof(g_ab32BitCode))
|
---|
89 | cb = cbMinRead;
|
---|
90 | memcpy(&pDis->abInstr[offInstr], &g_ab32BitCode[pDis->uInstrAddr + offInstr], cb);
|
---|
91 | pDis->cbCachedInstr = offInstr + (uint8_t)cb;
|
---|
92 | return VINF_SUCCESS;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Use an inline function here just to test '__textcoal_nt' sections on darwin.
|
---|
98 | */
|
---|
99 | inline int MyDisasm(uintptr_t CodeIndex, PDISCPUSTATE pCpu, uint32_t *pcb)
|
---|
100 | {
|
---|
101 | uint32_t cb;
|
---|
102 | int rc = DISInstrWithReader(CodeIndex, DISCPUMODE_32BIT, DisasmTest1ReadCode, 0, pCpu, &cb);
|
---|
103 | *pcb = cb;
|
---|
104 | MY_PRINTF(("DISCoreOneEx -> rc=%d cb=%d Cpu: bOpCode=%#x pCurInstr=%p (42=%d)\n", \
|
---|
105 | rc, cb, pCpu->bOpCode, pCpu->pCurInstr, 42)); \
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | extern "C" DECLEXPORT(int) DisasmTest1(void)
|
---|
111 | {
|
---|
112 | DISCPUSTATE Cpu;
|
---|
113 | uintptr_t CodeIndex = 0;
|
---|
114 | uint32_t cb;
|
---|
115 | int rc;
|
---|
116 | MY_PRINTF(("DisasmTest1: %p\n", &DisasmTest1));
|
---|
117 |
|
---|
118 | #if defined(IN_RING0)
|
---|
119 | MY_PRINTF(("GIP: g_pSUPGlobalInfoPage=%p\n", g_pSUPGlobalInfoPage));
|
---|
120 | MY_PRINTF(("GIP: magic=%#x version=%#x mode=%d cCpus=%d\n", g_pSUPGlobalInfoPage->u32Magic, g_pSUPGlobalInfoPage->u32Version,
|
---|
121 | g_pSUPGlobalInfoPage->u32Mode, g_pSUPGlobalInfoPage->cCpus));
|
---|
122 | if (g_pSUPGlobalInfoPage->u32Magic != SUPGLOBALINFOPAGE_MAGIC)
|
---|
123 | return 0xc001;
|
---|
124 | if (g_pSUPGlobalInfoPage->u32Version != SUPGLOBALINFOPAGE_VERSION)
|
---|
125 | return 0xc002;
|
---|
126 | if (g_pSUPGlobalInfoPage->u32Mode != SUPGIPMODE_INVARIANT_TSC)
|
---|
127 | return 0xc003;
|
---|
128 | if (g_pSUPGlobalInfoPage->cCpus != 42)
|
---|
129 | return 0xc004;
|
---|
130 | #endif
|
---|
131 |
|
---|
132 | memset(&Cpu, 0, sizeof(Cpu));
|
---|
133 |
|
---|
134 | #define DISAS_AND_CHECK(cbInstr, enmOp) \
|
---|
135 | do { \
|
---|
136 | rc = MyDisasm(CodeIndex, &Cpu, &cb); \
|
---|
137 | if (RT_FAILURE(rc)) \
|
---|
138 | return CodeIndex | 0xf000; \
|
---|
139 | if (Cpu.pCurInstr->uOpcode != (enmOp)) \
|
---|
140 | return CodeIndex| 0xe000; \
|
---|
141 | if (cb != (cbInstr)) \
|
---|
142 | return CodeIndex | 0xd000; \
|
---|
143 | CodeIndex += cb; \
|
---|
144 | } while (0)
|
---|
145 |
|
---|
146 | DISAS_AND_CHECK(1, OP_PUSH);
|
---|
147 | DISAS_AND_CHECK(2, OP_MOV);
|
---|
148 | DISAS_AND_CHECK(3, OP_MOV);
|
---|
149 | DISAS_AND_CHECK(6, OP_CMP);
|
---|
150 | DISAS_AND_CHECK(2, OP_JNE);
|
---|
151 | DISAS_AND_CHECK(3, OP_MOV);
|
---|
152 | DISAS_AND_CHECK(4, OP_CMP);
|
---|
153 | DISAS_AND_CHECK(2, OP_JNE);
|
---|
154 | DISAS_AND_CHECK(5, OP_MOV);
|
---|
155 | DISAS_AND_CHECK(2, OP_JMP);
|
---|
156 | DISAS_AND_CHECK(2, OP_XOR);
|
---|
157 | DISAS_AND_CHECK(2, OP_JNE);
|
---|
158 | DISAS_AND_CHECK(3, OP_MOV);
|
---|
159 | DISAS_AND_CHECK(1, OP_PUSH);
|
---|
160 | DISAS_AND_CHECK(3, OP_MOV);
|
---|
161 | DISAS_AND_CHECK(3, OP_MOV);
|
---|
162 | DISAS_AND_CHECK(3, OP_CALL);
|
---|
163 | DISAS_AND_CHECK(3, OP_ADD);
|
---|
164 | DISAS_AND_CHECK(1, OP_POP);
|
---|
165 | DISAS_AND_CHECK(1, OP_RETN);
|
---|
166 | DISAS_AND_CHECK(1, OP_INT3);
|
---|
167 |
|
---|
168 | return rc;
|
---|
169 | }
|
---|
170 |
|
---|