Skip to content

Instantly share code, notes, and snippets.

@pschanely
Created November 18, 2020 14:01
Show Gist options
  • Save pschanely/c884c1191057ec3939a8c40274b61717 to your computer and use it in GitHub Desktop.
Save pschanely/c884c1191057ec3939a8c40274b61717 to your computer and use it in GitHub Desktop.
Experimentation in getting CrossHair to reverse Hypothesis strategies.
from hypothesis import given
import hypothesis.strategies as st
# Background:
# https://github.com/pschanely/CrossHair
# https://hypothesis.readthedocs.io/en/latest/
# Hypothesis strategies produce python values from byte strings.
# In theory, CrossHair can "reverse" them - find byte strings that produce
# a specific value. Just run `crosshair check` on this file.
# This is the first attempt. I strongly suspect there is headroom to do better;
# but somewhat doubtful that it will ever be highly effective at this task.
# Stuff I can find bytes for:
strat, value = st.none(), None
strat, value = st.booleans(), True
strat, value = st.booleans(), False
strat, value = st.text(), '' # Empty string is the only one I can find right now.
strat, value = st.integers(min_value=0, max_value=10), 9
strat, value = st.integers(min_value=0, max_value=256), 42 # with --per_condition_timeout=5
strat, value = st.tuples(st.booleans()), (True,)
strat, value = st.tuples(st.booleans(), st.booleans()), (False, True) # with --per_condition_timeout=60 ((True, False) though I didn't get to work!)
strat, value = st.integers(), 0 # zero is the ONLY value I can find for unbounded integers()
# Easy stuff that I can't find bytes for:
strat, value = st.integers(), 1
strat, value = st.tuples(st.booleans(), st.booleans()), (True, False)
strat, value = st.text(), 'a'
def foo(i):
assert i != value
return i
fuzz_target = given(strat)(foo).hypothesis.fuzz_one_input
def test(byteinput: bytes):
''' post: True '''
fuzz_target(byteinput)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment