Skip to content
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

BasicBlock class #1121

Closed
JonathanSalwan opened this issue Apr 14, 2022 · 3 comments
Closed

BasicBlock class #1121

JonathanSalwan opened this issue Apr 14, 2022 · 3 comments
Assignees
Milestone

Comments

@JonathanSalwan
Copy link
Owner

Introducing basic blocks could bring cool features in the future. Currently i'm adding this to perform dead code elimination but it can be used in the future to represent CFG for example.

@JonathanSalwan JonathanSalwan added this to the v1.0 milestone Apr 14, 2022
@JonathanSalwan JonathanSalwan self-assigned this Apr 14, 2022
JonathanSalwan added a commit that referenced this issue Apr 14, 2022
@JonathanSalwan
Copy link
Owner Author

First example.

Disassembling and print a basic block

using namespace triton::arch;

// Creating the block
BasicBlock block = BasicBlock({
  Instruction((unsigned char *)"\x89\xd0", 2),      /* mov   eax, edx  */
  Instruction((unsigned char *)"\x80\xf4\x99", 3),  /* xor   ah, 0x99  */
  Instruction((unsigned char *)"\x85\xc0", 2),      /* test  eax, eax  */
  Instruction((unsigned char *)"\x74\x08", 2),      /* jz    10        */
});

// Disassemble the block
api.disassembly(block);

// Print the block
std::cout << block << std::endl;

// Result:
// 0x0: mov eax, edx
// 0x2: xor ah, 0x99
// 0x5: test eax, eax
// 0x7: je 0x11

Disassembling an instruction and add it to the block

// Create an instruction
auto i = Instruction((unsigned char *)"\x90", 1); /* nop */

// Disass the instruction
api.disassembly(i);

// Add it 3 times to the block
block.add(i);
block.add(i);
block.add(i);

// Print the block
std::cout << block << std::endl;

// Result
// 0x0: mov eax, edx
// 0x2: xor ah, 0x99
// 0x5: test eax, eax
// 0x7: je 0x11
// 0x9: nop
// 0xa: nop
// 0xb: nop

Disassembling a block from a base address

api.disassembly(block, 0x1000);
std::cout << block << std::endl;

// Result
// 0x1000: mov eax, edx
// 0x1002: xor ah, 0x99
// 0x1005: test eax, eax
// 0x1007: je 0x1011
// 0x1009: nop
// 0x100a: nop
// 0x100b: nop

Process a block

// Removing the instruction JE in order to not have instruction after a branch instruction
// Note that I could have also removed NOPs instead of the the JE.
block.remove(3);

// Process the block
// processing() raises an exception if there are instructions after a branch instruction.
// You can also execute a block which do not contains branch instruction.
api.processing(block);

@JonathanSalwan
Copy link
Owner Author

@SweetVishnya, when implementing this I remembered those lines you added months ago. I know you use those methods and I'm wondering if we can return a BasicBlock instead of a std::vector. Then, you can deal with the block and if you want to get instructions you can call BasicBlock::getInstructions() which returns a std::vector<triton::arch::Instruction>&.

@SweetVishnya
Copy link
Contributor

@SweetVishnya, when implementing this I remembered those lines you added months ago. I know you use those methods and I'm wondering if we can return a BasicBlock instead of a std::vector. Then, you can deal with the block and if you want to get instructions you can call BasicBlock::getInstructions() which returns a std::vector<triton::arch::Instruction>&.

Actually, we do not use that method :P We manage memory mapped files and get single instruction after insruction from it and manually call processing instruction-level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants