bat-cli 0.12.1

Blockchain Auditor Toolkit (BAT)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use crate::batbelt::metadata::functions_source_code_metadata::{
    FunctionMetadataType, FunctionSourceCodeMetadata,
};

use crate::batbelt::metadata::structs_source_code_metadata::{
    StructMetadataType, StructSourceCodeMetadata,
};
use crate::batbelt::parser::syn_struct_classifier;
use crate::batbelt::sonar::{BatSonar, SonarResultType};

use crate::config::{BatConfig, ProjectType};

use error_stack::{IntoReport, Report, Result, ResultExt};
use std::collections::HashSet;
use std::fs;

use crate::batbelt::metadata::entrypoint_metadata::EntrypointMetadata;
use crate::batbelt::metadata::{BatMetadata, SourceCodeMetadata};
use crate::batbelt::parser::function_parser::FunctionParser;

use crate::batbelt::parser::ParserError;

#[derive(Clone, Debug)]
pub struct EntrypointParser {
    pub name: String,
    pub program_name: String,
    pub dependencies: Vec<FunctionSourceCodeMetadata>,
    pub context_accounts: Option<StructSourceCodeMetadata>,
    pub entry_point_function: FunctionSourceCodeMetadata,
}

impl EntrypointParser {
    pub fn new(
        name: String,
        program_name: String,
        dependencies: Vec<FunctionSourceCodeMetadata>,
        context_accounts: Option<StructSourceCodeMetadata>,
        entry_point_function: FunctionSourceCodeMetadata,
    ) -> Self {
        Self {
            name,
            program_name,
            dependencies,
            context_accounts,
            entry_point_function,
        }
    }

    pub fn new_from_name(entrypoint_name: &str) -> Result<Self, ParserError> {
        Self::new_from_name_and_program(entrypoint_name, None)
    }

    pub fn new_from_name_and_program(
        entrypoint_name: &str,
        program_name: Option<&str>,
    ) -> Result<Self, ParserError> {
        let bat_metadata = BatMetadata::read_metadata().change_context(ParserError)?;
        if let Ok(ep_metadata) =
            bat_metadata.get_entrypoint_metadata_by_name(entrypoint_name.to_string())
        {
            let entry_point_function = bat_metadata
                .source_code
                .get_function_by_id(ep_metadata.entrypoint_function_id.clone())
                .change_context(ParserError)?;
            let context_accounts = if ep_metadata.context_accounts_id.is_empty() {
                None
            } else {
                bat_metadata
                    .source_code
                    .get_struct_by_id(ep_metadata.context_accounts_id.clone())
                    .ok()
            };

            // Resolve dependencies recursively from the entrypoint function
            // First ensure the entrypoint function's dependencies are computed
            let _ = FunctionParser::new_from_metadata(entry_point_function.clone());
            let bat_metadata = BatMetadata::read_metadata().change_context(ParserError)?;
            let dependencies =
                Self::resolve_all_dependencies(&entry_point_function.metadata_id, &bat_metadata);

            return Ok(Self {
                name: ep_metadata.name,
                program_name: ep_metadata.program_name,
                dependencies,
                context_accounts,
                entry_point_function,
            });
        };

        let entrypoint_section = BatMetadata::read_metadata()
            .change_context(ParserError)?
            .source_code
            .functions_source_code
            .into_iter()
            .filter(|func_meta| {
                func_meta.name == entrypoint_name
                    && func_meta.function_type == FunctionMetadataType::EntryPoint
                    && program_name.is_none_or(|pn| {
                        func_meta.program_name.is_empty() || func_meta.program_name == pn
                    })
            })
            .collect::<Vec<_>>();

        if entrypoint_section.len() != 1 {
            return Err(Report::new(ParserError)
                .attach_printable(
                    "Incorrect amount of results looking for entrypoint function section"
                        .to_string(),
                )
                .attach_printable(format!("expected: 1,  got: {}", entrypoint_section.len()))
                .attach_printable(format!("sections_filtered:\n{:#?}", entrypoint_section)))?;
        }

        let entrypoint_function = entrypoint_section.first().unwrap().clone();
        let resolved_program_name = if let Some(pn) = program_name {
            pn.to_string()
        } else {
            entrypoint_function.program_name.clone()
        };

        let config = BatConfig::get_config().change_context(ParserError)?;
        let context_accounts = if config.project_type == ProjectType::Pinocchio {
            // For Pinocchio: find ContextAccounts struct in the same file as the entry point.
            // Returns None if the entry point has no associated context struct (e.g. emit_event).
            Self::find_pinocchio_context_accounts(&entrypoint_function).ok()
        } else {
            let context_name = Self::get_context_name(entrypoint_name).unwrap();
            let structs_metadata = SourceCodeMetadata::get_filtered_structs_by_program(
                Some(context_name.clone()),
                Some(StructMetadataType::ContextAccounts),
                if resolved_program_name.is_empty() {
                    None
                } else {
                    Some(&resolved_program_name)
                },
            )
            .change_context(ParserError)?;
            Some(
                structs_metadata
                    .iter()
                    .find(|struct_metadata| struct_metadata.name == context_name)
                    .ok_or(ParserError)
                    .into_report()
                    .attach_printable(format!(
                        "Error context_accounts struct by name {} for entrypoint_name: {}",
                        context_name, entrypoint_name
                    ))?
                    .clone(),
            )
        };

        let ep_metadata = EntrypointMetadata::new(
            entrypoint_name.to_string(),
            context_accounts
                .as_ref()
                .map(|ca| ca.metadata_id.clone())
                .unwrap_or_default(),
            entrypoint_function.metadata_id.clone(),
            BatMetadata::create_metadata_id(),
            resolved_program_name.clone(),
        );

        ep_metadata
            .update_metadata_file()
            .change_context(ParserError)?;

        // Compute dependencies for the entrypoint function
        let _ = FunctionParser::new_from_metadata(entrypoint_function.clone());
        let bat_metadata = BatMetadata::read_metadata().change_context(ParserError)?;
        let dependencies =
            Self::resolve_all_dependencies(&entrypoint_function.metadata_id, &bat_metadata);

        Ok(Self {
            name: entrypoint_name.to_string(),
            program_name: resolved_program_name,
            dependencies,
            context_accounts,
            entry_point_function: entrypoint_function,
        })
    }

