Skip to content

npu-pack: OnnxTensor::raw dangles after parse_onnx() returns -- inline f32 / per-layer tables would read freed memory #2668

Closed

Description

@bong-water-water-bong

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.

Activity

  1. bong-water-water-bong commented on Nov 16, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions