builder/
builder.rs

1// Copyright © 2021 Keegan Saunders
2//
3// Permission to use, copy, modify, and/or distribute this software for
4// any purpose with or without fee is hereby granted.
5//
6// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
7// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
8// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
9// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
10// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
11// AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
12// OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
13//
14
15use vtil_parser::{ArchitectureIdentifier, InstructionBuilder, RegisterDesc, Result, Routine, Vip};
16
17fn main() -> Result<()> {
18    let mut routine = Routine::new(ArchitectureIdentifier::Virtual);
19    routine.header.arch_id = ArchitectureIdentifier::Amd64;
20    let basic_block = routine.create_block(Vip(0)).unwrap();
21    let mut builder = InstructionBuilder::from(basic_block);
22    let tmp1 = RegisterDesc::X86_REG_RAX;
23
24    for i in 0..100 {
25        builder
26            .add(tmp1, 13u32.into())
27            .nop()
28            .sub(tmp1, 12u32.into())
29            .nop()
30            .add(tmp1, 14u32.into())
31            .mov(tmp1, tmp1.into())
32            .sub(tmp1, tmp1.into())
33            .xor(tmp1, (i as u32).into())
34            .push(tmp1.into());
35    }
36
37    builder.vpinr(tmp1).vexit(0u64.into());
38
39    std::fs::write("built.vtil", routine.into_bytes()?)?;
40    Ok(())
41}