-
Notifications
You must be signed in to change notification settings - Fork 44
High-level disassembly API #522
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
Open
vchuravy
wants to merge
2
commits into
master
Choose a base branch
from
vc/untangle_disasm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
@checked struct DisasmContext | ||
ref::API.LLVMDisasmContextRef | ||
end | ||
Base.unsafe_convert(::Type{API.LLVMDisasmContextRef}, dc::DisasmContext) = dc.ref | ||
|
||
function create_disasm(triple) | ||
ctx = DisasmContext(API.LLVMCreateDisasm(triple, C_NULL, 0, C_NULL, C_NULL)) | ||
return ctx | ||
end | ||
|
||
function dispose(ctx::DisasmContext) | ||
API.LLVMDisasmDispose(ctx.ref) | ||
return nothing | ||
end | ||
|
||
function disassemble_code(io, ctx::DisasmContext, native_code::Vector{UInt8}, code_addr::Csize_t = 0; bufsize=32) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about simply |
||
pos = 1 | ||
buf = Vector{UInt8}(undef, bufsize) | ||
|
||
while pos < length(native_code) | ||
GC.@preserve native_code buf begin | ||
bytes = pointer(native_code, pos) | ||
pc = code_addr + pos - 1 | ||
nbytes = length(native_code) - pos + 1 | ||
# Need to use pointer(buf) here since the API is annotated as Cstring | ||
nb = API.LLVMDisasmInstruction(ctx, bytes, nbytes, pc, pointer(buf), bufsize) | ||
end | ||
if nb == 0 | ||
# Disassembly failed | ||
break | ||
end | ||
pos += nb | ||
for byte in buf | ||
byte == 0x00 && break | ||
write(io, byte) | ||
end | ||
write(io, '\n') | ||
end | ||
return nothing | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import LLVM: create_disasm, disassemble_code | ||
|
||
@testset "X86 Disassembly" begin | ||
# Example from jitdump | ||
native_code = UInt8[0x55, 0x48, 0x89, 0xe5, 0x49, 0x8b, 0x45, 0x10, 0x48, 0x8b, 0x40, 0x10, 0x48, 0x8b, 0x00, 0x48, 0x8b, 0x16, 0x48, 0x83, 0xc6, 0x08, 0x48, 0xb8, 0xe0, 0x7c, 0xf5, 0x81, 0xe4, 0x7f, 0x00, 0x00, 0xff, 0xd0, 0x5d, 0xc3] | ||
code_addr = 0x00007fe48befcde0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a comment explaining the PC |
||
|
||
ctx = create_disasm("x86_64-pc-linux-gnu") | ||
disassembled = sprint(disassemble_code, ctx, native_code, code_addr) | ||
LLVM.dispose(ctx) | ||
|
||
@test disassembled == "\tpushq\t%rbp\n\tmovq\t%rsp, %rbp\n\tmovq\t16(%r13), %rax\n\tmovq\t16(%rax), %rax\n\tmovq\t(%rax), %rax\n\tmovq\t(%rsi), %rdx\n\taddq\t\$8, %rsi\n\tmovabsq\t\$140619409620192, %rax\n\tcallq\t*%rax\n\tpopq\t%rbp\n" | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is my OOP kicking in, but why not tie this to a
DisasmContext
constructor like we do for other objects-like things (Context, IRBuilder, etc)?