def run(self): with fasteners.try_lock(self._lock) as was_locked: if not was_locked: raise exc.ExecutionFailure("Engine currently locked, please" " try again later") for _state in self.run_iter(): pass
def ensure_binaryen(url, version): """Download and use a compiled binaryen to generate WebAssembly files if it does not exist. Args: url (str): URL of the compressed binaryen binary package version (int): Version of the compressed binaryen binary package Returns: Path: Path of the extracted wasm-opt binary """ shell_cache = sm_compile_helpers.ensure_cache_dir(Path.home()) wasmopt_path = Path( shell_cache / f"binaryen-version_{version}" / ("wasm-opt" + (".exe" if platform.system() == "Windows" else ""))).resolve() sleep_time = 2 t_lock = threading.Lock() with fasteners.try_lock(t_lock) as gotten: while not wasmopt_path.is_file(): if gotten: with requests.get(url, allow_redirects=True, stream=True) as binaryen_gzip_request: try: with tarfile.open(fileobj=io.BytesIO( binaryen_gzip_request.content), mode="r:gz") as f: f.extractall(str(shell_cache.resolve())) except OSError: print("binaryen tarfile threw an OSError") break sleep(sleep_time) sleep_time *= 2 return wasmopt_path
def wasmopt_run(seed): """Runs binaryen with the generated seed. Args: seed (Path): Generated jsfunfuzz file (acts as the seed for binaryen) Returns: bool: Returns True on successful wasm-opt execution, False otherwise """ assert platform.system() == "Linux" assert seed.is_file() seed_wrapper_output = seed.resolve().with_suffix(".wrapper") seed_wasm_output = seed.resolve().with_suffix(".wasm") t_lock = threading.Lock() with fasteners.try_lock(t_lock) as gotten: while True: if gotten: subprocess.run([ ensure_binaryen(BINARYEN_URL, BINARYEN_VERSION), seed, "--translate-to-fuzz", "--disable-simd", "--output", seed_wasm_output, f"--emit-js-wrapper={seed_wrapper_output}" ], check=True) break time.sleep(5) assert seed_wrapper_output.is_file() assert seed_wasm_output.is_file() return (seed_wrapper_output, seed_wasm_output)
def wrapper(path, *args, **kwargs): lockfile_name = os.path.join(path, LOCKFILE_NAME) with fasteners.try_lock( fasteners.InterProcessLock(lockfile_name)) as locked: if not locked: raise RuntimeError("Failed to lock cache %r." % path) return wrapped(path, *args, **kwargs)
def wasmopt_run(seed): """Runs binaryen with the generated seed. Args: seed (Path): Generated jsfunfuzz file (acts as the seed for binaryen) Returns: bool: Returns True on successful wasm-opt execution, False otherwise """ assert platform.system() == "Linux" assert seed.is_file() seed_wrapper_output = seed.resolve().with_suffix(".wrapper") seed_wasm_output = seed.resolve().with_suffix(".wasm") sleep_time = 2 t_lock = threading.Lock() with fasteners.try_lock(t_lock) as gotten: while True: if gotten: try: # Wrapping this in str() seems necessary for Python 3.7.x and lower. # See Python issue 31961 subprocess.run([ str(ensure_binaryen(BINARYEN_URL, BINARYEN_VERSION)), str(seed), "--translate-to-fuzz", "--disable-simd", "--output", str(seed_wasm_output), f"--emit-js-wrapper={seed_wrapper_output}" ], check=True) except (subprocess.CalledProcessError, OSError): print( "wasm-opt aborted with a CalledProcessError or OSError. Trying again after 1 minute..." ) sleep(60) # Wrapping this in str() seems necessary for Python 3.7.x and lower. # See Python issue 31961 subprocess.run([ str(ensure_binaryen(BINARYEN_URL, BINARYEN_VERSION)), str(seed), "--translate-to-fuzz", "--disable-simd", "--output", str(seed_wasm_output), f"--emit-js-wrapper={seed_wrapper_output}" ], check=True) break sleep(sleep_time) sleep_time *= 2 assert seed_wrapper_output.is_file() assert seed_wasm_output.is_file() return (seed_wrapper_output, seed_wasm_output)
def run(self, timeout=None): """Runs the engine (or die trying). :param timeout: timeout to wait for any atoms to complete (this timeout will be used during the waiting period that occurs when unfinished atoms are being waited on). """ with fasteners.try_lock(self._lock) as was_locked: if not was_locked: raise exc.ExecutionFailure("Engine currently locked, please" " try again later") for _state in self.run_iter(timeout=timeout): pass
async def download_tar_ball_of(self, name: str, version: str, download_dir: str): file_name = npm_utils.build_filename(name, version) url = npm_utils.build_tarball_url(name, version) parent_dir = os.path.join(download_dir, name) os.makedirs(parent_dir, exist_ok=True) file_path = os.path.join(parent_dir, file_name) if os.path.exists(file_path): return (file_path, False) file_lock = fasteners.InterProcessLock(file_path) with fasteners.try_lock(file_lock) as got_file_lock: if not got_file_lock: return (file_path, False) async with self._session.get(url) as response: async with aiofiles.open(file_path, 'wb') as file_stream: await npm_utils.copyfileobj(response.content, file_stream) return (file_path, True)
def _open_grailfile(path): """Lock and open the Grailfile at the given path.""" # if the Grailfile is foobar/Grailfile, store a lock at foobar/.grail/LOCK dotdir_path = _get_dotgrail_dir(path) lock_path = dotdir_path / "LOCK" # Don't sit there waiting for the Grailfile to be unlocked lock = fasteners.InterProcessLock(str(lock_path)) with fasteners.try_lock(lock) as got: if not got: raise utils.GrailError("Grailfile is locked") # Open the manifest and read it entirely into memory lines = None with path.open("r") as f: lines = list(f.readlines()) # Return the Grailfile object from the context manager grailfile = Grailfile(lines) yield grailfile # When the context manager is exiting, write out the contents of the manifest to disk. with path.open("w") as f: grailfile.write(f)
def test_try_lock(self): lock = threading.Lock() with fasteners.try_lock(lock) as locked: self.assertTrue(locked) with fasteners.try_lock(lock) as locked: self.assertFalse(locked)
def wrapper(path, *args, **kwargs): lockfile_name = os.path.join(path, LOCKFILE_NAME) with fasteners.try_lock(fasteners.InterProcessLock(lockfile_name)) as locked: if not locked: raise RuntimeError("Failed to lock cache %r." % path) return wrapped(path, *args, **kwargs)
def test_try_lock(): lock = threading.Lock() with fasteners.try_lock(lock) as locked_1: assert locked_1 with fasteners.try_lock(lock) as locked_2: assert not locked_2