Logo Search packages:      
Sourcecode: virtualbox-ose version File versions

static int BasicTestsSub ( PKLDRMOD  pMod  )  [static]

Performs the basic module loader test on the specified module

Definition at line 402 of file tstkLdrMod.c.

References BasicTestsGetImport(), BasicTestsRelocate(), BasicTestsSub2(), Failure(), K_ARCH_IS_VALID, KCPU_END, KCPU_INVALID, KLDRENDIAN_END, KLDRENDIAN_INVALID, KLDRFMT_END, KLDRFMT_INVALID, KLDRMOD_MAGIC, kLdrModGetBits(), kLdrModSize(), KLDRTYPE_END, KLDRTYPE_INVALID, KPROT_END, KPROT_INVALID, KSIZE, NIL_KLDRADDR, NULL, PRI_KLDRADDR, PRI_KLDRSIZE, strlen(), and KLDRMOD::u32Magic.

Referenced by BasicTests().

{
    int         rc;
    KU32        i;
    void       *pvBits;
    KSIZE       cbImage;

    /*
     * Check/dump the module structure.
     */
    printf("pMod=%p u32Magic=%#x cSegments=%d\n", (void *)pMod, pMod->u32Magic, pMod->cSegments);
    printf("enmType=%d enmFmt=%d enmArch=%d enmCpu=%d enmEndian=%d\n",
           pMod->enmType, pMod->enmFmt, pMod->enmArch, pMod->enmCpu, pMod->enmEndian);
    printf("Filename: %s (%d bytes)\n", pMod->pszFilename, pMod->cchFilename);
    printf("    Name: %s (%d bytes)\n", pMod->pszName, pMod->cchName);
    printf("\n");

    if (pMod->u32Magic != KLDRMOD_MAGIC)
        return Failure("Bad u32Magic");
    if (strlen(pMod->pszFilename) != pMod->cchFilename)
        return Failure("Bad cchFilename");
    if (strlen(pMod->pszName) != pMod->cchName)
        return Failure("Bad cchName");
    if (pMod->enmFmt >= KLDRFMT_END || pMod->enmFmt <= KLDRFMT_INVALID)
        return Failure("Bad enmFmt");
    if (pMod->enmType >= KLDRTYPE_END || pMod->enmType <= KLDRTYPE_INVALID)
        return Failure("Bad enmType: %d", pMod->enmType);
    if (!K_ARCH_IS_VALID(pMod->enmArch))
        return Failure("Bad enmArch");
    if (pMod->enmCpu >= KCPU_END || pMod->enmCpu <= KCPU_INVALID)
        return Failure("Bad enmCpu");
    if (pMod->enmEndian >= KLDRENDIAN_END || pMod->enmEndian <= KLDRENDIAN_INVALID)
        return Failure("Bad enmEndian");

    for (i = 0; i < pMod->cSegments; i++)
    {
        printf("seg #%d: pvUser=%p enmProt=%d Name: '%.*s' (%d bytes)\n",
               i, pMod->aSegments[i].pvUser, pMod->aSegments[i].enmProt,
               pMod->aSegments[i].cchName, pMod->aSegments[i].pchName, pMod->aSegments[i].cchName);
        printf("LinkAddress: %016" PRI_KLDRADDR "       cb: %016" PRI_KLDRSIZE "  Alignment=%08" PRI_KLDRADDR " \n",
               pMod->aSegments[i].LinkAddress, pMod->aSegments[i].cb, pMod->aSegments[i].Alignment);
        printf("        RVA: %016" PRI_KLDRADDR " cbMapped: %016" PRI_KLDRSIZE " MapAddress=%p\n",
               pMod->aSegments[i].RVA, (KLDRSIZE)pMod->aSegments[i].cbMapped, (void *)pMod->aSegments[i].MapAddress);
        printf("    offFile: %016" PRI_KLDRADDR "   cbFile: %016" PRI_KLDRSIZE "\n",
               (KLDRADDR)pMod->aSegments[i].offFile, (KLDRSIZE)pMod->aSegments[i].cbFile);
        printf("\n");

        if (pMod->aSegments[i].pvUser != NULL)
            return Failure("Bad pvUser");
        if (pMod->aSegments[i].enmProt >= KPROT_END || pMod->aSegments[i].enmProt <= KPROT_INVALID)
            return Failure("Bad enmProt");
        if (pMod->aSegments[i].MapAddress != 0)
            return Failure("Bad MapAddress");
        if (pMod->aSegments[i].cbMapped < pMod->aSegments[i].cb)
            return Failure("Bad cbMapped (1)");
        if (pMod->aSegments[i].cbMapped && !pMod->aSegments[i].Alignment)
            return Failure("Bad cbMapped (2)");
        if (pMod->aSegments[i].cbMapped > kLdrModSize(pMod))
            return Failure("Bad cbMapped (3)");
        if (    pMod->aSegments[i].Alignment
            &&  (pMod->aSegments[i].RVA & (pMod->aSegments[i].Alignment - 1)))
            return Failure("Bad RVA (1)");
        if (pMod->aSegments[i].RVA != NIL_KLDRADDR && !pMod->aSegments[i].Alignment)
            return Failure("Bad RVA (2)");
        if (    pMod->aSegments[i].RVA != NIL_KLDRADDR
            &&  pMod->aSegments[i].RVA >= kLdrModSize(pMod))
            return Failure("Bad RVA (3)");
        if (    pMod->aSegments[i].RVA != NIL_KLDRADDR
            &&  pMod->aSegments[i].RVA + pMod->aSegments[i].cbMapped > kLdrModSize(pMod))
            return Failure("Bad RVA/cbMapped (4)");
        if (pMod->aSegments[i].LinkAddress != NIL_KLDRADDR && !pMod->aSegments[i].Alignment)
            return Failure("Bad LinkAddress");
        if (    pMod->aSegments[i].LinkAddress != NIL_KLDRADDR
            &&  (pMod->aSegments[i].LinkAddress) & (pMod->aSegments[i].Alignment - 1))
            return Failure("Bad LinkAddress alignment");
        if (pMod->aSegments[i].offFile != -1 && pMod->aSegments[i].cbFile == -1)
            return Failure("Bad offFile");
        if (pMod->aSegments[i].offFile == -1 && pMod->aSegments[i].cbFile != -1)
            return Failure("Bad cbFile");
    }


    /*
     * Get image the size and query the image bits.
     */
    printf("* Testing user mapping...\n");

    cbImage = (KSIZE)kLdrModSize(pMod);
    if (cbImage != kLdrModSize(pMod))
        return Failure("aborting test because the image is too huge!");
    pvBits = malloc((KSIZE)cbImage);
    if (!pvBits)
        return Failure("failed to allocate %d bytes for the image", cbImage);

    rc = kLdrModGetBits(pMod, pvBits, (KUPTR)pvBits, BasicTestsGetImport, NULL);
    if (rc)
        return Failure("failed to get image bits, rc=%d (%s)", rc, kErrName(rc));

    /*
     * Another cleanup nesting.
     */
    rc = BasicTestsSub2(pMod, pvBits);
    if (!rc)
    {
        /*
         * Test relocating the bits in a few different ways before we're done with them.
         */
        void *pvBits2 = malloc((KSIZE)cbImage);
        if (pvBits2)
        {
            rc = BasicTestsRelocate(pMod, pvBits, pvBits2);
            free(pvBits2);
        }
        else
            rc = Failure("failed to allocate %d bytes for the 2nd image", cbImage);
    }

    free(pvBits);
    return rc;
}


Generated by  Doxygen 1.6.0   Back to index