Skip to content

Commit

Permalink
Important Fix/support ast parsing from multiple lines (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
TilakMaddy authored Jun 3, 2024
1 parent 1de263b commit 2e725ee
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 50 deletions.
129 changes: 85 additions & 44 deletions aderyn_driver/src/process_auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,56 +116,97 @@ fn create_workspace_context_from_stdout(
let mut context = WorkspaceContext::default();
// dbg!(&stdout)

let mut pick_next_line = false;
// let mut pick_next_line = false;
let mut src_filepaths = vec![];

for line in stdout.lines() {
if line.starts_with("======= ") {
let end_marker = line.find(" =======").unwrap();
let filepath = &PathBuf::from(&line["======= ".len()..end_marker]);
if passes_scope(
scope,
utils::canonicalize(root_path.join(filepath))
.unwrap()
.as_path(),
absolute_root_path_str,
) && passes_exclude(
exclude,
utils::canonicalize(root_path.join(filepath))
.unwrap()
.as_path(),
absolute_root_path_str,
) && passes_src(
src,
utils::canonicalize(root_path.join(filepath))
.unwrap()
.as_path(),
) {
src_filepaths.push(filepath.to_string_lossy().to_string());
pick_next_line = true;
let lines = stdout.lines().collect::<Vec<_>>();

let mut idx = 0;
let mut keep_picking = false;
let mut ast_content = String::new();

while idx < lines.len() {
let line = lines[idx];

let (separation, filename) =
is_demarcation_line(line, scope, exclude, root_path, src, absolute_root_path_str);

if separation {
if !ast_content.is_empty() {
absorb_ast_content_into_context(&ast_content, root_path, &mut context);
}
ast_content = String::new();
keep_picking = false;

if let Some(filepath) = filename {
src_filepaths.push(filepath);
keep_picking = true;
}
} else if pick_next_line {
let ast_content = line.to_string();
let mut source_unit: SourceUnit = serde_json::from_str(&ast_content).unwrap();
let filepath = source_unit.absolute_path.as_ref().unwrap();
source_unit.source = std::fs::read_to_string(root_path.join(filepath)).ok();
// dbg!(&filepath);
source_unit.absolute_path = Some(filepath.to_string());
// dbg!(&filepath);

source_unit.accept(&mut context).unwrap_or_else(|err| {
// Exit with a non-zero exit code
eprintln!("Error loading AST into WorkspaceContext");
eprintln!("{:?}", err);
std::process::exit(1);
});

pick_next_line = false;
} else if keep_picking {
ast_content.push_str(line);
}

idx += 1;
}

if !ast_content.is_empty() {
absorb_ast_content_into_context(&ast_content, root_path, &mut context);
}

// println!("{:#?}", context);
// println!("New context !");
context.src_filepaths = src_filepaths;
context
}

fn absorb_ast_content_into_context(
ast_content: &str,
root_path: &Path,
context: &mut WorkspaceContext,
) {
let mut source_unit: SourceUnit = serde_json::from_str(ast_content).unwrap();
let filepath = source_unit.absolute_path.as_ref().unwrap();
source_unit.source = std::fs::read_to_string(root_path.join(filepath)).ok();
source_unit.absolute_path = Some(filepath.to_string());

source_unit.accept(context).unwrap_or_else(|err| {
// Exit with a non-zero exit code
eprintln!("Error loading AST into WorkspaceContext");
eprintln!("{:?}", err);
std::process::exit(1);
});
}

fn is_demarcation_line(
line: &str,
scope: &Option<Vec<String>>,
exclude: &Option<Vec<String>>,
root_path: &Path,
src: &Option<Vec<PathBuf>>,
absolute_root_path_str: &str,
) -> (bool, Option<String>) {
if line.starts_with("======= ") {
let end_marker = line.find(" =======").unwrap();
let filepath = &PathBuf::from(&line["======= ".len()..end_marker]);
if passes_scope(
scope,
utils::canonicalize(root_path.join(filepath))
.unwrap()
.as_path(),
absolute_root_path_str,
) && passes_exclude(
exclude,
utils::canonicalize(root_path.join(filepath))
.unwrap()
.as_path(),
absolute_root_path_str,
) && passes_src(
src,
utils::canonicalize(root_path.join(filepath))
.unwrap()
.as_path(),
) {
return (true, Some(filepath.to_string_lossy().to_string()));
}
return (true, None);
}
(false, None)
}
100 changes: 94 additions & 6 deletions reports/adhoc-sol-files-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ This report was generated by [Aderyn](https://github.com/Cyfrin/aderyn), a stati

| Key | Value |
| --- | --- |
| .sol Files | 8 |
| Total nSLOC | 154 |
| .sol Files | 18 |
| Total nSLOC | 206 |


## Files Details
Expand All @@ -46,7 +46,17 @@ This report was generated by [Aderyn](https://github.com/Cyfrin/aderyn), a stati
| inheritance/ExtendedInheritance.sol | 17 |
| inheritance/IContractInheritance.sol | 4 |
| inheritance/InheritanceBase.sol | 8 |
| **Total** | **154** |
| multiple-versions/0.4/A.sol | 5 |
| multiple-versions/0.4/B.sol | 5 |
| multiple-versions/0.5/A.sol | 5 |
| multiple-versions/0.5/B.sol | 7 |
| multiple-versions/0.6/A.sol | 5 |
| multiple-versions/0.6/B.sol | 5 |
| multiple-versions/0.7/A.sol | 5 |
| multiple-versions/0.7/B.sol | 5 |
| multiple-versions/0.8/A.sol | 5 |
| multiple-versions/0.8/B.sol | 5 |
| **Total** | **206** |


## Issue Summary
Expand Down Expand Up @@ -116,7 +126,7 @@ The `ecrecover` function is susceptible to signature malleability. This means th

Consider using a specific version of Solidity in your contracts instead of a wide version. For example, instead of `pragma solidity ^0.8.0;`, use `pragma solidity 0.8.0;`

<details><summary>4 Found Instances</summary>
<details><summary>14 Found Instances</summary>


- Found in Counter.sol [Line: 2](../tests/adhoc-sol-files/Counter.sol#L2)
Expand All @@ -143,6 +153,66 @@ Consider using a specific version of Solidity in your contracts instead of a wid
pragma solidity ^0.8.0;
```

- Found in multiple-versions/0.4/A.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.4/A.sol#L2)

```solidity
pragma solidity ^0.4.0;
```

- Found in multiple-versions/0.4/B.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.4/B.sol#L2)

```solidity
pragma solidity ^0.4.0;
```

- Found in multiple-versions/0.5/A.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.5/A.sol#L2)

```solidity
pragma solidity ^0.5.0;
```

- Found in multiple-versions/0.5/B.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.5/B.sol#L2)

```solidity
pragma solidity ^0.5.0;
```

- Found in multiple-versions/0.6/A.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.6/A.sol#L2)

```solidity
pragma solidity ^0.6.0;
```

- Found in multiple-versions/0.6/B.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.6/B.sol#L2)

```solidity
pragma solidity ^0.6.0;
```

- Found in multiple-versions/0.7/A.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.7/A.sol#L2)

```solidity
pragma solidity ^0.7.0;
```

- Found in multiple-versions/0.7/B.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.7/B.sol#L2)

```solidity
pragma solidity ^0.7.0;
```

- Found in multiple-versions/0.8/A.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.8/A.sol#L2)

```solidity
pragma solidity ^0.8.0;
```

- Found in multiple-versions/0.8/B.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.8/B.sol#L2)

```solidity
pragma solidity ^0.8.0;
```

</details>


Expand Down Expand Up @@ -238,7 +308,7 @@ Index event fields make the field more quickly accessible to off-chain tools tha

Solc compiler version 0.8.20 switches the default target EVM version to Shanghai, which means that the generated bytecode will include PUSH0 opcodes. Be sure to select the appropriate EVM version in case you intend to deploy on a chain other than mainnet like L2 chains that may not support PUSH0, otherwise deployment of your contracts will fail.

<details><summary>6 Found Instances</summary>
<details><summary>8 Found Instances</summary>


- Found in Counter.sol [Line: 2](../tests/adhoc-sol-files/Counter.sol#L2)
Expand Down Expand Up @@ -277,6 +347,18 @@ Solc compiler version 0.8.20 switches the default target EVM version to Shanghai
pragma solidity ^0.8.0;
```

- Found in multiple-versions/0.8/A.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.8/A.sol#L2)

```solidity
pragma solidity ^0.8.0;
```

- Found in multiple-versions/0.8/B.sol [Line: 2](../tests/adhoc-sol-files/multiple-versions/0.8/B.sol#L2)

```solidity
pragma solidity ^0.8.0;
```

</details>


Expand Down Expand Up @@ -308,7 +390,7 @@ Solc compiler version 0.8.20 switches the default target EVM version to Shanghai

Consider removing empty blocks.

<details><summary>1 Found Instances</summary>
<details><summary>2 Found Instances</summary>


- Found in OnceModifierExample.sol [Line: 10](../tests/adhoc-sol-files/OnceModifierExample.sol#L10)
Expand All @@ -317,6 +399,12 @@ Consider removing empty blocks.
function perform() external onlyOnce {
```

- Found in multiple-versions/0.5/B.sol [Line: 8](../tests/adhoc-sol-files/multiple-versions/0.5/B.sol#L8)

```solidity
function emptyBlockCatchMe() external {
```

</details>


Expand Down
7 changes: 7 additions & 0 deletions tests/adhoc-sol-files/multiple-versions/0.4/A.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.4.0;

contract Constants {
address public constant MY_ADDRESS = address(0);
uint256 public constant MY_UINT = 134131;
}
7 changes: 7 additions & 0 deletions tests/adhoc-sol-files/multiple-versions/0.4/B.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.4.0;

contract Vars {
address public MY_ADDRESS = address(0);
uint256 public MY_UINT = 134131;
}
7 changes: 7 additions & 0 deletions tests/adhoc-sol-files/multiple-versions/0.5/A.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;

contract Constants {
address public constant MY_ADDRESS = address(0);
uint256 public constant MY_UINT = 134131;
}
11 changes: 11 additions & 0 deletions tests/adhoc-sol-files/multiple-versions/0.5/B.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;

contract Vars {
address public MY_ADDRESS = address(0);
uint256 public MY_UINT = 134131;

function emptyBlockCatchMe() external {

}
}
7 changes: 7 additions & 0 deletions tests/adhoc-sol-files/multiple-versions/0.6/A.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract Constants {
address public constant MY_ADDRESS = address(0);
uint256 public constant MY_UINT = 134131;
}
7 changes: 7 additions & 0 deletions tests/adhoc-sol-files/multiple-versions/0.6/B.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract Vars {
address public MY_ADDRESS = address(0);
uint256 public MY_UINT = 134131;
}
7 changes: 7 additions & 0 deletions tests/adhoc-sol-files/multiple-versions/0.7/A.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;

contract Constants {
address public constant MY_ADDRESS = address(0);
uint256 public constant MY_UINT = 134131;
}
7 changes: 7 additions & 0 deletions tests/adhoc-sol-files/multiple-versions/0.7/B.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;

contract Vars {
address public MY_ADDRESS = address(0);
uint256 public MY_UINT = 134131;
}
7 changes: 7 additions & 0 deletions tests/adhoc-sol-files/multiple-versions/0.8/A.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Constants {
address public constant MY_ADDRESS = address(0);
uint256 public constant MY_UINT = 134131;
}
7 changes: 7 additions & 0 deletions tests/adhoc-sol-files/multiple-versions/0.8/B.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Vars {
address public MY_ADDRESS = address(0);
uint256 public MY_UINT = 134131;
}

0 comments on commit 2e725ee

Please sign in to comment.