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

Conversation

@Dead2
Copy link
Member

@Dead2 Dead2 commented Sep 25, 2024

  • Stop updating 'dmax' and 'sane' and remove the variables from 'inflate_state' unless the checks are compiled in.
  • Add a variable 'wbufsize' to track the real size of the window buffer, including padding. This allows chunkset code to spill garbage writes into the padding area if it is available. It is not available for inflateBack unfortunately.
  • Reorder the 'inflate_state' struct to improve cache-locality of the variables needed by inffast (reduced from 6 to 1 cacheline)
    Also fills in some unnecessary holes.

On x86-64, default settings, inflate_state also shrinks from 9216 to 9152 bytes and now ends cleanly on a cacheline boundary.

@Dead2 Dead2 added enhancement cleanup Improving maintainability or removing code. labels Sep 25, 2024
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 25, 2024

Walkthrough

The changes involve modifications to the inflation process within the compression library. Key updates include conditional initialization of state variables based on compilation flags, restructuring of the inflate_state structure, and adjustments to distance validation logic. New fields have been added to enhance functionality, while existing fields have been reorganized for clarity. The overall approach now allows for more flexible configurations and improved error handling during the inflation process.

Changes

Files Change Summary
infback.c, inflate.c Modifications to state variable initialization (dmax, sane) based on compilation flags. Introduction of a new field wbufsize in infback.c. Adjustments to logic for handling invalid distances.
inffast_tpl.h Changes to inflate_fast function, updating distance value handling to use state->wbufsize and state->dmax. Improvements to memory operation safety checks and handling of invalid distances.
inflate.h Modifications to inflate_state structure, adding new fields like was, wbufsize, lenbits, lencode, distcode, and distbits. Reorganization of fields for clarity and potential memory alignment improvements.

Possibly related PRs

  • Compute the "safe" distance properly #1801: The changes in inffast_tpl.h regarding the handling of distance values and memory operations are related to the modifications in the main PR, which also involve the inflate_fast function and the management of distance calculations using state->wbufsize and state->dmax.

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 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.

@Dead2
Copy link
Member Author

Dead2 commented Sep 25, 2024

x86-64 (i9-9900K) 2.2.2

Level   Comp   Comptime min/avg/max/stddev  Decomptime min/avg/max/stddev  Compressed size
1     54.185%      0.104/0.109/0.111/0.002        0.030/0.035/0.038/0.002        8,526,745
2     43.871%      0.198/0.203/0.204/0.001        0.032/0.036/0.038/0.002        6,903,702
3     42.390%      0.247/0.250/0.253/0.002        0.032/0.035/0.036/0.001        6,670,664
4     41.644%      0.275/0.280/0.282/0.002        0.031/0.034/0.035/0.001        6,553,205
5     41.215%      0.303/0.308/0.310/0.002        0.031/0.034/0.035/0.001        6,485,659
6     41.032%      0.348/0.355/0.359/0.003        0.029/0.033/0.035/0.002        6,456,912
7     40.778%      0.496/0.505/0.509/0.003        0.031/0.033/0.035/0.001        6,416,941
8     40.704%      0.613/0.619/0.624/0.003        0.032/0.033/0.035/0.001        6,405,249
9     40.409%      0.906/0.911/0.915/0.003        0.030/0.033/0.035/0.001        6,358,951

avg1  42.914%                        0.393                          0.034
tot                                106.189                          9.189       60,778,028

  text    data     bss     dec     hex filename
130774    1312       8  132094   203fe libz-ng.so.2

         5,521.85 msec task-clock:u                     #    1.000 CPUs utilized               ( +-  0.03% )
                0      context-switches:u               #    0.000 /sec
                0      cpu-migrations:u                 #    0.000 /sec
              144      page-faults:u                    #   26.078 /sec                        ( +-  0.39% )
   19,093,810,958      cycles:u                         #    3.458 GHz                         ( +-  0.03% )  (62.49%)
   35,726,573,927      instructions:u                   #    1.87  insn per cycle              ( +-  0.01% )  (74.99%)
    3,758,372,000      branches:u                       #  680.636 M/sec                       ( +-  0.02% )  (74.99%)
      174,901,372      branch-misses:u                  #    4.65% of all branches             ( +-  0.03% )  (74.99%)
    6,865,961,607      L1-dcache-loads:u                #    1.243 G/sec                       ( +-  0.02% )  (74.99%)
      233,340,453      L1-dcache-load-misses:u          #    3.40% of all L1-dcache accesses   ( +-  0.07% )  (75.01%)
       18,378,169      LLC-loads:u                      #    3.328 M/sec                       ( +-  1.58% )  (50.01%)
              144      LLC-load-misses:u                #    0.00% of all LL-cache accesses    ( +- 20.28% )  (50.00%)

