angr 9.2.87__py3-none-manylinux2014_x86_64.whl → 9.2.89__py3-none-manylinux2014_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of angr might be problematic. Click here for more details.

Files changed (248) hide show
  1. angr/__init__.py +4 -1
  2. angr/analyses/decompiler/clinic.py +16 -0
  3. angr/analyses/decompiler/decompiler.py +3 -0
  4. angr/analyses/decompiler/optimization_passes/__init__.py +5 -0
  5. angr/analyses/decompiler/optimization_passes/cross_jump_reverter.py +108 -0
  6. angr/analyses/decompiler/optimization_passes/optimization_pass.py +17 -4
  7. angr/analyses/decompiler/optimization_passes/return_duplicator.py +4 -32
  8. angr/analyses/decompiler/structured_codegen/c.py +12 -2
  9. angr/analyses/decompiler/utils.py +13 -0
  10. angr/analyses/typehoon/dfa.py +108 -0
  11. angr/analyses/typehoon/lifter.py +34 -2
  12. angr/analyses/typehoon/simple_solver.py +1043 -503
  13. angr/analyses/typehoon/translator.py +13 -4
  14. angr/analyses/typehoon/typeconsts.py +117 -36
  15. angr/analyses/typehoon/typehoon.py +31 -11
  16. angr/analyses/typehoon/typevars.py +88 -21
  17. angr/analyses/typehoon/variance.py +10 -0
  18. angr/analyses/variable_recovery/engine_ail.py +28 -9
  19. angr/analyses/variable_recovery/engine_base.py +50 -43
  20. angr/analyses/variable_recovery/variable_recovery_base.py +16 -3
  21. angr/analyses/variable_recovery/variable_recovery_fast.py +14 -5
  22. angr/exploration_techniques/tracer.py +2 -0
  23. angr/misc/autoimport.py +26 -0
  24. angr/procedures/definitions/__init__.py +32 -3
  25. angr/utils/constants.py +1 -0
  26. angr/utils/graph.py +20 -1
  27. {angr-9.2.87.dist-info → angr-9.2.89.dist-info}/METADATA +7 -6
  28. {angr-9.2.87.dist-info → angr-9.2.89.dist-info}/RECORD +32 -244
  29. angr-9.2.89.dist-info/top_level.txt +1 -0
  30. angr/procedures/definitions/ntdll.py +0 -12
  31. angr-9.2.87.dist-info/top_level.txt +0 -2
  32. tests/__init__.py +0 -0
  33. tests/analyses/__init__.py +0 -0
  34. tests/analyses/cfg/__init__.py +0 -0
  35. tests/analyses/cfg/test_cfg_clflush.py +0 -43
  36. tests/analyses/cfg/test_cfg_get_any_node.py +0 -34
  37. tests/analyses/cfg/test_cfg_manager.py +0 -32
  38. tests/analyses/cfg/test_cfg_model.py +0 -55
  39. tests/analyses/cfg/test_cfg_patching.py +0 -378
  40. tests/analyses/cfg/test_cfg_rust_got_resolution.py +0 -36
  41. tests/analyses/cfg/test_cfg_thumb_firmware.py +0 -50
  42. tests/analyses/cfg/test_cfg_vex_postprocessor.py +0 -27
  43. tests/analyses/cfg/test_cfgemulated.py +0 -634
  44. tests/analyses/cfg/test_cfgfast.py +0 -1123
  45. tests/analyses/cfg/test_cfgfast_soot.py +0 -38
  46. tests/analyses/cfg/test_const_resolver.py +0 -38
  47. tests/analyses/cfg/test_iat_resolver.py +0 -37
  48. tests/analyses/cfg/test_jumptables.py +0 -3008
  49. tests/analyses/cfg/test_noop_blocks.py +0 -54
  50. tests/analyses/cfg_slice_to_sink/__init__.py +0 -0
  51. tests/analyses/cfg_slice_to_sink/test_cfg_slice_to_sink.py +0 -93
  52. tests/analyses/cfg_slice_to_sink/test_graph.py +0 -114
  53. tests/analyses/cfg_slice_to_sink/test_transitions.py +0 -28
  54. tests/analyses/decompiler/__init__.py +0 -0
  55. tests/analyses/decompiler/test_baseptr_save_simplifier.py +0 -80
  56. tests/analyses/decompiler/test_decompiler.py +0 -3336
  57. tests/analyses/decompiler/test_peephole_optimizations.py +0 -48
  58. tests/analyses/decompiler/test_propagator_loops.py +0 -101
  59. tests/analyses/decompiler/test_structurer.py +0 -275
  60. tests/analyses/reaching_definitions/__init__.py +0 -0
  61. tests/analyses/reaching_definitions/test_dep_graph.py +0 -432
  62. tests/analyses/reaching_definitions/test_function_handler.py +0 -131
  63. tests/analyses/reaching_definitions/test_heap_allocator.py +0 -46
  64. tests/analyses/reaching_definitions/test_rd_state.py +0 -78
  65. tests/analyses/reaching_definitions/test_reachingdefinitions.py +0 -463
  66. tests/analyses/reaching_definitions/test_subject.py +0 -76
  67. tests/analyses/test_bindiff.py +0 -52
  68. tests/analyses/test_block_simplifier.py +0 -112
  69. tests/analyses/test_boyscout.py +0 -104
  70. tests/analyses/test_calling_convention_analysis.py +0 -352
  71. tests/analyses/test_callsite_maker.py +0 -60
  72. tests/analyses/test_cdg.py +0 -165
  73. tests/analyses/test_cfb.py +0 -37
  74. tests/analyses/test_class_identifier.py +0 -46
  75. tests/analyses/test_clinic.py +0 -30
  76. tests/analyses/test_codetagging.py +0 -32
  77. tests/analyses/test_constantpropagation.py +0 -88
  78. tests/analyses/test_ddg.py +0 -95
  79. tests/analyses/test_ddg_global_var_dependencies.py +0 -83
  80. tests/analyses/test_ddg_memvar_addresses.py +0 -40
  81. tests/analyses/test_disassembly.py +0 -121
  82. tests/analyses/test_find_objects_static.py +0 -35
  83. tests/analyses/test_flirt.py +0 -49
  84. tests/analyses/test_identifier.py +0 -33
  85. tests/analyses/test_init_finder.py +0 -38
  86. tests/analyses/test_proximitygraph.py +0 -31
  87. tests/analyses/test_reassembler.py +0 -295
  88. tests/analyses/test_regionidentifier.py +0 -27
  89. tests/analyses/test_slicing.py +0 -164
  90. tests/analyses/test_stack_pointer_tracker.py +0 -74
  91. tests/analyses/test_static_hooker.py +0 -28
  92. tests/analyses/test_typehoon.py +0 -55
  93. tests/analyses/test_variablerecovery.py +0 -464
  94. tests/analyses/test_vfg.py +0 -221
  95. tests/analyses/test_vtable.py +0 -31
  96. tests/analyses/test_xrefs.py +0 -77
  97. tests/common.py +0 -128
  98. tests/engines/__init__.py +0 -0
  99. tests/engines/light/__init__.py +0 -0
  100. tests/engines/light/test_data.py +0 -17
  101. tests/engines/pcode/__init__.py +0 -0
  102. tests/engines/pcode/test_emulate.py +0 -607
  103. tests/engines/pcode/test_pcode.py +0 -84
  104. tests/engines/test_actions.py +0 -27
  105. tests/engines/test_hook.py +0 -112
  106. tests/engines/test_java.py +0 -697
  107. tests/engines/test_unicorn.py +0 -518
  108. tests/engines/vex/__init__.py +0 -0
  109. tests/engines/vex/test_lifter.py +0 -124
  110. tests/engines/vex/test_vex.py +0 -574
  111. tests/exploration_techniques/__init__.py +0 -0
  112. tests/exploration_techniques/test_cacher.py +0 -45
  113. tests/exploration_techniques/test_director.py +0 -67
  114. tests/exploration_techniques/test_driller_core.py +0 -48
  115. tests/exploration_techniques/test_loop_seer.py +0 -158
  116. tests/exploration_techniques/test_memory_watcher.py +0 -46
  117. tests/exploration_techniques/test_oppologist.py +0 -65
  118. tests/exploration_techniques/test_spiller.py +0 -82
  119. tests/exploration_techniques/test_stochastic.py +0 -40
  120. tests/exploration_techniques/test_tech_builder.py +0 -61
  121. tests/exploration_techniques/test_tracer.py +0 -856
  122. tests/exploration_techniques/test_unique.py +0 -40
  123. tests/exploration_techniques/test_veritesting.py +0 -120
  124. tests/factory/__init__.py +0 -0
  125. tests/factory/block/__init__.py +0 -0
  126. tests/factory/block/test_block_cache.py +0 -33
  127. tests/factory/block/test_keystone.py +0 -106
  128. tests/factory/test_argc.py +0 -101
  129. tests/factory/test_argc_sym.py +0 -110
  130. tests/factory/test_argv.py +0 -158
  131. tests/factory/test_callable.py +0 -266
  132. tests/factory/test_windows_args.py +0 -36
  133. tests/knowledge_plugins/__init__.py +0 -0
  134. tests/knowledge_plugins/cfg/__init__.py +0 -0
  135. tests/knowledge_plugins/cfg/test_cfg_manager.py +0 -36
  136. tests/knowledge_plugins/functions/__init__.py +0 -0
  137. tests/knowledge_plugins/functions/test_function.py +0 -91
  138. tests/knowledge_plugins/functions/test_function2.py +0 -79
  139. tests/knowledge_plugins/functions/test_function_manager.py +0 -139
  140. tests/knowledge_plugins/functions/test_prototypes.py +0 -53
  141. tests/knowledge_plugins/key_definitions/__init__.py +0 -0
  142. tests/knowledge_plugins/key_definitions/test_atoms.py +0 -24
  143. tests/knowledge_plugins/key_definitions/test_environment.py +0 -126
  144. tests/knowledge_plugins/key_definitions/test_heap_address.py +0 -27
  145. tests/knowledge_plugins/key_definitions/test_live_definitions.py +0 -72
  146. tests/knowledge_plugins/test_dwarf_variables.py +0 -240
  147. tests/knowledge_plugins/test_kb_plugins.py +0 -91
  148. tests/knowledge_plugins/test_kb_plugins_dwarf.py +0 -36
  149. tests/knowledge_plugins/test_patches.py +0 -48
  150. tests/misc/__init__.py +0 -0
  151. tests/misc/test_hookset.py +0 -57
  152. tests/perf/__init__.py +0 -0
  153. tests/perf/perf_cfgemulated.py +0 -19
  154. tests/perf/perf_cfgfast.py +0 -18
  155. tests/perf/perf_concrete_execution.py +0 -41
  156. tests/perf/perf_siminspect_nop.py +0 -36
  157. tests/perf/perf_state_copy.py +0 -33
  158. tests/perf/perf_unicorn_0.py +0 -27
  159. tests/perf/perf_unicorn_1.py +0 -23
  160. tests/procedures/__init__.py +0 -0
  161. tests/procedures/glibc/__init__.py +0 -0
  162. tests/procedures/glibc/test_ctype_locale.py +0 -164
  163. tests/procedures/libc/__init__.py +0 -0
  164. tests/procedures/libc/test_fgets.py +0 -53
  165. tests/procedures/libc/test_scanf.py +0 -205
  166. tests/procedures/libc/test_sprintf.py +0 -44
  167. tests/procedures/libc/test_sscanf.py +0 -63
  168. tests/procedures/libc/test_strcasecmp.py +0 -37
  169. tests/procedures/libc/test_string.py +0 -1102
  170. tests/procedures/libc/test_strtol.py +0 -78
  171. tests/procedures/linux_kernel/__init__.py +0 -0
  172. tests/procedures/linux_kernel/test_lseek.py +0 -174
  173. tests/procedures/posix/__init__.py +0 -0
  174. tests/procedures/posix/test_chroot.py +0 -33
  175. tests/procedures/posix/test_getenv.py +0 -78
  176. tests/procedures/posix/test_pwrite_pread.py +0 -57
  177. tests/procedures/posix/test_sim_time.py +0 -46
  178. tests/procedures/posix/test_unlink.py +0 -46
  179. tests/procedures/test_project_resolve_simproc.py +0 -43
  180. tests/procedures/test_sim_procedure.py +0 -117
  181. tests/procedures/test_stub_procedure_args.py +0 -53
  182. tests/serialization/__init__.py +0 -0
  183. tests/serialization/test_db.py +0 -197
  184. tests/serialization/test_pickle.py +0 -95
  185. tests/serialization/test_serialization.py +0 -132
  186. tests/serialization/test_vault.py +0 -169
  187. tests/sim/__init__.py +0 -3
  188. tests/sim/exec_func/__init__.py +0 -0
  189. tests/sim/exec_func/test_mem_funcs.py +0 -55
  190. tests/sim/exec_func/test_str_funcs.py +0 -93
  191. tests/sim/exec_func/test_syscall_result.py +0 -39
  192. tests/sim/exec_insn/__init__.py +0 -0
  193. tests/sim/exec_insn/test_adc.py +0 -44
  194. tests/sim/exec_insn/test_ops.py +0 -83
  195. tests/sim/exec_insn/test_rcr.py +0 -26
  196. tests/sim/exec_insn/test_rol.py +0 -51
  197. tests/sim/exec_insn/test_signed_div.py +0 -34
  198. tests/sim/exec_insn/test_sqrt.py +0 -56
  199. tests/sim/options/__init__.py +0 -0
  200. tests/sim/options/test_0div.py +0 -54
  201. tests/sim/options/test_symbolic_fd.py +0 -59
  202. tests/sim/options/test_unsupported.py +0 -34
  203. tests/sim/test_accuracy.py +0 -137
  204. tests/sim/test_checkbyte.py +0 -53
  205. tests/sim/test_echo.py +0 -36
  206. tests/sim/test_fauxware.py +0 -202
  207. tests/sim/test_self_modifying_code.py +0 -65
  208. tests/sim/test_simple_api.py +0 -36
  209. tests/sim/test_simulation_manager.py +0 -147
  210. tests/sim/test_stack_alignment.py +0 -65
  211. tests/sim/test_state.py +0 -303
  212. tests/sim/test_state_customization.py +0 -54
  213. tests/sim/test_symbol_hooked_by.py +0 -49
  214. tests/simos/__init__.py +0 -0
  215. tests/simos/windows/__init__.py +0 -0
  216. tests/simos/windows/test_windows_stack_cookie.py +0 -58
  217. tests/state_plugins/__init__.py +0 -0
  218. tests/state_plugins/inspect/__init__.py +0 -0
  219. tests/state_plugins/inspect/test_inspect.py +0 -310
  220. tests/state_plugins/inspect/test_syscall_override.py +0 -90
  221. tests/state_plugins/posix/__init__.py +0 -0
  222. tests/state_plugins/posix/test_file_struct_funcs.py +0 -56
  223. tests/state_plugins/posix/test_files.py +0 -69
  224. tests/state_plugins/posix/test_posix.py +0 -72
  225. tests/state_plugins/solver/__init__.py +0 -0
  226. tests/state_plugins/solver/test_simsolver.py +0 -58
  227. tests/state_plugins/solver/test_symbolic.py +0 -153
  228. tests/state_plugins/solver/test_variable_registration.py +0 -46
  229. tests/state_plugins/test_callstack.py +0 -54
  230. tests/state_plugins/test_gdb_plugin.py +0 -35
  231. tests/state_plugins/test_multi_open_file.py +0 -47
  232. tests/state_plugins/test_symbolization.py +0 -38
  233. tests/storage/__init__.py +0 -0
  234. tests/storage/test_memory.py +0 -960
  235. tests/storage/test_memory_merge.py +0 -114
  236. tests/storage/test_memview.py +0 -205
  237. tests/storage/test_mmap.py +0 -26
  238. tests/storage/test_multivalues.py +0 -44
  239. tests/storage/test_permissions.py +0 -32
  240. tests/storage/test_ptmalloc.py +0 -291
  241. tests/storage/test_relro_perm.py +0 -49
  242. tests/test_calling_conventions.py +0 -86
  243. tests/test_types.py +0 -329
  244. tests/utils/__init__.py +0 -0
  245. tests/utils/test_graph.py +0 -41
  246. {angr-9.2.87.dist-info → angr-9.2.89.dist-info}/LICENSE +0 -0
  247. {angr-9.2.87.dist-info → angr-9.2.89.dist-info}/WHEEL +0 -0
  248. {angr-9.2.87.dist-info → angr-9.2.89.dist-info}/entry_points.txt +0 -0
