Skip to content

Commit 7401e1b

Browse files
authored
fix(ffi): prevent recursive session physical planning (apache#24492)
## Which issue does this PR close? - Closes apache#24065. ## Rationale for this change `ForeignSession::create_physical_plan` forwarded through the owning session. If that session had installed the calling library's query planner, the callback re-entered the same planner and could recurse until the stack was exhausted. The callback also returned an `FFI_ExecutionPlan`, which cannot reconstruct built-in nodes with the receiving library's Rust type identities for downcasting. The supported delegation boundary is `FFI_QueryPlanner`: the session owner exports its original planner before installing a foreign planner, and the foreign planner retains that handle. This boundary serializes physical plans and reconstructs them with local type identities. ## What changes are included in this PR? - Retain the legacy `create_physical_plan` callback at its original position and signature in `FFI_SessionRef` for DataFusion 55 ABI compatibility, while making it return an actionable `NotImplemented` error without invoking the installed planner. - Make `ForeignSession::create_physical_plan` return the same error without crossing the FFI boundary or invoking the installed planner. - Document the captured-`FFI_QueryPlanner` migration in the module documentation and DataFusion 56 upgrade guide. - Add unit and cross-library regression coverage for the rejected direct-delegation path and the successful captured-planner path. apache#24690 added the session's logical extension codec to the callback and fixed planning for logical plans that require custom codec support. It does not address the two remaining problems covered here: forwarding can re-enter the installed foreign planner, and the returned `FFI_ExecutionPlan` does not reconstruct built-in nodes with the receiving library's Rust type identities. This PR preserves the callback slot for ABI compatibility but makes direct session planning unsupported, so its callback-specific codec wiring is no longer exercised; other FFI codec work remains outside this PR. ## Are these changes tested? Yes. - The focused unit regression verifies that both `ForeignSession::create_physical_plan` and the retained legacy callback return `NotImplemented` with zero planner re-entry. - The three-library `dlopen` regression verifies that direct foreign-session delegation is rejected while the captured `FFI_QueryPlanner` route succeeds and restores local, downcastable physical-plan nodes. - `cargo test -p datafusion-ffi --features integration-tests` - `cargo clippy -p datafusion-ffi --all-targets --all-features -- -D warnings` - `cargo clippy --all-targets --all-features -- -D warnings` - `RUST_BACKTRACE=1 cargo test --profile ci --exclude datafusion-examples --exclude datafusion-benchmarks --exclude datafusion-cli --workspace --lib --tests --bins --features avro,json,backtrace,extended_tests,recursive_protection,parquet_encryption` - `RUSTDOCFLAGS="-D warnings" cargo doc -p datafusion-ffi --all-features --no-deps` - `cargo fmt --all -- --check` - `./ci/scripts/doc_prettier_check.sh` ## Are there any user-facing changes? Yes. `ForeignSession::create_physical_plan` and the retained legacy callback now return `NotImplemented`; callers must retain and invoke the session owner's exported `FFI_QueryPlanner`. The `FFI_SessionRef` callback remains at its original ABI position and signature, so this PR does not change the struct layout or require consumers to rebuild solely because of an ABI layout change.
1 parent 92746a9 commit 7401e1b

4 files changed

Lines changed: 114 additions & 88 deletions

File tree

‎datafusion/ffi/src/query_planner.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@
4444
//! C commonly wants A's built-in planning as a starting point, then rewrites the
4545
//! result. A must export its planner *before* installing C's planner on the
4646
//! session, and C must retain that handle: after the swap,
47-
//! [`Session::query_planner`] reports C's own planner, and
48-
//! [`Session::create_physical_plan`] dispatches to it, so either one is a
49-
//! self-call. Delegating to the retained handle is safe, because DataFusion's
47+
//! [`Session::query_planner`] reports C's own planner, so invoking it is a
48+
//! self-call. [`crate::session::ForeignSession::create_physical_plan`] is
49+
//! unsupported because forwarding through A's session would likewise re-enter
50+
//! C's planner. Delegating to the retained handle is safe, because DataFusion's
5051
//! built-in physical planner never re-dispatches through [`Session`].
5152
//!
5253
//! Retain the planner rather than the session. [`FFI_QueryPlanner`] owns a

‎datafusion/ffi/src/session/mod.rs‎

Lines changed: 78 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
//!
2222
//! Consider a session owned by library A that uses a query planner owned by
2323
//! library C. After A installs C's planner, [`ForeignSession::query_planner`]
24-
//! returns C's planner and [`ForeignSession::create_physical_plan`] dispatches
25-
//! to C's planner. C must not call `create_physical_plan`, or invoke the planner
26-
//! returned by `query_planner`, to delegate planning back to A. Repeating either
27-
//! self-call recurses until the stack is exhausted.
24+
//! returns C's planner. Invoking it to delegate planning back to A is a direct
25+
//! self-call. [`ForeignSession::create_physical_plan`] is deliberately
26+
//! unsupported because dispatching through A's session would likewise re-enter
27+
//! C's planner.
2828
//!
2929
//! To delegate safely, A must export its original planner before installing C's
3030
//! planner, and C must retain and invoke that planner directly. See the
@@ -40,7 +40,7 @@ use arrow_schema::ffi::FFI_ArrowSchema;
4040
use async_ffi::{FfiFuture, FutureExt};
4141
use async_trait::async_trait;
4242
use datafusion_common::config::{ConfigFileType, ConfigOptions, TableOptions};
43-
use datafusion_common::{DFSchema, DataFusionError};
43+
use datafusion_common::{DFSchema, DataFusionError, not_impl_err};
4444
use datafusion_execution::TaskContext;
4545
use datafusion_execution::config::SessionConfig;
4646
use datafusion_execution::runtime_env::RuntimeEnv;
@@ -117,6 +117,8 @@ pub(crate) struct FFI_SessionRef {
117117
logical_plan_serialized: SVec<u8>,
118118
) -> FFI_Result<SVec<u8>>,
119119

120+
/// Retained at its original position for ABI compatibility with consumers
121+
/// compiled against DataFusion 55. Direct session planning is unsupported.
120122
create_physical_plan:
121123
unsafe extern "C" fn(
122124
&Self,
@@ -239,31 +241,15 @@ unsafe extern "C" fn optimize_fn_wrapper(
239241
}
240242

241243
unsafe extern "C" fn create_physical_plan_fn_wrapper(
242-
session: &FFI_SessionRef,
243-
logical_plan_serialized: SVec<u8>,
244+
_session: &FFI_SessionRef,
245+
_logical_plan_serialized: SVec<u8>,
244246
) -> FfiFuture<FFI_Result<FFI_ExecutionPlan>> {
245-
unsafe {
246-
let runtime = session.runtime().cloned();
247-
let session = session.clone();
248-
async move {
249-
let logical_codec: Arc<dyn LogicalExtensionCodec> =
250-
(&session.logical_codec).into();
251-
let session = session.inner();
252-
let task_ctx = session.task_ctx();
253-
254-
let logical_plan =
255-
sresult_return!(logical_plan_from_bytes_with_extension_codec(
256-
logical_plan_serialized.as_slice(),
257-
task_ctx.as_ref(),
258-
logical_codec.as_ref(),
259-
));
260-
261-
let physical_plan = session.create_physical_plan(&logical_plan).await;
262-
263-
sresult!(physical_plan.map(|plan| FFI_ExecutionPlan::new(plan, runtime)))
264-
}
265-
.into_ffi()
247+
async move {
248+
sresult!(not_impl_err!(
249+
"FFI_SessionRef::create_physical_plan is unsupported; export and invoke an FFI_QueryPlanner captured before installing a foreign planner"
250+
))
266251
}
252+
.into_ffi()
267253
}
268254

269255
unsafe extern "C" fn create_physical_expr_fn_wrapper(
@@ -529,11 +515,11 @@ impl FFI_SessionRef {
529515
/// # Query planner delegation
530516
///
531517
/// If the session owner installed the current foreign query planner,
532-
/// [`Session::create_physical_plan`] dispatches back to that planner and
533518
/// [`Session::query_planner`] returns that planner. The planner must retain and
534-
/// invoke the session owner's previous planner instead of using either method to
535-
/// delegate back to the session. Otherwise, repeated delegation exhausts the
536-
/// stack. See [`crate::query_planner`] for details.
519+
/// invoke the session owner's previous planner rather than delegate back through
520+
/// the session. [`Session::create_physical_plan`] returns an error because such
521+
/// delegation would re-enter the installed planner. See [`crate::query_planner`]
522+
/// for details.
537523
#[derive(Debug)]
538524
pub struct ForeignSession {
539525
session: FFI_SessionRef,
@@ -748,24 +734,11 @@ impl Session for ForeignSession {
748734

749735
async fn create_physical_plan(
750736
&self,
751-
logical_plan: &LogicalPlan,
737+
_logical_plan: &LogicalPlan,
752738
) -> datafusion_common::Result<Arc<dyn ExecutionPlan>> {
753-
unsafe {
754-
let codec: Arc<dyn LogicalExtensionCodec> =
755-
(&self.session.logical_codec).into();
756-
let logical_plan =
757-
logical_plan_to_bytes_with_extension_codec(logical_plan, codec.as_ref())?;
758-
let physical_plan = df_result!(
759-
(self.session.create_physical_plan)(
760-
&self.session,
761-
logical_plan.as_ref().into()
762-
)
763-
.await
764-
)?;
765-
let physical_plan = <Arc<dyn ExecutionPlan>>::try_from(&physical_plan)?;
766-
767-
Ok(physical_plan)
768-
}
739+
not_impl_err!(
740+
"ForeignSession::create_physical_plan is unsupported; export and invoke an FFI_QueryPlanner captured before installing a foreign planner"
741+
)
769742
}
770743

771744
fn create_physical_expr(
@@ -859,20 +832,37 @@ mod tests {
859832
use std::sync::Arc;
860833
use std::sync::atomic::{AtomicUsize, Ordering};
861834

862-
use arrow::array::record_batch;
863835
use arrow_schema::{DataType, Field, Schema};
864-
use datafusion::catalog::{MemTable, MemoryCatalogProvider};
836+
use datafusion::catalog::MemoryCatalogProvider;
865837
use datafusion::execution::SessionStateBuilder;
866-
use datafusion_common::DataFusionError;
838+
use datafusion_common::{DataFusionError, Result, exec_err};
867839
use datafusion_expr::col;
868840
use datafusion_expr::registry::FunctionRegistry;
869841
use datafusion_proto::logical_plan::DefaultLogicalExtensionCodec;
870842

871843
use super::*;
872-
use crate::proto::physical_extension_codec::tests::TestExtensionCodec;
873844

874845
static QUERY_PLANNER_CALLS: AtomicUsize = AtomicUsize::new(0);
875846
static PHYSICAL_OPTIMIZER_CALLS: AtomicUsize = AtomicUsize::new(0);
847+
static REENTERING_PLANNER_CALLS: AtomicUsize = AtomicUsize::new(0);
848+
849+
#[derive(Debug)]
850+
struct ReenteringQueryPlanner;
851+
852+
#[async_trait]
853+
impl QueryPlanner for ReenteringQueryPlanner {
854+
async fn create_physical_plan(
855+
&self,
856+
logical_plan: &LogicalPlan,
857+
session: &dyn Session,
858+
) -> Result<Arc<dyn ExecutionPlan>> {
859+
if REENTERING_PLANNER_CALLS.fetch_add(1, Ordering::Relaxed) == 0 {
860+
session.create_physical_plan(logical_plan).await
861+
} else {
862+
exec_err!("query planner was re-entered through the session")
863+
}
864+
}
865+
}
876866

877867
unsafe extern "C" fn counting_query_planner(
878868
session: &FFI_SessionRef,
@@ -983,12 +973,6 @@ mod tests {
983973
.await?;
984974
assert_eq!(planned.name(), "EmptyExec");
985975

986-
let physical_plan = foreign_session.create_physical_plan(&logical_plan).await?;
987-
assert_eq!(
988-
format!("{physical_plan:?}"),
989-
"EmptyExec { schema: Schema { fields: [], metadata: {} }, partitions: 1, cache: PlanProperties { eq_properties: EquivalenceProperties { eq_group: EquivalenceGroup { map: {}, classes: [] }, oeq_class: OrderingEquivalenceClass { orderings: [] }, oeq_cache: OrderingEquivalenceCache { normal_cls: OrderingEquivalenceClass { orderings: [] }, leading_map: {} }, constraints: Constraints { inner: [] }, schema: Schema { fields: [], metadata: {} } }, partitioning: UnknownPartitioning(1), emission_type: Incremental, boundedness: Bounded, evaluation_type: Lazy, scheduling_type: Cooperative, output_ordering: None } }"
990-
);
991-
992976
assert_eq!(
993977
format!("{:?}", foreign_session.default_table_options()),
994978
format!("{:?}", state.default_table_options())
@@ -1015,35 +999,48 @@ mod tests {
1015999
Ok(())
10161000
}
10171001

1018-
/// `create_physical_plan` must serialize with the session's logical codec on
1019-
/// both sides of the boundary. A plan that scans a custom table provider is
1020-
/// unserializable without it.
10211002
#[tokio::test]
1022-
async fn test_create_physical_plan_uses_logical_codec() -> Result<(), DataFusionError>
1023-
{
1024-
let (ctx, task_ctx_provider) = crate::util::tests::test_session_and_ctx();
1025-
1026-
let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, true)]));
1027-
let batch = record_batch!(("a", Int32, [1, 2, 3]))?;
1028-
let table = MemTable::try_new(schema, vec![vec![batch]])?;
1029-
ctx.register_table("test_table", Arc::new(table))?;
1003+
async fn test_foreign_session_rejects_create_physical_plan() {
1004+
REENTERING_PLANNER_CALLS.store(0, Ordering::Relaxed);
10301005

1006+
let (ctx, task_ctx_provider) = crate::util::tests::test_session_and_ctx();
1007+
let state = SessionStateBuilder::new_from_existing(ctx.state())
1008+
.with_query_planner(Arc::new(ReenteringQueryPlanner))
1009+
.build();
10311010
let logical_codec = FFI_LogicalExtensionCodec::new(
1032-
Arc::new(TestExtensionCodec),
1011+
Arc::new(DefaultLogicalExtensionCodec {}),
10331012
None,
10341013
task_ctx_provider,
10351014
);
1036-
1037-
let state = ctx.state();
10381015
let local_session = FFI_SessionRef::new(&state, None, logical_codec);
1039-
let foreign_session = ForeignSession::try_from(&local_session)?;
1040-
1041-
let logical_plan = ctx.table("test_table").await?.into_optimized_plan()?;
1042-
let physical_plan = foreign_session.create_physical_plan(&logical_plan).await?;
1043-
1044-
assert_eq!(physical_plan.name(), "DataSourceExec");
1045-
assert_eq!(physical_plan.schema().field(0).name(), "a");
1016+
let foreign_session = ForeignSession::try_from(&local_session).unwrap();
1017+
1018+
let error = foreign_session
1019+
.create_physical_plan(&LogicalPlan::default())
1020+
.await
1021+
.unwrap_err();
1022+
1023+
assert_eq!(REENTERING_PLANNER_CALLS.load(Ordering::Relaxed), 0);
1024+
assert!(matches!(error, DataFusionError::NotImplemented(_)));
1025+
assert!(
1026+
error
1027+
.to_string()
1028+
.contains("export and invoke an FFI_QueryPlanner captured before")
1029+
);
10461030

1047-
Ok(())
1031+
// An already-compiled DataFusion 55 consumer calls this retained slot
1032+
// directly. It must receive the same safe failure without re-entering
1033+
// the installed planner.
1034+
let callback_error = unsafe {
1035+
(local_session.create_physical_plan)(&local_session, SVec::new())
1036+
.await
1037+
.unwrap_err()
1038+
};
1039+
assert_eq!(REENTERING_PLANNER_CALLS.load(Ordering::Relaxed), 0);
1040+
assert!(
1041+
callback_error
1042+
.as_str()
1043+
.contains("export and invoke an FFI_QueryPlanner captured before")
1044+
);
10481045
}
10491046
}

‎datafusion/ffi/src/tests/query_planner.rs‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::sync::Arc;
2121
use arrow::datatypes::{DataType, Field, Schema};
2222
use async_trait::async_trait;
2323
use datafusion_catalog::default_table_source::source_as_provider;
24-
use datafusion_common::{Result, exec_err};
24+
use datafusion_common::{DataFusionError, Result, exec_err};
2525
use datafusion_expr::LogicalPlan;
2626
use datafusion_physical_plan::ExecutionPlan;
2727
use datafusion_physical_plan::empty::EmptyExec;
@@ -115,9 +115,7 @@ impl QueryPlanner for SwappedQueryPlanner {
115115
}
116116

117117
// After the swap, the planner installed on library A's session is this
118-
// planner, so `session.query_planner()` and `session.create_physical_plan()`
119-
// are both self-references. Assert the hazard instead of triggering it:
120-
// calling either would recurse until the stack is exhausted.
118+
// planner, so `session.query_planner()` is a self-reference.
121119
let installed = session.query_planner();
122120
let installed: &dyn Any = installed.as_ref();
123121
if installed.downcast_ref::<Self>().is_none() {
@@ -126,6 +124,19 @@ impl QueryPlanner for SwappedQueryPlanner {
126124
);
127125
}
128126

127+
// Direct session delegation used to re-enter this planner recursively.
128+
// A foreign session must reject it before dispatching to the installed
129+
// planner, while the captured planner path below remains usable.
130+
let direct_error = session
131+
.create_physical_plan(logical_plan)
132+
.await
133+
.expect_err("direct foreign-session planning should be unsupported");
134+
if !matches!(direct_error, DataFusionError::NotImplemented(_)) {
135+
return exec_err!(
136+
"expected direct foreign-session planning to return NotImplemented; got {direct_error}"
137+
);
138+
}
139+
129140
// Delegate to library A. The result crosses the FFI boundary as
130141
// serialized bytes, so library C receives nodes carrying its own local
131142
// Rust type identities.

‎docs/source/library-user-guide/upgrading/56.0.0.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@
2525
in this section pertains to features and changes that have already been merged
2626
to the main branch and are awaiting release in this version.
2727

28+
### `ForeignSession::create_physical_plan` is unsupported
29+
30+
`ForeignSession::create_physical_plan` no longer forwards to the library that
31+
owns the session. It now returns a `NotImplemented` error because forwarding can
32+
re-enter an installed foreign planner, and the execution-plan handle returned by
33+
the old callback cannot restore local Rust type identities for downcasting.
34+
The original `FFI_SessionRef` callback slot remains in place for ABI compatibility
35+
with DataFusion 55 consumers, but calling that callback returns the same error.
36+
37+
The session-owning library should instead export its original planner as a
38+
`datafusion_ffi::query_planner::FFI_QueryPlanner` before installing a foreign
39+
planner. The foreign planner can retain and invoke that handle to receive a
40+
serialized physical plan reconstructed with local type identities. See the
41+
`datafusion_ffi::query_planner` module documentation for the complete delegation
42+
pattern. `ForeignSession::query_planner`, `optimize`, and `physical_optimizers`
43+
continue to forward to the owning session across the FFI boundary.
44+
2845
### `GroupColumn` now requires `values_preserving`
2946

3047
Custom implementations of the public `GroupColumn` trait must implement

0 commit comments

Comments
 (0)