Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chess changes breakout for upstream #2469

Draft
wants to merge 100 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
7d5b087
Use my capstone dev branch until fixes are merged into next
ekilmer May 8, 2020
f177ce8
Fix aarch64
ekilmer May 8, 2020
5ee1f15
Fix ARM
ekilmer May 8, 2020
10e49bd
Update Capstone commit to fix arm64 LD1 instruction immediates
ekilmer May 10, 2020
4f69dbc
Ignore coverage tracking for defensive assertions and exceptions
ekilmer May 26, 2020
f35744f
Merge branch 'master' into capstone-5-dev
feliam Jul 9, 2020
4b738bf
ENDBR64 as nop
feliam Jul 9, 2020
be9bdd1
Add lacking x86 tests
feliam Jul 14, 2020
6c24f8d
New x86 instrution test
feliam Jul 14, 2020
be00705
Merge branch 'master' into capstone-5-dev
ekilmer Aug 5, 2020
85b3bbb
Merge branch 'master' into capstone-5-dev
ekilmer Aug 11, 2020
4751e11
Disable write back once we hit our stop in Unicorn emulation
ekilmer Aug 3, 2020
3f21e92
Make emulator reinitialize after write backs are disabled
Sep 14, 2020
416b8b8
Merge branch 'master' into fix-emulate-step
Sep 14, 2020
cfafb6d
Fix linting on test_general
Sep 14, 2020
548fe47
Add a Rust/Unicorn resumption test
Sep 22, 2020
255eb70
Make rusticorn binary actually check behavior
Sep 22, 2020
d689bdd
Merge branch 'fix-emulate-step' into chess
ekilmer Sep 25, 2020
3e646f7
Merge branch 'master' into capstone-5-dev
ekilmer Sep 28, 2020
454a65f
Merge branch 'capstone-5-dev' into chess
ekilmer Sep 28, 2020
a21ea0f
Run CI on chess branch
ekilmer Sep 28, 2020
fd4a63d
Support for pread64 syscall
ekilmer Oct 6, 2020
8d6134f
Delete duplicated test method
Oct 14, 2020
fcbc2de
Merge branch 'master' into fix-emulate-step
Oct 14, 2020
565b35c
Fix addresses and improve error handling
Oct 14, 2020
9405a92
Merge branch 'master' into capstone-5-dev
ekilmer Oct 24, 2020
20c1526
Merge branch 'master' into chess
ekilmer Nov 9, 2020
df4baa7
Fix issue with sphinx autodoc
ekilmer Nov 30, 2020
065516f
Add last_executed_pc property to abstract CPU
ekilmer Jan 20, 2021
cceeb1f
Optionally skip publishing mem read/writes in CPU
ekilmer Jan 21, 2021
6079bce
Shallow copy AMD64RegFile but keep concrete register values
ekilmer Jan 21, 2021
0db7011
Merge branch 'master' into chess
ekilmer Mar 29, 2021
d54307c
Remove call to pkg_resources that breaks custom installation
ekilmer Apr 7, 2021
e0d5f3a
Fix mypy
ekilmer Apr 7, 2021
8577543
Merge branch 'master' into chess
ekilmer Apr 7, 2021
a659442
Merge branch 'fix-emulate-step' into chess
ekilmer Apr 8, 2021
c3d7885
Merge branch 'master' into fix-emulate-step
ekilmer Apr 10, 2021
702260c
Fix Unicorn resume
ekilmer Apr 10, 2021
ac9be30
Merge branch 'fix-emulate-step' into chess
ekilmer Apr 14, 2021
dc64f11
Fix test missed during merge
ekilmer Apr 14, 2021
101596e
Fix more tests missed during merge
ekilmer Apr 15, 2021
ba5597c
staticmethods to get syscall info
ekilmer Apr 30, 2021
5fa3f94
Rework some logging
ekilmer May 27, 2021
bb5d00d
Better logging initialization
ekilmer May 27, 2021
5529d86
Remember to set manticore property on state __enter__
ekilmer Jun 4, 2021
f879bfd
Better log message in sys_recvfrom
ekilmer Jun 4, 2021
f9bcc15
Chess heap tracking work (#2458)
sschriner Jun 4, 2021
b3ecaf6
corrected read_arg in hook_malloc_library
sschriner Jun 4, 2021
27e0754
Add location to alloc info and remove some debugging statements (#2463)
sschriner Jun 23, 2021
0fa906d
Kill Manticore if _any_ state encounters unrecoverable exception
ekilmer Jul 9, 2021
98a87e3
Merge branch 'master' into chess
ekilmer Jul 15, 2021
f3b9234
Merge branch 'master' into capstone-5-dev
ekilmer Jul 27, 2021
3811e3b
Merge branch 'master' into chess
ekilmer Sep 1, 2021
dd8af82
Merge branch 'master' into chess
ekilmer Sep 9, 2021
9c74e8d
Formatting
ekilmer Sep 9, 2021
ccebfbc
Mypy fixes
ekilmer Sep 10, 2021
4156aef
Merge branch 'master' into chess
ekilmer Sep 15, 2021
699deba
Remove unneeded sycall stubs helper function
ekilmer Sep 15, 2021
b6e3d05
Remove unneeded workaround for fast_crash option in memory
ekilmer Sep 15, 2021
7fa386d
Add SMT simplifications for bitvec subtraction
Boyan-MILANOV Nov 17, 2021
abbfa01
Replace operator SUB by built-in '-'
Boyan-MILANOV Nov 17, 2021
de3ced3
Merge branch 'dev-bitvecsub-simplifications' into chess
Boyan-MILANOV Nov 17, 2021
9c0a4ee
Add EXPLICIT fork policy (#2514)
Boyan-MILANOV Jan 4, 2022
26ccdd2
Fix `BitVecExtract` simplification for constant folding (#2524)
Boyan-MILANOV Jan 20, 2022
504ec9c
x86 FXSAVE & FXRSTOR support (#2511)
Boyan-MILANOV Feb 4, 2022
d20e777
Also ignore missing unicorn registers in the fallback emulator (#2531)
Boyan-MILANOV Feb 11, 2022
932ed0c
Revert "x86 FXSAVE & FXRSTOR support (#2511)"
Boyan-MILANOV Feb 15, 2022
a16a011
add ENDBR32 as nop (#2532)
lordidiot Feb 15, 2022
7589f48
Merge branch 'master' into capstone-5-dev
ekilmer Mar 3, 2022
b832407
Remove duplicate x86 ENDBR64 instruction
ekilmer Mar 3, 2022
3a7e4ab
Fix test
ekilmer Mar 3, 2022
11bb256
Fix more errors in tests
ekilmer Mar 3, 2022
ab798c7
Use latest tagged capstone==5.0.0rc2
ekilmer Mar 3, 2022
b762f7d
Merge branch 'master' into chess
ekilmer Apr 21, 2022
19ee948
Revert "x86 FXSAVE & FXRSTOR support (#2511)"
Boyan-MILANOV Feb 15, 2022
26e8c96
Merge branch 'capstone-5-dev' into chess
ekilmer Apr 21, 2022
9828f2c
Formatting
ekilmer Apr 22, 2022
634b6a4
Remove duplicate instruction from bad merge
ekilmer Apr 22, 2022
001683f
Fix bug in register_log_callback (#2542)
kokrui Apr 26, 2022
60b728d
add newfstatat syscall
lordidiot May 7, 2022
d13c4ff
add newfstatat syscall tests
lordidiot May 7, 2022
0f24f64
Merge branch 'add-sys-newfstatat' into test-newfstatat
ekilmer May 9, 2022
313dffa
Initial implementation of sys_rseq
ekilmer May 12, 2022
884a0b1
Fixes to CPUID to support latest glibc
ekilmer May 13, 2022
6c6b8d3
Hook CPUID instruction in emulator
ekilmer May 15, 2022
b824ff1
Use a patched version of Unicorn for CPUID hook in Python bindings
ekilmer May 16, 2022
b084d3f
Update unicorn to cpuid commit
ekilmer May 17, 2022
65a7a5b
Run CI on this branch
ekilmer May 17, 2022
33aa6c5
Use upstream merged result of Unicorn for CPUID hook
ekilmer May 17, 2022
9c3f447
Merge branch 'master' into chess
ekilmer May 23, 2022
73fa4e3
Merge branch 'chess' into fix-latest-glibc
ekilmer May 23, 2022
9ee8b4f
Revert CI run on branch
ekilmer May 23, 2022
cd8235b
Fix Docker image for building unicorn
ekilmer May 23, 2022
937ca98
Merge branch 'chess' into fix-latest-glibc
ekilmer May 23, 2022
d8afc95
Merge branch 'master' into chess
ekilmer May 25, 2022
e761932
Merge branch 'master' into chess
ekilmer Jun 1, 2022
33880e6
Use official Unicorn v2.0.0 release
ekilmer Jul 19, 2022
148f4fd
Merge branch 'master' into chess
ekilmer Jul 21, 2022
f514236
Merge branch 'master' into chess
ekilmer Jul 25, 2022
5d712a0
Add boolean simplifications (#2563)
Boyan-MILANOV Jul 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ LABEL dockerfile_maintenance=trailofbits

ENV LANG C.UTF-8

RUN apt-get -y update && DEBIAN_FRONTEND=noninteractive apt-get -y install python3.7 python3.7-dev python3-pip git wget
RUN export DEBIAN_FRONTEND="noninteractive" && \
apt-get update && \
apt-get install -y \
gpg wget && \
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null && \
echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ bionic main' | tee /etc/apt/sources.list.d/kitware.list >/dev/null && \
apt-get update && \
apt-get -y install python3.7 python3.7-dev python3-pip git wget cmake build-essential pkg-config

# Install solc 0.4.25 and validate it
RUN wget https://github.com/ethereum/solidity/releases/download/v0.4.25/solc-static-linux \
Expand Down
17 changes: 10 additions & 7 deletions manticore/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def main() -> None:
"""
Dispatches execution into one of Manticore's engines: evm or native.
"""
# Only print with Manticore's logger
logging.getLogger().handlers = []
log.init_logging()
Comment on lines +32 to +34
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything messing around with logging is in an unstable state and needs to be discussed/changed to not break all of the tests... The logging changes should include some documentation on how to use Manticore effectively as a component of a larger project and setting the log levels, redirection, etc.

args = parse_arguments()

if args.no_colors:
Expand Down Expand Up @@ -101,13 +104,13 @@ def positive(value):
help=("A folder name for temporaries and results." "(default mcore_?????)"),
)

current_version = pkg_resources.get_distribution("manticore").version
parser.add_argument(
"--version",
action="version",
version=f"Manticore {current_version}",
help="Show program version information",
)
# current_version = pkg_resources.get_distribution("manticore").version
# parser.add_argument(
# "--version",
# action="version",
# version=f"Manticore {current_version}",
# help="Show program version information",
# )
Comment on lines +107 to +113
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is commented because it breaks on "installs" that just naively copy the Manticore source tree into site-packages (due to the pkg_resources.get_distribution("manticore").version call)

parser.add_argument(
"--config",
type=str,
Expand Down
33 changes: 33 additions & 0 deletions manticore/core/smtlib/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,31 @@ def visit_BoolAnd(self, expression, *operands):
) == BitVecExtract(operand=value1, offset=beg, size=end - beg + 1)

def visit_BoolNot(self, expression, *operands):
"""
!!a -> a
!(a&&b) -> !a || !b
!(a||b) -> !a && !b
"""
if isinstance(operands[0], BoolNot):
return operands[0].operands[0]

if isinstance(operands[0], BoolAnd):
return BoolOr(
a=BoolNot(value=operands[0].operands[0]), b=BoolNot(value=operands[0].operands[1])
)

if isinstance(operands[0], BoolOr):
return BoolAnd(
a=BoolNot(value=operands[0].operands[0]), b=BoolNot(value=operands[0].operands[1])
)

def visit_BoolEqual(self, expression, *operands):
"""(EQ, ITE(cond, constant1, constant2), constant1) -> cond
(EQ, ITE(cond, constant1, constant2), constant2) -> NOT cond
(EQ (extract a, b, c) (extract a, b, c))

EQ (a) True -> a
EQ (a) False -> !a
"""
if isinstance(operands[0], BitVecITE) and isinstance(operands[1], Constant):
if isinstance(operands[0].operands[1], Constant) and isinstance(
Expand Down Expand Up @@ -537,6 +555,9 @@ def visit_BoolEqual(self, expression, *operands):

return BoolConstant(value=True, taint=expression.taint)

if isinstance(operands[1], BoolConstant):
return operands[0] if operands[1].value else BoolNot(value=operands[0])

def visit_BoolOr(self, expression, a, b):
if isinstance(a, Constant):
if a.value == False:
Expand Down Expand Up @@ -715,6 +736,18 @@ def visit_BitVecAdd(self, expression, *operands):
if left.value == 0:
return right

def visit_BitVecMul(self, expression, *operands):
"""
a * 1 ==> a
1 * a ==> a
"""
left = operands[0]
right = operands[1]
if isinstance(right, BitVecConstant) and right.value == 1:
return left
if isinstance(left, BitVecConstant) and left.value == 1:
return right

def visit_BitVecSub(self, expression, *operands):
"""a - 0 ==> a
(a + b) - b ==> a
Expand Down
196 changes: 3 additions & 193 deletions manticore/native/cpu/x86.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,6 @@ class AMD64RegFile(RegisterFile):
"TOP": Regspec("FPSW", int, 11, 3, False),
"FPTAG": Regspec("FPTAG", int, 0, 16, False),
"FPCW": Regspec("FPCW", int, 0, 16, False),
"FOP": Regspec("FOP", int, 0, 11, False),
"FIP": Regspec("FIP", int, 0, 64, False),
"FCS": Regspec("FCS", int, 0, 16, False),
"FDP": Regspec("FDP", int, 0, 64, False),
"FDS": Regspec("FDS", int, 0, 16, False),
"MXCSR": Regspec("MXCSR", int, 0, 32, False),
"MXCSR_MASK": Regspec("MXCSR_MASK", int, 0, 32, False),
"CF": Regspec("CF", bool, 0, 1, False),
"PF": Regspec("PF", bool, 0, 1, False),
"AF": Regspec("AF", bool, 0, 1, False),
Expand Down Expand Up @@ -363,13 +356,6 @@ class AMD64RegFile(RegisterFile):
"TOP": ("FPSW",),
"FPCW": (),
"FPTAG": (),
"FOP": (),
"FIP": (),
"FCS": (),
"FDP": (),
"FDS": (),
"MXCSR": (),
"MXCSR_MASK": (),
"FP0": (),
"FP1": (),
"FP2": (),
Expand Down Expand Up @@ -510,13 +496,6 @@ class AMD64RegFile(RegisterFile):
"FPSW",
"FPCW",
"FPTAG",
"FOP",
"FIP",
"FCS",
"FDP",
"FDS",
"MXCSR",
"MXCSR_MASK",
)

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -576,18 +555,7 @@ def __init__(self, *args, **kwargs):
for reg in ("FP0", "FP1", "FP2", "FP3", "FP4", "FP5", "FP6", "FP7"):
self._registers[reg] = (0, 0)

for reg in (
"FPSW",
"FPTAG",
"FPCW",
"FOP",
"FIP",
"FCS",
"FDP",
"FDS",
"MXCSR",
"MXCSR_MASK",
):
for reg in ("FPSW", "FPTAG", "FPCW"):
self._registers[reg] = 0

self._cache = {}
Expand Down Expand Up @@ -659,14 +627,7 @@ def _get_flag(self, register_id, register_size, offset, size):
def _set_float(self, register_id, register_size, offset, size, reset, value):
assert size == 80
assert offset == 0
# Translate int bitfield into a floating point value according
# to IEEE 754 standard, 80-bit double extended precision
if isinstance(value, int):
value &= 0xFFFFFFFFFFFFFFFFFFFF # 80-bit mask
exponent = value >> 64 # Exponent is the 16 higher bits
mantissa = value & 0xFFFFFFFFFFFFFFFF # Mantissa is the lower 64 bits
value = (mantissa, exponent)
elif not isinstance(value, tuple):
if not isinstance(value, tuple): # Add decimal here?
raise TypeError
self._registers[register_id] = value
return value
Expand Down Expand Up @@ -939,23 +900,6 @@ def canonicalize_instruction_name(self, instruction):
name = OP_NAME_MAP.get(name, name)
return name

def read_register_as_bitfield(self, name):
"""Read a register and return its value as a bitfield.
- if the register holds a bitvector, the bitvector object is returned.
- if the register holds a concrete value (int/float) it is returned as
a bitfield matching its representation in memory

This is mainly used to be able to write floating point registers to
memory.
"""
value = self.read_register(name)
if isinstance(value, tuple):
# Convert floating point to bitfield according to IEEE 754
# (16-bits exponent).(64-bits mantissa)
mantissa, exponent = value
value = mantissa + (exponent << 64)
return value

#
# Instruction Implementations
#
Expand Down Expand Up @@ -5748,37 +5692,6 @@ def sem_SYSCALL(cpu):
cpu.R11 = cpu.RFLAGS
raise Syscall()

def generic_FXSAVE(cpu, dest, reg_layout):
"""
Saves the current state of the x87 FPU, MMX technology, XMM, and
MXCSR registers to a 512-byte memory location specified in the
destination operand.

The content layout of the 512 byte region depends
on whether the processor is operating in non-64-bit operating modes
or 64-bit sub-mode of IA-32e mode
"""
addr = dest.address()
for offset, reg, size in reg_layout:
cpu.write_int(addr + offset, cpu.read_register_as_bitfield(reg), size)

def generic_FXRSTOR(cpu, dest, reg_layout):
"""
Reloads the x87 FPU, MMX technology, XMM, and MXCSR registers from
the 512-byte memory image specified in the source operand. This data should
have been written to memory previously using the FXSAVE instruction, and in
the same format as required by the operating modes. The first byte of the data
should be located on a 16-byte boundary.

There are three distinct layouts of the FXSAVE state map:
one for legacy and compatibility mode, a second
format for 64-bit mode FXSAVE/FXRSTOR with REX.W=0, and the third format is for
64-bit mode with FXSAVE64/FXRSTOR64
"""
addr = dest.address()
for offset, reg, size in reg_layout:
cpu.write_register(reg, cpu.read_int(addr + offset, size))

@instruction
def SYSCALL(cpu):
"""
Expand Down Expand Up @@ -6646,44 +6559,6 @@ class AMD64Cpu(X86Cpu):
arch = cs.CS_ARCH_X86
mode = cs.CS_MODE_64

# CPU specific instruction behaviour
FXSAVE_layout = [
(0, "FPCW", 16),
(2, "FPSW", 16),
(4, "FPTAG", 8),
(6, "FOP", 16),
(8, "FIP", 32),
(12, "FCS", 16),
(16, "FDP", 32),
(20, "FDS", 16),
(24, "MXCSR", 32),
(28, "MXCSR_MASK", 32),
(32, "FP0", 80),
(48, "FP1", 80),
(64, "FP2", 80),
(80, "FP3", 80),
(96, "FP4", 80),
(112, "FP5", 80),
(128, "FP6", 80),
(144, "FP7", 80),
(160, "XMM0", 128),
(176, "XMM1", 128),
(192, "XMM2", 128),
(208, "XMM3", 128),
(224, "XMM4", 128),
(240, "XMM5", 128),
(256, "XMM6", 128),
(272, "XMM7", 128),
(288, "XMM8", 128),
(304, "XMM9", 128),
(320, "XMM10", 128),
(336, "XMM11", 128),
(352, "XMM12", 128),
(368, "XMM13", 128),
(384, "XMM14", 128),
(400, "XMM15", 128),
]

def __init__(self, memory: Memory, *args, **kwargs):
"""
Builds a CPU model.
Expand Down Expand Up @@ -6799,14 +6674,6 @@ def XLATB(cpu):
"""
cpu.AL = cpu.read_int(cpu.RBX + Operators.ZEXTEND(cpu.AL, 64), 8)

@instruction
def FXSAVE(cpu, dest):
return cpu.generic_FXSAVE(dest, AMD64Cpu.FXSAVE_layout)

@instruction
def FXRSTOR(cpu, src):
return cpu.generic_FXRSTOR(src, AMD64Cpu.FXSAVE_layout)


class I386Cpu(X86Cpu):
# Config
Expand All @@ -6816,36 +6683,6 @@ class I386Cpu(X86Cpu):
arch = cs.CS_ARCH_X86
mode = cs.CS_MODE_32

# CPU specific instruction behaviour
FXSAVE_layout = [
(0, "FPCW", 16),
(2, "FPSW", 16),
(4, "FPTAG", 8),
(6, "FOP", 16),
(8, "FIP", 32),
(12, "FCS", 16),
(16, "FDP", 32),
(20, "FDS", 16),
(24, "MXCSR", 32),
(28, "MXCSR_MASK", 32),
(32, "FP0", 80),
(48, "FP1", 80),
(64, "FP2", 80),
(80, "FP3", 80),
(96, "FP4", 80),
(112, "FP5", 80),
(128, "FP6", 80),
(144, "FP7", 80),
(160, "XMM0", 128),
(176, "XMM1", 128),
(192, "XMM2", 128),
(208, "XMM3", 128),
(224, "XMM4", 128),
(240, "XMM5", 128),
(256, "XMM6", 128),
(272, "XMM7", 128),
]

def __init__(self, memory: Memory, *args, **kwargs):
"""
Builds a CPU model.
Expand Down Expand Up @@ -6912,26 +6749,7 @@ def canonical_registers(self):
regs = ["EAX", "ECX", "EDX", "EBX", "ESP", "EBP", "ESI", "EDI", "EIP"]
regs.extend(["CS", "DS", "ES", "SS", "FS", "GS"])
regs.extend(
[
"FP0",
"FP1",
"FP2",
"FP3",
"FP4",
"FP5",
"FP6",
"FP7",
"FPCW",
"FPSW",
"FPTAG",
"FOP",
"FIP",
"FCS",
"FDP",
"FDS",
"MXCSR",
"MXCSR_MASK",
]
["FP0", "FP1", "FP2", "FP3", "FP4", "FP5", "FP6", "FP7", "FPCW", "FPSW", "FPTAG"]
)
regs.extend(
[
Expand Down Expand Up @@ -6983,11 +6801,3 @@ def XLATB(cpu):
:param dest: destination operand.
"""
cpu.AL = cpu.read_int(cpu.EBX + Operators.ZEXTEND(cpu.AL, 32), 8)

@instruction
def FXSAVE(cpu, dest):
return cpu.generic_FXSAVE(dest, I386Cpu.FXSAVE_layout)

@instruction
def FXRSTOR(cpu, src):
return cpu.generic_FXRSTOR(src, I386Cpu.FXSAVE_layout)
Loading