Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 56f7f65

Browse files
committed
accounts-db: test_hash_stored_account: Avoid UB.
`unsafe { transmute }` in the test, as written, is undefined behavior. And, I think, we actually see the compiler generating different code for it as we are changing the generated hashes when we upgrade the compiler. It happened during upgrade to 1.67.1: #29947 And the hashes changed again in 1.71.
1 parent d1b8499 commit 56f7f65

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

accounts-db/src/accounts_db.rs

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12561,40 +12561,36 @@ pub mod tests {
1256112561

1256212562
#[test]
1256312563
fn test_hash_stored_account() {
12564-
// This test uses some UNSAFE tricks to detect most of hashing code changes, resulting from
12565-
// account's field additions and deletions of StoredAccountMeta and AccountSharedData and
12566-
// hashing-order changes.
12567-
12564+
// Number are just sequential.
12565+
let slot: Slot = 0x0102030405060708;
12566+
let meta = StoredMeta {
12567+
write_version_obsolete: 0x090a0b0c0d0e0f10,
12568+
data_len: 0x1112131415161718,
12569+
pubkey: Pubkey::from([
12570+
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
12571+
0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34,
12572+
0x35, 0x36, 0x37, 0x38,
12573+
]),
12574+
};
12575+
let account_meta = AccountMeta {
12576+
lamports: 0x393a3b3c3d3e3f40,
12577+
rent_epoch: 0x4142434445464748,
12578+
owner: Pubkey::from([
12579+
0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
12580+
0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64,
12581+
0x65, 0x66, 0x67, 0x68,
12582+
]),
12583+
executable: false,
12584+
};
1256812585
const ACCOUNT_DATA_LEN: usize = 3;
12569-
// the type of InputFields elements must not contain references;
12570-
// they should be simple scalars or data blobs
12571-
// repr(C) is needed for abi-stability in the dirtiest variant of std::mem::transmute().
12572-
#[repr(C)]
12573-
struct InputFields(
12574-
Slot,
12575-
StoredMeta,
12576-
AccountMeta,
12577-
[u8; ACCOUNT_DATA_LEN],
12578-
usize, // for StoredAccountMeta::offset
12579-
Hash,
12580-
);
12581-
const INPUT_LEN: usize = std::mem::size_of::<InputFields>();
12582-
type InputBlob = [u8; INPUT_LEN];
12583-
let mut blob: InputBlob = [0u8; INPUT_LEN];
12584-
12585-
// spray memory with decreasing integers so that, data layout change and/or hashing
12586-
// reordering can be detected. note that just zeroed blob can't detect field reordering.
12587-
for (i, byte) in blob.iter_mut().enumerate() {
12588-
*byte = (INPUT_LEN - i) as u8;
12589-
}
12586+
let data: [u8; ACCOUNT_DATA_LEN] = [0x69, 0x6a, 0x6b];
12587+
let offset: usize = 0x6c6d6e6970717273;
12588+
let hash = Hash::from([
12589+
0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81,
12590+
0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
12591+
0x90, 0x91, 0x92, 0x93,
12592+
]);
1259012593

12591-
//UNSAFE: forcibly cast the special byte pattern to actual account fields.
12592-
let InputFields(slot, meta, account_meta, data, offset, hash) =
12593-
unsafe { std::mem::transmute::<InputBlob, InputFields>(blob) };
12594-
12595-
// When adding a field to the following constructor, make sure this is sourced from
12596-
// InputFields as well after adding new corresponding one to it. Needless to say, but note
12597-
// that the hashing code itself must be adjusted
1259812594
let stored_account = StoredAccountMeta::AppendVec(AppendVecStoredAccountMeta {
1259912595
meta: &meta,
1260012596
account_meta: &account_meta,
@@ -12605,11 +12601,8 @@ pub mod tests {
1260512601
});
1260612602
let account = stored_account.to_account_shared_data();
1260712603

12608-
let expected_account_hash = if cfg!(debug_assertions) {
12609-
Hash::from_str("8GiQSN2VvWASKPUuZgFkH4v66ihEanrDVXAkMFvLwEa8").unwrap()
12610-
} else {
12611-
Hash::from_str("9MYASra3mm8oXzMapYUonB6TcRsKFPtjhNXVgY3MPPUX").unwrap()
12612-
};
12604+
let expected_account_hash =
12605+
Hash::from_str("6VeAL4x4PVkECKL1hD1avwPE1uMCRoWiZJzVMvVNYhTq").unwrap();
1261312606

1261412607
assert_eq!(
1261512608
AccountsDb::hash_account(

0 commit comments

Comments
 (0)