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

Use gsl::narrow in asm files #710

Merged
merged 5 commits into from
Oct 1, 2024
Merged

Use gsl::narrow in asm files #710

merged 5 commits into from
Oct 1, 2024

Conversation

elazarg
Copy link
Collaborator

@elazarg elazarg commented Oct 1, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced type safety and error handling across multiple components, improving overall stability.
    • Expanded test cases for instruction marshaling and unmarshaling, ensuring comprehensive coverage.
  • Bug Fixes

    • Improved error reporting and handling in various functions, addressing potential overflow and invalid type conversions.
  • Documentation

    • Updated parameter names for clarity in function signatures, enhancing readability and maintainability.

Signed-off-by: Elazar Gershuni <[email protected]>
Also: introduce num_safety.hpp and two helpers: to_signed() and to_unsigned()

Signed-off-by: Elazar Gershuni <[email protected]>
Signed-off-by: Elazar Gershuni <[email protected]>
Copy link

coderabbitai bot commented Oct 1, 2024

Walkthrough

The pull request introduces a series of modifications across multiple files, primarily focusing on enhancing type safety and improving error handling. Key changes include the adoption of gsl::narrow and gsl::narrow_cast for safer type conversions, updates to function signatures for clarity, and refinements in error handling mechanisms. The changes aim to ensure robust type handling and clearer code structure without altering the core logic of the existing functionality.

Changes

Files Change Summary
src/asm_files.cpp, src/asm_files.hpp, src/asm_ostream.hpp, src/asm_unmarshal.cpp, src/ebpf_yaml.cpp Modifications focus on enhancing type safety through the use of gsl::narrow and gsl::narrow_cast, updates to function signatures for clarity, and improvements in error handling and instruction processing.
src/test/test_marshal.cpp Enhancements to the testing framework for instruction marshaling and unmarshaling, including new test cases and refinements to ensure comprehensive coverage of various instruction types and behaviors.

Possibly related PRs

🐇 In the code we hop and play,
With types so safe, we leap all day!
Gsl's magic makes it right,
Errors vanish, out of sight.
So here's to safety, bright and clear,
In every function, we have no fear! 🐇


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.

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

🧹 Outside diff range comments (3)
src/test/test_marshal.cpp (3)

Line range hint 15-16: Fix the typographical error in the comment.

The word "tample" should be corrected to "sample" for clarity.


Line range hint 793-794: Correct the invalid shift operator in the comment.

The operator >>>= is not valid in C++. Use >>= for the right shift operation.


Line range hint 506-511: Use consistent named constants for opcode components.

Consistently using named constants like INST_OPSRC_REG instead of magic numbers enhances readability and reduces potential errors.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 1ef02eb and 6cf3b1b.

📒 Files selected for processing (6)
  • src/asm_files.cpp (5 hunks)
  • src/asm_files.hpp (1 hunks)
  • src/asm_ostream.hpp (2 hunks)
  • src/asm_unmarshal.cpp (12 hunks)
  • src/ebpf_yaml.cpp (2 hunks)
  • src/test/test_marshal.cpp (1 hunks)
🔇 Additional comments (26)
src/asm_files.hpp (3)

13-13: LGTM: Improved parameter naming

The change from section to desired_section enhances code readability by making the parameter's purpose more explicit. This aligns well with best practices for clear and self-documenting code.


15-15: LGTM: Consistent parameter naming

The change from section to desired_section in this overload maintains consistency with the previous function declaration. This consistency is crucial for code maintainability and reduces potential confusion for developers using these functions.


16-17: LGTM: Improved code formatting

The adjustment in line breaks and indentation enhances the readability of this long function declaration. It's consistent with common C++ style guidelines and makes the code easier to scan and understand.

src/asm_ostream.hpp (5)

12-12: Inclusion of crab_utils/num_safety.hpp enhances type safety

Including crab_utils/num_safety.hpp provides access to gsl::narrow, ensuring safer type conversions in the code that follows.


48-48: Use of crab::to_signed improves immediate value handling

Replacing direct casting with crab::to_signed(imm.v) ensures correct sign interpretation of immediate values, enhancing code correctness and type safety.


49-49: Safe conversion in operator<< for Reg using gsl::narrow

Using gsl::narrow<int>(a.v) ensures safe conversion of the register value to an int, preventing potential overflows or truncation issues.


57-70: Simplified operator<< overloads improve maintainability

Delegating the output to Instruction{a} in the operator<< overloads enhances code maintainability and ensures consistent output formatting across different instruction types.


28-31: Ensure exception safety when using gsl::narrow in label_to_offset32

As gsl::narrow can throw exceptions if the value cannot be narrowed safely, verify that any potential exceptions are properly handled or documented.

Run the following script to check for exception handling around label_to_offset32 usage:

#!/bin/bash
# Description: Search for usages of `label_to_offset32` and check for exception handling.

# Test: Find calls to `label_to_offset32` and include surrounding lines. Expect: Exception handling code present.
rg --type cpp -A 5 'label_to_offset32\('
src/ebpf_yaml.cpp (2)

147-147: Good use of gsl::narrow<int> for safe type conversion.

