Skip to content

Commit 264bcb0

Browse files
committed
fix(udf): plug exception leak, add SAFETY comment, validate row count and zero-arg case
- Clear pending JNI exceptions in jthrowable_to_string fallback branches to avoid undefined behavior on thread detach - Add SAFETY comment on the from_ffi unsafe block explaining FFI initialization guarantees - Replace usize-to-i32 silent truncation with i32::try_from checked conversion for row count - Replace StructArray::new with StructArray::try_new_with_length to handle zero-arg UDFs
1 parent 9d933c4 commit 264bcb0

1 file changed

Lines changed: 34 additions & 10 deletions

File tree

‎native/src/udf.rs‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ impl ScalarUDFImpl for JavaScalarUdf {
128128
.map(|f| f.as_ref().clone())
129129
.collect::<Vec<Field>>(),
130130
);
131-
let struct_array = StructArray::new(fields, arrays, None);
131+
let struct_array = StructArray::try_new_with_length(fields, arrays, None, number_rows)
132+
.map_err(|e| DataFusionError::ArrowError(Box::new(e), None))?;
132133
let args_data = struct_array.into_data();
133134
let (args_ffi_array, args_ffi_schema) =
134135
to_ffi(&args_data).map_err(|e| DataFusionError::ArrowError(Box::new(e), None))?;
@@ -158,6 +159,13 @@ impl ScalarUDFImpl for JavaScalarUdf {
158159
// Build the jvalue argument array for call_static_method_unchecked.
159160
// SAFETY: we build the args inline and pass them immediately; the JObject
160161
// pointed to by udf_global_ref is alive for the duration of this call.
162+
let expected_rows = i32::try_from(number_rows).map_err(|_| {
163+
DataFusionError::Execution(format!(
164+
"batch row count {} exceeds i32::MAX; UDFs cannot handle batches larger than 2^31 - 1 rows",
165+
number_rows
166+
))
167+
})?;
168+
161169
let udf_jobject = self.udf_global_ref.as_obj();
162170
// SAFETY: udf_jobject is derived from a GlobalRef alive for the duration of this
163171
// function. The raw pointer is only read by the JNI call below, which happens
@@ -182,9 +190,7 @@ impl ScalarUDFImpl for JavaScalarUdf {
182190
j: result_schema_addr,
183191
},
184192
// expectedRowCount
185-
jvalue {
186-
i: number_rows as i32,
187-
},
193+
jvalue { i: expected_rows },
188194
];
189195

190196
let call_result = unsafe {
@@ -210,6 +216,10 @@ impl ScalarUDFImpl for JavaScalarUdf {
210216
// 8. Import result. from_ffi consumes the FFI_ArrowArray.
211217
let result_array = *result_array_box;
212218
let result_schema = *result_schema_box;
219+
// SAFETY: Java's `Data.exportVector` populated `result_array_box` and
220+
// `result_schema_box` in place via the C Data Interface, and the
221+
// exception check above guarantees the call succeeded without
222+
// throwing — so the FFI structs are fully initialized.
213223
let result_data = unsafe { from_ffi(result_array, &result_schema) }
214224
.map_err(|e| DataFusionError::ArrowError(Box::new(e), None))?;
215225

@@ -244,26 +254,40 @@ pub(crate) fn volatility_from_byte(byte: u8) -> datafusion::error::Result<Volati
244254
/// Anything that goes wrong collapses to a generic message so we don't
245255
/// double-throw inside an error path.
246256
fn jthrowable_to_string(env: &mut JNIEnv, throwable: &JThrowable, udf_name: &str) -> String {
247-
let class_name = (|| -> jni::errors::Result<String> {
257+
let class_name_result = (|| -> jni::errors::Result<String> {
248258
let class = env.call_method(throwable, "getClass", "()Ljava/lang/Class;", &[])?;
249259
let class_obj = class.l()?;
250260
let name = env.call_method(&class_obj, "getName", "()Ljava/lang/String;", &[])?;
251261
let name_obj = name.l()?;
252262
let name_str: String = env.get_string(&name_obj.into())?.into();
253263
Ok(name_str)
254-
})()
255-
.unwrap_or_else(|_| "<unknown exception class>".to_string());
264+
})();
265+
let class_name = match class_name_result {
266+
Ok(s) => s,
267+
Err(_) => {
268+
// A reflective call itself threw — clear that secondary exception so the
269+
// thread is in a clean state when we return to the JVM.
270+
env.exception_clear().ok();
271+
"<unknown exception class>".to_string()
272+
}
273+
};
256274

257-
let message = (|| -> jni::errors::Result<String> {
275+
let message_result = (|| -> jni::errors::Result<String> {
258276
let msg = env.call_method(throwable, "getMessage", "()Ljava/lang/String;", &[])?;
259277
let msg_obj = msg.l()?;
260278
if msg_obj.is_null() {
261279
return Ok("<no message>".to_string());
262280
}
263281
let s: String = env.get_string(&msg_obj.into())?.into();
264282
Ok(s)
265-
})()
266-
.unwrap_or_else(|_| "<no message>".to_string());
283+
})();
284+
let message = match message_result {
285+
Ok(s) => s,
286+
Err(_) => {
287+
env.exception_clear().ok();
288+
"<no message>".to_string()
289+
}
290+
};
267291

268292
format!("Java UDF '{}' threw {}: {}", udf_name, class_name, message)
269293
}

0 commit comments

Comments
 (0)