| 1 | /* $Id: tst-3-driver.c 29 2009-07-01 20:30:29Z bird $ */ | 
|---|
| 2 | /** @file | 
|---|
| 3 | * kLdr - Dynamic Loader testcase no. 3, 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 <k/kErr.h> | 
|---|
| 36 |  | 
|---|
| 37 | #include <stdarg.h> | 
|---|
| 38 | #include <stdio.h> | 
|---|
| 39 | #include <stdlib.h> | 
|---|
| 40 | #include <string.h> | 
|---|
| 41 | #ifdef _MSC_VER | 
|---|
| 42 | # include <malloc.h> | 
|---|
| 43 | #endif | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | /******************************************************************************* | 
|---|
| 47 | *   Defined Constants And Macros                                               * | 
|---|
| 48 | *******************************************************************************/ | 
|---|
| 49 | /** Select the appropriate KLDRSYMKIND bit define. */ | 
|---|
| 50 | #define MY_KLDRSYMKIND_BITS     ( sizeof(void *) == 4 ? KLDRSYMKIND_32BIT : KLDRSYMKIND_64BIT ) | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | /******************************************************************************* | 
|---|
| 54 | *   Global Variables                                                           * | 
|---|
| 55 | *******************************************************************************/ | 
|---|
| 56 | /** The numbers of errors. */ | 
|---|
| 57 | static int g_cErrors = 0; | 
|---|
| 58 |  | 
|---|
| 59 |  | 
|---|
| 60 | /** | 
|---|
| 61 | * Report failure. | 
|---|
| 62 | */ | 
|---|
| 63 | static int Failure(const char *pszFormat, ...) | 
|---|
| 64 | { | 
|---|
| 65 | va_list va; | 
|---|
| 66 |  | 
|---|
| 67 | g_cErrors++; | 
|---|
| 68 |  | 
|---|
| 69 | printf("tst-3-driver: "); | 
|---|
| 70 | va_start(va, pszFormat); | 
|---|
| 71 | vprintf(pszFormat, va); | 
|---|
| 72 | va_end(va); | 
|---|
| 73 | printf("\n"); | 
|---|
| 74 | return 1; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 | * External symbol used by the testcase module. | 
|---|
| 80 | */ | 
|---|
| 81 | static int Tst3Ext(int iFortyTwo) | 
|---|
| 82 | { | 
|---|
| 83 | if (iFortyTwo != 42) | 
|---|
| 84 | return 256; | 
|---|
| 85 | return 42; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 89 | /** | 
|---|
| 90 | * Callback for resolving the Tst3Ext import. | 
|---|
| 91 | */ | 
|---|
| 92 | static int GetImport(PKLDRMOD pMod, KU32 iImport, KU32 iSymbol, const char *pchSymbol, KSIZE cchSymbol, | 
|---|
| 93 | const char *pszVersion, PKLDRADDR puValue, KU32 *pfKind, void *pvUser) | 
|---|
| 94 | { | 
|---|
| 95 | if (*pfKind != KLDRSYMKIND_REQ_FLAT) | 
|---|
| 96 | return -1; | 
|---|
| 97 |  | 
|---|
| 98 | if (    !strncmp(pchSymbol, "Tst3Ext", strlen("Tst3Ext")) | 
|---|
| 99 | ||  !strncmp(pchSymbol, "_Tst3Ext", strlen("_Tst3Ext"))) | 
|---|
| 100 | { | 
|---|
| 101 | *puValue = (KUPTR)&Tst3Ext; | 
|---|
| 102 | *pfKind = KLDRSYMKIND_CODE | (sizeof(pfKind) == 4 ? KLDRSYMKIND_32BIT : KLDRSYMKIND_64BIT); | 
|---|
| 103 | return 0; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | return -2; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 | /** | 
|---|
| 111 | * Performs the tests on one module. | 
|---|
| 112 | * @returns non sense. | 
|---|
| 113 | */ | 
|---|
| 114 | int TestModule(const char *pszFile) | 
|---|
| 115 | { | 
|---|
| 116 | PKLDRMOD pMod; | 
|---|
| 117 | KLDRSIZE cbImage; | 
|---|
| 118 | void *pvBits; | 
|---|
| 119 | int rc; | 
|---|
| 120 |  | 
|---|
| 121 | printf("tst-3-driver: testing '%s'...\n", pszFile); | 
|---|
| 122 |  | 
|---|
| 123 | /* open it. */ | 
|---|
| 124 | rc = kLdrModOpen(pszFile, &pMod); | 
|---|
| 125 | if (rc) | 
|---|
| 126 | return Failure("kLdrModOpen(%s,) -> %#d (%s)\n", pszFile, rc, kErrName(rc)); | 
|---|
| 127 |  | 
|---|
| 128 | /* get bits. */ | 
|---|
| 129 | cbImage = kLdrModSize(pMod); | 
|---|
| 130 | pvBits = malloc((KSIZE)cbImage + 0xfff); | 
|---|
| 131 | if (pvBits) | 
|---|
| 132 | { | 
|---|
| 133 | void *pvBits2 = (void *)( ((KUPTR)pvBits + 0xfff) & ~(KUPTR)0xfff ); | 
|---|
| 134 |  | 
|---|
| 135 | KLDRADDR BaseAddress = (KUPTR)pvBits2; | 
|---|
| 136 | rc = kLdrModGetBits(pMod, pvBits2, BaseAddress, GetImport, NULL); | 
|---|
| 137 | if (!rc) | 
|---|
| 138 | { | 
|---|
| 139 | KLDRADDR EntryPoint; | 
|---|
| 140 |  | 
|---|
| 141 | /* call into it */ | 
|---|
| 142 | rc = kLdrModQuerySymbol(pMod, pvBits2, BaseAddress, NIL_KLDRMOD_SYM_ORDINAL, "_Tst3", strlen("_Tst3"), NULL, NULL, NULL, | 
|---|
| 143 | &EntryPoint, NULL); | 
|---|
| 144 | if (rc == KLDR_ERR_SYMBOL_NOT_FOUND) | 
|---|
| 145 | rc = kLdrModQuerySymbol(pMod, pvBits2, BaseAddress, NIL_KLDRMOD_SYM_ORDINAL, "Tst3", strlen("Tst3"), NULL, NULL, NULL, | 
|---|
| 146 | &EntryPoint, NULL); | 
|---|
| 147 | if (!rc) | 
|---|
| 148 | { | 
|---|
| 149 | int (*pfnEntryPoint)(int) = (int (*)(int)) ((KUPTR)EntryPoint); | 
|---|
| 150 | rc = pfnEntryPoint(42); | 
|---|
| 151 | if (rc == 42) | 
|---|
| 152 | { | 
|---|
| 153 | /* relocate twice and try again. */ | 
|---|
| 154 | rc = kLdrModRelocateBits(pMod, pvBits2, BaseAddress + 0x22000, BaseAddress, GetImport, NULL); | 
|---|
| 155 | if (!rc) | 
|---|
| 156 | { | 
|---|
| 157 | rc = kLdrModRelocateBits(pMod, pvBits2, BaseAddress, BaseAddress + 0x22000, GetImport, NULL); | 
|---|
| 158 | if (!rc) | 
|---|
| 159 | { | 
|---|
| 160 | rc = pfnEntryPoint(42); | 
|---|
| 161 | if (rc == 42) | 
|---|
| 162 | { | 
|---|
| 163 | printf("tst-3-driver: success.\n"); | 
|---|
| 164 | } | 
|---|
| 165 | else | 
|---|
| 166 | Failure("pfnEntryPoint(42) -> %d (2nd)\n", rc); | 
|---|
| 167 | } | 
|---|
| 168 | else | 
|---|
| 169 | Failure("kLdrModRelocateBits(,,, + 0x22000,,,) -> %#x (%s)\n", rc, kErrName(rc)); | 
|---|
| 170 | } | 
|---|
| 171 | else | 
|---|
| 172 | Failure("kLdrModRelocateBits(,, + 0x22000,,,,) -> %#x (%s)\n", rc, kErrName(rc)); | 
|---|
| 173 | } | 
|---|
| 174 | else | 
|---|
| 175 | Failure("pfnEntryPoint(42) -> %d (1st)\n", rc); | 
|---|
| 176 | } | 
|---|
| 177 | else | 
|---|
| 178 | Failure("kLdrModQuerySymbol -> %#x (%s)\n", rc, kErrName(rc)); | 
|---|
| 179 | } | 
|---|
| 180 | else | 
|---|
| 181 | Failure("kLdrModGetBits -> %#x (%s)\n", rc, kErrName(rc)); | 
|---|
| 182 | free(pvBits); | 
|---|
| 183 | } | 
|---|
| 184 | else | 
|---|
| 185 | Failure("malloc(%lx) -> NULL\n", (long)cbImage); | 
|---|
| 186 |  | 
|---|
| 187 | /* clean up */ | 
|---|
| 188 | rc = kLdrModClose(pMod); | 
|---|
| 189 | if (rc) | 
|---|
| 190 | Failure("kLdrModOpen(%s,) -> %#x (%s)\n", pszFile, rc, kErrName(rc)); | 
|---|
| 191 | return 0; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 |  | 
|---|
| 195 | int main(int argc, char **argv) | 
|---|
| 196 | { | 
|---|
| 197 | int i; | 
|---|
| 198 |  | 
|---|
| 199 | /* | 
|---|
| 200 | * Test all the given modules (requires arguments). | 
|---|
| 201 | */ | 
|---|
| 202 | for (i = 1; i < argc; i++) | 
|---|
| 203 | { | 
|---|
| 204 | TestModule(argv[i]); | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 |  | 
|---|
| 208 | /* | 
|---|
| 209 | * Summary | 
|---|
| 210 | */ | 
|---|
| 211 | if (!g_cErrors) | 
|---|
| 212 | printf("tst-3-driver: SUCCESS\n"); | 
|---|
| 213 | else | 
|---|
| 214 | printf("tst-3-driver: FAILURE - %d errors\n", g_cErrors); | 
|---|
| 215 | return !!g_cErrors; | 
|---|
| 216 | } | 
|---|