gbf_core/function.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
#![deny(missing_docs)]
use petgraph::graph::{DiGraph, NodeIndex};
use petgraph::visit::{DfsPostOrder, Walker};
use petgraph::Direction;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::{self, Display, Formatter};
use std::hash::Hash;
use std::ops::{Deref, Index};
use thiserror::Error;
use crate::basic_block::{BasicBlock, BasicBlockId, BasicBlockType};
use crate::cfg_dot::{CfgDot, CfgDotConfig, DotRenderableGraph, NodeResolver};
use crate::utils::{Gs2BytecodeAddress, GBF_BLUE, GBF_GREEN, GBF_RED};
/// Represents an error that can occur when working with functions.
#[derive(Error, Debug)]
pub enum FunctionError {
/// The requested `BasicBlock` was not found by its block id.
#[error("BasicBlock not found by its block id: {0}")]
BasicBlockNotFoundById(BasicBlockId),
/// The requested `BasicBlock` was not found by its address.
#[error("BasicBlock not found by its address: {0}")]
BasicBlockNotFoundByAddress(Gs2BytecodeAddress),
/// The requested `BasicBlock` does not have a `NodeIndex`.
#[error("BasicBlock with id {0} does not have a NodeIndex")]
BasicBlockNodeIndexNotFound(BasicBlockId),
/// The function already has an entry block.
#[error("Function already has an entry block")]
EntryBlockAlreadyExists,
}
/// Represents the identifier of a function.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct FunctionId {
index: usize,
/// The name of the function, if it is not the entry point.
pub name: Option<String>,
/// The address of the function in the module.
pub address: Gs2BytecodeAddress,
}
impl FunctionId {
/// Create a new `FunctionId`.
///
/// # Arguments
/// - `index`: The index of the function in the module.
/// - `name`: The name of the function, if it is not the entry point.
/// - `address`: The address of the function in the module.
///
/// # Returns
/// - A new `FunctionId` instance.
///
/// # Example
/// ```
/// use gbf_core::function::FunctionId;
///
/// let entry = FunctionId::new_without_name(0, 0);
/// let add = FunctionId::new(1, Some("add"), 0x100);
/// ```
pub fn new<S>(index: usize, name: Option<S>, address: Gs2BytecodeAddress) -> Self
where
S: Into<String>,
{
Self {
index,
name: name.map(|n| n.into()),
address,
}
}
/// Helper method for creating a `FunctionId` without a name.
pub fn new_without_name(index: usize, address: Gs2BytecodeAddress) -> Self {
Self::new(index, None::<String>, address)
}
/// If the function has a name.
///
/// # Returns
/// - `true` if the function has a name.
/// - `false` if the function does not have a name.
///
/// # Example
/// ```
/// use gbf_core::function::FunctionId;
///
/// let entry = FunctionId::new_without_name(0, 0);
///
/// assert!(entry.is_named());
/// ```
pub fn is_named(&self) -> bool {
self.name.is_none()
}
}
/// Represents a function in a module.
#[derive(Debug, Serialize, Deserialize)]
pub struct Function {
/// The identifier of the function.
pub id: FunctionId,
/// A vector of all the `BasicBlock`s in the function.
blocks: Vec<BasicBlock>,
/// Maps `BasicBlockId` to their index in the `blocks` vector.
block_map: HashMap<BasicBlockId, usize>,
/// The control-flow graph of the function.
cfg: DiGraph<(), ()>,
/// Used to convert `NodeIndex` to `BasicBlockId`.
graph_node_to_block: HashMap<NodeIndex, BasicBlockId>,
/// Used to convert `BasicBlockId` to `NodeIndex`.
block_to_graph_node: HashMap<BasicBlockId, NodeIndex>,
/// A map of function addresses to their IDs.
address_to_id: HashMap<Gs2BytecodeAddress, FunctionId>,
}
impl Function {
/// Create a new `Function`. Automatically creates an entry block.
///
/// # Arguments
/// - `id`: The `FunctionId` of the function.
///
/// # Returns
/// - A new `Function` instance.
pub fn new(id: FunctionId) -> Self {
let mut blocks = Vec::new();
let mut block_map = HashMap::new();
let mut graph_node_to_block = HashMap::new();
let mut block_to_graph_node = HashMap::new();
let address_to_id = HashMap::new();
let mut cfg = DiGraph::new();
// Initialize entry block
let entry_block = BasicBlockId::new(blocks.len(), BasicBlockType::Entry, id.address);
blocks.push(BasicBlock::new(entry_block));
block_map.insert(entry_block, 0);
// Add an empty node in the graph to represent this BasicBlock
let entry_node_id = cfg.add_node(());
graph_node_to_block.insert(entry_node_id, entry_block);
block_to_graph_node.insert(entry_block, entry_node_id);
Self {
id,
blocks,
block_map,
cfg,
graph_node_to_block,
block_to_graph_node,
address_to_id,
}
}
/// Create a new `BasicBlock` and add it to the function.
///
/// # Arguments
/// - `block_type`: The type of the block.
///
/// # Returns
/// - A `BasicBlockId` for the new block.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
/// use gbf_core::basic_block::BasicBlockType;
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let block = function.create_block(BasicBlockType::Normal, 0);
/// ```
pub fn create_block(
&mut self,
block_type: BasicBlockType,
address: Gs2BytecodeAddress,
) -> Result<BasicBlockId, FunctionError> {
// do not allow entry block to be created more than once
if block_type == BasicBlockType::Entry {
return Err(FunctionError::EntryBlockAlreadyExists);
}
let id = BasicBlockId::new(self.blocks.len(), block_type, address);
self.blocks.push(BasicBlock::new(id));
self.block_map.insert(id, self.blocks.len() - 1);
// Insert a node in the petgraph to represent this BasicBlock
let node_id = self.cfg.add_node(());
self.block_to_graph_node.insert(id, node_id);
self.graph_node_to_block.insert(node_id, id);
Ok(id)
}
/// Get a reference to a `BasicBlock` by its `BasicBlockId`.
///
/// # Arguments
/// - `id`: The `BasicBlockId` of the block.
///
/// # Returns
/// - A reference to the `BasicBlock`.
///
/// # Errors
/// - `FunctionError::BasicBlockNotFound` if the block does not exist.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
/// use gbf_core::basic_block::BasicBlockType;
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let block_id = function.create_block(BasicBlockType::Normal, 0).unwrap();
/// let block_ref = function.get_basic_block_by_id(block_id).unwrap();
/// ```
pub fn get_basic_block_by_id(&self, id: BasicBlockId) -> Result<&BasicBlock, FunctionError> {
let index = self
.block_map
.get(&id)
.ok_or(FunctionError::BasicBlockNotFoundById(id))?;
Ok(&self.blocks[*index])
}
/// Get a reference to a `BasicBlock` by its address. The block address
/// -must- be the start address of the block.
///
/// # Arguments
/// - `id`: The `BasicBlockId` of the block.
///
/// # Returns
/// - A mutable reference to the `BasicBlock`.
///
/// # Errors
/// - `FunctionError::BasicBlockNotFound` if the block does not exist.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
/// use gbf_core::basic_block::BasicBlockType;
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let block_id = function.create_block(BasicBlockType::Normal, 0).unwrap();
/// let block_ref = function.get_basic_block_by_id_mut(block_id).unwrap();
/// ```
pub fn get_basic_block_by_id_mut(
&mut self,
id: BasicBlockId,
) -> Result<&mut BasicBlock, FunctionError> {
let index = self
.block_map
.get(&id)
.ok_or(FunctionError::BasicBlockNotFoundById(id))?;
Ok(&mut self.blocks[*index])
}
/// Get a reference to a `BasicBlock` by its address.
///
/// # Arguments
/// - `address`: The address of the block.
///
/// # Returns
/// - A reference to the `BasicBlock`.
///
/// # Errors
/// - `FunctionError::BasicBlockNotFoundByAddress` if the block does not exist.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
/// use gbf_core::basic_block::BasicBlockType;
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let block_id = function.create_block(BasicBlockType::Normal, 0x100).unwrap();
/// let block_ref = function.get_basic_block_by_start_address(0x100).unwrap();
/// ```
pub fn get_basic_block_by_start_address(
&self,
address: Gs2BytecodeAddress,
) -> Result<&BasicBlock, FunctionError> {
let id = self.get_basic_block_id_by_start_address(address)?;
self.get_basic_block_by_id(id)
}
/// Get a reference to a `BasicBlock` by its address (mutable). The block address
/// -must- be the start address of the block.
///
/// # Arguments
/// - `address`: The address of the block.
///
/// # Returns
/// - A reference to the `BasicBlock`.
///
/// # Errors
/// - `FunctionError::BasicBlockNotFoundByAddress` if the block does not exist.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
/// use gbf_core::basic_block::BasicBlockType;
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let block_id = function.create_block(BasicBlockType::Normal, 0x100).unwrap();
/// let block_ref = function.get_basic_block_by_start_address_mut(0x100).unwrap();
/// ```
pub fn get_basic_block_by_start_address_mut(
&mut self,
address: Gs2BytecodeAddress,
) -> Result<&mut BasicBlock, FunctionError> {
let id = self.get_basic_block_id_by_start_address(address)?;
self.get_basic_block_by_id_mut(id)
}
/// Check if a block exists by its address.
///
/// # Arguments
/// - `address`: The address of the block.
///
/// # Returns
/// - `true` if the block exists.
/// - `false` if the block does not exist.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
/// use gbf_core::basic_block::BasicBlockType;
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let block_id = function.create_block(BasicBlockType::Normal, 0x100).unwrap();
/// assert!(function.basic_block_exists_by_address(0x100));
/// ```
pub fn basic_block_exists_by_address(&self, address: Gs2BytecodeAddress) -> bool {
self.blocks.iter().any(|block| block.id.address == address)
}
/// Gets the entry basic block id of the function.
///
/// # Returns
/// - The `BasicBlockId` of the entry block.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let entry = function.get_entry_basic_block_id();
/// ```
pub fn get_entry_basic_block_id(&self) -> BasicBlockId {
self.blocks[0].id
}
/// Get the entry basic block of the function.
///
/// # Returns
/// - A reference to the entry block.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let entry = function.get_entry_basic_block();
/// ```
pub fn get_entry_basic_block(&self) -> &BasicBlock {
self.blocks.first().unwrap()
}
/// Get the entry block of the function.
///
/// # Returns
/// - A mutable reference to the entry block.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let entry = function.get_entry_basic_block_mut();
/// ```
pub fn get_entry_basic_block_mut(&mut self) -> &mut BasicBlock {
self.blocks.first_mut().unwrap()
}
/// Add an edge between two `BasicBlock`s.
///
/// # Arguments
/// - `source`: The `BasicBlockId` of the source block.
/// - `target`: The `BasicBlockId` of the target block.
///
/// # Errors
/// - `FunctionError::BasicBlockNodeIndexNotFound` if either block does not have a `NodeIndex`.
/// - `FunctionError::GraphError` if the edge could not be added to the graph.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
/// use gbf_core::basic_block::BasicBlockType;
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let block1 = function.create_block(BasicBlockType::Normal, 0).unwrap();
/// let block2 = function.create_block(BasicBlockType::Normal, 0).unwrap();
/// function.add_edge(block1, block2);
/// ```
pub fn add_edge(
&mut self,
source: BasicBlockId,
target: BasicBlockId,
) -> Result<(), FunctionError> {
let source_node_id = self
.block_id_to_node_id(source)
.ok_or(FunctionError::BasicBlockNodeIndexNotFound(source))?;
let target_node_id = self
.block_id_to_node_id(target)
.ok_or(FunctionError::BasicBlockNodeIndexNotFound(target))?;
// With petgraph, this does not fail, so we simply do it:
// It can panic if the node does not exist, but we have already checked that.
self.cfg.add_edge(source_node_id, target_node_id, ());
Ok(())
}
/// Get the number of `BasicBlock`s in the function.
///
/// # Returns
/// - The number of `BasicBlock`s in the function.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
/// use gbf_core::basic_block::BasicBlockType;
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let block1 = function.create_block(BasicBlockType::Normal, 0).unwrap();
/// let block2 = function.create_block(BasicBlockType::Normal, 0).unwrap();
/// let block3 = function.create_block(BasicBlockType::Normal, 0).unwrap();
///
/// assert_eq!(function.len(), 4);
/// ```
pub fn len(&self) -> usize {
self.blocks.len()
}
/// Check if the function is empty.
///
/// # Returns
/// - `true` if the function is empty.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
///
/// let function = Function::new(FunctionId::new_without_name(0, 0));
/// assert!(!function.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
// This will always be false since we always create an entry block
self.blocks.is_empty()
}
/// Get the predecessors of a `BasicBlock`.
///
/// # Arguments
/// - `id`: The `BasicBlockId` of the block.
///
/// # Returns
/// - A vector of `BasicBlockId`s that are predecessors of the block.
///
/// # Errors
/// - `FunctionError::BasicBlockNodeIndexNotFound` if the block does not exist.
/// - `FunctionError::GraphError` if the predecessors could not be retrieved from the graph.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
/// use gbf_core::basic_block::BasicBlockType;
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let block1 = function.create_block(BasicBlockType::Normal, 0).unwrap();
/// let block2 = function.create_block(BasicBlockType::Normal, 0).unwrap();
///
/// function.add_edge(block1, block2);
/// let preds = function.get_predecessors(block2).unwrap();
/// ```
pub fn get_predecessors(&self, id: BasicBlockId) -> Result<Vec<BasicBlockId>, FunctionError> {
let node_id = self
.block_id_to_node_id(id)
.ok_or(FunctionError::BasicBlockNodeIndexNotFound(id))?;
// Collect all incoming neighbors
let preds = self
.cfg
.neighbors_directed(node_id, Direction::Incoming)
.collect::<Vec<_>>();
Ok(preds
.into_iter()
.filter_map(|pred| self.node_id_to_block_id(pred))
.collect())
}
/// Get the successors of a `BasicBlock`.
///
/// # Arguments
/// - `id`: The `BasicBlockId` of the block.
///
/// # Returns
/// - A vector of `BasicBlockId`s that are successors of the block.
///
/// # Errors
/// - `FunctionError::BasicBlockNodeIndexNotFound` if the block does not exist.
/// - `FunctionError::GraphError` if the successors could not be retrieved from the graph.
///
/// # Example
/// ```
/// use gbf_core::function::{Function, FunctionId};
/// use gbf_core::basic_block::BasicBlockType;
///
/// let mut function = Function::new(FunctionId::new_without_name(0, 0));
/// let block1 = function.create_block(BasicBlockType::Normal, 0).unwrap();
/// let block2 = function.create_block(BasicBlockType::Normal, 0).unwrap();
///
/// function.add_edge(block1, block2);
/// let succs = function.get_successors(block1).unwrap();
/// ```
pub fn get_successors(&self, id: BasicBlockId) -> Result<Vec<BasicBlockId>, FunctionError> {
let node_id = self
.block_id_to_node_id(id)
.ok_or(FunctionError::BasicBlockNodeIndexNotFound(id))?;
// Collect all outgoing neighbors
let succs = self
.cfg
.neighbors_directed(node_id, Direction::Outgoing)
.collect::<Vec<_>>();
Ok(succs
.into_iter()
.filter_map(|succ| self.node_id_to_block_id(succ))
.collect())
}
/// Get the blocks in reverse post order
///
/// # Arguments
/// - `id`: The `BasicBlockId` of the starting block
///
/// # Returns
/// - A vector of `BasicBlockId`s that sort the graph in reverse post order
///
/// # Errors
/// - `FunctionError::BasicBlockNodeIndexNotFound` if the block does not exist.
/// - `FunctionError::GraphError` if the successors could not be retrieved from the graph.
pub fn get_reverse_post_order(
&self,
id: BasicBlockId,
) -> Result<Vec<BasicBlockId>, FunctionError> {
let node_id = self
.block_id_to_node_id(id)
.ok_or(FunctionError::BasicBlockNodeIndexNotFound(id))?;
let dfs = DfsPostOrder::new(&self.cfg, node_id)
.iter(&self.cfg)
.collect::<Vec<_>>();
Ok(dfs
.into_iter()
.rev()
.filter_map(|node_id| self.node_id_to_block_id(node_id))
.collect())
}
}
/// Internal API for `Function`.
impl Function {
/// Gets a block id based on its address.
///
/// # Arguments
/// - `address`: The address of the block.
///
/// # Returns
/// - The `BasicBlockId` of the block with the corresponding address.
///
/// # Errors
/// - `FunctionError::BasicBlockNotFoundByAddress` if the block does not exist.
pub fn get_basic_block_id_by_start_address(
&self,
address: Gs2BytecodeAddress,
) -> Result<BasicBlockId, FunctionError> {
self.blocks
.iter()
.find(|block| block.id.address == address)
.map(|block| block.id)
.ok_or(FunctionError::BasicBlockNotFoundByAddress(address))
}
/// Convert a `NodeIndex` to a `BasicBlockId`.
///
/// # Arguments
/// - `node_id`: The `NodeIndex` to convert.
///
/// # Returns
/// - The `BasicBlockId` of the block with the corresponding `NodeIndex`.
fn node_id_to_block_id(&self, node_id: NodeIndex) -> Option<BasicBlockId> {
self.graph_node_to_block.get(&node_id).cloned()
}
/// Convert a `BasicBlockId` to a `NodeIndex`.
///
/// # Arguments
/// - `block_id`: The `BasicBlockId` to convert.
///
/// # Returns
/// - The `NodeIndex` of the block with the corresponding `BasicBlockId`.
fn block_id_to_node_id(&self, block_id: BasicBlockId) -> Option<NodeIndex> {
self.block_to_graph_node.get(&block_id).cloned()
}
}
// === Implementations ===
/// Display implementation for `FunctionId`.
impl Display for FunctionId {
/// Display the `Function` as its name.
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if let Some(name) = &self.name {
write!(f, "{}", name)
} else {
write!(f, "Unnamed Function (Entry)")
}
}
}
/// Clone implementation for Function
impl Clone for Function {
fn clone(&self) -> Self {
let mut blocks = Vec::new();
let mut block_map = HashMap::new();
let mut graph_node_to_block = HashMap::new();
let mut block_to_graph_node = HashMap::new();
let address_to_id = HashMap::new();
let mut cfg = DiGraph::new();
// Clone blocks
for block in &self.blocks {
let new_block = block.clone();
let new_block_id = new_block.id;
blocks.push(new_block);
block_map.insert(new_block_id, blocks.len() - 1);
// Insert a node in the petgraph to represent this BasicBlock
let node_id = cfg.add_node(());
block_to_graph_node.insert(new_block_id, node_id);
graph_node_to_block.insert(node_id, new_block_id);
}
// Clone edges
for edge in self.cfg.raw_edges() {
let source = self.graph_node_to_block[&edge.source()];
let target = self.graph_node_to_block[&edge.target()];
let source_node_id = block_to_graph_node[&source];
let target_node_id = block_to_graph_node[&target];
cfg.add_edge(source_node_id, target_node_id, ());
}
Self {
id: self.id.clone(),
blocks,
block_map,
cfg,
graph_node_to_block,
block_to_graph_node,
address_to_id,
}
}
}
/// Deref implementation for Function
impl Deref for Function {
type Target = [BasicBlock];
fn deref(&self) -> &Self::Target {
&self.blocks
}
}
/// Index implementation for function, with usize
impl Index<usize> for Function {
type Output = BasicBlock;
fn index(&self, index: usize) -> &Self::Output {
&self.blocks[index]
}
}
/// IntoIterator implementation immutable reference
impl<'a> IntoIterator for &'a Function {
type Item = &'a BasicBlock;
type IntoIter = std::slice::Iter<'a, BasicBlock>;
fn into_iter(self) -> Self::IntoIter {
self.blocks.iter()
}
}
/// IntoIterator implementation mutable reference
impl<'a> IntoIterator for &'a mut Function {
type Item = &'a mut BasicBlock;
type IntoIter = std::slice::IterMut<'a, BasicBlock>;
fn into_iter(self) -> Self::IntoIter {
self.blocks.iter_mut()
}
}
impl NodeResolver for Function {
type NodeData = BasicBlock;
fn resolve(&self, node_index: NodeIndex) -> Option<&Self::NodeData> {
self.graph_node_to_block
.get(&node_index)
.and_then(|block_id| {
self.block_map
.get(block_id)
.and_then(|index| self.blocks.get(*index))
})
}
fn resolve_edge_color(&self, source: NodeIndex, target: NodeIndex) -> String {
// Get the last instruction of the source block
let source_block_id = self
.graph_node_to_block
.get(&source)
.expect("Source block not found");
let source_block = self
.get_basic_block_by_id(*source_block_id)
.expect("Source block not found");
let source_last_instruction = source_block.last().unwrap();
let target_block_id = self
.graph_node_to_block
.get(&target)
.expect("Target block not found");
let target_block = self
.get_basic_block_by_id(*target_block_id)
.expect("Target block not found");
// Figure out if the edge represents a branch by seeing if the target
// block address is NOT the next address after the source instruction.
let source_last_address = source_last_instruction.address;
let target_address = target_block.id.address;
if source_last_address + 1 != target_address {
// This represents a branch. Color the edge green.
return GBF_GREEN.to_string();
}
// If the opcode of the last instruction is a fall through, color the edge red since
// the target block's address is the next address
if source_last_instruction.opcode.has_fall_through() {
return GBF_RED.to_string();
}
// Otherwise, color the edge cyan (e.g. normal control flow)
GBF_BLUE.to_string()
}
}
impl DotRenderableGraph for Function {
/// Convert the Graph to `dot` format.
///
/// # Returns
/// - A `String` containing the `dot` representation of the graph.
fn render_dot(&self, config: CfgDotConfig) -> String {
let cfg = CfgDot { config };
cfg.render(&self.cfg, self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create_function() {
let id = FunctionId::new_without_name(0, 0);
let function = Function::new(id.clone());
assert_eq!(function.id, id);
assert_eq!(function.blocks.len(), 1);
}
#[test]
fn create_block() {
let id = FunctionId::new_without_name(0, 0);
let mut function = Function::new(id.clone());
let block_id = function.create_block(BasicBlockType::Normal, 32).unwrap();
assert_eq!(function.len(), 2);
// check block id & node id mappings
let node_id = function.block_to_graph_node.get(&block_id).unwrap();
let new_block_id = function.graph_node_to_block.get(node_id).unwrap();
assert_eq!(*new_block_id, block_id);
// test EntryBlockAlreadyExists error
let result = function.create_block(BasicBlockType::Entry, 0);
assert!(result.is_err());
}
#[test]
fn get_block() {
let id = FunctionId::new_without_name(0, 0);
let mut function = Function::new(id.clone());
let block_id = function.create_block(BasicBlockType::Normal, 32).unwrap();
let block = function.get_basic_block_by_id(block_id).unwrap();
assert_eq!(block.id, block_id);
}
#[test]
fn get_block_mut() {
let id = FunctionId::new_without_name(0, 0);
let mut function = Function::new(id);
let block_id = function.create_block(BasicBlockType::Normal, 43).unwrap();
let block = function.get_basic_block_by_id_mut(block_id).unwrap();
block.id = BasicBlockId::new(0, BasicBlockType::Exit, 43);
assert_eq!(block.id, BasicBlockId::new(0, BasicBlockType::Exit, 43));
}
#[test]
fn test_get_block_not_found() {
let id = FunctionId::new_without_name(0, 0);
let function = Function::new(id.clone());
let result =
function.get_basic_block_by_id(BasicBlockId::new(1234, BasicBlockType::Normal, 0));
assert!(result.is_err());
// test mut version
let mut function = Function::new(id.clone());
let result =
function.get_basic_block_by_id_mut(BasicBlockId::new(1234, BasicBlockType::Normal, 0));
assert!(result.is_err());
// get by start address
let result = function.get_basic_block_by_start_address(0x100);
assert!(result.is_err());
// get by start address mut
let result = function.get_basic_block_by_start_address_mut(0x100);
assert!(result.is_err());
}
#[test]
fn test_get_block_by_address() {
let id = FunctionId::new_without_name(0, 0);
let mut function = Function::new(id.clone());
let block_id = function
.create_block(BasicBlockType::Normal, 0x100)
.unwrap();
let block = function.get_basic_block_by_start_address(0x100).unwrap();
assert_eq!(block.id, block_id);
// test mut version
let block = function
.get_basic_block_by_start_address_mut(0x100)
.unwrap();
block.id = BasicBlockId::new(0, BasicBlockType::Exit, 0x100);
assert_eq!(block.id, BasicBlockId::new(0, BasicBlockType::Exit, 0x100));
}
#[test]
fn test_display_function_id() {
let id = FunctionId::new_without_name(0, 0);
assert_eq!(id.to_string(), "Unnamed Function (Entry)");
let id = FunctionId::new(0, Some("test".to_string()), 0);
assert_eq!(id.to_string(), "test");
}
#[test]
fn test_into_iter_mut() {
let id = FunctionId::new_without_name(0, 0);
let mut function = Function::new(id.clone());
let block_id = function.create_block(BasicBlockType::Normal, 32).unwrap();
for block in &mut function {
if block.id == block_id {
block.id = BasicBlockId::new(0, BasicBlockType::Exit, 32);
}
}
let block = function.get_basic_block_by_id(block_id).unwrap();
assert_eq!(block.id, BasicBlockId::new(0, BasicBlockType::Exit, 32));
}
#[test]
fn test_is_named() {
let id = FunctionId::new_without_name(0, 0);
assert!(id.is_named());
let id = FunctionId::new(0, Some("test".to_string()), 0);
assert!(!id.is_named());
}
#[test]
fn test_get_entry_block() {
let id = FunctionId::new_without_name(0, 0);
let function = Function::new(id.clone());
let entry = function.get_entry_basic_block();
assert_eq!(entry.id, function.get_entry_basic_block().id);
}
#[test]
fn test_get_entry_block_mut() {
let id = FunctionId::new_without_name(0, 0);
let mut function = Function::new(id.clone());
let entry_id = function.get_entry_basic_block().id;
let entry = function.get_entry_basic_block_mut();
assert_eq!(entry.id, entry_id);
}
#[test]
fn test_add_edge() {
let id = FunctionId::new_without_name(0, 0);
let mut function = Function::new(id.clone());
let block1 = function.create_block(BasicBlockType::Normal, 32).unwrap();
let block2 = function.create_block(BasicBlockType::Normal, 32).unwrap();
let result = function.add_edge(block1, block2);
assert!(result.is_ok());
let preds = function.get_predecessors(block2).unwrap();
assert_eq!(preds.len(), 1);
assert_eq!(preds[0], block1);
let succs = function.get_successors(block1).unwrap();
assert_eq!(succs.len(), 1);
assert_eq!(succs[0], block2);
// test source not found
let result = function.add_edge(BasicBlockId::new(1234, BasicBlockType::Normal, 0), block2);
assert!(result.is_err());
// test target not found
let result = function.add_edge(block1, BasicBlockId::new(1234, BasicBlockType::Normal, 0));
assert!(result.is_err());
}
#[test]
fn test_basic_block_is_empty() {
// will always be false since we always create an entry block
let id = FunctionId::new_without_name(0, 0);
let function = Function::new(id.clone());
assert!(!function.is_empty());
}
#[test]
fn test_get_predecessors() {
let id = FunctionId::new_without_name(0, 0);
let mut function = Function::new(id.clone());
let block1 = function.create_block(BasicBlockType::Normal, 32).unwrap();
let block2 = function.create_block(BasicBlockType::Normal, 32).unwrap();
function.add_edge(block1, block2).unwrap();
let preds = function.get_predecessors(block2).unwrap();
assert_eq!(preds.len(), 1);
assert_eq!(preds[0], block1);
// test error where block not found
let result = function.get_predecessors(BasicBlockId::new(1234, BasicBlockType::Normal, 0));
assert!(result.is_err());
}
#[test]
fn test_get_successors() {
let id = FunctionId::new_without_name(0, 0);
let mut function = Function::new(id.clone());
let block1 = function.create_block(BasicBlockType::Normal, 32).unwrap();
let block2 = function.create_block(BasicBlockType::Normal, 32).unwrap();
function.add_edge(block1, block2).unwrap();
let succs = function.get_successors(block1).unwrap();
assert_eq!(succs.len(), 1);
assert_eq!(succs[0], block2);
// test error where block not found
let result = function.get_successors(BasicBlockId::new(1234, BasicBlockType::Normal, 0));
assert!(result.is_err());
}
#[test]
fn test_get_entry_basic_block_id() {
let id = FunctionId::new_without_name(0, 0);
let function = Function::new(id.clone());
let entry = function.get_entry_basic_block_id();
assert_eq!(entry, function.get_entry_basic_block().id);
}
}