Exemple #1
0
def sample(seed, count, max_size):
    random = Random(seed)

    names = os.listdir(raw)
    names.sort()
    random.shuffle(names)

    printed = 0

    for n in names:
        with open(os.path.join(raw, n), 'rb') as i:
            buffer = i.read()
        if max_size > 0 and len(buffer) > max_size:
            continue

        initial_data = ConjectureData.for_buffer(buffer)
        prog = os.path.join(programs, n + '.c')
        with open(prog, 'r') as i:
            already_good = is_valid(prog)

        if not already_good:
            try:
                gen(initial_data, prog)
            except StopTest:
                continue

        if not interesting_reason(prog, printing=False):
            continue

        print(n)
        printed += 1
        if printed >= count:
            break
Exemple #2
0
def cleanup():
    files = glob('hypothesis-examples/raw/*')
    Random().shuffle(files)

    for f in files:
        try:
            with open(f, 'rb') as i:
                data = ConjectureData.for_buffer(i.read())
        except FileNotFoundError:
            continue

        with tempfile.TemporaryDirectory() as d:
            prog = os.path.join(d, 'test.c')

            try:
                gen(data, prog)
            except StopTest:
                continue

            name = os.path.basename(f)

            if not is_interesting(prog):
                print(f"Removing {name}")
                rmf(os.path.join(raw, name))
                rmf(os.path.join(programs, name + '.c'))
Exemple #3
0
def data_to_text(data):
    prog = mktemp(suffix='.c')
    try:
        gen(data, prog)
        with open(prog) as i:
            return i.read()
    finally:
        os.unlink(prog)
Exemple #4
0
def show(filename):
    with open(filename, 'rb') as i:
        initial_data = ConjectureData.for_buffer(i.read())
    prog = mktemp(suffix='.c')
    try:
        gen(initial_data, prog)
        with open(prog) as i:
            print(i.read())
    finally:
        os.unlink(prog)
Exemple #5
0
        def test_function(data):
            with tempfile.TemporaryDirectory() as d:
                prog = os.path.join(d, 'test.c')

                try:
                    gen(data, prog)
                    data.extra_information.generated_length = os.stat(prog).st_size
                    run_gcc(gcc, opt, prog)
                except subprocess.TimeoutExpired:
                    return
                except subprocess.SubprocessError:
                    name = hashlib.sha1(data.buffer).hexdigest()[:8]
                    data.unique_name = name
                    data.mark_interesting()
                finally:
                    log_data(data)
Exemple #6
0
        def test_function(data):
            with tempfile.TemporaryDirectory() as d:
                prog = os.path.join(d, 'test.c')

                try:
                    gen(data, prog)
                    data.generated_length = os.stat(prog).st_size
                    if run_gcc(gcc, opt, prog) != run_gcc(
                        '/opt/compiler-explorer/gcc-8.2.0/bin/gcc', '-O0', prog
                    ):
                        name = hashlib.sha1(data.buffer).hexdigest()[:8]
                        data.unique_name = name
                        data.mark_interesting()
                except subprocess.SubprocessError:
                    pass
                finally:
                    log_data(data)
Exemple #7
0
    def try_data(data):
        with tempfile.TemporaryDirectory() as d:
            prog = os.path.join(d, 'test.c')

            try:
                gen(data, prog)
            except StopTest:
                rmf(prog)
                return

            name = hashlib.sha1(data.buffer).hexdigest()[:8]

            print(name)

            if is_interesting(prog):
                with open(os.path.join(raw, name), 'wb') as o:
                    o.write(data.buffer)
                shutil.move(prog, os.path.join(programs, name + '.c'))
            else:
                rmf(os.path.join(raw, name))
                rmf(os.path.join(programs, name + '.c'))
    except FileExistsError:
        pass

    rnd = Random()

    found = 0
    i = 0
    while found < LIMIT:
        i += 1
        print("Iter", i)
        data = ConjectureData(
            max_length=2**29,
            draw_bytes=lambda self, n: rnd.getrandbits(8 * n).to_bytes(
                n, byteorder='big'
            )
        )

        sourcename = "example.c"

        gen(data, sourcename)

        print("Generated")

        if results_for_o1_and_o2_differ(sourcename):
            found += 1
            print("Found", found)
            result = bytes(data.buffer)
            name = hashlib.sha1(result).hexdigest()[:16]
            with open(os.path.join(EXAMPLES, name), 'wb') as outfile:
                outfile.write(result)
Exemple #9
0
    try:
        os.makedirs(EXAMPLES)
    except FileExistsError:
        pass

    rnd = Random()

    found = 0
    i = 0
    while found < LIMIT:
        i += 1
        print("Iter", i)
        data = ConjectureData(max_length=2**29,
                              draw_bytes=lambda self, n: rnd.getrandbits(8 * n)
                              .to_bytes(n, byteorder='big'))

        sourcename = "example.c"

        gen(data, sourcename)

        print("Generated")

        if results_for_o1_and_o2_differ(sourcename):
            found += 1
            print("Found", found)
            result = bytes(data.buffer)
            name = hashlib.sha1(result).hexdigest()[:16]
            with open(os.path.join(EXAMPLES, name), 'wb') as outfile:
                outfile.write(result)
