From d38768bdf984bba0d8c55dbb8947273b23553c2e Mon Sep 17 00:00:00 2001 From: Dominik Muhs Date: Tue, 4 Dec 2018 19:26:28 +0100 Subject: [PATCH] Add signature DB thread-safe singleton (for multithreaded tests) --- mythril/support/signatures.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/mythril/support/signatures.py b/mythril/support/signatures.py index e93897a0..5f968026 100644 --- a/mythril/support/signatures.py +++ b/mythril/support/signatures.py @@ -6,6 +6,8 @@ import os import time import logging import sqlite3 +import threading +import functools from typing import List from collections import defaultdict @@ -13,9 +15,27 @@ from subprocess import Popen, PIPE from mythril.exceptions import CompilerError +lock = threading.Lock() + + +def synchronized(sync_lock): + """ Synchronization decorator """ + + def wrapper(f): + @functools.wraps(f) + def inner_wrapper(*args, **kw): + with sync_lock: + return f(*args, **kw) + + return inner_wrapper + + return wrapper + + class Singleton(type): _instances = {} + @synchronized(lock) def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) @@ -207,7 +227,9 @@ class SignatureDB(object, metaclass=Singleton): solc_text = line.split(":")[1].strip() self.solidity_sigs[solc_bytes].append(solc_text) - logging.debug("Signatures: found %d signatures after parsing" % len(self.solidity_sigs)) + logging.debug( + "Signatures: found %d signatures after parsing" % len(self.solidity_sigs) + ) # update DB with what we've found for byte_sig, text_sigs in self.solidity_sigs.items():