Add Valgrind memcheck (ruby_memcheck) for the C extension - #147
Open
Watson1978 wants to merge 1 commit into
Open
Add Valgrind memcheck (ruby_memcheck) for the C extension#147Watson1978 wants to merge 1 commit into
Watson1978 wants to merge 1 commit into
Conversation
Running the suite under Valgrind on a Ruby C extension is normally impractical:
the interpreter itself produces a large volume of reports that have nothing to
do with the extension. ruby_memcheck wraps Valgrind and only surfaces errors
whose stack trace passes through the extension's .so, which makes the output
usable. It is the same tool Shopify runs on nokogiri and liquid-c.
rake spec:valgrind
The task is Linux-only, since Valgrind is not usable in practice on macOS or
Windows, and the dependency carries the same guard so bundle install keeps
working there. If ruby_memcheck is not installed the task is simply not
defined, rather than breaking the Rakefile. YJIT is disabled under Valgrind
because the two interfere, adding noise and slowdown.
The suite reports nothing on the current tree, so no suppression file is
needed. Verified that the task has teeth by running it against the
use-after-free of a stream's borrowed CDict/DDict: it reports 113 Invalid read
records, correctly attributed through ZSTD_CCtx_init_compressStream2 and
ZSTD_decompressBegin_usingDDict inside zstdruby.so.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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
This adds Shopify's
ruby_memcheckso the spec suite can be run under Valgrind's memcheck:Running Valgrind against a Ruby C extension is normally impractical, because the interpreter itself produces a large volume of reports that have nothing to do with the extension.
ruby_memcheckwraps Valgrind and applies a heuristic that only surfaces errors whose stack trace passes through the extension's.so, which makes the output usable. It is the same tool Shopify runs on nokogiri and liquid-c.The task layout follows rmagick/rmagick#1716 — Linux-only guard,
valgrind: :compileprerequisite, dependency added with a platform guard.What is in this PR
ruby_memcheckas a Linux-only development dependency in theGemfile. Valgrind is not usable in practice on macOS or Windows, and theRUBY_PLATFORM.include?('linux')guard keepsbundle installworking there. It goes in theGemfilerather than the gemspec so the packaged gem stays platform-agnostic.rake spec:valgrindtask in theRakefile. It disables YJIT under Valgrind (they interfere, adding noise and slowdown) and fails early with a clear message when Valgrind is not installed. Ifruby_memcheckitself is missing, the task is simply not defined rather than breaking theRakefile.No suppression file is included: on the current tree the suite reports nothing that needs silencing.
Current result
Clean.
rake spec:valgrindexits 0 with zero reports attributed to the extension — no invalid accesses, no leaks — in about 75 seconds for the 67 examples.🤖 Generated with Claude Code