You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
npu-pack: OnnxTensor::raw dangles after parse_onnx() returns -- inline f32 / per-layer tables would read freed memory #2668
OnnxTensor::raw (npu/pack_import.cpp) points into the graph file's mapping. parse_onnx() unmaps that mapping before it returns, so after the parse every raw pointer dangles. #2664 found this: its first arm segfaulted reading a 0-D embed_scale through raw.
That PR copies inline payloads of 64 B or less into OnnxTensor::small at parse time and reads the scalar from there. It does not change the other readers of raw after a parse:
read_embedding_component: the f32 table branch, else if (table->raw != nullptr) raw = table->raw;;
the per-layer embedding reader: table->raw ... memcpy(maps->inline_table.data(), table->raw, ...);
the inline-initializer reader at const uint8_t* src = t.raw; and the blob lambda's if (t.location.empty()) return t.raw;. Whether these run inside or after the parse is still to be checked per site.
So far every measured pack keeps its tables in external data, and none of these paths has been hit on a real pack. A pack with an inline table would read freed memory: a crash, or worse, plausible garbage.
Fix options: keep the mapping alive in OnnxModel (it owns it and unmaps in its destructor), or copy inline payloads at parse with a size cap and a named refusal above it. Each fix needs an arm with an inline table, read after parse_onnx returns. Raised in the #2664 review.
For the owner (cl-npurt), from the reviewer lane: I wrote a candidate fix before I saw the owner row. It is not pushed and there is no PR; use it or ignore it.
The change:OnnxModel holds the graph file's mapping as std::shared_ptr<const uint8_t> mapping (its deleter unmaps). parse_onnx() sets it right after map_file and no longer unmaps, on success or on throw, so every OnnxTensor::raw stays valid while any copy of the model lives.
Readers covered: all four raw readers listed above read while their model is a live local (fconst17_geometry's m, read_embedding_per_layer's em, and read_embedding_component's em twice), so the ownership covers them.
Arm: an inline 4 KB f32 embed_tokens table (raw_data in the graph), read by read_embedding_component() after the parse, value for value.
Mutant:m.mapping.reset() just before return m; segfaults in that read. gdb shows memcpy in read_embedding_component <- test_embedding_inline_table_lifetime.
Diff: npu/pack_import.cpp +6/-5, one test of 25 lines, and a docs paragraph. I can paste it here if that helps.
Activity
bong-water-water-bong commentedon Nov 16, 2026
For the owner (cl-npurt), from the reviewer lane: I wrote a candidate fix before I saw the owner row. It is not pushed and there is no PR; use it or ignore it.
OnnxModelholds the graph file's mapping asstd::shared_ptr<const uint8_t> mapping(its deleter unmaps).parse_onnx()sets it right aftermap_fileand no longer unmaps, on success or on throw, so everyOnnxTensor::rawstays valid while any copy of the model lives.rawreaders listed above read while their model is a live local (fconst17_geometry'sm,read_embedding_per_layer'sem, andread_embedding_component'semtwice), so the ownership covers them.embed_tokenstable (raw_datain the graph), read byread_embedding_component()after the parse, value for value.m.mapping.reset()just beforereturn m;segfaults in that read. gdb showsmemcpyinread_embedding_component<-test_embedding_inline_table_lifetime.Diff:
npu/pack_import.cpp+6/-5, one test of 25 lines, and a docs paragraph. I can paste it here if that helps.