mbx-cache-cc 0.11.2

mbx internals: conservative C and C++ action analysis and key construction. No API stability -- use the mbx CLI or mbx-cache-protocol.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use super::*;
use crate::manifest_snapshot;
use mbx_cache_core::NoFileDigestCache;
use std::collections::BTreeSet;

#[test]
fn parses_gnu_depfiles_with_continuations_escaped_spaces_and_dollar_signs() {
    let depfile = "entropy.o: lib/entropy.c \\\n  lib/entropy.h \\\n  /usr/include/stdio.h\n";
    let parsed = CcDepfile::parse(depfile).expect("depfile should parse");
    assert_eq!(
        parsed.files,
        [
            PathBuf::from("lib/entropy.c"),
            PathBuf::from("lib/entropy.h"),
            PathBuf::from("/usr/include/stdio.h"),
        ]
    );

    let escaped = "a.o: some\\ dir/a.c price$$.h\n";
    let parsed = CcDepfile::parse(escaped).expect("escapes should parse");
    assert_eq!(
        parsed.files,
        [PathBuf::from("some dir/a.c"), PathBuf::from("price$.h")]
    );
}

#[test]
fn only_the_first_rule_contributes_prerequisites() {
    let depfile = "a.o: a.c a.h\n\na.h:\n";
    let parsed = CcDepfile::parse(depfile).expect("depfile should parse");
    assert_eq!(parsed.files, [PathBuf::from("a.c"), PathBuf::from("a.h")]);
}

#[test]
fn malformed_depfiles_bypass_caching() {
    for contents in [
        "no rule here\n",
        "a.o: a.c\\",
        "a.o: we\\?ird.c\n",
        "a.o: $(VAR).c\n",
    ] {
        assert_eq!(
            CcDepfile::parse(contents).unwrap_err().kind(),
            "malformed-depfile",
            "{contents:?} should bypass"
        );
    }
}

fn write(directory: &Path, name: &str, contents: &str) -> PathBuf {
    let path = directory.join(name);
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).expect("create parent");
    }
    std::fs::write(&path, contents).expect("write file");
    path
}

#[test]
fn recursive_manifests_drop_directories_an_ancestor_already_covers() {
    let workspace = tempfile::tempdir().expect("tempdir");
    let root = workspace.path().join("include");
    let generated = workspace.path().join("generated");
    std::fs::create_dir_all(root.join("crypto/fipsmodule")).expect("nested include directory");
    std::fs::create_dir_all(&generated).expect("separate include directory");
    let directories = BTreeSet::from([
        root.join("crypto/fipsmodule"),
        root.clone(),
        root.join("crypto"),
        generated.clone(),
    ]);

    assert_eq!(minimal_manifest_directories(directories), [generated, root]);
}

#[test]
fn a_parent_path_does_not_cover_a_directory_that_escapes_through_dot_dot() {
    let workspace = tempfile::tempdir().expect("tempdir");
    let include = workspace.path().join("include");
    let sibling = workspace.path().join("sibling");
    std::fs::create_dir_all(&include).expect("include directory");
    std::fs::create_dir_all(&sibling).expect("sibling directory");
    let escaped = include.join("../sibling");

    assert_eq!(
        minimal_manifest_directories(BTreeSet::from([include.clone(), escaped.clone()])),
        [include, escaped]
    );
}

#[cfg(unix)]
#[test]
fn a_parent_manifest_does_not_claim_a_directory_reached_through_a_symlink() {
    use std::os::unix::fs::symlink;

    let workspace = tempfile::tempdir().expect("tempdir");
    let include = workspace.path().join("include");
    let generated = workspace.path().join("generated");
    std::fs::create_dir_all(&include).expect("include directory");
    std::fs::create_dir_all(generated.join("nested")).expect("generated directory");
    symlink(&generated, include.join("linked")).expect("directory symlink");
    let linked = include.join("linked/nested");

    assert_eq!(
        minimal_manifest_directories(BTreeSet::from([include.clone(), linked.clone()])),
        [include, linked]
    );
}

