fix: a staging-buffer use-after-free, plus two robustness fixes in the host-device transfer layer #3816
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
Vika-F
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_hostand theusm↔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_usm2hoststill issue a directqueue.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_hostbehaviour change.1. Use-after-free in
convert_matrix_host2devicecpp/oneapi/dal/table/backend/convert.cppconverts the source matrix row by row into a pinnedusm_hoststaging buffer, then:tmp_host_uniqueis destroyed on return, andusm_deleteris a plainsycl::freewith nosynchronization (
backend/memory.hpp), so the staging buffer is released while the copy is stillreading from it. The
.wait_and_throw()the caller applies to the returned event runs after thatdestructor, so it does not close the window.
The copy is now awaited inside the function, which is the contract
gather_device2hostandscatter_host2devicealready follow — block, then return an empty event. The single caller,convert_matrix, already waited on the result, so nothing else changes. Reached frompull_column_major_impl, i.e. a column-major table pull into device memory.2. Latent use-after-free in
scatter_host2device_blockingcpp/oneapi/dal/backend/transfer_dpc.cppends inreturn bk::wait_or_pass(events);, andwait_or_passonly waits whenevents.size() > 1— it otherwise forwards the event unwaited, whilegathered_device_uniqueis freed by the same non-synchronizing deleter. The function's own commentstates the kernels have to complete first.
The wait happens today only by accident:
The vector is sized to the block count and then
push_back-ed into, so it carries adefault-constructed event per block on top of every real one and
size()is never 1. Correcting thevector 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_hostallocated shared USM, and never aliasedcpp/oneapi/dal/backend/transfer.hpp:array<T>::empty(queue, count)defaultsalloctosycl::usm::alloc::shared, soto_hostreturned device-visible shared memory rather than host memory, the result still carried a queue, and
to_hostwas not idempotent. The allocation kind is now spelled out asalloc::host, which alsoputs the device-to-host copy into pinned memory.
to_hostalso had no fast path.to_devicealiases when the input is already device USM on the samedevice;
to_hostallocated and copied for every input carrying a queue, includingalloc::hostand
alloc::sharedinput the host can read directly. Its only caller outside tests hits exactlythat case:
svm::backend::get_unique_responsesandconvert_binary_responsespass the result ofrow_accessor::pull(queue), which defaults to shared USM. So every SVM train and infer copied theresponse column into a second shared buffer in order to read it on the host;
convert_binary_responsesdrops from four buffers to two.
The alias path drains the queue.
to_hostaccepts no dependency vector, so the discardedcopy_usm2hostsubmit-and-wait was the caller's only ordering against work in flight, andwait_and_throw()on the queue is at least as strong on both in-order and out-of-order queues.Every
to_host_syncresult in tree is bound to aconstand read only, andto_devicehas had thesame 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_sizeintransfer_dpc.cppignores both of its parameters.gather_device2host/scatter_host2deviceallocate a full-size device staging buffer and run abyte-at-a-time copy kernel unconditionally, including when the stride equals the block size and a
plain
memcpywould do.convert_vector_device2hostcallsgather_device2hosteven forsrc_stride == 1, and allocates a host staging buffer that is redundant whensrc_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 forfixes 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 onlyproduction caller of
to_host_sync/to_device_sync, so the behavioural check on fix 3.--config=host— confirms nothing outsideONEDAL_DATA_PARALLELbroke.
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
Testing
labelsandchecklistgates themselves; labels are now set and this is the checklist.Performance
queue.wait_and_throw()that on an in-order queue is what the discarded copy already implied.</details>