Exemple #10
0
def generate_from_bytes(filename):
    if filename.endswith(".c"):
        return
    with open(filename, 'rb') as infile:
        data = ConjectureData.for_buffer(infile.read())
    gen(data, filename + ".c")
Exemple #11
0
def shrink(filename, sizes):
    with open(filename, 'rb') as i:
        initial_data = ConjectureData.for_buffer(i.read())

    printing_to_stdout = False

    def data_info(data):
        name = hashlib.sha1(data.buffer).hexdigest()[:8]
        input_length = len(data.buffer)
        output_length =  data.extra_information.generated_length

        return {
            "name": name, "input_length": input_length, "output_length": output_length,
            "interesting": data.status == Status.INTERESTING
        }

    if sizes is not None:
        if sizes == "-":
            writer = sys.stdout
            printing_to_stdout = True
        else:
            writer = open(sizes, 'w')

        def log_data(data):
            if data.status >= Status.VALID and hasattr(data.extra_information, 'generated_length'):
                writer.write(json.dumps(data_info(data)) + "\n")
                writer.flush()
    else:
        writer = None
        def log_data(data):
            pass

    initial = mktemp(suffix='.c') 
    gen(initial_data, initial)
    errtype, gcc, opt = interesting_reason(initial, printing=not printing_to_stdout)

    if errtype == 'error':
        def test_function(data):
            with tempfile.TemporaryDirectory() as d:
                prog = os.path.join(d, 'test.c')

                try:
                    gen(data, prog)
                    data.extra_information.generated_length = os.stat(prog).st_size
                    run_gcc(gcc, opt, prog)
                except subprocess.TimeoutExpired:
                    return
                except subprocess.SubprocessError:
                    name = hashlib.sha1(data.buffer).hexdigest()[:8]
                    data.unique_name = name
                    data.mark_interesting()
                finally:
                    log_data(data)
    else:
        assert errtype == "differs"
        
        def test_function(data):
            with tempfile.TemporaryDirectory() as d:
                prog = os.path.join(d, 'test.c')

                try:
                    gen(data, prog)
                    data.generated_length = os.stat(prog).st_size
                    if run_gcc(gcc, opt, prog) != run_gcc(
                        '/opt/compiler-explorer/gcc-8.2.0/bin/gcc', '-O0', prog
                    ):
                        name = hashlib.sha1(data.buffer).hexdigest()[:8]
                        data.unique_name = name
                        data.mark_interesting()
                except subprocess.SubprocessError:
                    pass
                finally:
                    log_data(data)

    eng.MAX_SHRINKS = 10 ** 6

    with open(filename, 'rb') as i:
        buffer = i.read()

    # tracemalloc.start()

    runner = eng.ConjectureRunner(test_function, settings=settings(
        database=None,
        max_examples=1000,
        timeout=unlimited, suppress_health_check=HealthCheck.all(),
        deadline=None,
        verbosity=Verbosity.quiet if printing_to_stdout else Verbosity.debug,
        buffer_size=BUFFER_SIZE
    ), random=Random(int.from_bytes(hashlib.sha1(buffer).digest(), 'big')))


    class FakeTree(object):
        def add(self, data):
            pass

        def rewrite(self, buffer):
            return (buffer, None)

        def generate_novel_prefix(self, random):
            return b''

        is_exhausted = False


    def uncached_test_function(buffer):
        data = ConjectureData.for_buffer(buffer)
        runner.test_function(data)

#       snapshot = tracemalloc.take_snapshot()
#       top_stats = snapshot.statistics('lineno')

        print("[ Top 10 ]")
        for stat in top_stats[:10]:
            print(stat)

        return data.as_result()

#   runner.tree = FakeTree()
#   runner.cached_test_function = uncached_test_function
#   runner.target_selector.add = lambda x: None

    runner.test_function(ConjectureData.for_buffer(buffer))

    assert runner.interesting_examples

    runner.debug_data = lambda data: runner.debug(
        f"DATA({getattr(data, 'unique_name', None)}): {len(data.buffer)} bytes, {data.status.name}, {data.interesting_origin}"
    )

    v, = runner.interesting_examples.values()

    shrinker = runner.new_shrinker(
        v, lambda d: d.status == Status.INTERESTING and d.interesting_origin == v.interesting_origin
    )

    initial_calls = runner.call_count
    initial_valid = runner.valid_examples
    start = time.monotonic()
    shrinker.shrink()
    end = time.monotonic()
    final = data_info(shrinker.shrink_target)
    final["bytes"] = encode_failure(shrinker.shrink_target.buffer).decode('ascii')

    if writer is not None:
        writer.write(json.dumps({
            "runtime": end - start,
            "calls": runner.call_count - initial_calls,
            "valid": runner.valid_examples - initial_valid,
            "final": final,
        }) + "\n")

    result = runner.interesting_examples[v.interesting_origin]
    runner.debug_data(result)

    gen(ConjectureData.for_buffer(result.buffer), 'shrunk.c')
    if writer is not None:
        writer.close()
def generate_from_bytes(filename):
    if filename.endswith(".c"):
        return
    with open(filename, 'rb') as infile:
        data = ConjectureData.for_buffer(infile.read())
    gen(data, filename + ".c")