Skip to content

Commit 02cdc85

Browse files
chore: naively set warn(unsafe_op_in_unsafe_fn) in wgpu-hal
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.
1 parent d799498 commit 02cdc85

25 files changed

+2527
-2012
lines changed

wgpu-hal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ rustdoc-args = ["--cfg", "docsrs"]
2222
[lib]
2323

2424
[features]
25-
default = []
25+
default = ["gles"]
2626
metal = ["naga/msl-out", "block", "foreign-types"]
2727
vulkan = ["naga/spv-out", "ash", "gpu-alloc", "gpu-descriptor", "libloading", "smallvec"]
2828
gles = ["naga/glsl-out", "glow", "egl", "libloading"]

wgpu-hal/src/auxil/dxgi/exception.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,23 @@ unsafe extern "system" fn output_debug_string_handler(
4646
exception_info: *mut winnt::EXCEPTION_POINTERS,
4747
) -> i32 {
4848
// See https://stackoverflow.com/a/41480827
49-
let record = &*(*exception_info).ExceptionRecord;
49+
let record = unsafe { &*(*exception_info).ExceptionRecord };
5050
if record.NumberParameters != 2 {
5151
return excpt::EXCEPTION_CONTINUE_SEARCH;
5252
}
5353
let message = match record.ExceptionCode {
54-
winnt::DBG_PRINTEXCEPTION_C => String::from_utf8_lossy(slice::from_raw_parts(
55-
record.ExceptionInformation[1] as *const u8,
56-
record.ExceptionInformation[0],
57-
)),
58-
winnt::DBG_PRINTEXCEPTION_WIDE_C => {
59-
Cow::Owned(String::from_utf16_lossy(slice::from_raw_parts(
54+
winnt::DBG_PRINTEXCEPTION_C => String::from_utf8_lossy(unsafe {
55+
slice::from_raw_parts(
56+
record.ExceptionInformation[1] as *const u8,
57+
record.ExceptionInformation[0],
58+
)
59+
}),
60+
winnt::DBG_PRINTEXCEPTION_WIDE_C => Cow::Owned(String::from_utf16_lossy(unsafe {
61+
slice::from_raw_parts(
6062
record.ExceptionInformation[1] as *const u16,
6163
record.ExceptionInformation[0],
62-
)))
63-
}
64+
)
65+
})),
6466
_ => return excpt::EXCEPTION_CONTINUE_SEARCH,
6567
};
6668

wgpu-hal/src/auxil/renderdoc.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ impl RenderDoc {
4444
let renderdoc_filename = "libVkLayer_GLES_RenderDoc.so";
4545

4646
#[cfg(unix)]
47-
let renderdoc_result: Result<libloading::Library, libloading::Error> =
47+
let renderdoc_result: Result<libloading::Library, libloading::Error> = unsafe {
4848
libloading::os::unix::Library::open(
4949
Some(renderdoc_filename),
5050
libloading::os::unix::RTLD_NOW | RTLD_NOLOAD,
5151
)
52-
.map(|lib| lib.into());
52+
}
53+
.map(|lib| lib.into());
5354

5455
#[cfg(windows)]
5556
let renderdoc_result: Result<libloading::Library, libloading::Error> =
@@ -68,22 +69,23 @@ impl RenderDoc {
6869
}
6970
};
7071

71-
let get_api: libloading::Symbol<GetApiFn> = match renderdoc_lib.get(b"RENDERDOC_GetAPI\0") {
72-
Ok(api) => api,
73-
Err(e) => {
74-
return RenderDoc::NotAvailable {
75-
reason: format!(
76-
"Unable to get RENDERDOC_GetAPI from renderdoc library '{}': {:?}",
77-
renderdoc_filename, e
78-
),
72+
let get_api: libloading::Symbol<GetApiFn> =
73+
match unsafe { renderdoc_lib.get(b"RENDERDOC_GetAPI\0") } {
74+
Ok(api) => api,
75+
Err(e) => {
76+
return RenderDoc::NotAvailable {
77+
reason: format!(
78+
"Unable to get RENDERDOC_GetAPI from renderdoc library '{}': {:?}",
79+
renderdoc_filename, e
80+
),
81+
}
7982
}
80-
}
81-
};
83+
};
8284
let mut obj = ptr::null_mut();
83-
match get_api(10401, &mut obj) {
85+
match unsafe { get_api(10401, &mut obj) } {
8486
1 => RenderDoc::Available {
8587
api: RenderDocApi {
86-
api: *(obj as *mut renderdoc_sys::RENDERDOC_API_1_4_1),
88+
api: unsafe { *(obj as *mut renderdoc_sys::RENDERDOC_API_1_4_1) },
8789
lib: renderdoc_lib,
8890
},
8991
},
@@ -115,7 +117,7 @@ impl RenderDoc {
115117
pub unsafe fn start_frame_capture(&self, device_handle: Handle, window_handle: Handle) -> bool {
116118
match *self {
117119
Self::Available { api: ref entry } => {
118-
entry.api.StartFrameCapture.unwrap()(device_handle, window_handle);
120+
unsafe { entry.api.StartFrameCapture.unwrap()(device_handle, window_handle) };
119121
true
120122
}
121123
Self::NotAvailable { ref reason } => {
@@ -129,7 +131,7 @@ impl RenderDoc {
129131
pub unsafe fn end_frame_capture(&self, device_handle: Handle, window_handle: Handle) {
130132
match *self {
131133
Self::Available { api: ref entry } => {
132-
entry.api.EndFrameCapture.unwrap()(device_handle, window_handle);
134+
unsafe { entry.api.EndFrameCapture.unwrap()(device_handle, window_handle) };
133135
}
134136
Self::NotAvailable { ref reason } => {
135137
log::warn!("Could not end RenderDoc frame capture: {}", reason)

wgpu-hal/src/dx11/device.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,16 @@ impl crate::Queue<super::Api> for super::Queue {
227227
impl super::D3D11Device {
228228
#[allow(trivial_casts)] // come on
229229
pub unsafe fn check_feature_support<T>(&self, feature: d3d11::D3D11_FEATURE) -> T {
230-
let mut value = mem::zeroed::<T>();
231-
let ret = self.CheckFeatureSupport(
232-
feature,
233-
&mut value as *mut T as *mut c_void,
234-
mem::size_of::<T>() as u32,
235-
);
236-
assert_eq!(ret.into_result(), Ok(()));
237-
238-
value
230+
unsafe {
231+
let mut value = mem::zeroed::<T>();
232+
let ret = self.CheckFeatureSupport(
233+
feature,
234+
&mut value as *mut T as *mut c_void,
235+
mem::size_of::<T>() as u32,
236+
);
237+
assert_eq!(ret.into_result(), Ok(()));
238+
239+
value
240+
}
239241
}
240242
}

wgpu-hal/src/dx12/adapter.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ impl Drop for super::Adapter {
2929

3030
impl super::Adapter {
3131
pub unsafe fn report_live_objects(&self) {
32-
if let Ok(debug_device) = self
33-
.raw
34-
.cast::<d3d12sdklayers::ID3D12DebugDevice>()
35-
.into_result()
36-
{
37-
debug_device.ReportLiveDeviceObjects(
38-
d3d12sdklayers::D3D12_RLDO_SUMMARY | d3d12sdklayers::D3D12_RLDO_IGNORE_INTERNAL,
39-
);
40-
debug_device.destroy();
32+
if let Ok(debug_device) = unsafe {
33+
self.raw
34+
.cast::<d3d12sdklayers::ID3D12DebugDevice>()
35+
.into_result()
36+
} {
37+
unsafe {
38+
debug_device.ReportLiveDeviceObjects(
39+
d3d12sdklayers::D3D12_RLDO_SUMMARY | d3d12sdklayers::D3D12_RLDO_IGNORE_INTERNAL,
40+
)
41+
};
42+
unsafe { debug_device.destroy() };
4143
}
4244
}
4345

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

366368
let mut data = d3d12::D3D12_FEATURE_DATA_FORMAT_SUPPORT {
367369
Format: raw_format,
368-
Support1: mem::zeroed(),
369-
Support2: mem::zeroed(),
370+
Support1: unsafe { mem::zeroed() },
371+
Support2: unsafe { mem::zeroed() },
370372
};
371-
assert_eq!(
372-
winerror::S_OK,
373+
assert_eq!(winerror::S_OK, unsafe {
373374
self.device.CheckFeatureSupport(
374375
d3d12::D3D12_FEATURE_FORMAT_SUPPORT,
375376
&mut data as *mut _ as *mut _,
376377
mem::size_of::<d3d12::D3D12_FEATURE_DATA_FORMAT_SUPPORT>() as _,
377378
)
378-
);
379+
});
379380

380381
// Because we use a different format for SRV and UAV views of depth textures, we need to check
381382
// the features that use SRV/UAVs using the no-depth format.
382383
let mut data_no_depth = d3d12::D3D12_FEATURE_DATA_FORMAT_SUPPORT {
383384
Format: no_depth_format,
384-
Support1: mem::zeroed(),
385-
Support2: mem::zeroed(),
385+
Support1: unsafe { mem::zeroed() },
386+
Support2: unsafe { mem::zeroed() },
386387
};
387388
if raw_format != no_depth_format {
388389
// Only-recheck if we're using a different format
389-
assert_eq!(
390-
winerror::S_OK,
390+
assert_eq!(winerror::S_OK, unsafe {
391391
self.device.CheckFeatureSupport(
392392
d3d12::D3D12_FEATURE_FORMAT_SUPPORT,
393393
&mut data_no_depth as *mut _ as *mut _,
394394
mem::size_of::<d3d12::D3D12_FEATURE_DATA_FORMAT_SUPPORT>() as _,
395395
)
396-
);
396+
});
397397
} else {
398398
// Same format, just copy over.
399399
data_no_depth = data;
@@ -462,11 +462,13 @@ impl crate::Adapter<super::Api> for super::Adapter {
462462
let mut set_sample_count = |sc: u32, tfc: Tfc| {
463463
ms_levels.SampleCount = sc;
464464

465-
if self.device.CheckFeatureSupport(
466-
d3d12::D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS,
467-
<*mut _>::cast(&mut ms_levels),
468-
mem::size_of::<d3d12::D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS>() as _,
469-
) == winerror::S_OK
465+
if unsafe {
466+
self.device.CheckFeatureSupport(
467+
d3d12::D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS,
468+
<*mut _>::cast(&mut ms_levels),
469+
mem::size_of::<d3d12::D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS>() as _,
470+
)
471+
} == winerror::S_OK
470472
&& ms_levels.NumQualityLevels != 0
471473
{
472474
caps.set(tfc, !no_msaa_load && !no_msaa_target);
@@ -487,8 +489,8 @@ impl crate::Adapter<super::Api> for super::Adapter {
487489
let current_extent = {
488490
match surface.target {
489491
SurfaceTarget::WndHandle(wnd_handle) => {
490-
let mut rect: windef::RECT = mem::zeroed();
491-
if winuser::GetClientRect(wnd_handle, &mut rect) != 0 {
492+
let mut rect: windef::RECT = unsafe { mem::zeroed() };
493+
if unsafe { winuser::GetClientRect(wnd_handle, &mut rect) } != 0 {
492494
Some(wgt::Extent3d {
493495
width: (rect.right - rect.left) as u32,
494496
height: (rect.bottom - rect.top) as u32,

0 commit comments

Comments
 (0)