metasm 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.hgtags +3 -0
  4. data/Gemfile +1 -0
  5. data/INSTALL +61 -0
  6. data/LICENCE +458 -0
  7. data/README +29 -21
  8. data/Rakefile +10 -0
  9. data/TODO +10 -12
  10. data/doc/code_organisation.txt +2 -0
  11. data/doc/core/DynLdr.txt +247 -0
  12. data/doc/core/ExeFormat.txt +43 -0
  13. data/doc/core/Expression.txt +220 -0
  14. data/doc/core/GNUExports.txt +27 -0
  15. data/doc/core/Ia32.txt +236 -0
  16. data/doc/core/SerialStruct.txt +108 -0
  17. data/doc/core/VirtualString.txt +145 -0
  18. data/doc/core/WindowsExports.txt +61 -0
  19. data/doc/core/index.txt +1 -0
  20. data/doc/style.css +6 -3
  21. data/doc/usage/debugger.txt +327 -0
  22. data/doc/usage/index.txt +1 -0
  23. data/doc/use_cases.txt +2 -2
  24. data/metasm.gemspec +22 -0
  25. data/{lib/metasm.rb → metasm.rb} +11 -3
  26. data/{lib/metasm → metasm}/compile_c.rb +13 -7
  27. data/metasm/cpu/arc.rb +8 -0
  28. data/metasm/cpu/arc/decode.rb +425 -0
  29. data/metasm/cpu/arc/main.rb +191 -0
  30. data/metasm/cpu/arc/opcodes.rb +588 -0
  31. data/{lib/metasm → metasm/cpu}/arm.rb +7 -5
  32. data/{lib/metasm → metasm/cpu}/arm/debug.rb +2 -2
  33. data/{lib/metasm → metasm/cpu}/arm/decode.rb +13 -12
  34. data/{lib/metasm → metasm/cpu}/arm/encode.rb +23 -8
  35. data/{lib/metasm → metasm/cpu}/arm/main.rb +0 -3
  36. data/metasm/cpu/arm/opcodes.rb +324 -0
  37. data/{lib/metasm → metasm/cpu}/arm/parse.rb +25 -13
  38. data/{lib/metasm → metasm/cpu}/arm/render.rb +2 -2
  39. data/metasm/cpu/arm64.rb +15 -0
  40. data/metasm/cpu/arm64/debug.rb +38 -0
  41. data/metasm/cpu/arm64/decode.rb +289 -0
  42. data/metasm/cpu/arm64/encode.rb +41 -0
  43. data/metasm/cpu/arm64/main.rb +105 -0
  44. data/metasm/cpu/arm64/opcodes.rb +232 -0
  45. data/metasm/cpu/arm64/parse.rb +20 -0
  46. data/metasm/cpu/arm64/render.rb +95 -0
  47. data/{lib/metasm/ppc.rb → metasm/cpu/bpf.rb} +2 -4
  48. data/metasm/cpu/bpf/decode.rb +142 -0
  49. data/metasm/cpu/bpf/main.rb +60 -0
  50. data/metasm/cpu/bpf/opcodes.rb +81 -0
  51. data/metasm/cpu/bpf/render.rb +41 -0
  52. data/metasm/cpu/cy16.rb +9 -0
  53. data/metasm/cpu/cy16/decode.rb +253 -0
  54. data/metasm/cpu/cy16/main.rb +63 -0
  55. data/metasm/cpu/cy16/opcodes.rb +78 -0
  56. data/metasm/cpu/cy16/render.rb +41 -0
  57. data/metasm/cpu/dalvik.rb +11 -0
  58. data/{lib/metasm → metasm/cpu}/dalvik/decode.rb +35 -13
  59. data/{lib/metasm → metasm/cpu}/dalvik/main.rb +51 -2
  60. data/{lib/metasm → metasm/cpu}/dalvik/opcodes.rb +19 -11
  61. data/metasm/cpu/ia32.rb +17 -0
  62. data/{lib/metasm → metasm/cpu}/ia32/compile_c.rb +5 -7
  63. data/{lib/metasm → metasm/cpu}/ia32/debug.rb +5 -5
  64. data/{lib/metasm → metasm/cpu}/ia32/decode.rb +246 -59
  65. data/{lib/metasm → metasm/cpu}/ia32/decompile.rb +7 -7
  66. data/{lib/metasm → metasm/cpu}/ia32/encode.rb +19 -13
  67. data/{lib/metasm → metasm/cpu}/ia32/main.rb +51 -8
  68. data/metasm/cpu/ia32/opcodes.rb +1424 -0
  69. data/{lib/metasm → metasm/cpu}/ia32/parse.rb +47 -16
  70. data/{lib/metasm → metasm/cpu}/ia32/render.rb +31 -4
  71. data/metasm/cpu/mips.rb +14 -0
  72. data/{lib/metasm → metasm/cpu}/mips/compile_c.rb +1 -1
  73. data/metasm/cpu/mips/debug.rb +42 -0
  74. data/{lib/metasm → metasm/cpu}/mips/decode.rb +46 -16
  75. data/{lib/metasm → metasm/cpu}/mips/encode.rb +4 -3
  76. data/{lib/metasm → metasm/cpu}/mips/main.rb +11 -4
  77. data/{lib/metasm → metasm/cpu}/mips/opcodes.rb +86 -17
  78. data/{lib/metasm → metasm/cpu}/mips/parse.rb +1 -1
  79. data/{lib/metasm → metasm/cpu}/mips/render.rb +1 -1
  80. data/{lib/metasm/dalvik.rb → metasm/cpu/msp430.rb} +1 -1
  81. data/metasm/cpu/msp430/decode.rb +247 -0
  82. data/metasm/cpu/msp430/main.rb +62 -0
  83. data/metasm/cpu/msp430/opcodes.rb +101 -0
  84. data/{lib/metasm → metasm/cpu}/pic16c/decode.rb +6 -7
  85. data/{lib/metasm → metasm/cpu}/pic16c/main.rb +0 -0
  86. data/{lib/metasm → metasm/cpu}/pic16c/opcodes.rb +1 -1
  87. data/{lib/metasm/mips.rb → metasm/cpu/ppc.rb} +4 -4
  88. data/{lib/metasm → metasm/cpu}/ppc/decode.rb +18 -12
  89. data/{lib/metasm → metasm/cpu}/ppc/decompile.rb +3 -3
  90. data/{lib/metasm → metasm/cpu}/ppc/encode.rb +2 -2
  91. data/{lib/metasm → metasm/cpu}/ppc/main.rb +17 -12
  92. data/{lib/metasm → metasm/cpu}/ppc/opcodes.rb +11 -5
  93. data/metasm/cpu/ppc/parse.rb +55 -0
  94. data/metasm/cpu/python.rb +8 -0
  95. data/metasm/cpu/python/decode.rb +136 -0
  96. data/metasm/cpu/python/main.rb +36 -0
  97. data/metasm/cpu/python/opcodes.rb +180 -0
  98. data/{lib/metasm → metasm/cpu}/sh4.rb +1 -1
  99. data/{lib/metasm → metasm/cpu}/sh4/decode.rb +48 -17
  100. data/{lib/metasm → metasm/cpu}/sh4/main.rb +13 -4
  101. data/{lib/metasm → metasm/cpu}/sh4/opcodes.rb +7 -8
  102. data/metasm/cpu/x86_64.rb +15 -0
  103. data/{lib/metasm → metasm/cpu}/x86_64/compile_c.rb +28 -17
  104. data/{lib/metasm → metasm/cpu}/x86_64/debug.rb +4 -4
  105. data/{lib/metasm → metasm/cpu}/x86_64/decode.rb +57 -15
  106. data/{lib/metasm → metasm/cpu}/x86_64/encode.rb +55 -26
  107. data/{lib/metasm → metasm/cpu}/x86_64/main.rb +14 -6
  108. data/metasm/cpu/x86_64/opcodes.rb +136 -0
  109. data/{lib/metasm → metasm/cpu}/x86_64/parse.rb +10 -2
  110. data/metasm/cpu/x86_64/render.rb +35 -0
  111. data/metasm/cpu/z80.rb +9 -0
  112. data/metasm/cpu/z80/decode.rb +313 -0
  113. data/metasm/cpu/z80/main.rb +67 -0
  114. data/metasm/cpu/z80/opcodes.rb +224 -0
  115. data/metasm/cpu/z80/render.rb +59 -0
  116. data/{lib/metasm/os/main.rb → metasm/debug.rb} +160 -401
  117. data/{lib/metasm → metasm}/decode.rb +35 -4
  118. data/{lib/metasm → metasm}/decompile.rb +15 -16
  119. data/{lib/metasm → metasm}/disassemble.rb +201 -45
  120. data/{lib/metasm → metasm}/disassemble_api.rb +651 -87
  121. data/{lib/metasm → metasm}/dynldr.rb +220 -133
  122. data/{lib/metasm → metasm}/encode.rb +10 -1
  123. data/{lib/metasm → metasm}/exe_format/a_out.rb +9 -6
  124. data/{lib/metasm → metasm}/exe_format/autoexe.rb +1 -0
  125. data/{lib/metasm → metasm}/exe_format/bflt.rb +57 -27
  126. data/{lib/metasm → metasm}/exe_format/coff.rb +11 -3
  127. data/{lib/metasm → metasm}/exe_format/coff_decode.rb +53 -20
  128. data/{lib/metasm → metasm}/exe_format/coff_encode.rb +11 -13
  129. data/{lib/metasm → metasm}/exe_format/dex.rb +13 -5
  130. data/{lib/metasm → metasm}/exe_format/dol.rb +1 -0
  131. data/{lib/metasm → metasm}/exe_format/elf.rb +93 -57
  132. data/{lib/metasm → metasm}/exe_format/elf_decode.rb +143 -34
  133. data/{lib/metasm → metasm}/exe_format/elf_encode.rb +122 -31
  134. data/metasm/exe_format/gb.rb +65 -0
  135. data/metasm/exe_format/javaclass.rb +424 -0
  136. data/{lib/metasm → metasm}/exe_format/macho.rb +204 -16
  137. data/{lib/metasm → metasm}/exe_format/main.rb +26 -3
  138. data/{lib/metasm → metasm}/exe_format/mz.rb +1 -0
  139. data/{lib/metasm → metasm}/exe_format/nds.rb +7 -4
  140. data/{lib/metasm → metasm}/exe_format/pe.rb +71 -8
  141. data/metasm/exe_format/pyc.rb +167 -0
  142. data/{lib/metasm → metasm}/exe_format/serialstruct.rb +67 -14
  143. data/{lib/metasm → metasm}/exe_format/shellcode.rb +7 -3
  144. data/metasm/exe_format/shellcode_rwx.rb +114 -0
  145. data/metasm/exe_format/swf.rb +205 -0
  146. data/{lib/metasm → metasm}/exe_format/xcoff.rb +7 -7
  147. data/metasm/exe_format/zip.rb +335 -0
  148. data/metasm/gui.rb +13 -0
  149. data/{lib/metasm → metasm}/gui/cstruct.rb +35 -41
  150. data/{lib/metasm → metasm}/gui/dasm_coverage.rb +11 -11
  151. data/{lib/metasm → metasm}/gui/dasm_decomp.rb +7 -20
  152. data/{lib/metasm → metasm}/gui/dasm_funcgraph.rb +0 -0
  153. data/metasm/gui/dasm_graph.rb +1695 -0
  154. data/{lib/metasm → metasm}/gui/dasm_hex.rb +12 -8
  155. data/{lib/metasm → metasm}/gui/dasm_listing.rb +43 -28
  156. data/{lib/metasm → metasm}/gui/dasm_main.rb +310 -53
  157. data/{lib/metasm → metasm}/gui/dasm_opcodes.rb +5 -19
  158. data/{lib/metasm → metasm}/gui/debug.rb +93 -27
  159. data/{lib/metasm → metasm}/gui/gtk.rb +162 -40
  160. data/{lib/metasm → metasm}/gui/qt.rb +12 -2
  161. data/{lib/metasm → metasm}/gui/win32.rb +179 -42
  162. data/{lib/metasm → metasm}/gui/x11.rb +59 -59
  163. data/{lib/metasm → metasm}/main.rb +389 -264
  164. data/{lib/metasm/os/remote.rb → metasm/os/gdbremote.rb} +146 -54
  165. data/{lib/metasm → metasm}/os/gnu_exports.rb +1 -1
  166. data/{lib/metasm → metasm}/os/linux.rb +628 -151
  167. data/metasm/os/main.rb +330 -0
  168. data/{lib/metasm → metasm}/os/windows.rb +132 -42
  169. data/{lib/metasm → metasm}/os/windows_exports.rb +141 -0
  170. data/{lib/metasm → metasm}/parse.rb +26 -24
  171. data/{lib/metasm → metasm}/parse_c.rb +221 -116
  172. data/{lib/metasm → metasm}/preprocessor.rb +55 -40
  173. data/{lib/metasm → metasm}/render.rb +14 -38
  174. data/misc/hexdump.rb +2 -1
  175. data/misc/lint.rb +58 -0
  176. data/misc/txt2html.rb +9 -7
  177. data/samples/bindiff.rb +3 -4
  178. data/samples/dasm-plugins/bindiff.rb +15 -0
  179. data/samples/dasm-plugins/bookmark.rb +133 -0
  180. data/samples/dasm-plugins/c_constants.rb +57 -0
  181. data/samples/dasm-plugins/colortheme_solarized.rb +125 -0
  182. data/samples/dasm-plugins/cppobj_funcall.rb +60 -0
  183. data/samples/dasm-plugins/dasm_all.rb +70 -0
  184. data/samples/dasm-plugins/demangle_cpp.rb +31 -0
  185. data/samples/dasm-plugins/deobfuscate.rb +251 -0
  186. data/samples/dasm-plugins/dump_text.rb +35 -0
  187. data/samples/dasm-plugins/export_graph_svg.rb +86 -0
  188. data/samples/dasm-plugins/findgadget.rb +75 -0
  189. data/samples/dasm-plugins/hl_opcode.rb +32 -0
  190. data/samples/dasm-plugins/hotfix_gtk_dbg.rb +19 -0
  191. data/samples/dasm-plugins/imm2off.rb +34 -0
  192. data/samples/dasm-plugins/match_libsigs.rb +93 -0
  193. data/samples/dasm-plugins/patch_file.rb +95 -0
  194. data/samples/dasm-plugins/scanfuncstart.rb +36 -0
  195. data/samples/dasm-plugins/scanxrefs.rb +26 -0
  196. data/samples/dasm-plugins/selfmodify.rb +197 -0
  197. data/samples/dasm-plugins/stringsxrefs.rb +28 -0
  198. data/samples/dasmnavig.rb +1 -1
  199. data/samples/dbg-apihook.rb +24 -9
  200. data/samples/dbg-plugins/heapscan.rb +283 -0
  201. data/samples/dbg-plugins/heapscan/compiled_heapscan_lin.c +155 -0
  202. data/samples/dbg-plugins/heapscan/compiled_heapscan_win.c +128 -0
  203. data/samples/dbg-plugins/heapscan/graphheap.rb +616 -0
  204. data/samples/dbg-plugins/heapscan/heapscan.rb +709 -0
  205. data/samples/dbg-plugins/heapscan/winheap.h +174 -0
  206. data/samples/dbg-plugins/heapscan/winheap7.h +307 -0
  207. data/samples/dbg-plugins/trace_func.rb +214 -0
  208. data/samples/disassemble-gui.rb +35 -5
  209. data/samples/disassemble.rb +31 -6
  210. data/samples/dump_upx.rb +24 -12
  211. data/samples/dynamic_ruby.rb +12 -3
  212. data/samples/exeencode.rb +6 -5
  213. data/samples/factorize-headers-peimports.rb +1 -1
  214. data/samples/lindebug.rb +175 -381
  215. data/samples/metasm-shell.rb +1 -2
  216. data/samples/peldr.rb +2 -2
  217. data/tests/all.rb +1 -1
  218. data/tests/arc.rb +26 -0
  219. data/tests/dynldr.rb +22 -4
  220. data/tests/expression.rb +55 -0
  221. data/tests/graph_layout.rb +285 -0
  222. data/tests/ia32.rb +79 -26
  223. data/tests/mips.rb +9 -2
  224. data/tests/x86_64.rb +66 -18
  225. metadata +330 -218
  226. data/lib/metasm/arm/opcodes.rb +0 -177
  227. data/lib/metasm/gui.rb +0 -23
  228. data/lib/metasm/gui/dasm_graph.rb +0 -1354
  229. data/lib/metasm/ia32.rb +0 -14
  230. data/lib/metasm/ia32/opcodes.rb +0 -873
  231. data/lib/metasm/ppc/parse.rb +0 -52
  232. data/lib/metasm/x86_64.rb +0 -12
  233. data/lib/metasm/x86_64/opcodes.rb +0 -118
  234. data/samples/gdbclient.rb +0 -583
  235. data/samples/rubstop.rb +0 -399
