-
Notifications
You must be signed in to change notification settings - Fork 233
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
Add debug checks to const gep argument bit sizes to resolve #213 #214
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,17 @@ impl<'ctx> PointerValue<'ctx> { | |
|
||
// REVIEW: Should this be on array value too? | ||
/// GEP is very likely to segfault if indexes are used incorrectly, and is therefore an unsafe function. Maybe we can change this in the future. | ||
/// GEP indexes must be 32 bit integer values. Constant folding may result in other sizes working. | ||
pub unsafe fn const_gep(self, ordered_indexes: &[IntValue<'ctx>]) -> PointerValue<'ctx> { | ||
if cfg!(debug_assertions) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not make this just on debug builds. Imo, preventing UB is something we should always do. |
||
for (index, value) in ordered_indexes.iter().enumerate() { | ||
let bit_width = value.get_type().get_bit_width(); | ||
if bit_width != 32 { | ||
panic!("Index #{} in ordered_indexes argument to const_gep call was a {} bit integer instead of 32 bit integer - gep indexes must be i32 values", index, bit_width); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's return results instead. IE |
||
} | ||
} | ||
} | ||
|
||
let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter() | ||
.map(|val| val.as_value_ref()) | ||
.collect(); | ||
|
@@ -80,7 +90,17 @@ impl<'ctx> PointerValue<'ctx> { | |
} | ||
|
||
/// GEP is very likely to segfault if indexes are used incorrectly, and is therefore an unsafe function. Maybe we can change this in the future. | ||
/// GEP indexes must be 32 bit integer values. Constant folding may result in other sizes working. | ||
pub unsafe fn const_in_bounds_gep(self, ordered_indexes: &[IntValue<'ctx>]) -> PointerValue<'ctx> { | ||
if cfg!(debug_assertions) { | ||
for (index, value) in ordered_indexes.iter().enumerate() { | ||
let bit_width = value.get_type().get_bit_width(); | ||
if bit_width != 32 { | ||
panic!("Index #{} in ordered_indexes argument to const_in_bounds_gep call was a {} bit integer instead of 32 bit integer - gep indexes must be i32 values", index, bit_width); | ||
} | ||
} | ||
} | ||
|
||
let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter() | ||
.map(|val| val.as_value_ref()) | ||
.collect(); | ||
|
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.
Let's remove the "Constant folding may result in other sizes working." comment. It's a pretty neat insight but doesn't help users in any way. You can move it to a regular, non-doc comment if you'd like.