Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX proposal for a crash that happens because of some misuse of const_cast<> #1920

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Nibelheims
Copy link

@Nibelheims Nibelheims commented Sep 8, 2022

Hello,
First of all, thank you for providing libACE.
This commit is a proposal to fix a crash that can happen with ACE::sendv_n_i() and ACE::writev_n(), in case the given iovecs have been allocated on the heap.
Indeed in case of a partial send, libACE will loop until all the data has been sent. The problem is that it updates its arguments when doing so, leaving the caller with one or more updated pointers inside the table of iovecs. Attempting to free such a pointer results in a fault (the value can even not be aligned properly anymore).
The fix simply copies the iovec data if necessary (no perf penalty otherwise), and updates those values instead of the arguments.
Thank you.

Summary by CodeRabbit

  • New Features
    • Added a timeout option to data transmission operations for enhanced control.
  • Refactor
    • Improved data handling to ensure original inputs remain unaltered during transfers.
    • Optimized resource management to bolster performance and stability.

These enhancements improve overall reliability and efficiency of data transfer processes, providing a smoother and more predictable experience for users.

ACE::writev_n with iovecs that have been allocated on the heap.
In case of a partial send, libACE will loop until all the data has been
sent. The problem is that it updates its arguments when doing so,
leaving the caller with one or more updated pointers inside the table of
iovecs. Attempting to free such a pointer results in a fault (the value
can even not be aligned properly).
The fix simply copies the iovec data _if necessary_ (no perf penalty
otherwise), and updates those values instead of the arguments.
@jwillemsen jwillemsen added the needs review Needs to be reviewed label Sep 8, 2022
@Nibelheims
Copy link
Author

Hello,
Thank you for considering this PR. As you can see the modification is small and simple. Do not hesitate to discuss it. I am open to modifications if required.
Thanks and best regards,
Nibelheims

@jwillemsen
Copy link
Member

What is exactly copied, only the iovec array but not the application data it refers to, correct? Any idea to extend one of the existing ACE unit tests as reproducer?

@Nibelheims
Copy link
Author

Nibelheims commented Sep 14, 2022

Hello,
Thank you for your reply. Sorry you are right, my phrasing was bad. Yes only the iovec array gets copied, not the data it refers to.
I am quite new to libACE, I actually had to investigate a crash on some application and it turned out that it fell in the "data cannot be sent at once" case. Hence this PR.
That application sends data over the network. I don't know if sending big chunks of data (bigger than the MTU at least) to a socket would be enough to reproduce. The easiest way would be to stub ACE_OS::sendv to make it perform a partial send on the input.
I did this PR on my spare time and, unfortunately, could not look at the testing code (not doing so is bad, sorry about that). I had a bit of trouble compiling, I first had to figure out that MPC (which I was unaware of) was required, then I had some trouble making it generate a Makefile. I finally could compile the shared object successfully, but I did not have enough time to go further.

@Nibelheims
Copy link
Author

Hello,
would you have any update on this PR?
Thanks and best regards,
Nibelheims

Copy link

coderabbitai bot commented Feb 1, 2025

Walkthrough

This change modifies the sendv_n_i and writev_n methods in ACE.cpp. The functions now accept a pointer to a constant iovec rather than a modifiable pointer. A conditional shadow copy of the iovec array is created when modifications to the array elements are needed, ensuring that the original remains unaltered. Additionally, the signature of sendv_n_i has been updated to include a timeout parameter. Comments have been enhanced to clarify the updated design and its implications.

Changes

File Change Summary
ACE/.../ACE.cpp Updated sendv_n_i and writev_n methods to take a const iovec pointer. A conditional shadow copy of the iovec is allocated to allow modifications without affecting the original. The sendv_n_i signature now includes an extra timeout parameter. Improved comments document the changes.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant ACE_Function as ACE::sendv_n_i / writev_n
    participant Shadow as Shadow Copy Manager

    Caller->>ACE_Function: Call function with const iovec array
    alt Modification Needed
        ACE_Function->>Shadow: Allocate & copy iovec array
        ACE_Function->>Shadow: Update shadow copy fields
    else No Modification
        Note right of ACE_Function: Use original iovec directly
    end
    ACE_Function->>Caller: Return result and cleanup shadow copy if allocated
Loading

Poem

I'm a rabbit skipping through code so bright,
Hop through shadows to keep data just right.
I crafted a copy to guard the original trail,
With a timeout and care, our functions prevail.
Bugs hop away as we celebrate with delight!
🐇💻🎉

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 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

@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 (3)
ACE/ace/ACE.cpp (3)

1870-1876: Reinitialize local iov pointer with const correctness
Similar to the previous segment, this preserves the original iovec. Consider extracting this repeated pattern into a helper function for maintainability.


1922-1938: On-demand allocation block repeated
This block duplicates the earlier logic for partial sends. A unified helper method could reduce duplication and simplify future maintenance.


2242-2258: Repeats the shadow copy memory allocation logic
This third repetition further highlights a refactoring opportunity to centralize the partial-send logic.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6c3ca3f and ae097a4.

📒 Files selected for processing (1)
  • ACE/ace/ACE.cpp (6 hunks)
🔇 Additional comments (8)
ACE/ace/ACE.cpp (8)

1768-1774: Preserve original iovec to prevent unintended pointer mutations
This approach to storing i in a local iov pointer while keeping it const is a clean way to ensure the original array isn't modified unless needed.


1816-1838: Allocate shadow copy on-demand to handle partial sends
Allocating the shadow_iovec array only when partial data remains is a good strategy to avoid unnecessary overhead. The null check ensures this allocation happens exactly once.


1842-1850: Properly free shadow copy to avoid memory leaks
The conditional deallocation of shadow_iovec prevents leaks and keeps memory usage in check.


1940-1944: Pointer arithmetic on the shadow copy
Ensuring that the base pointer is updated on the shadow array (rather than the original) is key to preventing alignment and ownership issues. Looks solid.


1948-1956: Cleanup of dynamically allocated shadow iovec
This cleanup mirrors the earlier pattern, preventing resource leaks. The coherency is appreciated.


2213-2219: Maintains const correctness of user-supplied iovec
Again, using a local iov pointer helps enforce the “no modification unless needed” rule. This design is well-aligned with the updated function contract.


2260-2264: Shadow pointer arithmetic remains correct
Adjusting the iov_base and iov_len on the shadow array rather than modifying the caller’s iovec is consistent with preventing error-prone pointer updates.


2268-2276: Release shadow copy if allocated
Properly freeing the shadow_iovec resources ensures no leaks in this partial-send scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs review Needs to be reviewed
Development

Successfully merging this pull request may close these issues.

2 participants