From 0805ffc5020158e7c910f5a9e6a2d36f8327c656 Mon Sep 17 00:00:00 2001 From: Tanish Gudise Date: Tue, 27 Oct 2026 13:50:48 -0400 Subject: [PATCH 1/6] fix(cuda): bound dictionary gathers by the dictionary length Codes under null positions may hold any value, and so may corrupt input. The CUDA dictionary gathers read values[code] for every row, so such a code read outside the dictionary. Every gather now checks the code against the dictionary length and writes a zero value for a code outside it. Adds tests for out-of-range codes under nulls for string, primitive, decimal and bool values (with host-resident and device-resident codes), the dictionary the GPU fuzzer crashed on, all-null codes, codes sliced across 1024/2048 boundaries, full-range u16 and beyond-u16 dictionaries, the 12-byte inline boundary, values longer than 4 KiB and non-UTF-8 binary values. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Tanish Gudise --- vortex-cuda/benches/dynamic_dispatch_cuda.rs | 2 + vortex-cuda/kernels/src/dict.cu | 20 +- vortex-cuda/src/kernel/arrays/dict.rs | 237 +++++++++++++++++++ 3 files changed, 255 insertions(+), 4 deletions(-) diff --git a/vortex-cuda/benches/dynamic_dispatch_cuda.rs b/vortex-cuda/benches/dynamic_dispatch_cuda.rs index eda828727eb..a825c11d64b 100644 --- a/vortex-cuda/benches/dynamic_dispatch_cuda.rs +++ b/vortex-cuda/benches/dynamic_dispatch_cuda.rs @@ -634,9 +634,11 @@ mod standalone { { let mut launch = cuda_ctx.stream().launch_builder(&self.dict_fn); let codes_len = self.codes_len as u64; + let values_len = self.values_len as u64; launch.arg(&self.codes_buf); launch.arg(&codes_len); launch.arg(&self.values_alp_buf); + launch.arg(&values_len); launch.arg(&mut self.output_buf); unsafe { launch diff --git a/vortex-cuda/kernels/src/dict.cu b/vortex-cuda/kernels/src/dict.cu index a456b476821..7f92869daee 100644 --- a/vortex-cuda/kernels/src/dict.cu +++ b/vortex-cuda/kernels/src/dict.cu @@ -8,10 +8,19 @@ #include "config.cuh" #include "types.cuh" +// Codes under null positions may hold any value, and so may corrupt input, so every gather +// checks the code against the dictionary length. +template +__device__ inline bool in_dictionary(IndexT code, uint64_t values_len) { + return static_cast(code) < values_len; +} + +// A code outside the dictionary writes a zero value instead of reading past the end of `values`. template __device__ void dict_kernel(const IndexT *const __restrict codes, uint64_t codes_len, const ValueT *const __restrict values, + uint64_t values_len, ValueT *const __restrict output) { const uint32_t elements_per_block = blockDim.x * ELEMENTS_PER_THREAD; const uint64_t block_start = static_cast(blockIdx.x) * elements_per_block; @@ -20,7 +29,7 @@ __device__ void dict_kernel(const IndexT *const __restrict codes, for (uint64_t idx = block_start + threadIdx.x; idx < block_end; idx += blockDim.x) { const IndexT code = codes[idx]; - output[idx] = values[code]; + output[idx] = in_dictionary(code, values_len) ? values[code] : ValueT {}; } } @@ -39,6 +48,7 @@ template __device__ void dict_bool_kernel(const IndexT *const __restrict codes, uint64_t codes_len, const uint8_t *const __restrict values, + uint64_t values_len, uint64_t values_bit_offset, uint8_t *const __restrict output) { const uint64_t output_len = (codes_len + 7) / 8; @@ -53,7 +63,7 @@ __device__ void dict_bool_kernel(const IndexT *const __restrict codes, #pragma unroll for (uint32_t bit = 0; bit < 8; ++bit) { const uint64_t row = row_start + bit; - if (row < codes_len) { + if (row < codes_len && in_dictionary(codes[row], values_len)) { const uint64_t value_idx = values_bit_offset + static_cast(codes[row]); const uint8_t value = (values[value_idx / 8] >> (value_idx % 8)) & 1; packed |= static_cast(value << bit); @@ -69,8 +79,9 @@ __device__ void dict_bool_kernel(const IndexT *const __restrict codes, const IndexType *const __restrict codes, \ uint64_t codes_len, \ const ValueType *const __restrict values, \ + uint64_t values_len, \ ValueType *const __restrict output) { \ - dict_kernel(codes, codes_len, values, output); \ + dict_kernel(codes, codes_len, values, values_len, output); \ } // Generate dict kernel for all index types (unsigned integers) for a given value type @@ -84,9 +95,10 @@ __device__ void dict_bool_kernel(const IndexT *const __restrict codes, extern "C" __global__ void dict_bool_##index_suffix(const IndexType *const __restrict codes, \ uint64_t codes_len, \ const uint8_t *const __restrict values, \ + uint64_t values_len, \ uint64_t values_bit_offset, \ uint8_t *const __restrict output) { \ - dict_bool_kernel(codes, codes_len, values, values_bit_offset, output); \ + dict_bool_kernel(codes, codes_len, values, values_len, values_bit_offset, output); \ } // Generate fixed-width kernels for all native ptypes and decimal values. diff --git a/vortex-cuda/src/kernel/arrays/dict.rs b/vortex-cuda/src/kernel/arrays/dict.rs index aad8bc677dd..a4ea7aac0c3 100644 --- a/vortex-cuda/src/kernel/arrays/dict.rs +++ b/vortex-cuda/src/kernel/arrays/dict.rs @@ -143,6 +143,7 @@ async fn execute_dict_bool_typed( let values_view = values_device.cuda_view::()?; let codes_view = codes_device.cuda_view::()?; let codes_len_u64 = codes_len as u64; + let values_len_u64 = values_len as u64; let values_offset_u64 = values_meta.offset() as u64; let codes_ptype = I::PTYPE.to_string(); @@ -151,6 +152,7 @@ async fn execute_dict_bool_typed( args.arg(&codes_view) .arg(&codes_len_u64) .arg(&values_view) + .arg(&values_len_u64) .arg(&values_offset_u64) .arg(&mut output_slice); })?; @@ -171,6 +173,7 @@ async fn execute_dict_prim_typed VortexResult { assert!(!codes.is_empty()); let codes_len = codes.len(); + let values_len_u64 = values.len() as u64; let PrimitiveDataParts { ptype: value_ptype, @@ -202,6 +205,7 @@ async fn execute_dict_prim_typed( + codes: Vec, + valid: &[bool], + sane: T, + ) -> (PrimitiveArray, PrimitiveArray) { + let validity: Validity = valid.iter().copied().collect(); + let sane_codes: Vec = codes + .iter() + .zip(valid) + .map(|(&code, &valid)| if valid { code } else { sane }) + .collect(); + ( + PrimitiveArray::new(Buffer::from(codes), validity.clone()), + PrimitiveArray::new(Buffer::from(sane_codes), validity), + ) + } + + async fn gpu_dict_to_host( + dict: DictArray, + cuda_ctx: &mut CudaExecutionCtx, + ) -> VortexResult { + let canonical = DictExecutor.execute(dict.into_array(), cuda_ctx).await?; + Ok(canonical + .into_host(cuda_ctx.execution_ctx()) + .await? + .into_array()) + } + + async fn assert_gpu_matches( + codes: PrimitiveArray, + sane: PrimitiveArray, + values: ArrayRef, + ) -> VortexResult<()> { + let mut ctx = SESSION.create_execution_ctx(); + let mut cuda_ctx = CudaSession::create_execution_ctx(&crate::cuda_session()) + .vortex_expect("failed to create execution context"); + let dict = DictArray::try_new(codes.into_array(), values.clone())?; + let expected = DictArray::try_new(sane.into_array(), values)?; + let actual = gpu_dict_to_host(dict, &mut cuda_ctx).await?; + assert_arrays_eq!(actual, expected, &mut ctx); + Ok(()) + } + + #[crate::test] + async fn test_cuda_dict_string_out_of_range_codes_under_nulls() -> VortexResult<()> { + let values = + VarBinViewArray::from_iter_str(["a", "bb", "a string longer than twelve bytes"]); + // `u32::MAX` points 64 GiB past the views, so an unchecked gather faults. + let (codes, sane) = garbage_under_nulls( + vec![0u32, 200, 2, u32::MAX, 1], + &[true, false, true, false, true], + 0, + ); + assert_gpu_matches(codes, sane, values.into_array()).await + } + + #[crate::test] + async fn test_cuda_dict_primitive_far_out_of_range_codes_under_nulls() -> VortexResult<()> { + // These codes point gigabytes past the dictionary, so an unchecked gather faults. + let values = PrimitiveArray::new(Buffer::from(vec![10i64, 20, 30]), NonNullable); + let (codes, sane) = garbage_under_nulls( + vec![0u32, u32::MAX, 2, 0x8000_0000, 1], + &[true, false, true, false, true], + 0, + ); + assert_gpu_matches(codes, sane, values.into_array()).await + } + + #[crate::test] + async fn test_cuda_dict_decimal_out_of_range_codes_under_nulls() -> VortexResult<()> { + let values = DecimalArray::from_iter([1i128, 2, 3], DecimalDType::new(38, 0)); + let (codes, sane) = garbage_under_nulls(vec![1u32, u32::MAX, 2], &[true, false, true], 0); + assert_gpu_matches(codes, sane, values.into_array()).await + } + + #[crate::test] + async fn test_cuda_dict_bool_out_of_range_codes_under_nulls() -> VortexResult<()> { + let values = BoolArray::from_iter([true, false, true]); + let (codes, sane) = garbage_under_nulls( + vec![2u64, u64::MAX, 0, 1_000_000, 1, 9, 2, 0, 1], + &[true, false, true, false, true, false, true, true, true], + 0, + ); + assert_gpu_matches(codes, sane, values.into_array()).await + } + + /// The shape the `compress_gpu` fuzzer found (input `f6 88 10 00 00 00 00 02 41 f8 02 26 2d 0a ec + /// fe`): a one-value dictionary whose null code is 3,960,089,894. An unchecked gather reads about + /// 4 GB past the dictionary and fails with `CUDA_ERROR_ILLEGAL_ADDRESS`. + #[crate::test] + async fn test_cuda_dict_fuzz_garbage_null_code() -> VortexResult<()> { + let values = PrimitiveArray::new(Buffer::from(vec![0u8]), NonNullable); + let (codes, sane) = garbage_under_nulls(vec![0u32, 3_960_089_894], &[true, false], 0); + assert_gpu_matches(codes, sane, values.into_array()).await + } + + #[crate::test] + async fn test_cuda_dict_all_null_codes() -> VortexResult<()> { + let values = VarBinViewArray::from_iter_str(["x", "a string longer than twelve bytes"]); + let (codes, sane) = + garbage_under_nulls(vec![5_000u16, 7, u16::MAX], &[false, false, false], 0); + assert_gpu_matches(codes, sane, values.into_array()).await + } + + #[crate::test] + async fn test_cuda_dict_string_codes_sliced_across_block_boundaries() -> VortexResult<()> { + let values = VarBinViewArray::from_iter_str( + (0..50).map(|i| format!("value-{i}-{}", "x".repeat(i % 20))), + ); + let len = 3_000u16; + let valid: Vec = (0..len).map(|i| i % 11 != 0).collect(); + let raw: Vec = (0..len) + .map(|i| { + if valid[usize::from(i)] { + (i * 7) % 50 + } else { + 60_000 + } + }) + .collect(); + let (codes, sane) = garbage_under_nulls(raw, &valid, 0); + for range in [0..1023, 1023..1025, 1000..2049, 2047..2048, 1024..3000] { + let mut ctx = SESSION.create_execution_ctx(); + let mut cuda_ctx = CudaSession::create_execution_ctx(&crate::cuda_session()) + .vortex_expect("failed to create execution context"); + let sliced = codes.clone().into_array().slice(range.clone())?; + let sliced_sane = sane.clone().into_array().slice(range.clone())?; + let dict = DictArray::try_new(sliced, values.clone().into_array())?; + let expected = DictArray::try_new(sliced_sane, values.clone().into_array())?; + let actual = gpu_dict_to_host(dict, &mut cuda_ctx).await?; + assert_arrays_eq!(actual, expected, &mut ctx); + } + Ok(()) + } + + #[crate::test] + async fn test_cuda_dict_string_u16_codes_full_range() -> VortexResult<()> { + let values = VarBinViewArray::from_iter_str((0..65_536u32).map(|i| format!("v{i:05}"))); + let codes = PrimitiveArray::new( + Buffer::from(vec![0u16, u16::MAX, 32_768, 1, u16::MAX - 1]), + NonNullable, + ); + assert_gpu_matches(codes.clone(), codes, values.into_array()).await + } + + #[crate::test] + async fn test_cuda_dict_string_u32_codes_beyond_u16() -> VortexResult<()> { + let values = + VarBinViewArray::from_iter_str((0..70_000u32).map(|i| format!("dictionary-entry-{i}"))); + let codes = PrimitiveArray::new( + Buffer::from(vec![0u32, 69_999, 65_536, 12_345]), + NonNullable, + ); + assert_gpu_matches(codes.clone(), codes, values.into_array()).await + } + + #[crate::test] + async fn test_cuda_dict_string_inline_boundary_and_long_values() -> VortexResult<()> { + let long = "z".repeat(4_097); + let values = + VarBinViewArray::from_iter_str(["", "twelve-bytes", "thirteen-byte", long.as_str()]); + let codes = PrimitiveArray::new(Buffer::from(vec![3u8, 0, 1, 2, 3, 2, 1, 0]), NonNullable); + assert_gpu_matches(codes.clone(), codes, values.into_array()).await + } + + #[crate::test] + async fn test_cuda_dict_binary_non_utf8_values() -> VortexResult<()> { + let values = VarBinViewArray::from_iter_nullable_bin([ + Some(vec![0xffu8, 0xfe, 0x00]), + Some(vec![0x80]), + Some(vec![]), + None, + Some(vec![0xc3; 20]), + ]); + let (codes, sane) = garbage_under_nulls( + vec![0u8, 1, 2, 3, 4, 77, 4, 0], + &[true, true, true, true, true, false, true, true], + 0, + ); + assert_gpu_matches(codes, sane, values.into_array()).await + } + + /// Moves the codes' buffer to the device, as a file scan leaves it. + async fn codes_on_device( + codes: PrimitiveArray, + cuda_ctx: &mut CudaExecutionCtx, + ) -> VortexResult { + let handle = cuda_ctx + .ensure_on_device(codes.buffer_handle().clone()) + .await?; + Ok(PrimitiveArray::from_buffer_handle( + handle, + codes.ptype(), + codes.validity()?, + )) + } + + /// Decodes with device-resident codes, as a file scan leaves them, and copies the result back + /// to the host. + async fn assert_gpu_matches_with_device_codes( + codes: PrimitiveArray, + sane: PrimitiveArray, + values: ArrayRef, + ) -> VortexResult<()> { + let mut ctx = SESSION.create_execution_ctx(); + let mut cuda_ctx = CudaSession::create_execution_ctx(&crate::cuda_session()) + .vortex_expect("failed to create execution context"); + let device_codes = codes_on_device(codes, &mut cuda_ctx).await?; + let dict = DictArray::try_new(device_codes.into_array(), values.clone())?; + let expected = DictArray::try_new(sane.into_array(), values)?; + let actual = gpu_dict_to_host(dict, &mut cuda_ctx).await?; + assert_arrays_eq!(actual, expected, &mut ctx); + Ok(()) + } + + #[crate::test] + async fn test_cuda_dict_string_nullable_values_and_codes_device_codes() -> VortexResult<()> { + let values = VarBinViewArray::from_iter_nullable_str([Some("x"), None, Some("zz")]); + let (codes, sane) = garbage_under_nulls( + vec![0u16, 9_999, 2, 1, u16::MAX, 0], + &[true, false, true, true, false, true], + 0, + ); + assert_gpu_matches_with_device_codes(codes, sane, values.into_array()).await + } } From 630194371af631a329a69064cb5f64ed1cc7b11a Mon Sep 17 00:00:00 2001 From: Tanish Gudise Date: Tue, 27 Oct 2026 13:50:48 -0400 Subject: [PATCH 2/6] test(fuzz): fuzz GPU string dictionaries and garbage null codes The GPU fuzz target only generated dictionaries with primitive values. It now also generates dictionaries with UTF-8 or binary values, dictionaries whose nullable codes hold arbitrary values under null positions, and string arrays compressed with the CUDA preset. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Tanish Gudise --- fuzz/src/gpu/mod.rs | 122 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 117 insertions(+), 5 deletions(-) diff --git a/fuzz/src/gpu/mod.rs b/fuzz/src/gpu/mod.rs index 91e116e38cd..ff638a09832 100644 --- a/fuzz/src/gpu/mod.rs +++ b/fuzz/src/gpu/mod.rs @@ -13,9 +13,19 @@ use vortex_array::ArrayRef; use vortex_array::Canonical; use vortex_array::IntoArray; use vortex_array::VortexSessionExecute; +use vortex_array::arrays::DictArray; +use vortex_array::arrays::PrimitiveArray; +use vortex_array::arrays::arbitrary::ArbitraryArray; +use vortex_array::arrays::arbitrary::ArbitraryArrayConfig; +use vortex_array::arrays::arbitrary::ArbitraryWith; use vortex_array::arrays::dict::ArbitraryDictArray; +use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; +use vortex_array::validity::Validity; +use vortex_btrblocks::BtrBlocksCompressorBuilder; +use vortex_buffer::Buffer; +use vortex_error::VortexExpect; use vortex_error::vortex_err; use crate::SESSION; @@ -26,15 +36,24 @@ use crate::error::VortexFuzzResult; /// Which GPU-supported encoding to generate. #[derive(Debug, Clone, Copy)] pub enum GpuEncodingKind { - /// Dictionary encoding with GPU take support. + /// Dictionary encoding with primitive values. Dict, + /// Dictionary encoding with UTF-8 or binary values. + StringDict, + /// Dictionary whose nullable codes hold arbitrary values, possibly outside the dictionary, + /// under null positions. + DictGarbageNullCodes, + /// UTF-8 or binary arrays compressed with the CUDA-compatible BtrBlocks preset. + CudaCompressedStrings, } impl<'a> Arbitrary<'a> for GpuEncodingKind { fn arbitrary(u: &mut Unstructured<'a>) -> Result { - // Currently only Dict is supported - match u.int_in_range(0..=0)? { + match u.int_in_range(0..=3)? { 0 => Ok(GpuEncodingKind::Dict), + 1 => Ok(GpuEncodingKind::StringDict), + 2 => Ok(GpuEncodingKind::DictGarbageNullCodes), + 3 => Ok(GpuEncodingKind::CudaCompressedStrings), _ => unreachable!(), } } @@ -58,14 +77,107 @@ impl<'a> Arbitrary<'a> for FuzzCompressGpu { .0 .into_array() } + GpuEncodingKind::StringDict => { + let dtype = arbitrary_gpu_string_dtype(u)?; + ArbitraryDictArray::with_dtype(u, &dtype, None)? + .0 + .into_array() + } + GpuEncodingKind::DictGarbageNullCodes => arbitrary_dict_with_garbage_null_codes(u)?, + GpuEncodingKind::CudaCompressedStrings => { + // Draw rows from a small pool so the compressor sees repeated values and can + // choose a string dictionary as well as FSST, OnPair or constants. + let dtype = arbitrary_gpu_string_dtype(u)?; + let len = u.int_in_range(0..=4096)?; + let rows = ArbitraryDictArray::with_dtype(u, &dtype, Some(len))?.0; + let mut ctx = SESSION.create_execution_ctx(); + let canonical = rows + .into_array() + .execute::(&mut ctx) + .vortex_expect("canonicalize should succeed in fuzz test") + .into_array(); + BtrBlocksCompressorBuilder::from_session(&SESSION) + .only_cuda_compatible() + .build() + .compress(&canonical, &mut ctx) + .vortex_expect("BtrBlocksCompressor compress should succeed in fuzz test") + } }; Ok(FuzzCompressGpu { array }) } } +/// Generate a random UTF-8 or binary DType. +fn arbitrary_gpu_string_dtype(u: &mut Unstructured) -> Result { + let nullability: Nullability = u.arbitrary()?; + Ok(if u.arbitrary()? { + DType::Utf8(nullability) + } else { + DType::Binary(nullability) + }) +} + +/// Generate a dictionary whose codes are in range at valid positions and arbitrary, including far +/// outside the dictionary, at null positions. +fn arbitrary_dict_with_garbage_null_codes(u: &mut Unstructured) -> Result { + let dtype = if u.arbitrary()? { + arbitrary_gpu_string_dtype(u)? + } else { + arbitrary_gpu_primitive_dtype(u)? + }; + let values_len = u.int_in_range(1..=20)?; + let values = ArbitraryArray::arbitrary_with_config( + u, + &ArbitraryArrayConfig { + dtype: Some(dtype), + len: values_len..=values_len, + }, + )? + .0; + let len = u.int_in_range(0..=200)?; + let valid: Vec = (0..len).map(|_| u.arbitrary()).collect::>()?; + let values_len = u32::try_from(values_len).map_err(|_| arbitrary::Error::IncorrectFormat)?; + let validity: Validity = valid.iter().copied().collect(); + let codes = match u.int_in_range(0..=2)? { + 0 => PrimitiveArray::new( + Buffer::from(garbage_codes::(u, &valid, values_len)?), + validity, + ), + 1 => PrimitiveArray::new( + Buffer::from(garbage_codes::(u, &valid, values_len)?), + validity, + ), + _ => PrimitiveArray::new( + Buffer::from(garbage_codes::(u, &valid, values_len)?), + validity, + ), + }; + let dict = DictArray::try_new(codes.into_array(), values) + .map_err(|_| arbitrary::Error::IncorrectFormat)?; + Ok(dict.into_array()) +} + +/// Codes in `0..values_len` at valid positions and any value of `T` at null positions. +fn garbage_codes<'a, T>(u: &mut Unstructured<'a>, valid: &[bool], values_len: u32) -> Result> +where + T: Arbitrary<'a> + TryFrom, +{ + valid + .iter() + .map(|&is_valid| { + if is_valid { + T::try_from(u.int_in_range(0..=values_len - 1)?) + .map_err(|_| arbitrary::Error::IncorrectFormat) + } else { + u.arbitrary::() + } + }) + .collect() +} + /// Generate a random primitive DType suitable for GPU operations. -fn arbitrary_gpu_primitive_dtype(u: &mut Unstructured) -> Result { +fn arbitrary_gpu_primitive_dtype(u: &mut Unstructured) -> Result { let nullability: Nullability = u.arbitrary()?; let ptype = match u.int_in_range(0..=9)? { 0 => PType::U8, @@ -80,7 +192,7 @@ fn arbitrary_gpu_primitive_dtype(u: &mut Unstructured) -> Result PType::F64, _ => unreachable!(), }; - Ok(vortex_array::dtype::DType::Primitive(ptype, nullability)) + Ok(DType::Primitive(ptype, nullability)) } /// Run the GPU decompression fuzzer. From 2d3f77cc6d94077cb6a7dee242975e8ed2362699 Mon Sep 17 00:00:00 2001 From: Tanish Gudise Date: Tue, 27 Oct 2026 13:51:56 -0400 Subject: [PATCH 3/6] fix(cuda): copy encoded device-resident children to the host in into_host CanonicalCudaExt::into_host executed a child that CUDA execution left encoded on the host, with its buffers still on the device. A run-end encoded validity mask is such a child, and so is a nullable dictionary's validity, a lazy take of the values' validity through the device-resident codes. Both panicked on a host-buffer read: compress-bench --gpu-verify fails this way on CMSprovider and Euro2016 with the current CUDA preset. The child's buffers are now copied to the host before it is executed there. Children that are already canonical take the existing path, the copies run concurrently, and an array's buffers are replaced only when one of them moves. Adds tests that copy to the host a primitive array whose validity is a run-end or sparse encoded mask on the device, and string and primitive dictionaries with nullable values and device-resident codes. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Tanish Gudise --- vortex-cuda/src/canonical.rs | 186 ++++++++++++++++++++++++++++++++++- 1 file changed, 185 insertions(+), 1 deletion(-) diff --git a/vortex-cuda/src/canonical.rs b/vortex-cuda/src/canonical.rs index 138033a08ea..836f2e7544b 100644 --- a/vortex-cuda/src/canonical.rs +++ b/vortex-cuda/src/canonical.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use async_trait::async_trait; +use futures::future::BoxFuture; use futures::future::try_join_all; use vortex::array::ArrayRef; use vortex::array::Canonical; @@ -29,11 +30,17 @@ use vortex::array::validity::Validity; use vortex::buffer::BitBuffer; use vortex::buffer::Buffer; use vortex::buffer::ByteBuffer; +use vortex::error::VortexError; use vortex::error::VortexResult; use vortex::error::vortex_bail; -/// Copy a canonical child array to the host. +/// Copy a child array to the host, executing it there if CUDA execution left it encoded. async fn child_into_host(child: ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult { + let child = if child.is_canonical() { + child + } else { + buffers_into_host(child).await? + }; Ok(child .execute::(ctx)? .into_host(ctx) @@ -41,6 +48,46 @@ async fn child_into_host(child: ArrayRef, ctx: &mut ExecutionCtx) -> VortexResul .into_array()) } +/// Copies every device buffer of `array` and of its children to the host, keeping the encoding. +/// +/// A child that CUDA execution left encoded, such as a validity mask that is still a lazy `take` +/// through device-resident dictionary codes or a run-end encoded mask, can only be executed on +/// the host once all of its buffers are there. +fn buffers_into_host(array: ArrayRef) -> BoxFuture<'static, VortexResult> { + Box::pin(async move { + if array.is_host() { + return Ok(array); + } + let mut array = array; + if array.children_iter().any(|child| !child.is_host()) { + let mut slots = Vec::with_capacity(array.slots().len()); + for slot in array.slots().iter() { + slots.push(match slot { + Some(child) => Some(buffers_into_host(child.clone()).await?), + None => None, + }); + } + // SAFETY: the slots are byte-for-byte copies; only their placement changes. + array = unsafe { array.with_slots(slots.into())? }; + } + let buffers = array.buffer_handles(); + // Some encodings, such as Sparse, reject buffer replacement, so only replace when a + // buffer actually moves. + if buffers.iter().all(BufferHandle::is_on_host) { + return Ok(array); + } + let buffers = try_join_all(buffers.into_iter().map(|buffer| async move { + if buffer.is_on_host() { + return Ok::<_, VortexError>(buffer); + } + Ok(BufferHandle::new_host(buffer.try_into_host()?.await?)) + })) + .await?; + // SAFETY: the buffers are byte-for-byte copies; only their placement changes. + unsafe { array.with_buffers(buffers) } + }) +} + /// Move all canonical data from to_host from device. #[async_trait] pub trait CanonicalCudaExt { @@ -196,11 +243,19 @@ mod tests { use std::sync::LazyLock; use vortex::array::VortexSessionExecute; + use vortex::array::arrays::DictArray; + use vortex::array::assert_arrays_eq; use vortex::dtype::PType; + use vortex::encodings::runend::RunEnd; + use vortex::encodings::sparse::Sparse; + use vortex::scalar::Scalar; use vortex::session::VortexSession; use super::*; use crate::CudaSession; + use crate::executor::CudaExecute; + use crate::executor::CudaExecutionCtx; + use crate::kernel::DictExecutor; static SESSION: LazyLock = LazyLock::new(VortexSession::empty); @@ -225,4 +280,133 @@ mod tests { assert!(Canonical::List(list).into_host(&mut ctx).await.is_err()); Ok(()) } + + /// Moves every buffer of `array` and of its children to the device without decoding it. + fn encoded_on_device(array: ArrayRef, cuda_ctx: &CudaExecutionCtx) -> VortexResult { + let mut slots = Vec::new(); + for slot in array.slots().iter() { + slots.push(match slot { + Some(child) => Some(encoded_on_device(child.clone(), cuda_ctx)?), + None => None, + }); + } + // SAFETY: the slots are byte-for-byte copies; only their placement changes. + let array = unsafe { array.with_slots(slots.into())? }; + // Sparse keeps its fill value in a host buffer that it doesn't allow to be replaced. + if array.as_opt::().is_some() { + return Ok(array); + } + let mut buffers = Vec::new(); + for buffer in array.buffer_handles() { + buffers.push(cuda_ctx.ensure_on_device_sync(buffer)?); + } + // SAFETY: the buffers are byte-for-byte copies; only their placement changes. + unsafe { array.with_buffers(buffers) } + } + + /// A GPU decode can leave a validity mask encoded on the device, here run-end encoded. + /// Copying the values to the host used to execute that mask on the host while its buffers + /// were still on the device, which panicked with "expected host buffer". + #[crate::test] + async fn test_into_host_copies_encoded_device_validity() -> VortexResult<()> { + let mut cuda_ctx = CudaSession::create_execution_ctx(&crate::cuda_session())?; + let mut ctx = SESSION.create_execution_ctx(); + let mask = RunEnd::new( + PrimitiveArray::new(Buffer::from(vec![2u32, 5, 6]), Validity::NonNullable).into_array(), + BoolArray::from_iter([true, false, true]).into_array(), + &mut ctx, + ) + .into_array(); + let mask = encoded_on_device(mask, &cuda_ctx)?; + let values = cuda_ctx + .stream() + .copy_to_device(vec![1i32, 2, 3, 4, 5, 6])? + .await?; + let array = PrimitiveArray::from_buffer_handle(values, PType::I32, Validity::Array(mask)); + + let host = Canonical::Primitive(array) + .into_host(cuda_ctx.execution_ctx()) + .await? + .into_array(); + let expected = + PrimitiveArray::from_option_iter([Some(1i32), Some(2), None, None, None, Some(6)]); + assert_arrays_eq!(host, expected, &mut ctx); + Ok(()) + } + + /// A sparse validity mask keeps its fill value in a host buffer, which Sparse doesn't allow to + /// be replaced, while its patch indices and values are on the device. Only the children move + /// to the host. + #[crate::test] + async fn test_into_host_copies_sparse_device_validity() -> VortexResult<()> { + let mut cuda_ctx = CudaSession::create_execution_ctx(&crate::cuda_session())?; + let mut ctx = SESSION.create_execution_ctx(); + let mask = Sparse::try_new( + PrimitiveArray::new(Buffer::from(vec![1u32, 4]), Validity::NonNullable).into_array(), + BoolArray::from_iter([false, false]).into_array(), + 6, + Scalar::from(true), + &mut ctx, + )? + .into_array(); + let mask = encoded_on_device(mask, &cuda_ctx)?; + let values = cuda_ctx + .stream() + .copy_to_device(vec![1i32, 2, 3, 4, 5, 6])? + .await?; + let array = PrimitiveArray::from_buffer_handle(values, PType::I32, Validity::Array(mask)); + + let host = Canonical::Primitive(array) + .into_host(cuda_ctx.execution_ctx()) + .await? + .into_array(); + let expected = + PrimitiveArray::from_option_iter([Some(1i32), None, Some(3), Some(4), None, Some(6)]); + assert_arrays_eq!(host, expected, &mut ctx); + Ok(()) + } + + /// A dictionary with nullable values returns its validity as a lazy gather through the codes. + /// With the codes on the device, as a file scan leaves them, copying the decoded dictionary to + /// the host used to panic on a host-buffer read. + async fn assert_device_dict_into_host( + codes: PrimitiveArray, + values: ArrayRef, + ) -> VortexResult<()> { + let mut cuda_ctx = CudaSession::create_execution_ctx(&crate::cuda_session())?; + let mut ctx = SESSION.create_execution_ctx(); + let expected = DictArray::try_new(codes.clone().into_array(), values.clone())?; + let device_codes = encoded_on_device(codes.into_array(), &cuda_ctx)?; + let dict = DictArray::try_new(device_codes, values)?; + let host = DictExecutor + .execute(dict.into_array(), &mut cuda_ctx) + .await? + .into_host(cuda_ctx.execution_ctx()) + .await? + .into_array(); + assert_arrays_eq!(host, expected, &mut ctx); + Ok(()) + } + + #[crate::test] + async fn test_into_host_string_dict_with_nullable_values() -> VortexResult<()> { + let values = VarBinViewArray::from_iter_nullable_str([ + Some("a"), + None, + Some("a string longer than twelve bytes"), + ]); + let codes = PrimitiveArray::new( + Buffer::from(vec![0u8, 1, 2, 1, 0, 2]), + Validity::NonNullable, + ); + assert_device_dict_into_host(codes, values.into_array()).await + } + + #[crate::test] + async fn test_into_host_primitive_dict_with_nullable_values() -> VortexResult<()> { + let values = PrimitiveArray::from_option_iter([Some(7i32), None, Some(-3)]); + let codes = + PrimitiveArray::new(Buffer::from(vec![2u32, 0, 1, 1, 0]), Validity::NonNullable); + assert_device_dict_into_host(codes, values.into_array()).await + } } From 6a9da09640b50a3ff8dfd683b932f7f52619061a Mon Sep 17 00:00:00 2001 From: Tanish Gudise Date: Tue, 27 Oct 2026 05:36:51 -0400 Subject: [PATCH 4/6] feat(btrblocks): keep string dictionaries in the CUDA preset The CUDA dictionary gather decodes Dict into views over the decoded values, so the CUDA preset no longer needs to exclude StringDictScheme. Binary dictionaries stay excluded. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Tanish Gudise --- vortex-btrblocks/src/builder.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/vortex-btrblocks/src/builder.rs b/vortex-btrblocks/src/builder.rs index 9b6e9aaf7c4..9e12a955951 100644 --- a/vortex-btrblocks/src/builder.rs +++ b/vortex-btrblocks/src/builder.rs @@ -62,10 +62,12 @@ impl CompressionMode { excluded.push(binary::ZstdBuffersScheme.id()); } Self::Cuda => { - // Keep FSST, which has a CUDA decoder and direct Arrow offset-based export. Other - // string fragmentation and dictionary schemes still require unsupported decode - // paths. Delta has a CUDA decode kernel, but stays excluded until GPU delta - // decode is benchmarked against the schemes it would displace. + // Keep FSST, which has a CUDA decoder and direct Arrow offset-based export, and + // string dictionaries, which the CUDA dictionary gather decodes into views over + // the decoded values. Other string fragmentation schemes and binary dictionaries + // still require unsupported decode paths. Delta has a CUDA decode kernel, but + // stays excluded until GPU delta decode is benchmarked against the schemes it + // would displace. excluded.extend([ integer::DeltaScheme::default().id(), integer::SparseScheme.id(), @@ -74,7 +76,6 @@ impl CompressionMode { float::FloatRLEScheme.id(), float::NullDominatedSparseScheme.id(), string::NullDominatedSparseScheme.id(), - string::StringDictScheme.id(), binary::BinaryDictScheme.id(), ]); // Both binary Zstd schemes are kept. Buffer-level compression preserves binary @@ -319,4 +320,14 @@ mod tests { assert!(has_delta(builder.clone().with_compact())); assert!(!has_delta(builder.only_cuda_compatible())); } + + #[test] + fn cuda_keeps_string_dictionaries_but_not_binary_ones() { + let cuda = BtrBlocksCompressorBuilder::from_session(&SESSION) + .unrestricted() + .only_cuda_compatible(); + let has = |id: SchemeId| cuda.allowed_schemes().iter().any(|s| s.id() == id); + assert!(has(string::StringDictScheme.id())); + assert!(!has(binary::BinaryDictScheme.id())); + } } From 25b4f4472e7e7a72926fa548e17505cc1814b5fe Mon Sep 17 00:00:00 2001 From: Tanish Gudise Date: Mon, 9 Nov 2026 16:43:53 -0500 Subject: [PATCH 5/6] refactor(cuda): inline the dictionary bounds check Replace the `in_dictionary` helper with the comparison it wrapped at its two call sites, so the check reads directly in each kernel. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Tanish Gudise --- vortex-cuda/kernels/src/dict.cu | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/vortex-cuda/kernels/src/dict.cu b/vortex-cuda/kernels/src/dict.cu index 7f92869daee..db8b1caf249 100644 --- a/vortex-cuda/kernels/src/dict.cu +++ b/vortex-cuda/kernels/src/dict.cu @@ -8,14 +8,9 @@ #include "config.cuh" #include "types.cuh" -// Codes under null positions may hold any value, and so may corrupt input, so every gather -// checks the code against the dictionary length. -template -__device__ inline bool in_dictionary(IndexT code, uint64_t values_len) { - return static_cast(code) < values_len; -} - -// A code outside the dictionary writes a zero value instead of reading past the end of `values`. +// Codes under null positions may hold any value, and so may corrupt input, so every gather checks +// the code against the dictionary length. A code outside the dictionary writes a zero value instead +// of reading past the end of `values`. template __device__ void dict_kernel(const IndexT *const __restrict codes, uint64_t codes_len, @@ -29,7 +24,7 @@ __device__ void dict_kernel(const IndexT *const __restrict codes, for (uint64_t idx = block_start + threadIdx.x; idx < block_end; idx += blockDim.x) { const IndexT code = codes[idx]; - output[idx] = in_dictionary(code, values_len) ? values[code] : ValueT {}; + output[idx] = static_cast(code) < values_len ? values[code] : ValueT {}; } } @@ -63,7 +58,7 @@ __device__ void dict_bool_kernel(const IndexT *const __restrict codes, #pragma unroll for (uint32_t bit = 0; bit < 8; ++bit) { const uint64_t row = row_start + bit; - if (row < codes_len && in_dictionary(codes[row], values_len)) { + if (row < codes_len && static_cast(codes[row]) < values_len) { const uint64_t value_idx = values_bit_offset + static_cast(codes[row]); const uint8_t value = (values[value_idx / 8] >> (value_idx % 8)) & 1; packed |= static_cast(value << bit); From 2bc042b1ae2687eefccf0ff2989a46ea819b0aad Mon Sep 17 00:00:00 2001 From: Tanish Gudise Date: Mon, 9 Nov 2026 17:02:41 -0500 Subject: [PATCH 6/6] test(fuzz): keep only garbage-null-code dictionaries in this change The string-dictionary and CUDA-preset string generators in the GPU fuzz target aren't part of the bounds fix, so they move to a separate change. The fuzz target keeps the dictionaries with arbitrary codes under null positions, which exercise the bounds check, and the regression test now describes the dictionary the fuzzer found without quoting input bytes that no longer reproduce it. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Tanish Gudise --- fuzz/src/gpu/mod.rs | 38 +++------------------------ vortex-cuda/src/kernel/arrays/dict.rs | 6 ++--- 2 files changed, 6 insertions(+), 38 deletions(-) diff --git a/fuzz/src/gpu/mod.rs b/fuzz/src/gpu/mod.rs index ff638a09832..c972581d5f1 100644 --- a/fuzz/src/gpu/mod.rs +++ b/fuzz/src/gpu/mod.rs @@ -23,9 +23,7 @@ use vortex_array::dtype::DType; use vortex_array::dtype::Nullability; use vortex_array::dtype::PType; use vortex_array::validity::Validity; -use vortex_btrblocks::BtrBlocksCompressorBuilder; use vortex_buffer::Buffer; -use vortex_error::VortexExpect; use vortex_error::vortex_err; use crate::SESSION; @@ -36,24 +34,18 @@ use crate::error::VortexFuzzResult; /// Which GPU-supported encoding to generate. #[derive(Debug, Clone, Copy)] pub enum GpuEncodingKind { - /// Dictionary encoding with primitive values. + /// Dictionary encoding with GPU take support. Dict, - /// Dictionary encoding with UTF-8 or binary values. - StringDict, /// Dictionary whose nullable codes hold arbitrary values, possibly outside the dictionary, /// under null positions. DictGarbageNullCodes, - /// UTF-8 or binary arrays compressed with the CUDA-compatible BtrBlocks preset. - CudaCompressedStrings, } impl<'a> Arbitrary<'a> for GpuEncodingKind { fn arbitrary(u: &mut Unstructured<'a>) -> Result { - match u.int_in_range(0..=3)? { + match u.int_in_range(0..=1)? { 0 => Ok(GpuEncodingKind::Dict), - 1 => Ok(GpuEncodingKind::StringDict), - 2 => Ok(GpuEncodingKind::DictGarbageNullCodes), - 3 => Ok(GpuEncodingKind::CudaCompressedStrings), + 1 => Ok(GpuEncodingKind::DictGarbageNullCodes), _ => unreachable!(), } } @@ -77,31 +69,7 @@ impl<'a> Arbitrary<'a> for FuzzCompressGpu { .0 .into_array() } - GpuEncodingKind::StringDict => { - let dtype = arbitrary_gpu_string_dtype(u)?; - ArbitraryDictArray::with_dtype(u, &dtype, None)? - .0 - .into_array() - } GpuEncodingKind::DictGarbageNullCodes => arbitrary_dict_with_garbage_null_codes(u)?, - GpuEncodingKind::CudaCompressedStrings => { - // Draw rows from a small pool so the compressor sees repeated values and can - // choose a string dictionary as well as FSST, OnPair or constants. - let dtype = arbitrary_gpu_string_dtype(u)?; - let len = u.int_in_range(0..=4096)?; - let rows = ArbitraryDictArray::with_dtype(u, &dtype, Some(len))?.0; - let mut ctx = SESSION.create_execution_ctx(); - let canonical = rows - .into_array() - .execute::(&mut ctx) - .vortex_expect("canonicalize should succeed in fuzz test") - .into_array(); - BtrBlocksCompressorBuilder::from_session(&SESSION) - .only_cuda_compatible() - .build() - .compress(&canonical, &mut ctx) - .vortex_expect("BtrBlocksCompressor compress should succeed in fuzz test") - } }; Ok(FuzzCompressGpu { array }) diff --git a/vortex-cuda/src/kernel/arrays/dict.rs b/vortex-cuda/src/kernel/arrays/dict.rs index a518695c4e2..b5b866ad70e 100644 --- a/vortex-cuda/src/kernel/arrays/dict.rs +++ b/vortex-cuda/src/kernel/arrays/dict.rs @@ -1257,9 +1257,9 @@ mod tests { assert_gpu_matches(codes, sane, values.into_array()).await } - /// The shape the `compress_gpu` fuzzer found (input `f6 88 10 00 00 00 00 02 41 f8 02 26 2d 0a ec - /// fe`): a one-value dictionary whose null code is 3,960,089,894. An unchecked gather reads about - /// 4 GB past the dictionary and fails with `CUDA_ERROR_ILLEGAL_ADDRESS`. + /// The shape the `compress_gpu` fuzzer found: a one-value dictionary whose null code is + /// 3,960,089,894. An unchecked gather reads about 4 GB past the dictionary and fails with + /// `CUDA_ERROR_ILLEGAL_ADDRESS`. #[crate::test] async fn test_cuda_dict_fuzz_garbage_null_code() -> VortexResult<()> { let values = PrimitiveArray::new(Buffer::from(vec![0u8]), NonNullable);