#[test]
fn timestamp_macro_tokens_in_any_read_file_bypass() {
    let directory = tempfile::tempdir().expect("tempdir");
    let root = directory.path();
    let source = write(root, "a.c", "int main(void) { return 0; }\n");
    let header = write(root, "a.h", "/* built on __DATE__ */\n");

    let clean = CcDiscoveredInputs::collect(
        root,
        BTreeSet::from([source.clone()]),
        BTreeSet::new(),
        &NoFileDigestCache,
    )
    .expect("clean inputs should be discovered");
    assert_eq!(clean.inputs.len(), 1);

    let reason = CcDiscoveredInputs::collect(
        root,
        BTreeSet::from([source, header]),
        BTreeSet::new(),
        &NoFileDigestCache,
    )
    .unwrap_err();
    assert_eq!(reason.kind(), "embedded-timestamp-macro");
}

#[test]
fn file_macro_alone_does_not_bypass() {
    let directory = tempfile::tempdir().expect("tempdir");
    let root = directory.path();
    let source = write(root, "a.c", "#define A() assert(__FILE__ != 0)\n");
    let discovered = CcDiscoveredInputs::collect(
        root,
        BTreeSet::from([source]),
        BTreeSet::new(),
        &NoFileDigestCache,
    )
    .expect("__FILE__ is not a timestamp macro");
    assert_eq!(discovered.inputs.len(), 1);
}

#[test]
fn a_timestamp_macro_split_across_a_read_boundary_is_still_found() {
    let directory = tempfile::tempdir().expect("tempdir");
    let root = directory.path();
    let mut contents = "a".repeat(SCAN_CHUNK_BYTES - 4);
    contents.push_str("__TIMESTAMP__");
    let source = write(root, "a.c", &contents);
    let reason = CcDiscoveredInputs::collect(
        root,
        BTreeSet::from([source]),
        BTreeSet::new(),
        &NoFileDigestCache,
    )
    .unwrap_err();
    assert_eq!(reason.kind(), "embedded-timestamp-macro");
}

#[test]
fn include_directory_manifests_change_the_key_when_a_shadowing_header_appears() {
    let directory = tempfile::tempdir().expect("tempdir");
    let root = directory.path();
    let source = write(root, "src/a.c", "int a(void) { return 0; }\n");
    let include = root.join("include");
    std::fs::create_dir_all(&include).expect("create include dir");
    write(&include, "used.h", "int a(void);\n");

    let before = CcDiscoveredInputs::collect(
        root,
        BTreeSet::from([source.clone()]),
        BTreeSet::from([include.clone()]),
        &NoFileDigestCache,
    )
    .expect("discovery");

    // A header that nothing read yet, but that would now shadow a later
    // lookup, must still change the manifest digest.
    write(&include, "shadowing.h", "int a(void);\n");
    let after = CcDiscoveredInputs::collect(
        root,
        BTreeSet::from([source]),
        BTreeSet::from([include.clone()]),
        &NoFileDigestCache,
    )
    .expect("discovery");

    let manifest = |inputs: &CcDiscoveredInputs| {
        inputs
            .inputs
            .iter()
            .find(|input| is_manifest_input(&input.path))
            .expect("manifest input")
            .digest
            .clone()
    };
    assert_ne!(manifest(&before), manifest(&after));
    // The file inputs themselves are untouched, so only the manifest moved.
    assert_eq!(
        before.files().next().expect("file").digest,
        after.files().next().expect("file").digest,
    );
}

#[test]
fn assembly_names_contribute_to_include_manifests() {
    let directory = tempfile::tempdir().expect("tempdir");
    let include = directory.path().join("include");
    std::fs::create_dir_all(&include).expect("create include dir");
    let directories = BTreeSet::from([include.clone()]);
    let before = manifest_snapshot(&directories).expect("initial manifest");

    write(&include, "constants.S", ".equ constant, 1\n");
    let after = manifest_snapshot(&directories).expect("updated manifest");
    assert_ne!(before, after);
}

#[test]
fn a_missing_include_directory_has_an_empty_manifest_rather_than_an_error() {
    let directory = tempfile::tempdir().expect("tempdir");
    let root = directory.path();
    let source = write(root, "a.c", "int a(void) { return 0; }\n");
    let discovered = CcDiscoveredInputs::collect(
        root,
        BTreeSet::from([source]),
        BTreeSet::from([root.join("absent")]),
        &NoFileDigestCache,
    )
    .expect("a directory that does not exist yet is not an error");
    assert_eq!(discovered.inputs.len(), 2);
}

