Skip to content

Commit da48008

Browse files
authored
fix(array): require unsafe row initialization evidence (#10013)
## Summary `InitializedRow::fill` could return a token for an empty subslice, allowing a safe callback to leave its output row uninitialized. Dense execution could then publish that storage. ## Changes Makes `fill` unsafe and requires the entire current callback's row to remain initialized until that callback returns its token. Tightens the corresponding `InitializedElement::write` contract and updates callers and consumer safety comments. Retains ordinary, empty, zero-width, and partially valid output coverage. Error and unwind abandonment remain unchanged. Reviewed the complete diff with `rust-style`. The commit hook ran `cargo +nightly fmt --all -- --check` and reported formatting changes, which were applied manually without rerunning the check. Tests, doctests, builds, Clippy, and benchmarks were not run, following AGENTS.md. No reproduction read uninitialized memory. ## API Changes `InitializedRow::fill` now requires an explicit unsafe operation. --------- Signed-off-by: "Connor Tsui" <[email protected]>
1 parent 6c2eb22 commit da48008

5 files changed

Lines changed: 34 additions & 20 deletions

File tree

‎vortex-array/src/scalar_fn/unstable/row/batch/tests.rs‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ impl RowFn for RepeatValue {
321321
);
322322

323323
visitor.visit_into::<(i64,), FixedSizeListSink<i64>, _>(*width, |(value,), row| {
324-
InitializedRow::fill(row, |_| value)
324+
// SAFETY: fills the entire supplied row and returns its token without further writes.
325+
unsafe { InitializedRow::fill(row, |_| value) }
325326
})
326327
}
327328
}
@@ -459,10 +460,13 @@ impl RowFn for FilteredRepeat {
459460
"test.filtered_repeat width must fit in u32, got {width}",
460461
);
461462

462-
visitor
463-
.visit_into::<(FilterOnlyI64,), FixedSizeListSink<i64>, _>(*width, |(value,), row| {
464-
InitializedRow::fill(row, |_| value)
465-
})
463+
visitor.visit_into::<(FilterOnlyI64,), FixedSizeListSink<i64>, _>(
464+
*width,
465+
|(value,), row| {
466+
// SAFETY: fills the entire supplied row and returns its token without further writes.
467+
unsafe { InitializedRow::fill(row, |_| value) }
468+
},
469+
)
466470
}
467471
}
468472

‎vortex-array/src/scalar_fn/unstable/row/execute/sink.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ where
9090
}
9191
}
9292

93-
// SAFETY: every row callback completed successfully, so each returned the required write token.
93+
// SAFETY: every callback returned its token, which proves that its entire output row remains
94+
// initialized under the `OutputSink` contract.
9495
unsafe { Sink::finish(sink) }
9596
}
9697

@@ -174,7 +175,7 @@ where
174175
}
175176

176177
// SAFETY: the initializer completed before traversal, and every visited callback completed
177-
// successfully and returned the required write token.
178+
// successfully with a token proving its entire output row remains initialized.
178179
unsafe { Sink::finish(sink) }.map(Some)
179180
}
180181

@@ -267,7 +268,7 @@ where
267268
}
268269

269270
// SAFETY: the initializer completed before traversal, and every visited callback completed
270-
// successfully and returned the required write token.
271+
// successfully with a token proving its entire output row remains initialized.
271272
unsafe { Sink::finish(sink) }
272273
}
273274

‎vortex-array/src/scalar_fn/unstable/row/types/sink/fixed_size_list.rs‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,22 @@ use crate::scalar_fn::unstable::row::ViewLen;
2525
use crate::validity::Validity;
2626

2727
/// Proof that every element in one uninitialized fixed-size row was initialized.
28-
///
29-
/// The private field prevents construction without calling [`fill`](Self::fill), which writes the
30-
/// complete row before returning.
3128
#[must_use = "return this token from the row closure to prove that it initialized the output"]
3229
pub struct InitializedRow(());
3330

3431
impl InitializedRow {
3532
/// Fill every element in `row` and return its proof token.
33+
///
34+
/// # Safety
35+
///
36+
/// - `row` must be the entire [`FixedSizeListSink`] row supplied to the current callback,
37+
/// including when its width is zero.
38+
/// - On success, that callback must return this token and preserve every element's
39+
/// initialization until it returns.
40+
///
41+
/// Violating these requirements can cause undefined behavior.
3642
#[inline]
37-
pub fn fill<T>(
43+
pub unsafe fn fill<T>(
3844
row: &mut [MaybeUninit<T>],
3945
mut value_for_index: impl FnMut(usize) -> T,
4046
) -> Self {
@@ -92,9 +98,9 @@ pub struct FixedSizeListSink<T> {
9298

9399
// SAFETY: `with_capacity` reserves `row_count * width` elements, and `FixedSizeRows` retains that
94100
// shape for its lifetime. Each row is one disjoint `width`-element slice. `InitializedRow::fill`
95-
// writes every element before returning its private token, and `FixedSizeRows::fill_default`
96-
// writes every flat element before masked traversal. `values` retains length zero until every row
97-
// is safe to publish in `finish`.
101+
// requires the entire current row and preservation of its initialization until the callback
102+
// returns its private token. `FixedSizeRows::fill_default` writes every flat element before masked
103+
// traversal. `values` retains length zero until every row is safe to publish in `finish`.
98104
unsafe impl<T: OutputElement + Copy + Default> OutputSink for FixedSizeListSink<T> {
99105
type Params = usize;
100106
type Rows<'a> = FixedSizeRows<'a, T>;

‎vortex-array/src/scalar_fn/unstable/row/types/sink/mod.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ pub use utf8::Utf8Writer;
5757
/// - [`initialize_skipped_rows`] is the only exception to this stability requirement. The executor
5858
/// checks the length again after the initializer. The initializer **must** initialize every row.
5959
/// - A row must either be initialized before the callback or require a
60-
/// [`WriteToken`] that safe code cannot produce without initializing that exact row. Evidence for
61-
/// an uninitialized row **must not** be safely forgeable, reusable, or substitutable.
60+
/// [`WriteToken`] that safe code cannot produce without initializing that exact row and keeping
61+
/// it initialized until the callback returns. Evidence for an uninitialized row **must not** be
62+
/// safely forgeable, reusable, or substitutable.
6263
/// - `Self` and every borrowed [`Rows`] view **must** remain safe to drop if decoding,
6364
/// preparation, skipped-row initialization, or a row callback returns an error or unwinds. The
6465
/// executor can abandon a sink after any prefix of rows.

‎vortex-array/src/scalar_fn/unstable/row/types/sink/uninit_element.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ impl InitializedElement {
3737
///
3838
/// # Safety
3939
///
40-
/// `row` must be the [`UninitElementSink`] row supplied to the current callback. The caller
41-
/// must return the token from that callback. Using another row or returning the token from
42-
/// another callback can cause undefined behavior.
40+
/// - `row` must be the [`UninitElementSink`] slot supplied to the current callback.
41+
/// - On success, that callback must return this token and preserve the slot's initialization
42+
/// until it returns.
43+
///
44+
/// Violating these requirements can cause undefined behavior.
4345
#[inline]
4446
pub unsafe fn write<T>(row: &mut MaybeUninit<T>, value: T) -> Self {
4547
row.write(value);

0 commit comments

Comments
 (0)