Repository navigation
Don't merge assignments that read the target into its object initializer - #1
Merged
oleg-st merged 1 commit intoSep 25, 2026
Merged
Conversation
StructInitialization folds `*x = new T { ... }; x->f = expr;` into the
initializer. TryRemoveDependency substituted reads of fields already in the
initializer, but other reads of the target were kept as-is. Inside the
initializer such a read sees the old value of the struct, while in C the
struct was just zeroed.
zstd_compress.c ZSTD_CCtxParams_init_internal hit this:
ZSTD_memset(cctxParams, 0, sizeof(*cctxParams));
...
cctxParams->useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(cctxParams->useRowMatchFinder, ...);
was generated as
*cctxParams = new ZSTD_CCtx_params_s {
...,
useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(cctxParams->useRowMatchFinder, ...),
postBlockSplitter = ZSTD_resolveBlockSplitterMode(cctxParams->postBlockSplitter, ...)
};
so the resolved mode from the previous call leaked into the next one (and
callers passing an uninitialized local read stack garbage). Stop merging when
the value still references the target after substitution.
Owner
|
@kibertoad |
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.
Follow-up to oleg-st/ZstdSharp#74, as requested there.
Problem
StructInitializationfolds*x = new T { ... }; x->f = expr;into the object initializer.TryRemoveDependencysubstitutes reads of fields that are already in the initializer. It keeps any other read of the target unchanged. Inside the initializer that read sees the struct's old value, while in the C source the struct was just zeroed.ZSTD_CCtxParams_init_internalinzstd_compress.cis affected:It's currently generated as:
So a
ZSTD_compressCCtxcall at levels 1-4 leavessimpleApiParams.useRowMatchFinder = ZSTD_ps_disable. Later calls on the same context at levels 5-12 then use the hash chain match finder. Their output differs from native zstd, and compression is 2-5x slower. Callers that pass an uninitialized local (ZSTD_compressBegin_advanced,ZSTD_compressBegin_usingDict_deprecated,ZSTD_compressBegin_usingCDict_internal) read stack garbage, because the module usesSkipLocalsInit. Benchmarks and a regression test are in oleg-st/ZstdSharp#74.Fix
After substitution, if the value still references the target expression, stop merging, the same way an impure dependency already stops it.
Verification
I ran the generator against zstd v1.5.7
libbefore and after this change. The only difference across the whole output is this function inZstdCompress.cs:That matches the manual change in oleg-st/ZstdSharp#74.