Ejemplo n.º 1
0
def main():
    """Finds the commit SHA where an error was initally introduced."""
    logging.getLogger().setLevel(logging.INFO)
    utils.chdir_to_root()
    parser = argparse.ArgumentParser(
        description='git bisection for finding introduction of bugs')

    parser.add_argument('--project_name',
                        help='The name of the project where the bug occurred.',
                        required=True)
    parser.add_argument('--new_commit',
                        help='The newest commit SHA to be bisected.',
                        required=True)
    parser.add_argument('--old_commit',
                        help='The oldest commit SHA to be bisected.',
                        required=True)
    parser.add_argument('--fuzz_target',
                        help='The name of the fuzzer to be built.',
                        required=True)
    parser.add_argument('--test_case_path',
                        help='The path to test case.',
                        required=True)
    parser.add_argument('--engine',
                        help='The default is "libfuzzer".',
                        default='libfuzzer')
    parser.add_argument('--sanitizer',
                        default='address',
                        help='The default is "address".')
    parser.add_argument('--type',
                        choices=['regressed', 'fixed'],
                        help='The bisection type.',
                        required=True)
    parser.add_argument('--architecture', default='x86_64')
    args = parser.parse_args()

    build_data = build_specified_commit.BuildData(
        project_name=args.project_name,
        engine=args.engine,
        sanitizer=args.sanitizer,
        architecture=args.architecture)

    result = bisect(args.type, args.old_commit, args.new_commit,
                    args.test_case_path, args.fuzz_target, build_data)
    if not result.commit:
        logging.error('No error was found in commit range %s:%s',
                      args.old_commit, args.new_commit)
        return 1
    if result.commit == args.old_commit:
        logging.error(
            'Bisection Error: Both the first and the last commits in'
            'the given range have the same behavior, bisection is not possible. '
        )
        return 1
    if args.type == 'regressed':
        print('Error was introduced at commit %s' % result.commit)
    elif args.type == 'fixed':
        print('Error was fixed at commit %s' % result.commit)
    return 0
Ejemplo n.º 2
0
 def test_invalid_filepath(self):
     """Tests what get_fuzz_targets return when invalid filepath is used."""
     utils.chdir_to_root()
     helper.build_fuzzers_impl(EXAMPLE_PROJECT,
                               True,
                               'libfuzzer',
                               'address',
                               'x86_64', [],
                               None,
                               no_cache=False,
                               mount_location=None)
     fuzz_targets = utils.get_fuzz_targets('not/a/valid/file/path')
     self.assertFalse(fuzz_targets)
Ejemplo n.º 3
0
def main():
    """Finds the commit SHA where an error was initally introduced."""
    utils.chdir_to_root()
    parser = argparse.ArgumentParser(
        description='git bisection for finding introduction of bugs')

    parser.add_argument('--project_name',
                        help='The name of the project where the bug occurred.',
                        required=True)
    parser.add_argument('--commit_new',
                        help='The newest commit SHA to be bisected.',
                        required=True)
    parser.add_argument('--commit_old',
                        help='The oldest commit SHA to be bisected.',
                        required=True)
    parser.add_argument('--fuzz_target',
                        help='The name of the fuzzer to be built.',
                        required=True)
    parser.add_argument('--testcase',
                        help='The path to test case.',
                        required=True)
    parser.add_argument('--engine',
                        help='The default is "libfuzzer".',
                        default='libfuzzer')
    parser.add_argument('--sanitizer',
                        default='address',
                        help='The default is "address".')
    parser.add_argument('--architecture', default='x86_64')
    args = parser.parse_args()

    build_data = build_specified_commit.BuildData(
        project_name=args.project_name,
        engine=args.engine,
        sanitizer=args.sanitizer,
        architecture=args.architecture)

    error_sha = bisect(args.commit_old, args.commit_new, args.testcase,
                       args.fuzz_target, build_data)
    if not error_sha:
        logging.error('No error was found in commit range %s:%s',
                      args.commit_old, args.commit_new)
        return 1
    if error_sha == args.commit_old:
        logging.error(
            'Bisection Error: Both the first and the last commits in'
            'the given range have the same behavior, bisection is not possible. '
        )
        return 1
    print('Error was introduced at commit %s' % error_sha)
    return 0
Ejemplo n.º 4
0
def detect_main_repo(project_name, repo_name=None, commit=None):
    """Checks a docker image for the main repo of an OSS-Fuzz project.

  Note: The default is to use the repo name to detect the main repo.

  Args:
    project_name: The name of the oss-fuzz project.
    repo_name: The name of the main repo in an OSS-Fuzz project.
    commit: A commit SHA that is associated with the main repo.
    src_dir: The location of the projects source on the docker image.

  Returns:
    The repo's origin, the repo's path.
  """

    if not repo_name and not commit:
        logging.error(
            'Error: can not detect main repo without a repo_name or a commit.')
        return None, None
    if repo_name and commit:
        logging.info(
            'Both repo name and commit specific. Using repo name for detection.'
        )

    # Change to oss-fuzz main directory so helper.py runs correctly.
    utils.chdir_to_root()
    if not helper.build_image_impl(project_name):
        logging.error('Error: building %s image failed.', project_name)
        return None, None
    docker_image_name = 'gcr.io/oss-fuzz/' + project_name
    command_to_run = [
        'docker', 'run', '--rm', '-t', docker_image_name, 'python3',
        os.path.join('/opt', 'cifuzz', 'detect_repo.py')
    ]
    if repo_name:
        command_to_run.extend(['--repo_name', repo_name])
    else:
        command_to_run.extend(['--example_commit', commit])
    out, _, _ = utils.execute(command_to_run)
    match = re.search(r'\bDetected repo: ([^ ]+) ([^ ]+)', out.rstrip())
    if match and match.group(1) and match.group(2):
        return match.group(1), match.group(2)

    logging.error('Failed to detect repo:\n%s', out)
    return None, None
Ejemplo n.º 5
0
 def test_valid_filepath(self):
     """Checks is_fuzz_target_local function with a valid filepath."""
     utils.chdir_to_root()
     helper.build_fuzzers_impl(EXAMPLE_PROJECT,
                               True,
                               'libfuzzer',
                               'address',
                               'x86_64', [],
                               None,
                               no_cache=False,
                               mount_location=None)
     is_local = utils.is_fuzz_target_local(
         os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT,
                      'do_stuff_fuzzer'))
     self.assertTrue(is_local)
     is_local = utils.is_fuzz_target_local(
         os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT,
                      'do_stuff_fuzzer.dict'))
     self.assertFalse(is_local)
Ejemplo n.º 6
0
 def test_valid_filepath(self):
     """Tests that fuzz targets can be retrieved once the fuzzers are built."""
     utils.chdir_to_root()
     helper.build_fuzzers_impl(EXAMPLE_PROJECT,
                               True,
                               'libfuzzer',
                               'address',
                               'x86_64', [],
                               None,
                               no_cache=False,
                               mount_location=None)
     fuzz_targets = utils.get_fuzz_targets(
         os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT))
     self.assertCountEqual(fuzz_targets, [
         os.path.join(helper.OSSFUZZ_DIR, 'build', 'out', EXAMPLE_PROJECT,
                      'do_stuff_fuzzer')
     ])
     fuzz_targets = utils.get_fuzz_targets(
         os.path.join(helper.OSSFUZZ_DIR, 'infra'))
     self.assertFalse(fuzz_targets)