← Back to Pull requests

fix: a staging-buffer use-after-free, plus two robustness fixes in the host-device transfer layer #3816

Closed
unknown closed this pull request without merging on Oct 14, 2026 11:37 UTC
View on GitHub
unknown unknown commented on Sep 28, 2026 15:11 UTC
Author

What

Three fixes in the host↔device transfer layer: two use-after-frees of a staging buffer, and one
unnecessary allocation. They came out of an audit of copy / to_device / to_host and the
usm↔host helpers, prompted by the old "extra allocation in host-to-device transfer" bug fixed in
#2966. That one has not regressed — memcpy_host2usm / memcpy_usm2host still issue a direct
queue.submit(cgh.memcpy(...)) with no temporary — but the same two patterns survived elsewhere.

Three commits, independent of each other, so the correctness fixes can be taken without the
to_host behaviour change.

1. Use-after-free in convert_matrix_host2device

cpp/oneapi/dal/table/backend/convert.cpp converts the source matrix row by row into a pinned
usm_host staging buffer, then:

auto copy_event = memcpy(q, dst_device, tmp_host_unique.get(), dst_size_in_bytes);
return copy_event;                     // asynchronous overload

tmp_host_unique is destroyed on return, and usm_deleter is a plain sycl::free with no
synchronization (backend/memory.hpp), so the staging buffer is released while the copy is still
reading from it. The .wait_and_throw() the caller applies to the returned event runs after that
destructor, so it does not close the window.

The copy is now awaited inside the function, which is the contract gather_device2host and
scatter_host2device already follow — block, then return an empty event. The single caller,
convert_matrix, already waited on the result, so nothing else changes. Reached from
pull_column_major_impl, i.e. a column-major table pull into device memory.

2. Latent use-after-free in scatter_host2device_blocking

cpp/oneapi/dal/backend/transfer_dpc.cpp ends in return bk::wait_or_pass(events);, and
wait_or_pass only waits when events.size() > 1 — it otherwise forwards the event unwaited, while
gathered_device_unique is freed by the same non-synchronizing deleter. The function's own comment
states the kernels have to complete first.

The wait happens today only by accident:

std::vector<sycl::event> events(blocking.get_block_count());   // N default-constructed events
...
    events.push_back(scatter_event);                           // ends up 2N long

The vector is sized to the block count and then push_back-ed into, so it carries a
default-constructed event per block on top of every real one and size() is never 1. Correcting the
vector on its own would turn the single-block case into a real use-after-free, which is why both
halves move in one commit: the vector is reserved rather than sized, and the wait is unconditional.

3. to_host allocated shared USM, and never aliased

cpp/oneapi/dal/backend/transfer.hpp:

const auto ary_host = array<T>::empty(q, ary.get_count());

array<T>::empty(queue, count) defaults alloc to sycl::usm::alloc::shared, so to_host
returned device-visible shared memory rather than host memory, the result still carried a queue, and
to_host was not idempotent. The allocation kind is now spelled out as alloc::host, which also
puts the device-to-host copy into pinned memory.

to_host also had no fast path. to_device aliases when the input is already device USM on the same
device; to_host allocated and copied for every input carrying a queue, including alloc::host
and alloc::shared input the host can read directly. Its only caller outside tests hits exactly
that case: svm::backend::get_unique_responses and convert_binary_responses pass the result of
row_accessor::pull(queue), which defaults to shared USM. So every SVM train and infer copied the
response column into a second shared buffer in order to read it on the host; convert_binary_responses
drops from four buffers to two.

The alias path drains the queue. to_host accepts no dependency vector, so the discarded
copy_usm2host submit-and-wait was the caller's only ordering against work in flight, and
wait_and_throw() on the queue is at least as strong on both in-order and out-of-order queues.

Every to_host_sync result in tree is bound to a const and read only, and to_device has had the
same aliasing semantics all along, so callers already tolerate it.

Found but not fixed here

Reported for completeness; none of these is a correctness issue and each wants its own change:

propose_block_size in transfer_dpc.cpp ignores both of its parameters.
gather_device2host / scatter_host2device allocate a full-size device staging buffer and run a
byte-at-a-time copy kernel unconditionally, including when the stride equals the block size and a
plain memcpy would do. convert_vector_device2host calls gather_device2host even for
src_stride == 1, and allocates a host staging buffer that is redundant when
src_type == dst_type.

Validation

--config=dpc, all pass:

//cpp/oneapi/dal/table:test_convert_dpc, test_homogen_dpc, test_row_accessor_dpc,
test_csr_accessor_dpc, test_table_builder_dpc, test_common_dpc — the direct blast radius for
fixes 1 and 2, covering pull and push across host/device and row/column-major combinations.
//cpp/oneapi/dal/algo/svm:test_batch_dpc, test_badargs_dpc, test_serialization_dpc — the only
production caller of to_host_sync / to_device_sync, so the behavioural check on fix 3.

  • The same table targets with --config=host — confirms nothing outside ONEDAL_DATA_PARALLEL

  • broke.

    Neither use-after-free can be asserted by a test: there is no sanitizer configuration in the repo
    and the freed-buffer window is timing dependent. Both fixes are argued from destruction order, which
    is why the commit messages carry the reasoning.

    ---

    <details>
    <summary>Checklist:</summary>

    Completeness and readability

  • I have commented my code, particularly in hard-to-understand areas.

  • I have updated the documentation to reflect the changes or created a separate PR with updates and provided its number in the description, if necessary.

  • Git commit message contains an appropriate signed-off-by string _(see CONTRIBUTING.md for details)_.

  • I have resolved any merge conflicts that might occur with the base branch.
  • Testing

  • I have run it locally and tested the changes extensively.

  • All CI jobs are green or I have provided justification why they aren't — the only two reds were the labels and checklist gates themselves; labels are now set and this is the checklist.

  • I have extended testing suite if new functionality was introduced in this PR — no new functionality; neither use-after-free is assertable without a sanitizer build, see above.
  • Performance

  • I have measured performance for affected algorithms using scikit-learn_bench and provided at least a summary table with measured data, if performance change is expected — not measured, no measurable change expected; see the justification below.

  • I have provided justification why performance and/or quality metrics have changed or why changes are not expected: fixes 1 and 2 move an existing wait earlier without adding one. Fix 3 removes one allocation and one device-to-host copy of a single response column per SVM train and infer, which is below benchmark noise, and adds a queue.wait_and_throw() that on an in-order queue is what the discarded copy already implied.

  • I have extended the benchmarking suite and provided a corresponding scikit-learn_bench PR if new measurable functionality was introduced in this PR.
  • </details>

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: Vika-F Review duration: 22825m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3816"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%