Skip to content
Snippets Groups Projects
Commit f5c2f667 authored by vincent's avatar vincent
Browse files

[BTC Language] Raise an error when an invalid opcode is encountered.

parent 3eb25300
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
import hashlib
import operator
from typing import Any, Callable, Dict, List
from symbolic_execution.language.exceptions import InvalidOpCode
from symbolic_execution.program.instruction.general import (
NOP,
InvalidInstruction,
......@@ -787,7 +788,7 @@ class OpCodes:
if opcode in OpCodes.__int_to_tokens:
return OpCodes.__int_to_tokens[opcode]
else:
return None
raise InvalidOpCode(f"No known token for {opcode}")
def token_to_opcode(token: str) -> OpCode:
"""Return the Opcode (object) associated to a token."""
......
......@@ -51,11 +51,10 @@ class BitcoinParser(ILanguage):
# The only difference with a BYTE, is that an OP_CODE needs to be
# converted into a token.
OP_CODE = Word(hexnums, exact=2)
OP_CODE.addParseAction(
lambda tokens: list(
map(lambda t: OpCodes.int_to_tokens(int(t, 16)), tokens)
)
)
@OP_CODE.addParseAction
def tokenify(tokens):
return list(map(lambda t: OpCodes.int_to_tokens(int(t, 16)), tokens))
# CONSTANTS
OP_PUSH_0 = Or([format(op, "02x") for op in range(0x01, 0x4C)])
......
"""Custom exception for language."""
class InvalidOpCode(Exception):
"""Raised when attempting to tokenify an unknown opcode."""
def __init__(self, message: str):
"""Init the exception."""
super().__init__(message)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment