Profiling: Skip class/module memsize on Ruby 4 to avoid heap profiling SIGSEGV#5938
Open
navidemad wants to merge 2 commits into
Open
Profiling: Skip class/module memsize on Ruby 4 to avoid heap profiling SIGSEGV#5938navidemad wants to merge 2 commits into
navidemad wants to merge 2 commits into
Conversation
…g SIGSEGV On Ruby 4.0, computing the memsize of a class/module/iclass walks the per-namespace class extensions (rb_class_classext_foreach -> classext_memsize -> rb_id_table_memsize) and can crash the VM with a SIGSEGV when called on objects resurrected via ObjectSpace._id2ref during heap profiling. Add T_CLASS/T_MODULE/T_ICLASS to the existing crash-path denylist in ruby_obj_memsize_of, gated behind a new NO_SAFE_CLASS_MEMSIZE define for Ruby >= 4. On older Rubies the preprocessed code is unchanged. Class objects contribute negligibly to a heap size profile. See DataDog#5936
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.
What does this PR do?
Skips computing
rb_obj_memsize_offorT_CLASS/T_MODULE/T_ICLASSobjects on Ruby 4.0+, where it can crash the VM with a SIGSEGV during heap profiling.ruby_obj_memsize_ofalready keeps a denylist ofrb_obj_memsize_ofpaths that crash the VM (e.g.T_NODE). This adds the class-like types to that denylist on Ruby 4.0+, gated by a newNO_SAFE_CLASS_MEMSIZEcompile-time define. On older Rubies the define is absent, so the preprocessed code is byte-for-byte unchanged.Motivation:
With experimental heap profiling enabled on Ruby 4.0, we saw recurring SIGSEGV crashes on production Sidekiq workers (#5936). During heap profile serialization, the recorder resurrects each tracked object via
ObjectSpace._id2refand callsrb_obj_memsize_ofon it. For class objects,rb_obj_memsize_ofwalks Ruby 4.0's per-namespace class extensions (rb_class_classext_foreach→classext_memsize→rb_id_table_memsize) and dereferences invalid classext memory, killing the process. Class objects contribute negligibly to a heap size profile, so skipping them removes the crash with no meaningful loss of signal. The full root-cause analysis is in #5936.Change log entry
Yes. Profiling: Fix a SIGSEGV crash that could happen with experimental heap profiling enabled on Ruby 4.0.
Additional Notes:
The gate is a new
NO_SAFE_CLASS_MEMSIZEdefine, set forRUBY_VERSION >= "4", following the existing extconf.rb convention for Ruby-4-specific behavior. A complete fix would also need Ruby-side hardening ofclassext_memsize/rb_class_classext_foreach, but skipping class memsize is enough to stop the crash.How to test the change?
Added a regression test in
stack_recorder_spec.rb: it tracks aClassas a live heap object and checks that the profiler reports it without crashing, with a heap-live-size of 0 on Ruby 4.0+ (and the realObjectSpace.memsize_ofon older Rubies). Verified on Linux + Ruby 4.0 (tracer-4.0): the fullstack_recorder_specpasses (64 examples, 0 failures). On Ruby < 4 the change is a no-op at the preprocessor level.