summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/testcase/tstLdr-3.cpp
blob: 9098360593c2e964f16a543fdac3a3e95673aab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* $Id: tstLdr-3.cpp $ */
/** @file
 * IPRT - Testcase for parts of RTLdr*, manual inspection.
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/ldr.h>
#include <iprt/alloc.h>
#include <iprt/stream.h>
#include <iprt/assert.h>
#include <iprt/initterm.h>
#include <iprt/err.h>
#include <iprt/string.h>
#include <VBox/dis.h>


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
static RTUINTPTR g_uLoadAddr;
static RTLDRMOD  g_hLdrMod;
static void     *g_pvBits;
static uint8_t   g_cBits;
static uint8_t   g_fNearImports;

/**
 * Current nearest symbol.
 */
typedef struct TESTNEARSYM
{
    RTUINTPTR   Addr;
    struct TESTSYM
    {
        RTUINTPTR   Value;
        unsigned    uSymbol;
        char        szName[512];
    } aSyms[2];
} TESTNEARSYM, *PTESTNEARSYM;

/**
 * Enumeration callback function used by RTLdrEnumSymbols().
 *
 * @returns iprt status code. Failure will stop the enumeration.
 * @param   hLdrMod         The loader module handle.
 * @param   pszSymbol       Symbol name. NULL if ordinal only.
 * @param   uSymbol         Symbol ordinal, ~0 if not used.
 * @param   Value           Symbol value.
 * @param   pvUser          The user argument specified to RTLdrEnumSymbols().
 */
