facet-reflect 0.46.5

Build and manipulate values of arbitrary Facet types at runtime while respecting invariants - safe runtime reflection
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
#![cfg(not(miri))]
#![cfg(feature = "slow-tests")]

use std::collections::hash_map::DefaultHasher;
use std::fs;
use std::hash::{Hash, Hasher};
use std::path::Path;

use facet_testhelpers::test;

/// Test case structure for compilation tests
struct CompilationTest {
    /// Source code to compile
    source: &'static str,
    /// Expected error messages to find in the output
    expected_errors: &'static [&'static str],
    /// Name of the test for reporting purposes
    name: &'static str,
}

/// Strips ANSI escape sequences from a string
fn strip_ansi_escapes(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '\x1B' {
            if let Some(&'[') = chars.peek() {
                chars.next(); // consume '['
                // Skip until we find the end of the sequence
                for c in chars.by_ref() {
                    if c.is_ascii_alphabetic() || c == 'm' {
                        break;
                    }
                }
            } else {
                result.push(c);
            }
        } else {
            result.push(c);
        }
    }
    result
}

/// Calculate a hash for the source code to create a unique target directory
fn hash_source(name: &str, source: &str) -> u64 {
    let mut hasher = DefaultHasher::new();
    name.hash(&mut hasher);
    source.hash(&mut hasher);
    hasher.finish()
}

/// Run a single compilation test that is expected to fail
fn run_compilation_test(test: &CompilationTest) {
    println!("{}", format_args!("Running test: {}", test.name));

    // Create a random temp directory for the Cargo project
    let temp_dir = tempfile::tempdir().expect("Failed to create temp directory");
    let project_dir = temp_dir.path();
    println!(
        "{}",
        format_args!("  Project directory: {}", project_dir.display())
    );

    // Get absolute paths to the facet crates
    let workspace_dir = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
    let facet_path = workspace_dir.join("facet");
    let facet_reflect_path = workspace_dir.join("facet-reflect");

    // Create src directory
    fs::create_dir(project_dir.join("src")).expect("Failed to create src directory");

    // Create Cargo.toml with dependencies
    let cargo_toml = format!(
        r#"
[package]
name = "facet-test-project"
version = "0.1.0"
edition = "2021"

[dependencies]
eyre = "0.6"
facet = {{ path = {:?} }}
facet-reflect = {{ path = {:?} }}
    "#,
        facet_path.display(),
        facet_reflect_path.display()
    );

    // Write the Cargo.toml file
    fs::write(project_dir.join("Cargo.toml"), cargo_toml).expect("Failed to write Cargo.toml");

    // Write the main.rs file
    fs::write(project_dir.join("src").join("main.rs"), test.source)
        .expect("Failed to write main.rs");

    // Generate a unique target directory based on the test name and source code
    let source_hash = hash_source(test.name, test.source);
    let target_dir = format!("/tmp/ui_tests/target_{source_hash}");
    println!("{}", format_args!("  Target directory: {target_dir}"));

    // Run cargo build
    let mut cmd = std::process::Command::new("cargo");
    cmd.current_dir(project_dir)
        .args(["build", "--color=always"])
        .env("CARGO_TERM_COLOR", "always")
        .env("CARGO_TARGET_DIR", &target_dir); // Use source-hash based target directory

    let output = cmd.output().expect("Failed to execute cargo build");

    // Check if compilation failed (as expected)
    let exit_code = output.status.code().unwrap_or(0);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Strip ANSI escape sequences for error matching while preserving original for display
    let stderr_clean = strip_ansi_escapes(&stderr);

    // Verify the compilation failed as expected
    if exit_code == 0 {
        println!("❌ Test failed:");
        println!("  The code compiled successfully, but it should have failed");
        panic!(
            "Test '{}' compiled successfully but should have failed",
            test.name
        );
    } else {
        println!("  ✓ Compilation failed as expected");
    }

    // Check for expected error messages.
    // Each string may contain " OR " to specify alternatives (one must match).
    // Across array elements, ALL must match (AND semantics).
    let mut missing_errors = Vec::new();
    for &expected_error in test.expected_errors {
        let alternatives: Vec<&str> = expected_error.split(" OR ").collect();
        let found = alternatives.iter().any(|alt| stderr_clean.contains(alt));
        if !found {
            missing_errors.push(expected_error);
        } else {
            println!("  ✓ Found expected error: '{expected_error}'");
        }
    }

    // Report any missing expected errors
    if !missing_errors.is_empty() {
        println!("\n❌ MISSING EXPECTED ERRORS:");
        for error in &missing_errors {
            println!("  - '{error}'");
        }

        // Print the error output for debugging
        println!("\nCompiler error output:");
        println!("{stderr}");

        if !stdout.is_empty() {
            println!("\nCompiler standard output:");
            println!("{stdout}");
        }

        panic!(
            "Test '{}' did not produce the expected error messages: {:?}",
            test.name, missing_errors
        );
    }

    println!("{}", format_args!("  ✓ Test '{}' passed", test.name));
}

