Skip to content

Commit 67522db

Browse files
author
Sherry Fan
committed
start removing unsafes
1 parent 12e7680 commit 67522db

File tree

5 files changed

+200
-240
lines changed

5 files changed

+200
-240
lines changed

services_benchmark_test/src/bench.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use patina::uefi_protocol::ProtocolInterface;
12
use r_efi::efi;
23

34
pub(crate) mod controller;
@@ -13,3 +14,15 @@ const TEST_GUID1: efi::Guid =
1314
efi::Guid::from_fields(0x12345678, 0x1234, 0x5678, 0x9a, 0xbc, &[0xde, 0xf0, 0x12, 0x34, 0x56, 0x78]);
1415
const TEST_GUID2: efi::Guid =
1516
efi::Guid::from_fields(0x87654321, 0x4321, 0x8765, 0xba, 0x98, &[0x76, 0x54, 0x32, 0x10, 0xfe, 0xdc]);
17+
18+
pub struct TestProtocol1 {}
19+
20+
unsafe impl ProtocolInterface for TestProtocol1 {
21+
const PROTOCOL_GUID: efi::Guid = TEST_GUID1;
22+
}
23+
24+
pub struct TestProtocol2 {}
25+
26+
unsafe impl ProtocolInterface for TestProtocol2 {
27+
const PROTOCOL_GUID: efi::Guid = TEST_GUID2;
28+
}

services_benchmark_test/src/bench/controller.rs

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rolling_stats::Stats;
66

77
use crate::{
88
BOOT_SERVICES,
9-
bench::{TEST_GUID1, TEST_GUID2},
9+
bench::{TEST_GUID1, TEST_GUID2, TestProtocol1, TestProtocol2},
1010
error::BenchError,
1111
};
1212

@@ -39,24 +39,24 @@ pub(crate) fn bench_connect_controller(_handle: efi::Handle, num_calls: usize) -
3939
}
4040