#[test]
fn discovery_rejects_inputs_modified_during_compilation() {
    let directory = tempfile::tempdir().expect("tempdir");
    let root = directory.path();
    let source = write(root, "a.c", "int a(void) { return 0; }\n");
    let discovered = CcDiscoveredInputs::collect(
        root,
        BTreeSet::from([source.clone()]),
        BTreeSet::new(),
        &NoFileDigestCache,
    )
    .expect("discovery");

    // A file written before the compile started is what produced the object.
    assert!(
        discovered
            .verify_not_modified_since(SystemTime::now() + std::time::Duration::from_secs(60))
            .is_ok()
    );
    // A file whose timestamp lands at or after the compile start could have
    // been written by something racing the compiler, so it cannot be trusted.
    assert_eq!(
        discovered
            .verify_not_modified_since(SystemTime::UNIX_EPOCH)
            .unwrap_err()
            .kind(),
        "input-modified-during-compilation"
    );

    std::fs::write(&source, "int a(void) { return 1; }\n").expect("rewrite");
    assert_eq!(discovered.verify().unwrap_err().kind(), "input-changed");
}

#[test]
fn discovery_requires_an_absolute_working_directory() {
    assert_eq!(
        CcDiscoveredInputs::collect(
            Path::new("relative"),
            BTreeSet::new(),
            BTreeSet::new(),
            &NoFileDigestCache,
        )
        .unwrap_err()
        .kind(),
        "relative-working-directory"
    );
}

/// A build writes objects, dependency files, and archives into the same
/// directories a generated header lives in. None of those can shadow an
/// include, and counting them would make the key depend on how many sibling
/// compilations had finished by the time this one was discovered.
#[test]
fn manifests_ignore_files_that_could_never_shadow_an_include() {
    let directory = tempfile::tempdir().expect("tempdir");
    let root = directory.path();
    let include = root.join("include");
    std::fs::create_dir_all(&include).expect("create include dir");
    write(&include, "config.h", "#define V 1\n");

    let manifest = |directory: &Path| {
        manifest_snapshot(&BTreeSet::from([directory.to_path_buf()]))
            .expect("snapshot")
            .remove(directory)
            .expect("directory manifest")
    };
    let before = manifest(&include);

    // A sibling object landing beside the generated header must not move the
    // key.
    write(&include, "a.o", "\0object\0");
    write(&include, "a.d", "a.o: a.c\n");
    write(&include, "libfoo.a", "!<arch>\n");
    assert_eq!(before, manifest(&include));

    // A header appearing there still does.
    write(&include, "shadowing.h", "#define V 2\n");
    assert_ne!(before, manifest(&include));
}

/// A source can satisfy an `#include` too -- `#include "generated.c"` is
/// unusual but legal -- so one appearing in a search directory has to move the
/// key. Counting them is safe because a build writes its generated sources
/// before compiling, unlike the objects it emits during.
#[test]
fn a_source_appearing_in_a_search_directory_changes_the_manifest() {
    let directory = tempfile::tempdir().expect("tempdir");
    let include = directory.path().join("include");
    std::fs::create_dir_all(&include).expect("create include dir");
    write(&include, "used.h", "int a(void);\n");

    let manifest = |directory: &Path| {
        manifest_snapshot(&BTreeSet::from([directory.to_path_buf()]))
            .expect("snapshot")
            .remove(directory)
            .expect("directory manifest")
    };
    let before = manifest(&include);
    for source in ["shadow.c", "shadow.cpp", "shadow.cc", "shadow.cxx"] {
        write(&include, source, "int a(void) { return 0; }\n");
        assert_ne!(
            before,
            manifest(&include),
            "{source} can satisfy an #include and must move the key"
        );
        std::fs::remove_file(include.join(source)).expect("remove");
    }
}

