home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / disasm / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  6.2 KB  |  286 lines

  1. //    disasm - Disassembly module compiler for VirtualDub
  2. //    Copyright (C) 2002 Avery Lee, All Rights Reserved
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #pragma warning(disable: 4786)
  19.  
  20. #include <string>
  21. #include <list>
  22. #include <vector>
  23. #include <set>
  24. #include <utility>
  25. #include <algorithm>
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdarg.h>
  30. #include <ctype.h>
  31. #include <conio.h>
  32. #include <crtdbg.h>
  33. #include <windows.h>
  34.  
  35. #include "ruleset.h"
  36. #include "utils.h"
  37. #include "runtime_dasm.h"
  38. #include "runtime_tracedec.h"
  39.  
  40. void parse_ia(tRuleSystem& rsys, FILE *f);
  41.  
  42. tRuleSystem        g_RuleSystem;
  43.  
  44. void *VDDisasmDecompress(void *_dst, const unsigned char *src, int src_len);
  45. void *VDTracedecDecompress(void *_dst, const unsigned char *src, int src_len);
  46. bool VDTraceObjectCode(VDTracedecContext *pvdc, const uint8 *source, int bytes, VDTracedecResult& res);
  47. void dump_ia(std::vector<char>& dst, const tRuleSystem& rulesys);
  48. void dump_tracedec(std::vector<char>& dst, const tRuleSystem& rulesys);
  49.  
  50. ///////////////////////////////////////////////////////////////////////////
  51.  
  52. #ifdef _M_AMD64
  53. static const char test1[]={
  54.     0x50,
  55.     0x51,
  56.     0x52,
  57.     0x53,
  58.     0x54,
  59.     0x55,
  60.     0x56,
  61.     0x57,
  62.  
  63.     0x48, 0x50,
  64.     0x4c, 0x50,
  65.     0x4a, 0x50,
  66.     0x49, 0x50,
  67.  
  68.     0x66, 0x44, 0x50,
  69.     0x66, 0x42, 0x50,
  70.     0x66, 0x41, 0x50,
  71.  
  72.     0x58,
  73.     0x59,
  74.     0x5a,
  75.     0x5b,
  76.     0x5c,
  77.     0x5d,
  78.     0x5e,
  79.     0x5f,
  80. };
  81. #else
  82. #ifdef _MSC_VER
  83.     #pragma warning(disable: 4730)        // warning C4730: 'test1' : mixing _m64 and floating point expressions may result in incorrect code
  84. #endif
  85. void __declspec(naked) test1() {
  86.     __asm {
  87.         __emit 0x83
  88.         __emit 0xc3
  89.         __emit 0x01
  90.  
  91.         __emit 0x0f
  92.         __emit 0x18
  93.         __emit 0x05
  94.         __emit 0x40
  95.         __emit 0x07
  96.         __emit 0x90
  97.         __emit 0x02
  98.  
  99.         prefetchnta [eax]
  100.         prefetcht0 [eax]
  101.         prefetcht1 [eax]
  102.         prefetcht2 [eax]
  103.  
  104.         pavgusb        mm0,[eax]
  105.         prefetch    [eax]
  106.         prefetchw    [eax]
  107.         pswapd        mm1, mm0
  108.         push        [eax]
  109.         push        word ptr [eax]
  110.  
  111.         cvtsi2ss    xmm4, ecx
  112.         cvtsi2ss    xmm4, [ecx]
  113.         cvtpi2ps    xmm4, mm2
  114.         cvtpi2ps    xmm4, [ecx]
  115.  
  116.         cvtss2si    eax, xmm4
  117.         cvtss2si    eax, [ecx]
  118.         cvtps2pi    mm2, xmm4
  119.         cvtps2pi    mm2, [ecx]
  120.  
  121.         cvttss2si    eax, xmm4
  122.         cvttss2si    eax, [ecx]
  123.         cvttps2pi    mm2, xmm4
  124.         cvttps2pi    mm2, [ecx]
  125.  
  126.         cvtsi2sd    xmm4, ecx
  127.         cvtsi2sd    xmm4, [ecx]
  128.         cvtpi2pd    xmm4, mm2
  129.         cvtpi2pd    xmm4, [ecx]
  130.  
  131.         cvtsd2si    eax, xmm4
  132.         cvtsd2si    eax, [ecx]
  133.         cvtpd2pi    mm2, xmm4
  134.         cvtpd2pi    mm2, [ecx]
  135.  
  136.         cvttsd2si    eax, xmm4
  137.         cvttsd2si    eax, [ecx]
  138.         cvttpd2pi    mm2, xmm4
  139.         cvttpd2pi    mm2, [ecx]
  140.  
  141.         movq        xmm0, qword ptr [eax]
  142.  
  143. __emit 0x66
  144. __emit 0x0f
  145. __emit 0x6f
  146. __emit 0x2d
  147. __emit 0xf0
  148. __emit 0x42
  149. __emit 0x0e
  150. __emit 0x10
  151.  
  152.         rep movsw
  153.         lock rep movs es:word ptr [edi], cs:word ptr [esi]
  154.  
  155.         lock mov cs:dword ptr [eax+ecx*4+12300000h], 12345678h
  156.  
  157.         __emit 0x2e
  158.         jc x1
  159.  
  160.         __emit 0x3e
  161.         jc y1
  162.  
  163.         call esi
  164.  
  165.         shl ecx,1
  166.  
  167.         ret
  168. x1:
  169. y1:
  170.         nop
  171.  
  172.         fldcw word ptr [esp]
  173.  
  174.     }
  175. }
  176. #endif
  177.  
  178. ///////////////////////////////////////////////////////////////////////////
  179.  
  180. long symLookup(unsigned long virtAddr, char *buf, int buf_len) {
  181.     unsigned long offs;
  182.  
  183.     if ((offs = (virtAddr - (unsigned long)symLookup)) < 256) {
  184.         strcpy(buf, "symLookup");
  185.         return (long)offs;
  186.     }
  187.  
  188.     if ((offs = (virtAddr - (unsigned long)VDDisassemble)) < 256) {
  189.         strcpy(buf, "VDDisassemble");
  190.         return (long)offs;
  191.     }
  192.  
  193.     if ((offs = (virtAddr - (unsigned long)printf)) < 16) {
  194.         strcpy(buf, "printf");
  195.         return (long)offs;
  196.     }
  197.  
  198.     return -1;
  199. }
  200.  
  201. int main(int argc, char **argv) {
  202.     FILE *f = fopen(argc>1?argv[1]:"ia32.txt", "r");
  203.     parse_ia(g_RuleSystem, f);
  204.     fclose(f);
  205.  
  206. #if 0
  207.     std::vector<char> tr;
  208.     dump_tracedec(tr, g_RuleSystem);
  209.  
  210.     if (f = fopen(argc>2?argv[2]:"ia32trace.bin", "wb")) {
  211.         fwrite(&tr[0], tr.size(), 1, f);
  212.         fclose(f);
  213.     }
  214.  
  215.     std::vector<char> ruleTestHeap;
  216.     ruleTestHeap.resize(*(long *)&tr[68]);
  217.     void *dst_end = VDTracedecDecompress(&ruleTestHeap[0], (const unsigned char *)&tr[72], *(long *)&tr[64]);
  218.  
  219.     VDTracedecContext tdec;
  220.     tdec.pRuleSystem = (const unsigned char **)&ruleTestHeap[0];
  221.     tdec.physToVirtOffset = 0;
  222.  
  223.     void *src = (void *)0x401000;
  224.     VDTracedecResult res;
  225.  
  226.     std::set<ptrdiff_t> traces;
  227.     std::vector<ptrdiff_t> tracesToGo;
  228.  
  229.     tracesToGo.push_back((ptrdiff_t)0x401000);
  230.  
  231.     while(!tracesToGo.empty()) {
  232.         ptrdiff_t traceStart = tracesToGo.back();
  233.         tracesToGo.pop_back();
  234.  
  235.         printf("Tracing at %p\n", (void *)traceStart);
  236.         while(VDTraceObjectCode(&tdec, (const uint8 *)traceStart, 0xFFFFFF, res)) {
  237.             if (res.mbIsCall)
  238.                 printf("Call: %p\n", (void *)traceStart);
  239.             else if (res.mbIsJcc)
  240.                 printf("Jcc: %p\n", (void *)traceStart);
  241.             else if (res.mbIsJmp) {
  242.                 printf("Jmp: %p\n", (void *)traceStart);
  243.                 break;
  244.             } else if (res.mbIsReturn) {
  245.                 printf("Return: %p\n", (void *)traceStart);
  246.                 break;
  247.             }
  248.  
  249.             if (res.mbTargetValid) {
  250.                 ptrdiff_t newTrace = res.mBranchTarget;
  251.  
  252.                 if (traces.insert(newTrace).second)
  253.                     tracesToGo.push_back(newTrace);
  254.             }
  255.  
  256.             traceStart += res.mInsnLength;
  257.         }
  258.     }
  259. #else
  260.  
  261.     std::vector<char> dst;
  262.     dump_ia(dst, g_RuleSystem);
  263.  
  264.     if (f = fopen(argc>2?argv[2]:"ia32.bin", "wb")) {
  265.         fwrite(&dst[0], dst.size(), 1, f);
  266.         fclose(f);
  267.     }
  268.  
  269.     std::vector<char> ruleTestHeap;
  270.     ruleTestHeap.resize(*(long *)&dst[68]);
  271.     void *dst_end = VDDisasmDecompress(&ruleTestHeap[0], (const unsigned char *)&dst[72], *(long *)&dst[64]);
  272.  
  273.     VDDisassemblyContext vdc;
  274.  
  275.     vdc.pRuleSystem = (const unsigned char **)&ruleTestHeap[0];
  276.     vdc.pSymLookup = symLookup;
  277.     vdc.physToVirtOffset = 0;
  278.  
  279.     VDDisassemble(&vdc, (const uint8 *)test1, 1024);
  280. #endif
  281.  
  282.     _getch();
  283.  
  284.     return 0;
  285. }
  286.