    fn resolve_all_dependencies(
        entrypoint_function_id: &str,
        bat_metadata: &BatMetadata,
    ) -> Vec<FunctionSourceCodeMetadata> {
        let mut visited = HashSet::new();
        let mut result = vec![];
        Self::collect_deps(
            entrypoint_function_id,
            bat_metadata,
            &mut visited,
            &mut result,
        );
        result
    }

    fn collect_deps(
        function_id: &str,
        bat_metadata: &BatMetadata,
        visited: &mut HashSet<String>,
        result: &mut Vec<FunctionSourceCodeMetadata>,
    ) {
        if !visited.insert(function_id.to_string()) {
            return;
        }
        if let Ok(dep_meta) = bat_metadata
            .get_functions_dependencies_metadata_by_function_metadata_id(function_id.to_string())
        {
            for dep in &dep_meta.dependencies {
                if let Ok(func) = bat_metadata
                    .source_code
                    .get_function_by_id(dep.function_metadata_id.clone())
                {
                    result.push(func);
                    Self::collect_deps(&dep.function_metadata_id, bat_metadata, visited, result);
                }
            }
        }
    }

    pub fn get_entrypoint_names_from_program_lib(sorted: bool) -> Result<Vec<String>, ParserError> {
        Self::get_entrypoint_names_filtered(sorted, None)
    }

    pub fn get_entrypoint_names_filtered(
        sorted: bool,
        program_lib_path: Option<&str>,
    ) -> Result<Vec<String>, ParserError> {
        let config = BatConfig::get_config().change_context(ParserError)?;

        // For Pinocchio (and other non-Anchor), entry points are already classified
        // per-file by syn_struct_classifier. Read them from BatMetadata.
        if config.project_type == ProjectType::Pinocchio {
            let bat_metadata = BatMetadata::read_metadata().change_context(ParserError)?;
            let mut entrypoints_names: Vec<String> = bat_metadata
                .source_code
                .functions_source_code
                .iter()
                .filter(|f| {
                    f.function_type == FunctionMetadataType::EntryPoint
                        && program_lib_path.is_none_or(|lib_path| {
                            // Match by program: derive program name from lib_path
                            let pn = lib_path
                                .trim_end_matches("/src/lib.rs")
                                .trim_end_matches("/src/main.rs")
                                .split('/')
                                .next_back()
                                .unwrap_or("");
                            f.program_name.is_empty() || f.program_name == pn
                        })
                })
                .map(|f| f.name.clone())
                .collect();
            if sorted {
                entrypoints_names.sort();
            }
            return Ok(entrypoints_names);
        }

        let lib_paths = match program_lib_path {
            Some(path) => vec![path.to_string()],
            None => {
                if config.program_lib_paths.is_empty() {
                    vec![config.program_lib_path.clone()]
                } else {
                    config.program_lib_paths.clone()
                }
            }
        };

        let mut entrypoints_names: Vec<String> = Vec::new();
        for lib_path in &lib_paths {
            let classification = syn_struct_classifier::classify_file_from_path(lib_path);
            if !classification.entrypoint_function_names.is_empty() {
                entrypoints_names.extend(classification.entrypoint_function_names);
            } else {
                // Fallback to BatSonar if syn parsing fails
                let bat_sonar =
                    BatSonar::new_from_path(lib_path, Some("#[program"), SonarResultType::Function);
                entrypoints_names.extend(bat_sonar.results.iter().map(|ep| ep.name.clone()));
            }
        }
        if sorted {
            entrypoints_names.sort();
        }
        Ok(entrypoints_names)
    }

    /// For Pinocchio entry points: find the ContextAccounts struct used by the process function.
    /// Strategy:
    /// 1. Look for a ContextAccounts struct in the same file
    /// 2. If not found, parse the process function body for `SomeStruct::try_from(accounts)`
    ///    and find that struct in all ContextAccounts metadata
    fn find_pinocchio_context_accounts(
        entrypoint_function: &FunctionSourceCodeMetadata,
    ) -> Result<StructSourceCodeMetadata, ParserError> {
        let ep_path = &entrypoint_function.path;
        let all_ca = SourceCodeMetadata::get_filtered_structs(
            None,
            Some(StructMetadataType::ContextAccounts),
        )
        .change_context(ParserError)?;

        // Strategy 1: same file
        let same_file_ca: Vec<_> = all_ca.iter().filter(|s| s.path == *ep_path).collect();
        if let Some(ca) = same_file_ca.into_iter().next() {
            return Ok(ca.clone());
        }

        // Strategy 2: parse function body for `SomeStruct::try_from`
        let file_content = fs::read_to_string(ep_path).unwrap_or_default();
        if let Some(ca_name) = Self::extract_try_from_struct_name(&file_content) {
            if let Some(ca) = all_ca.into_iter().find(|s| s.name == ca_name) {
                return Ok(ca);
            }
        }

        Err(Report::new(ParserError).attach_printable(format!(
            "No ContextAccounts struct found for Pinocchio entry point {}",
            entrypoint_function.name
        )))
    }

    /// Extracts the struct name from `SomeStruct::try_from(accounts)` in a file.
    fn extract_try_from_struct_name(file_content: &str) -> Option<String> {
        // Look for pattern: SomeIdentifier::try_from(accounts
        // This is simpler and more reliable than full syn parsing of function bodies
        for line in file_content.lines() {
            let trimmed = line.trim();
            if let Some(pos) = trimmed.find("::try_from(accounts") {
                // Extract the identifier before ::try_from
                let before = &trimmed[..pos];
                // Get the last token (could be preceded by `let accounts =` etc.)
                let name = before
                    .split_whitespace()
                    .last()
                    .unwrap_or("")
                    .trim_start_matches('(');
                if !name.is_empty() && name.chars().next().unwrap().is_uppercase() {
                    return Some(name.to_string());
                }
            }
        }
        None
    }

    pub fn get_all_contexts_names() -> Vec<String> {
        let entrypoints_names = Self::get_entrypoint_names_from_program_lib(false).unwrap();

        entrypoints_names
            .into_iter()
            .map(|ep_name| Self::get_context_name(&ep_name).unwrap())
            .collect::<Vec<_>>()
    }

    pub fn get_context_name(entrypoint_name: &str) -> Result<String, ParserError> {
        let config = BatConfig::get_config().change_context(ParserError)?;
        let lib_paths = if config.program_lib_paths.is_empty() {
            vec![config.program_lib_path.clone()]
        } else {
            config.program_lib_paths.clone()
        };
        let clean_name = entrypoint_name.replace(".md", "");

        // Try syn-based extraction first
        for lib_path in &lib_paths {
            let content = fs::read_to_string(lib_path).unwrap_or_default();
            if let Some(ctx_type) =
                syn_struct_classifier::get_context_type_for_entrypoint(&content, &clean_name)
            {
                return Ok(ctx_type);
            }
        }

        // Fallback: string matching
        let mut lib_file = String::new();
        for lib_path in &lib_paths {
            let content = fs::read_to_string(lib_path).unwrap_or_default();
            if content.lines().any(|line| {
                if line.contains("pub fn") {
                    let function_name = line
                        .split('(')
                        .next()
                        .unwrap()
                        .split('<')
                        .next()
                        .unwrap()
                        .split_whitespace()
                        .last()
                        .unwrap();
                    function_name == clean_name
                } else {
                    false
                }
            }) {
                lib_file = content;
                break;
            }
        }
        if lib_file.is_empty() {
            lib_file = fs::read_to_string(&lib_paths[0]).unwrap();
        }
        let lib_file_lines: Vec<&str> = lib_file.lines().collect();
        let entrypoint_index = lib_file
            .lines()
            .position(|line| {
                if line.contains("pub fn") {
                    let function_name = line
                        .split('(')
                        .next()
                        .unwrap()
                        .split('<')
                        .next()
                        .unwrap()
                        .split_whitespace()
                        .last()
                        .unwrap();
                    function_name == clean_name
                } else {
                    false
                }
            })
            .unwrap();
        let canditate_lines = [
            lib_file_lines[entrypoint_index],
            lib_file_lines[entrypoint_index + 1],
        ];
        let context_line = if canditate_lines[0].contains("Context<") {
            canditate_lines[0]
        } else {
            canditate_lines[1]
        };
        let parsed_context_name = context_line
            .replace("'_, ", "")
            .replace("'info, ", "")
            .replace("<'info>", "")
            .split("Context<")
            .map(|l| l.to_string())
            .collect::<Vec<String>>()[1]
            .split('>')
            .map(|l| l.to_string())
            .collect::<Vec<String>>()[0]
            .clone();
        Ok(parsed_context_name)
    }
}