Ejemplo n.º 1
0
  def test_fuzzer_can_boot_and_run_reproducer(self):
    """Tests running a testcase that should cause a fast, predictable crash."""
    environment.set_value('FUZZ_TARGET', 'example-fuzzers/overflow_fuzzer')
    environment.set_value('JOB_NAME', 'libfuzzer_asan_fuchsia')
    build_manager.setup_build()
    testcase_path, _ = setup_testcase_and_corpus('fuchsia_crash',
                                                 'empty_corpus')
    engine_impl = engine.LibFuzzerEngine()
    result = engine_impl.reproduce('example-fuzzers/overflow_fuzzer',
                                   testcase_path,
                                   ['-timeout=25', '-rss_limit_mb=2560'], 30)

    self.assertIn('ERROR: AddressSanitizer: heap-buffer-overflow on address',
                  result.output)
    self.assertIn('Running: data/fuchsia_crash', result.output)
Ejemplo n.º 2
0
def _update_environment_for_testcase(testcase, build_directory):
  """Update environment variables that depend on the test case."""
  commands.update_environment_for_job(testcase.job_definition)
  environment.set_value('JOB_NAME', testcase.job_type)

  fuzzer_directory = setup.get_fuzzer_directory(testcase.fuzzer_name)
  environment.set_value('FUZZER_DIR', fuzzer_directory)

  task_name = environment.get_value('TASK_NAME')
  setup.prepare_environment_for_testcase(testcase, testcase.job_type, task_name)

  build_manager.set_environment_vars(
      [environment.get_value('FUZZER_DIR'), build_directory])

  _verify_target_exists(build_directory)
Ejemplo n.º 3
0
 def test_fuzzer_can_boot_and_run(self):
     """Tests running a single round of fuzzing on a Fuchsia target, using
 'echo' in place of a fuzzing command."""
     # TODO(flowerhack): Fuchsia's `fuzz` only calls 'echo running on fuchsia!'
     # right now by default, but we'll call it explicitly in here as we
     # diversity `fuzz`'s functionality
     build_manager.setup_fuchsia_build()
     environment.set_value('FUZZ_TARGET', 'example_fuzzers/toy_fuzzer')
     testcase_path = setup_testcase_and_corpus('aaaa',
                                               'empty_corpus',
                                               fuzz=True)
     output = run_launcher(testcase_path, 'test_fuzzer')
     self.assertIn(
         'localhost run \'fuchsia-pkg://fuchsia.com/example_fuzzers#meta/'
         'toy_fuzzer.cmx\'', output)
Ejemplo n.º 4
0
  def setUp(self):
    android_helpers.AndroidTest.setUp(self)
    BaseIntegrationTest.setUp(self)

    if android.settings.get_sanitizer_tool_name() != 'hwasan':
      raise Exception('Device is not set up with HWASan.')

    environment.set_value('BUILD_DIR', ANDROID_DATA_DIR)
    environment.set_value('JOB_NAME', 'libfuzzer_hwasan_android_device')
    environment.reset_current_memory_tool_options()

    self.crash_dir = TEMP_DIR
    self.adb_path = android.adb.get_adb_path()
    self.hwasan_options = 'HWASAN_OPTIONS="%s"' % quote(
        environment.get_value('HWASAN_OPTIONS'))
Ejemplo n.º 5
0
    def setUp(self):
        test_helpers.patch_environ(self)
        environment.set_value('FUZZER_NAME', 'fuzzer_1')
        environment.set_value('JOB_NAME', 'fake_job')

        # To be used for generation of date and time when uploading a log.
        self.fake_utcnow = datetime.datetime(2017, 3, 21, 11, 15, 13, 666666)
        self.fake_log_time = datetime.datetime(2017, 4, 22, 12, 16, 14, 777777)

        test_helpers.patch(self, [
            'datetime.datetime',
            'google_cloud_utils.storage.write_data',
        ])

        self.mock.datetime.utcnow.return_value = self.fake_utcnow
