Skip to content

Commit

Permalink
Marked GEP functions as unsafe due to easy unsafe usage per rust-lang#33
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDan64 committed Jun 16, 2018
1 parent 7bdd870 commit 239ba2c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ The Inkwell team is committed to the following rules:

We expect others to follow these rules as well.

<small>This COC is based on [dpc's](https://github.com/dpc/public/blob/master/COC.md).</small>
<sup><sub>This COC is based on [dpc's](https://github.com/dpc/public/blob/master/COC.md).</sup></sub>
9 changes: 6 additions & 3 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ impl Builder {
}

// REVIEW: Doesn't GEP work on array too?
pub fn build_gep(&self, ptr: &PointerValue, ordered_indexes: &[IntValue], name: &str) -> PointerValue {
/// 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.
pub unsafe fn build_gep(&self, ptr: &PointerValue, ordered_indexes: &[IntValue], name: &str) -> PointerValue {
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");

let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter()
Expand All @@ -108,7 +109,8 @@ impl Builder {
}

// REVIEW: Doesn't GEP work on array too?
pub fn build_in_bounds_gep(&self, ptr: &PointerValue, ordered_indexes: &[IntValue], name: &str) -> PointerValue {
/// 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.
pub unsafe fn build_in_bounds_gep(&self, ptr: &PointerValue, ordered_indexes: &[IntValue], name: &str) -> PointerValue {
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");

let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter()
Expand All @@ -122,7 +124,8 @@ impl Builder {
}

// REVIEW: Shouldn't this take a StructValue? Or does it still need to be PointerValue<StructValue>?
pub fn build_struct_gep(&self, ptr: &PointerValue, index: u32, name: &str) -> PointerValue {
/// 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.
pub unsafe fn build_struct_gep(&self, ptr: &PointerValue, index: u32, name: &str) -> PointerValue {
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");

let value = unsafe {
Expand Down
4 changes: 1 addition & 3 deletions src/execution_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,7 @@ impl ExecutionEngine {

let environment_variables = vec![]; // TODO: Support envp. Likely needs to be null terminated

unsafe {
LLVMRunFunctionAsMain(*self.execution_engine, function.as_value_ref(), raw_args.len() as u32, raw_args.as_ptr(), environment_variables.as_ptr()) // REVIEW: usize to u32 cast ok??
}
LLVMRunFunctionAsMain(*self.execution_engine, function.as_value_ref(), raw_args.len() as u32, raw_args.as_ptr(), environment_variables.as_ptr()) // REVIEW: usize to u32 cast ok??
}

pub fn free_fn_machine_code(&self, function: &FunctionValue) {
Expand Down
6 changes: 4 additions & 2 deletions src/values/ptr_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ impl PointerValue {
}

// REVIEW: Should this be on array value too?
pub fn const_gep(&self, ordered_indexes: &[&IntValue]) -> PointerValue {
/// 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.
pub unsafe fn const_gep(&self, ordered_indexes: &[IntValue]) -> PointerValue {
let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter()
.map(|val| val.as_value_ref())
.collect();
Expand All @@ -77,7 +78,8 @@ impl PointerValue {
PointerValue::new(value)
}

pub fn const_in_bounds_gep(&self, ordered_indexes: &[&IntValue]) -> PointerValue {
/// 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.
pub unsafe fn const_in_bounds_gep(&self, ordered_indexes: &[IntValue]) -> PointerValue {
let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter()
.map(|val| val.as_value_ref())
.collect();
Expand Down

0 comments on commit 239ba2c

Please sign in to comment.