@@ -1,960 +0,0 @@
1
- #!/usr/bin/env python3
2
- # pylint: disable=missing-class-docstring,no-self-use,line-too-long
3
-
4
- import time
5
- import unittest
6
-
7
- import claripy
8
-
9
- from angr.storage.memory_mixins import (
10
- DataNormalizationMixin,
11
- SizeNormalizationMixin,
12
- AddressConcretizationMixin,
13
- UltraPagesMixin,
14
- ListPagesMixin,
15
- PagedMemoryMixin,
16
- MultiValuedMemory,
17
- MVListPagesMixin,
18
- )
19
- from angr.storage.memory_mixins.paged_memory.pages.multi_values import MultiValues
20
- from angr import SimState, SIM_PROCEDURES
21
- from angr import options as o
22
- from angr.state_plugins import SimSystemPosix, SimLightRegisters
23
- from angr.storage.file import SimFile
24
-
25
-
26
- class UltraPageMemory(
27
- DataNormalizationMixin,
28
- SizeNormalizationMixin,
29
- AddressConcretizationMixin,
30
- UltraPagesMixin,
31
- PagedMemoryMixin,
32
- ):
33
- pass
34
-
35
-
36
- class ListPageMemory(
37
- DataNormalizationMixin,
38
- SizeNormalizationMixin,
39
- AddressConcretizationMixin,
40
- ListPagesMixin,
41
- PagedMemoryMixin,
42
- ):
43
- pass
44
-
45
-
46
- class MVPageMemory(
47
- SizeNormalizationMixin,
48
- AddressConcretizationMixin,
49
- MVListPagesMixin,
50
- PagedMemoryMixin,
51
- ):
52
- pass
53
-
54
-
55
- class TestMemory(unittest.TestCase):
56
- def test_copy(self):
57
- s = SimState(arch="AMD64", mode="symbolic")
58
- s.memory.store(0x100, b"ABCDEFGHIJKLMNOP")
59
- s.memory.store(0x200, b"XXXXXXXXXXXXXXXX")
60
- x = s.solver.BVS("size", s.arch.bits)
61
- s.add_constraints(s.solver.ULT(x, 10))
62
- s.memory.copy_contents(0x200, 0x100, x)
63
-
64
- assert sorted(s.solver.eval_upto(x, 100)) == list(range(10))
65
- result = s.memory.load(0x200, 5)
66
- assert sorted(s.solver.eval_upto(result, 100, cast_to=bytes)) == [
67
- b"ABCDE",
68
- b"ABCDX",
69
- b"ABCXX",
70
- b"ABXXX",
71
- b"AXXXX",
72
- b"XXXXX",
73
- ]
74
- assert sorted(s.solver.eval_upto(result, 100, cast_to=bytes, extra_constraints=[x == 3])) == [b"ABCXX"]
75
-
76
- s = SimState(arch="AMD64", mode="symbolic")
77
- s.register_plugin(
78
- "posix", SimSystemPosix(stdin=SimFile(name="stdin", content=b"ABCDEFGHIJKLMNOP", has_end=True))
79
- )
80
- s.memory.store(0x200, b"XXXXXXXXXXXXXXXX")
81
- x = s.solver.BVS("size", s.arch.bits)
82
- s.add_constraints(s.solver.ULT(x, 10))
83
-
84
- s.posix.get_fd(0).read(0x200, x)
85
- assert sorted(s.solver.eval_upto(x, 100)) == list(range(10))
86
- result = s.memory.load(0x200, 5)
87
- assert sorted(s.solver.eval_upto(result, 100, cast_to=bytes)) == [
88
- b"ABCDE",
89
- b"ABCDX",
90
- b"ABCXX",
91
- b"ABXXX",
92
- b"AXXXX",
93
- b"XXXXX",
94
- ]
95
- assert sorted(s.solver.eval_upto(result, 100, cast_to=bytes, extra_constraints=[x == 3])) == [b"ABCXX"]
96
-
97
- s = SimState(arch="AMD64", mode="symbolic")
98
- s.register_plugin("posix", SimSystemPosix(stdin=SimFile(name="stdin", content=b"ABCDEFGHIJKLMNOP")))
99
- s.memory.store(0x200, b"XXXXXXXXXXXXXXXX")
100
- x = s.solver.BVS("size", s.arch.bits)
101
- s.add_constraints(s.solver.ULT(x, 10))
102
-
103
- read_proc = SIM_PROCEDURES["posix"]["read"]()
104
- ret_x = read_proc.execute(s, arguments=(0, 0x200, x)).ret_expr
105
- assert sorted(s.solver.eval_upto(x, 100)) == list(range(10))
106
- result = s.memory.load(0x200, 5)
107
- assert sorted(s.solver.eval_upto(result, 100, cast_to=bytes)) == [
108
- b"ABCDE",
109
- b"ABCDX",
110
- b"ABCXX",
111
- b"ABXXX",
112
- b"AXXXX",
113
- b"XXXXX",
114
- ]
115
- assert sorted(s.solver.eval_upto(result, 100, cast_to=bytes, extra_constraints=[x == 3])) == [b"ABCXX"]
116
-
117
- assert sorted(s.solver.eval_upto(ret_x, 100)) == list(range(10))
118
- assert sorted(s.solver.eval_upto(result, 100, cast_to=bytes, extra_constraints=[ret_x == 3])) == [b"ABCXX"]
119
-
120
- @staticmethod
121
- def _concrete_memory_tests(s):
122
- # Store a 4-byte variable to memory directly...
123
- s.memory.store(100, s.solver.BVV(0x1337, 32))
124
- # ... then load it
125
-
126
- expr = s.memory.load(100, 4)
127
- assert expr is s.solver.BVV(0x1337, 32)
128
- expr = s.memory.load(100, 2)
129
- assert expr is s.solver.BVV(0, 16)
130
- expr = s.memory.load(102, 2)
131
- assert expr is s.solver.BVV(0x1337, 16)
132
-
133
- # partially symbolic
134
- expr = s.memory.load(102, 4)
135
- assert expr.length == 32
136
- assert s.solver.min(expr) == 0x13370000
137
- assert s.solver.max(expr) == 0x1337FFFF
138
-
139
- # partial overwrite
140
- s.memory.store(101, s.solver.BVV(0x1415, 16))
141
- expr = s.memory.load(101, 3)
142
- assert expr is s.solver.BVV(0x141537, 24)
143
- expr = s.memory.load(100, 2)
144
- assert s.solver.min(expr) == 0x14
145
- expr = s.memory.load(102, 2)
146
- assert expr is s.solver.BVV(0x1537, 16)
147
- expr = s.memory.load(102, 2, endness="Iend_LE")
148
- assert expr is s.solver.BVV(0x3715, 16)
149
-
150
- s.memory.store(0x100, s.solver.BVV(b"AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH"), endness="Iend_LE")
151
- expr = s.memory.load(0x104, 13)
152
- assert expr is s.solver.BVV(b"GGGGFFFFEEEED")
153
-
154
- # branching
155
- s2 = s.copy()
156
- s2a = s2.copy()
157
- s2b = s2.copy()
158
-
159
- s2a.memory.store(0x100, s.solver.BVV(b"A"))
160
- s2b.memory.store(0x100, s.solver.BVV(b"B"))
161
- assert s2b.memory.load(0x100, 1) is s.solver.BVV(b"B")
162
- assert s2a.memory.load(0x100, 1) is s.solver.BVV(b"A")
163
-
164
- ## pylint: disable=R0904
165
- def test_memory(self):
166
- initial_memory = {0: b"A", 1: b"A", 2: b"A", 3: b"A", 10: b"B"}
167
- s = SimState(
168
- arch="AMD64",
169
- dict_memory_backer=initial_memory,
170
- add_options={o.REVERSE_MEMORY_NAME_MAP, o.REVERSE_MEMORY_HASH_MAP},
171
- )
172
-
173
- self._concrete_memory_tests(s)
174
- # concrete address and partially symbolic result
175
- expr = s.memory.load(2, 4)
176
- expr = s.memory.load(2, 4)
177
- expr = s.memory.load(2, 4)
178
- expr = s.memory.load(2, 4)
179
- assert s.solver.symbolic(expr)
180
- assert not s.solver.unique(expr)
181
- assert s.solver.eval(expr) >= 0x41410000
182
- assert s.solver.eval(expr) <= 0x41420000
183
- assert s.solver.min_int(expr) == 0x41410000
184
- assert s.solver.max_int(expr) == 0x4141FFFF
185
-
186
- # concrete address and concrete result
187
- expr = s.memory.load(0, 4) # Returns: a z3 BVS representing 0x41414141
188
- assert not s.solver.symbolic(expr)
189
- assert s.solver.eval(expr) == 0x41414141
190
-
191
- c = s.solver.BVS("condition", 8)
192
- expr = s.memory.load(10, 1, condition=c == 1, fallback=s.solver.BVV(b"X"))
193
- assert s.solver.eval_upto(expr, 10, cast_to=bytes, extra_constraints=[c == 1]) == [b"B"]
194
- assert s.solver.eval_upto(expr, 10, cast_to=bytes, extra_constraints=[c != 1]) == [b"X"]
195
-
196
- x = s.solver.BVS("ref_test", 16, explicit_name=True)
197
- s.memory.store(0x1000, x)
198
- s.memory.store(0x2000, x)
199
- assert set(s.memory.addrs_for_name("ref_test")) == {0x1000, 0x1001, 0x2000, 0x2001}
200
- assert set(s.memory.addrs_for_hash(x.cache_key)) == {0x1000, 0x1001, 0x2000, 0x2001}
201
-
202
- s2 = s.copy()
203
- y = s2.solver.BVS("ref_test2", 16, explicit_name=True)
204
- s2.memory.store(0x2000, y)
205
- assert s2.memory.load(0x2000, 2) is y
206
- assert s.memory.load(0x2000, 2) is x
207
- assert set(s.memory.addrs_for_name("ref_test")) == {0x1000, 0x1001, 0x2000, 0x2001}
208
- assert set(s.memory.addrs_for_hash(x.cache_key)) == {0x1000, 0x1001, 0x2000, 0x2001}
209
- assert set(s2.memory.addrs_for_name("ref_test")) == {0x1000, 0x1001}
210
- assert set(s2.memory.addrs_for_hash(x.cache_key)) == {0x1000, 0x1001}
211
- assert set(s2.memory.addrs_for_name("ref_test2")) == {0x2000, 0x2001}
212
- assert set(s2.memory.addrs_for_hash(y.cache_key)) == {0x2000, 0x2001}
213
-
214
- s.memory.store(0x3000, s.solver.BVS("replace_old", 32, explicit_name=True))
215
- s.memory.store(0x3001, s.solver.BVV(b"AB"))
216
- assert set(s.memory.addrs_for_name("replace_old")) == {0x3000, 0x3003}
217
- assert s.solver.eval_upto(s.memory.load(0x3001, 2), 10, cast_to=bytes) == [b"AB"]
218
-
219
- # n = s.solver.BVS('replace_new', 32, explicit_name=True)
220
- # c = s.solver.BVS('replace_cool', 32, explicit_name=True)
221
-
222
- # mo = s.memory.memory_objects_for_name('replace_old')
223
- # assert len(mo) == 1
224
- # s.memory.replace_memory_object(next(iter(mo)), n)
225
- # assert set(s.memory.addrs_for_name('replace_old')) == set()
226
- # assert set(s.memory.addrs_for_name('replace_new')) == {0x3000, 0x3003}
227
- # assert s.solver.eval_upto(s.memory.load(0x3001, 2), 10, cast_to=bytes) == [b"AB"]
228
-
229
- # s.memory.store(0x4000, s.solver.If(n == 0, n+10, n+20))
230
-
231
- # assert set(s.memory.addrs_for_name('replace_new')) == {0x3000, 0x3003, 0x4000, 0x4001, 0x4002, 0x4003}
232
- # s.memory.replace_all(n, c)
233
- # assert set(s.memory.addrs_for_name('replace_old')) == set()
234
- # assert set(s.memory.addrs_for_name('replace_new')) == set()
235
- # assert set(s.memory.addrs_for_name('replace_cool')) == {0x3000, 0x3003, 0x4000, 0x4001, 0x4002, 0x4003}
236
- # assert s.solver.eval_upto(s.memory.load(0x3001, 2), 10, cast_to=bytes) == [b"AB"]
237
-
238
- # z = s.solver.BVV(0, 32)
239
- # s.memory.replace_all(c, z)
240
- # assert set(s.memory.addrs_for_name('replace_old')) == set()
241
- # assert set(s.memory.addrs_for_name('replace_new')) == set()
242
- # assert set(s.memory.addrs_for_name('replace_cool')) == set()
243
- # assert s.solver.eval_upto(s.memory.load(0x3001, 2), 10, cast_to=bytes) == [b"AB"]
244
- # assert s.solver.eval_upto(s.memory.load(0x3000, 4), 10) == [0x00414200]
245
- # assert s.solver.eval_upto(s.memory.load(0x4000, 4), 10) == [0x0000000a]
246
-
247
- # symbolic length
248
- x = s.solver.BVV(0x11223344, 32)
249
- y = s.solver.BVV(0xAABBCCDD, 32)
250
- n = s.solver.BVS("size", 32)
251
- s.add_constraints(n <= 4)
252
- s.memory.store(0x5000, x)
253
- s.memory.store(0x5000, y, size=n)
254
- assert set(s.solver.eval_upto(s.memory.load(0x5000, 4), 10)) == {
255
- 0x11223344,
256
- 0xAA223344,
257
- 0xAABB3344,
258
- 0xAABBCC44,
259
- 0xAABBCCDD,
260
- }
261
-
262
- s1 = s.copy()
263
- s1.add_constraints(n == 1)
264
- assert set(s1.solver.eval_upto(s1.memory.load(0x5000, 4), 10)) == {0xAA223344}
265
-
266
- s4 = s.copy()
267
- s4.add_constraints(n == 4)
268
- assert set(s4.solver.eval_upto(s4.memory.load(0x5000, 4), 10)) == {0xAABBCCDD}
269
-
270
- # condition without fallback
271
- x = s.solver.BVV(0x11223344, 32)
272
- y = s.solver.BVV(0xAABBCCDD, 32)
273
- c = s.solver.BVS("condition", 32)
274
- s.memory.store(0x6000, x)
275
- s.memory.store(0x6000, y, condition=c == 1)
276
- assert set(s.solver.eval_upto(s.memory.load(0x6000, 4), 10)) == {0x11223344, 0xAABBCCDD}
277
-
278
- s0 = s.copy()
279
- s0.add_constraints(c == 0)
280
- assert set(s0.solver.eval_upto(s0.memory.load(0x6000, 4), 10)) == {0x11223344}
281
-
282
- s1 = s.copy()
283
- s1.add_constraints(c == 1)
284
- assert set(s1.solver.eval_upto(s1.memory.load(0x6000, 4), 10)) == {0xAABBCCDD}
285
-
286
- # condition with symbolic size
287
- x = s.solver.BVV(0x11223344, 32)
288
- y = s.solver.BVV(0xAABBCCDD, 32)
289
- c = s.solver.BVS("condition", 32)
290
- n = s.solver.BVS("size", 32)
291
- s.add_constraints(n <= 4)
292
- s.memory.store(0x8000, x)
293
- s.memory.store(0x8000, y, condition=c == 1, size=n)
294
-
295
- s0 = s.copy()
296
- s0.add_constraints(c == 0)
297
- assert set(s0.solver.eval_upto(s0.memory.load(0x8000, 4), 10)) == {0x11223344}
298
-
299
- s1 = s.copy()
300
- s1.add_constraints(c == 1)
301
- assert set(s1.solver.eval_upto(s1.memory.load(0x8000, 4), 10)) == {
302
- 0x11223344,
303
- 0xAA223344,
304
- 0xAABB3344,
305
- 0xAABBCC44,
306
- 0xAABBCCDD,
307
- }
308
-
309
- def test_abstract_memory(self):
310
- initial_memory = {0: b"A", 1: b"B", 2: b"C", 3: b"D"}
311
-
312
- s = SimState(
313
- mode="static",
314
- arch="AMD64",
315
- dict_memory_backer=initial_memory,
316
- add_options={o.ABSTRACT_SOLVER, o.ABSTRACT_MEMORY},
317
- )
318
- se = s.solver
319
-
320
- def to_vs(region, offset):
321
- return s.solver.VS(s.arch.bits, region, 0, offset)
322
-
323
- # Load a single-byte constant from global region
324
- expr = s.memory.load(to_vs("global", 2), 1)
325
- assert s.solver.eval(expr) == 0x43
326
- assert s.solver.max_int(expr) == 0x43
327
- assert s.solver.min_int(expr) == 0x43
328
-
329
- # Store a single-byte constant to global region
330
- s.memory.store(to_vs("global", 1), s.solver.BVV(b"D"), 1)
331
- expr = s.memory.load(to_vs("global", 1), 1)
332
- assert s.solver.eval(expr) == 0x44
333
-
334
- # Store a single-byte StridedInterval to global region
335
- si_0 = s.solver.BVS("unnamed", 8, 10, 20, 2)
336
- s.memory.store(to_vs("global", 4), si_0)
337
-
338
- # Load the single-byte StridedInterval from global region
339
- expr = s.memory.load(to_vs("global", 4), 1)
340
- assert s.solver.min_int(expr) == 10
341
- assert s.solver.max_int(expr) == 20
342
- assert s.solver.eval_upto(expr, 100) == [10, 12, 14, 16, 18, 20]
343
-
344
- # Store a two-byte StridedInterval object to global region
345
- si_1 = s.solver.BVS("unnamed", 16, 10, 20, 2)
346
- s.memory.store(to_vs("global", 5), si_1)
347
-
348
- # Load the two-byte StridedInterval object from global region
349
- expr = s.memory.load(to_vs("global", 5), 2)
350
- assert claripy.backends.vsa.identical(expr, si_1)
351
-
352
- # Store a four-byte StridedInterval object to global region
353
- si_2 = s.solver.BVS("unnamed", 32, 8000, 9000, 2)
354
- s.memory.store(to_vs("global", 7), si_2)
355
-
356
- # Load the four-byte StridedInterval object from global region
357
- expr = s.memory.load(to_vs("global", 7), 4)
358
- assert claripy.backends.vsa.identical(expr, s.solver.BVS("unnamed", 32, 8000, 9000, 2))
359
-
360
- # Test default values
361
- s.options.remove(o.SYMBOLIC_INITIAL_VALUES)
362
- expr = s.memory.load(to_vs("global", 100), 4)
363
- assert claripy.backends.vsa.identical(expr, s.solver.BVS("unnamed", 32, 0, 0, 0))
364
-
365
- # Test default values (symbolic)
366
- s.options.add(o.SYMBOLIC_INITIAL_VALUES)
367
- expr = s.memory.load(to_vs("global", 104), 4)
368
- assert claripy.backends.vsa.identical(expr, s.solver.BVS("unnamed", 32, 0, 0xFFFFFFFF, 1))
369
- assert claripy.backends.vsa.identical(expr, s.solver.BVS("unnamed", 32, -0x80000000, 0x7FFFFFFF, 1))
370
-
371
- #
372
- # Merging
373
- #
374
-
375
- # Merging two one-byte values
376
- s.memory.store(to_vs("function_merge", 0), s.solver.BVS("unnamed", 8, 0x10, 0x10, 0))
377
- a = s.copy()
378
- a.memory.store(to_vs("function_merge", 0), s.solver.BVS("unnamed", 8, 0x20, 0x20, 0))
379
-
380
- b = s.merge(a)[0]
381
- expr = b.memory.load(to_vs("function_merge", 0), 1)
382
- assert claripy.backends.vsa.identical(expr, s.solver.BVS("unnamed", 8, 0x10, 0x20, 0x10))
383
-
384
- # | MO(value_0) |
385
- # | MO(value_1) |
386
- # 0x20 0x24
387
- # Merge one byte in value_0/1 means merging the entire MemoryObject
388
- a = s.copy()
389
- a.memory.store(
390
- to_vs("function_merge", 0x20), se.SI(bits=32, stride=0, lower_bound=0x100000, upper_bound=0x100000)
391
- )
392
- b = s.copy()
393
- b.memory.store(
394
- to_vs("function_merge", 0x20), se.SI(bits=32, stride=0, lower_bound=0x100001, upper_bound=0x100001)
395
- )
396
- c = a.merge(b)[0]
397
- expr = c.memory.load(to_vs("function_merge", 0x20), 4)
398
- assert claripy.backends.vsa.identical(
399
- expr, se.SI(bits=32, stride=1, lower_bound=0x100000, upper_bound=0x100001)
400
- )
401
- c_page = c.memory._regions["function_merge"]._pages[0]
402
- object_set = {
403
- c_page._get_object(0x20, 0),
404
- c_page._get_object(0x21, 0),
405
- c_page._get_object(0x22, 0),
406
- c_page._get_object(0x23, 0),
407
- }
408
- assert len(object_set) == 1
409
-
410
- a = s.copy()
411
- a.memory.store(
412
- to_vs("function_merge", 0x20), se.SI(bits=32, stride=0x100000, lower_bound=0x100000, upper_bound=0x200000)
413
- )
414
- b = s.copy()
415
- b.memory.store(
416
- to_vs("function_merge", 0x20), se.SI(bits=32, stride=0, lower_bound=0x300000, upper_bound=0x300000)
417
- )
418
- c = a.merge(b)[0]
419
- expr = c.memory.load(to_vs("function_merge", 0x20), 4)
420
- assert claripy.backends.vsa.identical(
421
- expr, se.SI(bits=32, stride=0x100000, lower_bound=0x100000, upper_bound=0x300000)
422
- )
423
- object_set = {
424
- c_page._get_object(0x20, 0),
425
- c_page._get_object(0x21, 0),
426
- c_page._get_object(0x22, 0),
427
- c_page._get_object(0x23, 0),
428
- }
429
- assert len(object_set) == 1
430
-
431
- #
432
- # Widening
433
- #
434
-
435
- a = s.solver.SI(bits=32, stride=1, lower_bound=1, upper_bound=2)
436
- b = s.solver.SI(bits=32, stride=1, lower_bound=1, upper_bound=3)
437
- a = a.reversed
438
- b = b.reversed
439
- # widened = a.widen(b)
440
- # TODO: Added a proper test case
441
- # print widened.reversed
442
-
443
- # We are done!
444
- # Restore the old claripy standalone object
445
- # claripy.set_claripy(old_claripy_standalone)
446
-
447
- def test_abstract_memory_find(self):
448
- initial_memory = {1: b"A", 2: b"B", 3: b"\x00"}
449
-
450
- s = SimState(
451
- mode="static",
452
- arch="AMD64",
453
- dict_memory_backer=initial_memory,
454
- add_options={o.ABSTRACT_SOLVER, o.ABSTRACT_MEMORY},
455
- )
456
-
457
- se = s.solver
458
- BVV = se.BVV
459
- VS = se.VS
460
- SI = se.SI
461
-
462
- s.memory.store(4, se.TSI(bits=64))
463
-
464
- def to_vs(region, offset):
465
- return VS(s.arch.bits, region, 0, offset)
466
-
467
- r, _, _ = s.memory.find(to_vs("global", 1), BVV(b"A"), 8)
468
-
469
- r_model = claripy.backends.vsa.convert(r)
470
- s_expected = claripy.backends.vsa.convert(SI(bits=64, to_conv=1))
471
- assert isinstance(r_model, claripy.vsa.ValueSet)
472
- assert list(r_model.regions.keys()) == ["global"]
473
- assert claripy.backends.vsa.identical(r_model.regions["global"], s_expected)
474
-
475
- r, _, _ = s.memory.find(to_vs("global", 1), BVV(b"B"), 8)
476
- r_model = claripy.backends.vsa.convert(r)
477
- s_expected = claripy.backends.vsa.convert(SI(bits=64, to_conv=2))
478
- assert isinstance(r_model, claripy.vsa.ValueSet)
479
- assert list(r_model.regions.keys()) == ["global"]
480
- assert claripy.backends.vsa.identical(r_model.regions["global"], s_expected)
481
-
482
- r, _, _ = s.memory.find(to_vs("global", 1), BVV(b"\0"), 8)
483
- r_model = claripy.backends.vsa.convert(r)
484
- s_expected = claripy.backends.vsa.convert(SI(bits=64, to_conv=3))
485
- assert isinstance(r_model, claripy.vsa.ValueSet)
486
- assert list(r_model.regions.keys()) == ["global"]
487
- assert claripy.backends.vsa.identical(r_model.regions["global"], s_expected)
488
-
489
- # Find in StridedIntervals
490
- r, _, _ = s.memory.find(to_vs("global", 4), BVV(b"\0"), 8)
491
- r_model = claripy.backends.vsa.convert(r)
492
- s_expected = claripy.backends.vsa.convert(SI(bits=64, stride=1, lower_bound=4, upper_bound=11))
493
- assert isinstance(r_model, claripy.vsa.ValueSet)
494
- assert list(r_model.regions.keys()) == ["global"]
495
- assert claripy.backends.vsa.identical(r_model.regions["global"], s_expected)
496
-
497
- def test_registers(self):
498
- s = SimState(arch="AMD64")
499
- expr = s.registers.load("rax")
500
- assert s.solver.symbolic(expr)
501
-
502
- s.registers.store("rax", 0x31)
503
- expr = s.registers.load("rax")
504
- assert not s.solver.symbolic(expr)
505
- assert s.solver.eval(expr) == 0x00000031
506
-
507
- def test_fullpage_write(self):
508
- s = SimState(arch="AMD64")
509
- a = s.solver.BVV(b"A" * 0x2000)
510
- s.memory.store(0, a)
511
- # assert len(s.memory.mem._pages) == 2
512
- # assert len(s.memory.mem._pages[0].keys()) == 0
513
- # assert len(s.memory.mem._pages[1].keys()) == 0
514
- assert s.memory.load(0, 0x2000) is a
515
- assert a.variables != s.memory.load(0x2000, 1).variables
516
-
517
- s = SimState(arch="AMD64")
518
- a = s.solver.BVV(b"A" * 2)
519
- s.memory.store(0x1000, a)
520
- s.memory.store(0x2000, a)
521
- assert a.variables == s.memory.load(0x2000, 1).variables
522
- assert a.variables == s.memory.load(0x2001, 1).variables
523
- assert a.variables != s.memory.load(0x2002, 1).variables
524
-
525
- s = SimState(arch="AMD64")
526
- x = s.solver.BVV(b"X")
527
- a = s.solver.BVV(b"A" * 0x1000)
528
- s.memory.store(1, x)
529
- s2 = s.copy()
530
- s2.memory.store(0, a)
531
- assert len(s.memory.changed_bytes(s2.memory)) == 0x1000
532
-
533
- s = SimState(arch="AMD64")
534
- s.memory._maximum_symbolic_size = 0x2000000
535
- a = s.solver.BVS("A", 0x1000000 * 8)
536
- s.memory.store(0, a)
537
- b = s.memory.load(0, 0x1000000)
538
- assert b is a
539
-
540
- def test_symbolic_write(self):
541
- s = SimState(arch="AMD64", add_options={o.SYMBOLIC_WRITE_ADDRESSES})
542
- x = s.solver.BVS("x", 64)
543
- y = s.solver.BVS("y", 64)
544
- a = s.solver.BVV(b"A" * 0x10)
545
- b = s.solver.BVV(b"B")
546
- c = s.solver.BVV(b"C")
547
- d = s.solver.BVV(b"D")
548
-
549
- s.memory.store(0x10, a)
550
- s.add_constraints(x >= 0x10, x < 0x20)
551
- s.memory.store(x, b)
552
-
553
- for i in range(0x10, 0x20):
554
- assert len(s.solver.eval_upto(s.memory.load(i, 1), 10)) == 2
555
-
556
- s.memory.store(x, c)
557
- for i in range(0x10, 0x20):
558
- assert len(s.solver.eval_upto(s.memory.load(i, 1), 10)) == 2
559
-
560
- s2 = s.copy()
561
- s2.add_constraints(y >= 0x10, y < 0x20)
562
- s2.memory.store(y, d)
563
- for i in range(0x10, 0x20):
564
- assert len(s2.solver.eval_upto(s2.memory.load(i, 1), 10)) == 3
565
-
566
- def test_concrete_memset(self):
567
- def _individual_test(state, base, val, size):
568
- # time it
569
- start = time.time()
570
- memset = SIM_PROCEDURES["libc"]["memset"]().execute(state, arguments=[base, state.solver.BVV(val, 8), size])
571
- elapsed = time.time() - start
572
-
573
- # should be done within 1 second
574
- assert elapsed <= 5
575
- # the result should be good
576
- byt_0 = memset.state.memory.load(base, 1)
577
- assert s.solver.eval_upto(byt_0, 10) == [val]
578
- byt_1 = memset.state.memory.load(base + 1, 1)
579
- assert s.solver.eval_upto(byt_1, 10) == [val]
580
- byt_2 = memset.state.memory.load(base + size - 1, 1)
581
- assert s.solver.eval_upto(byt_2, 10) == [val]
582
-
583
- BASE = 0x800000
584
- SIZE = 0x200000
585
-
586
- # Writes many zeros
587
- VAL = 0
588
- s = SimState(arch="AMD64")
589
- _individual_test(s, BASE, VAL, SIZE)
590
-
591
- # Writes many ones
592
- VAL = 1
593
- s = SimState(arch="AMD64")
594
- _individual_test(s, BASE, VAL, SIZE)
595
-
596
- def test_false_condition(self):
597
- s = SimState(arch="AMD64")
598
-
599
- asdf = s.solver.BVV(b"asdf")
600
- fdsa = s.solver.BVV(b"fdsa")
601
- s.memory.store(0x1000, asdf)
602
- s.memory.store(0x1000, fdsa, condition=s.solver.false)
603
- s.memory.store(0, fdsa, condition=s.solver.false)
604
-
605
- assert s.memory.load(0x1000, 4) is asdf
606
- assert 0 not in s.memory._pages
607
-
608
- def test_fast_memory(self):
609
- s = SimState(arch="AMD64", add_options={o.FAST_REGISTERS, o.FAST_MEMORY})
610
-
611
- s.regs.rax = 0x4142434445464748
612
- s.regs.rbx = 0x5555555544444444
613
- assert (s.regs.rax == 0x4142434445464748).is_true()
614
- assert (s.regs.rbx == 0x5555555544444444).is_true()
615
-
616
- self._concrete_memory_tests(s)
617
-
618
- def test_light_memory(self):
619
- s = SimState(arch="AMD64", plugins={"registers": SimLightRegisters()})
620
- assert type(s.registers) is SimLightRegisters
621
-
622
- assert s.regs.rax.symbolic
623
- s.regs.rax = 0x4142434445464748
624
- assert (s.regs.rax == 0x4142434445464748).is_true()
625
-
626
- assert s.regs.rbx.symbolic
627
- s.regs.rbx = 0x5555555544444444
628
- assert (s.regs.rbx == 0x5555555544444444).is_true()
629
-
630
- assert s.regs.rcx.symbolic
631
-
632
- s.regs.ah = 0
633
- assert (s.regs.rax == 0x4142434445460048).is_true()
634
-
635
- s.regs.cl = 0
636
- assert s.regs.rcx.symbolic
637
-
638
- def test_crosspage_store(self):
639
- for memcls in [
640
- UltraPageMemory,
641
- ListPageMemory,
642
- ]:
643
- state = SimState(arch="x86", mode="symbolic", plugins={"memory": memcls()})
644
-
645
- state.regs.sp = 0xBAAAFFFC
646
- state.memory.store(state.regs.sp, b"\x01\x02\x03\x04" + b"\x05\x06\x07\x08")
647
- assert state.solver.eval(state.memory.load(state.regs.sp, 8)) == 0x0102030405060708
648
-
649
- state.memory.store(state.regs.sp, b"\x01\x02\x03\x04" + b"\x05\x06\x07\x08", endness="Iend_LE")
650
- assert state.solver.eval(state.memory.load(state.regs.sp, 8)) == 0x0807060504030201
651
-
652
- symbol = claripy.BVS("symbol", 64)
653
- state.memory.store(state.regs.sp, symbol)
654
- assert state.memory.load(state.regs.sp, 8) is symbol
655
-
656
- state.memory.store(state.regs.sp, symbol, endness="Iend_LE")
657
- assert state.memory.load(state.regs.sp, 8) is symbol.reversed
658
-
659
- def test_mv_crosspage_store(self):
660
- for memcls in [
661
- MVPageMemory,
662
- ]:
663
- state = SimState(arch="x86", mode="symbolic", plugins={"memory": memcls()})
664
-
665
- mv = MultiValues(offset_to_values={0: {claripy.BVV(1337, 32)}, 4: {claripy.BVV(13371337, 8 * 5)}})
666
- state.memory.store(4096 - 3, mv)
667
-
668
- first_three_bytes = state.memory.load(4096 - 3, size=3)
669
- assert state.solver.eval_one(first_three_bytes.one_value()) == 1337 >> 8
670
-
671
- next_one_byte = state.memory.load(4096, size=1)
672
- assert state.solver.eval_one(next_one_byte.one_value()) == 1337 & 0xFF
673
-
674
- first_four_bytes = state.memory.load(4096 - 3, size=4)
675
- assert state.solver.eval_one(first_four_bytes.one_value()) == 1337
676
-
677
- all_bytes = state.memory.load(4096 - 3, size=9)
678
- assert state.solver.eval_one(all_bytes.one_value()) == (1337 << 40) | 13371337
679
-
680
- data = {
681
- 0: {claripy.BVS("TOP", 32), claripy.BVV(0x56495254, 32)},
682
- 4: {claripy.BVV(47, 8), claripy.BVV(85, 8)},
683
- 5: {claripy.BVS("TOP", 32), claripy.BVV(0x414C5F53, 32)},
684
- 9: {claripy.BVV(69, 8), claripy.BVV(47, 8)},
685
- 10: {
686
- claripy.BVV(0x52564552, 32),
687
- claripy.BVV(0x56495254, 32),
688
- claripy.BVS("TOP", 32),
689
- claripy.BVV(0x63757272, 32),
690
- },
691
- 14: {claripy.BVV(101, 8), claripy.BVV(47, 8), claripy.BVV(46, 8), claripy.BVV(85, 8)},
692
- 15: {
693
- claripy.BVV(0x656E74, 24),
694
- claripy.BVV(0x6E7400, 24),
695
- claripy.BVV(0x414C5F, 24),
696
- claripy.BVS("TOP", 24),
697
- },
698
- 18: {claripy.BVV(114, 8), claripy.BVS("TOP", 8), claripy.BVV(83, 8), claripy.BVV(47, 8)},
699
- 19: {
700
- claripy.BVV(0x45525645, 32),
701
- claripy.BVS("TOP", 32),
702
- claripy.BVV(0x2E747278, 32),
703
- claripy.BVV(0x795B2564, 32),
704
- },
705
- 23: {claripy.BVV(46, 8), claripy.BVV(82, 8), claripy.BVV(0, 8), claripy.BVV(93, 8)},
706
- 24: {claripy.BVV(0x74727800, 32), claripy.BVV(0x2E69705B, 32), claripy.BVV(0x2E656E74, 32)},
707
- 28: {
708
- claripy.BVV(0x72795B25645D2E69705B25645D2E636F75, 136),
709
- claripy.BVV(0x25645D2E636F756E74203D2025643B0A00, 136),
710
- },
711
- 45: {claripy.BVV(110, 8), claripy.BVV(47, 8)},
712
- 46: {claripy.BVV(0x74203D20, 32), claripy.BVS("TOP", 32)},
713
- 50: {claripy.BVV(37, 8), claripy.BVV(47, 8)},
714
- 51: {
715
- claripy.BVS("TOP", 32),
716
- claripy.BVV(0x56495254, 32),
717
- claripy.BVV(0x63757272, 32),
718
- claripy.BVV(0x643B0A00, 32),
719
- },
720
- 55: {claripy.BVV(101, 8), claripy.BVV(85, 8), claripy.BVV(47, 8)},
721
- 56: {claripy.BVV(0x6E7400, 24), claripy.BVV(0x414C5F, 24), claripy.BVS("TOP", 24)},
722
- 59: {claripy.BVS("TOP", 8)},
723
- 60: {claripy.BVV(0x45525645, 32), claripy.BVS("TOP", 32), claripy.BVV(0x2E747278, 32)},
724
- 64: {claripy.BVV(46, 8), claripy.BVV(82, 8), claripy.BVV(0, 8)},
725
- 65: {claripy.BVV(0x74727800, 32), claripy.BVV(0x2E656E74, 32)},
726
- 69: {claripy.BVV(0x72795B25645D2E69705B25645D2E636F756E74203D2025643B0A00, 216)},
727
- 96: {claripy.BVV(47, 8)},
728
- 97: {claripy.BVS("TOP", 32)},
729
- 101: {claripy.BVV(0x2E74727800, 40)},
730
- }
731
- mv = MultiValues(offset_to_values=data)
732
- state.memory.store(0x7FFEFF9C, mv) # should not crash
733
-
734
- def test_crosspage_read(self):
735
- state = SimState(arch="ARM")
736
- state.regs.sp = 0x7FFF0008
737
- state.stack_push(0x44556677)
738
- state.stack_push(0x1)
739
- state.stack_push(0x2)
740
- state.stack_push(0x3)
741
- state.stack_push(0x4)
742
- state.stack_push(0x99887766)
743
- state.stack_push(0x5)
744
- state.stack_push(0x105C8)
745
- state.stack_push(0x11223344)
746
-
747
- r1 = state.memory.load(state.regs.sp, 36)
748
- assert bytes.fromhex("77665544") in state.solver.eval(r1, cast_to=bytes)
749
-
750
- state.stack_push(0x10564)
751
-
752
- r2 = state.memory.load(state.regs.sp, 40)
753
- assert bytes.fromhex("77665544") in state.solver.eval(r2, cast_to=bytes)
754
- # assert s.solver.eval(r, 2) == ( 0xffeeddccbbaa998877665544, )
755
-
756
- def test_address_wrap(self):
757
- for memcls in [UltraPageMemory, ListPageMemory]:
758
- state = SimState(arch="x86", mode="symbolic", plugins={"memory": memcls()})
759
- symbol = claripy.BVS("symbol", 64)
760
-
761
- state.memory.store(0xFFFFFFFF, symbol.get_byte(0))
762
- state.memory.store(0, b"\0" * 8)
763
- assert len(state.memory.load(0xFFFFFFFF, 8)) == 64
764
-
765
- state.memory.store(0xFFFFFFFF, b"ABCD")
766
- assert state.solver.eval(state.memory.load(0, 3)) == 0x424344
767
-
768
- state.memory.store(0xFFFFFFFF, symbol)
769
- assert state.memory.load(0, 1) is symbol[64 - 8 - 1 : 64 - 16]
770
-
771
- def test_underconstrained(self):
772
- state = SimState(arch="AMD64", add_options={o.UNDER_CONSTRAINED_SYMEXEC})
773
-
774
- # test that under-constrained load is constrained
775
- ptr1 = state.memory.load(0x4141414141414000, size=8, endness="Iend_LE")
776
- assert ptr1.uc_alloc_depth == 0
777
- assert ptr1.uninitialized
778
- state.memory.load(ptr1, size=1)
779
- # ptr1 should have been constrained
780
- assert state.solver.min_int(ptr1) == state.solver.max_int(ptr1)
781
-
782
- # test that under-constrained store is constrained
783
- ptr2 = state.memory.load(0x4141414141414008, size=8, endness="Iend_LE")
784
- assert ptr2.uc_alloc_depth == 0
785
- assert ptr2.uninitialized
786
- state.memory.store(ptr2, b"\x41", size=1)
787
- # ptr2 should have been constrained
788
- assert state.solver.min_int(ptr2) == state.solver.max_int(ptr2)
789
-
790
- # ptr1 and ptr2 should not point to the same region
791
- assert state.solver.eval(ptr1) != state.solver.eval(ptr2)
792
-
793
- # uninitialized load and stores w/o uc_alloc_depth should not crash
794
- ptr3 = claripy.Concat(
795
- state.memory.load(0x4141414141414010, size=4, endness="Iend_LE"),
796
- state.memory.load(0x4141414141414014, size=4, endness="Iend_LE"),
797
- )
798
- assert ptr3.uninitialized
799
- assert ptr3.uc_alloc_depth is None # because uc_alloc_depth doesn't carry across Concat
800
- # we don't care what these do, as long as they don't crash
801
- state.memory.store(ptr3, b"\x41", size=1)
802
- state.memory.load(ptr3, size=1)
803
-
804
- def test_concrete_load_non_adjacent_pages(self):
805
- s = SimState(arch="AMD64", mode="symbolic", plugins={"memory": UltraPageMemory()})
806
-
807
- s.memory.store(0x100000, b"\x01" * 4096)
808
- s.memory.store(0x100000 + 4096, b"\x02" * 4096)
809
- mv = s.memory.concrete_load(0x100000 + 0xFFA, 400)
810
- assert len(mv) == 400, "Loading data across non-physically adjacent pages failed for ultra pages."
811
- assert mv == (b"\x01" * 6) + (b"\x02" * 394)
812
-
813
- def test_hex_dump(self):
814
- s = SimState(arch="AMD64")
815
- addr = s.heap.allocate(0x20)
816
- s.memory.store(addr, claripy.Concat(claripy.BVV("ABCDEFGH"), claripy.BVS("symbolic_part", 24 * s.arch.bits)))
817
- dump = s.memory.hex_dump(addr, 0x20)
818
- assert (
819
- dump == "c0000000: 41424344 45464748 ???????? ???????? ABCDEFGH????????\n"
820
- "c0000010: ???????? ???????? ???????? ???????? ????????????????\n"
821
- )
822
-
823
- dump = s.memory.hex_dump(
824
- addr,
825
- 0x20,
826
- extra_constraints=(s.memory.load(addr + 0x10, 4) == 0xDEADBEEF,),
827
- solve=True,
828
- endianness="Iend_LE",
829
- )
830
- assert (
831
- dump == "c0000000: 44434241 48474645 ???????? ???????? ABCDEFGH????????\n"
832
- "c0000010: efbeadde ???????? ???????? ???????? ....????????????\n"
833
- )
834
-
835
- def test_concrete_load(self):
836
- for memcls in [UltraPageMemory, ListPageMemory]:
837
- state = SimState(arch="AMD64", mode="symbolic", plugins={"memory": memcls()})
838
- state.memory.store(0x20000, b"aaaabbbbccccdddd")
839
-
840
- data, bitmap = state.memory.concrete_load(0x20000, 4, with_bitmap=True)
841
- data_bytes = bytes(d if b == 0 else 0 for d, b in zip(data, bitmap))
842
- assert data_bytes == b"aaaa"
843
- assert bitmap.tobytes() == b"\x00\x00\x00\x00"
844
-
845
- data, bitmap = state.memory.concrete_load(0x20004, 8, with_bitmap=True)
846
- data_bytes = bytes(d if b == 0 else 0 for d, b in zip(data, bitmap))
847
- assert data_bytes == b"bbbbcccc"
848
- assert bitmap.tobytes() == b"\x00\x00\x00\x00\x00\x00\x00\x00"
849
-
850
- state.memory.store(0x20001, claripy.BVS("flag", 8))
851
- data, bitmap = state.memory.concrete_load(0x20000, 4, with_bitmap=True)
852
- data_bytes = bytes(d if b == 0 else 0 for d, b in zip(data, bitmap))
853
- assert data_bytes == b"a\x00aa"
854
- assert bitmap.tobytes() == b"\x00\x01\x00\x00"
855
-
856
- expr = claripy.Concat(
857
- claripy.BVS("flag_0", 1),
858
- claripy.BVS("flag_1", 2),
859
- claripy.BVS("flag_2", 3),
860
- claripy.BVS("flag_3", 6),
861
- claripy.BVS("flag_4", 4),
862
- )
863
- state.memory.store(0x20001, expr)
864
- data, bitmap = state.memory.concrete_load(0x20000, 4, with_bitmap=True)
865
- data_bytes = bytes(d if b == 0 else 0 for d, b in zip(data, bitmap))
866
- assert data_bytes == b"a\x00\x00a"
867
- assert bitmap.tobytes() == b"\x00\x01\x01\x00"
868
-
869
- expr = claripy.Concat(
870
- claripy.BVS("flag_0", 1),
871
- claripy.BVV(1, 2),
872
- claripy.BVV(3, 3),
873
- claripy.BVV(6, 6),
874
- claripy.BVS("flag_4", 4),
875
- )
876
- state.memory.store(0x20005, expr)
877
- data, bitmap = state.memory.concrete_load(0x20004, 4, with_bitmap=True)
878
- data_bytes = bytes(d if b == 0 else 0 for d, b in zip(data, bitmap))
879
- assert data_bytes == b"b\x00\x00b"
880
- assert bitmap.tobytes() == b"\x00\x01\x01\x00"
881
-
882
- expr = claripy.Concat(
883
- claripy.BVV(7, 7),
884
- claripy.BVS("flag_0", 2),
885
- claripy.BVV(7, 7),
886
- )
887
- state.memory.store(0x20005, expr)
888
- data, bitmap = state.memory.concrete_load(0x20004, 4, with_bitmap=True)
889
- data_bytes = bytes(d if b == 0 else 0 for d, b in zip(data, bitmap))
890
- assert data_bytes == b"b\x00\x00b"
891
- assert bitmap.tobytes() == b"\x00\x01\x01\x00"
892
-
893
- expr = claripy.Concat(
894
- claripy.BVV(1, 1),
895
- claripy.BVS("flag_0", 14),
896
- claripy.BVV(1, 1),
897
- )
898
- state.memory.store(0x20005, expr)
899
- data, bitmap = state.memory.concrete_load(0x20004, 4, with_bitmap=True)
900
- data_bytes = bytes(d if b == 0 else 0 for d, b in zip(data, bitmap))
901
- assert data_bytes == b"b\x00\x00b"
902
- assert bitmap.tobytes() == b"\x00\x01\x01\x00"
903
-
904
- def test_multivalued_list_page(self):
905
- state = SimState(arch="AMD64", mode="symbolic", plugins={"memory": MultiValuedMemory()})
906
-
907
- # strong update
908
- state.memory.store(0x100, claripy.BVV(0x40, 64))
909
- state.memory.store(0x100, claripy.BVV(0x80818283, 64))
910
- a = state.memory.load(0x100, size=8).one_value()
911
- assert a is not None
912
- assert a is claripy.BVV(0x80818283, 64)
913
-
914
- # strong update with partial overwrites
915
- state.memory.store(0x100, claripy.BVV(0x0, 64))
916
- state.memory.store(0x104, claripy.BVV(0x1337, 32))
917
- a = state.memory.load(0x100, size=8).one_value()
918
- assert a is not None
919
- assert a is claripy.BVV(0x1337, 64)
920
-
921
- # weak updates
922
- state.memory.store(0x120, claripy.BVV(0x40, 64))
923
- state.memory.store(0x120, claripy.BVV(0x85868788, 64), weak=True)
924
- a = state.memory.load(0x120, size=8)
925
- assert len(a.keys()) == 1
926
- assert 0 in a
927
- assert len(list(a.values())[0]) == 2
928
- assert {state.solver.eval_exact(item, 1)[0] for item in list(a.values())[0]} == {0x40, 0x85868788}
929
-
930
- # weak updates with symbolic values
931
- A = claripy.BVS("a", 64)
932
- state.memory.store(0x140, A, endness=state.arch.memory_endness)
933
- state.memory.store(0x141, claripy.BVS("b", 64), endness=state.arch.memory_endness, weak=True)
934
- a = state.memory.load(0x140, size=8)
935
- assert len(a.keys()) == 2
936
- assert 0 in a
937
- assert 1 in a
938
- assert len(list(a.values())[0]) == 1
939
- assert next(iter(a[0])) is A[7:0]
940
- assert len(a[1]) == 2
941
-
942
- def test_conditional_concretization(self):
943
- class ZeroFillerMemory(UltraPageMemory):
944
- def _default_value(self, *args, size=None, **kwargs): # pylint:disable=unused-argument
945
- return self.state.solver.BVV(0, size)
946
-
947
- state = SimState(arch="AMD64", mode="symbolic", plugins={"memory": ZeroFillerMemory()})
948
- state.options.add(o.ZERO_FILL_UNCONSTRAINED_MEMORY)
949
-
950
- cond = state.solver.BoolS("cond")
951
- addr = state.solver.BVS("addr", 32)
952
-
953
- state.memory.store(addr, 0x12345678, size=4, condition=cond, endness=state.arch.memory_endness)
954
- val = state.memory.load(addr, size=4, condition=cond, endness=state.arch.memory_endness)
955
- assert set(state.solver.eval_upto(cond, 2)) == {True, False}
956
- assert (val == 0x12345678).is_true()
957
-
958
-
959
- if __name__ == "__main__":
960
- unittest.main()