Ejemplo n.º 6
0
def _handle_response(build, response):
    """Handle build setup response."""
    if not response.result:
        _clear_env()
        return False

    _update_env_from_response(response)

    if not environment.get_value('APP_PATH'):
        fuzzer_directory = environment.get_value('FUZZER_DIR')
        if fuzzer_directory:
            build_manager.set_environment_vars([fuzzer_directory])

    environment.set_value('APP_REVISION', build.revision)
    return True
Ejemplo n.º 7
0
def _connect():
  """Initial connect to the worker."""
  worker_assignment = _get_host_worker_assignment()
  assert worker_assignment is not None
  assert worker_assignment.worker_name is not None
  assert worker_assignment.project_name is not None

  root_cert = _get_root_cert(worker_assignment.project_name)
  if not root_cert:
    logs.log_warn('TLS certs not yet generated.')
    time.sleep(WAIT_TLS_CERT_SECONDS)
    sys.exit(0)

  environment.set_value(
      'QUEUE_OVERRIDE',
      untrusted.platform_name(worker_assignment.project_name, 'linux'))

  server_name = worker_assignment.worker_name
  if not environment.get_value('LOCAL_DEVELOPMENT'):
    server_name += untrusted.internal_network_domain()

  _host_state.worker_bot_name = worker_assignment.worker_name

  credentials = grpc.ssl_channel_credentials(root_cert)
  _host_state.channel = grpc.secure_channel(
      '%s:%d' % (server_name, config.PORT),
      credentials=credentials,
      options=config.GRPC_OPTIONS)
  _host_state.stub = UntrustedRunnerStub(_host_state.channel)

  logs.log('Connecting to worker %s...' % server_name)
  _host_state.channel.subscribe(
      _channel_connectivity_changed, try_to_connect=True)

  channel_state = _check_channel_state(config.INITIAL_CONNECT_TIMEOUT_SECONDS)
  if channel_state == ChannelState.INCONSISTENT:
    logs.log_warn('Worker inconsistent on initial connect.')
    monitoring_metrics.HOST_INCONSISTENT_COUNT.increment()
    host_exit_no_return(return_code=0)

  if channel_state != ChannelState.READY:
    raise untrusted.HostException('Failed to connect to worker.')

  environment.set_value('WORKER_BOT_NAME', worker_assignment.worker_name)

  _host_state.heartbeat_thread = threading.Thread(target=_do_heartbeat)
  _host_state.heartbeat_thread.daemon = True
  _host_state.heartbeat_thread.start()
Ejemplo n.º 8
0
def _download_testcase(testcase_id, testcase, configuration):
    """Download the test case and return its path."""
    testcase_download_url = '{url}?id={id}'.format(
        url=configuration.get('testcase_download_url'), id=testcase_id)
    response, content = http_utils.request(testcase_download_url,
                                           method=http_utils.GET_METHOD,
                                           configuration=configuration)

    if response.status != 200:
        raise errors.ReproduceToolUnrecoverableError(
            'Unable to download test case.')

    # Create a temporary directory where we can store the test case.
    bot_absolute_filename = response['x-goog-meta-filename']
    testcase_directory = os.path.join(environment.get_value('ROOT_DIR'),
                                      'current-testcase')
    shell.create_directory(testcase_directory)
    environment.set_value('FUZZ_INPUTS', testcase_directory)
    testcase_path = os.path.join(testcase_directory,
                                 os.path.basename(bot_absolute_filename))

    utils.write_data_to_file(content, testcase_path)

    # Unpack the test case if it's archived.
    # TODO(mbarbella): Rewrite setup.unpack_testcase and share this code.
    if testcase.minimized_keys and testcase.minimized_keys != 'NA':
        mask = data_types.ArchiveStatus.MINIMIZED
    else:
        mask = data_types.ArchiveStatus.FUZZED

    if testcase.archive_state & mask:
        archive.unpack(testcase_path, testcase_directory)
        file_list = archive.get_file_list(testcase_path)

        testcase_path = None
        for file_name in file_list:
            if testcase.absolute_path.endswith(file_name):
                testcase_path = os.path.join(testcase_directory, file_name)
                break

        if not testcase_path:
            raise errors.ReproduceToolUnrecoverableError(
                'Test case file was not found in archive.\n'
                'Original filename: {absolute_path}.\n'
                'Archive contents: {file_list}'.format(
                    absolute_path=testcase.absolute_path, file_list=file_list))

    return testcase_path
