Skip to content

Commit 741965d

Browse files
AlexandruCihodaruandreeaflorescu
authored andcommitted
Remove access to unaligned refs from bzImage tests
Replace access to unaligned reference in bzImage tests with std::ptr::addr_of macro and `read_unaligned` method. Signed-off-by: Alexandru Cihodaru <[email protected]>
1 parent e1dccdd commit 741965d

File tree

1 file changed

+26
-5
lines changed
  • src/loader/x86_64/bzimage

1 file changed

+26
-5
lines changed

src/loader/x86_64/bzimage/mod.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ mod tests {
221221
v
222222
}
223223

224-
#[allow(unaligned_references)]
225224
#[allow(non_snake_case)]
226225
#[test]
227226
fn test_load_bzImage() {
@@ -238,10 +237,23 @@ mod tests {
238237
Some(highmem_start_address),
239238
)
240239
.unwrap();
240+
let setup_header = loader_result.setup_header.unwrap();
241241

242242
assert_eq!(loader_result.kernel_load.raw_value(), 0x200000);
243-
assert_eq!(loader_result.setup_header.unwrap().header, 0x53726448);
244-
assert_eq!(loader_result.setup_header.unwrap().version, 0x20d);
243+
assert_eq!(
244+
// SAFETY:
245+
// Reading the value from an unaligned address is not considered safe.
246+
// but this is not an issue since this is a test.
247+
unsafe { std::ptr::addr_of!(setup_header.header).read_unaligned() },
248+
0x53726448
249+
);
250+
assert_eq!(
251+
// SAFETY:
252+
// Reading the value from an unaligned address is not considered safe.
253+
// but this is not an issue since this is a test.
254+
unsafe { std::ptr::addr_of!(setup_header.version).read_unaligned() },
255+
0x20d
256+
);
245257
assert_eq!(loader_result.setup_header.unwrap().loadflags, 1);
246258
assert_eq!(loader_result.kernel_end, 0x60D320);
247259

@@ -253,11 +265,20 @@ mod tests {
253265
Some(highmem_start_address),
254266
)
255267
.unwrap();
268+
let setup_header = loader_result.setup_header.unwrap();
269+
256270
assert_eq!(loader_result.kernel_load.raw_value(), 0x100000);
257271

258-
// load bzImage withouth himem_start
272+
// load bzImage without himem_start
259273
loader_result = BzImage::load(&gm, None, &mut Cursor::new(&image), None).unwrap();
260-
assert_eq!(0x53726448, loader_result.setup_header.unwrap().header);
274+
// Reading the value from an unaligned address is not considered safe.
275+
assert_eq!(
276+
0x53726448,
277+
// SAFETY:
278+
// Reading the value from an unaligned address is not considered safe.
279+
// but this is not an issue since this is a test.
280+
unsafe { std::ptr::addr_of!(setup_header.header).read_unaligned() }
281+
);
261282
assert_eq!(loader_result.kernel_load.raw_value(), 0x100000);
262283

263284
// load bzImage with a bad himem setting

0 commit comments

Comments
 (0)