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

Jit Code Disassembler? #121

Open
hsqStephenZhang opened this issue Feb 6, 2025 · 3 comments
Open

Jit Code Disassembler? #121

hsqStephenZhang opened this issue Feb 6, 2025 · 3 comments

Comments

@hsqStephenZhang
Copy link
Contributor

hsqStephenZhang commented Feb 6, 2025

motivation

disassemble the code into x86/aarch64 instruction format for debug usage, this can be gated behind a disassemble feature

example

demo code

// in JitMemory
//capstone = { version = "0.13", optional = true }
let cs = capstone::Capstone::new()
                  .x86() // shall handle x86 and aarch64 in the real world
                  .mode(arch::x86::ArchMode::Mode64)
                  .syntax(arch::x86::ArchSyntax::Att)
                  .detail(detail)
                  .build()
                  .unwrap()
let code = &self.contents[0..self.offset];
let insns = cs.disasm_all(code, 0).unwrap();
for insn in insns {
    println!("{}", insn);
}

output

for instructions

    let prog = &[
        0x71, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // ldxh r0, [r1+2]
        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // exit
    ];

output is

0x0: pushq %rbp
0x1: pushq %rbx
0x2: pushq %r13
0x4: pushq %r14
0x6: pushq %r15
0x8: movq %rdx, %r10
0xb: movq %rdx, %rdi
0xe: movq %rsp, %rbp
0x11: subq $0x200, %rsp
0x18: movzbl 2(%rdi), %eax
0x1c: addq $0x200, %rsp
0x23: popq %r15
0x25: popq %r14
0x27: popq %r13
0x29: popq %rbx
0x2a: popq %rbp
0x2b: retq 
@hsqStephenZhang
Copy link
Contributor Author

i'd like to make a draft first if it's wanted

@qmonnet
Copy link
Owner

qmonnet commented Feb 6, 2025

Thanks for the proposal, but I don't think we really need that in the crate. If users want to disassemble their program, they can probably do it outside of the library. I'm not sure we expose a way to retrieve the JITed program in rbpf, but we could add a function to do that; and then the user is free to disassemble or otherwise manipulate the JITed instructions in their app. What do you think?

@hsqStephenZhang
Copy link
Contributor Author

yeah, expose the JITed instructions as a &[u8] would be better

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

No branches or pull requests

2 participants