Ejemplo n.º 9
0
    def setUp(self):
        """Set up."""
        super(MinimizeTaskTestUntrusted, self).setUp()
        environment.set_value('JOB_NAME', 'libfuzzer_asan_job')

        helpers.patch(self, [
            'datastore.data_handler.get_data_bundle_bucket_name',
        ])

        patcher = mock.patch(
            'bot.fuzzers.libFuzzer.fuzzer.LibFuzzer.fuzzer_directory',
            new_callable=mock.PropertyMock)

        mock_fuzzer_directory = patcher.start()
        self.addCleanup(patcher.stop)

        mock_fuzzer_directory.return_value = os.path.join(
            environment.get_value('ROOT_DIR'), 'src', 'python', 'bot',
            'fuzzers', 'libFuzzer')

        job = data_types.Job(
            name='libfuzzer_asan_job',
            environment_string=(
                'APP_NAME = launcher.py\n'
                'RELEASE_BUILD_BUCKET_PATH = '
                'gs://clusterfuzz-test-data/test_libfuzzer_builds/'
                'test-libfuzzer-build-([0-9]+).zip\n'
                'REVISION_VARS_URL = https://commondatastorage.googleapis.com/'
                'clusterfuzz-test-data/test_libfuzzer_builds/'
                'test-libfuzzer-build-%s.srcmap.json\n'))
        job.put()

        data_types.FuzzTarget(engine='libFuzzer',
                              binary='test_fuzzer',
                              project='test-project').put()
        data_types.FuzzTargetJob(fuzz_target_name='libFuzzer_test_fuzzer',
                                 engine='libFuzzer',
                                 job='libfuzzer_asan_job').put()

        environment.set_value('USE_MINIJAIL', True)
        data_types.Fuzzer(revision=1,
                          file_size='builtin',
                          source='builtin',
                          name='libFuzzer',
                          max_testcases=4,
                          builtin=True).put()
        self.temp_dir = tempfile.mkdtemp(
            dir=environment.get_value('FUZZ_INPUTS'))
Ejemplo n.º 10
0
  def test_minimize_testcase(self):
    """Tests running a testcase that should be able to minimize."""
    environment.set_value('FUZZ_TARGET', 'example-fuzzers/trap_fuzzer')
    environment.set_value('JOB_NAME', 'libfuzzer_asan_fuchsia')
    build_manager.setup_build()
    testcase_path, _ = setup_testcase_and_corpus('fuchsia_overlong_crash',
                                                 'empty_corpus')
    minimize_output_path = os.path.join(TEMP_DIR, 'output')

    engine_impl = engine.LibFuzzerEngine()
    result = engine_impl.minimize_testcase('example-fuzzers/trap_fuzzer',
                                           ['-runs=1000000'], testcase_path,
                                           minimize_output_path, 30)
    with open(minimize_output_path) as f:
      result = f.read()
      self.assertEqual('HI!', result)
Ejemplo n.º 11
0
    def setUp(self):
        test_helpers.patch_environ(self)

        # Set Android specific environment variables like DEVICE_TMP_DIR, etc.
        environment.set_value('OS_OVERRIDE', 'ANDROID')
        environment.set_bot_environment()

        # Clear and create temporary directory on device.
        self.device_temp_dir = adb.DEVICE_TMP_DIR
        adb.remove_directory(self.device_temp_dir, recreate=True)

        # Create local temp directory.
        self.local_temp_dir = tempfile.mkdtemp()

        # Run adb as root.
        adb.run_as_root()
