Changeset 4838 for zzuf


Ignore:
Timestamp:
Aug 23, 2012, 1:21:31 PM (7 years ago)
Author:
Sam Hocevar
Message:

win32: add more explicit error messages and add support for 0xb8 opcode.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/libzzuf/sys.c

    r4837 r4838  
    4949#if defined HAVE_WINDOWS_H
    5050static void insert_funcs(void);
    51 
    52 /* TODO: get rid of this later */
    53 HINSTANCE (WINAPI *LoadLibraryA_orig)(LPCSTR);
    54 HINSTANCE WINAPI LoadLibraryA_new(LPCSTR path)
    55 {
    56     return LoadLibraryA_orig(path);
    57 }
    58 
    59 BOOL (WINAPI *AllocConsole_orig)(void);
    60 BOOL WINAPI AllocConsole_new(void)
    61 {
    62     return AllocConsole_orig();
    63 }
    64 
    65 BOOL (WINAPI *AttachConsole_orig)(DWORD);
    66 BOOL WINAPI AttachConsole_new(DWORD d)
    67 {
    68     return AttachConsole_orig(d);
    69 }
    7051#endif
    7152
     
    130111    switch (opcd)
    131112    {
    132     case 0x68: return (insn_size + 4); /* PUSH Iv */
    133     case 0x6a: return (insn_size + 1); /* PUSH Ib */
    134     case 0x90: return insn_size;       /* NOP     */
    135     default: break;
     113    case 0x68:
     114        return (insn_size + 4); /* PUSH Iv */
     115    case 0x6a:
     116        return (insn_size + 1); /* PUSH Ib */
     117    case 0x90:
     118        return insn_size;       /* NOP     */
     119    case 0xb8:
     120    case 0xb9:
     121    case 0xba:
     122    case 0xbb:
     123    case 0xbc:
     124    case 0xbd:
     125    case 0xbe:
     126    case 0xbf:
     127        return insn_size + 5;   /* MOV immediate */
     128    default:
     129        break;
    136130    }
    137131
    138132    /* PUSH/POP rv */
    139     if ((opcd & 0xf0) == 0x50) return insn_size;
     133    if ((opcd & 0xf0) == 0x50)
     134        return insn_size;
    140135
    141136    /* MNEM E?, G? or G?, E? */
     
    159154        break;
    160155
    161     default: break;
     156    default:
     157        fprintf(stderr, "unknown opcode %02x\n", opcd);
     158        break;
    162159    }
    163160
     
    172169    {
    173170        int insn_size = zz_lde(code);
    174         if (insn_size == 0) return -1;
     171        if (insn_size == 0)
     172            return -1;
    175173        patch_size += insn_size;
    176174    }
     
    206204
    207205        trampoline = malloc(patch_size + reloc_size + 13); /* Worst case */
    208         if (trampoline == NULL) return -1;
     206        if (trampoline == NULL)
     207            return -1;
    209208        memset(trampoline, 0xcc, patch_size + 13);
    210209
     
    212211        {
    213212            int insn_size = zz_lde(code + code_offset);
    214             if (insn_size == 0) return -1;
     213            if (insn_size == 0)
     214                return -1;
    215215
    216216            /* mov rax, [rip + ...] is the signature for stack cookie */
     
    335335    /* if we can't get enough byte, we quit */
    336336    if ((patch_size = compute_patch_size(old_api, required_size)) == -1)
     337    {
     338        fprintf(stderr, "cannot compute patch size\n");
    337339        return -1;
    338 
    339     if (make_trampoline(old_api, patch_size, &trampoline, &trampoline_size) < 0) goto _out;
     340    }
     341
     342    if (make_trampoline(old_api, patch_size, &trampoline, &trampoline_size) < 0)
     343    {
     344        fprintf(stderr, "cannot make trampoline\n");
     345        goto _out;
     346    }
    340347
    341348    /* We must make the trampoline executable, this line is required because of DEP */
    342349    /* NOTE: We _must_ set the write protection, otherwise the heap allocator will crash ! */
    343     if (!VirtualProtect(trampoline, trampoline_size, PAGE_EXECUTE_READWRITE, &old_prot)) goto _out;
     350    if (!VirtualProtect(trampoline, trampoline_size, PAGE_EXECUTE_READWRITE, &old_prot))
     351    {
     352        fprintf(stderr, "cannot make the trampoline writable\n");
     353        goto _out;
     354    }
    344355
    345356    /* We patch the targeted API, so we must set it as writable */
    346     if (!VirtualProtect(old_api, patch_size, PAGE_EXECUTE_READWRITE, &old_prot)) goto _out;
     357    if (!VirtualProtect(old_api, patch_size, PAGE_EXECUTE_READWRITE, &old_prot))
     358    {
     359        fprintf(stderr, "cannot make old API writable\n");
     360        goto _out;
     361    }
    347362    memcpy(old_api, jmp_prolog, patch_size);
    348363    VirtualProtect(old_api, patch_size, old_prot, &old_prot); /* we don't care if this functon fails */
Note: See TracChangeset for help on using the changeset viewer.