Fix dumping on x64 Linux (and possibly Windows)

This commit is contained in:
Andrzej Janik
2021-09-10 19:21:25 +02:00
parent da9cf4d583
commit ab67cd46fc
2 changed files with 25 additions and 4 deletions

View File

@ -1,5 +1,6 @@
use crate::cuda::CUuuid; use crate::cuda::CUuuid;
use std::ffi::{c_void, CStr}; use std::ffi::{c_void, CStr};
use std::mem;
const NVCUDA_DEFAULT_PATH: &'static [u8] = b"/usr/lib/x86_64-linux-gnu/libcuda.so.1\0"; const NVCUDA_DEFAULT_PATH: &'static [u8] = b"/usr/lib/x86_64-linux-gnu/libcuda.so.1\0";
@ -36,23 +37,34 @@ pub fn get_thunk(
guid: *const CUuuid, guid: *const CUuuid,
idx: usize, idx: usize,
) -> *const c_void { ) -> *const c_void {
use std::mem;
use dynasmrt::{dynasm, DynasmApi}; use dynasmrt::{dynasm, DynasmApi};
let mut ops = dynasmrt::x86::Assembler::new().unwrap(); let mut ops = dynasmrt::x86::Assembler::new().unwrap();
let start = ops.offset(); let start = ops.offset();
// Let's hope there's never more than 6 arguments
dynasm!(ops dynasm!(ops
; .arch x64 ; .arch x64
; push rbp
; mov rbp, rsp
; push rdi ; push rdi
; push rsi ; push rsi
; push rdx
; push rcx
; push r8
; push r9
; mov rdi, QWORD guid as i64 ; mov rdi, QWORD guid as i64
; mov rsi, QWORD idx as i64 ; mov rsi, QWORD idx as i64
; mov rax, QWORD report_fn as i64 ; mov rax, QWORD report_fn as i64
; call rax ; call rax
; pop r9
; pop r8
; pop rcx
; pop rdx
; pop rsi ; pop rsi
; pop rdi ; pop rdi
; mov rax, QWORD original_fn as i64 ; mov rax, QWORD original_fn as i64
; jmp rax ; call rax
; pop rbp
; ret
; int 3 ; int 3
); );
let exe_buf = ops.finalize().unwrap(); let exe_buf = ops.finalize().unwrap();

View File

@ -137,18 +137,27 @@ pub fn get_thunk(
use dynasmrt::{dynasm, DynasmApi}; use dynasmrt::{dynasm, DynasmApi};
let mut ops = dynasmrt::x86::Assembler::new().unwrap(); let mut ops = dynasmrt::x86::Assembler::new().unwrap();
let start = ops.offset(); let start = ops.offset();
// Let's hope there's never more than 4 arguments
dynasm!(ops dynasm!(ops
; .arch x64 ; .arch x64
; push rbp
; mov rbp, rsp
; push rcx ; push rcx
; push rdx ; push rdx
; push r8
; push r9
; mov rcx, QWORD guid as i64 ; mov rcx, QWORD guid as i64
; mov rdx, QWORD idx as i64 ; mov rdx, QWORD idx as i64
; mov rax, QWORD report_fn as i64 ; mov rax, QWORD report_fn as i64
; call rax ; call rax
; pop r9
; pop r8
; pop rdx ; pop rdx
; pop rcx ; pop rcx
; mov rax, QWORD original_fn as i64 ; mov rax, QWORD original_fn as i64
; jmp rax ; call rax
; pop rbp
; ret
; int 3 ; int 3
); );
let exe_buf = ops.finalize().unwrap(); let exe_buf = ops.finalize().unwrap();