Ejemplo n.º 12
0
    def setUp(self):
        super(AflRunnerTest, self).setUp()
        test_helpers.patch_environ(self)
        test_helpers.patch(self,
                           ['bot.fuzzers.engine_common.is_lpm_fuzz_target'])
        self.mock.is_lpm_fuzz_target.return_value = True
        environment.set_value('HARD_TIMEOUT_OVERRIDE', 600)
        config = launcher.AflConfig.from_target_path(self.TARGET_PATH)

        self.runner = launcher.AflRunner(self.TARGET_PATH, config,
                                         self.TESTCASE_FILE_PATH,
                                         self.INPUT_DIR)

        self.fuzz_result = new_process.ProcessResult()
        self.args = ['-iinput1', '-ooutput', '123', '456']
        self.times_called = 0
Ejemplo n.º 13
0
  def test_weighted_strategy_pool(self):
    """Tests whether a proper strategy pool is returned by the multi armed
    bandit selection implementation with medium temperature.

    Based on deterministic strategy selection. Mutator plugin is patched to
    be included in our strategy pool."""
    environment.set_value('STRATEGY_SELECTION_METHOD', 'multi_armed_bandit')
    strategy_pool = strategy_selection.generate_weighted_strategy_pool(
        strategy_list=strategy.AFL_STRATEGY_LIST,
        use_generator=True,
        engine_name='afl')
    self.assertTrue(
        strategy_pool.do_strategy(strategy.CORPUS_MUTATION_ML_RNN_STRATEGY))
    self.assertFalse(
        strategy_pool.do_strategy(strategy.CORPUS_MUTATION_RADAMSA_STRATEGY))
    self.assertTrue(strategy_pool.do_strategy(strategy.CORPUS_SUBSET_STRATEGY))
Ejemplo n.º 14
0
  def test_corpus_sync(self):
    """Test syncing corpus."""
    environment.set_value('CORPUS_BUCKET', 'clusterfuzz-test-data')
    corpus = corpus_manager.RemoteFuzzTargetCorpus('corpus_test_fuzzer',
                                                   'child_fuzzer')
    worker_root = environment.get_value('WORKER_ROOT_DIR')
    test_corpus_directory = os.path.join(worker_root, 'corpus')
    os.mkdir(test_corpus_directory)

    try:
      self.assertTrue(corpus.rsync_to_disk(test_corpus_directory))
      self.assertItemsEqual(
          os.listdir(test_corpus_directory), ['123', '456', 'abc'])
    finally:
      if os.path.exists(test_corpus_directory):
        shutil.rmtree(test_corpus_directory, ignore_errors=True)
Ejemplo n.º 15
0
    def __init__(self,
                 target_path,
                 config,
                 testcase_file_path,
                 input_directory,
                 afl_tools_path=None):
        """Inits the AflRunner.

    Args:
      target_path: Path to the fuzz target.
      config: AflConfig object.
      testcase_file_path: File to write crashes to.
      input_directory: Corpus directory passed to afl-fuzz.
      afl_tools_path: Path that is used to locate afl-* tools.
    """

        self.target_path = target_path
        self.config = config
        self.testcase_file_path = testcase_file_path
        self._input_directory = input_directory

        if afl_tools_path is None:
            afl_tools_path = os.path.dirname(target_path)

        # Set paths to afl tools.
        self.afl_fuzz_path = os.path.join(afl_tools_path, 'afl-fuzz')
        self.afl_showmap_path = os.path.join(afl_tools_path, 'afl-showmap')

        self._afl_input = None
        self._afl_output = None

        self.strategies = FuzzingStrategies()

        # Set this to None so we can tell if it has never been set or if it's just
        # empty.
        self._fuzzer_stderr = None

        self.initial_max_total_time = 0

        for env_var, value in config.additional_env_vars.iteritems():
            environment.set_value(env_var, value)

        self.showmap_output_path = os.path.join(fuzzer_utils.get_temp_dir(),
                                                self.SHOWMAP_FILENAME)
        self.merge_timeout = engine_common.get_merge_timeout(
            DEFAULT_MERGE_TIMEOUT)
        self.showmap_no_output_logged = False
