Skip to content

Commit 9b6c238

Browse files
committed
Auto merge of #43615 - dhduvall:lto-unaligned-read, r=nagisa
Fix some unaligned reads on SPARC in LTO This fixes #43593 by eliminating some undefined behavior.
2 parents d692a91 + e412cb3 commit 9b6c238

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/librustc_trans/back/lto.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use flate2::read::DeflateDecoder;
2727
use std::io::Read;
2828
use std::ffi::CString;
2929
use std::path::Path;
30+
use std::ptr::read_unaligned;
3031

3132
pub fn crate_type_allows_lto(crate_type: config::CrateType) -> bool {
3233
match crate_type {
@@ -223,13 +224,13 @@ fn is_versioned_bytecode_format(bc: &[u8]) -> bool {
223224
fn extract_bytecode_format_version(bc: &[u8]) -> u32 {
224225
let pos = link::RLIB_BYTECODE_OBJECT_VERSION_OFFSET;
225226
let byte_data = &bc[pos..pos + 4];
226-
let data = unsafe { *(byte_data.as_ptr() as *const u32) };
227+
let data = unsafe { read_unaligned(byte_data.as_ptr() as *const u32) };
227228
u32::from_le(data)
228229
}
229230

230231
fn extract_compressed_bytecode_size_v1(bc: &[u8]) -> u64 {
231232
let pos = link::RLIB_BYTECODE_OBJECT_V1_DATASIZE_OFFSET;
232233
let byte_data = &bc[pos..pos + 8];
233-
let data = unsafe { *(byte_data.as_ptr() as *const u64) };
234+
let data = unsafe { read_unaligned(byte_data.as_ptr() as *const u64) };
234235
u64::from_le(data)
235236
}

0 commit comments

Comments
 (0)