/// Test for lifetime issues in Poke implementation
#[test]
#[cfg(not(miri))]
fn test_partial_poke_lifetime_error() {
    let test = CompilationTest {
        name: "poke_lifetime_error",
        source: include_str!("fixtures/lifetimes.rs"),
        expected_errors: &["error[E0597]: `s` does not live long enough"],
    };

    run_compilation_test(&test);
}

#[test]
#[cfg(not(miri))]
fn test_partial_covariant_growing() {
    let test = CompilationTest {
        name: "covariant_growing",
        source: include_str!("fixtures/partial_covariant_growing.rs"),
        // Either E0521 or "lifetime may not live long enough" depending on Rust version
        expected_errors: &["lifetime may not live long enough"],
    };

    run_compilation_test(&test);
}

#[test]
#[cfg(not(miri))]
fn test_partial_invariant_growing() {
    let test = CompilationTest {
        name: "invariant_growing",
        source: include_str!("fixtures/partial_invariant_growing.rs"),
        // Either E0521 or "lifetime may not live long enough" depending on Rust version
        expected_errors: &["lifetime may not live long enough"],
    };

    run_compilation_test(&test);
}

#[test]
#[cfg(not(miri))]
fn test_partial_contravariant_shrinking() {
    let test = CompilationTest {
        name: "contravariant_shrinking",
        source: include_str!("fixtures/partial_contravariant_shrinking.rs"),
        expected_errors: &[
            "error[E0521]: borrowed data escapes outside of function OR error: lifetime may not live long enough",
        ],
    };

    run_compilation_test(&test);
}

#[test]
#[cfg(not(miri))]
fn test_partial_invariant_shrinking() {
    let test = CompilationTest {
        name: "invariant_shrinking",
        source: include_str!("fixtures/partial_invariant_shrinking.rs"),
        expected_errors: &[
            "error[E0521]: borrowed data escapes outside of function OR error: lifetime may not live long enough",
        ],
    };

    run_compilation_test(&test);
}

#[test]
#[cfg(not(miri))]
fn test_peek_covariant_growing() {
    let test = CompilationTest {
        name: "covariant_growing",
        source: include_str!("fixtures/peek_covariant_growing.rs"),
        expected_errors: &[
            "error[E0521]: borrowed data escapes outside of function OR error: lifetime may not live long enough",
        ],
    };

    run_compilation_test(&test);
}

#[test]
#[cfg(not(miri))]
fn test_peek_invariant_growing() {
    let test = CompilationTest {
        name: "invariant_growing",
        source: include_str!("fixtures/peek_invariant_growing.rs"),
        expected_errors: &[
            "error[E0521]: borrowed data escapes outside of function OR error: lifetime may not live long enough",
        ],
    };

    run_compilation_test(&test);
}

#[test]
#[cfg(not(miri))]
fn test_peek_contravariant_shrinking() {
    let test = CompilationTest {
        name: "contravariant_shrinking",
        source: include_str!("fixtures/peek_contravariant_shrinking.rs"),
        // Depending on Rust version / platform, error can be either:
        // - E0521 (borrowed data escapes) - the original invariance error
        // - "lifetime may not live long enough" - nightly/later Rust
        // Either one is acceptable as long as the code fails to compile.
        expected_errors: &[
            "error[E0521]: borrowed data escapes outside of function OR error: lifetime may not live long enough",
        ],
    };

    run_compilation_test(&test);
}

#[test]
#[cfg(not(miri))]
fn test_peek_invariant_shrinking() {
    let test = CompilationTest {
        name: "invariant_shrinking",
        source: include_str!("fixtures/peek_invariant_shrinking.rs"),
        expected_errors: &[
            "error[E0521]: borrowed data escapes outside of function OR error: lifetime may not live long enough",
        ],
    };

    run_compilation_test(&test);
}