Ejemplo n.º 16
0
  def setUp(self):
    """Set up."""
    super().setUp()
    environment.set_value('JOB_NAME', 'libfuzzer_asan_job')

    job = data_types.Job(
        name='libfuzzer_asan_job',
        environment_string=(
            'RELEASE_BUILD_BUCKET_PATH = '
            'gs://clusterfuzz-test-data/test_libfuzzer_builds/'
            'test-libfuzzer-build-([0-9]+).zip\n'
            'REVISION_VARS_URL = https://commondatastorage.googleapis.com/'
            'clusterfuzz-test-data/test_libfuzzer_builds/'
            'test-libfuzzer-build-%s.srcmap.json\n'))
    job.put()

    self.temp_dir = tempfile.mkdtemp(dir=environment.get_value('FUZZ_INPUTS'))
Ejemplo n.º 17
0
    def test_preemptible(self):
        """Test preemptible bot tasks."""
        environment.set_value('PREEMPTIBLE', True)
        environment.set_value('THREAD_MULTIPLIER', 1)
        tasks.add_task('test',
                       'high',
                       'job',
                       queue='high-end-jobs-linux',
                       wait_time=0)
        tasks.add_task('test',
                       'normal',
                       'job',
                       queue='jobs-linux',
                       wait_time=0)

        task = tasks.get_task()
        self.assertIsNone(task)
    def test_minimize(self):
        """Test minimize."""
        testcase_file_path = os.path.join(self.temp_dir, 'testcase')
        with open(testcase_file_path, 'wb') as f:
            f.write('EEE')

        with open(testcase_file_path) as f:
            fuzzed_keys = blobs.write_blob(f)

        testcase_path = os.path.join(self.temp_dir, 'testcase')

        testcase = data_types.Testcase(
            crash_type='Null-dereference WRITE',
            crash_address='',
            crash_state='Foo\n',
            crash_stacktrace='',
            crash_revision=1337,
            fuzzed_keys=fuzzed_keys,
            fuzzer_name='libFuzzer',
            overridden_fuzzer_name='libFuzzer_test_fuzzer',
            job_type='libfuzzer_asan_job',
            original_absolute_path=testcase_path,
            absolute_path=testcase_path,
            minimized_arguments='%TESTCASE% test_fuzzer')
        testcase.put()

        data_types.FuzzTarget(engine='libFuzzer', binary='test_fuzzer').put()

        fuzzers_init.run()

        self._setup_env(job_type='libfuzzer_asan_job')
        environment.set_value('APP_ARGS', testcase.minimized_arguments)
        environment.set_value('LIBFUZZER_MINIMIZATION_ROUNDS', 3)
        minimize_task.execute_task(testcase.key.id(), 'libfuzzer_asan_job')

        testcase = data_handler.get_testcase_by_id(testcase.key.id())
        self.assertNotEqual('', testcase.minimized_keys)
        self.assertNotEqual('NA', testcase.minimized_keys)
        self.assertNotEqual(testcase.fuzzed_keys, testcase.minimized_keys)
        self.assertEqual({'ASAN_OPTIONS': {}}, testcase.get_metadata('env'))

        blobs.read_blob_to_disk(testcase.minimized_keys, testcase_path)

        with open(testcase_path) as f:
            self.assertEqual(1, len(f.read()))
