solana_runtime/
append_vec.rs

1//! Persistent storage for accounts.
2//!
3//! For more information, see:
4//!
5//! <https://docs.solana.com/implemented-proposals/persistent-account-storage>
6
7use {
8    log::*,
9    memmap2::MmapMut,
10    serde::{Deserialize, Serialize},
11    solana_sdk::{
12        account::{Account, AccountSharedData, ReadableAccount},
13        clock::{Epoch, Slot},
14        hash::Hash,
15        pubkey::Pubkey,
16    },
17    std::{
18        borrow::Borrow,
19        convert::TryFrom,
20        fs::{remove_file, OpenOptions},
21        io::{self, Seek, SeekFrom, Write},
22        mem,
23        path::{Path, PathBuf},
24        sync::{
25            atomic::{AtomicUsize, Ordering},
26            Mutex,
27        },
28    },
29};
30
31pub mod test_utils;
32
33// Data placement should be aligned at the next boundary. Without alignment accessing the memory may
34// crash on some architectures.
35pub const ALIGN_BOUNDARY_OFFSET: usize = mem::size_of::<u64>();
36macro_rules! u64_align {
37    ($addr: expr) => {
38        ($addr + (ALIGN_BOUNDARY_OFFSET - 1)) & !(ALIGN_BOUNDARY_OFFSET - 1)
39    };
40}
41
42pub const MAXIMUM_APPEND_VEC_FILE_SIZE: u64 = 16 * 1024 * 1024 * 1024; // 16 GiB
43
44pub type StoredMetaWriteVersion = u64;
45
46/// Meta contains enough context to recover the index from storage itself
47/// This struct will be backed by mmaped and snapshotted data files.
48/// So the data layout must be stable and consistent across the entire cluster!
49#[derive(Clone, PartialEq, Eq, Debug)]
50pub struct StoredMeta {
51    /// global write version
52    pub write_version: StoredMetaWriteVersion,
53    /// key for the account
54    pub pubkey: Pubkey,
55    pub data_len: u64,
56}
57
58/// This struct will be backed by mmaped and snapshotted data files.
59/// So the data layout must be stable and consistent across the entire cluster!
60#[derive(Serialize, Deserialize, Clone, Debug, Default, Eq, PartialEq)]
61pub struct AccountMeta {
62    /// lamports in the account
63    pub lamports: u64,
64    /// the program that owns this account. If executable, the program that loads this account.
65    pub owner: Pubkey,
66    /// this account's data contains a loaded program (and is now read-only)
67    pub executable: bool,
68    /// the epoch at which this account will next owe rent
69    pub rent_epoch: Epoch,
70}
71
72impl<'a, T: ReadableAccount> From<&'a T> for AccountMeta {
73    fn from(account: &'a T) -> Self {
74        Self {
75            lamports: account.lamports(),
76            owner: *account.owner(),
77            executable: account.executable(),
78            rent_epoch: account.rent_epoch(),
79        }
80    }
81}
82
83impl<'a, T: ReadableAccount> From<Option<&'a T>> for AccountMeta {
84    fn from(account: Option<&'a T>) -> Self {
85        match account {
86            Some(account) => AccountMeta::from(account),
87            None => AccountMeta::default(),
88        }
89    }
90}
91
92/// References to account data stored elsewhere. Getting an `Account` requires cloning
93/// (see `StoredAccountMeta::clone_account()`).
94#[derive(PartialEq, Eq, Debug)]
95pub struct StoredAccountMeta<'a> {
96    pub meta: &'a StoredMeta,
97    /// account data
98    pub account_meta: &'a AccountMeta,
99    pub data: &'a [u8],
100    pub offset: usize,
101    pub stored_size: usize,
102    pub hash: &'a Hash,
103}
104
105impl<'a> StoredAccountMeta<'a> {
106    /// Return a new Account by copying all the data referenced by the `StoredAccountMeta`.
107    pub fn clone_account(&self) -> AccountSharedData {
108        AccountSharedData::from(Account {
109            lamports: self.account_meta.lamports,
110            owner: self.account_meta.owner,
111            executable: self.account_meta.executable,
112            rent_epoch: self.account_meta.rent_epoch,
113            data: self.data.to_vec(),
114        })
115    }
116
117    fn sanitize(&self) -> bool {
118        self.sanitize_executable() && self.sanitize_lamports()
119    }
120
121    fn sanitize_executable(&self) -> bool {
122        // Sanitize executable to ensure higher 7-bits are cleared correctly.
123        self.ref_executable_byte() & !1 == 0
124    }
125
126    fn sanitize_lamports(&self) -> bool {
127        // Sanitize 0 lamports to ensure to be same as AccountSharedData::default()
128        self.account_meta.lamports != 0 || self.clone_account() == AccountSharedData::default()
129    }
130
131    fn ref_executable_byte(&self) -> &u8 {
132        // Use extra references to avoid value silently clamped to 1 (=true) and 0 (=false)
133        // Yes, this really happens; see test_new_from_file_crafted_executable
134        let executable_bool: &bool = &self.account_meta.executable;
135        // UNSAFE: Force to interpret mmap-backed bool as u8 to really read the actual memory content
136        let executable_byte: &u8 = unsafe { &*(executable_bool as *const bool as *const u8) };
137        executable_byte
138    }
139}
140
141pub struct AppendVecAccountsIter<'a> {
142    append_vec: &'a AppendVec,
143    offset: usize,
144}
145
146impl<'a> AppendVecAccountsIter<'a> {
147    pub fn new(append_vec: &'a AppendVec) -> Self {
148        Self {
149            append_vec,
150            offset: 0,
151        }
152    }
153}
154
155impl<'a> Iterator for AppendVecAccountsIter<'a> {
156    type Item = StoredAccountMeta<'a>;
157
158    fn next(&mut self) -> Option<Self::Item> {
159        if let Some((account, next_offset)) = self.append_vec.get_account(self.offset) {
160            self.offset = next_offset;
161            Some(account)
162        } else {
163            None
164        }
165    }
166}
167
168/// A thread-safe, file-backed block of memory used to store `Account` instances. Append operations
169/// are serialized such that only one thread updates the internal `append_lock` at a time. No
170/// restrictions are placed on reading. That is, one may read items from one thread while another
171/// is appending new items.
172#[derive(Debug, AbiExample)]
173pub struct AppendVec {
174    /// The file path where the data is stored.
175    path: PathBuf,
176
177    /// A file-backed block of memory that is used to store the data for each appended item.
178    map: MmapMut,
179
180    /// A lock used to serialize append operations.
181    append_lock: Mutex<()>,
182
183    /// The number of bytes used to store items, not the number of items.
184    current_len: AtomicUsize,
185
186    /// The number of bytes available for storing items.
187    file_size: u64,
188
189    /// True if the file should automatically be deleted when this AppendVec is dropped.
190    remove_on_drop: bool,
191}
192
193impl Drop for AppendVec {
194    fn drop(&mut self) {
195        if self.remove_on_drop {
196            if let Err(_e) = remove_file(&self.path) {
197                // promote this to panic soon.
198                // disabled due to many false positive warnings while running tests.
199                // blocked by rpc's upgrade to jsonrpc v17
200                //error!("AppendVec failed to remove {:?}: {:?}", &self.path, e);
201                inc_new_counter_info!("append_vec_drop_fail", 1);
202            }
203        }
204    }
205}
206
207impl AppendVec {
208    pub fn new(file: &Path, create: bool, size: usize) -> Self {
209        let initial_len = 0;
210        AppendVec::sanitize_len_and_size(initial_len, size).unwrap();
211
212        if create {
213            let _ignored = remove_file(file);
214        }
215
216        let mut data = OpenOptions::new()
217            .read(true)
218            .write(true)
219            .create(create)
220            .open(file)
221            .map_err(|e| {
222                panic!(
223                    "Unable to {} data file {} in current dir({:?}): {:?}",
224                    if create { "create" } else { "open" },
225                    file.display(),
226                    std::env::current_dir(),
227                    e
228                );
229            })
230            .unwrap();
231
232        // Theoretical performance optimization: write a zero to the end of
233        // the file so that we won't have to resize it later, which may be
234        // expensive.
235        data.seek(SeekFrom::Start((size - 1) as u64)).unwrap();
236        data.write_all(&[0]).unwrap();
237        data.seek(SeekFrom::Start(0)).unwrap();
238        data.flush().unwrap();
239
240        //UNSAFE: Required to create a Mmap
241        let map = unsafe { MmapMut::map_mut(&data) };
242        let map = map.unwrap_or_else(|e| {
243            error!(
244                "Failed to map the data file (size: {}): {}.\n
245                    Please increase sysctl vm.max_map_count or equivalent for your platform.",
246                size, e
247            );
248            std::process::exit(1);
249        });
250
251        AppendVec {
252            path: file.to_path_buf(),
253            map,
254            // This mutex forces append to be single threaded, but concurrent with reads
255            // See UNSAFE usage in `append_ptr`
256            append_lock: Mutex::new(()),
257            current_len: AtomicUsize::new(initial_len),
258            file_size: size as u64,
259            remove_on_drop: true,
260        }
261    }
262
263    pub fn set_no_remove_on_drop(&mut self) {
264        self.remove_on_drop = false;
265    }
266
267    fn sanitize_len_and_size(current_len: usize, file_size: usize) -> io::Result<()> {
268        if file_size == 0 {
269            Err(std::io::Error::new(
270                std::io::ErrorKind::Other,
271                format!("too small file size {} for AppendVec", file_size),
272            ))
273        } else if usize::try_from(MAXIMUM_APPEND_VEC_FILE_SIZE)
274            .map(|max| file_size > max)
275            .unwrap_or(true)
276        {
277            Err(std::io::Error::new(
278                std::io::ErrorKind::Other,
279                format!("too large file size {} for AppendVec", file_size),
280            ))
281        } else if current_len > file_size {
282            Err(std::io::Error::new(
283                std::io::ErrorKind::Other,
284                format!("current_len is larger than file size ({})", file_size),
285            ))
286        } else {
287            Ok(())
288        }
289    }
290
291    pub fn flush(&self) -> io::Result<()> {
292        self.map.flush()
293    }
294
295    pub fn reset(&self) {
296        // This mutex forces append to be single threaded, but concurrent with reads
297        // See UNSAFE usage in `append_ptr`
298        let _lock = self.append_lock.lock().unwrap();
299        self.current_len.store(0, Ordering::Release);
300    }
301
302    /// how many more bytes can be stored in this append vec
303    pub fn remaining_bytes(&self) -> u64 {
304        (self.capacity()).saturating_sub(self.len() as u64)
305    }
306
307    pub fn len(&self) -> usize {
308        self.current_len.load(Ordering::Acquire)
309    }
310
311    pub fn is_empty(&self) -> bool {
312        self.len() == 0
313    }
314
315    pub fn capacity(&self) -> u64 {
316        self.file_size
317    }
318
319    pub fn file_name(slot: Slot, id: impl std::fmt::Display) -> String {
320        format!("{}.{}", slot, id)
321    }
322
323    pub fn new_from_file<P: AsRef<Path>>(path: P, current_len: usize) -> io::Result<(Self, usize)> {
324        let new = Self::new_from_file_unchecked(path, current_len)?;
325
326        let (sanitized, num_accounts) = new.sanitize_layout_and_length();
327        if !sanitized {
328            return Err(std::io::Error::new(
329                std::io::ErrorKind::Other,
330                "incorrect layout/length/data",
331            ));
332        }
333
334        Ok((new, num_accounts))
335    }
336
337    /// Creates an appendvec from file without performing sanitize checks or counting the number of accounts
338    pub fn new_from_file_unchecked<P: AsRef<Path>>(
339        path: P,
340        current_len: usize,
341    ) -> io::Result<Self> {
342        let file_size = std::fs::metadata(&path)?.len();
343        Self::sanitize_len_and_size(current_len, file_size as usize)?;
344
345        let data = OpenOptions::new()
346            .read(true)
347            .write(true)
348            .create(false)
349            .open(&path)?;
350
351        let map = unsafe {
352            let result = MmapMut::map_mut(&data);
353            if result.is_err() {
354                // for vm.max_map_count, error is: {code: 12, kind: Other, message: "Cannot allocate memory"}
355                info!("memory map error: {:?}. This may be because vm.max_map_count is not set correctly.", result);
356            }
357            result?
358        };
359
360        Ok(AppendVec {
361            path: path.as_ref().to_path_buf(),
362            map,
363            append_lock: Mutex::new(()),
364            current_len: AtomicUsize::new(current_len),
365            file_size,
366            remove_on_drop: true,
367        })
368    }
369
370    fn sanitize_layout_and_length(&self) -> (bool, usize) {
371        let mut offset = 0;
372
373        // This discards allocated accounts immediately after check at each loop iteration.
374        //
375        // This code should not reuse AppendVec.accounts() method as the current form or
376        // extend it to be reused here because it would allow attackers to accumulate
377        // some measurable amount of memory needlessly.
378        let mut num_accounts = 0;
379        while let Some((account, next_offset)) = self.get_account(offset) {
380            if !account.sanitize() {
381                return (false, num_accounts);
382            }
383            offset = next_offset;
384            num_accounts += 1;
385        }
386        let aligned_current_len = u64_align!(self.current_len.load(Ordering::Acquire));
387
388        (offset == aligned_current_len, num_accounts)
389    }
390
391    /// Get a reference to the data at `offset` of `size` bytes if that slice
392    /// doesn't overrun the internal buffer. Otherwise return None.
393    /// Also return the offset of the first byte after the requested data that
394    /// falls on a 64-byte boundary.
395    fn get_slice(&self, offset: usize, size: usize) -> Option<(&[u8], usize)> {
396        let (next, overflow) = offset.overflowing_add(size);
397        if overflow || next > self.len() {
398            return None;
399        }
400        let data = &self.map[offset..next];
401        let next = u64_align!(next);
402
403        Some((
404            //UNSAFE: This unsafe creates a slice that represents a chunk of self.map memory
405            //The lifetime of this slice is tied to &self, since it points to self.map memory
406            unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u8, size) },
407            next,
408        ))
409    }
410
411    /// Copy `len` bytes from `src` to the first 64-byte boundary after position `offset` of
412    /// the internal buffer. Then update `offset` to the first byte after the copied data.
413    fn append_ptr(&self, offset: &mut usize, src: *const u8, len: usize) {
414        let pos = u64_align!(*offset);
415        let data = &self.map[pos..(pos + len)];
416        //UNSAFE: This mut append is safe because only 1 thread can append at a time
417        //Mutex<()> guarantees exclusive write access to the memory occupied in
418        //the range.
419        unsafe {
420            let dst = data.as_ptr() as *mut u8;
421            std::ptr::copy(src, dst, len);
422        };
423        *offset = pos + len;
424    }
425
426    /// Copy each value in `vals`, in order, to the first 64-byte boundary after position `offset`.
427    /// If there is sufficient space, then update `offset` and the internal `current_len` to the
428    /// first byte after the copied data and return the starting position of the copied data.
429    /// Otherwise return None and leave `offset` unchanged.
430    fn append_ptrs_locked(&self, offset: &mut usize, vals: &[(*const u8, usize)]) -> Option<usize> {
431        let mut end = *offset;
432        for val in vals {
433            end = u64_align!(end);
434            end += val.1;
435        }
436
437        if (self.file_size as usize) < end {
438            return None;
439        }
440
441        let pos = u64_align!(*offset);
442        for val in vals {
443            self.append_ptr(offset, val.0, val.1)
444        }
445        self.current_len.store(*offset, Ordering::Release);
446        Some(pos)
447    }
448
449    /// Return a reference to the type at `offset` if its data doesn't overrun the internal buffer.
450    /// Otherwise return None. Also return the offset of the first byte after the requested data
451    /// that falls on a 64-byte boundary.
452    fn get_type<'a, T>(&self, offset: usize) -> Option<(&'a T, usize)> {
453        let (data, next) = self.get_slice(offset, mem::size_of::<T>())?;
454        let ptr: *const T = data.as_ptr() as *const T;
455        //UNSAFE: The cast is safe because the slice is aligned and fits into the memory
456        //and the lifetime of the &T is tied to self, which holds the underlying memory map
457        Some((unsafe { &*ptr }, next))
458    }
459
460    /// Return account metadata for the account at `offset` if its data doesn't overrun
461    /// the internal buffer. Otherwise return None. Also return the offset of the first byte
462    /// after the requested data that falls on a 64-byte boundary.
463    pub fn get_account<'a>(&'a self, offset: usize) -> Option<(StoredAccountMeta<'a>, usize)> {
464        let (meta, next): (&'a StoredMeta, _) = self.get_type(offset)?;
465        let (account_meta, next): (&'a AccountMeta, _) = self.get_type(next)?;
466        let (hash, next): (&'a Hash, _) = self.get_type(next)?;
467        let (data, next) = self.get_slice(next, meta.data_len as usize)?;
468        let stored_size = next - offset;
469        Some((
470            StoredAccountMeta {
471                meta,
472                account_meta,
473                data,
474                offset,
475                stored_size,
476                hash,
477            },
478            next,
479        ))
480    }
481
482    #[cfg(test)]
483    pub fn get_account_test(&self, offset: usize) -> Option<(StoredMeta, AccountSharedData)> {
484        let (stored_account, _) = self.get_account(offset)?;
485        let meta = stored_account.meta.clone();
486        Some((meta, stored_account.clone_account()))
487    }
488
489    pub fn get_path(&self) -> PathBuf {
490        self.path.clone()
491    }
492
493    /// Return iterator for account metadata
494    pub fn account_iter(&self) -> AppendVecAccountsIter {
495        AppendVecAccountsIter::new(self)
496    }
497
498    /// Return a vector of account metadata for each account, starting from `offset`.
499    pub fn accounts(&self, mut offset: usize) -> Vec<StoredAccountMeta> {
500        let mut accounts = vec![];
501        while let Some((account, next)) = self.get_account(offset) {
502            accounts.push(account);
503            offset = next;
504        }
505        accounts
506    }
507
508    /// Copy each account metadata, account and hash to the internal buffer.
509    /// Return the starting offset of each account metadata.
510    /// After each account is appended, the internal `current_len` is updated
511    /// and will be available to other threads.
512    pub fn append_accounts(
513        &self,
514        accounts: &[(StoredMeta, Option<&impl ReadableAccount>)],
515        hashes: &[impl Borrow<Hash>],
516    ) -> Vec<usize> {
517        let _lock = self.append_lock.lock().unwrap();
518        let mut offset = self.len();
519        let mut rv = Vec::with_capacity(accounts.len());
520        for ((stored_meta, account), hash) in accounts.iter().zip(hashes) {
521            let meta_ptr = stored_meta as *const StoredMeta;
522            let account_meta = AccountMeta::from(*account);
523            let account_meta_ptr = &account_meta as *const AccountMeta;
524            let data_len = stored_meta.data_len as usize;
525            let data_ptr = account
526                .map(|account| account.data())
527                .unwrap_or_default()
528                .as_ptr();
529            let hash_ptr = hash.borrow().as_ref().as_ptr();
530            let ptrs = [
531                (meta_ptr as *const u8, mem::size_of::<StoredMeta>()),
532                (account_meta_ptr as *const u8, mem::size_of::<AccountMeta>()),
533                (hash_ptr as *const u8, mem::size_of::<Hash>()),
534                (data_ptr, data_len),
535            ];
536            if let Some(res) = self.append_ptrs_locked(&mut offset, &ptrs) {
537                rv.push(res)
538            } else {
539                break;
540            }
541        }
542
543        // The last entry in this offset needs to be the u64 aligned offset, because that's
544        // where the *next* entry will begin to be stored.
545        rv.push(u64_align!(offset));
546
547        rv
548    }
549
550    /// Copy the account metadata, account and hash to the internal buffer.
551    /// Return the starting offset of the account metadata.
552    /// After the account is appended, the internal `current_len` is updated.
553    pub fn append_account(
554        &self,
555        storage_meta: StoredMeta,
556        account: &AccountSharedData,
557        hash: Hash,
558    ) -> Option<usize> {
559        let res = self.append_accounts(&[(storage_meta, Some(account))], &[&hash]);
560        if res.len() == 1 {
561            None
562        } else {
563            res.first().cloned()
564        }
565    }
566}
567
568#[cfg(test)]
569pub mod tests {
570    use {
571        super::{test_utils::*, *},
572        assert_matches::assert_matches,
573        rand::{thread_rng, Rng},
574        solana_sdk::{account::WritableAccount, timing::duration_as_ms},
575        std::time::Instant,
576    };
577
578    impl AppendVec {
579        fn append_account_test(&self, data: &(StoredMeta, AccountSharedData)) -> Option<usize> {
580            self.append_account(data.0.clone(), &data.1, Hash::default())
581        }
582    }
583
584    impl<'a> StoredAccountMeta<'a> {
585        #[allow(clippy::cast_ref_to_mut)]
586        fn set_data_len_unsafe(&self, new_data_len: u64) {
587            // UNSAFE: cast away & (= const ref) to &mut to force to mutate append-only (=read-only) AppendVec
588            unsafe {
589                *(&self.meta.data_len as *const u64 as *mut u64) = new_data_len;
590            }
591        }
592
593        fn get_executable_byte(&self) -> u8 {
594            let executable_bool: bool = self.account_meta.executable;
595            // UNSAFE: Force to interpret mmap-backed bool as u8 to really read the actual memory content
596            let executable_byte: u8 = unsafe { std::mem::transmute::<bool, u8>(executable_bool) };
597            executable_byte
598        }
599
600        #[allow(clippy::cast_ref_to_mut)]
601        fn set_executable_as_byte(&self, new_executable_byte: u8) {
602            // UNSAFE: Force to interpret mmap-backed &bool as &u8 to write some crafted value;
603            unsafe {
604                *(&self.account_meta.executable as *const bool as *mut u8) = new_executable_byte;
605            }
606        }
607    }
608
609    #[test]
610    fn test_account_meta_default() {
611        let def1 = AccountMeta::default();
612        let def2 = AccountMeta::from(&Account::default());
613        assert_eq!(&def1, &def2);
614        let def2 = AccountMeta::from(&AccountSharedData::default());
615        assert_eq!(&def1, &def2);
616        let def2 = AccountMeta::from(Some(&AccountSharedData::default()));
617        assert_eq!(&def1, &def2);
618        let none: Option<&AccountSharedData> = None;
619        let def2 = AccountMeta::from(none);
620        assert_eq!(&def1, &def2);
621    }
622
623    #[test]
624    fn test_account_meta_non_default() {
625        let def1 = AccountMeta {
626            lamports: 1,
627            owner: Pubkey::new_unique(),
628            executable: true,
629            rent_epoch: 3,
630        };
631        let def2_account = Account {
632            lamports: def1.lamports,
633            owner: def1.owner,
634            executable: def1.executable,
635            rent_epoch: def1.rent_epoch,
636            data: Vec::new(),
637        };
638        let def2 = AccountMeta::from(&def2_account);
639        assert_eq!(&def1, &def2);
640        let def2 = AccountMeta::from(&AccountSharedData::from(def2_account.clone()));
641        assert_eq!(&def1, &def2);
642        let def2 = AccountMeta::from(Some(&AccountSharedData::from(def2_account)));
643        assert_eq!(&def1, &def2);
644    }
645
646    #[test]
647    #[should_panic(expected = "too small file size 0 for AppendVec")]
648    fn test_append_vec_new_bad_size() {
649        let path = get_append_vec_path("test_append_vec_new_bad_size");
650        let _av = AppendVec::new(&path.path, true, 0);
651    }
652
653    #[test]
654    fn test_append_vec_new_from_file_bad_size() {
655        let file = get_append_vec_path("test_append_vec_new_from_file_bad_size");
656        let path = &file.path;
657
658        let _data = OpenOptions::new()
659            .read(true)
660            .write(true)
661            .create(true)
662            .open(path)
663            .expect("create a test file for mmap");
664
665        let result = AppendVec::new_from_file(path, 0);
666        assert_matches!(result, Err(ref message) if message.to_string() == *"too small file size 0 for AppendVec");
667    }
668
669    #[test]
670    fn test_append_vec_sanitize_len_and_size_too_small() {
671        const LEN: usize = 0;
672        const SIZE: usize = 0;
673        let result = AppendVec::sanitize_len_and_size(LEN, SIZE);
674        assert_matches!(result, Err(ref message) if message.to_string() == *"too small file size 0 for AppendVec");
675    }
676
677    #[test]
678    fn test_append_vec_sanitize_len_and_size_maximum() {
679        const LEN: usize = 0;
680        const SIZE: usize = 16 * 1024 * 1024 * 1024;
681        let result = AppendVec::sanitize_len_and_size(LEN, SIZE);
682        assert_matches!(result, Ok(_));
683    }
684
685    #[test]
686    fn test_append_vec_sanitize_len_and_size_too_large() {
687        const LEN: usize = 0;
688        const SIZE: usize = 16 * 1024 * 1024 * 1024 + 1;
689        let result = AppendVec::sanitize_len_and_size(LEN, SIZE);
690        assert_matches!(result, Err(ref message) if message.to_string() == *"too large file size 17179869185 for AppendVec");
691    }
692
693    #[test]
694    fn test_append_vec_sanitize_len_and_size_full_and_same_as_current_len() {
695        const LEN: usize = 1024 * 1024;
696        const SIZE: usize = 1024 * 1024;
697        let result = AppendVec::sanitize_len_and_size(LEN, SIZE);
698        assert_matches!(result, Ok(_));
699    }
700
701    #[test]
702    fn test_append_vec_sanitize_len_and_size_larger_current_len() {
703        const LEN: usize = 1024 * 1024 + 1;
704        const SIZE: usize = 1024 * 1024;
705        let result = AppendVec::sanitize_len_and_size(LEN, SIZE);
706        assert_matches!(result, Err(ref message) if message.to_string() == *"current_len is larger than file size (1048576)");
707    }
708
709    #[test]
710    fn test_append_vec_one() {
711        let path = get_append_vec_path("test_append");
712        let av = AppendVec::new(&path.path, true, 1024 * 1024);
713        let account = create_test_account(0);
714        let index = av.append_account_test(&account).unwrap();
715        assert_eq!(av.get_account_test(index).unwrap(), account);
716    }
717
718    #[test]
719    fn test_remaining_bytes() {
720        let path = get_append_vec_path("test_append");
721        let sz = 1024 * 1024;
722        let sz64 = sz as u64;
723        let av = AppendVec::new(&path.path, true, sz);
724        assert_eq!(av.capacity(), sz64);
725        assert_eq!(av.remaining_bytes(), sz64);
726        let account = create_test_account(0);
727        let acct_size = 136;
728        av.append_account_test(&account).unwrap();
729        assert_eq!(av.capacity(), sz64);
730        assert_eq!(av.remaining_bytes(), sz64 - acct_size);
731    }
732
733    #[test]
734    fn test_append_vec_data() {
735        let path = get_append_vec_path("test_append_data");
736        let av = AppendVec::new(&path.path, true, 1024 * 1024);
737        let account = create_test_account(5);
738        let index = av.append_account_test(&account).unwrap();
739        assert_eq!(av.get_account_test(index).unwrap(), account);
740        let account1 = create_test_account(6);
741        let index1 = av.append_account_test(&account1).unwrap();
742        assert_eq!(av.get_account_test(index).unwrap(), account);
743        assert_eq!(av.get_account_test(index1).unwrap(), account1);
744    }
745
746    #[test]
747    fn test_append_vec_append_many() {
748        let path = get_append_vec_path("test_append_many");
749        let av = AppendVec::new(&path.path, true, 1024 * 1024);
750        let size = 1000;
751        let mut indexes = vec![];
752        let now = Instant::now();
753        for sample in 0..size {
754            let account = create_test_account(sample);
755            let pos = av.append_account_test(&account).unwrap();
756            assert_eq!(av.get_account_test(pos).unwrap(), account);
757            indexes.push(pos)
758        }
759        trace!("append time: {} ms", duration_as_ms(&now.elapsed()),);
760
761        let now = Instant::now();
762        for _ in 0..size {
763            let sample = thread_rng().gen_range(0, indexes.len());
764            let account = create_test_account(sample);
765            assert_eq!(av.get_account_test(indexes[sample]).unwrap(), account);
766        }
767        trace!("random read time: {} ms", duration_as_ms(&now.elapsed()),);
768
769        let now = Instant::now();
770        assert_eq!(indexes.len(), size);
771        assert_eq!(indexes[0], 0);
772        let mut accounts = av.accounts(indexes[0]);
773        assert_eq!(accounts.len(), size);
774        for (sample, v) in accounts.iter_mut().enumerate() {
775            let account = create_test_account(sample);
776            let recovered = v.clone_account();
777            assert_eq!(recovered, account.1)
778        }
779        trace!(
780            "sequential read time: {} ms",
781            duration_as_ms(&now.elapsed()),
782        );
783    }
784
785    #[test]
786    fn test_new_from_file_crafted_zero_lamport_account() {
787        let file = get_append_vec_path("test_append");
788        let path = &file.path;
789        let mut av = AppendVec::new(path, true, 1024 * 1024);
790        av.set_no_remove_on_drop();
791
792        let pubkey = solana_sdk::pubkey::new_rand();
793        let owner = Pubkey::default();
794        let data_len = 3_u64;
795        let mut account = AccountSharedData::new(0, data_len as usize, &owner);
796        account.set_data(b"abc".to_vec());
797        let stored_meta = StoredMeta {
798            write_version: 0,
799            pubkey,
800            data_len,
801        };
802        let account_with_meta = (stored_meta, account);
803        let index = av.append_account_test(&account_with_meta).unwrap();
804        assert_eq!(av.get_account_test(index).unwrap(), account_with_meta);
805
806        av.flush().unwrap();
807        let accounts_len = av.len();
808        drop(av);
809        let result = AppendVec::new_from_file(path, accounts_len);
810        assert_matches!(result, Err(ref message) if message.to_string() == *"incorrect layout/length/data");
811    }
812
813    #[test]
814    fn test_new_from_file_crafted_data_len() {
815        let file = get_append_vec_path("test_new_from_file_crafted_data_len");
816        let path = &file.path;
817        let mut av = AppendVec::new(path, true, 1024 * 1024);
818        av.set_no_remove_on_drop();
819
820        let crafted_data_len = 1;
821
822        av.append_account_test(&create_test_account(10)).unwrap();
823
824        let accounts = av.accounts(0);
825        let account = accounts.first().unwrap();
826        account.set_data_len_unsafe(crafted_data_len);
827        assert_eq!(account.meta.data_len, crafted_data_len);
828
829        // Reload accounts and observe crafted_data_len
830        let accounts = av.accounts(0);
831        let account = accounts.first().unwrap();
832        assert_eq!(account.meta.data_len, crafted_data_len);
833
834        av.flush().unwrap();
835        let accounts_len = av.len();
836        drop(av);
837        let result = AppendVec::new_from_file(path, accounts_len);
838        assert_matches!(result, Err(ref message) if message.to_string() == *"incorrect layout/length/data");
839    }
840
841    #[test]
842    fn test_new_from_file_too_large_data_len() {
843        let file = get_append_vec_path("test_new_from_file_too_large_data_len");
844        let path = &file.path;
845        let mut av = AppendVec::new(path, true, 1024 * 1024);
846        av.set_no_remove_on_drop();
847
848        let too_large_data_len = u64::max_value();
849        av.append_account_test(&create_test_account(10)).unwrap();
850
851        let accounts = av.accounts(0);
852        let account = accounts.first().unwrap();
853        account.set_data_len_unsafe(too_large_data_len);
854        assert_eq!(account.meta.data_len, too_large_data_len);
855
856        // Reload accounts and observe no account with bad offset
857        let accounts = av.accounts(0);
858        assert_matches!(accounts.first(), None);
859
860        av.flush().unwrap();
861        let accounts_len = av.len();
862        drop(av);
863        let result = AppendVec::new_from_file(path, accounts_len);
864        assert_matches!(result, Err(ref message) if message.to_string() == *"incorrect layout/length/data");
865    }
866
867    #[test]
868    fn test_new_from_file_crafted_executable() {
869        let file = get_append_vec_path("test_new_from_crafted_executable");
870        let path = &file.path;
871        let mut av = AppendVec::new(path, true, 1024 * 1024);
872        av.set_no_remove_on_drop();
873        av.append_account_test(&create_test_account(10)).unwrap();
874        {
875            let mut executable_account = create_test_account(10);
876            executable_account.1.set_executable(true);
877            av.append_account_test(&executable_account).unwrap();
878        }
879
880        // reload accounts
881        let accounts = av.accounts(0);
882
883        // ensure false is 0u8 and true is 1u8 actually
884        assert_eq!(*accounts[0].ref_executable_byte(), 0);
885        assert_eq!(*accounts[1].ref_executable_byte(), 1);
886
887        let account = &accounts[0];
888        let crafted_executable = u8::max_value() - 1;
889
890        account.set_executable_as_byte(crafted_executable);
891
892        // reload crafted accounts
893        let accounts = av.accounts(0);
894        let account = accounts.first().unwrap();
895
896        // upper 7-bits are not 0, so sanitization should fail
897        assert!(!account.sanitize_executable());
898
899        // we can observe crafted value by ref
900        {
901            let executable_bool: &bool = &account.account_meta.executable;
902            // Depending on use, *executable_bool can be truthy or falsy due to direct memory manipulation
903            // assert_eq! thinks *executable_bool is equal to false but the if condition thinks it's not, contradictorily.
904            assert!(!*executable_bool);
905            #[cfg(not(target_arch = "aarch64"))]
906            {
907                const FALSE: bool = false; // keep clippy happy
908                if *executable_bool == FALSE {
909                    panic!("This didn't occur if this test passed.");
910                }
911            }
912            assert_eq!(*account.ref_executable_byte(), crafted_executable);
913        }
914
915        // we can NOT observe crafted value by value
916        {
917            let executable_bool: bool = account.account_meta.executable;
918            assert!(!executable_bool);
919            assert_eq!(account.get_executable_byte(), 0); // Wow, not crafted_executable!
920        }
921
922        av.flush().unwrap();
923        let accounts_len = av.len();
924        drop(av);
925        let result = AppendVec::new_from_file(path, accounts_len);
926        assert_matches!(result, Err(ref message) if message.to_string() == *"incorrect layout/length/data");
927    }
928}