1 | /* $Id: tstDisasm-1.cpp 62479 2016-07-22 18:29:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox disassembler - Test application
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <VBox/dis.h>
|
---|
23 | #include <iprt/test.h>
|
---|
24 | #include <iprt/ctype.h>
|
---|
25 | #include <iprt/string.h>
|
---|
26 | #include <iprt/err.h>
|
---|
27 | #include <iprt/time.h>
|
---|
28 |
|
---|
29 |
|
---|
30 | DECLASM(int) TestProc32(void);
|
---|
31 | DECLASM(int) TestProc32_EndProc(void);
|
---|
32 | #ifndef RT_OS_OS2
|
---|
33 | DECLASM(int) TestProc64(void);
|
---|
34 | DECLASM(int) TestProc64_EndProc(void);
|
---|
35 | #endif
|
---|
36 | //uint8_t aCode16[] = { 0x66, 0x67, 0x89, 0x07 };
|
---|
37 |
|
---|
38 | static void testDisas(const char *pszSub, uint8_t const *pabInstrs, uintptr_t uEndPtr, DISCPUMODE enmDisCpuMode)
|
---|
39 | {
|
---|
40 | RTTestISub(pszSub);
|
---|
41 | size_t const cbInstrs = uEndPtr - (uintptr_t)pabInstrs;
|
---|
42 | for (size_t off = 0; off < cbInstrs;)
|
---|
43 | {
|
---|
44 | uint32_t const cErrBefore = RTTestIErrorCount();
|
---|
45 | uint32_t cb = 1;
|
---|
46 | DISSTATE Dis;
|
---|
47 | char szOutput[256] = {0};
|
---|
48 | int rc = DISInstrToStr(&pabInstrs[off], enmDisCpuMode, &Dis, &cb, szOutput, sizeof(szOutput));
|
---|
49 |
|
---|
50 | RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
|
---|
51 | RTTESTI_CHECK(cb == Dis.cbInstr);
|
---|
52 | RTTESTI_CHECK(cb > 0);
|
---|
53 | RTTESTI_CHECK(cb <= 16);
|
---|
54 | RTStrStripR(szOutput);
|
---|
55 | RTTESTI_CHECK(szOutput[0]);
|
---|
56 | if (szOutput[0])
|
---|
57 | {
|
---|
58 | char *pszBytes = strchr(szOutput, '[');
|
---|
59 | RTTESTI_CHECK(pszBytes);
|
---|
60 | if (pszBytes)
|
---|
61 | {
|
---|
62 | RTTESTI_CHECK(pszBytes[-1] == ' ');
|
---|
63 | RTTESTI_CHECK(RT_C_IS_XDIGIT(pszBytes[1]));
|
---|
64 | RTTESTI_CHECK(pszBytes[cb * 3] == ']');
|
---|
65 | RTTESTI_CHECK(pszBytes[cb * 3 + 1] == ' ');
|
---|
66 |
|
---|
67 | size_t cch = strlen(szOutput);
|
---|
68 | RTTESTI_CHECK(szOutput[cch - 1] != ',');
|
---|
69 | }
|
---|
70 | }
|
---|
71 | if (cErrBefore != RTTestIErrorCount())
|
---|
72 | RTTestIFailureDetails("rc=%Rrc, off=%#x (%u) cbInstr=%u enmDisCpuMode=%d\n",
|
---|
73 | rc, off, off, Dis.cbInstr, enmDisCpuMode);
|
---|
74 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s\n", szOutput);
|
---|
75 |
|
---|
76 | /* Check with size-only. */
|
---|
77 | uint32_t cbOnly = 1;
|
---|
78 | DISSTATE DisOnly;
|
---|
79 | rc = DISInstrWithPrefetchedBytes((uintptr_t)&pabInstrs[off], enmDisCpuMode, 0 /*fFilter - none */,
|
---|
80 | Dis.abInstr, Dis.cbCachedInstr, NULL, NULL, &DisOnly, &cbOnly);
|
---|
81 |
|
---|
82 | RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
|
---|
83 | RTTESTI_CHECK(cbOnly == DisOnly.cbInstr);
|
---|
84 | RTTESTI_CHECK_MSG(cbOnly == cb, ("%#x vs %#x\n", cbOnly, cb));
|
---|
85 |
|
---|
86 | off += cb;
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | static DECLCALLBACK(int) testReadBytes(PDISSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
|
---|
92 | {
|
---|
93 | memcpy(&pDis->abInstr[offInstr], (void *)((uintptr_t)pDis->uInstrAddr + offInstr), cbMaxRead);
|
---|
94 | pDis->cbCachedInstr = offInstr + cbMaxRead;
|
---|
95 | return VINF_SUCCESS;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | static void testPerformance(const char *pszSub, uint8_t const *pabInstrs, uintptr_t uEndPtr, DISCPUMODE enmDisCpuMode)
|
---|
100 | {
|
---|
101 | RTTestISubF("Performance - %s", pszSub);
|
---|
102 |
|
---|
103 | size_t const cbInstrs = uEndPtr - (uintptr_t)pabInstrs;
|
---|
104 | uint64_t cInstrs = 0;
|
---|
105 | uint64_t nsStart = RTTimeNanoTS();
|
---|
106 | for (uint32_t i = 0; i < _512K; i++) /* the samples are way to small. :-) */
|
---|
107 | {
|
---|
108 | for (size_t off = 0; off < cbInstrs; cInstrs++)
|
---|
109 | {
|
---|
110 | uint32_t cb = 1;
|
---|
111 | DISSTATE Dis;
|
---|
112 | DISInstrWithReader((uintptr_t)&pabInstrs[off], enmDisCpuMode, testReadBytes, NULL, &Dis, &cb);
|
---|
113 | off += cb;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | uint64_t cNsElapsed = RTTimeNanoTS() - nsStart;
|
---|
117 |
|
---|
118 | RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "%s-Total", pszSub);
|
---|
119 | RTTestIValueF(cNsElapsed / cInstrs, RTTESTUNIT_NS_PER_CALL, "%s-per-instruction", pszSub);
|
---|
120 | }
|
---|
121 |
|
---|
122 | void testTwo(void)
|
---|
123 | {
|
---|
124 | static const struct
|
---|
125 | {
|
---|
126 | DISCPUMODE enmMode;
|
---|
127 | uint8_t abInstr[24];
|
---|
128 | uint8_t cbParam1;
|
---|
129 | uint8_t cbParam2;
|
---|
130 | uint8_t cbParam3;
|
---|
131 | } s_gInstrs[] =
|
---|
132 | {
|
---|
133 | { DISCPUMODE_64BIT, { 0x48, 0xc7, 0x03, 0x00, 0x00, 0x00, 0x00, }, 8, 8, 0, },
|
---|
134 | };
|
---|
135 | for (unsigned i = 0; i < RT_ELEMENTS(s_gInstrs); i++)
|
---|
136 | {
|
---|
137 | uint32_t cb = 1;
|
---|
138 | DISSTATE Dis;
|
---|
139 | int rc;
|
---|
140 | RTTESTI_CHECK_RC(rc = DISInstr(s_gInstrs[i].abInstr, s_gInstrs[i].enmMode, &Dis, &cb), VINF_SUCCESS);
|
---|
141 | if (rc == VINF_SUCCESS)
|
---|
142 | {
|
---|
143 | uint32_t cb2;
|
---|
144 | RTTESTI_CHECK_MSG((cb2 = DISGetParamSize(&Dis, &Dis.Param1)) == s_gInstrs[i].cbParam1,
|
---|
145 | ("%u: %#x vs %#x\n", i , cb2, s_gInstrs[i].cbParam1));
|
---|
146 | RTTESTI_CHECK_MSG((cb2 = DISGetParamSize(&Dis, &Dis.Param2)) == s_gInstrs[i].cbParam2,
|
---|
147 | ("%u: %#x vs %#x (%s)\n", i , cb2, s_gInstrs[i].cbParam2, Dis.pCurInstr->pszOpcode));
|
---|
148 | RTTESTI_CHECK_MSG((cb2 = DISGetParamSize(&Dis, &Dis.Param3)) == s_gInstrs[i].cbParam3,
|
---|
149 | ("%u: %#x vs %#x\n", i , cb2, s_gInstrs[i].cbParam3));
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | int main(int argc, char **argv)
|
---|
156 | {
|
---|
157 | RTTEST hTest;
|
---|
158 | RTEXITCODE rcExit = RTTestInitAndCreate("tstDisasm", &hTest);
|
---|
159 | if (rcExit)
|
---|
160 | return rcExit;
|
---|
161 | RTTestBanner(hTest);
|
---|
162 |
|
---|
163 | static const struct
|
---|
164 | {
|
---|
165 | const char *pszDesc;
|
---|
166 | uint8_t const *pbStart;
|
---|
167 | uintptr_t uEndPtr;
|
---|
168 | DISCPUMODE enmCpuMode;
|
---|
169 | } aSnippets[] =
|
---|
170 | {
|
---|
171 | { "32-bit", (uint8_t const *)(uintptr_t)TestProc32, (uintptr_t)&TestProc32_EndProc, DISCPUMODE_32BIT },
|
---|
172 | { "64-bit", (uint8_t const *)(uintptr_t)TestProc64, (uintptr_t)&TestProc64_EndProc, DISCPUMODE_64BIT },
|
---|
173 | };
|
---|
174 |
|
---|
175 | for (unsigned i = 0; i < RT_ELEMENTS(aSnippets); i++)
|
---|
176 | testDisas(aSnippets[i].pszDesc, aSnippets[i].pbStart, aSnippets[i].uEndPtr, aSnippets[i].enmCpuMode);
|
---|
177 |
|
---|
178 | testTwo();
|
---|
179 |
|
---|
180 | if (RTTestIErrorCount() == 0)
|
---|
181 | {
|
---|
182 | for (unsigned i = 0; i < RT_ELEMENTS(aSnippets); i++)
|
---|
183 | testPerformance(aSnippets[i].pszDesc, aSnippets[i].pbStart, aSnippets[i].uEndPtr, aSnippets[i].enmCpuMode);
|
---|
184 | }
|
---|
185 |
|
---|
186 | return RTTestSummaryAndDestroy(hTest);
|
---|
187 | }
|
---|
188 |
|
---|