Skip to content

Commit 83cfb3a

Browse files
committed
feat: add debug assertions for new bulk append unsafe code
Add debug_assert! statements before unsafe blocks in the new bulk append methods (impl_append_to_builder macro, append_booleans, append_timestamps, append_dates) and the read_row_at macro. Assertions verify null pointer and element_offset validity before raw pointer dereference in the hot path for array element iteration.
1 parent 463d277 commit 83cfb3a

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

native/core/src/execution/shuffle/spark_unsafe/list.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ macro_rules! impl_append_to_builder {
4949
if NULLABLE {
5050
let mut ptr = self.element_offset as *const $element_type;
5151
let null_words = self.null_bitset_ptr();
52+
debug_assert!(!null_words.is_null(), "null_bitset_ptr is null");
53+
debug_assert!(!ptr.is_null(), "element_offset pointer is null");
5254
for idx in 0..num_elements {
5355
let word_idx = idx >> 6;
5456
let bit_idx = idx & 0x3f;
@@ -66,6 +68,7 @@ macro_rules! impl_append_to_builder {
6668
}
6769
} else {
6870
// SAFETY: element_offset points to contiguous data of length num_elements
71+
debug_assert!(self.element_offset != 0, "element_offset is null");
6972
let slice = unsafe {
7073
std::slice::from_raw_parts(
7174
self.element_offset as *const $element_type,
@@ -168,9 +171,11 @@ impl SparkUnsafeArray {
168171
}
169172

170173
let mut ptr = self.element_offset as *const u8;
174+
debug_assert!(!ptr.is_null(), "append_booleans: element_offset pointer is null");
171175

172176
if NULLABLE {
173177
let null_words = self.null_bitset_ptr();
178+
debug_assert!(!null_words.is_null(), "append_booleans: null_bitset_ptr is null");
174179
for idx in 0..num_elements {
175180
let word_idx = idx >> 6;
176181
let bit_idx = idx & 0x3f;
@@ -208,6 +213,8 @@ impl SparkUnsafeArray {
208213
if NULLABLE {
209214
let mut ptr = self.element_offset as *const i64;
210215
let null_words = self.null_bitset_ptr();
216+
debug_assert!(!null_words.is_null(), "append_timestamps: null_bitset_ptr is null");
217+
debug_assert!(!ptr.is_null(), "append_timestamps: element_offset pointer is null");
211218
for idx in 0..num_elements {
212219
let word_idx = idx >> 6;
213220
let bit_idx = idx & 0x3f;
@@ -225,6 +232,7 @@ impl SparkUnsafeArray {
225232
}
226233
} else {
227234
// SAFETY: element_offset points to contiguous i64 data of length num_elements
235+
debug_assert!(self.element_offset != 0, "append_timestamps: element_offset is null");
228236
let slice = unsafe {
229237
std::slice::from_raw_parts(self.element_offset as *const i64, num_elements)
230238
};
@@ -245,6 +253,8 @@ impl SparkUnsafeArray {
245253
if NULLABLE {
246254
let mut ptr = self.element_offset as *const i32;
247255
let null_words = self.null_bitset_ptr();
256+
debug_assert!(!null_words.is_null(), "append_dates: null_bitset_ptr is null");
257+
debug_assert!(!ptr.is_null(), "append_dates: element_offset pointer is null");
248258
for idx in 0..num_elements {
249259
let word_idx = idx >> 6;
250260
let bit_idx = idx & 0x3f;
@@ -262,6 +272,7 @@ impl SparkUnsafeArray {
262272
}
263273
} else {
264274
// SAFETY: element_offset points to contiguous i32 data of length num_elements
275+
debug_assert!(self.element_offset != 0, "append_dates: element_offset is null");
265276
let slice = unsafe {
266277
std::slice::from_raw_parts(self.element_offset as *const i32, num_elements)
267278
};

native/core/src/execution/shuffle/spark_unsafe/row.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,8 @@ fn append_nested_struct_fields_field_major(
726726
macro_rules! read_row_at {
727727
($row:expr, $row_addresses_ptr:expr, $row_sizes_ptr:expr, $i:expr) => {{
728728
// SAFETY: Caller guarantees pointers are valid for this index (see macro doc)
729+
debug_assert!(!$row_addresses_ptr.is_null(), "read_row_at: null row_addresses_ptr");
730+
debug_assert!(!$row_sizes_ptr.is_null(), "read_row_at: null row_sizes_ptr");
729731
let row_addr = unsafe { *$row_addresses_ptr.add($i) };
730732
let row_size = unsafe { *$row_sizes_ptr.add($i) };
731733
$row.point_to(row_addr, row_size);

0 commit comments

Comments
 (0)