Skip to content

Commit 110b97f

Browse files
author
Sherry Fan
committed
remove all unsafes
1 parent 67522db commit 110b97f

File tree

1 file changed

+65
-74
lines changed

1 file changed

+65
-74
lines changed

services_benchmark_test/src/bench/protocol.rs

Lines changed: 65 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -36,74 +36,78 @@ pub(crate) fn bench_install_protocol_interface(
3636
/// This is the preferred method (over `handle_protcol`) for retrieving protocol interfaces in modern UEFI (2.0+).
3737
pub(crate) fn bench_open_protocol(_handle: efi::Handle, num_calls: usize) -> Result<Stats<f64>, BenchError> {
3838
// Set up and install the protocol to be opened.
39-
let agent_handle = BOOT_SERVICES
39+
let agent_install = BOOT_SERVICES
4040
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
4141
.map_err(|e| BenchError::BenchSetup("Failed to install agent protocol", e))?;
42-
let controller_handle = BOOT_SERVICES
42+
let controller_install = BOOT_SERVICES
4343
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
4444
.map_err(|e| BenchError::BenchSetup("Failed to install controller protocol", e))?;
45-
let protocol_handle = BOOT_SERVICES
45+
let protocol_install = BOOT_SERVICES
4646
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
4747
.map_err(|e| BenchError::BenchSetup("Failed to install protocol", e))?;
4848
let mut stats: Stats<f64> = Stats::new();
4949
for _ in 0..num_calls {
5050
let start = Arch::cpu_count();
51-
unsafe {
51+
(unsafe {
5252
BOOT_SERVICES
53-
.open_protocol(protocol_handle, agent_handle, controller_handle, efi::OPEN_PROTOCOL_BY_DRIVER)
54-
.map_err(|e| BenchError::BenchTest("Failed to open protocol", e))?;
55-
}
53+
.open_protocol::<TestProtocol1>(
54+
protocol_install.0,
55+
agent_install.0,
56+
controller_install.0,
57+
efi::OPEN_PROTOCOL_BY_DRIVER,
58+
)
59+
.map_err(|e| BenchError::BenchTest("Failed to open protocol", e))
60+
})?;
5661
let end = Arch::cpu_count();
5762
stats.update((end - start) as f64);
5863

5964
BOOT_SERVICES
60-
.close_protocol(protocol_handle, &TEST_GUID1, agent_handle, controller_handle)
65+
.close_protocol(protocol_install.0, &TEST_GUID1, agent_install.0, controller_install.0)
6166
.map_err(|e| BenchError::BenchCleanup("Failed to close protocol", e))?;
6267
}
6368

6469
// Uninstall mock protocols after benchmarking.
65-
unsafe {
66-
BOOT_SERVICES
67-
.uninstall_protocol_interface_unchecked(protocol_handle, &TEST_GUID1, interface1)
68-
.map_err(|e| BenchError::BenchCleanup("Failed to uninstall protocol", e))?;
69-
BOOT_SERVICES
70-
.uninstall_protocol_interface_unchecked(agent_handle, &TEST_GUID1, interface1)
71-
.map_err(|e| BenchError::BenchCleanup("Failed to uninstall agent protocol", e))?;
72-
BOOT_SERVICES
73-
.uninstall_protocol_interface_unchecked(controller_handle, &TEST_GUID1, interface1)
74-
.map_err(|e| BenchError::BenchCleanup("Failed to uninstall controller protocol", e))?;
75-
}
70+
BOOT_SERVICES
71+
.uninstall_protocol_interface(protocol_install.0, protocol_install.1)
72+
.map_err(|e| BenchError::BenchCleanup("Failed to uninstall protocol", e))?;
73+
BOOT_SERVICES
74+
.uninstall_protocol_interface(agent_install.0, agent_install.1)
75+
.map_err(|e| BenchError::BenchCleanup("Failed to uninstall agent protocol", e))?;
76+
BOOT_SERVICES
77+
.uninstall_protocol_interface(controller_install.0, controller_install.1)
78+
.map_err(|e| BenchError::BenchCleanup("Failed to uninstall controller protocol", e))?;
79+
7680
Ok(stats)
7781
}
7882

7983
/// Benchmarks protocol closing performance.
8084
pub(crate) fn bench_close_protocol(_handle: efi::Handle, num_calls: usize) -> Result<Stats<f64>, BenchError> {
8185
// Set up and install the necessary protocol.
82-
let interface1: *mut c_void = 0x1234 as *mut c_void;
83-
let agent_handle = unsafe { BOOT_SERVICES.install_protocol_interface_unchecked(None, &TEST_GUID1, interface1) }
86+
let agent_install = BOOT_SERVICES
87+
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
8488
.map_err(|e| BenchError::BenchSetup("Failed install agent handle", e))?;
85-
let controller_handle =
86-
unsafe { BOOT_SERVICES.install_protocol_interface_unchecked(None, &TEST_GUID1, interface1) }
87-
.map_err(|e| BenchError::BenchSetup("Failed to install controller handle.", e))?;
88-
let protocol_handle = unsafe { BOOT_SERVICES.install_protocol_interface_unchecked(None, &TEST_GUID1, interface1) }
89+
let controller_install = BOOT_SERVICES
90+
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
91+
.map_err(|e| BenchError::BenchSetup("Failed to install controller handle.", e))?;
92+
let protocol_install = BOOT_SERVICES
93+
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
8994
.map_err(|e| BenchError::BenchSetup("Failed to install protocol handle", e))?;
9095
let mut stats: Stats<f64> = Stats::new();
9196
for _ in 0..num_calls {
9297
unsafe {
9398
BOOT_SERVICES
94-
.open_protocol_unchecked(
95-
protocol_handle,
96-
&TEST_GUID1,
97-
agent_handle,
98-
controller_handle,
99+
.open_protocol::<TestProtocol1>(
100+
protocol_install.0,
101+
agent_install.0,
102+
controller_install.0,
99103
efi::OPEN_PROTOCOL_BY_DRIVER,
100104
)
101105
.map_err(|e| BenchError::BenchSetup("Failed to open protocol", e))?;
102106
}
103107

104108
let start = Arch::cpu_count();
105109
BOOT_SERVICES
106-
.close_protocol(protocol_handle, &TEST_GUID1, agent_handle, controller_handle)
110+
.close_protocol(protocol_install.0, &TEST_GUID1, agent_install.0, controller_install.0)
107111
.map_err(|e| BenchError::BenchTest("Failed to close protocol", e))?;
108112
let end = Arch::cpu_count();
109113
stats.update((end - start) as f64);
@@ -115,17 +119,18 @@ pub(crate) fn bench_close_protocol(_handle: efi::Handle, num_calls: usize) -> Re
115119
/// This is a legacy method but is still included due to needing to support legacy UEFI (1.0).
116120
pub(crate) fn bench_handle_protocol(_handle: efi::Handle, num_calls: usize) -> Result<Stats<f64>, BenchError> {
117121
// Set up and install the protocol to be accessed.
118-
let interface1: *mut c_void = 0x1234 as *mut c_void;
119-
let protocol_handle = unsafe { BOOT_SERVICES.install_protocol_interface_unchecked(None, &TEST_GUID1, interface1) }
120-
.map_err(|e| BenchError::BenchSetup("Failed to install protocol", e))?;
122+
let protocol_handle = BOOT_SERVICES
123+
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
124+
.map_err(|e| BenchError::BenchSetup("Failed to install protocol", e))?
125+
.0;
121126
let mut stats: Stats<f64> = Stats::new();
122127
for _ in 0..num_calls {
123128
let start = Arch::cpu_count();
124-
unsafe {
129+
(unsafe {
125130
BOOT_SERVICES
126-
.handle_protocol_unchecked(protocol_handle, &TEST_GUID1)
127-
.map_err(|e| BenchError::BenchTest("Failed to handle protocol", e))?;
128-
}
131+
.handle_protocol::<TestProtocol1>(protocol_handle)
132+
.map_err(|e| BenchError::BenchTest("Failed to handle protocol", e))
133+
})?;
129134

130135
let end = Arch::cpu_count();
131136
stats.update((end - start) as f64);
@@ -200,16 +205,9 @@ pub(crate) fn bench_register_protocol_notify(_handle: efi::Handle, num_calls: us
200205

201206
let mut stats: Stats<f64> = Stats::new();
202207
for _ in 0..num_calls {
203-
let event = unsafe {
204-
BOOT_SERVICES
205-
.create_event_unchecked::<i32>(
206-
EventType::NOTIFY_SIGNAL,
207-
Tpl::NOTIFY,
208-
Some(mock_notify),
209-
&mut 0 as *mut i32,
210-
)
211-
.map_err(|e| BenchError::BenchSetup("Failed to create valid event", e))
212-
}?;
208+
let event = BOOT_SERVICES
209+
.create_event(EventType::NOTIFY_SIGNAL, Tpl::NOTIFY, Some(mock_notify), &mut 0 as *mut i32)
210+
.map_err(|e| BenchError::BenchSetup("Failed to create valid event", e))?;
213211
let start = Arch::cpu_count();
214212
BOOT_SERVICES
215213
.register_protocol_notify(&efi::protocols::loaded_image::PROTOCOL_GUID, event)
@@ -228,21 +226,19 @@ pub(crate) fn bench_reinstall_protocol_interface(
228226
_handle: efi::Handle,
229227
num_calls: usize,
230228
) -> Result<Stats<f64>, BenchError> {
231-
let mut prev_interface: *mut c_void = 0x1234 as *mut c_void;
232-
let mut new_interface = 0x5678 as *mut c_void;
233-
let protocol_handle =
234-
unsafe { BOOT_SERVICES.install_protocol_interface_unchecked(None, &TEST_GUID1, prev_interface) }
235-
.map_err(|e| BenchError::BenchSetup("Failed to install dummy protocol", e))?;
236229
let mut stats: Stats<f64> = Stats::new();
237230
for _ in 0..num_calls {
231+
let prev_interface = Box::new(TestProtocol1 {});
232+
let new_interface = Box::new(TestProtocol1 {});
233+
let protocol_install = BOOT_SERVICES
234+
.install_protocol_interface(None, prev_interface)
235+
.map_err(|e| BenchError::BenchSetup("Failed to install dummy protocol", e))?;
236+
238237
let start = Arch::cpu_count();
239-
unsafe {
240-
BOOT_SERVICES
241-
.reinstall_protocol_interface_unchecked(protocol_handle, &TEST_GUID1, prev_interface, new_interface)
242-
.map_err(|e| BenchError::BenchTest("Failed to reinstall protocol interface", e))?;
243-
}
244-
prev_interface = new_interface;
245-
new_interface = 0x5678 as *mut c_void;
238+
BOOT_SERVICES
239+
.reinstall_protocol_interface(protocol_install.0, protocol_install.1, new_interface)
240+
.map_err(|e| BenchError::BenchTest("Failed to reinstall protocol interface", e))?;
241+
246242
let end = Arch::cpu_count();
247243
stats.update((end - start) as f64);
248244
}
@@ -254,27 +250,22 @@ pub(crate) fn bench_uninstall_protocol_interface(
254250
_handle: efi::Handle,
255251
num_calls: usize,
256252
) -> Result<Stats<f64>, BenchError> {
257-
let interface1: *mut c_void = 0x1234 as *mut c_void;
258-
let mut protocol_handle =
259-
unsafe { BOOT_SERVICES.install_protocol_interface_unchecked(None, &TEST_GUID1, interface1) }
260-
.map_err(|e| BenchError::BenchSetup("Failed to install dummy protocol", e))?;
253+
let mut protocol_install = BOOT_SERVICES
254+
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
255+
.map_err(|e| BenchError::BenchSetup("Failed to install dummy protocol", e))?;
261256
let mut stats: Stats<f64> = Stats::new();
262257
for _ in 0..num_calls {
263258
let start = Arch::cpu_count();
264-
unsafe {
265-
BOOT_SERVICES
266-
.uninstall_protocol_interface_unchecked(protocol_handle, &TEST_GUID1, interface1)
267-
.map_err(|e| BenchError::BenchTest("Failed to uninstall protocol interface", e))?;
268-
}
259+
BOOT_SERVICES
260+
.uninstall_protocol_interface(protocol_install.0, protocol_install.1)
261+
.map_err(|e| BenchError::BenchTest("Failed to uninstall protocol interface", e))?;
269262
let end = Arch::cpu_count();
270263
stats.update((end - start) as f64);
271264

272265
// Reinstall for next iteration.
273-
unsafe {
274-
protocol_handle = BOOT_SERVICES
275-
.install_protocol_interface_unchecked(None, &TEST_GUID1, interface1)
276-
.map_err(|e| BenchError::BenchCleanup("Failed to install a new dummy protocol", e))?;
277-
}
266+
protocol_install = BOOT_SERVICES
267+
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
268+
.map_err(|e| BenchError::BenchCleanup("Failed to install a new dummy protocol", e))?;
278269
}
279270
Ok(stats)
280271
}

0 commit comments

Comments
 (0)