Ejemplo n.º 19
0
  def test_fuzzer_can_boot_and_run_reproducer(self):
    """Tests running a testcase that should cause a fast, predictable crash."""
    environment.set_value("FUZZ_TARGET", "example_fuzzers/overflow_fuzzer")
    environment.set_value("JOB_NAME", "libfuzzer_asan_fuchsia")
    build_manager.setup_build()
    testcase_path, _ = setup_testcase_and_corpus("fuchsia_crash",
                                                 "empty_corpus")
    engine_impl = engine.LibFuzzerEngine()
    result = engine_impl.reproduce(
        "example_fuzzers/overflow_fuzzer",
        testcase_path,
        ["-timeout=25", "-rss_limit_mb=2560"],
        30,
    )

    self.assertIn("ERROR: AddressSanitizer: heap-buffer-overflow on address",
                  result.output)
    self.assertIn("Running: data/fuchsia_crash", result.output)
Ejemplo n.º 20
0
def update_environment_for_job(environment_string):
    """Process the environment variable string included with a job."""
    # Now parse the job's environment definition.
    environment_values = (
        environment.parse_environment_definition(environment_string))

    for key, value in six.iteritems(environment_values):
        environment.set_value(key, value)

    # If we share the build with another job type, force us to be a custom binary
    # job type.
    if environment.get_value('SHARE_BUILD_WITH_JOB_TYPE'):
        environment.set_value('CUSTOM_BINARY', True)

    if environment.is_trusted_host():
        environment_values['JOB_NAME'] = environment.get_value('JOB_NAME')
        from bot.untrusted_runner import environment as worker_environment
        worker_environment.update_environment(environment_values)
Ejemplo n.º 21
0
    def test_libfuzzer_testcase_with_default_help_format(self):
        """Test the function with a libfuzzer test case, with HELP_FORMAT set in
    environment."""
        environment.set_value(
            'HELP_FORMAT',
            '-%TESTCASE%\\n-%FUZZER_NAME%\\n-%FUZZ_TARGET%\\n-%PROJECT%\\n'
            '-%REVISION%\\n')

        testcase = data_types.Testcase()
        testcase.fuzzer_name = 'simple_fuzzer'
        testcase.job_type = 'job_without_help_format'
        testcase.crash_revision = 1337
        testcase.put()

        self.assertEquals(
            data_handler.get_formatted_reproduction_help(testcase),
            '-{id}\n-simple_fuzzer\n-\n-test_project\n-1337\n'.format(
                id=testcase.key.id()))
Ejemplo n.º 22
0
def _set_random_fuzz_target_for_fuzzing_if_needed(fuzz_targets, target_weights):
  """Sets a random fuzz target for fuzzing."""
  if not environment.is_engine_fuzzer_job():
    return

  # TODO(ochang): Untie this dependency on knowledge of the current task.
  task_name = environment.get_value('TASK_NAME')
  if task_name != 'fuzz':
    return

  fuzz_targets = list(fuzz_targets)
  if not fuzz_targets:
    logs.log_error('No fuzz targets found. Unable to pick random one.')
    return

  fuzz_target = fuzzer_selection.select_fuzz_target(fuzz_targets,
                                                    target_weights)
  environment.set_value('FUZZ_TARGET', fuzz_target)
  logs.log('Picked fuzz target %s for fuzzing.' % fuzz_target)
Ejemplo n.º 23
0
    def test_blackbox_fuzzer_testcase_with_default_help_format(self):
        """Test the function with a blackbox fuzzer test case, with HELP_FORMAT
    set in environment."""
        environment.set_value(
            'HELP_FORMAT',
            '-%TESTCASE%\\n-%FUZZER_NAME%\\n-%FUZZ_TARGET%\\n-%PROJECT%\\n'
            '-%REVISION%\\n-%ENGINE%\\n-%SANITIZER%\\n%ARGS%\\n')

        testcase = data_types.Testcase()
        testcase.fuzzer_name = 'simple_fuzzer'
        testcase.job_type = 'ubsan_job_without_help_format'
        testcase.crash_revision = 1337
        testcase.put()

        self.assertEquals(
            data_handler.get_formatted_reproduction_help(testcase),
            ('-{id}\n-simple_fuzzer\n-NA\n-test_project\n-1337\n'
             '-NA\n-UBSAN\n--disable-logging --disable-experiments\n').format(
                 id=testcase.key.id()))
