Skip to content

Use gsl::narrow in ebpf_domain.cpp#709

Merged
elazarg merged 4 commits into
mainfrom
narraow-ebpf_domain
Sep 30, 2024
Merged

Use gsl::narrow in ebpf_domain.cpp#709
elazarg merged 4 commits into
mainfrom
narraow-ebpf_domain

Conversation

@elazarg

@elazarg elazarg commented Sep 30, 2024

Copy link
Copy Markdown
Collaborator

Also: introduce num_safety.hpp and two helpers: to_signed() and to_unsigned()

Summary by CodeRabbit

  • New Features

    • Enhanced type handling and arithmetic operations for improved safety and clarity.\
    • Added utility functions for type safety in numeric operations.
  • Bug Fixes

    • Improved error handling for arithmetic operations to prevent overflow and type mismatches.
  • Documentation

    • Updated method signatures and added new methods for better clarity and usability.
  • Refactor

    • Streamlined type handling and error reporting across various classes and methods.

Signed-off-by: Elazar Gershuni <elazarg@gmail.com>
Also: introduce num_safety.hpp and two helpers: to_signed() and to_unsigned()

Signed-off-by: Elazar Gershuni <elazarg@gmail.com>
@coderabbitai

coderabbitai Bot commented Sep 30, 2024

Copy link
Copy Markdown

Warning

Rate limit exceeded

@elazarg has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 9 minutes and 50 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Files that changed from the base of the PR and between 95c6f2f and 7ee1ec3.

Walkthrough

This pull request introduces several modifications across multiple files, primarily focusing on enhancing type safety, improving arithmetic operations, and refining the handling of data types and intervals. Key changes include updates to method signatures, the introduction of new functions for type iteration, and enhancements to error handling and logging. The overall structure of the code remains intact, but the changes aim to improve clarity, maintainability, and robustness in type handling and arithmetic operations.

Changes

Files Change Summary
src/crab/array_domain.cpp Updates to cell_t and array_domain_t classes, including changes to to_interval, load, and get_value_byte methods for better type handling and clarity. Refined methods for managing cells in offset_map_t.
src/crab/ebpf_domain.cpp Enhancements in type handling using gsl::narrow_cast, improved arithmetic operations with overflow checks, and updated function signatures for better type safety.
src/crab/type_domain.cpp Modifications for handling type encodings, introduction of new methods for type iteration, and enhancements in the TypeDomain class for better type management and merging functionalities.
src/crab/type_encoding.hpp Addition of constants for data_kind_t enumeration and a new function for iterating over data_kind_t values, enhancing the operational capabilities related to types.
src/crab_utils/num_big.hpp Simplification of the number_t class by removing the is_enum concept, updating constructors, and enhancing type safety in arithmetic operations.
src/crab_utils/num_safety.hpp Introduction of utility components for numeric operations, including a concept for enum checking and functions for signed/unsigned conversions, enhancing type safety.
src/ebpf_vm_isa.hpp New enumeration for register definitions within the eBPF virtual machine ISA, enhancing clarity and organization of register usage.

Possibly related PRs

Poem

🐇 In the code where numbers play,
Types are safe, come what may.
With new checks and clearer paths,
Arithmetic now avoids the wraths.
So hop along, let’s celebrate,
For type safety is truly great! 🎉


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 11

🧹 Outside diff range comments (3)
src/ebpf_vm_isa.hpp (2)

Line range hint 92-105: LGTM! Consider adding a brief comment for R6-R9 registers.

The new enumeration for register definitions is well-structured and aligns with the eBPF architecture specifications. The naming convention is clear, and the comments for special registers (R0, R10, R11) add valuable context.

Consider adding a brief comment for registers R6-R9 to maintain consistency with the other register comments. For example:

 R5_ARG = 5,
-R6 = 6,
-R7 = 7,
-R8 = 8,
-R9 = 9,
+R6 = 6, // General purpose register
+R7 = 7, // General purpose register
+R8 = 8, // General purpose register
+R9 = 9, // General purpose register
 R10_STACK_POINTER = 10,

Hardcoded register values detected. Please replace them with the new enumeration constants to ensure consistency.

  • src/test/test_verify.cpp: Multiple instances of hardcoded registers such as r4, r5, r7, and r9.
🔗 Analysis chain

Line range hint 92-105: Verify usage of the new register enumeration across the codebase.

The new register enumeration is well-defined and placed appropriately in the file. To ensure its effectiveness and consistency:

  1. Verify that this enumeration is used consistently across the codebase where register operations are performed.
  2. Update any existing hardcoded register values to use these enumeration constants.
  3. Ensure that documentation is updated to reflect the use of these new constants.

