Skip to content

Commit d103497

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 d103497

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

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

Lines changed: 35 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,17 @@ impl SparkUnsafeArray {
168171
}
169172

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

172179
if NULLABLE {
173180
let null_words = self.null_bitset_ptr();
181+
debug_assert!(
182+
!null_words.is_null(),
183+
"append_booleans: null_bitset_ptr is null"
184+
);
174185
for idx in 0..num_elements {
175186
let word_idx = idx >> 6;
176187
let bit_idx = idx & 0x3f;
@@ -208,6 +219,14 @@ impl SparkUnsafeArray {
208219
if NULLABLE {
209220
let mut ptr = self.element_offset as *const i64;
210221
let null_words = self.null_bitset_ptr();
222+
debug_assert!(
223+
!null_words.is_null(),
224+
"append_timestamps: null_bitset_ptr is null"
225+
);
226+
debug_assert!(
227+
!ptr.is_null(),
228+
"append_timestamps: element_offset pointer is null"
229+
);
211230
for idx in 0..num_elements {
212231
let word_idx = idx >> 6;
213232
let bit_idx = idx & 0x3f;
@@ -225,6 +244,10 @@ impl SparkUnsafeArray {
225244
}
226245
} else {
227246
// SAFETY: element_offset points to contiguous i64 data of length num_elements
247+
debug_assert!(
248+
self.element_offset != 0,
249+
"append_timestamps: element_offset is null"
250+
);
228251
let slice = unsafe {
229252
std::slice::from_raw_parts(self.element_offset as *const i64, num_elements)
230253
};
@@ -245,6 +268,14 @@ impl SparkUnsafeArray {
245268
if NULLABLE {
246269
let mut ptr = self.element_offset as *const i32;
247270
let null_words = self.null_bitset_ptr();
271+
debug_assert!(
272+
!null_words.is_null(),
273+
"append_dates: null_bitset_ptr is null"
274+
);
275+
debug_assert!(
276+
!ptr.is_null(),
277+
"append_dates: element_offset pointer is null"
278+
);
248279
for idx in 0..num_elements {
249280
let word_idx = idx >> 6;
250281
let bit_idx = idx & 0x3f;
@@ -262,6 +293,10 @@ impl SparkUnsafeArray {
262293
}
263294
} else {
264295
// SAFETY: element_offset points to contiguous i32 data of length num_elements
296+
debug_assert!(
297+
self.element_offset != 0,
298+
"append_dates: element_offset is null"
299+
);
265300
let slice = unsafe {
266301
std::slice::from_raw_parts(self.element_offset as *const i32, num_elements)
267302
};

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,11 @@ 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!(
730+
!$row_addresses_ptr.is_null(),
731+
"read_row_at: null row_addresses_ptr"
732+
);
733+
debug_assert!(!$row_sizes_ptr.is_null(), "read_row_at: null row_sizes_ptr");
729734
let row_addr = unsafe { *$row_addresses_ptr.add($i) };
730735
let row_size = unsafe { *$row_sizes_ptr.add($i) };
731736
$row.point_to(row_addr, row_size);

0 commit comments

Comments
 (0)