Repository navigation
Validate darts-clone trie offsets at model load - #1216
Conversation
|
Can you provide concrete examples of when this check would be necessary? |
|
The check is needed when loading model files from untrusted sources (model marketplaces, user uploads, shared pipelines). A crafted Concrete example: set unit[0] to On performance: the bounds check is a single |
|
Thank you for the details. Currently, the PR is trying to ignore errors and fall back to a generic default process. This isn't ideal because it essentially suppresses the error and can lead to unexpected behavior. Would it be possible to scan the memory image and perform a range check just once during model loading? That would be much more preferable. |
1137b1b to
5eed76e
Compare
Move bounds checking from the hot search path (exactMatchSearch, commonPrefixSearch, traverse) to a one-time validate() scan at model load. For each array unit, verify that (position ^ offset) stays within the allocated array for all 256 possible child bytes. Call validate() in Normalizer::Init() right after set_array(). If the trie contains out-of-bounds offsets the model is rejected before any query is served.
5eed76e to
0313764
Compare
|
Hi @taku910, thank you for the feedback. I reworked the PR to move all bounds checking to model load time. The new approach adds a The search methods (exactMatchSearch, commonPrefixSearch, traverse) are left unchanged. |
|
The unitest has start failing, so let us revert this PR. |
|
Hi @taku910, the unit test failure appears unrelated to this PR. The "CI for general build" workflow (which runs all unit tests) passed on all 14 platforms after the merge: ubuntu x64/arm64, macos x64/arm64, windows x64/Win32/ARM64, both internal and module builds. The only failing workflow is "Build Wheels on windows-latest", which errors out trying to build Could you point me to which specific test is failing? Happy to investigate and fix if there is a real issue. |
|
You can reproduce the test error with following command: |
|
Thank you for the detailed reproduction steps and for identifying the root cause. You were right that the pre-compiled NFKC maps have leaf units whose stored values decode to large offsets. I've submitted a fix in #1224. The issue was that |
Adds a validate() method to DoubleArrayImpl that checks whether internal node offsets reference positions within the array bounds. Leaf/value units (identified by label() > 0xFF) are skipped, since their offset field encodes the stored value rather than a child pointer. This matches the existing pattern used in open(). Called during Normalizer::Init() after set_array() to reject corrupted or malicious model files before they can trigger out-of-bounds reads during trie traversal. Fixes the test regression from #1216 where pre-compiled NFKC maps were incorrectly rejected because leaf unit values were being interpreted as child pointers.
Summary
Add a
validate()method toDoubleArrayImplthat performs a single O(n) scanof the trie array at model load, verifying that every offset-derived position
stays within the allocated array. This catches malformed trie data early
without adding any overhead to the search hot path.
darts.h: addvalidate()that checks(i ^ offset) | 0xFF < size_foreach unit with a non-zero offset
normalizer.cc: callvalidate()afterset_array(), reject the model ifthe check fails
Approach
For each unit at position
iwith offseto, children are reachable at(i ^ o) ^ cfor all byte valuescin [0, 255]. The maximum of thatexpression is
(i ^ o) | 0xFF. If that exceeds the array size, the trieis structurally invalid.
Running the check once during
Normalizer::Init()is sufficient because thearray is immutable after loading.