By using gsl::narrow<int>(raw_block.size()), you ensure that any narrowing from size_t to int is checked at runtime, preventing potential data loss or overflow. This enhances the robustness of the code.


299-299: Good use of gsl::narrow<int> for safe type conversion.

Wrapping memory_bytes.size() with gsl::narrow<int> ensures that the conversion from size_t to int is safe, and any value that cannot be represented in an int will throw an exception. This adds safety to the code by preventing unintended behavior due to integer overflow or data loss.

src/asm_files.cpp (5)

17-17: Including necessary header for gsl::narrow functions

Including "crab_utils/num_safety.hpp" is appropriate to enable the use of gsl::narrow and gsl::narrow_cast functions for safer type conversions.


41-41: Handle potential overflow when casting cache.size()

Using gsl::narrow<int> on global_program_info->cache.size() assumes that the cache size will always fit within an int. If the cache size exceeds INT_MAX, this will throw an exception.

Please confirm that global_program_info->cache.size() cannot exceed INT_MAX. If it can, consider using a larger integer type or handling the exception accordingly.


253-254: Ensure safe narrowing of reloc.source_offset

Casting reloc.source_offset with gsl::narrow<int64_t> assumes that its value fits within int64_t. If reloc.source_offset exceeds the range of int64_t, this will throw an exception.

Please verify that reloc.source_offset is guaranteed to be within the range of int64_t. If not, consider using a wider integer type or handling potential exceptions.


269-269: Safely casting map.type_id to int

Using gsl::narrow_cast<int> to cast map.type_id ensures type safety. Confirm that map.type_id is within the range of int to prevent unexpected behavior.

Please ensure that map.type_id cannot exceed the limits of an int. If there's a possibility, consider using a larger type or handling the casting carefully.


383-383: Confirm that program_offset fits within uint32_t

Casting program_offset with gsl::narrow_cast<uint32_t> assumes that its value is within the range of uint32_t. If program_offset exceeds UINT32_MAX, this could lead to incorrect behavior.

Please verify that program_offset will always be less than or equal to UINT32_MAX. If not, consider using a wider integer type or adding checks to handle large values.

src/asm_unmarshal.cpp (11)

9-9: Proper inclusion of crab_utils/num_safety.hpp

The inclusion of crab_utils/num_safety.hpp is appropriate to utilize numerical safety functions like crab::to_unsigned.


222-222: Appropriate use of gsl::narrow<Atomic::Op> for safe type conversion

Using gsl::narrow<Atomic::Op> ensures that the narrowing conversion from inst.imm & ~INST_FETCH to Atomic::Op is safe at runtime, throwing an exception if the value cannot be represented. This enhances type safety.


236-236: Correct use of crab::to_unsigned for sign extension

Replacing the previous casting with crab::to_unsigned(int64_t{imm}) in sign_extend ensures accurate sign extension from a 32-bit integer to an unsigned 64-bit integer, enhancing readability and safety.


238-238: Correct use of crab::to_unsigned for zero extension

Using crab::to_unsigned(imm) in zero_extend effectively zero-extends a 32-bit integer to an unsigned 64-bit integer, providing a clearer and safer conversion.


508-508: Ensure safe narrowing with gsl::narrow<uint8_t>(i)

Using gsl::narrow<uint8_t>(i) safely converts the index i to uint8_t, ensuring it fits within the expected range. Since i ranges from 1 to 5 in this context, the narrowing should not cause issues.


541-542: Validate indices when narrowing to uint8_t

When using gsl::narrow<uint8_t>(i) and gsl::narrow<uint8_t>(i + 1), ensure that i and i + 1 are within the uint8_t range to prevent exceptions. Given the controlled range of i in this context, this usage is appropriate.


559-559: Appropriate use of gsl::narrow<int> for label conversion

Converting new_pc to label_t using gsl::narrow<int>(new_pc) ensures that the program counter fits into an int, providing type safety.


585-585: Safe narrowing of inst.imm to uint8_t

Using gsl::narrow<uint8_t>(inst.imm) in Callx{} ensures that inst.imm is within the valid range for a uint8_t, enhancing type safety.


691-693: Correct use of sign_extend for immediate values in jump conditions

Using sign_extend(inst.imm) when setting up the right-hand side of the jump condition ensures that negative immediate values are correctly sign-extended, preserving the intended logic of conditional jumps.


714-714: Proper call to makeLddw with updated parameters

The call to makeLddw(inst, next_imm, insts, pc) appropriately passes next_imm, ensuring that the 64-bit immediate value is correctly constructed from two instructions.


780-780: Ensure pc fits into int when creating labels

Using gsl::narrow<int>(pc) when constructing label_t ensures that the program counter value fits into an int, adding type safety to label handling.

src/asm_files.hpp Show resolved Hide resolved
src/asm_ostream.hpp Show resolved Hide resolved
src/asm_files.cpp Show resolved Hide resolved
src/asm_unmarshal.cpp Show resolved Hide resolved
src/asm_unmarshal.cpp Show resolved Hide resolved
src/test/test_marshal.cpp Show resolved Hide resolved
@coveralls
Copy link

Coverage Status

coverage: 90.4% (+0.02%) from 90.385%
when pulling 6cf3b1b on narrow-asm
into 1ef02eb on main.

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.

2 participants