/// Extensionless names are how C++ standard headers are spelled, and projects
/// ship their own, so they stay in the manifest.
#[test]
fn extensionless_names_count_as_includable() {
    let directory = tempfile::tempdir().expect("tempdir");
    let root = directory.path();
    let include = root.join("include");
    std::fs::create_dir_all(&include).expect("create include dir");
    let manifest = |directory: &Path| {
        manifest_snapshot(&BTreeSet::from([directory.to_path_buf()]))
            .expect("snapshot")
            .remove(directory)
            .expect("directory manifest")
    };
    let empty = manifest(&include);
    write(&include, "vector", "#pragma once\n");
    assert_ne!(empty, manifest(&include));
}

/// A precompiled header answers an `#include` without being named by one:
/// GCC prefers `foo.h.gch` over `foo.h` with nothing on the command line to
/// say so. Nothing else in the adapter can see that substitution, so the
/// manifest has to.
#[test]
fn a_precompiled_header_appearing_beside_its_header_changes_the_manifest() {
    let directory = tempfile::tempdir().expect("tempdir");
    let include = directory.path().join("include");
    std::fs::create_dir_all(&include).expect("create include dir");
    write(&include, "foo.h", "#define V 1\n");

    let manifest = |directory: &Path| {
        manifest_snapshot(&BTreeSet::from([directory.to_path_buf()]))
            .expect("snapshot")
            .remove(directory)
            .expect("directory manifest")
    };
    let before = manifest(&include);
    for precompiled in ["foo.h.gch", "foo.h.pch"] {
        write(&include, precompiled, "\0precompiled\0");
        assert_ne!(
            before,
            manifest(&include),
            "{precompiled} can answer an #include and must move the key"
        );
        std::fs::remove_file(include.join(precompiled)).expect("remove");
    }
}

/// A ledger that vouches for one cc input identity.
struct VouchingLedger {
    known: mbx_cache_core::FileIdentity,
    digest: CacheDigest,
}

impl mbx_cache_core::FileDigestCache for VouchingLedger {
    fn find(
        &self,
        scope: mbx_cache_core::FileDigestScope,
        files: &[mbx_cache_core::FileIdentity],
    ) -> Vec<Option<CacheDigest>> {
        assert_eq!(scope, mbx_cache_core::FileDigestScope::CcInput);
        files
            .iter()
            .map(|file| (*file == self.known).then(|| self.digest.clone()))
            .collect()
    }

    fn record(
        &self,
        _scope: mbx_cache_core::FileDigestScope,
        _entries: Vec<mbx_cache_core::RecordedFileDigest>,
    ) {
    }
}

#[test]
fn a_vouched_cc_input_skips_the_scan_it_already_passed() {
    let directory = tempfile::tempdir().expect("tempdir");
    let root = directory.path();
    // The macro token would bypass on a first encounter; a cc-scope ledger
    // entry says this exact identity already passed the scan, so neither the
    // scan nor the hash reads the file again.
    let source = write(root, "a.c", "/* __DATE__ */ int main(void) { return 0; }\n");
    let metadata = std::fs::metadata(&source).expect("metadata");
    let digest = CacheDigest {
        algorithm: "blake3".into(),
        hash: "d".repeat(64),
        size: metadata.len(),
    };
    let ledger = VouchingLedger {
        known: mbx_cache_core::FileIdentity::describe(&source, &metadata).expect("identity"),
        digest: digest.clone(),
    };

    let discovered = CcDiscoveredInputs::collect(
        root,
        BTreeSet::from([source.clone()]),
        BTreeSet::new(),
        &ledger,
    )
    .expect("the vouched identity answers without a scan");
    assert_eq!(discovered.inputs.len(), 1);
    assert_eq!(discovered.inputs[0].digest, digest);

    // Rewrite the file to a new length: the identity no longer matches, so
    // the scan runs and the macro token bypasses again.
    std::fs::write(&source, "/* __DATE__ */ int main(void) { return 12345; }\n").expect("rewrite");
    let reason =
        CcDiscoveredInputs::collect(root, BTreeSet::from([source]), BTreeSet::new(), &ledger)
            .unwrap_err();
    assert_eq!(reason.kind(), "embedded-timestamp-macro");
}

