Skip to content

Commit 31f1936

Browse files
committed
[Rust] Add LowLevelILInstruction::basic_block / LowLevelILFunction::basic_block_containing_index
1 parent 1431470 commit 31f1936

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

rust/src/low_level_il/function.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,21 @@ where
159159
Array::new(blocks, count, context)
160160
}
161161
}
162+
163+
/// Returns the [`BasicBlock`] at the given instruction `index`.
164+
///
165+
/// You can also retrieve this using [`LowLevelILInstruction::basic_block`].
166+
pub fn basic_block_containing_index(
167+
&self,
168+
index: LowLevelInstructionIndex,
169+
) -> Option<Ref<BasicBlock<LowLevelILBlock<M, F>>>> {
170+
let block = unsafe { BNGetLowLevelILBasicBlockForInstruction(self.handle, index.0) };
171+
if block.is_null() {
172+
None
173+
} else {
174+
Some(unsafe { BasicBlock::ref_from_raw(block, LowLevelILBlock { function: self }) })
175+
}
176+
}
162177
}
163178

164179
impl<M: FunctionMutability> LowLevelILFunction<M, NonSSA> {

rust/src/low_level_il/instruction.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use crate::basic_block::BasicBlock;
16+
use crate::rc::Ref;
17+
18+
use super::block::LowLevelILBlock;
1519
use super::operation;
1620
use super::operation::Operation;
1721
use super::VisitorAction;
@@ -98,6 +102,12 @@ where
98102
pub fn into_raw(&self) -> BNLowLevelILInstruction {
99103
unsafe { BNGetLowLevelILByIndex(self.function.handle, self.expr_idx().0) }
100104
}
105+
106+
/// Returns the [`BasicBlock`] containing the given [`LowLevelILInstruction`].
107+
pub fn basic_block(&self) -> Option<Ref<BasicBlock<LowLevelILBlock<M, F>>>> {
108+
// TODO: We might be able to .expect this if we guarantee that self.index is valid.
109+
self.function.basic_block_containing_index(self.index)
110+
}
101111
}
102112

103113
impl<M, F> Debug for LowLevelILInstruction<'_, M, F>

0 commit comments

Comments
 (0)