[bug] Avoid virtual freeDataMemoryImpl calls from CSRNumericTable constructors and destructor #3769
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
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 #3769"
general
weight: 0.35
Vika-F
Description
CSRNumericTablemakes a virtual call from constructor and destructor bodies, and GCC 13 mis-analyzes it once the call chain is inlined, silently deleting the rest of the construction. Result is wrong data,std::invalid_argumentfrom unrelated code, or a segfault — depending on optimization flags.This surfaced as recurrent, flaky-looking segfaults in the oneDAL
LinuxSklearnexAzure job (definition-7,main), indaal4pyexamples (bf_knn_classification,tsne,covariance,ridge_regression). The trigger turned out to be environmental, not a oneDAL code change: a Python interpreter package rebuild changed the extension build flags from-O2to-O3 -flto, anddaal4py's extension inheritsCFLAGSfrom the interpreter'ssysconfig.The bug
CSRNumericTable's constructors callsetArrays()from their bodies, and bothsetArraysoverloads begin with a virtualfreeDataMemoryImpl()call. The destructor calls it directly.Four constructors reach
setArraysthis way (lines 677, 701, 713, 734), covering both overloads.This is legal C++: per
[class.cdtor]/4, inside the derived constructor body the dynamic type isCSRNumericTable, sofreeDataMemoryImpl()resolves toCSRNumericTable::freeDataMemoryImpl. But with-O3 -fltoGCC 13.3 inlinescreate<T>→new CSRNumericTable(...)→setArraysall into one caller, and then gets the dynamic-type analysis wrong. GCC's own-fdump-tree-fre-detailsoutput:Step by step:
1. GCC loads vtable slot 26 from
_ZTVN4daal15data_management10interface115CSRNumericTableE + 224B. Slot 26 resolves toCSRNumericTable::freeDataMemoryImpl()(confirmed fromlibonedal_corerelocations).2. Walking back from the polymorphic call, GCC finds the nearest constructor invocation — the inlined base
NumericTable::NumericTable(...)— and records the dynamic type asNumericTable.3. It looks up a
CSRNumericTabletoken-26 call on aNumericTableobject, gets an empty target list, and marks it "complete".4. A polymorphic call with a provably empty target set is unreachable, so GCC replaces it with
__builtin_unreachable().5. At
-O3that is not a trap.setArraysand the remainder of the construction are deleted:_ptr,_colIndicesand_rowOffsetsare never assigned, and control falls into adjacent code with the row count still live in a register.Step 2 is the error —
NumericTableis the dynamic type only during the base constructor, and the call happens after it returns and after the derived vptr store. GCC had the correctCSRNumericTablevtable slot in hand and overrode it with the base-constructor inference. So this is a GCC 13 miscompilation, not UB in oneDAL. It is being reported upstream separately; this PR removes oneDAL's exposure to it.The fix
Keep the constructor bodies free of virtual calls, rather than devirtualizing
setArrays().The assignment part of
setArrays()is split out into a non-virtual, protectedsetArraysImpl(), and the four constructors call that directly. They have nothing to release at that point — all threeSharedPtrmembers are default-constructed andNumericTable's constructor has already set_memStatustonotAllocated— so thefreeDataMemoryImpl()call they used to make throughsetArrays()was a no-op. Removing it is what takes theOBJ_TYPE_REFout of the constructors.setArrays()itself keeps its unqualified, fully polymorphicfreeDataMemoryImpl()call. This matters:oneapi::dal::backend::interop::host_csr_table_adapterderives fromCSRNumericTable, overridesfreeDataMemoryImpl(), and callssetArrays()from its own constructor body — where the dynamic type is the adapter and the virtual call legitimately resolves to the adapter's override. That dispatch is unchanged.The destructor keeps a qualified call. Per
[class.cdtor]/4the dynamic type during~CSRNumericTable()is alreadyCSRNumericTable, so the unqualified call resolved to the same function; qualifying it only removes theOBJ_TYPE_REFthat GCC mis-analyzes. Demonstration:The two calls in
allocateDataMemory()are deliberately left virtual — they are not on a construction path.No ABI change: no new virtual functions, no change to the class layout.
setArraysImplis a non-virtual member template.Evidence
Measured in
make_nt(the daal4py conversion function into which the whole chain inlines):| build |
__builtin_unreachableinmake_nt|setArraysinlines ||---|---|---|
|
-O3 -flto, before this PR | 12 (one per instantiated type) | 12, all bodies deleted ||
-O3 -flto -fno-strict-aliasing| 0 | intact ||
-O3 -flto, with this PR | 0 | 12, intact |Re-measured after the review rework (constructors calling
setArraysImpl,setArraysleft polymorphic): still 0__builtin_unreachableinmake_nt, and more of the construction survives than with the earlier qualified-call version — 60 CSR member stores versus 48, against 24 on unpatchedmain. The reproducer below passes 5/5; on unpatched-O3 -fltoit fails 5/5 withValueError: Unsupported NPY type 102. Full suite re-run at exact parity with the-O2baseline: legacy 1736 passed, daal4py 532 passed, onedal 1693 passed, sklearnex byte-identical.Pass bisect: 0 unreachables through
fre4(pass 176), 36 appear atfre5(pass 194, full redundancy elimination).-fno-devirtualizeor-fno-strict-aliasingalso suppress it, since both disable the analysis that feeds the fold.Flag sensitivity, before this PR —
-O3and-fltotogether are necessary and sufficient;-ffat-lto-objects,-flto-partition=noneand-fuse-linker-pluginare irrelevant:| extension build flags | daal4py examples suite |
|---|---|
|
-O2| pass ||
-O3| pass ||
-O2 -flto| pass ||
-O3 -flto| 4 failures, then SIGSEGV ||
-O3 -flto -ffat-lto-objects| 30 failures, then SIGSEGV ||
-O3 -flto -flto-partition=none| SIGSEGV |clang 18 full-LTO at
-O3is unaffected. Building the extension under ASAN masks the problem entirely (instrumentation perturbs inlining so the pattern never forms), which is why earlier sanitizer runs came back clean.Validation
Built
daal4py/sklearnexagainst a patched oneDAL header, GCC 13.3, oneDAL 2026.2.0 host build, Python 3.12.With the fix, the
daal4pyexamples suite passes under every previously-failing flag set:Full sklearnex CI suite under the
-O3 -fltoflag set, with the fix, versus the-O2baseline:| suite | with fix |
-O2baseline ||---|---|---|
| legacy | 1736 passed, 885 skipped, 1 xfailed | 1736 passed |
| daal4py | 532 passed, 32 skipped | 532 passed |
| onedal | 1693 passed, 130 skipped | 1693 passed |
| sklearnex | 252 failed, 8381 passed, 2573 errors | identical |
Exact parity, no segfault. The
sklearnexnumbers are pre-existing environment noise (sklearn 1.9.0 / numpy 2.5.2 / pandas 3.0.5 against that branch, no DPC) and are byte-identical across every flag variant including plain-O2.On whether this is inside random variation — it isn't; the reproducer is deterministic in a fresh process, 50 consecutive runs per build:
| extension build flags | unpatched
main| with this PR ||---|---|---|
|
-O3 -flto| 0 pass / 50ValueError: Unsupported NPY type 102| 50 pass / 50 ||
-O3 -flto -flto-partition=none| 0 pass / 50 SIGSEGV | 50 pass / 50 |No run in either direction crossed over. The primary discriminator is not a pass rate at all, though:
-fdump-tree-optimizedeither contains the__builtin_unreachablethat replaced the call or it doesn't, and that is a static property of the build.A single test reproduces it deterministically in a fresh process, no accumulated state needed:
Follow-ups (not in this PR)
Memory leaks are a separate defect. @david-cortes-intel reports leaks in the CSR cases under clang and ICX as well. Neither compiler ever miscompiled this pattern in my measurements — clang 18 full-LTO at
-O3passes the reproducer on unpatchedmain— so those leaks cannot be this fold, and this PR is not expected to fix them. Worth a separate issue; happy to dig into it with the ASAN report.The same pattern — constructor body calling
setArray(s), which calls virtualfreeDataMemoryImpl()— also exists inhomogen_numeric_table.h(6 call sites),soa_numeric_table.h(1) andaos_numeric_table.h(2). GCC did not fold those in the cases measured here, so I have not touched them; applying the samesetArraysImplsplit would be cheap and is worth a separate PR. All the misbehaviour observed in this suite, including the crashes in the dense-path examples, traced to the CSR path alone — those were downstream corruption from CSR tests running earlier in the same process.---
<details>
<summary>Checklist:</summary>
Completeness and readability
Testing
Pre-Commiterror is unrelated to this change (confirmed in review).LinuxSklearnexjob is the regression test and was failing onmainbefore this change.Performance
</details>