<commit_before>#!/usr/bin/env python3
# Copyright 2020 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.
"""Reads experiment-requests.yaml and determines if there is a new experiment
and runs it if needed. Note that this code uses a config file for experiments
that is specific to the FuzzBench service. Therefore this code will break if
others try to run it."""
import argparse
import collections
import os
import re
import sys
from typing import Optional

from common import benchmark_utils
from common import fuzzer_utils
from common import logs
from common import utils
from common import yaml_utils
from database import models
from database import utils as db_utils
from experiment import run_experiment

logger = logs.Logger('automatic_run_experiment')  # pylint: disable=invalid-name

EXPERIMENT_CONFIG_FILE = os.path.join(utils.ROOT_DIR, 'service',
                                      'experiment-config.yaml')

REQUESTED_EXPERIMENTS_PATH = os.path.join(utils.ROOT_DIR, 'service',
                                          'experiment-requests.yaml')

# Don't run an experiment if we have a "request" just containing this keyword.
# TODO(metzman): Look into replacing this mechanism for pausing the service.
PAUSE_SERVICE_KEYWORD = 'PAUSE_SERVICE'

EXPERIMENT_NAME_REGEX = re.compile(r'^\d{4}-\d{2}-\d{2}.*')

# TODO(metzman): Stop hardcoding benchmarks and support marking benchmarks as
# disabled using a config file in each benchmark.
CODE_BENCHMARKS = [
    # OSS-Fuzz benchmarks.
    'bloaty_fuzz_target',
    'curl_curl_fuzzer_http',
    'jsoncpp_jsoncpp_fuzzer',
    'libpcap_fuzz_both',
    'libxslt_xpath',
    'mbedtls_fuzz_dtlsclient',
    'openssl_x509',

    # Temporarily disabled due to out-of-memory issues with clang coverage.
    # 'php_php-fuzz-parser',
    'sqlite3_ossfuzz',
    'systemd_fuzz-link-parser',
    'zlib_zlib_uncompress_fuzzer',

    # Standard benchmarks.
    'freetype2-2017',
    'harfbuzz-1.3.2',
    'lcms-2017-03-21',
    'libjpeg-turbo-07-2017',
    'libpng-1.2.56',
    'libxml2-v2.9.2',
    'openthread-2019-12-23',
    'proj4-2017-08-14',
    're2-2014-12-09',
    'vorbis-2017-12-11',
    'woff2-2016-05-06',
]
BUG_BENCHMARKS = [
    'arrow_parquet-arrow-fuzz',
    'harfbuzz_hb-subset-fuzzer',
    'libhevc_hevc_dec_fuzzer',
    'matio_matio_fuzzer',

    # Temporarily disabled since it is extremely crashy.
    # 'ndpi_fuzz_ndpi_reader',
    'openexr_openexr_exrenvmap_fuzzer',
    'openh264_decoder_fuzzer',
    'php_php-fuzz-execute',
    'php_php-fuzz-parser-2020-07-25',
    'stb_stbi_read_fuzzer',
]


def _get_experiment_name(experiment_config: dict) -> str:
    """Returns the name of the experiment described by |experiment_config| as a
    string."""
    # Use str because the yaml parser will parse things like `2020-05-06` as
    # a datetime if not included in quotes.
    return str(experiment_config['experiment'])


def _get_description(experiment_config: dict) -> Optional[str]:<commit_msg>add file bug benchmark to auto run (#1071)<commit_after>#!/usr/bin/env python3
# Copyright 2020 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.
"""Reads experiment-requests.yaml and determines if there is a new experiment
and runs it if needed. Note that this code uses a config file for experiments
that is specific to the FuzzBench service. Therefore this code will break if
others try to run it."""
import argparse
import collections
import os
import re
import sys
from typing import Optional

from common import benchmark_utils
from common import fuzzer_utils
from common import logs
from common import utils
from common import yaml_utils
from database import models
from database import utils as db_utils
from experiment import run_experiment

logger = logs.Logger('automatic_run_experiment')  # pylint: disable=invalid-name

EXPERIMENT_CONFIG_FILE = os.path.join(utils.ROOT_DIR, 'service',
                                      'experiment-config.yaml')

REQUESTED_EXPERIMENTS_PATH = os.path.join(utils.ROOT_DIR, 'service',
                                          'experiment-requests.yaml')

# Don't run an experiment if we have a "request" just containing this keyword.
# TODO(metzman): Look into replacing this mechanism for pausing the service.
PAUSE_SERVICE_KEYWORD = 'PAUSE_SERVICE'

EXPERIMENT_NAME_REGEX = re.compile(r'^\d{4}-\d{2}-\d{2}.*')

# TODO(metzman): Stop hardcoding benchmarks and support marking benchmarks as
# disabled using a config file in each benchmark.
CODE_BENCHMARKS = [
    # OSS-Fuzz benchmarks.
    'bloaty_fuzz_target',
    'curl_curl_fuzzer_http',
    'jsoncpp_jsoncpp_fuzzer',
    'libpcap_fuzz_both',
    'libxslt_xpath',
    'mbedtls_fuzz_dtlsclient',
    'openssl_x509',

    # Temporarily disabled due to out-of-memory issues with clang coverage.
    # 'php_php-fuzz-parser',
    'sqlite3_ossfuzz',
    'systemd_fuzz-link-parser',
    'zlib_zlib_uncompress_fuzzer',

    # Standard benchmarks.
    'freetype2-2017',
    'harfbuzz-1.3.2',
    'lcms-2017-03-21',
    'libjpeg-turbo-07-2017',
    'libpng-1.2.56',
    'libxml2-v2.9.2',
    'openthread-2019-12-23',
    'proj4-2017-08-14',
    're2-2014-12-09',
    'vorbis-2017-12-11',
    'woff2-2016-05-06',
]
BUG_BENCHMARKS = [
    'arrow_parquet-arrow-fuzz',
    'file_magic_fuzzer',
    'harfbuzz_hb-subset-fuzzer',
    'libhevc_hevc_dec_fuzzer',
    'matio_matio_fuzzer',

    # Temporarily disabled since it is extremely crashy.
    # 'ndpi_fuzz_ndpi_reader',
    'openexr_openexr_exrenvmap_fuzzer',
    'openh264_decoder_fuzzer',
    'php_php-fuzz-execute',
    'php_php-fuzz-parser-2020-07-25',
    'stb_stbi_read_fuzzer',
]


def _get_experiment_name(experiment_config: dict) -> str:
    """Returns the name of the experiment described by |experiment_config| as a
    string."""
    # Use str because the yaml parser will parse things like `2020-05-06` as
    # a datetime if not included in quotes.
    return str(experiment_config['experiment'])


def _get_description(experiment_config: dict) -> Optional[str]:
{'fraction_non_alphanumeric': 0.09517004858531009, 'fraction_numerical': 0.03143755358673907, 'mean_word_length': 3.757987763426241, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4314630', 'n_tokens_mistral': 2417, 'n_tokens_neox': 2240, 'n_words': 659}