Skip to content

Fix init/state registration correctness - #67

Merged
asonje merged 1 commit into
mainfrom
fix/init-state-registration
Aug 4, 2026
Merged

Fix init/state registration correctness#67
asonje merged 1 commit into
mainfrom
fix/init-state-registration

Conversation

@mulugetam

@mulugetam mulugetam commented Jul 28, 2026

Copy link
Copy Markdown
Contributor
  • deflateInit_, deflateInit2_, inflateInit_, inflateInit2_: call the original zlib init function first and only register stream settings on Z_OK. Previously settings were registered unconditionally, so a failed init left a stale entry keyed by a z_streamp the app may free (it never calls the matching End), and a rejected re-init overwrote the settings of a stream zlib had left valid and usable.

  • gzeof: two fixes, same root cause — reached_eof was trusted where the shim does not fully maintain it.

    • Fall through to orig_gzeof() when the file has no map entry or is on the ZLIB path. reached_eof is only maintained by the accelerator read path, so on the zlib path it stayed false forever and gzeof never reported EOF, causing infinite read loops.
    • On the accelerator path, do not report EOF while data_buf/io_buf still hold bytes gzread has not returned. reached_eof only records that a read() of the file came up short, so while (!gzeof(fp)) gzread(...) exited early and silently truncated with a success return. Measured: 4096 of 8192 bytes on an 8KB file read in 4KB chunks. gzeof now checks the same buffer state gzread already does.

Tests: two GzipFileTest cases, one per gzeof fix, each verified to fail with its hunk reverted. Full suite with QAT+IAA+IGZIP on real hardware: 28703 tests, 24383 passed, 4320 skipped, 0 failed.

Rebased onto main after #65; depends on its null guards, since conditional registration means a failed *Init leaves no map entry.

@asonje
asonje force-pushed the fix/init-state-registration branch from c7be4d4 to 3816ae6 Compare August 4, 2026 21:37
@asonje
asonje requested review from asonje and a lite review from Copilot August 4, 2026 21:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes correctness issues in the zlib interposition layer by ensuring per-stream/per-file shim state is only registered/used when zlib has actually accepted/owns that state, preventing stale map entries and incorrect EOF reporting.

Changes:

  • Make deflateInit_ / deflateInit2_ / inflateInit_ / inflateInit2_ register stream settings only when the underlying zlib init returns Z_OK.
  • Fix gzeof to defer to orig_gzeof() on the zlib path (or when unregistered), and to avoid reporting EOF while shim buffers still contain unread data.
  • Add unit tests covering both gzeof fixes and update an existing test comment to reflect the new init/registration behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
zlib_accel.cpp Registers per-stream settings only on successful init; fixes gzeof behavior for both zlib and accelerator paths.
tests/zlib_accel_test.cpp Adds GzipFileTest coverage for gzeof on zlib path and buffered-data cases; updates existing test rationale text.
Suppressed comments (1)

tests/zlib_accel_test.cpp:1531

  • Same issue here: the test updates global config and relies on reaching the bottom of the function to restore it. A failing ASSERT in the middle will leave non-default config set for subsequent tests. Add an on-scope-exit restorer right after saving the values.
  const uint32_t saved_iaa = GetConfig(USE_IAA_UNCOMPRESS);
  const uint32_t saved_qat = GetConfig(USE_QAT_UNCOMPRESS);
  const uint32_t saved_zlib = GetConfig(USE_ZLIB_UNCOMPRESS);
  const uint32_t saved_iaa_compress = GetConfig(USE_IAA_COMPRESS);
  const uint32_t saved_qat_compress = GetConfig(USE_QAT_COMPRESS);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/zlib_accel_test.cpp Outdated
Comment on lines +1470 to +1475
const uint32_t saved_iaa = GetConfig(USE_IAA_UNCOMPRESS);
const uint32_t saved_qat = GetConfig(USE_QAT_UNCOMPRESS);
const uint32_t saved_zlib = GetConfig(USE_ZLIB_UNCOMPRESS);
const uint32_t saved_iaa_compress = GetConfig(USE_IAA_COMPRESS);
const uint32_t saved_qat_compress = GetConfig(USE_QAT_COMPRESS);
const uint32_t saved_zlib_compress = GetConfig(USE_ZLIB_COMPRESS);
- deflateInit_, deflateInit2_, inflateInit_, inflateInit2_: call the
  original zlib init function first and only register stream settings in
  the map on Z_OK. Previously settings were registered unconditionally,
  so a failed init left a stale entry keyed by a z_streamp the app may
  free -- it never calls the matching End -- and a rejected re-init
  overwrote the settings of a stream zlib had left valid and usable.

- gzeof: fall through to orig_gzeof() when the file has no map entry or
  is on the ZLIB path. reached_eof is only maintained by the accelerator
  read path, so on the zlib path it stayed false forever and gzeof never
  reported EOF, causing infinite read loops.

- gzeof: on the accelerator path, do not report EOF while data_buf or
  io_buf still hold bytes gzread has not returned. reached_eof only
  records that a read() of the file came up short, so
  while (!gzeof(fp)) gzread(...) exited early and silently truncated
  with a success return: 4096 of 8192 bytes on an 8KB file read in 4KB
  chunks. gzeof now checks the same buffer state gzread already does.

Adds two GzipFileTest cases, one per gzeof fix, each verified to fail
with its hunk reverted.

Co-authored-by: Mulugeta Mammo <mulugeta.mammo@intel.com>
Signed-off-by: Mulugeta Mammo <mulugeta.mammo@intel.com>
Signed-off-by: Olasoji <olasoji.denloye@intel.com>
@asonje
asonje force-pushed the fix/init-state-registration branch from 3816ae6 to 3a34c39 Compare August 4, 2026 22:26

@asonje asonje left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@asonje
asonje merged commit f92994b into main Aug 4, 2026
6 checks passed
@asonje
asonje deleted the fix/init-state-registration branch August 4, 2026 22:28
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.

3 participants