From 676da3a084ff02493a56bcb67b67171c765caae0 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Mon, 29 Apr 2024 18:50:40 +0200 Subject: [PATCH] Fix alignment of ordertree nodes (#954) This ensures casts of local variables don't run into alignment differences. (cherry picked from commit d9c4f69e0e0446a6fbf974c45d70474c262c80d9) --- programs/mango-v4/src/state/orderbook/nodes.rs | 14 +++++++++++--- programs/mango-v4/src/state/orderbook/ordertree.rs | 3 ++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/programs/mango-v4/src/state/orderbook/nodes.rs b/programs/mango-v4/src/state/orderbook/nodes.rs index 8b5454d89..96dbb810d 100644 --- a/programs/mango-v4/src/state/orderbook/nodes.rs +++ b/programs/mango-v4/src/state/orderbook/nodes.rs @@ -1,4 +1,4 @@ -use std::mem::size_of; +use std::mem::{align_of, size_of}; use anchor_lang::prelude::*; use bytemuck::{cast_mut, cast_ref}; @@ -252,7 +252,9 @@ pub struct FreeNode { pub(crate) tag: u8, // NodeTag pub(crate) padding: [u8; 3], pub(crate) next: NodeHandle, - pub(crate) reserved: [u8; NODE_SIZE - 8], + pub(crate) reserved: [u8; NODE_SIZE - 16], + // ensure that FreeNode has the same 8-byte alignment as other nodes + pub(crate) force_align: u64, } const_assert_eq!(size_of::(), NODE_SIZE); const_assert_eq!(size_of::() % 8, 0); @@ -261,13 +263,19 @@ const_assert_eq!(size_of::() % 8, 0); #[derive(bytemuck::Pod, bytemuck::Zeroable)] pub struct AnyNode { pub tag: u8, - pub data: [u8; 119], + pub data: [u8; 111], + // ensure that AnyNode has the same 8-byte alignment as other nodes + pub(crate) force_align: u64, } const_assert_eq!(size_of::(), NODE_SIZE); const_assert_eq!(size_of::() % 8, 0); const_assert_eq!(size_of::(), size_of::()); const_assert_eq!(size_of::(), size_of::()); const_assert_eq!(size_of::(), size_of::()); +const_assert_eq!(align_of::(), 8); +const_assert_eq!(align_of::(), align_of::()); +const_assert_eq!(align_of::(), align_of::()); +const_assert_eq!(align_of::(), align_of::()); pub(crate) enum NodeRef<'a> { Inner(&'a InnerNode), diff --git a/programs/mango-v4/src/state/orderbook/ordertree.rs b/programs/mango-v4/src/state/orderbook/ordertree.rs index 0e9748b31..f32a9112a 100644 --- a/programs/mango-v4/src/state/orderbook/ordertree.rs +++ b/programs/mango-v4/src/state/orderbook/ordertree.rs @@ -262,7 +262,8 @@ impl OrderTreeNodes { }, padding: Default::default(), next: self.free_list_head, - reserved: [0; 112], + reserved: [0; 104], + force_align: 0, }); self.free_list_len += 1;