Skip to content

Don't merge assignments that read the target into its object initializer - #1

Merged
oleg-st merged 1 commit into
oleg-st:masterfrom
kibertoad:fix/struct-init-stale-field-read
Sep 25, 2026
Merged

oleg-st merged 1 commit into
oleg-st:masterfrom
kibertoad:fix/struct-init-stale-field-read

Conversation

@kibertoad

Copy link
Copy Markdown
Contributor

Follow-up to oleg-st/ZstdSharp#74, as requested there.

Problem

StructInitialization folds *x = new T { ... }; x->f = expr; into the object initializer. TryRemoveDependency substitutes 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_internal in zstd_compress.c is affected:

ZSTD_memset(cctxParams, 0, sizeof(*cctxParams));
...
cctxParams->useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(cctxParams->useRowMatchFinder, &params->cParams);
cctxParams->postBlockSplitter = ZSTD_resolveBlockSplitterMode(cctxParams->postBlockSplitter, &params->cParams);

It's currently generated as:

*cctxParams = new ZSTD_CCtx_params_s
{
    ...
    useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(cctxParams->useRowMatchFinder, &@params->cParams),
    postBlockSplitter = ZSTD_resolveBlockSplitterMode(cctxParams->postBlockSplitter, &@params->cParams)
};

So a ZSTD_compressCCtx call at levels 1-4 leaves simpleApiParams.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 uses SkipLocalsInit. 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 lib before and after this change. The only difference across the whole output is this function in ZstdCompress.cs:

-                compressionLevel = compressionLevel,
-                useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(cctxParams->useRowMatchFinder, &@params->cParams),
-                postBlockSplitter = ZSTD_resolveBlockSplitterMode(cctxParams->postBlockSplitter, &@params->cParams)
+                compressionLevel = compressionLevel
             };
+            cctxParams->useRowMatchFinder = ZSTD_resolveRowMatchFinderMode(cctxParams->useRowMatchFinder, &@params->cParams);
+            cctxParams->postBlockSplitter = ZSTD_resolveBlockSplitterMode(cctxParams->postBlockSplitter, &@params->cParams);

That matches the manual change in oleg-st/ZstdSharp#74.

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.
@oleg-st

oleg-st commented Sep 25, 2026

Copy link
Copy Markdown
Owner

@kibertoad
Thank you!

@kibertoad
kibertoad deleted the fix/struct-init-stale-field-read branch September 25, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants