Skip to content

Commit

Permalink
Explicit transmute types (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
veeenu authored May 1, 2024
1 parent 19e2f1f commit 969261f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 16 deletions.
12 changes: 9 additions & 3 deletions src/hooks/dx11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,12 @@ fn get_target_addrs() -> DXGISwapChainPresentType {

let swap_chain = p_swap_chain.unwrap();

let present_ptr: DXGISwapChainPresentType =
unsafe { mem::transmute(swap_chain.vtable().Present) };
let present_ptr: DXGISwapChainPresentType = unsafe {
mem::transmute::<
unsafe extern "system" fn(*mut c_void, u32, u32) -> HRESULT,
DXGISwapChainPresentType,
>(swap_chain.vtable().Present)
};

present_ptr
}
Expand Down Expand Up @@ -167,7 +171,9 @@ impl ImguiDx11Hooks {

RENDER_LOOP.get_or_init(|| Box::new(t));
TRAMPOLINES.get_or_init(|| Trampolines {
dxgi_swap_chain_present: mem::transmute(hook_present.trampoline()),
dxgi_swap_chain_present: mem::transmute::<*mut c_void, DXGISwapChainPresentType>(
hook_present.trampoline(),
),
});

Self([hook_present])
Expand Down
14 changes: 11 additions & 3 deletions src/hooks/dx12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,17 @@ impl ImguiDx12Hooks {
RENDER_LOOP.get_or_init(|| Box::new(t));

TRAMPOLINES.get_or_init(|| Trampolines {
dxgi_swap_chain_present: mem::transmute(hook_present.trampoline()),
dxgi_swap_chain_resize_buffers: mem::transmute(hook_resize_buffers.trampoline()),
d3d12_command_queue_execute_command_lists: mem::transmute(hook_cqecl.trampoline()),
dxgi_swap_chain_present: mem::transmute::<*mut c_void, DXGISwapChainPresentType>(
hook_present.trampoline(),
),
dxgi_swap_chain_resize_buffers: mem::transmute::<
*mut c_void,
DXGISwapChainResizeBuffersType,
>(hook_resize_buffers.trampoline()),
d3d12_command_queue_execute_command_lists: mem::transmute::<
*mut c_void,
D3D12CommandQueueExecuteCommandListsType,
>(hook_cqecl.trampoline()),
});

Self([hook_present, hook_resize_buffers, hook_cqecl])
Expand Down
23 changes: 20 additions & 3 deletions src/hooks/dx9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,24 @@ fn get_target_addrs() -> (Dx9PresentType, Dx9ResetType) {
let present_ptr = device.vtable().Present;
let reset_ptr = device.vtable().Reset;

unsafe { (mem::transmute(present_ptr), mem::transmute(reset_ptr)) }
unsafe {
(
mem::transmute::<
unsafe extern "system" fn(
*mut c_void,
*const RECT,
*const RECT,
HWND,
*const RGNDATA,
) -> HRESULT,
Dx9PresentType,
>(present_ptr),
mem::transmute::<
unsafe extern "system" fn(*mut c_void, *mut D3DPRESENT_PARAMETERS) -> HRESULT,
Dx9ResetType,
>(reset_ptr),
)
}
}

/// Hooks for DirectX 9.
Expand Down Expand Up @@ -182,8 +199,8 @@ impl ImguiDx9Hooks {

RENDER_LOOP.get_or_init(|| Box::new(t));
TRAMPOLINES.get_or_init(|| Trampolines {
dx9_present: mem::transmute(hook_present.trampoline()),
dx9_reset: mem::transmute(hook_reset.trampoline()),
dx9_present: mem::transmute::<*mut c_void, Dx9PresentType>(hook_present.trampoline()),
dx9_reset: mem::transmute::<*mut c_void, Dx9ResetType>(hook_reset.trampoline()),
});

Self([hook_present, hook_reset])
Expand Down
8 changes: 5 additions & 3 deletions src/hooks/opengl3.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Hooks for OpenGL 3.

use std::ffi::CString;
use std::ffi::{c_void, CString};
use std::mem;
use std::sync::OnceLock;

Expand Down Expand Up @@ -86,7 +86,9 @@ unsafe fn get_opengl_wglswapbuffers_addr() -> OpenGl32wglSwapBuffersType {
let wglswapbuffers_func =
GetProcAddress(opengl32module, PCSTR(wglswapbuffers.as_ptr() as *mut _)).unwrap();

mem::transmute(wglswapbuffers_func)
mem::transmute::<unsafe extern "system" fn() -> isize, OpenGl32wglSwapBuffersType>(
wglswapbuffers_func,
)
}

/// Hooks for OpenGL 3.
Expand Down Expand Up @@ -119,7 +121,7 @@ impl ImguiOpenGl3Hooks {
// Initialize the render loop and store detours
RENDER_LOOP.get_or_init(move || Box::new(t));
TRAMPOLINES.get_or_init(|| Trampolines {
opengl32_wgl_swap_buffers: std::mem::transmute(
opengl32_wgl_swap_buffers: mem::transmute::<*mut c_void, OpenGl32wglSwapBuffersType>(
hook_opengl_wgl_swap_buffers.trampoline(),
),
});
Expand Down
10 changes: 8 additions & 2 deletions src/inject.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Facilities for injecting compiled DLLs into target processes.

use std::ffi::c_void;
use std::mem::{self, size_of};
use std::path::PathBuf;

Expand Down Expand Up @@ -58,7 +59,7 @@ impl Process {
WriteProcessMemory(
self.0,
dll_path_buf,
dll_path.as_ptr() as *const std::ffi::c_void,
dll_path.as_ptr() as *const c_void,
(MAX_PATH as usize) * size_of::<u16>(),
Some(&mut bytes_written),
)
Expand All @@ -71,7 +72,12 @@ impl Process {
self.0,
None,
0,
Some(std::mem::transmute(proc_addr)),
proc_addr.map(|proc_addr| {
mem::transmute::<
unsafe extern "system" fn() -> isize,
unsafe extern "system" fn(*mut c_void) -> u32,
>(proc_addr)
}),
Some(dll_path_buf),
0,
None,
Expand Down
7 changes: 6 additions & 1 deletion src/renderer/backend/opengl3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ mod gl {
clippy::too_many_arguments,
clippy::unused_unit,
clippy::upper_case_acronyms,
clippy::manual_non_exhaustive
clippy::manual_non_exhaustive,
// We support stable but lint on nightly. The following lint isn't available on stable,
// so allow `unknown_lints`.
unknown_lints,
clippy::missing_transmute_annotations
)]

include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs"));
Expand Down
11 changes: 10 additions & 1 deletion src/renderer/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,16 @@ impl<T: RenderEngine> Pipeline<T> {
}

let wnd_proc = unsafe {
mem::transmute(SetWindowLongPtrA(hwnd, GWLP_WNDPROC, pipeline_wnd_proc as usize as _))
#[cfg(target_arch = "x86")]
type SwlpRet = i32;
#[cfg(target_arch = "x86_64")]
type SwlpRet = isize;

mem::transmute::<SwlpRet, WndProcType>(SetWindowLongPtrA(
hwnd,
GWLP_WNDPROC,
pipeline_wnd_proc as usize as _,
))
};

let (tx, rx) = mpsc::channel();
Expand Down

0 comments on commit 969261f

Please sign in to comment.