Changeset 2865 for trunk/src


Ignore:
Timestamp:
Sep 3, 2016, 12:17:13 AM (9 years ago)
Author:
bird
Message:

fixed exception in c1xx.dll/msvcrt/memcpy.

Location:
trunk/src/kWorker
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kWorker/kWorker.c

    r2860 r2865  
    164164            /** The state. */
    165165            KWMODSTATE          enmState;
     166#if defined(KBUILD_OS_WINDOWS) && defined(KBUILD_ARCH_AMD64)
     167            /** The number of entries in the table. */
     168            KU32                cFunctions;
     169            /** The function table address (in the copy). */
     170            PRUNTIME_FUNCTION   paFunctions;
     171            /** Set if we've already registered a function table already. */
     172            KBOOL               fRegisteredFunctionTable;
     173#endif
     174            /** Set if we share memory with other executables. */
     175            KBOOL               fUseLdBuf;
    166176            /** Number of imported modules. */
    167177            KSIZE               cImpMods;
     
    431441static KWSANDBOX    g_Sandbox;
    432442
     443/** The module currently occupying g_abDefLdBuf. */
     444static PKWMODULE    g_pModInLdBuf = NULL;
     445
    433446/** Module hash table. */
    434447static PKWMODULE    g_apModules[127];
     
    12191232                    pMod->pLdrMod       = pLdrMod;
    12201233                    pMod->u.Manual.cImpMods = (KU32)cImports;
     1234                    pMod->u.Manual.fUseLdBuf = K_FALSE;
     1235#if defined(KBUILD_OS_WINDOWS) && defined(KBUILD_ARCH_AMD64)
     1236                    pMod->u.Manual.fRegisteredFunctionTable = K_FALSE;
     1237#endif
    12211238                    pMod->pszPath       = (char *)kHlpMemCopy(&pMod->u.Manual.apImpMods[cImports + 1], pszPath, cbPath);
    12221239                    pMod->pwszPath      = (wchar_t *)(pMod->pszPath + cbPath + (cbPath & 1));
     
    12311248                    pMod->u.Manual.cbImage = kLdrModSize(pLdrMod);
    12321249                    if (   !fFixed
     1250                        || pLdrMod->enmType != KLDRTYPE_EXECUTABLE_FIXED /* only allow fixed executables */
    12331251                        || (KUPTR)pMod->u.Manual.pvLoad - (KUPTR)g_abDefLdBuf >= sizeof(g_abDefLdBuf)
    12341252                        || sizeof(g_abDefLdBuf) - (KUPTR)pMod->u.Manual.pvLoad - (KUPTR)g_abDefLdBuf < pMod->u.Manual.cbImage)
    12351253                        rc = kHlpPageAlloc(&pMod->u.Manual.pvLoad, pMod->u.Manual.cbImage, KPROT_EXECUTE_READWRITE, fFixed);
     1254                    else
     1255                        pMod->u.Manual.fUseLdBuf = K_TRUE;
    12361256                    if (rc == 0)
    12371257                    {
     
    12711291                                if (rc == 0)
    12721292                                {
     1293#if defined(KBUILD_OS_WINDOWS) && defined(KBUILD_ARCH_AMD64)
     1294                                    /*
     1295                                     * Find the function table.  No validation here because the
     1296                                     * loader did that already, right...
     1297                                     */
     1298                                    KU8                        *pbImg = (KU8 *)pMod->u.Manual.pvCopy;
     1299                                    IMAGE_NT_HEADERS const     *pNtHdrs;
     1300                                    IMAGE_DATA_DIRECTORY const *pXcptDir;
     1301                                    if (((PIMAGE_DOS_HEADER)pbImg)->e_magic == IMAGE_DOS_SIGNATURE)
     1302                                        pNtHdrs = (PIMAGE_NT_HEADERS)&pbImg[((PIMAGE_DOS_HEADER)pbImg)->e_lfanew];
     1303                                    else
     1304                                        pNtHdrs = (PIMAGE_NT_HEADERS)pbImg;
     1305                                    pXcptDir = &pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION];
     1306                                    kHlpAssert(pNtHdrs->Signature == IMAGE_NT_SIGNATURE);
     1307                                    if (pXcptDir->Size > 0)
     1308                                    {
     1309                                        pMod->u.Manual.cFunctions  = pXcptDir->Size / sizeof(pMod->u.Manual.paFunctions[0]);
     1310                                        kHlpAssert(   pMod->u.Manual.cFunctions * sizeof(pMod->u.Manual.paFunctions[0])
     1311                                                   == pXcptDir->Size);
     1312                                        pMod->u.Manual.paFunctions = (PRUNTIME_FUNCTION)&pbImg[pXcptDir->VirtualAddress];
     1313                                    }
     1314                                    else
     1315                                    {
     1316                                        pMod->u.Manual.cFunctions  = 0;
     1317                                        pMod->u.Manual.paFunctions = NULL;
     1318                                    }
     1319#endif
     1320
     1321                                    /*
     1322                                     * Final finish.
     1323                                     */
    12731324                                    pMod->u.Manual.pvBits = pMod->u.Manual.pvCopy;
    12741325                                    pMod->u.Manual.enmState = KWMODSTATE_NEEDS_BITS;
     
    15771628        if (pMod->u.Manual.enmState == KWMODSTATE_NEEDS_BITS)
    15781629        {
     1630            if (pMod->u.Manual.fUseLdBuf)
     1631            {
     1632#if defined(KBUILD_OS_WINDOWS) && defined(KBUILD_ARCH_AMD64)
     1633                if (g_pModInLdBuf != NULL && g_pModInLdBuf != pMod && pMod->u.Manual.fRegisteredFunctionTable)
     1634                {
     1635                    BOOLEAN fRc = RtlDeleteFunctionTable(pMod->u.Manual.paFunctions);
     1636                    kHlpAssert(fRc); K_NOREF(fRc);
     1637                }
     1638#endif
     1639                g_pModInLdBuf = pMod;
     1640            }
     1641
    15791642            kHlpMemCopy(pMod->u.Manual.pvLoad, pMod->u.Manual.pvCopy, pMod->u.Manual.cbImage);
    15801643            pMod->u.Manual.enmState = KWMODSTATE_NEEDS_INIT;
    15811644        }
     1645
     1646#if defined(KBUILD_OS_WINDOWS) && defined(KBUILD_ARCH_AMD64)
     1647        /* Need to register function table? */
     1648        if (   !pMod->u.Manual.fRegisteredFunctionTable
     1649            && pMod->u.Manual.cFunctions > 0)
     1650        {
     1651            pMod->u.Manual.fRegisteredFunctionTable = RtlAddFunctionTable(pMod->u.Manual.paFunctions,
     1652                                                                          pMod->u.Manual.cFunctions,
     1653                                                                          (KUPTR)pMod->u.Manual.pvLoad) != FALSE;
     1654            kHlpAssert(pMod->u.Manual.fRegisteredFunctionTable);
     1655        }
     1656#endif
    15821657
    15831658        if (pMod->u.Manual.enmState == KWMODSTATE_NEEDS_INIT)
     
    40664141
    40674142
     4143/*
     4144 *
     4145 * Misc function only intercepted while debugging.
     4146 * Misc function only intercepted while debugging.
     4147 * Misc function only intercepted while debugging.
     4148 *
     4149 */
     4150
     4151#ifndef NDEBUG
     4152
     4153/** CRT - memcpy   */
     4154static void * __cdecl kwSandbox_msvcrt_memcpy(void *pvDst, void const *pvSrc, size_t cb)
     4155{
     4156    KU8 const *pbSrc = (KU8 const *)pvSrc;
     4157    KU8       *pbDst = (KU8 *)pvDst;
     4158    KSIZE      cbLeft = cb;
     4159    while (cbLeft-- > 0)
     4160        *pbDst++ = *pbSrc++;
     4161    return pvDst;
     4162}
     4163
     4164#endif /* NDEBUG */
     4165
     4166
     4167
    40684168/**
    40694169 * Functions that needs replacing for sandboxed execution.
     
    41724272    { TUPLE("__p__environ"),                NULL,       (KUPTR)kwSandbox_msvcrt___p__environ },
    41734273    { TUPLE("__p__wenviron"),               NULL,       (KUPTR)kwSandbox_msvcrt___p__wenviron },
     4274
     4275#ifndef NDEBUG
     4276    { TUPLE("memcpy"),                      NULL,       (KUPTR)kwSandbox_msvcrt_memcpy },
     4277#endif
    41744278};
    41754279/** Number of entries in g_aReplacements. */
     
    44554559
    44564560
    4457 static int kwSandboxExec(PKWTOOL pTool, KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange,
     4561static int kwSandboxExec(PKWSANDBOX pSandbox, PKWTOOL pTool, KU32 cArgs, const char **papszArgs, KBOOL fWatcomBrainDamange,
    44584562                         KU32 cEnvVars, const char **papszEnvVars)
    44594563{
     
    45214625    return rcExit;
    45224626}
    4523 
    4524 
    4525 #if 0
    4526 static int kwExecCmdLine(const char *pszExe, const char *pszCmdLine)
    4527 {
    4528     int rc;
    4529     PKWTOOL pTool = kwToolLookup(pszExe);
    4530     if (pTool)
    4531     {
    4532         int rc;
    4533         int rcExitCode;
    4534         switch (pTool->enmType)
    4535         {
    4536             case KWTOOLTYPE_SANDBOXED:
    4537                 KW_LOG(("Sandboxing tool %s\n", pTool->pszPath));
    4538                 rc = kwSandboxExec(pTool, pszCmdLine, &rcExitCode);
    4539                 break;
    4540 
    4541             case KWTOOLTYPE_WATCOM:
    4542                 KW_LOG(("TODO: Watcom style tool %s\n", pTool->pszPath));
    4543                 rc = rcExitCode = 2;
    4544                 break;
    4545 
    4546             case KWTOOLTYPE_EXEC:
    4547                 KW_LOG(("TODO: Direct exec tool %s\n", pTool->pszPath));
    4548                 rc = rcExitCode = 2;
    4549                 break;
    4550 
    4551             default:
    4552                 kHlpAssertFailed();
    4553                 rc = rcExitCode = 2;
    4554                 break;
    4555         }
    4556         KW_LOG(("rcExitCode=%d (rc=%d)\n", rcExitCode, rc));
    4557     }
    4558     else
    4559         rc = 1;
    4560     return rc;
    4561 }
    4562 #endif
    45634627
    45644628
     
    46204684                {
    46214685                    KW_LOG(("Sandboxing tool %s\n", pTool->pszPath));
    4622                     rcExit = kwSandboxExec(pTool, cArgs, papszArgs, fWatcomBrainDamange, cEnvVars, papszEnvVars);
     4686                    rcExit = kwSandboxExec(&g_Sandbox, pTool, cArgs, papszArgs, fWatcomBrainDamange, cEnvVars, papszEnvVars);
    46234687                }
    46244688                else
     
    49505014}
    49515015
    4952 
     5016#if 1
    49535017
    49545018int main(int argc, char **argv)
    49555019{
    4956 #if 1
    49575020    KSIZE   cbMsgBuf = 0;
    49585021    KU8    *pbMsgBuf = NULL;
     
    50815144        return rc > 0 ? 0 : 1;
    50825145    }
     5146}
    50835147
    50845148#else
     5149
     5150static int kwExecCmdLine(const char *pszExe, const char *pszCmdLine)
     5151{
     5152    int rc;
     5153    PKWTOOL pTool = kwToolLookup(pszExe);
     5154    if (pTool)
     5155    {
     5156        int rcExitCode;
     5157        switch (pTool->enmType)
     5158        {
     5159            case KWTOOLTYPE_SANDBOXED:
     5160                KW_LOG(("Sandboxing tool %s\n", pTool->pszPath));
     5161                rc = kwSandboxExec(&g_Sandbox, pTool, pszCmdLine, &rcExitCode);
     5162                break;
     5163            default:
     5164                kHlpAssertFailed();
     5165                KW_LOG(("TODO: Direct exec tool %s\n", pTool->pszPath));
     5166                rc = rcExitCode = 2;
     5167                break;
     5168        }
     5169        KW_LOG(("rcExitCode=%d (rc=%d)\n", rcExitCode, rc));
     5170    }
     5171    else
     5172        rc = 1;
     5173    return rc;
     5174}
     5175
     5176int main(int argc, char **argv)
     5177{
    50855178    int rc = 0;
    50865179    int i;
     
    51085201# endif
    51095202    return rc;
     5203}
    51105204
    51115205#endif
    5112 }
    5113 
     5206
Note: See TracChangeset for help on using the changeset viewer.