Ejemplo n.º 24
0
    def test_regular(self):
        """Test regular tasks."""
        environment.set_value('THREAD_MULTIPLIER', 1)
        tasks.add_task('test',
                       'high',
                       'job',
                       queue='high-end-jobs-linux',
                       wait_time=0)
        tasks.add_task('test',
                       'normal',
                       'job',
                       queue='jobs-linux',
                       wait_time=0)

        task = tasks.get_task()
        self.assertEqual('test', task.command)
        self.assertEqual('normal', task.argument)
        self.assertEqual('job', task.job)
        self.assertEqual('test normal job', task.payload())
Ejemplo n.º 25
0
def setup_additional_args_for_app():
    """Select additional args for the specified app at random."""
    # Convert the app_name to lowercase. Case may vary by platform.
    app_name = environment.get_value('APP_NAME', '').lower()

    # Hack: strip file extensions that may be appended on various platforms.
    extensions_to_strip = ['.exe', '.apk']
    for extension in extensions_to_strip:
        app_name = utils.strip_from_right(app_name, extension)

    trials = data_types.Trial.query(data_types.Trial.app_name == app_name)
    trial = select_trial(trials)
    if not trial or not trial.app_args:
        return

    current_app_args = environment.get_value('APP_ARGS', '').rstrip()
    environment.set_value('APP_ARGS',
                          '%s %s' % (current_app_args, trial.app_args))

    environment.set_value('TRIAL_APP_ARGS', trial.app_args)
    def setUp(self):
        """Put data in the local ndb table the tests to query from."""
        test_helpers.patch_environ(self)
        test_helpers.patch(
            self, ['bot.fuzzers.engine_common.decide_with_probability'])
        self.mock.decide_with_probability.return_value = True

        data = []

        strategy1 = data_types.FuzzStrategyProbability()
        strategy1.strategy_name = 'corpus_mutations_ml_rnn,corpus_subset,'
        strategy1.probability = 1
        strategy1.engine = 'afl'
        data.append(strategy1)
        ndb.put_multi(data)

        distribution = fuzz_task.get_strategy_distribution_from_ndb()

        environment.set_value('USE_BANDIT_STRATEGY_SELECTION', True)
        environment.set_value('STRATEGY_SELECTION_DISTRIBUTION', distribution)
Ejemplo n.º 27
0
def _setup_x():
    """Start Xvfb and blackbox before running the test application."""
    if environment.platform() != 'LINUX':
        return []

    environment.set_value('DISPLAY', DISPLAY)

    print('Starting Xvfb...')
    xvfb_runner = new_process.ProcessRunner('/usr/bin/Xvfb')
    xvfb_process = xvfb_runner.run(additional_args=[
        DISPLAY, '-screen', '0', '1280x1024x24', '-ac', '-nolisten', 'tcp'
    ])
    time.sleep(5)  # Allow some time for Xvfb to start.

    print('Starting blackbox...')
    blackbox_runner = new_process.ProcessRunner('/usr/bin/blackbox')
    blackbox_process = blackbox_runner.run()
    time.sleep(5)  # Allow some time for blackbox to start.

    # Return all handles we create so they can be terminated properly at exit.
    return [xvfb_process, blackbox_process]