#[test]
#[cfg(not(miri))]
fn test_peek_owned_dropped_before_ref() {
    let test = CompilationTest {
        name: "owned_dropped_before_ref",
        source: include_str!("fixtures/owned_dropped_before_ref.rs"),
        expected_errors: &["error[E0505]: cannot move out of `owned` because it is borrowed"],
    };

    run_compilation_test(&test);
}

/// Soundness test: ensures the fn pointer UB exploit is prevented.
///
/// Before making Peek invariant with respect to 'facet, code like this would compile
/// and lead to use-after-free (miri would detect it). Now it should fail
/// to compile because Peek is invariant with respect to 'facet.
///
/// See: https://github.com/facet-rs/facet/issues/1168
#[test]
#[cfg(not(miri))]
fn test_peek_fn_ptr_ub_exploit() {
    let test = CompilationTest {
        name: "fn_ptr_ub_exploit",
        source: include_str!("fixtures/fn_ptr_ub_exploit.rs"),
        expected_errors: &[
            "error[E0521]: borrowed data escapes outside of function OR error: lifetime may not live long enough",
        ],
    };

    run_compilation_test(&test);
}

/// Soundness test for GitHub issue #1555
///
/// Before the fix, OxRef::new was safe but accepted arbitrary pointers,
/// allowing use-after-free through safe code like PartialEq.
///
/// After the fix, OxRef::new is unsafe, so this code should fail to compile.
///
/// See: https://github.com/facet-rs/facet/issues/1555
#[test]
#[cfg(not(miri))]
fn test_oxref_unsound_from_raw_ptr() {
    let test = CompilationTest {
        name: "oxref_unsound_from_raw_ptr",
        source: include_str!("fixtures/oxref_unsound_from_raw_ptr.rs"),
        expected_errors: &["call to unsafe function `OxRef::<'a>::new` is unsafe"],
    };

    run_compilation_test(&test);
}

/// Soundness test for GitHub issue #1563
///
/// After the fix, Opaque<T> requires T: 'static, so borrowed references
/// are rejected and lifetime laundering through Poke is prevented.
#[test]
#[cfg(not(miri))]
fn test_poke_opaque_insufficient_lifetime() {
    let test = CompilationTest {
        name: "opaque_insufficient_lifetime",
        source: include_str!("fixtures/opaque_insufficient_lifetime.rs"),
        expected_errors: &["does not live long enough"],
    };

    run_compilation_test(&test);
}

/// Soundness test for GitHub issue #1663
///
/// Before the fix, Partial::alloc_shape() was safe but accepted untrusted shapes,
/// allowing UB when the shape didn't match the type being materialized.
///
/// After the fix, Partial::alloc_shape() is unsafe, so this code should fail to compile.
///
/// See: https://github.com/facet-rs/facet/issues/1663
#[test]
#[cfg(not(miri))]
fn test_partial_untrusted_shape() {
    let test = CompilationTest {
        name: "untrusted_shape",
        source: include_str!("fixtures/untrusted_shape.rs"),
        expected_errors: &["call to unsafe function"],
    };

    run_compilation_test(&test);
}

/// Soundness test for GitHub issue #1573
///
/// Before the fix, Attr could store non-Sync data like Rc<T>, but Attr itself
/// was Sync, allowing data races when accessed from multiple threads.
///
/// After the fix, Attr::new requires T: Sync, so this code should fail to compile.
///
/// See: https://github.com/facet-rs/facet/issues/1573
#[test]
#[cfg(not(miri))]
fn test_attr_non_sync_data() {
    let test = CompilationTest {
        name: "attr_non_sync_data",
        source: include_str!("fixtures/non_sync_data.rs"),
        expected_errors: &["Rc<i32>` cannot be shared between threads safely"],
    };

    run_compilation_test(&test);
}

/// Soundness test for GitHub issues #1665, #1684, #1685
///
/// Before the fix, PeekListLike::new(), PeekMap::new(), PeekSet::new(), PeekList::new(),
/// and PeekNdArray::new() were safe but accepted untrusted vtables, allowing UB when
/// those vtables had malicious function pointers.
///
/// After the fix, these constructors are unsafe, so this code should fail to compile.
///
/// See: https://github.com/facet-rs/facet/issues/1665
/// See: https://github.com/facet-rs/facet/issues/1684
/// See: https://github.com/facet-rs/facet/issues/1685
#[test]
#[cfg(not(miri))]
fn test_peek_untrusted_vtable() {
    let test = CompilationTest {
        name: "peek_untrusted_vtable",
        source: include_str!("fixtures/untrusted_vtable.rs"),
        expected_errors: &["call to unsafe function"],
    };

    run_compilation_test(&test);
}