Skip to content

[WebAssembly] Fix stack overflow in crc32_chorba_118960_nondestructive.#1915

Merged
Dead2 merged 1 commit intozlib-ng:developfrom
mtl1979:chorba-wasm
May 27, 2025
Merged

[WebAssembly] Fix stack overflow in crc32_chorba_118960_nondestructive.#1915
Dead2 merged 1 commit intozlib-ng:developfrom
mtl1979:chorba-wasm

Conversation

@mtl1979
Copy link
Copy Markdown
Collaborator

@mtl1979 mtl1979 commented May 20, 2025

  • Currently default stack size on emscripten is 64 kB, which is too small for Chorba.

Summary by CodeRabbit

  • Bug Fixes
    • Improved compatibility with Emscripten by updating memory allocation for temporary buffers, ensuring stable operation in web environments.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented May 20, 2025

Walkthrough

The update modifies the crc32_chorba_118960_nondestructive function to use dynamic memory allocation for a large temporary buffer when compiling for Emscripten, instead of stack allocation. It conditionally includes the zutil_p.h header and ensures the buffer is freed appropriately in the Emscripten environment. No public interfaces are changed.

Changes

File(s) Change Summary
arch/generic/crc32_chorba_c.c Added conditional compilation for Emscripten: dynamically allocates and frees a large buffer using zng_alloc/zng_free; conditionally includes zutil_p.h. No other logic or interface changes.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant crc32_chorba_118960_nondestructive
    participant zng_alloc
    participant zng_free

    Caller->>crc32_chorba_118960_nondestructive: call
    alt Compiling for Emscripten
        crc32_chorba_118960_nondestructive->>zng_alloc: allocate bitbuffer
        zng_alloc-->>crc32_chorba_118960_nondestructive: pointer to buffer
        Note over crc32_chorba_118960_nondestructive: Use buffer for computation
        crc32_chorba_118960_nondestructive->>zng_free: free bitbuffer
    else Not Emscripten
        Note over crc32_chorba_118960_nondestructive: Use stack-allocated bitbuffer
    end
    crc32_chorba_118960_nondestructive-->>Caller: return
Loading

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch chorba-wasm
  • Post Copyable Unit Tests in Comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this 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.

Copy link
Copy Markdown
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

🧹 Nitpick comments (1)
arch/generic/crc32_chorba_c.c (1)

31-492: Consider error handling for memory allocation failure.

The current implementation doesn't check if zng_alloc() successfully allocated memory. For a more robust solution, consider adding error handling for allocation failures.

#if defined(__EMSCRIPTEN__)
    z_word_t* bitbuffer = (z_word_t*)zng_alloc(bitbuffersizebytes);
+   if (bitbuffer == NULL) {
+       /* Handle allocation failure - options include:
+        * 1. Return an error code
+        * 2. Fallback to a slower algorithm that uses less memory
+        * 3. Assert/abort if this is a critical function that cannot fail
+        */
+       return 0; /* Or appropriate error value */
+   }
#else
    ALIGNED_(16) z_word_t bitbuffer[bitbuffersizezwords];
#endif

And update the cleanup section accordingly:

#if defined(__EMSCRIPTEN__)
+   if (bitbuffer != NULL) {
        zng_free(bitbuffer);
+   }
#endif
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 830995f and 75878e3.

📒 Files selected for processing (1)
  • arch/generic/crc32_chorba_c.c (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (68)
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC SSSE3 UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: Ubuntu GCC S390X No vectorized CRC32
  • GitHub Check: Ubuntu GCC PPC
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC SSSE3 UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: Ubuntu GCC S390X No vectorized CRC32
  • GitHub Check: Ubuntu GCC PPC
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC SSSE3 UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: Ubuntu GCC S390X No vectorized CRC32
  • GitHub Check: Ubuntu GCC PPC
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC SSSE3 UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS GCC Symbol Prefix (ARM64)
  • GitHub Check: Ubuntu GCC S390X No vectorized CRC32
  • GitHub Check: Ubuntu GCC PPC
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC SSSE3 UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS GCC Symbol Prefix (ARM64)
  • GitHub Check: Ubuntu GCC S390X No vectorized CRC32
  • GitHub Check: Ubuntu GCC PPC
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC SSSE3 UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS GCC Symbol Prefix (ARM64)
  • GitHub Check: Ubuntu GCC S390X No vectorized CRC32
  • GitHub Check: Ubuntu GCC PPC
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC SSSE3 UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS GCC Symbol Prefix (ARM64)
  • GitHub Check: Ubuntu GCC S390X No vectorized CRC32
  • GitHub Check: Ubuntu GCC PPC
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC SSSE3 UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS GCC Symbol Prefix (ARM64)
  • GitHub Check: Ubuntu GCC S390X No vectorized CRC32
  • GitHub Check: Ubuntu GCC PPC
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Windows MSVC 2022 v143 Win32
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC SSSE3 UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS GCC Symbol Prefix (ARM64)
  • GitHub Check: Ubuntu GCC S390X No vectorized CRC32
  • GitHub Check: Ubuntu GCC PPC
  • GitHub Check: macOS Clang Native Instructions (ARM64)
  • GitHub Check: Windows MSVC 2022 v143 Win32
  • GitHub Check: Ubuntu GCC AARCH64 No NEON UBSAN
  • GitHub Check: Ubuntu GCC SSSE3 UBSAN
  • GitHub Check: Ubuntu GCC Symbol Prefix
  • GitHub Check: macOS GCC Symbol Prefix (ARM64)
  • GitHub Check: Ubuntu GCC S390X No vectorized CRC32
🔇 Additional comments (4)
arch/generic/crc32_chorba_c.c (4)

2-4: Good addition of conditional include for Emscripten.

Including zutil_p.h only for Emscripten builds is a clean approach that avoids unnecessary dependencies in other build environments.


31-35: Effective solution to the stack overflow issue.

The change to use dynamic memory allocation in Emscripten addresses the root cause of the stack overflow. The default Emscripten stack size (64 KB) is insufficient for the 128 KB temporary buffer, as noted in the function's comment on line 28.

This is a good approach as it:

  1. Only affects the Emscripten target
  2. Preserves the aligned stack allocation for other platforms where it's efficient
  3. Avoids modifying the algorithm's logic

490-492: Proper resource cleanup.

The addition of zng_free() correctly prevents memory leaks by freeing the dynamically allocated buffer in the Emscripten-specific code path.


10-11: Verify the buffer size requirement across platforms.

The bitbuffer size is defined as 16 * 1024 * sizeof(z_word_t), which is 128 KB on platforms where z_word_t is 8 bytes. While the PR fixes the Emscripten issue, it's worth noting that this function requires a large temporary buffer as documented in the function comments.

This is for information only - no changes needed to the implementation.

@codecov
Copy link
Copy Markdown

codecov bot commented May 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.65%. Comparing base (e4a31e0) to head (75878e3).
Report is 14 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1915      +/-   ##
===========================================
- Coverage    83.42%   81.65%   -1.78%     
===========================================
  Files          144      147       +3     
  Lines        12948    13308     +360     
  Branches      2857     2951      +94     
===========================================
+ Hits         10802    10866      +64     
- Misses        1202     1516     +314     
+ Partials       944      926      -18     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mtl1979
Copy link
Copy Markdown
Collaborator Author

mtl1979 commented May 20, 2025

Emscripten doesn't by default need NULL check for allocation failure as it will abort on out-of-memory condition.

@Dead2 Dead2 merged commit f90c011 into zlib-ng:develop May 27, 2025
139 of 146 checks passed
@Dead2 Dead2 mentioned this pull request Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants