Skip to content

Commit d7bb00a

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 49ddd3c commit d7bb00a

25 files changed

+2485
-1981
lines changed

wgpu-hal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ rust-version = "1.60"
1313
[lib]
1414

1515
[features]
16-
default = []
16+
default = ["gles"]
1717
metal = ["naga/msl-out", "block", "foreign-types"]
1818
vulkan = ["naga/spv-out", "ash", "gpu-alloc", "gpu-descriptor", "libloading", "smallvec"]
1919
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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,14 @@ 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-
);
230+
let mut value = unsafe { mem::zeroed::<T>() };
231+
let ret = unsafe {
232+
self.CheckFeatureSupport(
233+
feature,
234+
&mut value as *mut T as *mut c_void,
235+
mem::size_of::<T>() as u32,
236+
)
237+
};
236238
assert_eq!(ret.into_result(), Ok(()));
237239

238240
value

wgpu-hal/src/dx12/adapter.rs

Lines changed: 21 additions & 21 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

@@ -366,35 +368,33 @@ impl crate::Adapter<super::Api> for super::Adapter {
366368

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

381382
// Because we use a different format for SRV and UAV views of depth textures, we need to check
382383
// the features that use SRV/UAVs using the no-depth format.
383384
let mut data_no_depth = d3d12::D3D12_FEATURE_DATA_FORMAT_SUPPORT {
384385
Format: no_depth_format,
385-
Support1: mem::zeroed(),
386-
Support2: mem::zeroed(),
386+
Support1: unsafe { mem::zeroed() },
387+
Support2: unsafe { mem::zeroed() },
387388
};
388389
if raw_format != no_depth_format {
389390
// Only-recheck if we're using a different format
390-
assert_eq!(
391-
winerror::S_OK,
391+
assert_eq!(winerror::S_OK, unsafe {
392392
self.device.CheckFeatureSupport(
393393
d3d12::D3D12_FEATURE_FORMAT_SUPPORT,
394394
&mut data_no_depth as *mut _ as *mut _,
395395
mem::size_of::<d3d12::D3D12_FEATURE_DATA_FORMAT_SUPPORT>() as _,
396396
)
397-
);
397+
});
398398
} else {
399399
// Same format, just copy over.
400400
data_no_depth = data;
@@ -463,8 +463,8 @@ impl crate::Adapter<super::Api> for super::Adapter {
463463
let current_extent = {
464464
match surface.target {
465465
SurfaceTarget::WndHandle(wnd_handle) => {
466-
let mut rect: windef::RECT = mem::zeroed();
467-
if winuser::GetClientRect(wnd_handle, &mut rect) != 0 {
466+
let mut rect: windef::RECT = unsafe { mem::zeroed() };
467+
if unsafe { winuser::GetClientRect(wnd_handle, &mut rect) } != 0 {
468468
Some(wgt::Extent3d {
469469
width: (rect.right - rect.left) as u32,
470470
height: (rect.bottom - rect.top) as u32,

0 commit comments

Comments
 (0)