vyre-self-substrate 0.6.3

Vyre self-substrate: vyre using its own primitives on its own scheduler problems. The recursion-thesis layer between vyre-primitives and vyre-driver.
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
//! Hostile input coverage validation.

use std::collections::BTreeSet;

/// Hostile input class required by the release plan.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum HostileInputClass {
    /// Invalid or undecodable bytes.
    InvalidBytes,
    /// Extreme parser/preprocessor nesting.
    ExtremeNesting,
    /// Include graph cycle.
    IncludeCycle,
    /// Recursive macro expansion.
    RecursiveMacro,
    /// Massive graph shape or fact set.
    MassiveGraph,
    /// Corrupted cache metadata.
    CorruptedCacheMetadata,
}

/// One hostile input evidence record.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct HostileInputCoverageRecord<'a> {
    /// Hostile input class.
    pub class: HostileInputClass,
    /// Owning module or crate.
    pub owner: &'a str,
    /// Exact cargo_full command.
    pub command: &'a str,
    /// Evidence path or test name.
    pub evidence: &'a str,
}

/// Hostile input coverage proof.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct HostileInputCoverageProof {
    /// Number of covered classes.
    pub class_count: usize,
}

/// Committed hostile-input artifact/source proof.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct HostileInputArtifactProof {
    /// Adversarial suite file count.
    pub adversarial_file_count: u64,
}

/// Hostile input coverage validation errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum HostileInputCoverageError {
    /// No records supplied.
    EmptyRecords,
    /// Metadata is empty.
    EmptyMetadata {
        /// Field name.
        field: &'static str,
    },
    /// Command does not use cargo_full.
    CommandDoesNotUseCargoFull {
        /// Command.
        command: String,
    },
    /// A required hostile input class is missing.
    MissingClass {
        /// Missing class.
        class: HostileInputClass,
    },
    /// Committed hostile-input evidence is missing required proof.
    ArtifactMissingEvidence {
        /// Missing evidence.
        evidence: &'static str,
    },
    /// Committed hostile-input evidence is missing a numeric field.
    ArtifactMissingNumber {
        /// Missing field.
        field: &'static str,
    },
    /// Committed hostile-input evidence missed a threshold.
    ArtifactThresholdMiss {
        /// Field.
        field: &'static str,
        /// Observed value.
        observed: u64,
        /// Required value.
        required: u64,
    },
}

impl std::fmt::Display for HostileInputCoverageError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::EmptyRecords => write!(
                f,
                "hostile input coverage is empty. Fix: add evidence for invalid bytes, nesting, include cycles, recursive macros, massive graphs, and corrupted cache metadata."
            ),
            Self::EmptyMetadata { field } => write!(
                f,
                "hostile input coverage has empty {field}. Fix: record owner, command, and evidence."
            ),
            Self::CommandDoesNotUseCargoFull { command } => write!(
                f,
                "hostile input coverage command `{command}` does not use ./cargo_full. Fix: run hostile tests through cargo_full."
            ),
            Self::MissingClass { class } => write!(
                f,
                "hostile input coverage is missing {class:?}. Fix: add an adversarial test for that class."
            ),
            Self::ArtifactMissingEvidence { evidence } => write!(
                f,
                "hostile input committed evidence is missing {evidence}. Fix: preserve adversarial suite and source tests for invalid bytes, nesting, include cycles, recursive macros, massive graphs, and corrupted caches."
            ),
            Self::ArtifactMissingNumber { field } => write!(
                f,
                "hostile input committed evidence has no numeric {field}. Fix: record exact adversarial suite counters."
            ),
            Self::ArtifactThresholdMiss {
                field,
                observed,
                required,
            } => write!(
                f,
                "hostile input committed evidence {field}={observed} missed required {required}. Fix: restore adversarial release coverage."
            ),
        }
    }
}

impl std::error::Error for HostileInputCoverageError {}

const REQUIRED_CLASSES: &[HostileInputClass] = &[
    HostileInputClass::InvalidBytes,
    HostileInputClass::ExtremeNesting,
    HostileInputClass::IncludeCycle,
    HostileInputClass::RecursiveMacro,
    HostileInputClass::MassiveGraph,
    HostileInputClass::CorruptedCacheMetadata,
];

