pytorch / pytorch/executorch

_load_for_executorch_from_buffer doesn't keep buffer alive

Open
#10,725 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

module: extension triaged
Dominant language
Python
Stars
5k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
581

Description

🐛 Describe the bug

When using _load_for_executorch_from_buffer, the python bindings do not appear to maintain a reference to the buffer passed in, but native code does. This means that if the buffer is GC'd, the native code will still use it. Python doesn't know that it's still a live reference.

It's likely that need to do something in the pybind or native code to mark that there are native references into they python buffer object. I have not tested this with the new pybind APIs, but I believe they use the same calls under the hood.

Repro:

def load_model(file_path):
    with pathmgr.open(
        file_path,
        "rb",
    ) as f:
        buffer = f.read()
        et_model = _load_for_executorch_from_buffer(buffer)
    return et_model

et_model = load_model(model_file_path)
output = et_model(inputs)

Wait a second or two, then run the model again...

et_model(inputs)

It may crash. ASAN will reliably catch the use after free.

Versions

N/A

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the _load_for_executorch_from_buffer Python binding and trace the pybind or native ownership handling described in the report. Reproduce the delayed second inference with ASAN. Done means the model remains safe to use after the original buffer goes out of scope, with no use-after-free detected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
embedded-iot, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.