File: fuzzloops.py

package info (click to toggle)
prjtrellis 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 83,000 kB
  • sloc: cpp: 20,813; python: 16,246; sh: 375; makefile: 262; asm: 80; ansic: 58
file content (51 lines) | stat: -rw-r--r-- 1,463 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
General Utilities for Fuzzing
"""

import os
from threading import Thread, RLock

def parallel_foreach(items, func):
    """
    Run a function over a list of values, running a number of jobs
    in parallel. TRELLIS_JOBS should be set to the number of jobs to run,
    defaulting to 4.
    """
    if "TRELLIS_JOBS" in os.environ:
        jobs = int(os.environ["TRELLIS_JOBS"])
    else:
        jobs = 4
    items_queue = list(items)
    items_lock = RLock()

    def runner():
        while True:
            with items_lock:
                if len(items_queue) == 0:
                    return
                item = items_queue[0]
                items_queue.pop(0)
            func(item)

    threads = [Thread(target=runner) for i in range(jobs)]
    for t in threads:
        t.start()
    for t in threads:
        t.join()


def journal_foreach(items, func):
    """
    Run a function over a list of items, keeping a journal of which items have been visited. If the script is
    interrupted, it will return where it stopped if the list of items have not changed.

    If an exception occurs during an item, that exception will be logged in the journal also.

    Items must have an unambiguous string conversion, and should normally be string keys, that can be saved in the
    journal.

    The journal is called "fuzz.journal" in the current working directory. At present, this implementation is not thread
    safe.
    """
    # TODO
    pass