x86-64 Inflate cleanup PR

 Level   Comp   Comptime min/avg/max/stddev  Decomptime min/avg/max/stddev  Compressed size
 1     54.185%      0.105/0.109/0.111/0.002        0.030/0.036/0.038/0.002        8,526,745
 2     43.871%      0.194/0.197/0.198/0.001        0.033/0.036/0.037/0.001        6,903,702
 3     42.390%      0.240/0.244/0.246/0.002        0.030/0.034/0.036/0.002        6,670,664
 4     41.644%      0.270/0.275/0.277/0.002        0.029/0.033/0.035/0.001        6,553,205
 5     41.215%      0.293/0.299/0.302/0.002        0.031/0.034/0.035/0.001        6,485,659
 6     41.032%      0.341/0.348/0.352/0.003        0.029/0.033/0.035/0.002        6,456,912
 7     40.778%      0.491/0.501/0.507/0.004        0.031/0.033/0.035/0.001        6,416,941
 8     40.704%      0.609/0.616/0.621/0.003        0.031/0.033/0.035/0.001        6,405,249
 9     40.409%      0.904/0.911/0.917/0.003        0.029/0.033/0.034/0.001        6,358,951

 avg1  42.914%                        0.389                          0.034
 tot                                104.973                          9.172       60,778,028

   text    data     bss     dec     hex filename
 130102    1312       8  131422   2015e libz-ng.so.2

          5,518.07 msec task-clock:u                     #    1.000 CPUs utilized               ( +-  0.02% )
                 0      context-switches:u               #    0.000 /sec
                 0      cpu-migrations:u                 #    0.000 /sec
               144      page-faults:u                    #   26.096 /sec                        ( +-  0.37% )
    19,068,549,752      cycles:u                         #    3.456 GHz                         ( +-  0.03% )  (62.47%)
    35,717,171,689      instructions:u                   #    1.87  insn per cycle              ( +-  0.01% )  (74.98%)
     3,758,323,631      branches:u                       #  681.094 M/sec                       ( +-  0.02% )  (74.98%)
       175,939,466      branch-misses:u                  #    4.68% of all branches             ( +-  0.06% )  (75.00%)
     6,864,926,063      L1-dcache-loads:u                #    1.244 G/sec                       ( +-  0.03% )  (75.02%)
       232,739,294      L1-dcache-load-misses:u          #    3.39% of all L1-dcache accesses   ( +-  0.07% )  (75.00%)
        18,475,946      LLC-loads:u                      #    3.348 M/sec                       ( +-  0.95% )  (50.00%)
               124      LLC-load-misses:u                #    0.00% of all LL-cache accesses    ( +- 24.54% )  (49.99%)

Rpi5b 2.2.2

 Level   Comp   Comptime min/avg/max/stddev  Decomptime min/avg/max/stddev  Compressed size
 0    100.008%      0.000/0.000/0.000/0.000        0.000/0.000/0.003/0.001       15,737,543
 1     54.185%      0.096/0.109/0.112/0.004        0.028/0.041/0.047/0.005        8,526,745
 2     43.871%      0.184/0.192/0.195/0.003        0.027/0.040/0.044/0.004        6,903,702
 3     42.390%      0.216/0.230/0.236/0.004        0.031/0.040/0.045/0.003        6,670,664
 4     41.644%      0.250/0.262/0.266/0.003        0.024/0.034/0.040/0.004        6,553,205
 5     41.215%      0.276/0.286/0.290/0.004        0.020/0.036/0.040/0.005        6,485,659
 6     41.032%      0.323/0.336/0.340/0.004        0.024/0.036/0.040/0.004        6,456,912
 7     40.778%      0.428/0.446/0.451/0.005        0.028/0.037/0.040/0.003        6,416,941
 8     40.704%      0.562/0.568/0.573/0.002        0.020/0.037/0.040/0.004        6,405,249
 9     40.409%      0.679/0.684/0.688/0.002        0.027/0.037/0.042/0.003        6,358,951

 avg1  48.624%                        0.311                          0.034
 avg2  54.026%                        0.346                          0.038
 tot                                124.488                         13.537       76,515,571

   text    data     bss     dec     hex filename
 109760    1504       8  111272   1b2a8 libz-ng.so.2

