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