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
//! Versioned cross-engine analysis fact records.
//!
//! Concrete analysis engines own their internal fact bodies. This module owns
//! the structural interchange record shared across engine boundaries.
use crate::soundness::Soundness;
use serde::{Deserialize, Serialize};
/// Current structural analysis-fact schema version.
pub const ANALYSIS_FACT_SCHEMA_VERSION: u16 = 1;
/// Failure to structurally encode, decode, or admit an analysis fact.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "error", rename_all = "snake_case")]
pub enum AnalysisFactCodecError {
/// The record carries a stale or unknown schema version.
UnsupportedVersion {
/// Version carried by the rejected record.
found: u16,
/// Current version required by this decoder.
expected: u16,
},
/// The structural representation is malformed.
Malformed {
/// Decoder or encoder failure detail.
message: String,
},
}
impl core::fmt::Display for AnalysisFactCodecError {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::UnsupportedVersion { found, expected } => write!(
formatter,
"unsupported analysis fact schema version {found}; expected {expected}"
),
Self::Malformed { message } => {
write!(formatter, "malformed analysis fact record: {message}")
}
}
}
}
impl std::error::Error for AnalysisFactCodecError {}
/// Cross-engine fact families accepted by the structural schema.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AnalysisFactKind {
/// Attacker-controlled or analysis source fact.
Source,
/// Security sink fact.
Sink,
/// Taint/dataflow reachability fact.
Taint,
/// Sanitizer or kill-set fact.
Sanitizer,
/// Program graph edge or call/control edge fact.
GraphEdge,
/// Rust borrow loan fact.
BorrowLoan,
/// Rust origin/region fact.
BorrowOrigin,
/// Rust origin subset/outlives fact.
BorrowSubset,
/// Dominance or authorization-guard fact.
Dominance,
/// Numeric range or bounds fact.
Range,
/// Source-to-sink witness/path fact.
Witness,
}
/// Structural cross-engine analysis fact record.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AnalysisFactRecord {
/// Schema version, currently [`ANALYSIS_FACT_SCHEMA_VERSION`].
pub schema_version: u16,
/// Producer id such as `c-c11`, `rustc-nll`, or `external-dataflow`.
pub producer: String,
/// Shared fact family.
pub kind: AnalysisFactKind,
/// Stable producer-local fact id.
pub fact_id: u64,
/// Primary subject id.
pub subject: u64,
/// Optional object id.
pub object: Option<u64>,
/// Optional auxiliary id, usually a point, edge kind, or relation id.
pub aux: Option<u64>,
/// Stable file id, or zero when not source-spanned.
pub file_id: u32,
/// Start byte offset, inclusive.
pub start_byte: u32,
/// End byte offset, exclusive.
pub end_byte: u32,
/// Soundness label for the fact.
pub soundness: Soundness,
}
impl AnalysisFactRecord {
/// Build one structural analysis fact record.
#[must_use]
pub fn new(
producer: impl Into<String>,
kind: AnalysisFactKind,
fact_id: u64,
subject: u64,
soundness: Soundness,
) -> Self {
Self {
schema_version: ANALYSIS_FACT_SCHEMA_VERSION,
producer: producer.into(),
kind,
fact_id,
subject,
object: None,
aux: None,
file_id: 0,
start_byte: 0,
end_byte: 0,
soundness,
}
}
/// Reject stale or unknown schema versions before consuming this record.
///
/// # Errors
///
/// Returns [`AnalysisFactCodecError::UnsupportedVersion`] unless the record
/// carries [`ANALYSIS_FACT_SCHEMA_VERSION`].
pub const fn validate_schema_version(&self) -> Result<(), AnalysisFactCodecError> {
if self.schema_version == ANALYSIS_FACT_SCHEMA_VERSION {
Ok(())
} else {
Err(AnalysisFactCodecError::UnsupportedVersion {
found: self.schema_version,
expected: ANALYSIS_FACT_SCHEMA_VERSION,
})
}
}
/// Encode this record as structural JSON.
///
/// # Errors
///
/// Rejects non-current versions and serialization failures.
pub fn encode_json(&self) -> Result<Vec<u8>, AnalysisFactCodecError> {
self.validate_schema_version()?;
serde_json::to_vec(self).map_err(|error| AnalysisFactCodecError::Malformed {
message: error.to_string(),
})
}
/// Decode and version-admit one structural JSON record.
///
/// # Errors
///
/// Rejects malformed JSON and every non-current schema version.
pub fn decode_json(bytes: &[u8]) -> Result<Self, AnalysisFactCodecError> {
let record: Self =
serde_json::from_slice(bytes).map_err(|error| AnalysisFactCodecError::Malformed {
message: error.to_string(),
})?;
record.validate_schema_version()?;
Ok(record)
}
/// Attach an object id.
#[must_use]
pub const fn with_object(mut self, object: u64) -> Self {
self.object = Some(object);
self
}
/// Attach an auxiliary relation id.
#[must_use]
pub const fn with_aux(mut self, aux: u64) -> Self {
self.aux = Some(aux);
self
}
/// Attach a byte span.
#[must_use]
pub const fn with_span(mut self, file_id: u32, start_byte: u32, end_byte: u32) -> Self {
self.file_id = file_id;
self.start_byte = start_byte;
self.end_byte = end_byte;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_record() -> AnalysisFactRecord {
AnalysisFactRecord::new(
"rustc-nll",
AnalysisFactKind::BorrowSubset,
9,
3,
Soundness::Exact,
)
.with_object(5)
.with_aux(11)
.with_span(7, 100, 120)
}
/// WHY: structural interchange must preserve every field without relying on
/// hand-rendered tokens. This does not validate engine-specific fact bodies.
#[test]
fn structural_json_round_trip_preserves_the_record() {
let record = sample_record();
let encoded = record.encode_json().expect("current record must encode");
let decoded = AnalysisFactRecord::decode_json(&encoded)
.expect("current structural record must decode");
assert_eq!(decoded, record);
}
#[test]
fn optional_zero_ids_remain_distinct_from_absence() {
let absent = AnalysisFactRecord::new(
"rustc-nll",
AnalysisFactKind::BorrowLoan,
1,
5,
Soundness::Exact,
);
let zero = absent.clone().with_object(0).with_aux(0);
let decoded_absent =
AnalysisFactRecord::decode_json(&absent.encode_json().expect("absent ids must encode"))
.expect("absent ids must decode");
let decoded_zero =
AnalysisFactRecord::decode_json(&zero.encode_json().expect("zero ids must encode"))
.expect("zero ids must decode");
assert_eq!(decoded_absent.object, None);
assert_eq!(decoded_absent.aux, None);
assert_eq!(decoded_zero.object, Some(0));
assert_eq!(decoded_zero.aux, Some(0));
assert_ne!(decoded_absent, decoded_zero);
}
/// WHY: stale and future records must fail closed before an engine can
/// reinterpret their fields. This covers version admission, not body semantics.
#[test]
fn decoder_rejects_every_non_current_version_class() {
for rejected in [
ANALYSIS_FACT_SCHEMA_VERSION - 1,
ANALYSIS_FACT_SCHEMA_VERSION + 1,
u16::MAX,
] {
let mut record = sample_record();
record.schema_version = rejected;
let encoded = serde_json::to_vec(&record).expect("test record must serialize");
assert_eq!(
AnalysisFactRecord::decode_json(&encoded),
Err(AnalysisFactCodecError::UnsupportedVersion {
found: rejected,
expected: ANALYSIS_FACT_SCHEMA_VERSION,
})
);
assert_eq!(
record.encode_json(),
Err(AnalysisFactCodecError::UnsupportedVersion {
found: rejected,
expected: ANALYSIS_FACT_SCHEMA_VERSION,
})
);
}
}
#[test]
fn malformed_structural_record_is_rejected() {
let error = AnalysisFactRecord::decode_json(br#"{"schema_version":1"#)
.expect_err("truncated JSON must be rejected");
assert!(matches!(error, AnalysisFactCodecError::Malformed { .. }));
}
}