Rpi5b Inflate cleanup PR

 Level   Comp   Comptime min/avg/max/stddev  Decomptime min/avg/max/stddev  Compressed size
 0    100.008%      0.000/0.000/0.000/0.000        0.000/0.000/0.000/0.000       15,737,543
 1     54.185%      0.096/0.109/0.112/0.004        0.031/0.040/0.043/0.003        8,526,745
 2     43.871%      0.184/0.192/0.197/0.004        0.025/0.039/0.042/0.004        6,903,702
 3     42.390%      0.226/0.232/0.234/0.002        0.032/0.039/0.044/0.003        6,670,664
 4     41.644%      0.254/0.262/0.266/0.003        0.021/0.035/0.039/0.004        6,553,205
 5     41.215%      0.281/0.288/0.293/0.003        0.030/0.037/0.042/0.003        6,485,659
 6     41.032%      0.319/0.338/0.342/0.004        0.031/0.036/0.039/0.003        6,456,912
 7     40.778%      0.436/0.449/0.452/0.004        0.023/0.034/0.039/0.004        6,416,941
 8     40.704%      0.558/0.570/0.574/0.003        0.020/0.035/0.039/0.005        6,405,249
 9     40.409%      0.681/0.689/0.692/0.003        0.025/0.034/0.038/0.003        6,358,951

 avg1  48.624%                        0.313                          0.033
 avg2  54.026%                        0.348                          0.037
 tot                                125.146                         13.153       76,515,571

   text    data     bss     dec     hex filename
 109496    1504       8  111008   1b1a0 libz-ng.so.2

TLDR;
x86-64 deflatebench: 0.15% speedup
x86-64 perf: 0.069% speedup
Codesize reduced by 672 bytes

Aarch64 deflatebench: 2.83% speedup (Probably just hit a sweet-spot and not reliable)
Codesize reduced by 264 bytes

All in all it seems likely to be a minor speedup along with a slight reduction of codesize.
It should also make it easier to gain more speed from chunkset code improvements later.

@codecov
Copy link

codecov bot commented Sep 25, 2024

Codecov Report

Attention: Patch coverage is 12.50000% with 7 lines in your changes missing coverage. Please review.

Project coverage is 33.27%. Comparing base (3297953) to head (e128eb0).
Report is 3 commits behind head on develop.

Files with missing lines Patch % Lines
inflate.c 0.00% 4 Missing ⚠️
inffast_tpl.h 0.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1797      +/-   ##
===========================================
+ Coverage    33.18%   33.27%   +0.09%     
===========================================
  Files           66       66              
  Lines         5490     5481       -9     
  Branches      1225     1222       -3     
===========================================
+ Hits          1822     1824       +2     
  Misses        3407     3407              
+ Partials       261      250      -11     

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

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: 5

🧹 Outside diff range and nitpick comments (2)
inflate.h (2)

108-108: Consider renaming the was variable for clarity

The variable was may not be immediately descriptive. Consider renaming it to something more meaningful like initial_match_length to improve code readability.


112-112: Clarify the comment for the back variable

The comment bits back of last unprocessed length/lit could be clearer. Rewriting it as Number of bits of the last unprocessed length/literal code may enhance understanding.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 13d0a89 and b996bc8.

📒 Files selected for processing (4)
  • infback.c (1 hunks)
  • inffast_tpl.h (4 hunks)
  • inflate.c (6 hunks)
  • inflate.h (1 hunks)
🔇 Additional comments not posted (17)
infback.c (4)

58-58: LGTM: Addition of wsize_real field