/// Validate hostile input coverage.
pub fn validate_hostile_input_coverage(
    records: &[HostileInputCoverageRecord<'_>],
) -> Result<HostileInputCoverageProof, HostileInputCoverageError> {
    if records.is_empty() {
        return Err(HostileInputCoverageError::EmptyRecords);
    }
    let mut classes = BTreeSet::new();
    for record in records {
        for (field, value) in [
            ("owner", record.owner),
            ("command", record.command),
            ("evidence", record.evidence),
        ] {
            if value.trim().is_empty() {
                return Err(HostileInputCoverageError::EmptyMetadata { field });
            }
        }
        if !record.command.trim_start().starts_with("./cargo_full ") {
            return Err(HostileInputCoverageError::CommandDoesNotUseCargoFull {
                command: record.command.to_owned(),
            });
        }
        classes.insert(record.class);
    }
    for class in REQUIRED_CLASSES {
        if !classes.contains(class) {
            return Err(HostileInputCoverageError::MissingClass { class: *class });
        }
    }
    Ok(HostileInputCoverageProof {
        class_count: classes.len(),
    })
}

/// Validate committed hostile input artifacts and source tests.
pub fn validate_hostile_input_artifacts(
    adversarial_suite: &str,
    lexer_diagnostics_source: &str,
    include_errors_source: &str,
    preprocessor_adversarial_source: &str,
    gpu_preprocess_adversarial_source: &str,
    cache_budget_source: &str,
) -> Result<HostileInputArtifactProof, HostileInputCoverageError> {
    for (evidence, needle) in [
        ("adversarial suite marker", "\"suite\": \"adversarial\""),
        ("zero blockers", "\"blockers\": []"),
        ("Vyre adversarial root", "/matching/vyre/"),
        (
            "Dataflow consumer adversarial coverage",
            "\"dataflow_consumer_file_count\": 43",
        ),
        ("Vyrec adversarial coverage", "\"vyrec_file_count\": 4"),
        (
            "hostile parser stream tests",
            "c_parser_hostile_malformed_stream_contracts",
        ),
        ("hostile full C parser tests", "c11_parser_hostile_full_c"),
        (
            "hostile Linux flow tests",
            "c_ast_linux_corpus_hostile_flow_and_pg_parity_contracts",
        ),
        ("engine cache adversarial tests", "engine_cache_adversarial"),
        ("corruption tests", "serial_envelope_corruption"),
    ] {
        artifact_contains(adversarial_suite, evidence, needle)?;
    }

    for (source, evidence, needle) in [
        (
            lexer_diagnostics_source,
            "invalid byte diagnostic",
            "invalid_escape",
        ),
        (
            lexer_diagnostics_source,
            "diagnostic rejects invalid escape",
            "invalid string or character escape",
        ),
        (
            include_errors_source,
            "include cycle A-B-A",
            "include_cycle_a_to_b_to_a_returns_error",
        ),
        (
            include_errors_source,
            "include self cycle",
            "include_self_cycle_returns_error",
        ),
        (
            preprocessor_adversarial_source,
            "nested conditional adversary",
            "deeply_nested_conditionals",
        ),
        (
            preprocessor_adversarial_source,
            "nested macro args",
            "function_macro_nested_parens_in_args",
        ),
        (
            gpu_preprocess_adversarial_source,
            "recursive macro adversary",
            "adversarial_recursive_object_macro_does_not_loop_or_cpu_fallback",
        ),
        (
            gpu_preprocess_adversarial_source,
            "hostile diagnostic",
            "hostile diagnostic",
        ),
        (
            cache_budget_source,
            "cache byte bound contract",
            "entry_bytes > max_bytes",
        ),
        (
            cache_budget_source,
            "cache overflow guard",
            "checked_add(entry_bytes)",
        ),
    ] {
        artifact_contains(source, evidence, needle)?;
    }

    let adversarial_file_count = artifact_number_field(adversarial_suite, "file_count")?;
    artifact_at_least("adversarial file_count", adversarial_file_count, 100)?;
    let test_entrypoints = adversarial_suite
        .matches("\"has_test_entrypoint\": true")
        .count() as u64;
    artifact_at_least("adversarial test entrypoints", test_entrypoints, 50)?;

    Ok(HostileInputArtifactProof {
        adversarial_file_count,
    })
}

fn artifact_contains(
    artifact: &str,
    evidence: &'static str,
    needle: &str,
) -> Result<(), HostileInputCoverageError> {
    if artifact.contains(needle) {
        Ok(())
    } else {
        Err(HostileInputCoverageError::ArtifactMissingEvidence { evidence })
    }
}

fn artifact_at_least(
    field: &'static str,
    observed: u64,
    required: u64,
) -> Result<(), HostileInputCoverageError> {
    if observed >= required {
        Ok(())
    } else {
        Err(HostileInputCoverageError::ArtifactThresholdMiss {
            field,
            observed,
            required,
        })
    }
}

fn artifact_number_field(
    artifact: &str,
    field: &'static str,
) -> Result<u64, HostileInputCoverageError> {
    let key = format!("\"{field}\"");
    let start = artifact
        .find(&key)
        .ok_or(HostileInputCoverageError::ArtifactMissingNumber { field })?;
    let after_key = &artifact[start + key.len()..];
    let colon = after_key
        .find(':')
        .ok_or(HostileInputCoverageError::ArtifactMissingNumber { field })?;
    let after_colon = after_key[colon + 1..].trim_start();
    let digits = after_colon
        .chars()
        .take_while(|ch| ch.is_ascii_digit())
        .collect::<String>();
    if digits.is_empty() {
        return Err(HostileInputCoverageError::ArtifactMissingNumber { field });
    }
    digits
        .parse::<u64>()
        .map_err(|_| HostileInputCoverageError::ArtifactMissingNumber { field })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn hostile_input_coverage_accepts_all_required_classes() {
        let proof = validate_hostile_input_coverage(&records())
            .expect("Fix: complete hostile coverage should pass");

        assert_eq!(proof.class_count, 6);
    }

    #[test]
    fn hostile_input_coverage_rejects_missing_corrupted_cache() {
        let mut records = records();
        records.pop();

        assert_eq!(
            validate_hostile_input_coverage(&records)
                .expect_err("missing hostile class should fail"),
            HostileInputCoverageError::MissingClass {
                class: HostileInputClass::CorruptedCacheMetadata,
            }
        );
    }

    #[test]
    fn hostile_input_coverage_rejects_raw_cargo() {
        let mut records = records();
        records[0].command = "cargo test";

        assert_eq!(
            validate_hostile_input_coverage(&records).expect_err("raw cargo should fail"),
            HostileInputCoverageError::CommandDoesNotUseCargoFull {
                command: "cargo test".to_owned(),
            }
        );
    }

    #[test]
    fn hostile_input_coverage_accepts_committed_adversarial_artifacts() {
        let proof = validate_hostile_input_artifacts(
            include_str!("../../../../release/evidence/tests/adversarial-suite.json"),
            include_str!("../../../../vyre-frontend-c/tests/c11_lexer_diagnostics.rs"),
            include_str!("../../../../vyre-frontend-c/tests/preprocessor_include_errors.rs"),
            include_str!("../../../../vyre-frontend-c/tests/preprocessor_adversarial.rs"),
            include_str!("../../../../vyre-frontend-c/tests/gpu_preprocess_adversarial.rs"),
            include_str!(
                "../../../../vyre-frontend-c/tests/frontend_pipeline_cache_budget_contract.rs"
            ),
        )
        .expect("Fix: committed hostile input evidence should pass");

        assert!(proof.adversarial_file_count >= 100);
    }

    #[test]
    fn hostile_input_coverage_rejects_missing_recursive_macro_source() {
        let err = validate_hostile_input_artifacts(
            include_str!("../../../../release/evidence/tests/adversarial-suite.json"),
            include_str!("../../../../vyre-frontend-c/tests/c11_lexer_diagnostics.rs"),
            include_str!("../../../../vyre-frontend-c/tests/preprocessor_include_errors.rs"),
            include_str!("../../../../vyre-frontend-c/tests/preprocessor_adversarial.rs"),
            "fn unrelated() {}",
            include_str!(
                "../../../../vyre-frontend-c/tests/frontend_pipeline_cache_budget_contract.rs"
            ),
        )
        .expect_err("missing recursive macro source should fail");

        assert_eq!(
            err,
            HostileInputCoverageError::ArtifactMissingEvidence {
                evidence: "recursive macro adversary",
            }
        );
    }

    fn records() -> Vec<HostileInputCoverageRecord<'static>> {
        vec![
            record(HostileInputClass::InvalidBytes),
            record(HostileInputClass::ExtremeNesting),
            record(HostileInputClass::IncludeCycle),
            record(HostileInputClass::RecursiveMacro),
            record(HostileInputClass::MassiveGraph),
            record(HostileInputClass::CorruptedCacheMetadata),
        ]
    }

    fn record(class: HostileInputClass) -> HostileInputCoverageRecord<'static> {
        HostileInputCoverageRecord {
            class,
            owner: "vyrec/dataflow",
            command: "./cargo_full test -j1 -p vyre-self-substrate",
            evidence: "release/hostile-input.md",
        }
    }
}