Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit code #1278

Merged
merged 11 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions scripts/ci_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ test_slither(){
expected="$DIR/../tests/expected_json/$(basename "$1" .sol).$2.json"

# run slither detector on input file and save output as json
slither "$1" --solc-disable-warnings --detect "$2" --json "$DIR/tmp-test.json"
if [ $? -eq 255 ]
if ! slither "$1" --solc-disable-warnings --detect "$2" --json "$DIR/tmp-test.json";
then
echo "Slither crashed"
exit 255
Expand All @@ -40,8 +39,7 @@ test_slither(){
fi

# run slither detector on input file and save output as json
slither "$1" --solc-disable-warnings --detect "$2" --legacy-ast --json "$DIR/tmp-test.json"
if [ $? -eq 255 ]
if ! slither "$1" --solc-disable-warnings --detect "$2" --legacy-ast --json "$DIR/tmp-test.json";
then
echo "Slither crashed"
exit 255
Expand Down
6 changes: 3 additions & 3 deletions scripts/ci_test_cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

solc-select use 0.7.0

if ! slither "tests/config/test.sol" --solc-ast --ignore-return-value; then
if ! slither "tests/config/test.sol" --solc-ast; then
echo "--solc-ast failed"
exit 1
fi

if ! slither "tests/config/test.sol" --solc-disable-warnings --ignore-return-value; then
if ! slither "tests/config/test.sol" --solc-disable-warnings; then
echo "--solc-disable-warnings failed"
exit 1
fi

if ! slither "tests/config/test.sol" --disable-color --ignore-return-value; then
if ! slither "tests/config/test.sol" --disable-color; then
echo "--disable-color failed"
exit 1
fi
8 changes: 4 additions & 4 deletions scripts/ci_test_dapp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ nix-env -f "$HOME/.dapp/dapptools" -iA dapp seth solc hevm ethsign

dapp init

if slither . --detect external-function; then
exit 0
if ! slither . --detect external-function; then
echo "Dapp test failed"
exit 1
fi

echo "Dapp test failed"
exit 255
exit 0
11 changes: 4 additions & 7 deletions scripts/ci_test_embark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ npm install -g [email protected]
embark demo
cd embark_demo || exit 255
npm install
slither . --embark-overwrite-config

if [ $? -eq 4 ]
then
exit 0
if ! slither . --embark-overwrite-config; then
echo "Embark test failed"
exit 255
fi

echo "Embark test failed"
exit 255

exit 0
10 changes: 4 additions & 6 deletions scripts/ci_test_etherlime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ nvm use 10.17.0

npm i -g etherlime
etherlime init
slither .

if [ $? -eq 7 ]
then
exit 0
if ! slither .; then
echo "Etherlime test failed"
exit 1
fi

echo "Etherlime test failed"
exit 255
exit 0
16 changes: 6 additions & 10 deletions scripts/ci_test_etherscan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
mkdir etherscan
cd etherscan || exit 255

slither 0x7F37f78cBD74481E593F9C737776F7113d76B315 --etherscan-apikey "$GITHUB_ETHERSCAN"

if [ $? -ne 5 ]
then
if ! slither 0x7F37f78cBD74481E593F9C737776F7113d76B315 --etherscan-apikey "$GITHUB_ETHERSCAN"; then
echo "Etherscan test failed"
exit 255
exit 1
fi

slither rinkeby:0xFe05820C5A92D9bc906D4A46F662dbeba794d3b7 --etherscan-apikey "$GITHUB_ETHERSCAN"

if [ $? -ne 70 ]
then
if ! slither rinkeby:0xFe05820C5A92D9bc906D4A46F662dbeba794d3b7 --etherscan-apikey "$GITHUB_ETHERSCAN"; then
echo "Etherscan test failed"
exit 255
exit 1
fi

exit 0

2 changes: 1 addition & 1 deletion scripts/ci_test_find_paths.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

### Test slither-check-upgradability
### Test slither-check-upgradeability

DIR_TESTS="tests/possible_paths"

Expand Down
10 changes: 4 additions & 6 deletions scripts/ci_test_truffle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ nvm use --lts

npm install -g truffle
truffle unbox metacoin
slither .

if [ $? -eq 3 ]
then
exit 0
if ! slither .; then
echo "Truffle test failed"
exit 1
fi

echo "Truffle test failed"
exit 255
exit 0
125 changes: 84 additions & 41 deletions slither/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pstats
import sys
import traceback
from typing import Optional
from typing import Tuple, Optional, List, Dict

from pkg_resources import iter_entry_points, require

Expand Down Expand Up @@ -54,7 +54,12 @@
###################################################################################


def process_single(target, args, detector_classes, printer_classes):
def process_single(
target: str,
args: argparse.Namespace,
detector_classes: List[AbstractDetector],
printer_classes: List[AbstractPrinter],
) -> Tuple[Slither, List[Dict], List[Dict], int]:
"""
The core high-level code for running Slither static analysis.

Expand All @@ -72,7 +77,12 @@ def process_single(target, args, detector_classes, printer_classes):
return _process(slither, detector_classes, printer_classes)


def process_all(target, args, detector_classes, printer_classes):
def process_all(
target: str,
args: argparse.Namespace,
detector_classes: List[AbstractDetector],
printer_classes: List[AbstractPrinter],
) -> Tuple[List[Slither], List[Dict], List[Dict], int]:
compilations = compile_all(target, **vars(args))
slither_instances = []
results_detectors = []
Expand All @@ -97,7 +107,11 @@ def process_all(target, args, detector_classes, printer_classes):
)


def _process(slither, detector_classes, printer_classes):
def _process(
slither: Slither,
detector_classes: List[AbstractDetector],
printer_classes: List[AbstractPrinter],
) -> Tuple[Slither, List[Dict], List[Dict], int]:
for detector_cls in detector_classes:
slither.register_detector(detector_cls)

Expand All @@ -123,7 +137,12 @@ def _process(slither, detector_classes, printer_classes):
return slither, results_detectors, results_printers, analyzed_contracts_count


def process_from_asts(filenames, args, detector_classes, printer_classes):
def process_from_asts(
filenames: List[str],
args: argparse.Namespace,
detector_classes: List[AbstractDetector],
printer_classes: List[AbstractPrinter],
):
all_contracts = []

for filename in filenames:
Expand All @@ -137,29 +156,13 @@ def process_from_asts(filenames, args, detector_classes, printer_classes):
# endregion
###################################################################################
###################################################################################
# region Exit
###################################################################################
###################################################################################


def my_exit(results):
if not results:
sys.exit(0)
sys.exit(len(results))


# endregion
###################################################################################
###################################################################################
# region Detectors and printers
###################################################################################
###################################################################################


def get_detectors_and_printers():
"""
NOTE: This contains just a few detectors and printers that we made public.
"""

detectors = [getattr(all_detectors, name) for name in dir(all_detectors)]
detectors = [d for d in detectors if inspect.isclass(d) and issubclass(d, AbstractDetector)]
Expand Down Expand Up @@ -190,7 +193,9 @@ def get_detectors_and_printers():


# pylint: disable=too-many-branches
def choose_detectors(args, all_detector_classes):
def choose_detectors(
args: argparse.Namespace, all_detector_classes: List[AbstractDetector]
) -> List[AbstractDetector]:
# If detectors are specified, run only these ones

detectors_to_run = []
Expand All @@ -212,22 +217,22 @@ def choose_detectors(args, all_detector_classes):
detectors_to_run = sorted(detectors_to_run, key=lambda x: x.IMPACT)
return detectors_to_run

if args.exclude_optimization:
if args.exclude_optimization and not args.fail_pedantic:
detectors_to_run = [
d for d in detectors_to_run if d.IMPACT != DetectorClassification.OPTIMIZATION
]

if args.exclude_informational:
if args.exclude_informational and not args.fail_pedantic:
detectors_to_run = [
d for d in detectors_to_run if d.IMPACT != DetectorClassification.INFORMATIONAL
]
if args.exclude_low:
if args.exclude_low and not args.fail_low:
detectors_to_run = [d for d in detectors_to_run if d.IMPACT != DetectorClassification.LOW]
if args.exclude_medium:
if args.exclude_medium and not args.fail_medium:
detectors_to_run = [
d for d in detectors_to_run if d.IMPACT != DetectorClassification.MEDIUM
]
if args.exclude_high:
if args.exclude_high and not args.fail_high:
detectors_to_run = [d for d in detectors_to_run if d.IMPACT != DetectorClassification.HIGH]
if args.detectors_to_exclude:
detectors_to_run = [
Expand All @@ -239,7 +244,9 @@ def choose_detectors(args, all_detector_classes):
return detectors_to_run


def choose_printers(args, all_printer_classes):
def choose_printers(
args: argparse.Namespace, all_printer_classes: List[AbstractPrinter]
) -> List[AbstractPrinter]:
printers_to_run = []

# disable default printer
Expand Down Expand Up @@ -388,6 +395,34 @@ def parse_args(detector_classes, printer_classes): # pylint: disable=too-many-s
default=defaults_flag_in_config["exclude_high"],
)

group_detector.add_argument(
"--fail-pedantic",
help="Fail if any finding is detected",
action="store_true",
default=defaults_flag_in_config["fail_pedantic"],
)

group_detector.add_argument(
"--fail-low",
help="Fail if low or greater impact finding is detected",
action="store_true",
default=defaults_flag_in_config["fail_low"],
)

group_detector.add_argument(
"--fail-medium",
help="Fail if medium or greater impact finding is detected",
action="store_true",
default=defaults_flag_in_config["fail_medium"],
)

group_detector.add_argument(
"--fail-high",
help="Fail if high impact finding is detected",
action="store_true",
default=defaults_flag_in_config["fail_high"],
)

group_detector.add_argument(
"--show-ignored-findings",
help="Show all the findings",
Expand Down Expand Up @@ -538,13 +573,6 @@ def parse_args(detector_classes, printer_classes): # pylint: disable=too-many-s
default=defaults_flag_in_config["skip_assembly"],
)

parser.add_argument(
"--ignore-return-value",
help=argparse.SUPPRESS,
action="store_true",
default=defaults_flag_in_config["ignore_return_value"],
)

parser.add_argument(
"--perf",
help=argparse.SUPPRESS,
Expand Down Expand Up @@ -652,7 +680,9 @@ def main():


# pylint: disable=too-many-statements,too-many-branches,too-many-locals
def main_impl(all_detector_classes, all_printer_classes):
def main_impl(
all_detector_classes: List[AbstractDetector], all_printer_classes: List[AbstractPrinter]
):
"""
:param all_detector_classes: A list of all detectors that can be included/excluded.
:param all_printer_classes: A list of all printers that can be included.
Expand Down Expand Up @@ -808,8 +838,6 @@ def main_impl(all_detector_classes, all_printer_classes):
len(detector_classes),
len(results_detectors),
)
if args.ignore_return_value:
return

except SlitherException as slither_exception:
output_error = str(slither_exception)
Expand Down Expand Up @@ -848,11 +876,26 @@ def main_impl(all_detector_classes, all_printer_classes):
stats = pstats.Stats(cp).sort_stats("cumtime")
stats.print_stats()

# Exit with the appropriate status code
if output_error:
if args.fail_high:
fail_on_detection = any(result["impact"] == "High" for result in results_detectors)
elif args.fail_medium:
fail_on_detection = any(
result["impact"] in ["Medium", "High"] for result in results_detectors
)
elif args.fail_low:
fail_on_detection = any(
result["impact"] in ["Low", "Medium", "High"] for result in results_detectors
)
elif args.fail_pedantic:
fail_on_detection = bool(results_detectors)
else:
fail_on_detection = False

# Exit with them appropriate status code
if output_error or fail_on_detection:
sys.exit(-1)
else:
my_exit(results_detectors)
sys.exit(0)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion slither/detectors/abstract_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def _detect(self) -> List[Output]:
# pylint: disable=too-many-branches
def detect(self) -> List[Dict]:
results: List[Dict] = []
# only keep valid result, and remove dupplicate
# only keep valid result, and remove duplicate
# Keep only dictionaries
for r in [output.data for output in self._detect()]:
if self.compilation_unit.core.valid_result(r) and r not in results:
Expand Down
Loading