The new wsize_real field is a good addition. It allows for accurate tracking of the actual window buffer size, including any padding, which aligns with the PR objectives. Initializing it with the same value as wsize is correct for the initial state.


63-65: LGTM: Conditional compilation for dmax

The conditional compilation for dmax is a good optimization. It ensures that dmax is only set when INFLATE_STRICT is defined, which aligns with the PR objectives. This change can potentially reduce unnecessary updates in non-strict inflation scenarios, improving performance.

The value 32768U (2^15) is appropriate as it's the maximum distance value in the DEFLATE specification.


58-68: LGTM: Enhanced inflateBackInit function

The changes to the inflateBackInit function successfully implement the PR objectives:

  1. Addition of wsize_real for accurate window buffer size tracking.
  2. Conditional setting of dmax based on INFLATE_STRICT.
  3. Conditional setting of sane based on INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR.

These modifications allow for more flexible configurations and potentially improved performance by avoiding unnecessary updates. The changes are well-integrated into the existing function structure without introducing any apparent issues.


58-68: Summary: Successful enhancement of inflateBackInit

The changes to infback.c successfully implement the PR objectives by enhancing the inflate_state structure. The addition of wsize_real and the conditional compilation for dmax and sane allow for more accurate tracking and flexible configurations. These modifications should improve the inflation process and potentially enhance performance.

The code changes are well-integrated and don't introduce any apparent issues. However, a clarification on the purpose of the INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR directive would be beneficial for maintainability.

Overall, these changes represent a solid improvement to the zlib-ng library's inflation process.

inflate.h (6)

100-100: LGTM: Addition of 'strm' pointer in state structure

The addition of PREFIX3(stream) *strm; to the inflate_state structure is appropriate and enhances state management.


155-156: Approve conditional inclusion of dmax variable

The inclusion of unsigned dmax; under #ifdef INFLATE_STRICT is appropriate. Ensure that all references to dmax are also conditionally compiled.


143-145: Validate architecture-specific padding implementation

Confirm that the added padding for _M_IX86 and _M_ARM architectures meets the alignment requirements and does not introduce unintended side effects.

Run the following script to ensure correct implementation:

#!/bin/bash
# Description: Check the padding implementation for specific architectures

# Test: Examine the definitions related to padding
rg --type c -A 2 -B 2 'padding\[2\]'

121-121: Confirm the appropriate usage of chunksize

Ensure that the chunksize variable is properly set and utilized in memory copying operations to optimize performance without introducing errors.

Run the following script to find and review usages:

#!/bin/bash
# Description: Find all usages of 'chunksize' in the codebase

# Test: Verify that 'chunksize' is used correctly
rg --type c 'chunksize'

158-159: Verify logic associated with the sane variable

Under #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR, ensure that the int sane; variable correctly controls the allowance of invalid distances during inflation.

Run the following script to check the usage:

#!/bin/bash
# Description: Verify the implementation and usage of 'sane'

# Test: Find all code sections that reference 'sane'
rg --type c 'sane'

117-117: Verify proper initialization and usage of wsize_real

Ensure that wsize_real is correctly initialized and consistently used throughout the codebase to account for the actual window buffer size, including padding.

Run the following script to check the initialization and usage:

✅ Verification successful

Initialization and Usage of wsize_real Verified

The variable wsize_real is correctly initialized and consistently used across the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify initialization and usage of 'wsize_real'

# Test: Search for all references to 'wsize_real'
rg --type c 'wsize_real'

Length of output: 415


Script:

#!/bin/bash
# Description: Re-verify initialization and usage of 'wsize_real' with corrected commands

# Test 1: Search for all references to 'wsize_real'
rg --type c 'wsize_real'

# Test 2: Search for patterns related to 'padding' that might affect 'wsize_real' initialization
rg --type c 'padding\[2\]'

Length of output: 486

inffast_tpl.h (4)

140-140: Verify the use of state->wsize_real in extra_safe calculation

The condition for extra_safe now uses state->wsize_real instead of state->wsize. Ensure that state->wsize_real accurately represents the actual size of the window buffer including any padding. This change affects the memory safety checks and could impact buffer boundary validations.


190-192: Confirm the direct use of state->dmax for distance validation

