Exemple #1
0
def test_is_valid_opcode_invalidates_bytes_after_PUSHXX_opcodes():
    code_stream = CodeStream(b'\x02\x60\x02\x04')
    assert code_stream.is_valid_opcode(0) is True
    assert code_stream.is_valid_opcode(1) is True
    assert code_stream.is_valid_opcode(2) is False
    assert code_stream.is_valid_opcode(3) is True
    assert code_stream.is_valid_opcode(4) is False
Exemple #2
0
def test_right_number_of_bytes_invalidated_after_pushxx():
    code_stream = CodeStream(b'\x02\x03\x60\x02\x02')
    assert code_stream.is_valid_opcode(0) is True
    assert code_stream.is_valid_opcode(1) is True
    assert code_stream.is_valid_opcode(2) is True
    assert code_stream.is_valid_opcode(3) is False
    assert code_stream.is_valid_opcode(4) is True
    assert code_stream.is_valid_opcode(5) is False
Exemple #3
0
def test_is_valid_opcode_valid_with_PUSH32_just_past_boundary():
    # valid: 0 :: 33
    # invalid: 1 - 32 (PUSH32) :: 34+ (too long)
    code_stream = CodeStream(b'\x7f' + (b'\0' * 32) + b'\x60')
    assert code_stream.is_valid_opcode(0) is True
    for pos in range(1, 33):
        assert code_stream.is_valid_opcode(pos) is False
    assert code_stream.is_valid_opcode(33) is True
    assert code_stream.is_valid_opcode(34) is False
Exemple #4
0
def test_harder_is_valid_opcode():
    code_stream = CodeStream(b'\x02\x03\x72' + (b'\x04' * 32) + b'\x05')
    # valid: 0 - 2 :: 22 - 35
    # invalid: 3-21 (PUSH19) :: 36+ (too long)
    assert code_stream.is_valid_opcode(0) is True
    assert code_stream.is_valid_opcode(1) is True
    assert code_stream.is_valid_opcode(2) is True
    assert code_stream.is_valid_opcode(3) is False
    assert code_stream.is_valid_opcode(21) is False
    assert code_stream.is_valid_opcode(22) is True
    assert code_stream.is_valid_opcode(35) is True
    assert code_stream.is_valid_opcode(36) is False
Exemple #5
0
def test_even_harder_is_valid_opcode_first_check_deep():
    test = b'\x02\x03\x7d' + (b'\x04' * 32) + b'\x05\x7e' + (
        b'\x04' * 35) + b'\x01\x61\x01\x01\x01'
    code_stream = CodeStream(test)
    # valid: 0 - 2 :: 33 - 36 :: 68 - 73 :: 76
    # invalid: 3 - 32 (PUSH30) :: 37 - 67 (PUSH31) :: 74, 75 (PUSH2) :: 77+ (too long)
    assert code_stream.is_valid_opcode(75) is False
Exemple #6
0
def test_even_harder_is_valid_opcode():
    test = b'\x02\x03\x7d' + (b'\x04' * 32) + b'\x05\x7e' + (
        b'\x04' * 35) + b'\x01\x61\x01\x01\x01'
    code_stream = CodeStream(test)
    # valid: 0 - 2 :: 33 - 36 :: 68 - 73 :: 76
    # invalid: 3 - 32 (PUSH30) :: 37 - 67 (PUSH31) :: 74, 75 (PUSH2) :: 77+ (too long)
    assert code_stream.is_valid_opcode(0) is True
    assert code_stream.is_valid_opcode(1) is True
    assert code_stream.is_valid_opcode(2) is True
    assert code_stream.is_valid_opcode(3) is False
    assert code_stream.is_valid_opcode(32) is False
    assert code_stream.is_valid_opcode(33) is True
    assert code_stream.is_valid_opcode(36) is True
    assert code_stream.is_valid_opcode(37) is False
    assert code_stream.is_valid_opcode(67) is False
    assert code_stream.is_valid_opcode(68) is True
    assert code_stream.is_valid_opcode(71) is True
    assert code_stream.is_valid_opcode(72) is True
    assert code_stream.is_valid_opcode(73) is True
    assert code_stream.is_valid_opcode(74) is False
    assert code_stream.is_valid_opcode(75) is False
    assert code_stream.is_valid_opcode(76) is True
    assert code_stream.is_valid_opcode(77) is False
Exemple #7
0
def test_fuzzy_is_valid_opcode(opcodes, data):
    if opcodes:
        indices, bytecode_sections = zip(*_mk_bytecode(opcodes, data))
        bytecode = b''.join(bytecode_sections)
    else:
        indices = set()
        bytecode = b''

    valid_indices = set(indices)

    stream = CodeStream(bytecode)

    index_st = st.integers(min_value=0, max_value=len(bytecode) + 10)
    to_check = data.draw(st.lists(index_st, max_size=len(bytecode)))
    for index in to_check:
        is_valid = stream.is_valid_opcode(index)
        expected = index in valid_indices
        assert is_valid is expected