THRIFT-6051: Limit struct read/write recursion depth in OCaml library#3556
Merged
Conversation
efefe5d to
82b706c
Compare
Client: ocaml Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
82b706c to
9626050
Compare
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
Adds a struct read/write recursion-depth limit (default 64) to the OCaml library, matching the other languages in this series.
lib/ocaml/src/Thrift.ml: addrecursion_depth_plusincrement_recursion_depth/decrement_recursion_depthtoProtocol.t; the increment raisesProtocol.E(DEPTH_LIMIT, …)once the depth exceeds 64.t_ocaml_generator.cc): wrap every generated struct reader and writer withincrement_recursion_depth; Fun.protect ~finally:decrement_recursion_depth (fun () -> …), so the counter is always restored, even when an exception unwinds. The OCaml generator snapshot test (compiler/cpp/tests/ocaml/snapshot_service_handle_ex.hpp, run byThriftCompilerTests) is updated to match the guarded output.Protocol.E (Protocol.INVALID_DATA, …)(introduced indbc1f8def) referenced theProtocolmodule from inside its own definition.exn_type/exception Eare moved ahead ofclass virtual tand the reference is unqualified.Union & exception coverage
In OCaml there is no separate union/exception codegen: the base generator routes unions through
generate_structand exceptions throughgenerate_xception, both reaching the singlegenerate_ocaml_struct_definitionreader/writer. The guard therefore applies uniformly to all three — verified in generated output (read_recTree,read_coUnion,read_coErrorand everywritecarry the identical wrapper).Test
lib/ocaml/test/test_recursion_depth.mlnow performs full read/write round-trips (not isolated counter calls) for a recursive struct, union, and exception, against an in-memory protocol:DEPTH_LIMIT;DEPTH_LIMIT.Build & run (there is no
dunein the repo; the OCaml lib pre-dates it):Result:
10 passed, 0 failed(validated against OCaml 4.13.1).Confirmed the problem exists on master: an unguarded (old-generator-style) reader accepts a 100-deep payload with no limit, whereas the guarded reader raises
DEPTH_LIMITat depth 65.Why inline types instead of
test/Recursive.thriftThe shared recursive types cannot be exercised through generated OCaml here, for two pre-existing, out-of-scope reasons (neither related to THRIFT-6051):
CoRec/CoRec2,CoUnion/CoUnion2,CoError/CoError2) as separateclassdeclarations instead ofclass … and …, so the generated module fails to compile (Unbound type constructor coRec2).TBinaryProtocol.mldoes not compile on modern OCaml (uses removed mutable-stringAPIs; the compiler is-force-safe-string).The test's hand-written types mirror the generator's exact (verified) output, and the in-memory protocol stands in for
TBinaryProtocol. There is currently no OCaml CI, so this test is run manually.🤖 Generated with Claude Code
Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com
Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com