[2878] | 1 | /* $Id: tst-0-driver.c 3601 2007-10-29 00:21:13Z bird $ */
|
---|
[2864] | 2 | /** @file
|
---|
| 3 | * kLdr - Dynamic Loader testcase no. 0, Driver.
|
---|
[3601] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
|
---|
[2864] | 8 | *
|
---|
[3601] | 9 | * This file is part of kStuff.
|
---|
[2864] | 10 | *
|
---|
[3601] | 11 | * kStuff is free software; you can redistribute it and/or
|
---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
---|
| 13 | * License as published by the Free Software Foundation; either
|
---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
---|
[2864] | 15 | *
|
---|
[3601] | 16 | * kStuff is distributed in the hope that it will be useful,
|
---|
[2864] | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
[3601] | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 19 | * Lesser General Public License for more details.
|
---|
[2864] | 20 | *
|
---|
[3601] | 21 | * You should have received a copy of the GNU Lesser General Public
|
---|
| 22 | * License along with kStuff; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
[2864] | 24 | *
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 | /*******************************************************************************
|
---|
| 28 | * Header Files *
|
---|
| 29 | *******************************************************************************/
|
---|
[2870] | 30 | #include "tst.h"
|
---|
[2864] | 31 | #include <stdarg.h>
|
---|
| 32 | #include <stdio.h>
|
---|
| 33 | #include <stdlib.h>
|
---|
| 34 | #include <string.h>
|
---|
| 35 |
|
---|
| 36 |
|
---|
[2870] | 37 | /*******************************************************************************
|
---|
| 38 | * Defined Constants And Macros *
|
---|
| 39 | *******************************************************************************/
|
---|
| 40 | /** Select the appropriate KLDRSYMKIND bit define. */
|
---|
| 41 | #define MY_KLDRSYMKIND_BITS ( sizeof(void *) == 4 ? KLDRSYMKIND_32BIT : KLDRSYMKIND_64BIT )
|
---|
[2864] | 42 |
|
---|
[2870] | 43 |
|
---|
[2864] | 44 | /*******************************************************************************
|
---|
| 45 | * Global Variables *
|
---|
| 46 | *******************************************************************************/
|
---|
| 47 | /** The numbers of errors. */
|
---|
| 48 | static int g_cErrors = 0;
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Report failure.
|
---|
| 54 | */
|
---|
| 55 | static int Failure(const char *pszFormat, ...)
|
---|
| 56 | {
|
---|
| 57 | va_list va;
|
---|
| 58 |
|
---|
| 59 | g_cErrors++;
|
---|
| 60 |
|
---|
[2965] | 61 | printf("tst-0-driver: ");
|
---|
[2864] | 62 | va_start(va, pszFormat);
|
---|
| 63 | vprintf(pszFormat, va);
|
---|
| 64 | va_end(va);
|
---|
| 65 | printf("\n");
|
---|
| 66 | return 1;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | int main(int argc, char **argv)
|
---|
| 71 | {
|
---|
| 72 | const char *pszErrInit = "Error, szErr wasn't zapped";
|
---|
| 73 | char szErr[512];
|
---|
[2870] | 74 | char szBuf[512];
|
---|
| 75 | char *psz;
|
---|
[3584] | 76 | KSIZE cch;
|
---|
[2864] | 77 | HKLDRMOD hMod;
|
---|
| 78 | int rc;
|
---|
| 79 |
|
---|
[2870] | 80 | /*
|
---|
| 81 | * The first thing to do is a simple load / unload test
|
---|
| 82 | * using the tst-0-a library (it'll drag in tst-0-d).
|
---|
| 83 | */
|
---|
| 84 | printf("tst-0-driver: Basic API test using 'tst-0-a'...\n");
|
---|
[2864] | 85 | hMod = (HKLDRMOD)0xffffeeee;
|
---|
| 86 | strcpy(szErr, pszErrInit);
|
---|
[2869] | 87 | rc = kLdrDyldLoad("tst-0-a", NULL, NULL, KLDRDYLD_SEARCH_HOST,
|
---|
[2864] | 88 | KLDRDYLD_LOAD_FLAGS_RECURSIVE_INIT, &hMod, szErr, sizeof(szErr));
|
---|
| 89 | if (rc)
|
---|
| 90 | Failure("kLdrDyldLoad(\"tst-0\",...) failed, rc=%d (%#x). szErr='%s'.\n", rc, rc, szErr);
|
---|
[2869] | 91 | if (!strcmp(szErr, pszErrInit))
|
---|
[2864] | 92 | Failure("szErr wasn't set.\n");
|
---|
| 93 | if (hMod == (HKLDRMOD)0xffffeeee)
|
---|
| 94 | Failure("hMod wasn't set.\n");
|
---|
| 95 | if (hMod == NIL_HKLDRMOD && !rc)
|
---|
| 96 | Failure("rc=0 but hMod=NIL_HKLDRMOD\n");
|
---|
| 97 | if (!rc)
|
---|
| 98 | {
|
---|
[2870] | 99 | HKLDRMOD hMod2;
|
---|
| 100 | HKLDRMOD hMod3;
|
---|
| 101 | printf("tst-0-driver: hMod=%p ('tst-0-a')\n", (void *)hMod);
|
---|
| 102 |
|
---|
| 103 | /*
|
---|
| 104 | * Simple test of kLdrDyldFindByName.
|
---|
| 105 | */
|
---|
| 106 | hMod2 = (HKLDRMOD)0xffffeeee;
|
---|
| 107 | rc = kLdrDyldFindByName("tst-0", NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hMod2);
|
---|
| 108 | if (!rc)
|
---|
| 109 | Failure("kLdrDyldFindByName(\"tst-0\",,,) didn't fail!\n");
|
---|
| 110 | if (rc && hMod2 != NIL_HKLDRMOD)
|
---|
| 111 | Failure("hMod2 wasn't set correctly on kLdrDyldFindByName failure!\n");
|
---|
| 112 |
|
---|
| 113 | hMod2 = (HKLDRMOD)0xffffeeee;
|
---|
| 114 | rc = kLdrDyldFindByName("tst-0-a", NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hMod2);
|
---|
| 115 | if (rc)
|
---|
| 116 | Failure("kLdrDyldFindByName(\"tst-0-a\",,,) failed, rc=%d (%#x)\n", rc, rc);
|
---|
| 117 | if (!rc && hMod2 != hMod)
|
---|
| 118 | Failure("kLdrDyldFindByName(\"tst-0-a\",,,) returned the wrong module handle: %p instead of %p\n",
|
---|
| 119 | (void *)hMod2, (void *)hMod);
|
---|
| 120 |
|
---|
| 121 | hMod2 = (HKLDRMOD)0xffffeeee;
|
---|
| 122 | rc = kLdrDyldFindByName("tst-0-d", NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hMod2);
|
---|
| 123 | if (!rc)
|
---|
| 124 | printf("tst-0-driver: hMod2=%p ('tst-0-d')\n", (void *)hMod2);
|
---|
| 125 | else
|
---|
| 126 | Failure("kLdrDyldFindByName(\"tst-0-d\",,,) failed, rc=%d (%#x)\n", rc, rc);
|
---|
| 127 |
|
---|
| 128 | /*
|
---|
| 129 | * Get the name and filename for each of the two modules.
|
---|
| 130 | */
|
---|
| 131 | rc = kLdrDyldGetName(hMod2, szBuf, sizeof(szBuf));
|
---|
| 132 | if (!rc)
|
---|
| 133 | {
|
---|
| 134 | printf("tst-0-driver: name: '%s' ('tst-0-d')\n", szBuf);
|
---|
| 135 | psz = strstr(szBuf, "-0-");
|
---|
| 136 | if ( !psz
|
---|
| 137 | || strnicmp(psz, "-0-d", sizeof("-0-d") - 1))
|
---|
| 138 | Failure("kLdrDyldGetName(\"tst-0-d\",,,) -> '%s': pattern '-0-d' not found\n", szBuf);
|
---|
| 139 |
|
---|
| 140 | /* overflow test. */
|
---|
| 141 | cch = strlen(szBuf);
|
---|
| 142 | szBuf[cch + 1] = szBuf[cch] = szBuf[cch - 1] = 'x';
|
---|
| 143 | szBuf[cch + 2] = '\0';
|
---|
| 144 | rc = kLdrDyldGetName(hMod2, szBuf, cch);
|
---|
[3584] | 145 | if (rc == KERR_BUFFER_OVERFLOW)
|
---|
[2870] | 146 | {
|
---|
| 147 | if (!szBuf[0])
|
---|
| 148 | Failure("kLdrDyldGetName didn't return partial result on overflow\n");
|
---|
| 149 | else if (szBuf[cch - 1])
|
---|
| 150 | Failure("kLdrDyldGetName didn't terminate partial result correctly overflow: '%s'\n", szBuf);
|
---|
| 151 | else if (szBuf[cch] != 'x')
|
---|
| 152 | Failure("kLdrDyldGetName exceeded the buffer limit on partial overflow: '%s'\n", szBuf);
|
---|
| 153 | }
|
---|
| 154 | else
|
---|
[3584] | 155 | Failure("kLdrDyldGetName(\"tst-0-d\",,,) -> rc=%d (%#x) instead of KERR_BUFFER_OVERFLOW\n", rc, rc);
|
---|
[2870] | 156 |
|
---|
| 157 | /* check that we can query the module by the returned name. */
|
---|
| 158 | rc = kLdrDyldGetName(hMod2, szBuf, sizeof(szBuf));
|
---|
| 159 | if (!rc)
|
---|
| 160 | {
|
---|
| 161 | hMod3 = (HKLDRMOD)0xffffeeee;
|
---|
| 162 | rc = kLdrDyldFindByName(szBuf, NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hMod3);
|
---|
| 163 | if (rc || hMod3 != hMod2)
|
---|
| 164 | Failure("kLdrDyldFindByName(\"%s\",,,) failed, rc=%d (%#x) hMod3=%p hMod2=%p\n",
|
---|
| 165 | szBuf, rc, rc, (void *)hMod3, (void *)hMod2);
|
---|
| 166 | }
|
---|
| 167 | else
|
---|
| 168 | Failure("kLdrDyldGetName(\"tst-0-d\",,,) failed (b), rc=%d (%#x)\n", rc, rc);
|
---|
| 169 | }
|
---|
| 170 | else
|
---|
| 171 | Failure("kLdrDyldGetName(\"tst-0-d\",,,) failed, rc=%d (%#x)\n", rc, rc);
|
---|
| 172 |
|
---|
| 173 | rc = kLdrDyldGetFilename(hMod2, szBuf, sizeof(szBuf));
|
---|
| 174 | if (!rc)
|
---|
| 175 | {
|
---|
| 176 | printf("tst-0-driver: filename: '%s' ('tst-0-d')\n", szBuf);
|
---|
| 177 |
|
---|
| 178 | /* overflow test. */
|
---|
| 179 | cch = strlen(szBuf);
|
---|
| 180 | szBuf[cch + 1] = szBuf[cch] = szBuf[cch - 1] = 'x';
|
---|
| 181 | szBuf[cch + 2] = '\0';
|
---|
| 182 | rc = kLdrDyldGetFilename(hMod2, szBuf, cch);
|
---|
[3584] | 183 | if (rc == KERR_BUFFER_OVERFLOW)
|
---|
[2870] | 184 | {
|
---|
| 185 | if (!szBuf[0])
|
---|
| 186 | Failure("kLdrDyldGetFilename didn't return partial result on overflow\n");
|
---|
| 187 | else if (szBuf[cch - 1])
|
---|
| 188 | Failure("kLdrDyldGetFilename didn't terminate partial result correctly overflow: '%s'\n", szBuf);
|
---|
| 189 | else if (szBuf[cch] != 'x')
|
---|
| 190 | Failure("kLdrDyldGetFilename exceeded the buffer limit on partial overflow: '%s'\n", szBuf);
|
---|
| 191 | }
|
---|
| 192 | else
|
---|
[3584] | 193 | Failure("kLdrDyldGetFilename(\"tst-0-d\",,,) -> rc=%d (%#x) instead of KERR_BUFFER_OVERFLOW\n", rc, rc);
|
---|
[2870] | 194 |
|
---|
| 195 | /* check that we can query the module by the returned filename. */
|
---|
| 196 | rc = kLdrDyldGetFilename(hMod2, szBuf, sizeof(szBuf));
|
---|
| 197 | if (!rc)
|
---|
| 198 | {
|
---|
| 199 | hMod3 = (HKLDRMOD)0xffffeeee;
|
---|
| 200 | rc = kLdrDyldFindByName(szBuf, NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hMod3);
|
---|
| 201 | if (rc || hMod3 != hMod2)
|
---|
| 202 | Failure("kLdrDyldFindByName(\"%s\",,,) failed, rc=%d (%#x) hMod3=%p hMod2=%p\n",
|
---|
| 203 | szBuf, rc, rc, (void *)hMod3, (void *)hMod2);
|
---|
| 204 | }
|
---|
| 205 | else
|
---|
| 206 | Failure("kLdrDyldGetName(\"tst-0-d\",,,) failed (b), rc=%d (%#x)\n", rc, rc);
|
---|
| 207 | }
|
---|
| 208 | else
|
---|
| 209 | Failure("kLdrDyldGetFilename(\"tst-0-d\",,,) failed, rc=%d (%#x)\n", rc, rc);
|
---|
| 210 |
|
---|
| 211 | /* the other module */
|
---|
| 212 | rc = kLdrDyldGetName(hMod, szBuf, sizeof(szBuf));
|
---|
| 213 | if (!rc)
|
---|
| 214 | {
|
---|
| 215 | printf("tst-0-driver: name: '%s' ('tst-0-a')\n", szBuf);
|
---|
| 216 | psz = strstr(szBuf, "-0-");
|
---|
| 217 | if ( !psz
|
---|
| 218 | || strnicmp(psz, "-0-a", sizeof("-0-a") - 1))
|
---|
| 219 | Failure("kLdrDyldGetName(\"tst-0-a\",,,) -> '%s': pattern '-0-a' not found\n", szBuf);
|
---|
| 220 |
|
---|
| 221 | /* check that we can query the module by the returned name. */
|
---|
| 222 | hMod3 = (HKLDRMOD)0xffffeeee;
|
---|
| 223 | rc = kLdrDyldFindByName(szBuf, NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hMod3);
|
---|
| 224 | if (rc || hMod3 != hMod)
|
---|
| 225 | Failure("kLdrDyldFindByName(\"%s\",,,) failed, rc=%d (%#x) hMod3=%p hMod=%p\n",
|
---|
| 226 | szBuf, rc, rc, (void *)hMod3, (void *)hMod);
|
---|
| 227 | }
|
---|
| 228 | else
|
---|
| 229 | Failure("kLdrDyldGetName(\"tst-0-a\",,,) failed, rc=%d (%#x)\n", rc, rc);
|
---|
| 230 |
|
---|
| 231 | rc = kLdrDyldGetFilename(hMod, szBuf, sizeof(szBuf));
|
---|
| 232 | if (!rc)
|
---|
| 233 | {
|
---|
| 234 | printf("tst-0-driver: filename: '%s' ('tst-0-a')\n", szBuf);
|
---|
| 235 |
|
---|
| 236 | /* check that we can query the module by the returned filename. */
|
---|
| 237 | hMod3 = (HKLDRMOD)0xffffeeee;
|
---|
| 238 | rc = kLdrDyldFindByName(szBuf, NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hMod3);
|
---|
| 239 | if (rc || hMod3 != hMod)
|
---|
| 240 | Failure("kLdrDyldFindByName(\"%s\",,,) failed, rc=%d (%#x) hMod3=%p hMod=%p\n",
|
---|
| 241 | szBuf, rc, rc, (void *)hMod3, (void *)hMod);
|
---|
| 242 | }
|
---|
| 243 | else
|
---|
| 244 | Failure("kLdrDyldGetFilename(\"tst-0-a\",,,) failed, rc=%d (%#x)\n", rc, rc);
|
---|
| 245 |
|
---|
| 246 |
|
---|
| 247 | /*
|
---|
| 248 | * Resolve the symbol exported by each of the two modules and call them.
|
---|
| 249 | */
|
---|
| 250 | if (!g_cErrors)
|
---|
| 251 | {
|
---|
[3584] | 252 | KUPTR uValue;
|
---|
| 253 | KU32 fKind;
|
---|
[2870] | 254 |
|
---|
| 255 | fKind = 0xffeeffee;
|
---|
[3584] | 256 | uValue = ~(KUPTR)42;
|
---|
| 257 | rc = kLdrDyldQuerySymbol(hMod, NIL_KLDRMOD_SYM_ORDINAL, MY_NAME("FuncA"), NULL, &uValue, &fKind);
|
---|
[2870] | 258 | if (!rc)
|
---|
| 259 | {
|
---|
[3584] | 260 | if (uValue == ~(KUPTR)42)
|
---|
[2870] | 261 | Failure("kLdrDyldQuerySymbol(\"tst-0-a\",,\"FuncA\",): uValue wasn't set.\n");
|
---|
| 262 | if (fKind == 0xffeeffee)
|
---|
| 263 | Failure("kLdrDyldQuerySymbol(\"tst-0-a\",,\"FuncA\",): fKind wasn't set.\n");
|
---|
| 264 | if ( (fKind & KLDRSYMKIND_BIT_MASK) != KLDRSYMKIND_NO_BIT
|
---|
| 265 | && (fKind & KLDRSYMKIND_BIT_MASK) != MY_KLDRSYMKIND_BITS)
|
---|
| 266 | Failure("fKind=%#x indicates a different code 'bit' mode than we running at.\n", fKind);
|
---|
| 267 | if ( (fKind & KLDRSYMKIND_TYPE_MASK) != KLDRSYMKIND_NO_TYPE
|
---|
| 268 | && (fKind & KLDRSYMKIND_TYPE_MASK) != KLDRSYMKIND_CODE)
|
---|
| 269 | Failure("fKind=%#x indicates that \"FuncA\" isn't code.\n", fKind);
|
---|
| 270 | if (fKind & KLDRSYMKIND_FORWARDER)
|
---|
| 271 | Failure("fKind=%#x indicates that \"FuncA\" is a forwarder. it isn't.\n", fKind);
|
---|
| 272 |
|
---|
| 273 | /* call it. */
|
---|
| 274 | if (!g_cErrors)
|
---|
| 275 | {
|
---|
| 276 | int (*pfnFuncA)(void) = (int (*)(void))uValue;
|
---|
| 277 | rc = pfnFuncA();
|
---|
| 278 | if (rc != 0x42000042)
|
---|
| 279 | Failure("FuncA returned %#x expected 0x42000042\n", rc);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | /*
|
---|
| 283 | * Test kLdrDyldFindByAddress now that we've got an address.
|
---|
| 284 | */
|
---|
| 285 | hMod3 = (HKLDRMOD)0xeeeeffff;
|
---|
| 286 | rc = kLdrDyldFindByAddress(uValue, &hMod3, NULL, NULL);
|
---|
| 287 | if (!rc)
|
---|
| 288 | {
|
---|
[3584] | 289 | KUPTR offSegment;
|
---|
| 290 | KU32 iSegment;
|
---|
[2870] | 291 |
|
---|
| 292 | if (hMod3 != hMod)
|
---|
| 293 | Failure("kLdrDyldFindByAddress(%#p/*FuncA*/, \"tst-0-a\",,,) return incorrect hMod3=%p instead of %p.\n",
|
---|
| 294 | uValue, hMod3, hMod);
|
---|
| 295 |
|
---|
| 296 | hMod3 = (HKLDRMOD)0xeeeeffff;
|
---|
| 297 | iSegment = 0x42424242;
|
---|
| 298 | rc = kLdrDyldFindByAddress(uValue, &hMod3, &iSegment, &offSegment);
|
---|
| 299 | if (!rc)
|
---|
| 300 | {
|
---|
| 301 | if (hMod3 != hMod)
|
---|
| 302 | Failure("Bad hMod3 on 2nd kLdrDyldFindByAddress call.\n");
|
---|
| 303 | if (iSegment > 0x1000) /* safe guess */
|
---|
| 304 | Failure("Bad iSegment=%#x\n", iSegment);
|
---|
| 305 | if (offSegment > 0x100000) /* guesswork */
|
---|
| 306 | Failure("Bad offSegment=%p\n", (void *)offSegment);
|
---|
| 307 | }
|
---|
| 308 | else
|
---|
| 309 | Failure("kLdrDyldFindByAddress(%#p/*FuncA*/, \"tst-0-a\",,,) failed (b), rc=%d (%#x)\n",
|
---|
| 310 | uValue, rc, rc);
|
---|
| 311 |
|
---|
| 312 | /* negative test */
|
---|
| 313 | hMod3 = (HKLDRMOD)0xeeeeffff;
|
---|
| 314 | iSegment = 0x42424242;
|
---|
| 315 | offSegment = 0x87654321;
|
---|
[3584] | 316 | rc = kLdrDyldFindByAddress(~(KUPTR)16, &hMod3, &iSegment, &offSegment);
|
---|
[2870] | 317 | if (!rc)
|
---|
| 318 | Failure("negative kLdrDyldFindByAddress test returned successfully!\n");
|
---|
[3584] | 319 | if (iSegment != ~(KU32)0)
|
---|
[2870] | 320 | Failure("negative kLdrDyldFindByAddress: bad iSegment=%#x\n", iSegment);
|
---|
[3584] | 321 | if (offSegment != ~(KUPTR)0)
|
---|
[2870] | 322 | Failure("negative kLdrDyldFindByAddress: bad offSegment=%p\n", (void *)offSegment);
|
---|
| 323 | if (hMod3 != NIL_HKLDRMOD)
|
---|
| 324 | Failure("negative kLdrDyldFindByAddress: bad hMod3=%p\n", (void *)hMod3);
|
---|
| 325 | }
|
---|
| 326 | else
|
---|
| 327 | Failure("kLdrDyldFindByAddress(%#p/*FuncA*/, \"tst-0-a\",,,) failed, rc=%d (%#x)\n",
|
---|
| 328 | uValue, rc, rc);
|
---|
| 329 | }
|
---|
| 330 | else
|
---|
| 331 | Failure("kLdrDyldQuerySymbol(\"tst-0-a\",,\"FuncA\",) failed, rc=%d (%#x)\n", rc, rc);
|
---|
| 332 |
|
---|
| 333 | fKind = 0xffeeffee;
|
---|
[3584] | 334 | uValue = ~(KUPTR)42;
|
---|
| 335 | rc = kLdrDyldQuerySymbol(hMod2, NIL_KLDRMOD_SYM_ORDINAL, MY_NAME("FuncD"), NULL, &uValue, &fKind);
|
---|
[2870] | 336 | if (!rc)
|
---|
| 337 | {
|
---|
[3584] | 338 | if (uValue == ~(KUPTR)42)
|
---|
[2870] | 339 | Failure("kLdrDyldQuerySymbol(\"tst-0-d\",,\"FuncD\",): uValue wasn't set.\n");
|
---|
| 340 | if (fKind == 0xffeeffee)
|
---|
| 341 | Failure("kLdrDyldQuerySymbol(\"tst-0-d\",,\"FuncD\",): fKind wasn't set.\n");
|
---|
| 342 | if ( (fKind & KLDRSYMKIND_BIT_MASK) != KLDRSYMKIND_NO_BIT
|
---|
| 343 | && (fKind & KLDRSYMKIND_BIT_MASK) != MY_KLDRSYMKIND_BITS)
|
---|
| 344 | Failure("fKind=%#x indicates a different code 'bit' mode than we running at.\n", fKind);
|
---|
| 345 | if ( (fKind & KLDRSYMKIND_TYPE_MASK) != KLDRSYMKIND_NO_TYPE
|
---|
| 346 | && (fKind & KLDRSYMKIND_TYPE_MASK) != KLDRSYMKIND_CODE)
|
---|
| 347 | Failure("fKind=%#x indicates that \"FuncD\" isn't code.\n", fKind);
|
---|
| 348 | if (fKind & KLDRSYMKIND_FORWARDER)
|
---|
| 349 | Failure("fKind=%#x indicates that \"FuncD\" is a forwarder. it isn't.\n", fKind);
|
---|
| 350 |
|
---|
| 351 | /* call it. */
|
---|
| 352 | if (!g_cErrors)
|
---|
| 353 | {
|
---|
| 354 | int (*pfnFuncD)(void) = (int (*)(void))uValue;
|
---|
| 355 | rc = pfnFuncD();
|
---|
| 356 | if (rc != 0x42000000)
|
---|
| 357 | Failure("FuncD returned %#x expected 0x42000000\n", rc);
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | /* use the address to get the module handle. */
|
---|
| 361 | hMod3 = (HKLDRMOD)0xeeeeffff;
|
---|
| 362 | rc = kLdrDyldFindByAddress(uValue, &hMod3, NULL, NULL);
|
---|
| 363 | if (!rc)
|
---|
| 364 | {
|
---|
| 365 | if (hMod3 != hMod2)
|
---|
| 366 | Failure("kLdrDyldFindByAddress(%#p/*FuncD*/,,,) return incorrect hMod3=%p instead of %p.\n",
|
---|
| 367 | uValue, hMod3, hMod2);
|
---|
| 368 | }
|
---|
| 369 | else
|
---|
| 370 | Failure("kLdrDyldFindByAddress(%#p/*FuncD*/,,,) failed, rc=%d (%#x)\n",
|
---|
| 371 | uValue, rc, rc);
|
---|
| 372 | }
|
---|
| 373 | else
|
---|
| 374 | Failure("kLdrDyldQuerySymbol(\"tst-0-a\",,\"FuncA\",) failed, rc=%d (%#x)\n", rc, rc);
|
---|
| 375 |
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | /*
|
---|
| 379 | * Finally unload it.
|
---|
| 380 | */
|
---|
[2864] | 381 | rc = kLdrDyldUnload(hMod);
|
---|
| 382 | if (rc)
|
---|
| 383 | Failure("kLdrDyldUnload() failed. rc=%d (%#x)\n", rc, rc);
|
---|
[2870] | 384 | if (!rc)
|
---|
| 385 | {
|
---|
| 386 | rc = kLdrDyldFindByName("tst-0-d", NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hMod2);
|
---|
| 387 | if (rc != KLDR_ERR_MODULE_NOT_FOUND)
|
---|
| 388 | Failure("kLdrDyldFindByName(\"tst-0-d\",,,) return rc=%d (%#x), expected KLDR_ERR_MODULE_NOT_FOUND\n", rc, rc);
|
---|
| 389 |
|
---|
| 390 | rc = kLdrDyldFindByName("tst-0-a", NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hMod2);
|
---|
| 391 | if (rc != KLDR_ERR_MODULE_NOT_FOUND)
|
---|
| 392 | Failure("kLdrDyldFindByName(\"tst-0-a\",,,) return rc=%d (%#x), expected KLDR_ERR_MODULE_NOT_FOUND\n", rc, rc);
|
---|
| 393 | }
|
---|
[2864] | 394 | }
|
---|
| 395 |
|
---|
| 396 | /*
|
---|
[2871] | 397 | * Now do what tst-0 would do; load the three dlls, resolve and call their functions.
|
---|
[2870] | 398 | */
|
---|
| 399 | if (!g_cErrors)
|
---|
| 400 | {
|
---|
[2871] | 401 | HKLDRMOD hModA;
|
---|
| 402 | int (*pfnFuncA)(void);
|
---|
| 403 | HKLDRMOD hModB;
|
---|
| 404 | int (*pfnFuncB)(void);
|
---|
| 405 | HKLDRMOD hModC;
|
---|
| 406 | int (*pfnFuncC)(void);
|
---|
[3584] | 407 | KUPTR uValue;
|
---|
[2870] | 408 |
|
---|
[2871] | 409 | rc = kLdrDyldLoad("tst-0-a", NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hModA, NULL, 0);
|
---|
| 410 | if (rc)
|
---|
| 411 | Failure("kLdrDyldLoad(\"tst-0-a\",,,,) -> %d (%#x)\n", rc, rc);
|
---|
| 412 | if (!rc)
|
---|
| 413 | {
|
---|
| 414 | rc = kLdrDyldLoad("tst-0-b", NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hModB, szErr, sizeof(szErr));
|
---|
| 415 | if (rc)
|
---|
| 416 | Failure("kLdrDyldLoad(\"tst-0-b\",,,,) -> %d (%#x) szErr='%s'\n", rc, rc, szErr);
|
---|
| 417 | }
|
---|
| 418 | if (!rc)
|
---|
| 419 | {
|
---|
| 420 | rc = kLdrDyldLoad("tst-0-c", NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hModC, szErr, sizeof(szErr));
|
---|
| 421 | if (rc)
|
---|
| 422 | Failure("kLdrDyldLoad(\"tst-0-c\",,,,) -> %d (%#x) szErr='%s'\n", rc, rc, szErr);
|
---|
| 423 | }
|
---|
| 424 | if (!rc)
|
---|
| 425 | {
|
---|
[3584] | 426 | rc = kLdrDyldQuerySymbol(hModA, NIL_KLDRMOD_SYM_ORDINAL, MY_NAME("FuncA"), NULL, &uValue, NULL);
|
---|
[2871] | 427 | if (!rc)
|
---|
| 428 | pfnFuncA = (int (*)(void))uValue;
|
---|
| 429 | else
|
---|
| 430 | Failure("kLdrDyldQuerySymbol(,,\"FuncA\",,) -> %d (%#x)\n", rc, rc);
|
---|
| 431 | }
|
---|
| 432 | if (!rc)
|
---|
| 433 | {
|
---|
[3584] | 434 | rc = kLdrDyldQuerySymbol(hModB, NIL_KLDRMOD_SYM_ORDINAL, MY_NAME("FuncB"), NULL, &uValue, NULL);
|
---|
[2871] | 435 | if (!rc)
|
---|
| 436 | pfnFuncB = (int (*)(void))uValue;
|
---|
| 437 | else
|
---|
| 438 | Failure("kLdrDyldQuerySymbol(,,\"FuncB\",,) -> %d (%#x)\n", rc, rc);
|
---|
| 439 | }
|
---|
| 440 | if (!rc)
|
---|
| 441 | {
|
---|
[3584] | 442 | rc = kLdrDyldQuerySymbol(hModC, NIL_KLDRMOD_SYM_ORDINAL, MY_NAME("FuncC"), NULL, &uValue, NULL);
|
---|
[2871] | 443 | if (!rc)
|
---|
| 444 | pfnFuncC = (int (*)(void))uValue;
|
---|
| 445 | else
|
---|
| 446 | Failure("kLdrDyldQuerySymbol(,,\"FuncA\",,) -> %d (%#x)\n", rc, rc);
|
---|
| 447 | }
|
---|
| 448 | if (!rc)
|
---|
| 449 | {
|
---|
[2872] | 450 | int u = pfnFuncA() | pfnFuncB() | pfnFuncC();
|
---|
| 451 | if (u == 0x42424242)
|
---|
| 452 | printf("tst-0-driver: FuncA/B/C => %#x (correct)\n", u);
|
---|
[2871] | 453 | else
|
---|
[2872] | 454 | Failure("FuncA/B/C => %#x\n", u);
|
---|
| 455 |
|
---|
| 456 | rc = kLdrDyldUnload(hModA);
|
---|
| 457 | if (rc)
|
---|
| 458 | Failure("Unload A failed, rc=%d (%#x)\n", rc, rc);
|
---|
| 459 | u = pfnFuncB() | pfnFuncC();
|
---|
| 460 | if (u != 0x42424200)
|
---|
| 461 | Failure("FuncB/C returns %#x instead of 0x42424200 after unloading A\n", u);
|
---|
| 462 |
|
---|
| 463 | rc = kLdrDyldUnload(hModB);
|
---|
| 464 | if (rc)
|
---|
| 465 | Failure("Unload B failed, rc=%d (%#x)\n", rc, rc);
|
---|
| 466 | u = pfnFuncC();
|
---|
| 467 | if (u != 0x42420000)
|
---|
| 468 | Failure("FuncC returns %#x instead of 0x42420000 after unloading A\n", u);
|
---|
| 469 |
|
---|
| 470 | rc = kLdrDyldUnload(hModC);
|
---|
| 471 | if (rc)
|
---|
| 472 | Failure("Unload C failed, rc=%d (%#x)\n", rc, rc);
|
---|
| 473 |
|
---|
| 474 | rc = kLdrDyldFindByName("tst-0-d", NULL, NULL, KLDRDYLD_SEARCH_HOST, 0, &hMod);
|
---|
| 475 | if (rc != KLDR_ERR_MODULE_NOT_FOUND)
|
---|
| 476 | Failure("Query for \"tst-0-d\" after unloading A,B and C returns rc=%d (%#x) instead of KLDR_ERR_MODULE_NOT_FOUND\n",
|
---|
| 477 | rc, rc);
|
---|
[2871] | 478 | }
|
---|
[2870] | 479 | }
|
---|
| 480 |
|
---|
| 481 | /*
|
---|
[2872] | 482 | * Now invoke the executable stub which launches the tst-0 program.
|
---|
| 483 | */
|
---|
| 484 | if (!g_cErrors)
|
---|
| 485 | {
|
---|
| 486 | /// @todo
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | /*
|
---|
[2864] | 490 | * Summary
|
---|
| 491 | */
|
---|
| 492 | if (!g_cErrors)
|
---|
[2871] | 493 | printf("tst-0-driver: SUCCESS\n");
|
---|
[2864] | 494 | else
|
---|
[2871] | 495 | printf("tst-0-driver: FAILURE - %d errors\n", g_cErrors);
|
---|
[2864] | 496 | return !!g_cErrors;
|
---|
| 497 | }
|
---|
| 498 |
|
---|