示例#1
0
    def test_resume(self):
        m = ManticoreWASM(collatz_file)
        plugin = CallCounterPlugin()
        m.register_plugin(plugin)
        m.collatz(lambda s: [I32(1337)])
        m.run()

        counts_canonical = plugin.context.get("counter")

        m = ManticoreWASM(collatz_file)
        plugin = SerializerPlugin()
        m.register_plugin(plugin)
        m.collatz(lambda s: [I32(1337)])
        m.run()

        counts_save = plugin.context.get("counter")

        m = ManticoreWASM.from_saved_state("/tmp/collatz_checkpoint.pkl")
        plugin = CallCounterPlugin()
        m.register_plugin(plugin)
        m.run()

        counts_resume = plugin.context.get("counter")

        for k in counts_canonical:
            with self.subTest(k):
                self.assertEqual(
                    counts_save.get(k, 0) + counts_resume.get(k, 0),
                    counts_canonical[k],
                    f"Mismatched {k} count",
                )

        results = []
        for idx, val_list in enumerate(m.collect_returns()):
            results.append(val_list[0][0])

        self.assertEqual(sorted(results), [44])
示例#2
0
    def test_implicit_call(self):
        m = ManticoreWASM(collatz_file)
        counter_plugin = CallCounterPlugin()
        m.register_plugin(counter_plugin)
        m.collatz(lambda s: [I32(1337)])

        counts = counter_plugin.context.get("counter")

        self.assertEqual(counts["br_if"], 45)
        self.assertEqual(counts["loop"], 44)
        self.assertEqual(counts["i32.add"], 88)

        results = []
        for idx, val_list in enumerate(m.collect_returns()):
            results.append(val_list[0][0])

        self.assertEqual(sorted(results), [44])

        m.collatz(lambda s: [I32(1338)])
        results = []
        for idx, val_list in enumerate(m.collect_returns()):
            results.append(val_list[0][0])

        self.assertEqual(sorted(results), [70])
示例#3
0
def arg_gen(state):
    # Generate a symbolic argument to pass to the collatz function.
    # Possible values: 4, 6, 8
    arg = state.new_symbolic_value(32, "collatz_arg")
    state.constrain(arg > 3)
    state.constrain(arg < 9)
    state.constrain(arg % 2 == 0)
    return [arg]


# Tell Manticore to run the collatz function with the given argument generator.
# We use an argument generator function instead of a list of arguments because Manticore
# might have multiple states waiting to begin execution, and we can conveniently map a
# generator function over all the ready states and get access to their respective
# constraint sets.
m.collatz(arg_gen)

# Manually collect return values
for idx, val_list in enumerate(m.collect_returns()):
    print("State", idx, "::", val_list[0])

# Finalize the run (dump testcases)
m.finalize()


print(
    """

============ Example 2 ============
"""
)