Skip to content

Commit 2a8c8d3

Browse files
OSS-Fuzz Teamcopybara-github
authored andcommitted
clang_wrapper: Handle multiple entries in a single CDB fragment.
PiperOrigin-RevId: 818383644
1 parent 8707a16 commit 2a8c8d3

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

infra/base-images/base-builder/indexer/clang_wrapper.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
_INDEXER_THREADS_PER_MERGE_QUEUE = 16
6969
_INDEXER_PER_THREAD_MEMORY = 2 * 1024**3 # 2 GiB
7070

71+
_CDB_FRAGMENT_DELIMITER = ",\n"
72+
7173
SRC = Path(os.getenv("SRC", "/src"))
7274
# On OSS-Fuzz build infra, $OUT is not /out.
7375
OUT = Path(os.getenv("OUT", "/out"))
@@ -177,13 +179,13 @@ def files_by_creation_time(folder_path: Path) -> Sequence[Path]:
177179
return files
178180

179181

180-
def _wait_for_cdb_fragment(file: Path) -> str | None:
182+
def _wait_for_cdb_fragment(file: Path) -> Sequence[str]:
181183
"""Returns the CDB fragment from the given file, waiting if needed."""
182184
num_retries = 3
183185
for i in range(1 + num_retries):
184186
data = file.read_text()
185-
if data.endswith(",\n"):
186-
return data.rstrip().rstrip(",")
187+
if data.endswith(_CDB_FRAGMENT_DELIMITER):
188+
return data.split(_CDB_FRAGMENT_DELIMITER)[:-1]
187189

188190
if i < num_retries:
189191
print(
@@ -202,7 +204,7 @@ def _wait_for_cdb_fragment(file: Path) -> str | None:
202204
else:
203205
raise RuntimeError(error)
204206

205-
return None
207+
return ()
206208

207209

208210
def read_cdb_fragments(cdb_path: Path) -> Any:
@@ -216,11 +218,10 @@ def read_cdb_fragments(cdb_path: Path) -> Any:
216218
if not file.name.endswith(".json"):
217219
continue
218220

219-
fragment = _wait_for_cdb_fragment(file)
220-
if fragment:
221-
contents.append(fragment)
221+
fragments = _wait_for_cdb_fragment(file)
222+
contents.extend(fragments)
222223

223-
contents = ",\n".join(contents)
224+
contents = _CDB_FRAGMENT_DELIMITER.join(contents)
224225
contents = "[" + contents + "]"
225226
return json.loads(contents)
226227

@@ -514,15 +515,13 @@ def load_cdbs(directory: Path) -> Iterator[tuple[Path, dict[str, Any]]]:
514515
if file.name.endswith("_linker_commands.json"):
515516
continue
516517

517-
fragment_data = _wait_for_cdb_fragment(file)
518-
if not fragment_data:
519-
continue
520-
521-
fragment = json.loads(fragment_data)
522-
if "output" not in fragment:
523-
continue
518+
fragments_data = _wait_for_cdb_fragment(file)
519+
for fragment_data in fragments_data:
520+
fragment = json.loads(fragment_data)
521+
if "output" not in fragment:
522+
continue
524523

525-
yield file, fragment
524+
yield file, fragment
526525

527526
# We could be running multiple linking steps in parallel, so serialize merges.
528527
with _file_lock(merged_cdb_path / ".lock"):

infra/base-images/base-builder/indexer/clang_wrapper_test.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,26 @@ def test_merge_incremental_cdb(self):
9090
}
9191

9292
new_cdb_fragments = {
93-
"test.c.aaa.json": {
93+
"test.c.aaa.json": [{
9494
"directory": "/build/subdir",
9595
"file": "test.c",
9696
"output": "test.o",
9797
"arguments": ["-c", "test.c"],
98-
},
98+
}],
99+
"bar.c.bbb.json": [
100+
{
101+
"directory": "/build/subdir",
102+
"file": "bar.c",
103+
"output": "bar.o",
104+
"arguments": ["-c", "bar.c"],
105+
},
106+
{
107+
"directory": "/build/subdir",
108+
"file": "bar2.c",
109+
"output": "bar2.o",
110+
"arguments": ["-c", "bar2.c"],
111+
},
112+
],
99113
}
100114

101115
for cdb_fragment_path, cdb_fragment in old_cdb_fragments.items():
@@ -110,7 +124,7 @@ def test_merge_incremental_cdb(self):
110124

111125
for cdb_fragment_path, cdb_fragment in new_cdb_fragments.items():
112126
(cdb_path / cdb_fragment_path).write_text(
113-
json.dumps(cdb_fragment) + ",\n"
127+
",\n".join([json.dumps(frag) for frag in cdb_fragment]) + ",\n"
114128
)
115129

116130
(cdb_path / "not_a_json").write_text("not a json")
@@ -125,6 +139,7 @@ def test_merge_incremental_cdb(self):
125139
pathlib.Path(merged_cdb_path) / "test.c.aaa.json",
126140
pathlib.Path(merged_cdb_path) / "foo.c.455.json",
127141
pathlib.Path(merged_cdb_path) / "foo.123_linker_commands.json",
142+
pathlib.Path(merged_cdb_path) / "bar.c.bbb.json",
128143
],
129144
)
130145

0 commit comments

Comments
 (0)