static DECLCALLBACK(int) testEnumSymbol2(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
{
    RT_NOREF1(hLdrMod);
    PTESTNEARSYM pSym = (PTESTNEARSYM)pvUser;

    /* less or equal */
    if (    Value <= pSym->Addr
        &&  (   Value > pSym->aSyms[0].Value
             || (   Value == pSym->aSyms[0].Value
                 && !pSym->aSyms[0].szName[0]
                 && pszSymbol
                 && *pszSymbol
                )
            )
       )
    {
        pSym->aSyms[0].Value     = Value;
        pSym->aSyms[0].uSymbol   = uSymbol;
        pSym->aSyms[0].szName[0] = '\0';
        if (pszSymbol)
            strncat(pSym->aSyms[0].szName, pszSymbol, sizeof(pSym->aSyms[0].szName)-1);
    }

    /* above */
    if (    Value > pSym->Addr
        &&  (   Value < pSym->aSyms[1].Value
             || (   Value == pSym->aSyms[1].Value
                 && !pSym->aSyms[1].szName[1]
                 && pszSymbol
                 && *pszSymbol
                )
            )
       )
    {
        pSym->aSyms[1].Value     = Value;
        pSym->aSyms[1].uSymbol   = uSymbol;
        pSym->aSyms[1].szName[0] = '\0';
        if (pszSymbol)
            strncat(pSym->aSyms[1].szName, pszSymbol, sizeof(pSym->aSyms[1].szName)-1);
    }

    return VINF_SUCCESS;
}

static int FindNearSymbol(RTUINTPTR uAddr, PTESTNEARSYM pNearSym)
{
    RT_ZERO(*pNearSym);
    pNearSym->Addr = (RTUINTPTR)uAddr;
    pNearSym->aSyms[1].Value = ~(RTUINTPTR)0;
    int rc = RTLdrEnumSymbols(g_hLdrMod, RTLDR_ENUM_SYMBOL_FLAGS_ALL, g_pvBits, g_uLoadAddr, testEnumSymbol2, pNearSym);
    if (RT_FAILURE(rc))
        RTPrintf("tstLdr-3: Failed to enumerate symbols: %Rra\n", rc);
    return rc;
}

static DECLCALLBACK(int) MyGetSymbol(PCDISCPUSTATE pCpu, uint32_t u32Sel, RTUINTPTR uAddress,
                                     char *pszBuf, size_t cchBuf, RTINTPTR *poff,
                                     void *pvUser)
{
    RT_NOREF3(pCpu, u32Sel, pvUser);

    if (   uAddress > RTLdrSize(g_hLdrMod) + g_uLoadAddr
        || uAddress < g_uLoadAddr)
        return VERR_SYMBOL_NOT_FOUND;

    TESTNEARSYM NearSym;
    int rc = FindNearSymbol(uAddress, &NearSym);
    if (RT_FAILURE(rc))
        return rc;

    RTStrCopy(pszBuf, cchBuf, NearSym.aSyms[0].szName);
    *poff = uAddress - NearSym.aSyms[0].Value;
    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNDISREADBYTES}
 */
static DECLCALLBACK(int) MyReadBytes(PDISCPUSTATE pDis, uint8_t offInstr, uint8_t cbMinRead, uint8_t cbMaxRead)
{
    RT_NOREF1(cbMaxRead);
    uint8_t const *pbSrc = (uint8_t const *)((uintptr_t)pDis->uInstrAddr + (uintptr_t)pDis->pvUser + offInstr);
    memcpy(&pDis->abInstr[offInstr], pbSrc, cbMinRead);
    pDis->cbCachedInstr = offInstr + cbMinRead;
    return VINF_SUCCESS;
}


static bool MyDisBlock(DISCPUMODE enmCpuMode, RTHCUINTPTR pvCodeBlock, int32_t cbMax, RTUINTPTR off,
                       RTUINTPTR uNearAddr, RTUINTPTR uSearchAddr)
{
    DISCPUSTATE Cpu;
    int32_t     i = 0;
    while (i < cbMax)
    {
        bool        fQuiet    = RTAssertSetQuiet(true);
        bool        fMayPanic = RTAssertSetMayPanic(false);
        char        szOutput[256];
        unsigned    cbInstr;
        int rc = DISInstrWithReader(uNearAddr + i, enmCpuMode,
                                    MyReadBytes, (uint8_t *)pvCodeBlock - (uintptr_t)uNearAddr,
                                    &Cpu, &cbInstr);
        RTAssertSetMayPanic(fMayPanic);
        RTAssertSetQuiet(fQuiet);
        if (RT_FAILURE(rc))
            return false;

        TESTNEARSYM NearSym;
        rc = FindNearSymbol(uNearAddr + i, &NearSym);
        if (RT_SUCCESS(rc) && NearSym.aSyms[0].Value == NearSym.Addr)
            RTPrintf("%s:\n", NearSym.aSyms[0].szName);

        DISFormatYasmEx(&Cpu, szOutput, sizeof(szOutput),
                        DIS_FMT_FLAGS_RELATIVE_BRANCH | DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_ADDR_LEFT  | DIS_FMT_FLAGS_BYTES_SPACED,
                        MyGetSymbol, NULL);

        RTPrintf("%s\n", szOutput);
        if (pvCodeBlock + i + off == uSearchAddr)
            RTPrintf("^^^^^^^^\n");

        /* next */
        i += cbInstr;
    }
    return true;
}



/**
 * Resolve an external symbol during RTLdrGetBits().
 *
 * @returns iprt status code.
 * @param   hLdrMod         The loader module handle.
 * @param   pszModule       Module name.
 * @param   pszSymbol       Symbol name, NULL if uSymbol should be used.
 * @param   uSymbol         Symbol ordinal, ~0 if pszSymbol should be used.
 * @param   pValue          Where to store the symbol value (address).
 * @param   pvUser          User argument.
 */
static DECLCALLBACK(int) testGetImport(RTLDRMOD hLdrMod, const char *pszModule, const char *pszSymbol,
                                       unsigned uSymbol, RTUINTPTR *pValue, void *pvUser)
{
    RT_NOREF5(hLdrMod, pszModule, pszSymbol, uSymbol, pvUser);
    RTUINTPTR BaseAddr = *(PCRTUINTPTR)pvUser;
    if (g_fNearImports)
        *pValue = BaseAddr + UINT32_C(0x604020f0);
    else if (   BaseAddr < UINT64_C(0xffffff7f820df000) - _4G
             || BaseAddr > UINT64_C(0xffffff7f820df000) + _4G)
        *pValue = UINT64_C(0xffffff7f820df000);
    else
        *pValue = UINT64_C(0xffffff7c820df000);
    if (g_cBits == 32)
        *pValue &= UINT32_MAX;
    return VINF_SUCCESS;
}

static uint32_t g_iSegNo = 0;
static DECLCALLBACK(int) testEnumSegment1(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
{
    if (hLdrMod != g_hLdrMod || pvUser != NULL)
        return VERR_INTERNAL_ERROR_3;
    RTPrintf("Seg#%02u: %RTptr LB %RTptr %s\n"
             "   link=%RTptr LB %RTptr align=%RTptr fProt=%#x offFile=%RTfoff\n"
             , g_iSegNo++, pSeg->RVA, pSeg->cbMapped, pSeg->pszName,
             pSeg->LinkAddress, pSeg->cb, pSeg->Alignment, pSeg->fProt, pSeg->offFile);

    return VINF_SUCCESS;
}


/**
 * Enumeration callback function used by RTLdrEnumSymbols().
 *
 * @returns iprt status code. Failure will stop the enumeration.
 * @param   hLdrMod         The loader module handle.
 * @param   pszSymbol       Symbol name. NULL if ordinal only.
 * @param   uSymbol         Symbol ordinal, ~0 if not used.
 * @param   Value           Symbol value.
 * @param   pvUser          The user argument specified to RTLdrEnumSymbols().
 */
static DECLCALLBACK(int) testEnumSymbol1(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol, RTUINTPTR Value, void *pvUser)
{
    if (hLdrMod != g_hLdrMod || pvUser != NULL)
        return VERR_INTERNAL_ERROR_3;
    RTPrintf("  %RTptr %s (%d)\n", Value, pszSymbol, uSymbol);
    return VINF_SUCCESS;
}


static int testDisasNear(uint64_t uAddr)
{
    TESTNEARSYM NearSym;
    int rc = FindNearSymbol(uAddr, &NearSym);
    if (RT_FAILURE(rc))
        return rc;

    RTPrintf("tstLdr-3: Addr=%RTptr\n"
             "%RTptr %s (%d) - %RTptr %s (%d)\n",
             NearSym.Addr,
             NearSym.aSyms[0].Value, NearSym.aSyms[0].szName, NearSym.aSyms[0].uSymbol,
             NearSym.aSyms[1].Value, NearSym.aSyms[1].szName, NearSym.aSyms[1].uSymbol);
    if (NearSym.Addr - NearSym.aSyms[0].Value < 0x10000)
    {
        DISCPUMODE enmDisCpuMode = g_cBits == 32 ? DISCPUMODE_32BIT : DISCPUMODE_64BIT;
        uint8_t *pbCode = (uint8_t *)g_pvBits + (NearSym.aSyms[0].Value - g_uLoadAddr);
        MyDisBlock(enmDisCpuMode, (uintptr_t)pbCode,
                   RT_MAX(NearSym.aSyms[1].Value - NearSym.aSyms[0].Value, 0x20000),
                   NearSym.aSyms[0].Value - (uintptr_t)pbCode,
                   NearSym.aSyms[0].Value,
                   NearSym.Addr);
    }

    return VINF_SUCCESS;
}

int main(int argc, char **argv)
{
    RTR3InitExe(argc, &argv, 0);

    /*
     * Module & code bitness (optional).
     */
    g_cBits = ARCH_BITS;
#if !defined(RT_OS_WINDOWS) || defined(RT_OS_DARWIN)
    g_fNearImports = false;
#else
    g_fNearImports = true;
#endif
    while (argc > 1)
    {
        if (!strcmp(argv[1], "--32"))
            g_cBits = 32;
        else if (!strcmp(argv[1], "--64"))
            g_cBits = 64;
        else if (!strcmp(argv[1], "--near-imports"))
            g_fNearImports = true;
        else if (!strcmp(argv[1], "--wide-imports"))
            g_fNearImports = false;
        else
            break;
        argc--;
        argv++;
    }

    int rcRet = 0;
    if (argc <= 2)
    {
        RTPrintf("usage: %s [--32|--64] [--<near|wide>-imports] <load-addr> <module> [addr1 []]\n", argv[0]);
        return 1;
    }

    /*
     * Load the module.
     */
    RTERRINFOSTATIC ErrInfo;
    g_uLoadAddr = (RTUINTPTR)RTStrToUInt64(argv[1]);
    int rc = RTLdrOpenEx(argv[2], 0, RTLDRARCH_WHATEVER, &g_hLdrMod, RTErrInfoInitStatic(&ErrInfo));
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstLdr-3: Failed to open '%s': %Rra\n", argv[2], rc);
        if (ErrInfo.szMsg[0])
            RTPrintf("tstLdr-3: %s\n", ErrInfo.szMsg);
        return 1;
    }

    g_pvBits = RTMemAlloc(RTLdrSize(g_hLdrMod));
    rc = RTLdrGetBits(g_hLdrMod, g_pvBits, g_uLoadAddr, testGetImport, &g_uLoadAddr);
    if (RT_SUCCESS(rc))
    {
        if (   argc == 4
            && argv[3][0] == '*')
        {
            /*
             * Wildcard address mode.
             */
            uint64_t uWild       = RTStrToUInt64(&argv[3][1]);
            uint64_t uIncrements = strchr(argv[3], '/') ? RTStrToUInt64(strchr(argv[3], '/') + 1) : 0x1000;
            if (!uIncrements)
                uIncrements = 0x1000;
            uint64_t uMax        = RTLdrSize(g_hLdrMod) + g_uLoadAddr;
            for (uint64_t uCur = g_uLoadAddr + uWild; uCur < uMax; uCur += uIncrements)
                testDisasNear(uCur);
        }
        else if (argc > 3)
        {
            /*
             * User specified addresses within the module.
             */
            for (int i = 3; i < argc; i++)
            {
                rc = testDisasNear(RTStrToUInt64(argv[i]));
                if (RT_FAILURE(rc))
                    rcRet++;
            }
        }
        else
        {
            /*
             * Enumerate symbols.
             */
            rc = RTLdrEnumSymbols(g_hLdrMod, RTLDR_ENUM_SYMBOL_FLAGS_ALL, g_pvBits, g_uLoadAddr, testEnumSymbol1, NULL);
            if (RT_FAILURE(rc))
            {
                RTPrintf("tstLdr-3: Failed to enumerate symbols: %Rra\n", rc);
                rcRet++;
            }

            /*
             * Query various properties.
             */
            union
            {
                char        szName[256];
                uint32_t    iImpModule;
                RTUUID      Uuid;
            } uBuf;
            rc = RTLdrQueryProp(g_hLdrMod, RTLDRPROP_INTERNAL_NAME, &uBuf, sizeof(uBuf));
            if (RT_SUCCESS(rc))
                RTPrintf("tstLdr-3: Internal name: %s\n", uBuf.szName);
            else if (rc != VERR_NOT_FOUND && rc != VERR_NOT_SUPPORTED)
            {
                RTPrintf("tstLdr-3: Internal name: failed - %Rrc\n", rc);
                rcRet++;
            }

            uint32_t cImports = 0;
            rc = RTLdrQueryProp(g_hLdrMod, RTLDRPROP_IMPORT_COUNT, &cImports, sizeof(cImports));
            if (RT_SUCCESS(rc))
            {
                RTPrintf("tstLdr-3: Import count: %u\n", cImports);
                for (uint32_t i = 0; i < cImports; i++)
                {
                    uBuf.iImpModule = i;
                    rc = RTLdrQueryProp(g_hLdrMod, RTLDRPROP_IMPORT_MODULE, &uBuf, sizeof(uBuf));
                    if (RT_SUCCESS(rc))
                        RTPrintf("tstLdr-3: Import module #%u: %s\n", i, uBuf.szName);
                    else
                    {
                        RTPrintf("tstLdr-3: Import module #%u: failed - %Rrc\n", i, rc);
                        rcRet++;
                    }
                }
            }
            else if (rc != VERR_NOT_FOUND && rc != VERR_NOT_SUPPORTED)
            {
                RTPrintf("tstLdr-3: Import count: failed - %Rrc\n", rc);
                rcRet++;
            }

            rc = RTLdrQueryProp(g_hLdrMod, RTLDRPROP_UUID, &uBuf.Uuid, sizeof(uBuf.Uuid));
            if (RT_SUCCESS(rc))
                RTPrintf("tstLdr-3: UUID: %RTuuid\n", uBuf.Uuid);
            else if (rc != VERR_NOT_FOUND && rc != VERR_NOT_SUPPORTED)
            {
                RTPrintf("tstLdr-3: UUID: failed - %Rrc\n", rc);
                rcRet++;
            }

            /*
             * Enumerate segments.
             */
            RTPrintf("tstLdr-3: Segments:\n");
            rc = RTLdrEnumSegments(g_hLdrMod, testEnumSegment1, NULL);
            if (RT_FAILURE(rc))
            {
                RTPrintf("tstLdr-3: Failed to enumerate symbols: %Rra\n", rc);
                rcRet++;
            }
        }
    }
    else
    {
        RTPrintf("tstLdr-3: Failed to get bits for '%s' at %RTptr: %Rra\n", argv[2], g_uLoadAddr, rc);
        rcRet++;
    }
    RTMemFree(g_pvBits);
    RTLdrClose(g_hLdrMod);

    /*
     * Test result summary.
     */
    if (!rcRet)
        RTPrintf("tstLdr-3: SUCCESS\n");
    else
        RTPrintf("tstLdr-3: FAILURE - %d errors\n", rcRet);
    return !!rcRet;
}