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

Commit 37d46a7

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 17b1b56 commit 37d46a7

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
@@ -12549,40 +12549,36 @@ pub mod tests {
1254912549

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

12579-
//UNSAFE: forcibly cast the special byte pattern to actual account fields.
12580-
let InputFields(slot, meta, account_meta, data, offset, hash) =
12581-
unsafe { std::mem::transmute::<InputBlob, InputFields>(blob) };
12582-
12583-
// When adding a field to the following constructor, make sure this is sourced from
12584-
// InputFields as well after adding new corresponding one to it. Needless to say, but note
12585-
// that the hashing code itself must be adjusted
1258612582
let stored_account = StoredAccountMeta::AppendVec(AppendVecStoredAccountMeta {
1258712583
meta: &meta,
1258812584
account_meta: &account_meta,
@@ -12593,11 +12589,8 @@ pub mod tests {
1259312589
});
1259412590
let account = stored_account.to_account_shared_data();
1259512591

12596-
let expected_account_hash = if cfg!(debug_assertions) {
12597-
Hash::from_str("8GiQSN2VvWASKPUuZgFkH4v66ihEanrDVXAkMFvLwEa8").unwrap()
12598-
} else {
12599-
Hash::from_str("9MYASra3mm8oXzMapYUonB6TcRsKFPtjhNXVgY3MPPUX").unwrap()
12600-
};
12592+
let expected_account_hash =
12593+
Hash::from_str("6VeAL4x4PVkECKL1hD1avwPE1uMCRoWiZJzVMvVNYhTq").unwrap();
1260112594

1260212595
assert_eq!(
1260312596
AccountsDb::hash_account(

0 commit comments

Comments
 (0)