trident_fuzz/config/
fuzz.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::{
    fs,
    path::{Path, PathBuf},
    str::FromStr,
};

use serde::{Deserialize, Serialize};
use solana_sdk::pubkey::Pubkey;

use super::discover_root;

#[derive(Debug, Deserialize, Clone, Default)]
pub struct Fuzz {
    pub fuzzing_with_stats: bool,
    pub allow_duplicate_txs: bool,
    pub programs: Vec<FuzzProgram>,
    pub accounts: Vec<FuzzAccount>,
}

#[derive(Default, Debug, Deserialize, Clone)]
pub struct _Fuzz {
    #[serde(default)]
    pub fuzzing_with_stats: Option<bool>,
    #[serde(default)]
    pub allow_duplicate_txs: Option<bool>,
    #[serde(default)]
    pub programs: Option<Vec<_FuzzProgram>>,
    #[serde(default)]
    pub accounts: Option<Vec<_FuzzAccount>>,
}
impl From<_Fuzz> for Fuzz {
    fn from(_f: _Fuzz) -> Self {
        let mut _self = Self {
            fuzzing_with_stats: _f.fuzzing_with_stats.unwrap_or_default(),
            allow_duplicate_txs: _f.allow_duplicate_txs.unwrap_or_default(),
            programs: vec![],
            accounts: vec![],
        };

        if let Some(accounts) = _f.accounts {
            for account in accounts {
                _self
                    .accounts
                    .push(read_and_parse_account(&account.filename));
            }
        }
        if let Some(programs) = _f.programs {
            for account in programs {
                _self
                    .programs
                    .push(read_and_parse_program(&account.program, &account.address));
            }
        }

        _self
    }
}

impl Fuzz {
    pub fn get_fuzzing_with_stats(&self) -> bool {
        self.fuzzing_with_stats
    }
    pub fn get_allow_duplicate_txs(&self) -> bool {
        self.allow_duplicate_txs
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct _FuzzProgram {
    pub address: String,
    pub program: String,
}

#[derive(Debug, Deserialize, Clone)]
pub struct _FuzzAccount {
    pub address: String,
    pub filename: String,
}

#[derive(Debug, Deserialize, Clone)]
pub struct FuzzProgram {
    pub address: Pubkey,
    pub data: Vec<u8>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct FuzzAccount {
    pub pubkey: Pubkey,
    pub account: Account,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Account {
    pub lamports: u64,
    pub data: String,
    pub owner: Pubkey,
    pub executable: bool,
    pub rent_epoch: u64,
}

#[derive(Debug, Deserialize, Clone)]
pub struct FuzzAccountRaw {
    pub pubkey: String,
    pub account: AccountRaw,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AccountRaw {
    pub lamports: u64,
    pub data: Vec<String>,
    pub owner: String,
    pub executable: bool,
    #[serde(rename = "rentEpoch")]
    pub rent_epoch: u64,
}

fn read_and_parse_program(filename: &str, program_address: &str) -> FuzzProgram {
    let path = resolve_path(filename);

    let program_data =
        fs::read(path).unwrap_or_else(|_| panic!("Failed to read file: {}", filename));

    let pubkey = Pubkey::from_str(program_address)
        .unwrap_or_else(|_| panic!("Cannot parse the program address: {}", program_address));

    FuzzProgram {
        address: pubkey,
        data: program_data,
    }
}

fn read_and_parse_account(filename: &str) -> FuzzAccount {
    let path = resolve_path(filename);

    let file_content =
        fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read file: {}", filename));

    let account_raw: FuzzAccountRaw = serde_json::from_str(&file_content)
        .unwrap_or_else(|_| panic!("Failed to parse JSON from file: {}", filename));

    let pubkey = Pubkey::from_str(&account_raw.pubkey)
        .unwrap_or_else(|_| panic!("Cannot convert address for: {}", account_raw.pubkey));

    let owner_address = Pubkey::from_str(&account_raw.account.owner).unwrap_or_else(|_| {
        panic!(
            "Cannot convert address for owner: {}",
            account_raw.account.owner
        )
    });

    let data_base_64 = account_raw.account.data.first().unwrap_or_else(|| {
        panic!(
            "Cannot read base64 data for account: {}",
            account_raw.pubkey
        )
    });

    let account = Account {
        lamports: account_raw.account.lamports,
        data: data_base_64.to_string(),
        owner: owner_address,
        executable: account_raw.account.executable,
        rent_epoch: account_raw.account.rent_epoch,
    };

    FuzzAccount { pubkey, account }
}

fn resolve_path(filename: &str) -> PathBuf {
    let path = Path::new(filename);
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        discover_root()
            .map(|cwd| cwd.join(path))
            .unwrap_or_else(|_| panic!("Failed to resolve relative path: {}", path.display()))
    }
}