2023-09-07 18:15:18 -07:00
|
|
|
#!/usr/bin/env python3
|
2024-08-28 18:31:43 -07:00
|
|
|
from __future__ import annotations
|
2026-06-02 14:48:07 -07:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
import unittest
|
2017-05-28 13:32:38 -07:00
|
|
|
|
2017-07-04 01:56:21 -07:00
|
|
|
from angr.state_plugins.callstack import CallStack
|
2017-05-28 13:32:38 -07:00
|
|
|
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
# pylint: disable=missing-class-docstring
|
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
|
class TestCallstack(unittest.TestCase):
|
|
|
|
|
def test_empty_stack(self):
|
|
|
|
|
cs = CallStack()
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
# Initial setting: just assume the control flow starts from 0x300000
|
|
|
|
|
cs = cs.call(None, 0x300000, None, 0xFFFFFFF0)
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
# Calling 0x401000 from 0x400000. When it returns, it should return to 0x400004.
|
|
|
|
|
# The stack pointer after entering the new function should be 0xffffff00
|
|
|
|
|
# Note: this means on platforms like x86 and AMD64 where CALL instruction actually pushes the ret address on to
|
|
|
|
|
# the stack and modifies the stack pointer, the user should adjust stack pointer accordingly (minus 4 or 8, for
|
|
|
|
|
# example) before passing to CallStack.call(). CallStack has no way to know what the architecture it is used on.
|
|
|
|
|
cs = cs.call(0x400000, 0x401000, 0x400004, 0xFFFFFF00)
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
cs = cs.call(0x401008, 0x402000, 0x40100C, 0xFFFFFE80)
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
assert cs.current_function_address == 0x402000
|
|
|
|
|
assert cs.current_stack_pointer == 0xFFFFFE80
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
# Return to 0x40100c
|
|
|
|
|
cs = cs.ret(0x40100C)
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
assert cs.current_function_address == 0x401000
|
|
|
|
|
assert cs.current_stack_pointer == 0xFFFFFF00
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
cs = cs.ret(0x400004)
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
assert cs.current_function_address == 0x300000
|
|
|
|
|
assert cs.current_stack_pointer == 0xFFFFFFF0
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
# We return one more time to see what happens
|
|
|
|
|
# Ideally nothing should be popped out
|
|
|
|
|
cs = cs.ret(0x200000)
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
assert cs.current_function_address == 0x300000
|
|
|
|
|
assert cs.current_stack_pointer == 0xFFFFFFF0
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2022-01-20 20:09:43 -07:00
|
|
|
# Final return!
|
|
|
|
|
cs = cs.ret(None)
|
|
|
|
|
|
|
|
|
|
assert cs.current_function_address == 0
|
|
|
|
|
assert cs.current_stack_pointer == 0
|
2015-02-13 17:28:49 -08:00
|
|
|
|
2023-01-12 16:07:58 -07:00
|
|
|
|
2015-02-13 17:28:49 -08:00
|
|
|
if __name__ == "__main__":
|
2022-01-20 20:09:43 -07:00
|
|
|
unittest.main()
|