Run the following script to check the usage of the new register enumeration:

This script will help identify areas where the new enumeration should be used and verify its current usage.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check usage of new register enumeration constants

# Test: Search for hardcoded register values
echo "Searching for potential hardcoded register values:"
rg '\b([Rr]0|[Rr]1|[Rr]2|[Rr]3|[Rr]4|[Rr]5|[Rr]6|[Rr]7|[Rr]8|[Rr]9|[Rr]10|[Rr]11)\b' --type cpp

# Test: Search for usage of new enumeration constants
echo "Searching for usage of new enumeration constants:"
rg '\b(R0_RETURN_VALUE|R1_ARG|R2_ARG|R3_ARG|R4_ARG|R5_ARG|R6|R7|R8|R9|R10_STACK_POINTER|R11_ATOMIC_SCRATCH)\b' --type cpp

Length of output: 15971

src/crab_utils/num_big.hpp (1)

Line range hint 31-39: LGTM: New methods for enum handling added.

The addition of the narrow() and cast_to() methods for enum types enhances type safety and aligns with the PR objectives. The narrow() method correctly uses static_cast to convert to the underlying type of the enum.

However, consider the following suggestion:

The cast_to() method for enums seems to provide similar functionality to narrow(). To avoid potential confusion, you might want to consider:

  1. Removing the cast_to() method for enums and using narrow() consistently.
  2. Or, if both are needed, add comments explaining the difference in usage between narrow() and cast_to() for enum types.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 130ef6c and 95c6f2f.

📒 Files selected for processing (7)
  • src/crab/array_domain.cpp (4 hunks)
  • src/crab/ebpf_domain.cpp (21 hunks)
  • src/crab/type_domain.cpp (1 hunks)
  • src/crab/type_encoding.hpp (1 hunks)
  • src/crab_utils/num_big.hpp (1 hunks)
  • src/crab_utils/num_safety.hpp (1 hunks)
  • src/ebpf_vm_isa.hpp (1 hunks)
🧰 Additional context used
📓 Learnings (1)
src/crab_utils/num_big.hpp (2)
Learnt from: elazarg
PR: vbpf/ebpf-verifier#689
File: src/crab_utils/bignums.hpp:63-71
Timestamp: 2024-09-25T22:59:40.646Z
Learning: The casting logic in the `number_t::cast_to` method, using `swap_signedness` when `_n` doesn't fit into `T`, is intentional and should not be flagged.
Learnt from: elazarg
PR: vbpf/ebpf-verifier#689
File: src/crab_utils/num_big.hpp:30-30
Timestamp: 2024-09-26T00:56:36.307Z
Learning: Casting enums to `int64_t` in the `number_t` constructor is intentional and should not be flagged.
🪛 cppcheck
src/crab/type_domain.cpp

[style] 25-25: The function 'iterate_kinds' is never used.

(unusedFunction)

🔇 Additional comments (14)
src/crab_utils/num_safety.hpp (3)

11-12: LGTM: Well-defined concept for enum types.

The is_enum concept is correctly implemented using std::is_enum_v. This can be useful for template metaprogramming and type traits involving enumeration types.


14-15: LGTM: Well-implemented type alias for swapping signedness.

The swap_signedness type alias is correctly implemented using std::conditional_t, std::make_unsigned_t, and std::make_signed_t. It's properly constrained to integral types and will be useful for type conversions.


1-25: Overall, well-implemented utility components with minor suggestions for improvement.

The num_safety.hpp file introduces useful utility components for enhancing type safety in numeric operations. It effectively uses modern C++ features like concepts and type traits. The implementations are correct and appropriate for their intended purposes.

Consider the following overall suggestions:

  1. Remove the unused <gsl/narrow> include if it's not needed for future implementations.
  2. Apply the suggested changes to to_signed and to_unsigned functions (removing static and adding constexpr).
  3. Consider adding brief documentation comments for each component to explain their purpose and usage.
src/crab/type_encoding.hpp (2)

23-24: LGTM: Addition of KIND_MIN and KIND_MAX constants

The introduction of KIND_MIN and KIND_MAX constants is a good practice. These constants provide clear bounds for the data_kind_t enum, which can be useful for iteration and range checking. This addition enhances code readability and maintainability.


23-28: Verify usage of new constants and function

The additions of KIND_MIN, KIND_MAX, and iterate_kinds function enhance the API for working with data_kind_t. To ensure these changes are fully utilized:

Run the following script to check for usage of these new additions:

If the script doesn't find any usage, consider adding examples or updating existing code to leverage these new additions.

src/crab_utils/num_big.hpp (4)

12-12: LGTM: New include statement added.

The addition of #include "crab_utils/num_safety.hpp" is appropriate and aligns with the PR objectives to implement gsl::narrow. This inclusion will likely provide the necessary functions for improved type safety.


Line range hint 23-24: LGTM: Constructor signatures updated for improved type handling.

The changes to the constructor signatures simplify type handling by using std::integral instead of the is_enum concept. This update aligns with the retrieved learning that casting enums in the constructor is intentional. The new signatures provide a more generic approach to handling integer types.


Line range hint 265-266: LGTM: New operator overloads added for improved interoperability.

The addition of <= operator overloads for integral and enum types on the left-hand side enhances the usability of the number_t class. These overloads correctly delegate to the existing >= operator and use concepts to ensure type safety.


Line range hint 1-269: Overall, the changes improve type safety and enum handling.

The modifications to src/crab_utils/num_big.hpp align well with the PR objectives of enhancing type safety and improving enum handling. The new methods and operator overloads contribute to a more robust number_t class.

However, there's one point that needs verification:

The PR objectives mention the use of gsl::narrow, but it's not directly visible in this file. Could you please verify its implementation and usage? You can use the following script to check:

This will help ensure that gsl::narrow is properly implemented and used as intended in the PR objectives.

✅ Verification successful

Verification Successful: gsl::narrow is properly implemented and utilized within the codebase.

The inclusion of gsl::narrow in num_safety.hpp and its usage across multiple files ensures that type safety enhancements are effectively integrated as intended by the PR objectives.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation and usage of gsl::narrow

# Check the content of num_safety.hpp
echo "Content of num_safety.hpp:"
cat src/crab_utils/num_safety.hpp

# Search for gsl::narrow usage in the codebase
echo -e "\nUsage of gsl::narrow in the codebase:"
rg "gsl::narrow" src

Length of output: 4529

src/crab/type_domain.cpp (2)

Line range hint 184-192: New Method join_by_if_else Reviewed

The join_by_if_else method introduces conditional domain transitions using inv.when(condition) and its negation. This approach effectively splits the abstract domain based on the condition and merges the results. The implementation aligns with standard practices for abstract domain manipulation.

🧰 Tools
🪛 cppcheck

[style] 25-25: The function 'iterate_kinds' is never used.

(unusedFunction)


Line range hint 272-290: Verify Exhaustive Handling of TypeGroup Enum Values

In the operator<< overload for TypeGroup, ensure that all possible enum values are accounted for in the string_to_type map. Missing mappings could lead to runtime errors due to the CRAB_ERROR call when an unsupported type group is encountered.

You can run the following script to check for any unhandled TypeGroup values:

🧰 Tools
🪛 cppcheck

[style] 25-25: The function 'iterate_kinds' is never used.

(unusedFunction)

src/crab/array_domain.cpp (3)

18-18: Include necessary header for numerical safety functions

Including "crab_utils/num_safety.hpp" is appropriate as it provides to_signed() and to_unsigned() used in subsequent code changes.


651-651: Use 'to_unsigned' for safe conversion of 'width'

Replacing gsl::narrow<unsigned>(width) with to_unsigned(width) enhances type safety and consistency by using the helper function designed for safe unsigned conversions.


710-710: Ensure correct sign when converting bytes to 'number_t'

Using to_signed(b) when kind == data_kind_t::svalues ensures that the value is correctly interpreted as a signed integer. This change improves accuracy in handling signed values.

Comment thread src/crab_utils/num_safety.hpp
Comment thread src/crab_utils/num_safety.hpp Outdated
Comment thread src/crab_utils/num_safety.hpp Outdated
Comment thread src/crab/type_encoding.hpp
Comment thread src/crab/type_domain.cpp
Comment thread src/crab/array_domain.cpp
Comment thread src/crab/ebpf_domain.cpp
Comment thread src/crab/ebpf_domain.cpp
Comment thread src/crab/ebpf_domain.cpp
Comment thread src/crab/ebpf_domain.cpp
elazarg and others added 2 commits October 1, 2024 01:39
Signed-off-by: Elazar Gershuni <elazarg@gmail.com>

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Elazar Gershuni <elazarg@gmail.com>

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@coveralls

coveralls commented Sep 30, 2024

Copy link
Copy Markdown

Coverage Status

coverage: 90.385% (-0.01%) from 90.398%
when pulling 7ee1ec3 on narraow-ebpf_domain
into 130ef6c 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