#[test]
fn parses_msvc_source_dependencies() {
    let directory = tempfile::tempdir().expect("tempdir");
    let path = directory.path().join("deps.json");
    std::fs::write(
        &path,
        r#"{"Version":"1.2","Data":{"Source":"src\\a.c","ProvidedModule":"","ImportedModules":[],"Includes":["include\\a.h","C:\\SDK\\stdio.h"]}}"#,
    )
    .expect("write dependency JSON");

    let dependencies = CcDepfile::read_msvc(&path).expect("parse");
    assert_eq!(
        dependencies.files,
        [
            PathBuf::from("include\\a.h"),
            PathBuf::from("C:\\SDK\\stdio.h")
        ]
    );
}

/// A ledger that answers one known identity with a fixed digest.
struct SentinelLedger {
    known: FileIdentity,
    digest: CacheDigest,
}

impl FileDigestCache for SentinelLedger {
    fn find(&self, _scope: FileDigestScope, files: &[FileIdentity]) -> Vec<Option<CacheDigest>> {
        files
            .iter()
            .map(|file| (*file == self.known).then(|| self.digest.clone()))
            .collect()
    }

    fn record(&self, _scope: FileDigestScope, _entries: Vec<RecordedFileDigest>) {}
}

/// Verification confirms an input by the identity `collect` recorded rather
/// than by reading it again, and reads it again once that identity has moved.
/// Only where the identity carries a change time.
#[cfg(unix)]
#[test]
fn verification_trusts_an_unchanged_identity_and_rereads_a_changed_one() {
    let directory = tempfile::tempdir().expect("tempdir");
    let root = directory.path();
    let header = write(root, "a.h", "int a(void);\n");
    let metadata = std::fs::metadata(&header).expect("metadata");
    // Hashing could never produce the sentinel, so a verify that passes can
    // only have trusted the identity.
    let sentinel = CacheDigest {
        algorithm: "blake3".into(),
        hash: "c".repeat(64),
        size: metadata.len(),
    };
    let ledger = SentinelLedger {
        known: FileIdentity::describe(&header, &metadata).expect("identity"),
        digest: sentinel.clone(),
    };
    let discovered = CcDiscoveredInputs::collect(
        root,
        BTreeSet::from([header.clone()]),
        BTreeSet::from([root.to_path_buf()]),
        &ledger,
    )
    .expect("discovery");
    assert_eq!(discovered.files().next().expect("input").digest, sentinel);

    discovered.verify().expect("an unchanged identity verifies");

    // Same length, new bytes: the write moves the identity, so the file is
    // read again and the sentinel no longer describes it.
    std::thread::sleep(std::time::Duration::from_millis(20));
    std::fs::write(&header, "int b(void);\n").expect("rewrite");
    assert_eq!(discovered.verify().unwrap_err().kind(), "input-changed");
}

/// The list the shim writes for a caller reads back through the same parser,
/// escapes exactly what the parser unescapes, quotes only the targets that
/// asked for it, and gives every header but the source a phony rule when `-MP`
/// asked.
#[test]
fn a_rendered_caller_depfile_reads_back_and_quotes_like_the_driver() {
    let files = vec![
        PathBuf::from("/src/a.c"),
        PathBuf::from("/src/my dir/a.h"),
        PathBuf::from("/src/#odd$/b.h"),
    ];
    let targets = [
        crate::DepfileTarget {
            name: "$(OBJ)".into(),
            quoted: false,
        },
        crate::DepfileTarget {
            name: "out/a b.o".into(),
            quoted: true,
        },
    ];

    let rendered = CcDepfile::render(&targets, &files, Path::new("/src/a.c"), true);

    assert_eq!(
        rendered,
        "$(OBJ) out/a\\ b.o: \\\n /src/a.c \\\n /src/my\\ dir/a.h \\\n /src/\\#odd$$/b.h\n/src/my\\ dir/a.h:\n/src/\\#odd$$/b.h:\n"
    );
    assert_eq!(CcDepfile::parse(&rendered).unwrap().files, files);

    let plain = CcDepfile::render(&targets[1..], &files[..1], Path::new("/src/a.c"), false);
    assert_eq!(plain, "out/a\\ b.o: \\\n /src/a.c\n");
}