Ejemplo n.º 28
0
  def test_weighted_strategy_pool(self):
    """Tests whether a proper strategy pool is returned by the multi armed
    bandit selection implementation with medium temperature.

    Based on deterministic strategy selection. Mutator plugin is patched to
    be included in our strategy pool."""
    environment.set_value('STRATEGY_SELECTION_METHOD', 'multi_armed_bandit')
    strategy_pool = strategy_selection.generate_weighted_strategy_pool(
        strategy_list=strategy.LIBFUZZER_STRATEGY_LIST,
        use_generator=True,
        engine_name='libFuzzer')
    self.assertTrue(
        strategy_pool.do_strategy(strategy.CORPUS_MUTATION_ML_RNN_STRATEGY))
    self.assertTrue(
        strategy_pool.do_strategy(strategy.RANDOM_MAX_LENGTH_STRATEGY))
    self.assertTrue(strategy_pool.do_strategy(strategy.VALUE_PROFILE_STRATEGY))
    self.assertTrue(
        strategy_pool.do_strategy(strategy.RECOMMENDED_DICTIONARY_STRATEGY))
    self.assertFalse(
        strategy_pool.do_strategy(strategy.CORPUS_MUTATION_RADAMSA_STRATEGY))
    self.assertFalse(strategy_pool.do_strategy(strategy.FORK_STRATEGY))
Ejemplo n.º 29
0
  def test_setup_regular_build_fuzz_target(self):
    """Test setting up a regular build."""
    environment.set_value('TASK_NAME', 'fuzz')
    environment.set_value('TASK_ARGUMENT', 'libFuzzer')

    launcher_dir = os.path.join('src', 'python', 'bot', 'fuzzers', 'libFuzzer')
    environment.set_value('FUZZER_DIR',
                          os.path.join(os.environ['ROOT_DIR'], launcher_dir))

    self._setup_env(job_type='libfuzzer_asan_job')
    build = build_manager.setup_build(target_weights={})
    self.assertIsNotNone(build)

    worker_root_dir = os.environ['WORKER_ROOT_DIR']
    expected_build_dir = os.path.join(
        worker_root_dir, 'bot', 'builds',
        'clusterfuzz-test-data_test_libfuzzer_builds_'
        '41a87efdd470c6f00e8babf61548bf6c7de57137', 'revisions')

    expected_app_dir = os.path.join(worker_root_dir, launcher_dir)

    self.assertEqual(
        os.path.join(expected_app_dir, 'launcher.py'), os.environ['APP_PATH'])
    self.assertEqual('1337', os.environ['APP_REVISION'])
    self.assertEqual('', os.environ['APP_PATH_DEBUG'])
    self.assertEqual(expected_build_dir, os.environ['BUILD_DIR'])
    self.assertEqual(expected_app_dir, os.environ['APP_DIR'])
    self.assertEqual('test_fuzzer', os.environ['FUZZ_TARGET'])
Ejemplo n.º 30
0
    def setUp(self):
        helpers.patch_environ(self)
        test_utils.set_up_pyfakefs(self)
        self.fs.create_dir("/input")
        self.fs.create_dir("/output")

        helpers.patch(
            self,
            [
                "bot.fuzzers.libFuzzer.fuzzer.LibFuzzer.run",
                "metrics.fuzzer_logs.get_bucket",
                "google_cloud_utils.blobs.write_blob",
            ],
        )

        self.fuzzer = data_types.Fuzzer(
            revision=1,
            file_size="builtin",
            source="builtin",
            name="libFuzzer",
            max_testcases=4,
            builtin=True,
        )
        self.fuzzer.put()

        self.fuzzer_directory = os.path.join(
            environment.get_value("ROOT_DIR"),
            "src",
            "python",
            "bot",
            "fuzzers",
            "libFuzzer",
        )

        # Needed since local config is not available with fakefs.
        self.mock.get_bucket.return_value = None
        self.mock.write_blob.return_value = "sample"

        environment.set_value("JOB_NAME", "job")
        environment.set_value("INPUT_DIR", "/input")