[2878] | 1 | /* $Id: tstkLdrMod.c 2955 2007-02-07 07:07:16Z bird $ */
|
---|
[2859] | 2 | /** @file
|
---|
| 3 | *
|
---|
| 4 | * kLdr - Module interpreter testcase.
|
---|
| 5 | *
|
---|
| 6 | * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
|
---|
| 7 | *
|
---|
| 8 | *
|
---|
| 9 | * This file is part of kLdr.
|
---|
| 10 | *
|
---|
| 11 | * kLdr is free software; you can redistribute it and/or modify
|
---|
| 12 | * it under the terms of the GNU General Public License as published by
|
---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * kLdr is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with kLdr; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 24 | *
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | /*******************************************************************************
|
---|
| 29 | * Header Files *
|
---|
| 30 | *******************************************************************************/
|
---|
| 31 | #include <kLdr.h>
|
---|
| 32 | #include <stdarg.h>
|
---|
| 33 | #include <stdio.h>
|
---|
| 34 | #include <stdlib.h>
|
---|
| 35 | #include <string.h>
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | /*******************************************************************************
|
---|
| 39 | * Defined Constants And Macros *
|
---|
| 40 | *******************************************************************************/
|
---|
| 41 | /** The default base address used in the tests. */
|
---|
[2861] | 42 | #define MY_BASEADDRESS 0x2400000
|
---|
[2859] | 43 |
|
---|
| 44 |
|
---|
| 45 | /*******************************************************************************
|
---|
| 46 | * Global Variables *
|
---|
| 47 | *******************************************************************************/
|
---|
| 48 | /** The numbers of errors. */
|
---|
| 49 | static int g_cErrors = 0;
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * Report failure.
|
---|
| 55 | */
|
---|
| 56 | static int Failure(const char *pszFormat, ...)
|
---|
| 57 | {
|
---|
| 58 | va_list va;
|
---|
| 59 |
|
---|
| 60 | g_cErrors++;
|
---|
| 61 |
|
---|
| 62 | printf("tstLdrMod: ");
|
---|
| 63 | va_start(va, pszFormat);
|
---|
| 64 | vprintf(pszFormat, va);
|
---|
| 65 | va_end(va);
|
---|
| 66 | printf("\n");
|
---|
| 67 | return 1;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 |
|
---|
[2861] | 71 | /** Dummy import resolver callback. */
|
---|
[2899] | 72 | static int BasicTestsGetImport(PKLDRMOD pMod, uint32_t iImport, uint32_t iSymbol, const char *pchSymbol, size_t cchSymbol,
|
---|
| 73 | const char *pszVersion, PKLDRADDR puValue, uint32_t *pfKind, void *pvUser)
|
---|
[2861] | 74 | {
|
---|
| 75 | *puValue = 0xdeadface;
|
---|
| 76 | *pfKind = KLDRSYMKIND_NO_BIT | KLDRSYMKIND_NO_TYPE;
|
---|
| 77 | return 0;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
[2890] | 81 | /**
|
---|
| 82 | * Verbose memcmp().
|
---|
| 83 | */
|
---|
| 84 | static int TestMemComp(const void *pv1, const void *pv2, size_t cb)
|
---|
| 85 | {
|
---|
| 86 | size_t off;
|
---|
| 87 | const uint8_t *pb1 = (const uint8_t *)pv1;
|
---|
| 88 | const uint8_t *pb2 = (const uint8_t *)pv2;
|
---|
| 89 | if (!memcmp(pb1, pb2, cb))
|
---|
| 90 | return 0;
|
---|
| 91 | printf("Mismatching blocks pv1=%p pv2=%p cb=%#x:\n", pv1, pv2, cb);
|
---|
| 92 | for (off = 0; off < cb; off++)
|
---|
| 93 | {
|
---|
| 94 | if (pb1[off] == pb2[off])
|
---|
| 95 | continue;
|
---|
| 96 | printf("%08x %02x != %02x\n", off, pb1[off], pb2[off]);
|
---|
| 97 | }
|
---|
| 98 | return memcmp(pb1, pb2, cb); /* lazy */
|
---|
| 99 | }
|
---|
[2861] | 100 |
|
---|
[2890] | 101 |
|
---|
[2859] | 102 | /**
|
---|
[2861] | 103 | * Performs basic relocation tests.
|
---|
| 104 | */
|
---|
| 105 | static int BasicTestsRelocate(PKLDRMOD pMod, void *pvBits, void *pvBits2)
|
---|
| 106 | {
|
---|
| 107 | const size_t cbImage = (size_t)kLdrModSize(pMod);
|
---|
| 108 | int rc;
|
---|
| 109 |
|
---|
| 110 | printf("* Relocation test...\n");
|
---|
| 111 |
|
---|
| 112 | /*
|
---|
| 113 | * Get the same bits again to check that we get the same result.
|
---|
| 114 | */
|
---|
| 115 | memset(pvBits2, 0xfe, cbImage);
|
---|
| 116 | rc = kLdrModGetBits(pMod, pvBits2, (uintptr_t)pvBits, BasicTestsGetImport, NULL);
|
---|
| 117 | if (rc)
|
---|
[2955] | 118 | return Failure("failed to get image bits, rc=%d (%s) (a)", rc, kLdrErrStr(rc));
|
---|
[2890] | 119 | if (TestMemComp(pvBits2, pvBits, cbImage))
|
---|
[2861] | 120 | return Failure("relocation test failed, mismatching bits (a)");
|
---|
| 121 |
|
---|
| 122 | /*
|
---|
| 123 | * Short relocation round trip.
|
---|
| 124 | */
|
---|
| 125 | rc = kLdrModRelocateBits(pMod, pvBits2, 0x1000, (uintptr_t)pvBits, BasicTestsGetImport, NULL);
|
---|
| 126 | if (rc)
|
---|
[2955] | 127 | return Failure("failed to relocate, rc=%d (%s) (b1)", rc, kLdrErrStr(rc));
|
---|
[2861] | 128 | rc = kLdrModRelocateBits(pMod, pvBits2, (uintptr_t)pvBits, 0x1000, BasicTestsGetImport, NULL);
|
---|
| 129 | if (rc)
|
---|
[2955] | 130 | return Failure("failed to relocate, rc=%d (%s) (b2)", rc, kLdrErrStr(rc));
|
---|
[2890] | 131 | if (TestMemComp(pvBits2, pvBits, cbImage))
|
---|
[2861] | 132 | return Failure("relocation test failed, mismatching bits (b)");
|
---|
| 133 |
|
---|
| 134 | /*
|
---|
| 135 | * Longer trip where we also check the intermediate results.
|
---|
| 136 | */
|
---|
| 137 | /* stage one */
|
---|
| 138 | rc = kLdrModRelocateBits(pMod, pvBits, 0x1000000, (uintptr_t)pvBits, BasicTestsGetImport, NULL);
|
---|
| 139 | if (rc)
|
---|
[2955] | 140 | return Failure("failed to relocate, rc=%d (%s) (c1)", rc, kLdrErrStr(rc));
|
---|
[2861] | 141 | memset(pvBits2, 0xfe, cbImage);
|
---|
| 142 | rc = kLdrModGetBits(pMod, pvBits2, 0x1000000, BasicTestsGetImport, NULL);
|
---|
| 143 | if (rc)
|
---|
[2955] | 144 | return Failure("failed to get image bits, rc=%d (%s) (c1)", rc, kLdrErrStr(rc));
|
---|
[2890] | 145 | if (TestMemComp(pvBits2, pvBits, cbImage))
|
---|
[2861] | 146 | return Failure("relocation test failed, mismatching bits (c1)");
|
---|
| 147 |
|
---|
| 148 | /* stage two */
|
---|
| 149 | rc = kLdrModRelocateBits(pMod, pvBits, ~(uintptr_t)0x1010000, 0x1000000, BasicTestsGetImport, NULL);
|
---|
| 150 | if (rc)
|
---|
[2955] | 151 | return Failure("failed to relocate, rc=%d (%s) (c2)", rc, kLdrErrStr(rc));
|
---|
[2861] | 152 | memset(pvBits2, 0xef, cbImage);
|
---|
| 153 | rc = kLdrModGetBits(pMod, pvBits2, ~(uintptr_t)0x1010000, BasicTestsGetImport, NULL);
|
---|
| 154 | if (rc)
|
---|
[2955] | 155 | return Failure("failed to get image bits, rc=%d (%s) (c2)", rc, kLdrErrStr(rc));
|
---|
[2890] | 156 | if (TestMemComp(pvBits2, pvBits, cbImage))
|
---|
[2861] | 157 | return Failure("relocation test failed, mismatching bits (c2)");
|
---|
| 158 |
|
---|
| 159 | /* stage three */
|
---|
| 160 | rc = kLdrModRelocateBits(pMod, pvBits, MY_BASEADDRESS, ~(uintptr_t)0x1010000, BasicTestsGetImport, NULL);
|
---|
| 161 | if (rc)
|
---|
[2955] | 162 | return Failure("failed to relocate, rc=%d (%s) (c3)", rc, kLdrErrStr(rc));
|
---|
[2861] | 163 | memset(pvBits2, 0xef, cbImage);
|
---|
| 164 | rc = kLdrModGetBits(pMod, pvBits2, MY_BASEADDRESS, BasicTestsGetImport, NULL);
|
---|
| 165 | if (rc)
|
---|
[2955] | 166 | return Failure("failed to get image bits, rc=%d (%s) (c3)", rc, kLdrErrStr(rc));
|
---|
[2890] | 167 | if (TestMemComp(pvBits2, pvBits, cbImage))
|
---|
[2861] | 168 | return Failure("relocation test failed, mismatching bits (c3)");
|
---|
| 169 |
|
---|
| 170 | /* stage four */
|
---|
| 171 | rc = kLdrModRelocateBits(pMod, pvBits, ~(uintptr_t)0 / 2 - 0x10000, MY_BASEADDRESS, BasicTestsGetImport, NULL);
|
---|
| 172 | if (rc)
|
---|
[2955] | 173 | return Failure("failed to relocate, rc=%d %(s) (c4)", rc, kLdrErrStr(rc));
|
---|
[2861] | 174 | memset(pvBits2, 0xdc, cbImage);
|
---|
| 175 | rc = kLdrModGetBits(pMod, pvBits2, ~(uintptr_t)0 / 2 - 0x10000, BasicTestsGetImport, NULL);
|
---|
| 176 | if (rc)
|
---|
[2955] | 177 | return Failure("failed to get image bits, rc=%d (%s) (c4)", rc, kLdrErrStr(rc));
|
---|
[2890] | 178 | if (TestMemComp(pvBits2, pvBits, cbImage))
|
---|
[2861] | 179 | return Failure("relocation test failed, mismatching bits (c4)");
|
---|
| 180 |
|
---|
| 181 | /* return */
|
---|
| 182 | rc = kLdrModRelocateBits(pMod, pvBits, (uintptr_t)pvBits, ~(uintptr_t)0 / 2 - 0x10000, BasicTestsGetImport, NULL);
|
---|
| 183 | if (rc)
|
---|
[2955] | 184 | return Failure("failed to relocate, rc=%d (%s) (c5)", rc, kLdrErrStr(rc));
|
---|
[2861] | 185 | memset(pvBits2, 0xcd, cbImage);
|
---|
| 186 | rc = kLdrModGetBits(pMod, pvBits2, (uintptr_t)pvBits, BasicTestsGetImport, NULL);
|
---|
| 187 | if (rc)
|
---|
[2955] | 188 | return Failure("failed to get image bits, rc=%d (%s) (c5)", rc, kLdrErrStr(rc));
|
---|
[2890] | 189 | if (TestMemComp(pvBits2, pvBits, cbImage))
|
---|
[2861] | 190 | return Failure("relocation test failed, mismatching bits (c5)");
|
---|
| 191 |
|
---|
| 192 | return 0;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 |
|
---|
| 196 | /**
|
---|
[2859] | 197 | * Dump symbols and check that we can query each of them recursivly.
|
---|
| 198 | */
|
---|
[2899] | 199 | static int BasicTestsEnumSymCallback(PKLDRMOD pMod, uint32_t iSymbol, const char *pchSymbol, size_t cchSymbol,
|
---|
| 200 | const char *pszVersion, KLDRADDR uValue, uint32_t fKind, void *pvUser)
|
---|
[2859] | 201 | {
|
---|
| 202 | KLDRADDR uValue2;
|
---|
| 203 | uint32_t fKind2;
|
---|
| 204 | int rc;
|
---|
| 205 |
|
---|
| 206 | /* dump */
|
---|
| 207 | printf("#0x%08x: %016" PRI_KLDRADDR " %#08x", iSymbol, uValue, fKind);
|
---|
[2899] | 208 | if (pchSymbol)
|
---|
| 209 | printf(" %.*s", cchSymbol, pchSymbol);
|
---|
[2859] | 210 | printf("\n");
|
---|
| 211 |
|
---|
| 212 | /* query by ordinal */
|
---|
| 213 | if (iSymbol != NIL_KLDRMOD_SYM_ORDINAL)
|
---|
| 214 | {
|
---|
[2899] | 215 | rc = kLdrModQuerySymbol(pMod, pvUser, MY_BASEADDRESS, iSymbol, NULL, 0, NULL, NULL, NULL,
|
---|
[2859] | 216 | &uValue2, &fKind2);
|
---|
| 217 | if (rc)
|
---|
[2955] | 218 | return Failure("Couldn't find symbol %#x (%.*s) by ordinal. rc=%d (%s)", iSymbol, cchSymbol, pchSymbol, rc, kLdrErrStr(rc));
|
---|
[2859] | 219 | if (uValue != uValue2)
|
---|
[2899] | 220 | return Failure("Symbol %#x (%.*s): Value mismatch %016" PRI_KLDRADDR " != %016" PRI_KLDRADDR " (enum!=query/ord) pvBits=%p",
|
---|
| 221 | iSymbol, cchSymbol, pchSymbol, uValue, uValue2, pvUser);
|
---|
[2859] | 222 | if (fKind != fKind2)
|
---|
[2899] | 223 | return Failure("Symbol %#x (%.*s): Kind mismatch %#x != %#x (enum!=query/ord) pvBits=%p",
|
---|
| 224 | iSymbol, cchSymbol, pchSymbol, fKind, fKind2, pvUser);
|
---|
[2859] | 225 | }
|
---|
| 226 |
|
---|
| 227 | /* query by name. */
|
---|
[2899] | 228 | if (pchSymbol)
|
---|
[2859] | 229 | {
|
---|
[2899] | 230 | rc = kLdrModQuerySymbol(pMod, pvUser, MY_BASEADDRESS, NIL_KLDRMOD_SYM_ORDINAL, pchSymbol, cchSymbol, pszVersion,
|
---|
| 231 | NULL, NULL, &uValue2, &fKind2);
|
---|
[2859] | 232 | if (rc)
|
---|
[2955] | 233 | return Failure("Couldn't find symbol %#x (%.*s) by name. rc=%d (%s)", iSymbol, cchSymbol, pchSymbol, rc, kLdrErrStr(rc));
|
---|
[2859] | 234 | if (uValue != uValue2)
|
---|
[2899] | 235 | return Failure("Symbol %#x (%.*s): Value mismatch %016" PRI_KLDRADDR " != %016" PRI_KLDRADDR " (enum!=query/name) pvBits=%p",
|
---|
| 236 | iSymbol, cchSymbol, pchSymbol, uValue, uValue2, pvUser);
|
---|
[2859] | 237 | if (fKind != fKind2)
|
---|
[2899] | 238 | return Failure("Symbol %#x (%.*s): Kind mismatch %#x != %#x (enum!=query/name) pvBits=%p",
|
---|
| 239 | iSymbol, cchSymbol, pchSymbol, fKind, fKind2, pvUser);
|
---|
[2859] | 240 | }
|
---|
| 241 |
|
---|
| 242 | return 0;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 |
|
---|
| 246 | /**
|
---|
| 247 | * Dump debugger information and check it for correctness.
|
---|
| 248 | */
|
---|
| 249 | static int BasicTestEnumDbgInfoCallback(PKLDRMOD pMod, uint32_t iDbgInfo, KLDRDBGINFOTYPE enmType,
|
---|
| 250 | int16_t iMajorVer, int16_t iMinorVer, off_t offFile, KLDRADDR LinkAddress,
|
---|
| 251 | KLDRSIZE cb, const char *pszExtFile, void *pvUser)
|
---|
| 252 | {
|
---|
| 253 | printf("#0x%08x: enmType=%d %d.%d offFile=0x%" PRI_KLDRADDR " LinkAddress=%" PRI_KLDRADDR " cb=%" PRI_KLDRSIZE " pvUser=%p\n",
|
---|
| 254 | iDbgInfo, enmType, iMajorVer, iMinorVer, (KLDRADDR)offFile, LinkAddress, cb, pvUser);
|
---|
| 255 | if (pszExtFile)
|
---|
| 256 | printf(" pszExtFile=%p '%s'\n", pszExtFile, pszExtFile);
|
---|
| 257 |
|
---|
| 258 | if (enmType >= KLDRDBGINFOTYPE_END || enmType <= KLDRDBGINFOTYPE_INVALID)
|
---|
| 259 | return Failure("Bad enmType");
|
---|
| 260 | if (pvUser != NULL)
|
---|
| 261 | return Failure("pvUser");
|
---|
| 262 |
|
---|
| 263 | return 0;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 |
|
---|
| 267 | /**
|
---|
| 268 | * Performs the basic module loader test on the specified module and image bits.
|
---|
| 269 | */
|
---|
| 270 | static int BasicTestsSub2(PKLDRMOD pMod, void *pvBits)
|
---|
| 271 | {
|
---|
| 272 | int32_t cImports;
|
---|
| 273 | int32_t i;
|
---|
| 274 | int rc;
|
---|
| 275 | uint32_t fKind;
|
---|
| 276 | KLDRADDR Value;
|
---|
| 277 | KLDRADDR MainEPAddress;
|
---|
| 278 | KLDRSTACKINFO StackInfo;
|
---|
| 279 |
|
---|
[2861] | 280 | printf("* Testing queries with pvBits=%p...\n", pvBits);
|
---|
| 281 |
|
---|
[2859] | 282 | /*
|
---|
| 283 | * Get the import modules.
|
---|
| 284 | */
|
---|
| 285 | cImports = kLdrModNumberOfImports(pMod, pvBits);
|
---|
| 286 | printf("cImports=%d\n", cImports);
|
---|
| 287 | if (cImports < 0)
|
---|
[2861] | 288 | return Failure("failed to query the number of import, cImports=%d", cImports);
|
---|
[2859] | 289 | for (i = 0; i < cImports; i++)
|
---|
| 290 | {
|
---|
| 291 | char szImportModule[260];
|
---|
| 292 | rc = kLdrModGetImport(pMod, pvBits, i, szImportModule, sizeof(szImportModule));
|
---|
| 293 | if (rc)
|
---|
[2955] | 294 | return Failure("failed to get import module name, rc=%d (%s). (%.260s)", rc, kLdrErrStr(rc), szImportModule);
|
---|
[2859] | 295 | printf("import #%d: '%s'\n", i, szImportModule);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | /*
|
---|
| 299 | * Query stack info.
|
---|
| 300 | */
|
---|
| 301 | StackInfo.Address = ~(KLDRADDR)42;
|
---|
| 302 | StackInfo.LinkAddress = ~(KLDRADDR)42;
|
---|
| 303 | StackInfo.cbStack = ~(KLDRSIZE)42;
|
---|
| 304 | StackInfo.cbStackThread = ~(KLDRSIZE)42;
|
---|
| 305 | rc = kLdrModGetStackInfo(pMod, pvBits, MY_BASEADDRESS, &StackInfo);
|
---|
| 306 | if (rc)
|
---|
[2955] | 307 | return Failure("kLdrModGetStackInfo failed with rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2859] | 308 | printf("Stack: Address=%016" PRI_KLDRADDR " LinkAddress=%016" PRI_KLDRADDR "\n"
|
---|
| 309 | " cbStack=%016" PRI_KLDRSIZE " cbStackThread=%016" PRI_KLDRSIZE "\n",
|
---|
| 310 | StackInfo.Address, StackInfo.LinkAddress, StackInfo.cbStack, StackInfo.cbStackThread);
|
---|
| 311 | if (StackInfo.Address == ~(KLDRADDR)42)
|
---|
| 312 | return Failure("Bad StackInfo.Address");
|
---|
| 313 | if (StackInfo.LinkAddress == ~(KLDRADDR)42)
|
---|
| 314 | return Failure("Bad StackInfo.LinkAddress");
|
---|
| 315 | if (StackInfo.cbStack == ~(KLDRSIZE)42)
|
---|
| 316 | return Failure("Bad StackInfo.cbStack");
|
---|
| 317 | if (StackInfo.cbStackThread == ~(KLDRSIZE)42)
|
---|
| 318 | return Failure("Bad StackInfo.cbStackThread");
|
---|
| 319 |
|
---|
| 320 | /*
|
---|
| 321 | * Query entrypoint.
|
---|
| 322 | */
|
---|
| 323 | MainEPAddress = ~(KLDRADDR)42;
|
---|
| 324 | rc = kLdrModQueryMainEntrypoint(pMod, pvBits, MY_BASEADDRESS, &MainEPAddress);
|
---|
| 325 | if (rc)
|
---|
[2955] | 326 | return Failure("kLdrModQueryMainEntrypoint failed with rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2859] | 327 | printf("Entrypoint: %016" PRI_KLDRADDR "\n", MainEPAddress);
|
---|
| 328 | if (MainEPAddress == ~(KLDRADDR)42)
|
---|
| 329 | return Failure("MainEPAddress wasn't set.");
|
---|
[2861] | 330 | if (MainEPAddress != NIL_KLDRADDR && MainEPAddress < MY_BASEADDRESS)
|
---|
| 331 | return Failure("Bad MainEPAddress (a).");
|
---|
| 332 | if (MainEPAddress != NIL_KLDRADDR && MainEPAddress >= MY_BASEADDRESS + kLdrModSize(pMod))
|
---|
| 333 | return Failure("Bad MainEPAddress (b).");
|
---|
[2859] | 334 |
|
---|
| 335 | /*
|
---|
| 336 | * Debugger information.
|
---|
| 337 | */
|
---|
| 338 | rc = kLdrModHasDbgInfo(pMod, pvBits);
|
---|
| 339 | if (!rc)
|
---|
| 340 | printf("Has Debugger Information\n");
|
---|
| 341 | else if (rc == KLDR_ERR_NO_DEBUG_INFO)
|
---|
| 342 | printf("NO Debugger Information\n");
|
---|
| 343 | else
|
---|
[2955] | 344 | return Failure("kLdrModHasDbgInfo failed with rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2859] | 345 | rc = kLdrModEnumDbgInfo(pMod, pvBits, BasicTestEnumDbgInfoCallback, NULL);
|
---|
| 346 | if (rc)
|
---|
[2955] | 347 | return Failure("kLdrModEnumDbgInfo failed with rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2859] | 348 |
|
---|
| 349 |
|
---|
| 350 | /*
|
---|
| 351 | * Negative symbol query tests.
|
---|
| 352 | */
|
---|
| 353 | fKind = 0xdeadf00d;
|
---|
| 354 | Value = 0x0badc0de;
|
---|
[2899] | 355 | rc = kLdrModQuerySymbol(pMod, pvBits, MY_BASEADDRESS, NIL_KLDRMOD_SYM_ORDINAL - 20, NULL, 0, NULL, NULL, NULL,
|
---|
[2859] | 356 | &Value, &fKind);
|
---|
| 357 | if (rc)
|
---|
| 358 | {
|
---|
| 359 | if (fKind != 0)
|
---|
| 360 | return Failure("fKind wasn't cleared on failure.");
|
---|
| 361 | if (Value != 0)
|
---|
| 362 | return Failure("Value wasn't cleared on failure.");
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | fKind = 0xdeadf00d;
|
---|
| 366 | Value = 0x0badc0de;
|
---|
[2899] | 367 | rc = kLdrModQuerySymbol(pMod, pvBits, MY_BASEADDRESS, NIL_KLDRMOD_SYM_ORDINAL, NULL, 0, NULL, NULL, NULL,
|
---|
[2859] | 368 | &Value, &fKind);
|
---|
| 369 | if (!rc)
|
---|
| 370 | return Failure("NIL ordinal succeeded!");
|
---|
| 371 | if (fKind != 0)
|
---|
| 372 | return Failure("fKind wasn't cleared on failure.");
|
---|
| 373 | if (Value != 0)
|
---|
| 374 | return Failure("Value wasn't cleared on failure.");
|
---|
| 375 |
|
---|
| 376 | /*
|
---|
| 377 | * Enumerate and query all symbols.
|
---|
| 378 | */
|
---|
| 379 | printf("\n"
|
---|
| 380 | "Symbols:\n");
|
---|
| 381 | rc = kLdrModEnumSymbols(pMod, pvBits, MY_BASEADDRESS, 0, BasicTestsEnumSymCallback, pvBits);
|
---|
| 382 | if (rc)
|
---|
[2955] | 383 | return Failure("kLdrModEnumSymbols failed with rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2859] | 384 |
|
---|
| 385 |
|
---|
| 386 | /*int kLdrModCanExecuteOn(PKLDRMOD pMod, const void *pvBits, KLDRARCH enmArch, KLDRCPU enmCpu);
|
---|
| 387 | */
|
---|
| 388 |
|
---|
| 389 | return 0;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 |
|
---|
| 393 | /**
|
---|
| 394 | * Performs the basic module loader test on the specified module
|
---|
| 395 | */
|
---|
| 396 | static int BasicTestsSub(PKLDRMOD pMod)
|
---|
| 397 | {
|
---|
| 398 | int rc;
|
---|
| 399 | uint32_t i;
|
---|
| 400 | void *pvBits;
|
---|
| 401 | size_t cbImage;
|
---|
| 402 |
|
---|
| 403 | /*
|
---|
| 404 | * Check/dump the module structure.
|
---|
| 405 | */
|
---|
[2899] | 406 | printf("pMod=%p u32Magic=%#x cSegments=%d\n", (void *)pMod, pMod->u32Magic, pMod->cSegments);
|
---|
[2859] | 407 | printf("enmType=%d enmFmt=%d enmArch=%d enmCpu=%d enmEndian=%d\n",
|
---|
| 408 | pMod->enmType, pMod->enmFmt, pMod->enmArch, pMod->enmCpu, pMod->enmEndian);
|
---|
| 409 | printf("Filename: %s (%d bytes)\n", pMod->pszFilename, pMod->cchFilename);
|
---|
| 410 | printf(" Name: %s (%d bytes)\n", pMod->pszName, pMod->cchName);
|
---|
| 411 | printf("\n");
|
---|
| 412 |
|
---|
| 413 | if (pMod->u32Magic != KLDRMOD_MAGIC)
|
---|
| 414 | return Failure("Bad u32Magic");
|
---|
| 415 | if (strlen(pMod->pszFilename) != pMod->cchFilename)
|
---|
| 416 | return Failure("Bad cchFilename");
|
---|
| 417 | if (strlen(pMod->pszName) != pMod->cchName)
|
---|
| 418 | return Failure("Bad cchName");
|
---|
| 419 | if (pMod->enmFmt >= KLDRFMT_END || pMod->enmFmt <= KLDRFMT_INVALID)
|
---|
| 420 | return Failure("Bad enmFmt");
|
---|
| 421 | if (pMod->enmType >= KLDRTYPE_END || pMod->enmType <= KLDRTYPE_INVALID)
|
---|
| 422 | return Failure("Bad enmType: %d", pMod->enmType);
|
---|
| 423 | if (pMod->enmArch >= KLDRARCH_END || pMod->enmArch <= KLDRARCH_INVALID)
|
---|
| 424 | return Failure("Bad enmArch");
|
---|
| 425 | if (pMod->enmCpu >= KLDRCPU_END || pMod->enmCpu <= KLDRCPU_INVALID)
|
---|
| 426 | return Failure("Bad enmCpu");
|
---|
| 427 | if (pMod->enmEndian >= KLDRENDIAN_END || pMod->enmEndian <= KLDRENDIAN_INVALID)
|
---|
| 428 | return Failure("Bad enmEndian");
|
---|
| 429 |
|
---|
| 430 | for (i = 0; i < pMod->cSegments; i++)
|
---|
| 431 | {
|
---|
| 432 | printf("seg #%d: pvUser=%p enmProt=%d Name: '%.*s' (%d bytes)\n",
|
---|
| 433 | i, pMod->aSegments[i].pvUser, pMod->aSegments[i].enmProt,
|
---|
| 434 | pMod->aSegments[i].cchName, pMod->aSegments[i].pchName, pMod->aSegments[i].cchName);
|
---|
| 435 | printf("LinkAddress: %016" PRI_KLDRADDR " cb: %016" PRI_KLDRSIZE " Alignment=%08" PRI_KLDRADDR " \n",
|
---|
| 436 | pMod->aSegments[i].LinkAddress, pMod->aSegments[i].cb, pMod->aSegments[i].Alignment);
|
---|
| 437 | printf(" RVA: %016" PRI_KLDRADDR " cbMapped: %016" PRI_KLDRSIZE " MapAddress=%p\n",
|
---|
[2899] | 438 | pMod->aSegments[i].RVA, (KLDRSIZE)pMod->aSegments[i].cbMapped, (void *)pMod->aSegments[i].MapAddress);
|
---|
[2859] | 439 | printf(" offFile: %016" PRI_KLDRADDR " cbFile: %016" PRI_KLDRSIZE "\n",
|
---|
| 440 | (KLDRADDR)pMod->aSegments[i].offFile, (KLDRSIZE)pMod->aSegments[i].cbFile);
|
---|
| 441 | printf("\n");
|
---|
| 442 |
|
---|
| 443 | if (pMod->aSegments[i].pvUser != NULL)
|
---|
| 444 | return Failure("Bad pvUser");
|
---|
| 445 | if (pMod->aSegments[i].enmProt >= KLDRPROT_END || pMod->aSegments[i].enmProt <= KLDRPROT_INVALID)
|
---|
| 446 | return Failure("Bad enmProt");
|
---|
| 447 | if (pMod->aSegments[i].MapAddress != 0)
|
---|
| 448 | return Failure("Bad MapAddress");
|
---|
| 449 | if (pMod->aSegments[i].cbMapped < pMod->aSegments[i].cb)
|
---|
| 450 | return Failure("Bad cbMapped (1)");
|
---|
| 451 | if (pMod->aSegments[i].cbMapped && !pMod->aSegments[i].Alignment)
|
---|
| 452 | return Failure("Bad cbMapped (2)");
|
---|
| 453 | if (pMod->aSegments[i].cbMapped > kLdrModSize(pMod))
|
---|
| 454 | return Failure("Bad cbMapped (3)");
|
---|
| 455 | if ( pMod->aSegments[i].Alignment
|
---|
| 456 | && (pMod->aSegments[i].RVA & (pMod->aSegments[i].Alignment - 1)))
|
---|
| 457 | return Failure("Bad RVA (1)");
|
---|
| 458 | if (pMod->aSegments[i].RVA != NIL_KLDRADDR && !pMod->aSegments[i].Alignment)
|
---|
| 459 | return Failure("Bad RVA (2)");
|
---|
| 460 | if ( pMod->aSegments[i].RVA != NIL_KLDRADDR
|
---|
| 461 | && pMod->aSegments[i].RVA >= kLdrModSize(pMod))
|
---|
| 462 | return Failure("Bad RVA (3)");
|
---|
| 463 | if ( pMod->aSegments[i].RVA != NIL_KLDRADDR
|
---|
| 464 | && pMod->aSegments[i].RVA + pMod->aSegments[i].cbMapped > kLdrModSize(pMod))
|
---|
| 465 | return Failure("Bad RVA/cbMapped (4)");
|
---|
| 466 | if (pMod->aSegments[i].LinkAddress != NIL_KLDRADDR && !pMod->aSegments[i].Alignment)
|
---|
| 467 | return Failure("Bad LinkAddress");
|
---|
| 468 | if ( pMod->aSegments[i].LinkAddress != NIL_KLDRADDR
|
---|
| 469 | && (pMod->aSegments[i].LinkAddress) & (pMod->aSegments[i].Alignment - 1))
|
---|
| 470 | return Failure("Bad LinkAddress alignment");
|
---|
| 471 | if (pMod->aSegments[i].offFile != -1 && pMod->aSegments[i].cbFile == -1)
|
---|
| 472 | return Failure("Bad offFile");
|
---|
| 473 | if (pMod->aSegments[i].offFile == -1 && pMod->aSegments[i].cbFile != -1)
|
---|
| 474 | return Failure("Bad cbFile");
|
---|
| 475 | }
|
---|
| 476 |
|
---|
[2861] | 477 |
|
---|
[2859] | 478 | /*
|
---|
| 479 | * Get image the size and query the image bits.
|
---|
| 480 | */
|
---|
[2861] | 481 | printf("* Testing user mapping...\n");
|
---|
| 482 |
|
---|
[2859] | 483 | cbImage = (size_t)kLdrModSize(pMod);
|
---|
| 484 | if (cbImage != kLdrModSize(pMod))
|
---|
[2861] | 485 | return Failure("aborting test because the image is too huge!");
|
---|
[2859] | 486 | pvBits = malloc((size_t)cbImage);
|
---|
| 487 | if (!pvBits)
|
---|
[2861] | 488 | return Failure("failed to allocate %d bytes for the image", cbImage);
|
---|
[2859] | 489 |
|
---|
| 490 | rc = kLdrModGetBits(pMod, pvBits, (uintptr_t)pvBits, BasicTestsGetImport, NULL);
|
---|
| 491 | if (rc)
|
---|
[2955] | 492 | return Failure("failed to get image bits, rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2859] | 493 |
|
---|
| 494 | /*
|
---|
| 495 | * Another cleanup nesting.
|
---|
| 496 | */
|
---|
| 497 | rc = BasicTestsSub2(pMod, pvBits);
|
---|
[2861] | 498 | if (!rc)
|
---|
| 499 | {
|
---|
| 500 | /*
|
---|
| 501 | * Test relocating the bits in a few different ways before we're done with them.
|
---|
| 502 | */
|
---|
| 503 | void *pvBits2 = malloc((size_t)cbImage);
|
---|
| 504 | if (pvBits2)
|
---|
| 505 | {
|
---|
| 506 | rc = BasicTestsRelocate(pMod, pvBits, pvBits2);
|
---|
| 507 | free(pvBits2);
|
---|
| 508 | }
|
---|
| 509 | else
|
---|
| 510 | rc = Failure("failed to allocate %d bytes for the 2nd image", cbImage);
|
---|
| 511 | }
|
---|
| 512 |
|
---|
[2859] | 513 | free(pvBits);
|
---|
| 514 | return rc;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 |
|
---|
| 518 | /**
|
---|
[2861] | 519 | * Tests the mapping related api, after mapping.
|
---|
| 520 | */
|
---|
| 521 | static int BasicTestsSubMap2(PKLDRMOD pMod)
|
---|
| 522 | {
|
---|
| 523 | int rc;
|
---|
| 524 |
|
---|
| 525 | rc = kLdrModFixupMapping(pMod, BasicTestsGetImport, NULL);
|
---|
| 526 | if (rc)
|
---|
[2955] | 527 | return Failure("kLdrModFixupMapping (a) failed, rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2861] | 528 |
|
---|
| 529 | rc = kLdrModReload(pMod);
|
---|
| 530 | if (rc)
|
---|
[2955] | 531 | return Failure("kLdrModReload (a) failed, rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2861] | 532 |
|
---|
| 533 | rc = kLdrModReload(pMod);
|
---|
| 534 | if (rc)
|
---|
[2955] | 535 | return Failure("kLdrModReload (b) failed, rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2861] | 536 |
|
---|
| 537 | rc = kLdrModFixupMapping(pMod, BasicTestsGetImport, NULL);
|
---|
| 538 | if (rc)
|
---|
[2955] | 539 | return Failure("kLdrModFixupMapping (b) failed, rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2861] | 540 |
|
---|
| 541 | rc = kLdrModAllocTLS(pMod);
|
---|
| 542 | if (rc)
|
---|
[2955] | 543 | return Failure("kLdrModAllocTLS (a) failed, rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2861] | 544 | kLdrModFreeTLS(pMod);
|
---|
| 545 |
|
---|
| 546 | rc = kLdrModAllocTLS(pMod);
|
---|
| 547 | if (rc)
|
---|
[2955] | 548 | return Failure("kLdrModAllocTLS (b) failed, rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2861] | 549 | kLdrModFreeTLS(pMod);
|
---|
| 550 |
|
---|
| 551 | /*
|
---|
| 552 | * Repeat the BasicTestsSub2 with pvBits as NULL to test module
|
---|
| 553 | * interpreters that can utilize the mapping.
|
---|
| 554 | */
|
---|
| 555 | rc = BasicTestsSub2(pMod, NULL);
|
---|
| 556 | if (rc)
|
---|
[2955] | 557 | return Failure("BasicTestsSub2 in Map2 failed, rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2861] | 558 | return 0;
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 |
|
---|
| 562 | /**
|
---|
[2860] | 563 | * Tests the mapping related api.
|
---|
| 564 | */
|
---|
| 565 | static int BasicTestsSubMap(PKLDRMOD pMod)
|
---|
| 566 | {
|
---|
[2861] | 567 | int rc, rc2;
|
---|
| 568 | printf("* Mapping tests...\n");
|
---|
[2860] | 569 |
|
---|
| 570 | rc = kLdrModMap(pMod);
|
---|
| 571 | if (rc)
|
---|
[2955] | 572 | return Failure("kLdrModMap failed, rc=%d (%s)", rc, kLdrErrStr(rc));
|
---|
[2861] | 573 | rc = BasicTestsSubMap2(pMod);
|
---|
| 574 | rc2 = kLdrModUnmap(pMod);
|
---|
| 575 | if (rc2)
|
---|
| 576 | {
|
---|
[2955] | 577 | Failure("kLdrModUnmap failed, rc=%d (%s)", rc2, kLdrErrStr(rc2));
|
---|
[2861] | 578 | rc = rc ? rc : rc2;
|
---|
| 579 | }
|
---|
[2860] | 580 |
|
---|
[2861] | 581 | printf("* Mapping tests done.\n");
|
---|
| 582 | return rc;
|
---|
[2860] | 583 | }
|
---|
| 584 |
|
---|
| 585 |
|
---|
| 586 | /**
|
---|
[2859] | 587 | * Performs basic module loader tests on the specified file.
|
---|
| 588 | */
|
---|
| 589 | static int BasicTests(const char *pszFilename)
|
---|
| 590 | {
|
---|
| 591 | PKLDRMOD pMod;
|
---|
| 592 | int rc, rc2;
|
---|
| 593 |
|
---|
[2861] | 594 | printf("tstLdrMod: Testing '%s'", pszFilename);
|
---|
[2859] | 595 | rc = kLdrModOpen(pszFilename, &pMod);
|
---|
| 596 | if (!rc)
|
---|
| 597 | {
|
---|
| 598 | rc = BasicTestsSub(pMod);
|
---|
[2860] | 599 | if (!rc)
|
---|
| 600 | rc = BasicTestsSubMap(pMod);
|
---|
[2861] | 601 | if (!rc)
|
---|
| 602 | rc = BasicTestsSub2(pMod, NULL);
|
---|
[2859] | 603 | rc2 = kLdrModClose(pMod);
|
---|
| 604 | if (rc2)
|
---|
[2955] | 605 | Failure("failed to close '%s', rc=%d (%s)", pszFilename, rc, kLdrErrStr(rc));
|
---|
[2859] | 606 | if (rc2 && !rc)
|
---|
| 607 | rc = rc2;
|
---|
| 608 | }
|
---|
| 609 | else
|
---|
[2955] | 610 | Failure("Failed to open '%s', rc=%d (%s)", pszFilename, rc, kLdrErrStr(rc));
|
---|
[2859] | 611 | return rc ? 1 : 0;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 |
|
---|
| 615 | int main(int argc, char **argv)
|
---|
| 616 | {
|
---|
| 617 | BasicTests(argv[argc-1]);
|
---|
| 618 |
|
---|
| 619 | if (!g_cErrors)
|
---|
| 620 | printf("tstLdrMod: SUCCESS\n");
|
---|
| 621 | else
|
---|
| 622 | printf("tstLdrMod: FAILURE - %d errors\n", g_cErrors);
|
---|
| 623 | return !!g_cErrors;
|
---|
| 624 | }
|
---|
| 625 |
|
---|