The code now directly accesses state->dmax instead of using a local dmax variable. Verify that state->dmax is correctly initialized and accessible at this point in the code, especially when INFLATE_STRICT is defined. This ensures that distance checks function as intended without introducing errors.


Line range hint 201-223: Review the logic within INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR block

The modified code introduces additional conditions when state->sane is true and handles cases where the distance is too far back by either setting an error or filling with zeros. Please consider:

  • Ensuring that the condition if (state->sane) aligns with the intended behavior of strict mode.
  • Verifying that the loops and conditions correctly handle edge cases without causing infinite loops or unintended side effects.
  • Checking that the fall-through logic correctly progresses to the next operation when appropriate.

223-225: Approve the error handling for invalid distances when strict mode is not allowed

In the #else block corresponding to INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR, the code appropriately sets an error when an invalid distance is encountered and breaks out of the loop. This ensures consistent behavior when the strict mode is not permitted.

inflate.c (3)

243-243: Addition of 'state->wsize_real'

The new variable state->wsize_real is introduced and initialized correctly with INFLATE_ADJUST_WINDOW_SIZE((1 << MAX_WBITS) + 64). This enhancement tracks the actual size of the window buffer, including any padding, which can improve the usage within the chunkset code.


Line range hint 1059-1078: Conditional handling of 'invalid distance too far back' error

The code now conditionally handles the "invalid distance too far back" error based on the INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR macro. This change provides flexibility, allowing the library to either enforce strict distance checks or permit certain invalid distances based on compilation flags.


1416-1421: Conditional behavior in 'inflateUndermine' function

In the inflateUndermine function, the behavior is now contingent on the INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR macro. When the macro is defined, state->sane is set based on the subvert parameter; otherwise, the function returns Z_DATA_ERROR. This ensures that the function's behavior aligns with the configured inflation policy.

@Dead2
Copy link
Member Author

Dead2 commented Sep 25, 2024

PS: Betatesting coderabbitai, so far it does not seem too intelligent when it comes to these changes 😆

@Dead2
Copy link
Member Author

Dead2 commented Sep 25, 2024

MSVC CI is failing due to warning C4324 (struct padded due to alignment). I don't really think that warning (especially as an error) provides us any benefit at all. The fact that the end of a struct is padded up to the next cacheline boundary is pretty much a non-issue.

@mtl1979 Perhaps we should just disable that warning entirely? It would end up failing if someone compiles with optional features anyway, and it also makes it harder to maintain since not everyone has access to MSVC.

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)
inffast_tpl.h (1)

Incomplete Removal of Validation Macros

The following files still contain references to the removed macros INFLATE_STRICT and INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR:

  • inflate.h
  • inffast_tpl.h
  • inflate.c
  • infback.c
  • test/infcover.c

Please ensure that all instances of these macros are fully removed to maintain code consistency and prevent potential issues.

🔗 Analysis chain

Line range hint 190-225: Simplified distance validation logic

The changes streamline the distance validation process by:

  1. Removing the INFLATE_STRICT conditional compilation.
  2. Eliminating the complex INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR error handling.
  3. Introducing a simpler error handling mechanism for invalid distances.

These modifications simplify the code and potentially improve performance. However, we should ensure that this doesn't compromise the robustness of the inflation process.

To verify the impact of these changes, please run the following script:

This script will help ensure that the changes are consistent across the codebase and that distance handling is uniform.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining references to removed macros and validate distance handling

# Test 1: Check for any remaining references to removed macros
echo "Checking for removed macro references:"
rg --type c "INFLATE_STRICT|INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR" .

# Test 2: Analyze distance handling in other parts of the codebase
echo "Analyzing distance handling:"
ast-grep --lang c --pattern 'if ($_ > state->dmax) { $$ }'

Length of output: 1136

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b996bc8 and 3f00a03.

