VirtualBox

source: vbox/trunk/src/VBox/Disassembler/testcase/tstDisasm-1.cpp@ 74473

Last change on this file since 74473 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1/* $Id: tstDisasm-1.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * VBox disassembler - Test application
4 */
5
6/*
7 * Copyright (C) 2006-2017 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
30DECLASM(int) TestProc32(void);
31DECLASM(int) TestProc32_EndProc(void);
32#ifndef RT_OS_OS2
33DECLASM(int) TestProc64(void);
34DECLASM(int) TestProc64_EndProc(void);
35#endif
36//uint8_t aCode16[] = { 0x66, 0x67, 0x89, 0x07 };
37
38static 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
91static DECLCALLBACK(int) testReadBytes(PDISSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
92{
93 RT_NOREF1(cbMinRead);
94 memcpy(&pDis->abInstr[offInstr], (void *)((uintptr_t)pDis->uInstrAddr + offInstr), cbMaxRead);
95 pDis->cbCachedInstr = offInstr + cbMaxRead;
96 return VINF_SUCCESS;
97}
98
99
100static void testPerformance(const char *pszSub, uint8_t const *pabInstrs, uintptr_t uEndPtr, DISCPUMODE enmDisCpuMode)
101{
102 RTTestISubF("Performance - %s", pszSub);
103
104 size_t const cbInstrs = uEndPtr - (uintptr_t)pabInstrs;
105 uint64_t cInstrs = 0;
106 uint64_t nsStart = RTTimeNanoTS();
107 for (uint32_t i = 0; i < _512K; i++) /* the samples are way to small. :-) */
108 {
109 for (size_t off = 0; off < cbInstrs; cInstrs++)
110 {
111 uint32_t cb = 1;
112 DISSTATE Dis;
113 DISInstrWithReader((uintptr_t)&pabInstrs[off], enmDisCpuMode, testReadBytes, NULL, &Dis, &cb);
114 off += cb;
115 }
116 }
117 uint64_t cNsElapsed = RTTimeNanoTS() - nsStart;
118
119 RTTestIValueF(cNsElapsed, RTTESTUNIT_NS, "%s-Total", pszSub);
120 RTTestIValueF(cNsElapsed / cInstrs, RTTESTUNIT_NS_PER_CALL, "%s-per-instruction", pszSub);
121}
122
123void testTwo(void)
124{
125 static const struct
126 {
127 DISCPUMODE enmMode;
128 uint8_t abInstr[24];
129 uint8_t cbParam1;
130 uint8_t cbParam2;
131 uint8_t cbParam3;
132 } s_gInstrs[] =
133 {
134 { DISCPUMODE_64BIT, { 0x48, 0xc7, 0x03, 0x00, 0x00, 0x00, 0x00, }, 8, 8, 0, },
135 };
136 for (unsigned i = 0; i < RT_ELEMENTS(s_gInstrs); i++)
137 {
138 uint32_t cb = 1;
139 DISSTATE Dis;
140 int rc;
141 RTTESTI_CHECK_RC(rc = DISInstr(s_gInstrs[i].abInstr, s_gInstrs[i].enmMode, &Dis, &cb), VINF_SUCCESS);
142 if (rc == VINF_SUCCESS)
143 {
144 uint32_t cb2;
145 RTTESTI_CHECK_MSG((cb2 = DISGetParamSize(&Dis, &Dis.Param1)) == s_gInstrs[i].cbParam1,
146 ("%u: %#x vs %#x\n", i , cb2, s_gInstrs[i].cbParam1));
147 RTTESTI_CHECK_MSG((cb2 = DISGetParamSize(&Dis, &Dis.Param2)) == s_gInstrs[i].cbParam2,
148 ("%u: %#x vs %#x (%s)\n", i , cb2, s_gInstrs[i].cbParam2, Dis.pCurInstr->pszOpcode));
149 RTTESTI_CHECK_MSG((cb2 = DISGetParamSize(&Dis, &Dis.Param3)) == s_gInstrs[i].cbParam3,
150 ("%u: %#x vs %#x\n", i , cb2, s_gInstrs[i].cbParam3));
151 }
152 }
153}
154
155
156int main(int argc, char **argv)
157{
158 RT_NOREF2(argc, argv);
159 RTTEST hTest;
160 RTEXITCODE rcExit = RTTestInitAndCreate("tstDisasm", &hTest);
161 if (rcExit)
162 return rcExit;
163 RTTestBanner(hTest);
164
165 static const struct
166 {
167 const char *pszDesc;
168 uint8_t const *pbStart;
169 uintptr_t uEndPtr;
170 DISCPUMODE enmCpuMode;
171 } aSnippets[] =
172 {
173 { "32-bit", (uint8_t const *)(uintptr_t)TestProc32, (uintptr_t)&TestProc32_EndProc, DISCPUMODE_32BIT },
174 { "64-bit", (uint8_t const *)(uintptr_t)TestProc64, (uintptr_t)&TestProc64_EndProc, DISCPUMODE_64BIT },
175 };
176
177 for (unsigned i = 0; i < RT_ELEMENTS(aSnippets); i++)
178 testDisas(aSnippets[i].pszDesc, aSnippets[i].pbStart, aSnippets[i].uEndPtr, aSnippets[i].enmCpuMode);
179
180 testTwo();
181
182 if (RTTestIErrorCount() == 0)
183 {
184 for (unsigned i = 0; i < RT_ELEMENTS(aSnippets); i++)
185 testPerformance(aSnippets[i].pszDesc, aSnippets[i].pbStart, aSnippets[i].uEndPtr, aSnippets[i].enmCpuMode);
186 }
187
188 return RTTestSummaryAndDestroy(hTest);
189}
190
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