4141
// Setup controller, driver, and image handles with test protocols.
42-
let controller_handle = unsafe {
43-
BOOT_SERVICES
44-
.install_protocol_interface_unchecked(None, &TEST_GUID1, 0x1111 as *mut core::ffi::c_void)
45-
.map_err(|e| BenchError::BenchSetup("Failed to install protocol interface for controller", e))
46-
}?;
47-
let driver_handle = unsafe {
48-
BOOT_SERVICES
49-
.install_protocol_interface_unchecked(
50-
None,
51-
&efi::protocols::device_path::PROTOCOL_GUID,
52-
0x2222 as *mut core::ffi::c_void,
53-
)
54-
.map_err(|e| BenchError::BenchSetup("Failed to install protocol interface for driver", e))
55-
}?;
42+
let controller_handle = BOOT_SERVICES
43+
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
44+
.map_err(|e| BenchError::BenchSetup("Failed to install protocol interface for controller", e))?
45+
.0;
46+
47+
let driver_handle = BOOT_SERVICES
48+
.install_protocol_interface(
49+
None,
50+
Box::new(efi::protocols::device_path::Protocol { r#type: 4, sub_type: 5, length: [0, 0] }),
51+
)
52+
.map_err(|e| BenchError::BenchSetup("Failed to install protocol interface for driver", e))?
53+
.0;
54+
55+
let image_handle = BOOT_SERVICES
56+
.install_protocol_interface(None, Box::new(TestProtocol2 {}))
57+
.map_err(|e| BenchError::BenchSetup("Failed to install protocol interface for image", e))?
58+
.0;
5659

57-
let image_handle =
58-
unsafe { BOOT_SERVICES.install_protocol_interface_unchecked(None, &TEST_GUID2, core::ptr::null_mut()) }
59-
.map_err(|e| BenchError::BenchSetup("Failed to install protocol interface for image", e))?;
6060
let binding = Box::new(efi::protocols::driver_binding::Protocol {
6161
version: 10,
6262
supported: mock_supported,
@@ -65,21 +65,16 @@ pub(crate) fn bench_connect_controller(_handle: efi::Handle, num_calls: usize) -
6565
driver_binding_handle: driver_handle,
6666
image_handle,
6767
});
68-
let binding_ptr = Box::into_raw(binding) as *mut core::ffi::c_void;
6968

70-
unsafe {
71-
BOOT_SERVICES
72-
.install_protocol_interface_unchecked(
73-
Some(driver_handle),
74-
&efi::protocols::driver_binding::PROTOCOL_GUID,
75-
binding_ptr,
76-
)
77-
.map_err(|e| BenchError::BenchSetup("Failed to install protocol interface for driver binding", e))?;
78-
}
69+
let driver_binding_key = BOOT_SERVICES
70+
.install_protocol_interface(Some(driver_handle), binding)
71+
.map_err(|e| BenchError::BenchSetup("Failed to install protocol interface for driver binding", e))?
72+
.1;
7973

8074
let mut stats: Stats<f64> = Stats::new();
8175
for _ in 0..num_calls {
8276
let start = Arch::cpu_count();
77+
// SAFETY: All handles and pointers are valid (constructed by benchmark).
8378
unsafe {
8479
BOOT_SERVICES
8580
.connect_controller(controller_handle, vec![driver_handle], core::ptr::null_mut(), false)
@@ -93,15 +88,9 @@ pub(crate) fn bench_connect_controller(_handle: efi::Handle, num_calls: usize) -
9388
}
9489

9590
// Uninstall protocols to prevent side effects.
96-
unsafe {
97-
BOOT_SERVICES
98-
.uninstall_protocol_interface_unchecked(
99-
driver_handle,
100-
&efi::protocols::device_path::PROTOCOL_GUID,
101-
0x2222 as *mut core::ffi::c_void,
102-
)
103-
.map_err(|e| BenchError::BenchCleanup("Failed to uninstall protocol interface", e))?
104-
};
91+
BOOT_SERVICES
92+
.uninstall_protocol_interface(driver_handle, driver_binding_key)
93+
.map_err(|e| BenchError::BenchCleanup("Failed to uninstall protocol interface", e))?;
10594

10695
Ok(stats)
10796
}

services_benchmark_test/src/bench/event.rs

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@ pub(crate) fn bench_check_event_signaled(_handle: efi::Handle, num_calls: usize)
1313
extern "efiapi" fn test_notify(_event: efi::Event, _context: *mut c_void) {}
1414
let mut stats: Stats<f64> = Stats::new();
1515
for _ in 0..num_calls {
16-
let event_handle = unsafe {
17-
BOOT_SERVICES.create_event_unchecked(
18-
EventType::NOTIFY_WAIT,
19-
Tpl::NOTIFY,
20-
Some(test_notify),
21-
ptr::null_mut(),
22-
)
23-
}
24-
.map_err(|e| BenchError::BenchSetup("Failed to create event", e))?;
16+
let event_handle = BOOT_SERVICES
17+
.create_event(EventType::NOTIFY_WAIT, Tpl::NOTIFY, Some(test_notify), ptr::null_mut())
18+
.map_err(|e| BenchError::BenchSetup("Failed to create event", e))?;
2519
// Signal the event to set it to the signaled state.
2620
BOOT_SERVICES.signal_event(event_handle).map_err(|e| BenchError::BenchSetup("Failed to signal event", e))?;
2721

@@ -40,15 +34,9 @@ pub(crate) fn bench_check_event_unsignaled(_handle: efi::Handle, num_calls: usiz
4034
extern "efiapi" fn test_notify(_event: efi::Event, _context: *mut c_void) {}
4135
let mut stats: Stats<f64> = Stats::new();
4236
for _ in 0..num_calls {
43-
let event_handle = unsafe {
44-
BOOT_SERVICES.create_event_unchecked(
45-
EventType::NOTIFY_WAIT,
46-
Tpl::NOTIFY,
47-
Some(test_notify),
48-
ptr::null_mut(),
49-
)
50-
}
51-
.map_err(|e| BenchError::BenchSetup("Failed to create event", e))?;
37+
let event_handle = BOOT_SERVICES
38+
.create_event(EventType::NOTIFY_WAIT, Tpl::NOTIFY, Some(test_notify), ptr::null_mut())
39+
.map_err(|e| BenchError::BenchSetup("Failed to create event", e))?;
5240

5341
let start = Arch::cpu_count();
5442
if let Err(e) = BOOT_SERVICES.check_event(event_handle) {
@@ -71,15 +59,9 @@ pub(crate) fn bench_create_event(_handle: efi::Handle, num_calls: usize) -> Resu
7159
let mut stats: Stats<f64> = Stats::new();
7260
for _ in 0..num_calls {
7361
let start = Arch::cpu_count();
74-
let event_handle = unsafe {
75-
BOOT_SERVICES.create_event_unchecked(
76-
EventType::NOTIFY_WAIT,
77-
Tpl::NOTIFY,
78-
Some(test_notify),
79-
ptr::null_mut(),
80-
)
81-
}
82-
.map_err(|e| BenchError::BenchTest("Failed to create event", e))?;
62+
let event_handle = BOOT_SERVICES
63+
.create_event(EventType::NOTIFY_WAIT, Tpl::NOTIFY, Some(test_notify), ptr::null_mut())
64+
.map_err(|e| BenchError::BenchTest("Failed to create event", e))?;
8365
let end = Arch::cpu_count();
8466
stats.update((end - start) as f64);
8567

@@ -93,15 +75,9 @@ pub(crate) fn bench_close_event(_handle: efi::Handle, num_calls: usize) -> Resul
9375
extern "efiapi" fn test_notify(_event: efi::Event, _context: *mut c_void) {}
9476
let mut stats: Stats<f64> = Stats::new();
9577
for _ in 0..num_calls {
96-
let event_handle = unsafe {
97-
BOOT_SERVICES.create_event_unchecked(
98-
EventType::NOTIFY_WAIT,
99-
Tpl::NOTIFY,
100-
Some(test_notify),
101-
ptr::null_mut(),
102-
)
103-
}
104-
.map_err(|e| BenchError::BenchSetup("Failed to create event", e))?;
78+
let event_handle = BOOT_SERVICES
79+
.create_event(EventType::NOTIFY_WAIT, Tpl::NOTIFY, Some(test_notify), ptr::null_mut())
80+
.map_err(|e| BenchError::BenchSetup("Failed to create event", e))?;
10581
let start = Arch::cpu_count();
10682
BOOT_SERVICES.close_event(event_handle).map_err(|e| BenchError::BenchTest("Failed to close event", e))?;
10783
let end = Arch::cpu_count();
@@ -115,15 +91,9 @@ pub(crate) fn bench_signal_event(_handle: efi::Handle, num_calls: usize) -> Resu
11591
extern "efiapi" fn test_notify(_event: efi::Event, _context: *mut c_void) {}
11692
let mut stats: Stats<f64> = Stats::new();
11793
for _ in 0..num_calls {
118-
let event_handle = unsafe {
119-
BOOT_SERVICES.create_event_unchecked(
120-
EventType::NOTIFY_WAIT,
121-
Tpl::NOTIFY,
122-
Some(test_notify),
123-
ptr::null_mut(),
124-
)
125-
}
126-
.map_err(|e| BenchError::BenchSetup("Failed to create event", e))?;
94+
let event_handle = BOOT_SERVICES
95+
.create_event(EventType::NOTIFY_WAIT, Tpl::NOTIFY, Some(test_notify), ptr::null_mut())
96+
.map_err(|e| BenchError::BenchSetup("Failed to create event", e))?;
12797

12898
let start = Arch::cpu_count();
12999
BOOT_SERVICES.signal_event(event_handle).map_err(|e| BenchError::BenchTest("Failed to signal event", e))?;
@@ -149,16 +119,15 @@ pub(crate) fn bench_signal_event_group(_handle: efi::Handle, num_calls: usize) -
149119
// The event group will increase in size with each iteration to test the impact of group size on signaling time.
150120
let mut event_grp = Vec::with_capacity(num_calls);
151121
for _ in 0..num_calls {
152-
let event_handle = unsafe {
153-
BOOT_SERVICES.create_event_ex_unchecked(
122+
let event_handle = BOOT_SERVICES
123+
.create_event_ex(
154124
EventType::NOTIFY_WAIT,
155125
Tpl::NOTIFY,
156-
test_notify,
126+
Some(test_notify),
157127
ptr::null_mut(),
158128
&BENCH_EVENT_GROUP,
159129
)
160-
}
161-
.map_err(|e| BenchError::BenchSetup("Failed to create event", e))?;
130+
.map_err(|e| BenchError::BenchSetup("Failed to create event", e))?;
162131
event_grp.push(event_handle);
163132

164133
let start = Arch::cpu_count();

services_benchmark_test/src/bench/protocol.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ use patina::boot_services::{BootServices, event::EventType, tpl::Tpl};
55
use r_efi::efi;
66
use rolling_stats::Stats;
77

8-
use crate::{BOOT_SERVICES, bench::TEST_GUID1, error::BenchError};
8+
use crate::{
9+
BOOT_SERVICES,
10+
bench::{TEST_GUID1, TestProtocol1},
11+
error::BenchError,
12+
};
913

1014
/// Benchmarks protocol installation performance.
1115
pub(crate) fn bench_install_protocol_interface(
@@ -16,18 +20,14 @@ pub(crate) fn bench_install_protocol_interface(
1620
let mut stats: Stats<f64> = Stats::new();
1721
for _ in 0..num_calls {
1822
let start = Arch::cpu_count();
19-
let protocol_handle = unsafe {
20-
BOOT_SERVICES
21-
.install_protocol_interface_unchecked(None, &TEST_GUID1, protocol_interface)
22-
.map_err(|e| BenchError::BenchTest("Failed to install protocol", e))?
23-
};
23+
let protocol_install = BOOT_SERVICES
24+
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
25+
.map_err(|e| BenchError::BenchTest("Failed to install protocol", e))?;
2426
let end = Arch::cpu_count();
2527
stats.update((end - start) as f64);
26-
unsafe {
27-
BOOT_SERVICES
28-
.uninstall_protocol_interface_unchecked(protocol_handle, &TEST_GUID1, protocol_interface)
29-
.map_err(|e| BenchError::BenchCleanup("Failed to uninstall protocol", e))?;
30-
};
28+
BOOT_SERVICES
29+
.uninstall_protocol_interface(protocol_install.0, protocol_install.1)
30+
.map_err(|e| BenchError::BenchCleanup("Failed to uninstall protocol", e))?;
3131
}
3232
Ok(stats)
3333
}
@@ -36,26 +36,21 @@ 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 interface1: *mut c_void = 0x1234 as *mut c_void;
40-
let agent_handle = unsafe { BOOT_SERVICES.install_protocol_interface_unchecked(None, &TEST_GUID1, interface1) }
39+
let agent_handle = BOOT_SERVICES
40+
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
4141
.map_err(|e| BenchError::BenchSetup("Failed to install agent protocol", e))?;
42-
let controller_handle =
43-
unsafe { BOOT_SERVICES.install_protocol_interface_unchecked(None, &TEST_GUID1, interface1) }
44-
.map_err(|e| BenchError::BenchSetup("Failed to install controller protocol", e))?;
45-
let protocol_handle = unsafe { BOOT_SERVICES.install_protocol_interface_unchecked(None, &TEST_GUID1, interface1) }
42+
let controller_handle = BOOT_SERVICES
43+
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
44+
.map_err(|e| BenchError::BenchSetup("Failed to install controller protocol", e))?;
45+
let protocol_handle = BOOT_SERVICES
46+
.install_protocol_interface(None, Box::new(TestProtocol1 {}))
4647
.map_err(|e| BenchError::BenchSetup("Failed to install protocol", e))?;
4748
let mut stats: Stats<f64> = Stats::new();
4849
for _ in 0..num_calls {
4950
let start = Arch::cpu_count();
5051
unsafe {
5152
BOOT_SERVICES
52-
.open_protocol_unchecked(
53-
protocol_handle,
54-
&TEST_GUID1,
55-
agent_handle,
56-
controller_handle,
57-
efi::OPEN_PROTOCOL_BY_DRIVER,
58-
)
53+
.open_protocol(protocol_handle, agent_handle, controller_handle, efi::OPEN_PROTOCOL_BY_DRIVER)
5954
.map_err(|e| BenchError::BenchTest("Failed to open protocol", e))?;
6055
}
6156
let end = Arch::cpu_count();

0 commit comments

Comments
 (0)