-
Notifications
You must be signed in to change notification settings - Fork 44
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
Conversation
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]>
Signed-off-by: Elazar Gershuni <[email protected]>
Signed-off-by: Elazar Gershuni <[email protected]>
WalkthroughThe 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 Changes
Possibly related PRs
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
📒 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 namingThe change from
section
todesired_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 namingThe change from
section
todesired_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 formattingThe 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 ofcrab_utils/num_safety.hpp
enhances type safetyIncluding
crab_utils/num_safety.hpp
provides access togsl::narrow
, ensuring safer type conversions in the code that follows.
48-48
: Use ofcrab::to_signed
improves immediate value handlingReplacing 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 inoperator<<
forReg
usinggsl::narrow
Using
gsl::narrow<int>(a.v)
ensures safe conversion of the register value to anint
, preventing potential overflows or truncation issues.
57-70
: Simplifiedoperator<<
overloads improve maintainabilityDelegating the output to
Instruction{a}
in theoperator<<
overloads enhances code maintainability and ensures consistent output formatting across different instruction types.
28-31
: Ensure exception safety when usinggsl::narrow
inlabel_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 ofgsl::narrow<int>
for safe type conversion.By using
gsl::narrow<int>(raw_block.size())
, you ensure that any narrowing fromsize_t
toint
is checked at runtime, preventing potential data loss or overflow. This enhances the robustness of the code.
299-299
: Good use ofgsl::narrow<int>
for safe type conversion.Wrapping
memory_bytes.size()
withgsl::narrow<int>
ensures that the conversion fromsize_t
toint
is safe, and any value that cannot be represented in anint
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 forgsl::narrow
functionsIncluding
"crab_utils/num_safety.hpp"
is appropriate to enable the use ofgsl::narrow
andgsl::narrow_cast
functions for safer type conversions.
41-41
: Handle potential overflow when castingcache.size()
Using
gsl::narrow<int>
onglobal_program_info->cache.size()
assumes that the cache size will always fit within anint
. If the cache size exceedsINT_MAX
, this will throw an exception.Please confirm that
global_program_info->cache.size()
cannot exceedINT_MAX
. If it can, consider using a larger integer type or handling the exception accordingly.
253-254
: Ensure safe narrowing ofreloc.source_offset
Casting
reloc.source_offset
withgsl::narrow<int64_t>
assumes that its value fits withinint64_t
. Ifreloc.source_offset
exceeds the range ofint64_t
, this will throw an exception.Please verify that
reloc.source_offset
is guaranteed to be within the range ofint64_t
. If not, consider using a wider integer type or handling potential exceptions.
269-269
: Safely castingmap.type_id
toint
Using
gsl::narrow_cast<int>
to castmap.type_id
ensures type safety. Confirm thatmap.type_id
is within the range ofint
to prevent unexpected behavior.Please ensure that
map.type_id
cannot exceed the limits of anint
. If there's a possibility, consider using a larger type or handling the casting carefully.
383-383
: Confirm thatprogram_offset
fits withinuint32_t
Casting
program_offset
withgsl::narrow_cast<uint32_t>
assumes that its value is within the range ofuint32_t
. Ifprogram_offset
exceedsUINT32_MAX
, this could lead to incorrect behavior.Please verify that
program_offset
will always be less than or equal toUINT32_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 ofcrab_utils/num_safety.hpp
The inclusion of
crab_utils/num_safety.hpp
is appropriate to utilize numerical safety functions likecrab::to_unsigned
.
222-222
: Appropriate use ofgsl::narrow<Atomic::Op>
for safe type conversionUsing
gsl::narrow<Atomic::Op>
ensures that the narrowing conversion frominst.imm & ~INST_FETCH
toAtomic::Op
is safe at runtime, throwing an exception if the value cannot be represented. This enhances type safety.
236-236
: Correct use ofcrab::to_unsigned
for sign extensionReplacing the previous casting with
crab::to_unsigned(int64_t{imm})
insign_extend
ensures accurate sign extension from a 32-bit integer to an unsigned 64-bit integer, enhancing readability and safety.
238-238
: Correct use ofcrab::to_unsigned
for zero extensionUsing
crab::to_unsigned(imm)
inzero_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 withgsl::narrow<uint8_t>(i)
Using
gsl::narrow<uint8_t>(i)
safely converts the indexi
touint8_t
, ensuring it fits within the expected range. Sincei
ranges from 1 to 5 in this context, the narrowing should not cause issues.
541-542
: Validate indices when narrowing touint8_t
When using
gsl::narrow<uint8_t>(i)
andgsl::narrow<uint8_t>(i + 1)
, ensure thati
andi + 1
are within theuint8_t
range to prevent exceptions. Given the controlled range ofi
in this context, this usage is appropriate.
559-559
: Appropriate use ofgsl::narrow<int>
for label conversionConverting
new_pc
tolabel_t
usinggsl::narrow<int>(new_pc)
ensures that the program counter fits into anint
, providing type safety.
585-585
: Safe narrowing ofinst.imm
touint8_t
Using
gsl::narrow<uint8_t>(inst.imm)
inCallx{}
ensures thatinst.imm
is within the valid range for auint8_t
, enhancing type safety.
691-693
: Correct use ofsign_extend
for immediate values in jump conditionsUsing
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 tomakeLddw
with updated parametersThe call to
makeLddw(inst, next_imm, insts, pc)
appropriately passesnext_imm
, ensuring that the 64-bit immediate value is correctly constructed from two instructions.
780-780
: Ensurepc
fits intoint
when creating labelsUsing
gsl::narrow<int>(pc)
when constructinglabel_t
ensures that the program counter value fits into anint
, adding type safety to label handling.
Summary by CodeRabbit
New Features
Bug Fixes
Documentation