@@ -31,8 +31,7 @@ class String
31
31
 
32
32
  # encodes the current string as a Shellcode, returns the resulting EncodedData
33
33
  def encode_edata
34
- s = Metasm::Shellcode.assemble @@cpu, self
35
- s.encoded
34
+ Metasm::Shellcode.assemble(@@cpu, self).encode.encoded
36
35
  end
37
36
 
38
37
  # encodes the current string as a Shellcode, returns the resulting binary String
@@ -148,7 +148,7 @@ class PeLdr
148
148
  end
149
149
  }
150
150
 
151
- cp.numeric_constants.each { |k, v|
151
+ cp.numeric_constants.each { |k, v, f|
152
152
  n = k.upcase
153
153
  n = "C#{n}" if n !~ /^[A-Z]/
154
154
  DL.const_set(n, v) if not DL.const_defined?(n) and v.kind_of? Integer
@@ -178,7 +178,7 @@ class PeLdr
178
178
 
179
179
  def self.populate_peb
180
180
  DL.memory_write(@@peb, 0.chr*4096)
181
- set = lambda { |off, val| DL.memory_write_int(@@peb+off, val) }
181
+ #set = lambda { |off, val| DL.memory_write_int(@@peb+off, val) }
182
182
  end
183
183
 
184
184
  def self.teb ; @@teb ; end
@@ -4,5 +4,5 @@
4
4
  # Licence is LGPL, see LICENCE in the top-level directory
5
5
 
6
6
 
7
- Dir['tests/*.rb'].each { |f| require f }
7
+ Dir['tests/*.rb'].sort.each { |f| require f }
8
8
 
@@ -0,0 +1,26 @@
1
+ # This file is part of Metasm, the Ruby assembly manipulation suite
2
+ # Copyright (C) 2006-2009 Yoann GUILLOT
3
+ #
4
+ # Licence is LGPL, see LICENCE in the top-level directory
5
+
6
+ require 'test/unit'
7
+ require 'metasm'
8
+
9
+ class TestArc < Test::Unit::TestCase
10
+ def test_arc_dec
11
+ hex_stream = "\x0A\x23\x80\x0F\x80\x0\x60\x0D" # mov r3, 0x800D60
12
+ hex_stream += "\x40\x83" # ld_s r2, [r3, 0]
13
+
14
+ dasm = Metasm::Shellcode.disassemble(Metasm::ARC.new, hex_stream)
15
+ assert_equal(2, dasm.decoded.length)
16
+
17
+ assert_equal('mov', dasm.decoded[0].instruction.opname)
18
+ assert_equal('r3', dasm.decoded[0].instruction.args[0].to_s)
19
+ assert_equal(0x800d60, dasm.decoded[0].instruction.args[1].reduce)
20
+
21
+ assert_equal('ld_s', dasm.decoded[8].instruction.opname)
22
+ assert_equal('r2', dasm.decoded[8].instruction.args[0].to_s)
23
+ assert_equal('r3', dasm.decoded[8].instruction.args[1].base.to_s)
24
+ assert_equal(0, dasm.decoded[8].instruction.args[1].disp.reduce)
25
+ end
26
+ end
@@ -7,29 +7,47 @@ require 'test/unit'
7
7
  require 'metasm'
8
8
 
9
9
  class TestDynldr < Test::Unit::TestCase
10
+ def d; Metasm::DynLdr; end
10
11
 
11
- def test_dynldr
12
+ def test_new_api_c
12
13
  str = "1234"
13
- d = Metasm::DynLdr
14
14
  d.new_api_c('int memcpy(char*, char*, int)')
15
15
  d.memcpy(str, "9999", 2)
16
16
  assert_equal('9934', str)
17
+ end
17
18
 
19
+ def test_new_func_c
18
20
  c_src = <<EOS
19
- int sprintf(char*, char*, ...);
21
+ int sprintf(char*, char*, ...); // stdlib external
20
22
  void fufu(int i, char* ptr)
21
23
  {
22
- sprintf(ptr, "lolzor %i\\n", i);
24
+ static int meh; // .data
25
+ meh = i;
26
+ sprintf(ptr, "lolzor %i\\n", meh); // string constant = .rodata
23
27
  }
24
28
  EOS
25
29
  buf = 'aaaaaaaaaaaaaaaaaa'
26
30
  d.new_func_c(c_src) { d.fufu(42, buf) }
27
31
  assert_equal("lolzor 42\n\000aaaaaaa", buf)
32
+ end
28
33
 
34
+ def test_new_func_asm
29
35
  if d.host_cpu.shortname == 'ia32'
30
36
  ret = d.new_func_asm('int __fastcall bla(int)', "lea eax, [ecx+1]\nret") { d.bla(42) }
31
37
  assert_equal(43, ret)
32
38
  assert_equal(false, d.respond_to?(:bla))
33
39
  end
34
40
  end
41
+
42
+ def test_callback
43
+ c1 = d.callback_alloc_c('int lol(int);') { |i| i+1 }
44
+ c2 = d.callback_alloc_c('int lol(int);') { |i| i+2 }
45
+ c3 = d.callback_alloc_c('int lol(int);') { |i| i/2 }
46
+
47
+ d.new_func_c "int blop(int i, int (*fp)(int)) { return fp(i); }"
48
+
49
+ assert_equal(2, d.blop(1, c1))
50
+ assert_equal(4, d.blop(2, c2))
51
+ assert_equal(6, d.blop(13, c3))
52
+ end
35
53
  end
@@ -0,0 +1,55 @@
1
+ # This file is part of Metasm, the Ruby assembly manipulation suite
2
+ # Copyright (C) 2006-2009 Yoann GUILLOT
3
+ #
4
+ # Licence is LGPL, see LICENCE in the top-level directory
5
+
6
+
7
+ require 'test/unit'
8
+ require 'metasm'
9
+
10
+ class TestExpression < Test::Unit::TestCase
11
+ E = Metasm::Expression
12
+ def test_encode
13
+ assert_equal("\x04\0\0\0", E[4].encode(:u32, :little).data)
14
+ end
15
+
16
+ def test_numeric
17
+ assert_equal(5, E[4, :+, 1].reduce)
18
+ end
19
+
20
+ def test_reduce
21
+ assert_equal(0, E[:a, :-, :a].reduce)
22
+ assert_equal(0, E[:a, :^, :a].reduce)
23
+
24
+ assert_equal(E[:a, :^, 3], E[[1, :^, :a], :^, 2].reduce)
25
+ assert_equal(E[:a], E[[:b, :^, :a], :^, :b].reduce)
26
+
27
+ assert_equal(E[:a, :+, 2], E[:b, :+, [:a, :-, [4, :+, [:b, :-, 6]]]].reduce)
28
+ assert_equal(E[:a, :&, 0xff00], E[[[:a, :>>, 8], :&, 0xff], :<<, 8].reduce)
29
+
30
+ assert_equal(E[[:a, :>>, 1], :&, 0xff0], E[[[:a, :>>, 5], :&, 0xff], :<<, 4].reduce)
31
+
32
+ assert_equal(0, E[[:a, :&, 0xff00], :&, [:b, :&, 0xff]].reduce)
33
+ assert_equal(0, E[[:a, :&, 0xff], :>>, 8].reduce)
34
+
35
+ assert_equal(E[:a, :&, 0xffff], E[[:a, :&, 0x3333], :|, [[:a, :&, 0x8888], :+, [:a, :&, 0x4444]]].reduce)
36
+
37
+ assert_equal(E[:a, :&, 0xff], E[[:a, :|, [:b, :&, 0xff00]], :&, 0xff].reduce)
38
+
39
+ assert_equal(1, E[[2, :>, 1], :'||', [:a, :<=, :b]].reduce)
40
+ assert_equal(0, E[[:a, :>, :b], :'&&', [1, :>, 2]].reduce)
41
+
42
+ assert_equal(E[:a, :>, :b], E[[:'!', [:a, :<=, :b]], :==, 1].reduce)
43
+ end
44
+
45
+ def test_pattern
46
+ pat = E[:a, :+, [:b, :&, 0xffff]].match(E['a', :|, 'b'], 'a', 'b')
47
+ assert_equal(false, pat)
48
+
49
+ pat = E[:a, :+, [:b, :&, 0xffff]].match(E['a', :+, 'b'], 'a', 'b')
50
+ assert_equal(:a, pat['a'])
51
+ p2 = pat['b'].match(E[:a, :b, :c], :a, :b, :c)
52
+ assert_equal(0xffff, p2[:c])
53
+ assert_equal(:&, p2[:b])
54
+ end
55
+ end
@@ -0,0 +1,285 @@
1
+ # This file is part of Metasm, the Ruby assembly manipulation suite
2
+ # Copyright (C) 2006-2009 Yoann GUILLOT
3
+ #
4
+ # Licence is LGPL, see LICENCE in the top-level directory
5
+
6
+
7
+ # special file to test the graph layout engine
8
+ # call this file directly to run
9
+
10
+ require 'metasm'
11
+ include Metasm
12
+
13
+ def test_layout(lo)
14
+ $cur ||= 0
15
+ $cur += 1
16
+ if $target.to_i != 0
17
+ return if $cur != $target
18
+ else
19
+ return if not lo.include? $target
20
+ end if $target
21
+ puts $cur, lo, '' if $VERBOSE
22
+ w = Gui::Window.new
23
+ ww = w.widget = Gui::GraphViewWidget.new(nil, nil)
24
+ ww.grab_focus
25
+ Gui.idle_add {
26
+ ww.load_dot(lo)
27
+ ww.curcontext.auto_arrange_boxes
28
+ ww.zoom_all
29
+ false
30
+ }
31
+ Gui.main
32
+ end
33
+
34
+ def test_all
35
+ test_layout <<EOS
36
+ line -> 2 -> 3 -> 4 -> 5 -> 6 -> 7;
37
+ EOS
38
+ test_layout <<EOS
39
+ sep1 -> 1;
40
+ sep2 -> 2;
41
+ sep3 -> 3;
42
+ sep4 -> 4;
43
+ sep5 -> 5;
44
+ EOS
45
+ test_layout <<EOS
46
+ fork -> 2 -> 3;
47
+ 2 -> 4;
48
+ EOS
49
+ test_layout <<EOS
50
+ diamond -> 2 -> 3 -> 5;
51
+ 2 -> 4 -> 5;
52
+ EOS
53
+ test_layout <<EOS
54
+ ifthen -> 2 -> 3 -> 4;
55
+ 2 -> 4;
56
+ EOS
57
+ test_layout <<EOS
58
+ ufork -> 2 -> 3;
59
+ 1 -> 2;
60
+ EOS
61
+ test_layout <<EOS
62
+ multidiamond -> 2 -> 31 -> 32 -> 34 -> 5 -> 6 -> 8;
63
+ 2 -> 41 -> 42 -> 44 -> 5 -> 7 -> 8;
64
+ 41 -> 43 -> 44;
65
+ 31 -> 33 -> 34;
66
+ EOS
67
+ test_layout <<EOS
68
+ dmdout -> 2 -> 3a -> 4;
69
+ 2 -> 3b -> 4;
70
+ 3a -> 4a;
71
+ 3b -> 4b;
72
+ EOS
73
+ test_layout <<EOS
74
+ ifthenthen -> 2 -> 8;
75
+ 2 -> 3 -> 8;
76
+ 2 -> 4 -> 5 -> 8;
77
+ 2 -> 6 -> 7 -> 8;
78
+ EOS
79
+ test_layout <<EOS
80
+ multipod -> 2 -> 3;
81
+ 2 -> 4;
82
+ 2 -> 5;
83
+ 2 -> 6;
84
+ 2 -> 7;
85
+ 2 -> 8;
86
+ EOS
87
+ test_layout <<EOS
88
+ mplarge -> 1 -> 2;
89
+ 1 -> 3333333333333333333333333333333333;
90
+ EOS
91
+ test_layout <<EOS
92
+ multif -> 1
93
+ 1 -> a2 -> a3
94
+ a2 -> a222222222 -> a3
95
+ 1 -> b2 -> b3
96
+ b2 -> b222222222 -> b3
97
+ EOS
98
+ test_layout <<EOS
99
+ ifx -> 1 -> 2 -> 3 -> 4 -> 5
100
+ 4 -> eeeeeeeeeeee -> 5
101
+ EOS
102
+ test_layout <<EOS
103
+ llll -> 1 -> 22222222222222222222222222 -> e
104
+ 1 -> 33333333333333333333333333 -> e
105
+ 1 -> 4444444444444444444444 -> e
106
+ 1 -> 5 -> e
107
+ 5 -> 5t -> e
108
+ 1 -> 6 -> e
109
+ 6 -> 6t -> e
110
+ 1 -> 7 -> e
111
+ 7 -> 7t -> e
112
+ EOS
113
+ test_layout <<EOS
114
+ dangling -> 2 -> 11 -> 12 -> 13 -> 4;
115
+ 2 -> 21 -> 22 -> 23 -> 4;
116
+ 2 -> 31 -> 32 -> 33 -> 4;
117
+ 21 -> 2z;
118
+ 31 -> 3z;
119
+ EOS
120
+ test_layout <<EOS
121
+ dangin -> 2 -> 11 -> 12 -> 13;
122
+ 2 -> 21 -> 22 -> 13;
123
+ 2 -> 31 -> 32 -> 33;
124
+ 22 -> 33;
125
+ 21 -> z;
126
+ EOS
127
+ test_layout <<EOS
128
+ cascadeclean -> 2 -> 3 -> 4 -> 5 -> 6 -> 62 -> 52 -> 42 -> 32 -> 22 -> e;
129
+ 2 -> 21 -> 22;
130
+ 3 -> 31 -> 32;
131
+ 4 -> 41 -> 42;
132
+ 5 -> 51 -> 52;
133
+ 6 -> 61 -> 62;
134
+ EOS
135
+ test_layout <<EOS
136
+ cascade -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> e;
137
+ 2 -> 21 -> e;
138
+ 3 -> 31 -> e;
139
+ 4 -> 41 -> e;
140
+ 5 -> 51 -> e;
141
+ 6 -> 61 -> e;
142
+ EOS
143
+ test_layout <<EOS
144
+ rstair -> 2 -> 3 -> 4 -> 5 -> 6;
145
+ 2 -> 4;
146
+ 2 -> 5;
147
+ 2 -> 6;
148
+ EOS
149
+ test_layout <<EOS
150
+ drstair -> 2a -> 3 -> 4 -> 5 -> 6;
151
+ drstair -> 2b -> 4;
152
+ 2a -> 4;
153
+ 2a -> 5;
154
+ 2a -> 6;
155
+ 2b -> 4;
156
+ 2b -> 5;
157
+ 2b -> 6;
158
+ EOS
159
+ test_layout <<EOS
160
+ mrstair -> 2a -> 3a -> 4a -> 5a -> 6a;
161
+ mrstair -> 2b -> 4a;
162
+ 2a -> 4a;
163
+ 2a -> 5a;
164
+ 2a -> 6a;
165
+ 2b -> 4a;
166
+ 2b -> 5a;
167
+ 2b -> 6a;
168
+ 2a -> 3b -> 4b -> 5b -> 6b;
169
+ 2a -> 4b;
170
+ 2a -> 5b;
171
+ 2a -> 6b;
172
+ 2b -> 3b;
173
+ 2b -> 4b;
174
+ 2b -> 5b;
175
+ 2b -> 6b;
176
+ EOS
177
+ test_layout <<EOS
178
+ loop -> 2 -> 3 -> 4;
179
+ 3 -> 2;
180
+ EOS
181
+ test_layout <<EOS
182
+ loopbreak -> 2 -> 3 -> e;
183
+ 2 -> 4 -> 5 -> 6 -> 8 -> e;
184
+ 5 -> 7 -> 4;
185
+ EOS
186
+ test_layout <<EOS
187
+ loopbreak2 -> 2 -> 3 -> e;
188
+ 2 -> 4 -> 5 -> 6 -> 8 -> e;
189
+ 5 -> 7 -> 4;
190
+ 7 -> 8;
191
+ EOS
192
+ test_layout <<EOS
193
+ unbalance -> 2 -> 3 -> 4 -> 5 -> e;
194
+ 2 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> e;
195
+ EOS
196
+ test_layout <<EOS
197
+ unbalance2 -> 2 -> 3 -> e;
198
+ 2 -> 4 -> e;
199
+ 2 -> 5 -> e;
200
+ 2 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> e;
201
+ EOS
202
+ test_layout <<EOS
203
+ unbalance3 -> 2 -> 3 -> e;
204
+ 2 -> 4 -> e;
205
+ 2 -> 5 -> e;
206
+ 2 -> 6 -> e;
207
+ 8 -> 9 -> e;
208
+ 2 -> 7 -> e;
209
+ EOS
210
+ test_layout <<EOS
211
+ disjoint -> 1 -> 2 -> 3 -> 4 -> 5 -> 6;
212
+ l1 -> l2;
213
+ l1 -> l3;
214
+ l1 -> l4;
215
+ l1 -> l5;
216
+ l1 -> l6;
217
+ EOS
218
+
219
+ test_layout <<EOS
220
+ lol -> 2 -> 31 -> 41 -> 5;
221
+ 2 -> 32 -> 42 -> 5;
222
+ 31 -> 42;
223
+ 41 -> 32;
224
+ EOS
225
+ test_layout <<EOS
226
+ nestloop -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> e;
227
+ 6 -> 4;
228
+ 7 -> 3;
229
+ EOS
230
+ test_layout <<EOS
231
+ escape -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8;
232
+ 2 -> 21;
233
+ 4 -> 6;
234
+ EOS
235
+ test_layout <<EOS
236
+ loophead -> 1 -> loophead;
237
+ 2 -> 3 -> 2;
238
+ 3 -> 4;
239
+ 1 -> 4;
240
+ EOS
241
+ test_layout <<EOS
242
+ 1 -> e1;
243
+ l00pz -> 1 -> l00pz;
244
+ l2 -> 2 -> l2;
245
+ 2 -> e1;
246
+ 2 -> e2;
247
+ l3 -> 3 -> l3;
248
+ 3 -> e2;
249
+ EOS
250
+ test_layout <<EOS
251
+ 3loop -> 1 -> 3loop;
252
+ 1 -> 2 -> 3 -> 2;
253
+ 0 -> 00 -> 0 -> 2;
254
+ EOS
255
+ test_layout <<EOS
256
+ foo -> 0 -> 1
257
+ 0 -> 2 -> 3 -> 4 -> 5
258
+ 4 -> 6
259
+ 4 -> 7 -> 5
260
+ 4 -> 8 -> 6
261
+ 2 -> 1 -> 7
262
+ 3 -> 1 -> 8
263
+ EOS
264
+ test_layout <<EOS
265
+ dang -> 2 -> 3 -> 4 -> 5 -> 6 -> 4;
266
+ 2 -> 9;
267
+ 5 -> 9;
268
+ EOS
269
+ test_layout <<EOS
270
+ dang2 -> 2 -> 3 -> 4 -> 5 -> 6 -> 4
271
+ 2 -> 9
272
+ 5 -> 9
273
+ 9 -> a -> 9
274
+ EOS
275
+ test_layout <<EOS
276
+ onlyloop -> onlyloop
277
+ EOS
278
+
279
+ rescue Interrupt
280
+ end
281
+
282
+ if __FILE__ == $0
283
+ $target = ARGV[0]
284
+ test_all
285
+ end