Repository navigation
Validate trace input ids match trace inputs in import_function - #4790
Merged
Merged
Conversation
turingH
marked this pull request as ready for review
October 29, 2026 06:14
The .mlxfn format serializes trace input ids and trace inputs as two parallel arrays. The importer loops over trace_inputs and indexes trace_input_ids with the same counter and no count check, so a crafted file that records fewer ids than inputs reads past the end of the ids vector during import; with zero recorded ids the empty vector never allocates and hashing the key dereferences its null data pointer, killing the process with SIGSEGV before the imported function is ever called. Check the two counts at the deserialization boundary and throw std::runtime_error on mismatch so a corrupt file is rejected cleanly instead of crashing.
zcbenz
force-pushed
the
fix-mlxfn-input-count-mismatch
branch
from
November 2, 2026 17:33
55532b0 to
9e96434
Compare
zcbenz
approved these changes
Nov 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
.mlxfnformat serializestrace_input_idsandtrace_inputsas twoparallel arrays. On import, the code never checks that the two lengths
match: it loops over
trace_inputs.size()and directly indexestrace_input_ids[i].With
trace_input_ids = []andtrace_inputs = [array], the very firstiteration accesses
trace_input_ids[0]on an empty vector. This happensduring the import itself — the imported function never has to be called,
and no particular model or op parameters are involved.
Root cause
The relevant code is in the import path of
mlx/export.cpp:When exporting a well-formed file the two arrays naturally have the same
length; when importing a third-party file that invariant cannot be assumed
— it must be validated before the first index. The bug triggers while
untrusted deserialized data is used to build internal object relationships,
before the imported graph is ever executed.
Fix
Validate the two counts at the deserialization boundary, before the first
lookup, so a corrupt file raises
std::runtime_error(a catchableexception from Python) instead of crashing the process:
Reproducing via the test added in this PR
The new test case
test import rejects mismatched trace input arraysbuilds the malformed file in-test from the serialized fields directly, so
no external file is needed:
Running just this case on
main(without the fix) crashes:Under ASan the crash site is exact:
The top frames are
array_map.emplace(trace_input_ids[i], trace_inputs[i]):an empty ids vector never allocated (the deserializer's
reserve(0)is ano-op), so
trace_input_ids[0]is a reference through a null data pointerand hashing the key reads from address 0. The crash happens while
deserialization builds the id-to-array map, before any of the imported
graph runs.
Behavior after the fix
The same commands pass with this PR: the malformed file is rejected at the
import boundary and existing export/import tests are unaffected.