📒 Files selected for processing (4)
  • infback.c (1 hunks)
  • inffast_tpl.h (4 hunks)
  • inflate.c (6 hunks)
  • inflate.h (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • infback.c
  • inflate.c
  • inflate.h
🧰 Additional context used
📓 Learnings (1)
📓 Common learnings
Learnt from: Dead2
PR: zlib-ng/zlib-ng#1797
File: inflate.h:127-130
Timestamp: 2024-09-25T16:24:41.405Z
Learning: In the `inflate_state` structure, fields like `lenbits`, `lencode`, `distcode`, and `distbits` may be moved within the structure for optimization purposes; they are not necessarily new additions.
🔇 Additional comments not posted (2)
inffast_tpl.h (2)

140-140: Improved safety check for window buffer overlap

This new condition enhances the safety of chunk copy operations by detecting when the output buffer overlaps with the window buffer. The use of state->wsize_real accounts for potential padding in the window buffer, providing a more accurate boundary check.


Line range hint 1-359: Overall assessment of changes

The modifications in this file align well with the PR objectives:

  1. The introduction of extra_safe improves the safety of chunk copy operations by accurately detecting window buffer overlaps.
  2. The simplification of distance validation logic streamlines the code and potentially improves performance.
  3. The removal of complex conditional compilation directives and error handling makes the code more maintainable.

These changes contribute to a more efficient and robust inflation process. However, it's crucial to ensure that these modifications don't introduce any regressions in edge cases or compromise the library's ability to handle malformed input.

To ensure the changes don't introduce any regressions, please run the existing test suite with a focus on:

  1. Edge cases for distance handling
  2. Performance benchmarks for inflation operations
  3. Handling of malformed input

Additionally, consider adding new test cases that specifically target the modified logic to ensure its correctness and robustness.

@mtl1979
Copy link
Collaborator

mtl1979 commented Sep 25, 2024

@mtl1979 Perhaps we should just disable that warning entirely? It would end up failing if someone compiles with optional features anyway, and it also makes it harder to maintain since not everyone has access to MSVC.

Some warnings are safe to disable, disabling others can cause crashes or poor performance... We have line in CMakeLists.txt that disables useless warnings.

@Dead2
Copy link
Member Author

Dead2 commented Sep 25, 2024

Some warnings are safe to disable, disabling others can cause crashes or poor performance... We have line in CMakeLists.txt that disables useless warnings.

Yeah, I meant to add a commit that disables that warning here, but instead I managed to push it directly into Develop. Can't force-push without it, so I think we'll just keep it rather than revert it and add it back by PR.

@Dead2 Dead2 requested review from KungFuJesus and nmoinvaz October 7, 2024 12:42
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.

Nice improvements and nice code size reduction.

Copy link
Contributor

@KungFuJesus KungFuJesus left a comment

Choose a reason for hiding this comment

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

LGTM

Dead2 added 3 commits October 8, 2024 12:44
…allow

the chunkset code to spill garbage data into the padding area if available.
needed by inffast (from 6 cachelines to 1).
Also fill in some unnecessary holes.
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: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 79fe094 and e128eb0.

📒 Files selected for processing (4)
  • infback.c (1 hunks)
  • inffast_tpl.h (4 hunks)
  • inflate.c (6 hunks)
  • inflate.h (1 hunks)
🧰 Additional context used
📓 Learnings (3)
📓 Common learnings
Learnt from: Dead2
PR: zlib-ng/zlib-ng#1797
File: inflate.h:127-130
Timestamp: 2024-09-25T16:24:41.405Z
Learning: In the `inflate_state` structure, fields like `lenbits`, `lencode`, `distcode`, and `distbits` may be moved within the structure for optimization purposes; they are not necessarily new additions.
inflate.c (2)
Learnt from: Dead2
PR: zlib-ng/zlib-ng#1797
File: inflate.c:81-83
Timestamp: 2024-09-25T16:25:56.686Z
Learning: When `INFLATE_STRICT` is not defined, the variable `state->dmax` is completely removed from the code.
Learnt from: Dead2
PR: zlib-ng/zlib-ng#1797
File: inflate.c:84-86
Timestamp: 2024-09-25T16:26:23.643Z
Learning: When `INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR` is not defined, `state->sane` is completely removed from the codebase and does not need to be initialized.
inflate.h (1)
Learnt from: Dead2
PR: zlib-ng/zlib-ng#1797
File: inflate.h:127-130
Timestamp: 2024-09-25T16:24:41.405Z
Learning: In the `inflate_state` structure, fields like `lenbits`, `lencode`, `distcode`, and `distbits` may be moved within the structure for optimization purposes; they are not necessarily new additions.
🔇 Additional comments (12)
inflate.h (3)

127-130: No action needed on code table fields

Based on previous discussions, I recognize that the fields lenbits, lencode, distcode, and distbits have been moved within the inflate_state structure for optimization purposes and are not new additions. No further action is needed.


117-117: Ensure wbufsize is correctly handled

The wbufsize field has been added to represent the real size of the allocated window buffer, including any padding. Verify that wbufsize is correctly set during window buffer allocation and that any calculations or operations involving the window buffer correctly use wbufsize where appropriate.

To verify, you can run the following script:

#!/bin/bash
# Description: Check initialization and usage of `wbufsize`

# Search for initialization of `wbufsize`
rg --type c --type h -A 3 -B 3 'wbufsize\s*='

# Search for usage of `wbufsize`
rg --type c --type h 'wbufsize'

154-159: Confirm conditional compilation of dmax and sane

The variables dmax and sane are now conditionally compiled under INFLATE_STRICT and INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR, respectively. Ensure that all references to these variables in the code are also properly guarded with the same conditional compilation checks to prevent any compilation errors or unintended behavior when these flags are not defined.

To check the conditional usage of dmax and sane, you can run:

#!/bin/bash
# Description: Verify that `dmax` and `sane` are conditionally used.

# Check for uses of `dmax` outside of `#ifdef INFLATE_STRICT` guards
rg --type c --type h 'dmax' -g '!*inflate.h' | rg -v '#ifdef INFLATE_STRICT' -B 5 -A 5

# Check for uses of `sane` outside of `#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR` guards
rg --type c --type h 'sane' -g '!*inflate.h' | rg -v '#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR' -B 5 -A 5
inffast_tpl.h (3)

140-140: Update to 'extra_safe' calculation enhances buffer safety

The change from wsize to state->wbufsize in the extra_safe assignment ensures that the buffer size includes any padding in the window buffer. This adjustment allows the chunkset code to safely utilize the padding area during decompression, preventing potential out-of-bounds memory access.


190-190: Consistent use of 'state->dmax' for distance validation

Replacing dmax with state->dmax in the distance check directly references the dmax value from the current inflate state. This change improves consistency and clarity by ensuring that distance validations are based on the active state parameters.


Line range hint 201-225: Improved error handling for invalid distances in conditional compilation

Within the #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR block, the updated code now checks state->sane before setting the error state. This allows for more nuanced error handling based on the sane flag, providing flexibility for different compilation configurations and enhancing robustness in handling invalid distances during inflation.

infback.c (2)

58-68: Ensure consistent conditional compilation blocks

Both state->dmax and state->sane are now conditionally initialized within their respective #ifdef blocks. Ensure that any usage of these variables is also appropriately guarded or that default values are assigned when the conditions are not met to prevent any unintended behavior.

You might want to verify that state->sane is not used when INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR is not defined. You can use the following script:

#!/bin/bash
# Description: Check for usage of 'state->sane' outside '#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR' blocks.

# Find all occurrences of 'state->sane' not within INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR guards.
rg --type c --no-heading --line-number "state->sane" | while read -r line; do
  file=$(echo "$line" | cut -d':' -f1)
  linenum=$(echo "$line" | cut -d':' -f2)
  # Check if the line is within INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR guards
  if ! awk "NR==$linenum {print f}{f} /#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR/ {f=1} /#endif/ {f=0}" "$file" | grep -q 'state->sane'; then
    echo "$line"
  fi
done

This will help ensure that state->sane is not accessed unintentionally when it's not initialized.


63-65: 💡 Codebase verification

⚠️ Potential issue

Potential Uninitialized Use of state->dmax Detected

The variable state->dmax is assigned outside of #ifdef INFLATE_STRICT blocks in the following locations:

  • infback.c:64
  • inflate.c:82
  • inflate.c:548
  • inflate.c:1042

This indicates that state->dmax may remain uninitialized when INFLATE_STRICT is not defined, potentially leading to undefined behavior.

🔗 Analysis chain

Potential uninitialized use of state->dmax

By moving the initialization of state->dmax inside the #ifdef INFLATE_STRICT block, there's a risk that state->dmax may remain uninitialized when INFLATE_STRICT is not defined. If state->dmax is accessed elsewhere without this definition, it could lead to undefined behavior. Please ensure that state->dmax is either conditionally used or properly initialized in all configurations.

To verify the usage of state->dmax outside of the INFLATE_STRICT conditions, you can run the following script:

This script will list occurrences of state->dmax that are outside of #ifdef INFLATE_STRICT blocks.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for usage of 'state->dmax' outside '#ifdef INFLATE_STRICT' blocks.

# Find all occurrences of 'state->dmax' not within 'INFLATE_STRICT' blocks.
rg --type c --no-heading --line-number "state->dmax" | while read -r line; do
  file=$(echo "$line" | cut -d':' -f1)
  linenum=$(echo "$line" | cut -d':' -f2)
  # Check if the line is within INFLATE_STRICT guards
  if ! awk "NR==$linenum {print f}{f} /#ifdef INFLATE_STRICT/ {f=1} /#endif/ {f=0}" "$file" | grep -q 'state->dmax'; then
    echo "$line"
  fi
done

Length of output: 2186


Script:

#!/bin/bash
# Description: Check for usage of 'state->dmax' outside '#ifdef INFLATE_STRICT' blocks.

# Find all occurrences of 'state->dmax' in C files
rg --type c --no-heading --line-number "state->dmax" | while read -r line; do
  file=$(echo "$line" | cut -d':' -f1)
  linenum=$(echo "$line" | cut -d':' -f2)
  # Check if the line is within INFLATE_STRICT guards
  context=$(sed -n "$linenum p" "$file")
  before=$(sed -n "1,$((linenum-1)) p" "$file")
  
  # Determine if the 'state->dmax' line is inside an #ifdef INFLATE_STRICT block
  is_within_ifdef=$(echo "$before" | grep -B1 '#ifdef INFLATE_STRICT' | grep '#endif' | wc -l)
  
  if [ "$is_within_ifdef" -eq 0 ]; then
    echo "$line"
  fi
done

Length of output: 146816

⛔ Skipped due to learnings
Learnt from: Dead2
PR: zlib-ng/zlib-ng#1797
File: inflate.c:81-83
Timestamp: 2024-09-25T16:25:56.686Z
Learning: When `INFLATE_STRICT` is not defined, the variable `state->dmax` is completely removed from the code.
inflate.c (4)

243-243: Consistent window buffer size initialization

The assignment to state->wbufsize ensures that the window buffer size is consistently calculated with the necessary padding. This change aligns state->wbufsize with the adjusted window size, including the 64-byte padding for chunk size.


1416-1429: Conditional compilation in inflateUndermine function

The inflateUndermine function now conditionally compiles its logic based on INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR. The implementation correctly handles both cases:

  • When INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR is defined, it updates state->sane.
  • When not defined, it returns Z_DATA_ERROR, indicating the operation is not permitted.

Line range hint 1059-1078: Ensure state->sane is consistently guarded

The code introduces conditional handling based on INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR. Please verify that all occurrences of state->sane are properly enclosed within #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR to avoid referencing it when the macro is not defined.

Run the following script to identify any unguarded usages:

#!/bin/bash
# Description: Find usages of 'state->sane' not guarded by 'INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR'

# Search for 'state->sane' in the codebase, excluding lines within '#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR' blocks
rg 'state->sane' --type-add 'c_ext:*.{c,h}' --type c_ext --vimgrep | grep -v 'INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR'

547-549: Verify all usages of state->dmax are conditionally compiled

The initialization of state->dmax is enclosed within #ifdef INFLATE_STRICT. Please verify that all references to state->dmax in the codebase are also guarded by #ifdef INFLATE_STRICT to prevent any potential usage when INFLATE_STRICT is not defined.

Run the following script to check for unguarded usages:

@Dead2 Dead2 merged commit 18af700 into develop Oct 8, 2024
279 of 290 checks passed
@Dead2 Dead2 deleted the inflate-cleanup branch October 8, 2024 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cleanup Improving maintainability or removing code. enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants