这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@Icenowy
Copy link
Contributor

@Icenowy Icenowy commented Dec 14, 2024

There are currently two overflow problems in adler32_rvv implementation, which can lead to wrong results for some input, and these problems could be easily exhibited when running git fsck with zlib-ng suitituting the system zlib on a big git repository.

These problems and the solutions are the following:

  • When the input data is long enough, the v_buf32_accu can overflow too. Add it to the modulo code that happens per ~NMAX bytes.
  • When the remaining data less than vl bytes, the code falls back to a scalar implementation; however the sum2 and alder2 values are just reduced from vectors and could be very big that makes sum2 overflows in the scalar code. Modulo them before the scalar code to prevent such overflow (because vl is surely quite smaller than NMAX).

Summary by CodeRabbit

  • New Features

    • Enhanced the Adler-32 checksum calculations to ensure values remain within defined bounds.
  • Bug Fixes

    • Adjusted checksum accumulation logic to prevent exceeding limits during calculations.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 14, 2024

Walkthrough

The changes in the adler32_rvv.c file enhance the handling of the Adler-32 checksum calculation for RISC-V Vector (RVV) architecture. Modifications include the introduction of additional modular arithmetic operations to ensure that the computed values for adler and sum2 remain within the defined bounds of BASE. Specifically, modulo operations are applied after the accumulation of checksum values, maintaining the function's integrity without altering the overall control flow.

Changes

File Change Summary
arch/riscv/adler32_rvv.c Added modulo operations to constrain sum2 and adler values within BASE after calculations. Updated function signatures without changes.

Suggested Labels

Build Env, Architecture

Suggested Reviewers

  • Dead2

Note: No sequence diagram was generated as the changes are relatively straightforward and do not introduce significant new control flow or interactions.

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. This feature will be included in our Pro Plan when released.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2161085 and 5fff59c.

📒 Files selected for processing (1)
  • arch/riscv/adler32_rvv.c (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • arch/riscv/adler32_rvv.c

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
arch/riscv/adler32_rvv.c (1)

113-115: LGTM: Proper bounds checking before scalar fallback

The modulo operations correctly prevent overflow when transitioning to scalar processing for remaining bytes < vl. This is essential since vector calculations can produce large intermediate values.

Consider adding a comment explaining why these modulo operations are necessary here, as it's a subtle but critical detail for maintaining numerical stability in the hybrid vector-scalar implementation.

+    /* Ensure values are within bounds before scalar processing */
     sum2 %= BASE;
     adler %= BASE;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 43d74a2 and 8a11d0c.

📒 Files selected for processing (1)
  • arch/riscv/adler32_rvv.c (2 hunks)
🔇 Additional comments (1)
arch/riscv/adler32_rvv.c (1)

75-75: LGTM: Overflow protection for v_buf32_accu

The added modulo operation correctly prevents overflow in v_buf32_accu when processing large input data blocks.

Let's verify the fix handles large inputs correctly:

✅ Verification successful

LGTM: Overflow protection for v_buf32_accu is correctly implemented

The added modulo operation is verified to be correct based on the following evidence:

  • The test suite includes extensive test cases with various input sizes, including a large 5552-byte test string
  • The fuzzer test validates correctness by comparing results with reference implementation
  • The modulo operation is correctly placed within the NMAX block check, alongside the existing v_adler32_prev_accu modulo
  • The implementation follows the standard Adler-32 algorithm requirements by using BASE as modulo value
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for test cases that verify overflow handling
rg -l "test.*adler.*overflow|test.*large.*input" test/

Length of output: 56


Script:

#!/bin/bash
# Let's try a broader search for test files related to adler32
fd "test.*adler" test/

# Also search for any test files in the RISC-V specific directory
fd "test" arch/riscv/

# Look for any test vectors or test data in the codebase
rg -i "test.*data|test.*vector" -A 5 | grep -i "adler"

Length of output: 289


Script:

#!/bin/bash
# Let's examine the test files to see how they verify adler32 implementation
cat test/test_adler32.cc

# Also check the fuzzer test to understand test data generation
cat test/fuzz/fuzzer_checksum.c

# Look for any constants or defines related to overflow prevention
rg -i "NMAX|BASE" arch/riscv/adler32_rvv.c -B 2 -A 2

Length of output: 38746

@Icenowy Icenowy force-pushed the adler32_rvv_overflow_fix branch from 8a11d0c to 2161085 Compare December 14, 2024 18:38
There are currently some overflow problems in adler32_rvv
implementation, which can lead to wrong results for some input, and
these problems could be easily exhibited when running `git fsck` with
zlib-ng suitituting the system zlib on a big git repository.

These problems and the solutions are the following:

- When the input data is long enough, the v_buf32_accu can overflow too.
  Add it to the modulo code that happens per ~NMAX bytes.
- When the vector data is reduced to scalar ones, the resulting scalar
  value (and the proceeded length) may lead to the calculation of sum2
  to overflow. Add mod BASE to all these reductions and initial
  calculation of sum2.
- When the remaining data less than vl bytes, the code falls back to a
  scalar implementation; however the sum2 and alder2 values are just
  reduced from vectors and could be very big that makes sum2 overflows
  in the scalar code. Modulo them before the scalar code to prevent such
  overflow (because vl is surely quite smaller than NMAX).

Signed-off-by: Icenowy Zheng <uwu@icenowy.me>
@Icenowy Icenowy force-pushed the adler32_rvv_overflow_fix branch from 2161085 to 5fff59c Compare December 14, 2024 18:43
@Icenowy
Copy link
Contributor Author

Icenowy commented Dec 14, 2024

Added another overflow fix about the reduction from vector to scalar.

Copy link
Member

@nmoinvaz nmoinvaz left a comment

Choose a reason for hiding this comment

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

Is it possible to add unit tests for this or is it not practical?

@Icenowy
Copy link
Contributor Author

Icenowy commented Dec 15, 2024

For the first problem, I have a 2M random .bin file that can lead to this issue being exhibited when running adler32 on it.
For the second/third problem, I think maybe carefully crafted data is needed, and I currently do not have suitable test data (I tested and found these problems by running git fsck on https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git )

@Icenowy
Copy link
Contributor Author

Icenowy commented Dec 15, 2024

For the second problem (in the commit message now), I think crafting a dataset with very long len - left (well technically this means len / vl * vl, I think), a big enough initial adler32 value (0xFFF0FFF0 ? 0xFFF0 equals to 65520) could make it easily exhibit?

Then for the third, attaching (vl - 1) 0xFF's to a data that will be calculated to very big sum2 and adler may exhibit the problems?

Well these two dataset construction methods both depend on a pre-determined vl ...

(My testing platform is SpacemiT K1, which should have VLEN=256 and the vl is 32 in this situation)

@Icenowy
Copy link
Contributor Author

Icenowy commented Dec 15, 2024

And surely the behavior between QEMU and K1 differs on these problems -- QEMU defaults to VLEN=128 (vl = 16).

@codecov
Copy link

codecov bot commented Dec 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 32.13%. Comparing base (43d74a2) to head (5fff59c).
Report is 6 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1826      +/-   ##
===========================================
- Coverage    32.25%   32.13%   -0.13%     
===========================================
  Files           67       67              
  Lines         5745     5745              
  Branches      1239     1239              
===========================================
- Hits          1853     1846       -7     
- Misses        3637     3646       +9     
+ Partials       255      253       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@Dead2 Dead2 left a comment

Choose a reason for hiding this comment

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

LGTM

@nmoinvaz nmoinvaz added bug Architecture Architecture specific labels Dec 21, 2024
@Dead2 Dead2 merged commit dbccbd1 into zlib-ng:develop Dec 21, 2024
143 of 150 checks passed
@Dead2 Dead2 mentioned this pull request Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Architecture Architecture specific bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants