Skip to content

Commit

Permalink
chore: naively set warn(unsafe_ops_in_unsafe_fn) in wgpu-hal
Browse files Browse the repository at this point in the history
Do the simplest mechanical work necessary to enable and satisfy this lint; only put down `unsafe`
blocks, don't try to inspect for correctness or anything else.

N.B.: that there _are_ some adjustments identified that could be made here, like breaking multiple
individual `unsafe` operations into their `unsafe` spans. This is left for a follow-up commit.
  • Loading branch information
ErichDonGubler committed Sep 21, 2022
1 parent bf1e6e5 commit 1f984c3
Show file tree
Hide file tree
Showing 16 changed files with 1,360 additions and 1,053 deletions.
20 changes: 11 additions & 9 deletions wgpu-hal/src/auxil/dxgi/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,23 @@ unsafe extern "system" fn output_debug_string_handler(
exception_info: *mut winnt::EXCEPTION_POINTERS,
) -> i32 {
// See https://stackoverflow.com/a/41480827
let record = &*(*exception_info).ExceptionRecord;
let record = unsafe { &*(*exception_info).ExceptionRecord };
if record.NumberParameters != 2 {
return excpt::EXCEPTION_CONTINUE_SEARCH;
}
let message = match record.ExceptionCode {
winnt::DBG_PRINTEXCEPTION_C => String::from_utf8_lossy(slice::from_raw_parts(
record.ExceptionInformation[1] as *const u8,
record.ExceptionInformation[0],
)),
winnt::DBG_PRINTEXCEPTION_WIDE_C => {
Cow::Owned(String::from_utf16_lossy(slice::from_raw_parts(
winnt::DBG_PRINTEXCEPTION_C => String::from_utf8_lossy(unsafe {
slice::from_raw_parts(
record.ExceptionInformation[1] as *const u8,
record.ExceptionInformation[0],
)
}),
winnt::DBG_PRINTEXCEPTION_WIDE_C => Cow::Owned(String::from_utf16_lossy(unsafe {
slice::from_raw_parts(
record.ExceptionInformation[1] as *const u16,
record.ExceptionInformation[0],
)))
}
)
})),
_ => return excpt::EXCEPTION_CONTINUE_SEARCH,
};

Expand Down
29 changes: 15 additions & 14 deletions wgpu-hal/src/auxil/renderdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,23 @@ impl RenderDoc {
}
};

let get_api: libloading::Symbol<GetApiFn> = match renderdoc_lib.get(b"RENDERDOC_GetAPI\0") {
Ok(api) => api,
Err(e) => {
return RenderDoc::NotAvailable {
reason: format!(
"Unable to get RENDERDOC_GetAPI from renderdoc library '{}': {:?}",
renderdoc_filename, e
),
let get_api: libloading::Symbol<GetApiFn> =
match unsafe { renderdoc_lib.get(b"RENDERDOC_GetAPI\0") } {
Ok(api) => api,
Err(e) => {
return RenderDoc::NotAvailable {
reason: format!(
"Unable to get RENDERDOC_GetAPI from renderdoc library '{}': {:?}",
renderdoc_filename, e
),
}
}
}
};
};
let mut obj = ptr::null_mut();
match get_api(10401, &mut obj) {
match unsafe { get_api(10401, &mut obj) } {
1 => RenderDoc::Available {
api: RenderDocApi {
api: *(obj as *mut renderdoc_sys::RENDERDOC_API_1_4_1),
api: unsafe { *(obj as *mut renderdoc_sys::RENDERDOC_API_1_4_1) },
lib: renderdoc_lib,
},
},
Expand Down Expand Up @@ -115,7 +116,7 @@ impl RenderDoc {
pub unsafe fn start_frame_capture(&self, device_handle: Handle, window_handle: Handle) -> bool {
match *self {
Self::Available { api: ref entry } => {
entry.api.StartFrameCapture.unwrap()(device_handle, window_handle);
unsafe { entry.api.StartFrameCapture.unwrap()(device_handle, window_handle) };
true
}
Self::NotAvailable { ref reason } => {
Expand All @@ -129,7 +130,7 @@ impl RenderDoc {
pub unsafe fn end_frame_capture(&self, device_handle: Handle, window_handle: Handle) {
match *self {
Self::Available { api: ref entry } => {
entry.api.EndFrameCapture.unwrap()(device_handle, window_handle);
unsafe { entry.api.EndFrameCapture.unwrap()(device_handle, window_handle) };
}
Self::NotAvailable { ref reason } => {
log::warn!("Could not end RenderDoc frame capture: {}", reason)
Expand Down
14 changes: 8 additions & 6 deletions wgpu-hal/src/dx11/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,14 @@ impl crate::Queue<super::Api> for super::Queue {
impl super::D3D11Device {
#[allow(trivial_casts)] // come on
pub unsafe fn check_feature_support<T>(&self, feature: d3d11::D3D11_FEATURE) -> T {
let mut value = mem::zeroed::<T>();
let ret = self.CheckFeatureSupport(
feature,
&mut value as *mut T as *mut c_void,
mem::size_of::<T>() as u32,
);
let mut value = unsafe { mem::zeroed::<T>() };
let ret = unsafe {
self.CheckFeatureSupport(
feature,
&mut value as *mut T as *mut c_void,
mem::size_of::<T>() as u32,
)
};
assert_eq!(ret.into_result(), Ok(()));

value
Expand Down
42 changes: 21 additions & 21 deletions wgpu-hal/src/dx12/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ impl Drop for super::Adapter {

impl super::Adapter {
pub unsafe fn report_live_objects(&self) {
if let Ok(debug_device) = self
.raw
.cast::<d3d12sdklayers::ID3D12DebugDevice>()
.into_result()
{
debug_device.ReportLiveDeviceObjects(
d3d12sdklayers::D3D12_RLDO_SUMMARY | d3d12sdklayers::D3D12_RLDO_IGNORE_INTERNAL,
);
debug_device.destroy();
if let Ok(debug_device) = unsafe {
self.raw
.cast::<d3d12sdklayers::ID3D12DebugDevice>()
.into_result()
} {
unsafe {
debug_device.ReportLiveDeviceObjects(
d3d12sdklayers::D3D12_RLDO_SUMMARY | d3d12sdklayers::D3D12_RLDO_IGNORE_INTERNAL,
)
};
unsafe { debug_device.destroy() };
}
}

Expand Down Expand Up @@ -365,35 +367,33 @@ impl crate::Adapter<super::Api> for super::Adapter {

let mut data = d3d12::D3D12_FEATURE_DATA_FORMAT_SUPPORT {
Format: raw_format,
Support1: mem::zeroed(),
Support2: mem::zeroed(),
Support1: unsafe { mem::zeroed() },
Support2: unsafe { mem::zeroed() },
};
assert_eq!(
winerror::S_OK,
assert_eq!(winerror::S_OK, unsafe {
self.device.CheckFeatureSupport(
d3d12::D3D12_FEATURE_FORMAT_SUPPORT,
&mut data as *mut _ as *mut _,
mem::size_of::<d3d12::D3D12_FEATURE_DATA_FORMAT_SUPPORT>() as _,
)
);
});

// Because we use a different format for SRV and UAV views of depth textures, we need to check
// the features that use SRV/UAVs using the no-depth format.
let mut data_no_depth = d3d12::D3D12_FEATURE_DATA_FORMAT_SUPPORT {
Format: no_depth_format,
Support1: mem::zeroed(),
Support2: mem::zeroed(),
Support1: unsafe { mem::zeroed() },
Support2: unsafe { mem::zeroed() },
};
if raw_format != no_depth_format {
// Only-recheck if we're using a different format
assert_eq!(
winerror::S_OK,
assert_eq!(winerror::S_OK, unsafe {
self.device.CheckFeatureSupport(
d3d12::D3D12_FEATURE_FORMAT_SUPPORT,
&mut data_no_depth as *mut _ as *mut _,
mem::size_of::<d3d12::D3D12_FEATURE_DATA_FORMAT_SUPPORT>() as _,
)
);
});
} else {
// Same format, just copy over.
data_no_depth = data;
Expand Down Expand Up @@ -462,8 +462,8 @@ impl crate::Adapter<super::Api> for super::Adapter {
let current_extent = {
match surface.target {
SurfaceTarget::WndHandle(wnd_handle) => {
let mut rect: windef::RECT = mem::zeroed();
if winuser::GetClientRect(wnd_handle, &mut rect) != 0 {
let mut rect: windef::RECT = unsafe { mem::zeroed() };
if unsafe { winuser::GetClientRect(wnd_handle, &mut rect) } != 0 {
Some(wgt::Extent3d {
width: (rect.right - rect.left) as u32,
height: (rect.bottom - rect.top) as u32,
Expand Down
Loading

0 comments on commit 1f984c3

Please sign in to comment.