Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra: enable building projects using cached images #12597

Merged
merged 6 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions infra/build/functions/build_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
LOCAL_BUILD_LOG_PATH = '/workspace/build.log'
BUILD_SUCCESS_MARKER = '/workspace/build.succeeded'

_CACHED_IMAGE = ('us-central1-docker.pkg.dev/oss-fuzz/oss-fuzz-gen/'
'{name}-ofg-cached-{sanitizer}')
_CACHED_SANITIZERS = ('address', 'coverage')


@dataclass
class Config:
Expand Down Expand Up @@ -163,6 +167,8 @@ def __init__(self, name, project_yaml, dockerfile):
else:
self.main_repo = ''

self.cached_sanitizer = None

@property
def sanitizers(self):
"""Returns processed sanitizers."""
Expand All @@ -172,8 +178,14 @@ def sanitizers(self):
@property
def image(self):
"""Returns the docker image for the project."""
if self.cached_sanitizer:
return self.cached_image(self.cached_sanitizer)

return f'gcr.io/{build_lib.IMAGE_PROJECT}/{self.name}'

def cached_image(self, sanitizer):
return _CACHED_IMAGE.format(name=self.name, sanitizer=sanitizer)


def get_last_step_id(steps):
"""Returns the id of the last step in |steps|."""
Expand Down Expand Up @@ -313,30 +325,37 @@ def get_build_steps( # pylint: disable=too-many-locals, too-many-statements, to
project_yaml,
dockerfile,
config,
additional_env=None):
additional_env=None,
use_caching=False):
"""Returns build steps for project."""

project = Project(project_name, project_yaml, dockerfile)

if project.disabled:
logging.info('Project "%s" is disabled.', project.name)
return []

timestamp = get_datetime_now().strftime('%Y%m%d%H%M')
build_steps = build_lib.get_project_image_steps(
project.name,
project.image,
project.fuzzing_language,
config=config,
architectures=project.architectures,
experiment=config.experiment)
if use_caching:
# Use cached built image.
build_steps = []
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here we make the decision that we won't be building anything in the caching run of a OFG run -- we'll rely on builds done asynchronously (as we went over here: #12675 (comment))

Just double confirming that this is the intention here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes -- we will rely on our existing infra to build chronos images daily.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oliverchang this does not work out of the box with ccache builds. The problem here is that the existing build step is used to copy in the copied harness from OFG.

We still need to build the docker image of a given project, however, the Dockerfiles will now look something like:

FROM us-central1-docker.pkg.dev/oss-fuzz/oss-fuzz-gen/PROJECT-ofg-cached-address
COPY 01.c /target/fuzz/path.c

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at least

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure about this. In google/oss-fuzz-gen#692 some harnesses are not overwritten correctly, whereas others are. I'm trying to debug what's going on there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I think I know why some are succeeding: those that does not have a cached image worked correctly. The other ones did not.

Duckdb works.

However:

$ docker manifest inspect us-central1-docker.pkg.dev/oss-fuzz/oss-fuzz-gen/duckdb-ofg-cached-address
no such manifest: us-central1-docker.pkg.dev/oss-fuzz/oss-fuzz-gen/duckdb-ofg-cached-address:latest

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ntpsec however does not work, but it has a cached image. See for example this harness: https://llm-exp.oss-fuzz.com/Result-reports/ofg-pr/2024-11-06-692-oc-20241106-large-generated-20241106/benchmark/output-ntpsec-_src_ntpsec_tests_libntp_decodenetnum_c/index.html

The generated harnesses does not correlate with what is in the coverage report. This is because we're missing copying the harness in for ccached containers.

else:
build_steps = build_lib.get_project_image_steps(
project.name,
project.image,
project.fuzzing_language,
config=config,
architectures=project.architectures,
experiment=config.experiment)

# Sort engines to make AFL first to test if libFuzzer has an advantage in
# finding bugs first since it is generally built first.
for fuzzing_engine in sorted(project.fuzzing_engines):
# Sort sanitizers and architectures so order is determinisitic (good for
# tests).
for sanitizer in sorted(project.sanitizers):
if use_caching and sanitizer in _CACHED_SANITIZERS:
project.cached_sanitizer = sanitizer

# Build x86_64 before i386.
for architecture in reversed(sorted(project.architectures)):
build = Build(fuzzing_engine, sanitizer, architecture)
Expand Down
14 changes: 11 additions & 3 deletions infra/build/functions/target_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

def run_experiment(project_name, target_name, args, output_path, errlog_path,
build_output_path, upload_corpus_path, upload_coverage_path,
experiment_name, upload_reproducer_path, tags):
experiment_name, upload_reproducer_path, tags,
use_cached_image):
config = build_project.Config(testing=True,
test_image_suffix='',
repo=build_project.DEFAULT_OSS_FUZZ_REPO,
Expand Down Expand Up @@ -68,7 +69,8 @@ def run_experiment(project_name, target_name, args, output_path, errlog_path,
project_yaml,
dockerfile_contents,
config,
additional_env=jcc_env)
additional_env=jcc_env,
use_caching=use_cached_image)

build = build_project.Build('libfuzzer', 'address', 'x86_64')
local_output_path = '/workspace/output.log'
Expand Down Expand Up @@ -209,6 +211,9 @@ def run_experiment(project_name, target_name, args, output_path, errlog_path,
env = build_project.get_env(project_yaml['language'], build)
env.extend(jcc_env)

if use_cached_image:
project.cached_sanitizer = 'coverage'

steps.append(
build_project.get_compile_step(project, build, env, config.parallel))

Expand Down Expand Up @@ -330,12 +335,15 @@ def main():
nargs='*',
help='Tags for cloud build.',
default=[])
parser.add_argument('--use_cached_image',
action='store_true',
help='Use cached images post build.')
args = parser.parse_args()

run_experiment(args.project, args.target, args.args, args.upload_output_log,
args.upload_err_log, args.upload_build_log, args.upload_corpus,
args.upload_coverage, args.experiment_name,
args.upload_reproducer, args.tags)
args.upload_reproducer, args.tags, args.use_cached_image)


if __name__ == '__main__':
Expand Down
Loading