<commit_before># Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Integration code for centipede fuzzer."""

import subprocess
import os

from fuzzers import utils


def build():
    """Build benchmark."""
    san_cflags = ['-fsanitize-coverage=trace-loads']

    link_cflags = [
        '-ldl',
        '-lrt',
        '-lpthread',
        '/lib/weak.o',
    ]

    # TODO(Dongge): Build targets with sanitizers.
    try:
        centipede_cflags = subprocess.run(
            ['cat', '/src/centipede/clang-flags.txt'],
            stdout=subprocess.PIPE,
            check=True).stdout.decode('utf-8').split('\n')
    except subprocess.CalledProcessError as err:
        print(err)

    cflags = san_cflags + centipede_cflags + link_cflags
    utils.append_flags('CFLAGS', cflags)
    utils.append_flags('CXXFLAGS', cflags)

    os.environ['CC'] = '/clang/bin/clang'
    os.environ['CXX'] = '/clang/bin/clang++'
    os.environ[
        'FUZZER_LIB'] = '/src/centipede/bazel-bin/libcentipede_runner.pic.a'

    utils.build_benchmark()


def fuzz(input_corpus, output_corpus, target_binary):
    """Run fuzzer. Wrapper that uses the defaults when calling run_fuzzer."""
    run_fuzzer(input_corpus, output_corpus, target_binary)


def run_fuzzer(input_corpus, output_corpus, target_binary, extra_flags=None):
    """Run fuzzer."""
    if extra_flags is None:
        extra_flags = []

    # Seperate out corpus and crash directories as sub-directories of
    # |output_corpus| to avoid conflicts when corpus directory is reloaded.
    work_dir = os.path.join(output_corpus, 'work-dir')
    work_dir_crash = os.path.join(work_dir, 'crashes')
    crashes_dir = os.path.join(output_corpus, 'crashes')
    output_corpus = os.path.join(output_corpus, 'corpus')<commit_msg>Improve centipede integration. (#1445)<commit_after># Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Integration code for centipede fuzzer."""

import subprocess
import os

from fuzzers import utils


def build():
    """Build benchmark."""
    san_cflags = ['-fsanitize-coverage=trace-loads']

    link_cflags = [
        '-ldl',
        '-lrt',
        '-lpthread',
        '/lib/weak.o',
    ]

    # TODO(Dongge): Build targets with sanitizers.
    with open('/src/centipede/clang-flags.txt', 'r') as clang_flags_handle:
        centipede_cflags = [
            line.strip() for line in clang_flags_handle.readlines()
        ]

    cflags = san_cflags + centipede_cflags + link_cflags
    utils.append_flags('CFLAGS', cflags)
    utils.append_flags('CXXFLAGS', cflags)

    os.environ['CC'] = '/clang/bin/clang'
    os.environ['CXX'] = '/clang/bin/clang++'
    os.environ['FUZZER_LIB'] = (
        '/src/centipede/bazel-bin/libcentipede_runner.pic.a')
    utils.build_benchmark()


def fuzz(input_corpus, output_corpus, target_binary):
    """Run fuzzer. Wrapper that uses the defaults when calling run_fuzzer."""
    run_fuzzer(input_corpus, output_corpus, target_binary)


def run_fuzzer(input_corpus, output_corpus, target_binary, extra_flags=None):
    """Run fuzzer."""
    if extra_flags is None:
        extra_flags = []

    # Seperate out corpus and crash directories as sub-directories of
    # |output_corpus| to avoid conflicts when corpus directory is reloaded.
    work_dir = os.path.join(output_corpus, 'work-dir')
    work_dir_crash = os.path.join(work_dir, 'crashes')
    crashes_dir = os.path.join(output_corpus, 'crashes')
    output_corpus = os.path.join(output_corpus, 'corpus')
{'fraction_non_alphanumeric': 0.11357095417312375, 'fraction_numerical': 0.0046491033872038965, 'mean_word_length': 3.5590312815338043, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7630105', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1437, 'n_words': 398}