Skip to content

Commit 20a387d

Browse files
authored
indexer: Push image saved from built container (#13441)
1 parent db2ccf3 commit 20a387d

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

infra/base-images/base-builder/compile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ else
298298
BUILD_CMD="bash -eux $SRC/build.sh $@"
299299
fi
300300

301+
# If there are cdb (compilation database) fragments saved, restore them to $OUT
302+
# before building.
303+
if [ -d /cdb ]; then
304+
cp -rT /cdb $OUT/cdb
305+
fi
306+
301307
# Set +u temporarily to continue even if GOPATH and OSSFUZZ_RUSTPATH are undefined.
302308
set +u
303309
# We need to preserve source code files for generating a code coverage report.

infra/base-images/base-builder/replay_build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ if [ ! -f /usr/bin/bash.real ]; then
1919
# Only run this once.
2020
python /usr/local/bin/make_build_replayable.py
2121
fi
22-
. $SRC/build.sh "$@"
22+
23+
. $SRC/build.sh "$@"

infra/build/functions/build_lib.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ def get_targets_list_url(bucket, project, sanitizer):
139139
return url
140140

141141

142-
def dockerify_run_step(step, build, use_architecture_image_name=False):
142+
def dockerify_run_step(step,
143+
build,
144+
use_architecture_image_name=False,
145+
container_name=None):
143146
"""Modify a docker run step to run using gcr.io/cloud-builders/docker. This
144147
allows us to specify which architecture to run the image on."""
145148
image = step['name']
@@ -154,6 +157,10 @@ def dockerify_run_step(step, build, use_architecture_image_name=False):
154157
'run', '--platform', platform, '-v', '/workspace:/workspace',
155158
'--privileged', '--cap-add=all'
156159
]
160+
161+
if container_name:
162+
new_args.extend(['--name', container_name])
163+
157164
for env_var in step.get('env', {}):
158165
new_args.extend(['-e', env_var])
159166
new_args += ['-t', image]

infra/build/functions/build_project.py

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
_CACHED_IMAGE = ('us-central1-docker.pkg.dev/oss-fuzz/oss-fuzz-gen/'
6666
'{name}-ofg-cached-{sanitizer}')
6767
_CACHED_SANITIZERS = ('address', 'coverage')
68+
_INDEXED_CONTAINER_NAME = 'indexed-container'
6869

6970

7071
@dataclass
@@ -503,6 +504,12 @@ def get_build_steps_for_project(project,
503504
return build_steps
504505

505506

507+
def _indexer_built_image_name(name: str):
508+
# TODO(ochang): Write this to a tar (via docker image save) and upload this to
509+
# GCS.
510+
return f'us-docker.pkg.dev/oss-fuzz/indexer/{name}'
511+
512+
506513
def get_indexer_build_steps(project_name,
507514
project_yaml,
508515
dockerfile,
@@ -533,6 +540,7 @@ def get_indexer_build_steps(project_name,
533540
build = Build('none', 'address', 'x86_64')
534541
env = get_env(project.fuzzing_language, build, project.name)
535542
env.append('INDEXER_BUILD=1')
543+
env.append('CAPTURE_REPLAY_SCRIPT=1')
536544

537545
prefix = f'indexer_indexes/{project.name}/{timestamp}/'
538546
signed_policy_document = build_lib.get_signed_policy_document_upload_prefix(
@@ -543,14 +551,60 @@ def get_indexer_build_steps(project_name,
543551
index_step = {
544552
'name': project.image,
545553
'args': [
546-
'bash', '-c',
547-
f'cd /src && cd {project.workdir} && mkdir -p {build.out} && /opt/indexer/index_build.py'
554+
'bash',
555+
'-c',
556+
f'cd /src && cd {project.workdir} && mkdir -p {build.out} && '
557+
'/opt/indexer/index_build.py && '
558+
# Enable re-building both the project and the indexes.
559+
'cp -n /usr/local/bin/replay_build.sh $$SRC/ && '
560+
# Save the CDB fragments so we can re-use them for rebuilding indexes.
561+
'cp -r $$OUT/cdb /cdb && '
562+
# Link /out to the actual $OUT and actually create it in the
563+
# container's filesystem since it's a mount.
564+
'rm -rf /out && ln -s $$OUT /out && '
565+
'umount /workspace && mkdir -p $$OUT'
548566
],
549567
'env': env,
550568
}
551569
build_lib.dockerify_run_step(index_step,
552570
build,
553-
use_architecture_image_name=build.is_arm)
571+
use_architecture_image_name=build.is_arm,
572+
container_name=_INDEXED_CONTAINER_NAME)
573+
push_image_steps = [
574+
{
575+
'name':
576+
build_lib.DOCKER_TOOL_IMAGE,
577+
'args': [
578+
'container',
579+
'commit',
580+
'-c',
581+
'ENV REPLAY_ENABLED 1',
582+
# Add CFLAGS that enable debugging (this should match the
583+
# index_build.py CFLAGS)
584+
'-c',
585+
'ENV CFLAGS "$$CFLAGS -O0 -glldb"',
586+
_INDEXED_CONTAINER_NAME,
587+
_indexer_built_image_name(project.name) + f':{timestamp}'
588+
],
589+
},
590+
{
591+
'name':
592+
build_lib.DOCKER_TOOL_IMAGE,
593+
'args': [
594+
'tag',
595+
_indexer_built_image_name(project.name) + f':{timestamp}',
596+
_indexer_built_image_name(project.name)
597+
],
598+
},
599+
{
600+
'name':
601+
build_lib.DOCKER_TOOL_IMAGE,
602+
'args': [
603+
'push', '--all-tags',
604+
_indexer_built_image_name(project.name)
605+
],
606+
},
607+
]
554608

555609
# TODO: Don't upload anything if we're in trial build.
556610
build_steps.extend([
@@ -565,12 +619,13 @@ def get_indexer_build_steps(project_name,
565619
f'https://{signed_policy_document.bucket}.storage.googleapis.com;'
566620
' done'
567621
],
568-
'entrypoint': 'bash'
622+
'entrypoint': 'bash',
623+
'allowFailure': True,
569624
},
570625
build_lib.upload_using_signed_policy_document('/workspace/srcmap.json',
571626
f'{prefix}srcmap.json',
572627
signed_policy_document),
573-
])
628+
] + push_image_steps)
574629
return build_steps
575630

576631

0 commit comments

Comments
 (0)