From 5b1f748815323eab3cf429684201e4c9922a079b Mon Sep 17 00:00:00 2001 From: anishnaik Date: Wed, 15 Jan 2025 13:50:50 -0500 Subject: [PATCH] Introduce --use-slither-force flag (#542) * introduce new flag and allow for cache to be overwritten * Fix prettier --- cmd/fuzz_flags.go | 21 ++++++++++++++++++--- compilation/types/slither.go | 10 +++++++--- docs/src/cli/fuzz.md | 25 ++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/cmd/fuzz_flags.go b/cmd/fuzz_flags.go index 7a07360d..149beb96 100644 --- a/cmd/fuzz_flags.go +++ b/cmd/fuzz_flags.go @@ -69,8 +69,11 @@ func addFuzzFlags() error { // Exploration mode fuzzCmd.Flags().Bool("explore", false, "enables exploration mode") - // Run slither on-the-fly - fuzzCmd.Flags().Bool("use-slither", false, "runs slither") + // Run slither while still trying to use the cache + fuzzCmd.Flags().Bool("use-slither", false, "runs slither and use the current cached results") + + // Run slither and overwrite the cache + fuzzCmd.Flags().Bool("use-slither-force", false, "runs slither and overwrite the cached results") return nil } @@ -196,7 +199,7 @@ func updateProjectConfigWithFuzzFlags(cmd *cobra.Command, projectConfig *config. } } - // Update configuration to run slither + // Update configuration to run slither while using current cache if cmd.Flags().Changed("use-slither") { useSlither, err := cmd.Flags().GetBool("use-slither") if err != nil { @@ -207,5 +210,17 @@ func updateProjectConfigWithFuzzFlags(cmd *cobra.Command, projectConfig *config. } } + // Update configuration to run slither and overwrite the current cache + if cmd.Flags().Changed("use-slither-force") { + useSlitherForce, err := cmd.Flags().GetBool("use-slither-force") + if err != nil { + return err + } + if useSlitherForce { + projectConfig.Slither.UseSlither = true + projectConfig.Slither.OverwriteCache = true + } + } + return nil } diff --git a/compilation/types/slither.go b/compilation/types/slither.go index 3a31d9ff..6995a17e 100644 --- a/compilation/types/slither.go +++ b/compilation/types/slither.go @@ -16,14 +16,18 @@ type SlitherConfig struct { UseSlither bool `json:"useSlither"` // CachePath determines the path where the slither cache file will be located CachePath string `json:"cachePath"` + // OverwriteCache determines whether to overwrite the cache or not + // We will not serialize this value since it is something we want to control internally + OverwriteCache bool `json:"-"` } // NewDefaultSlitherConfig provides a default configuration to run slither. The default configuration enables the // running of slither with the use of a cache. func NewDefaultSlitherConfig() (*SlitherConfig, error) { return &SlitherConfig{ - UseSlither: true, - CachePath: "slither_results.json", + UseSlither: true, + CachePath: "slither_results.json", + OverwriteCache: false, }, nil } @@ -53,7 +57,7 @@ func (s *SlitherConfig) RunSlither(target string) (*SlitherResults, error) { var haveCachedResults bool var out []byte var err error - if s.CachePath != "" { + if s.CachePath != "" && !s.OverwriteCache { // Check to see if the file exists in the first place. // If not, we will re-run slither if _, err = os.Stat(s.CachePath); os.IsNotExist(err) { diff --git a/docs/src/cli/fuzz.md b/docs/src/cli/fuzz.md index 6430b3ed..650d81ac 100644 --- a/docs/src/cli/fuzz.md +++ b/docs/src/cli/fuzz.md @@ -109,6 +109,28 @@ The `--deployer` flag allows you to update `medusa`'s contract deployer (equival medusa fuzz --deployer "0x40000" ``` +### `--use-slither` + +The `--use-slither` flag allows you to run Slither on the codebase to extract valuable constants for mutation testing. +Equivalent to [`slither.useSlither`](../project_configuration/slither_config.md#useslither). Note +that if there are cached results (via [`slither.CachePath`](../project_configuration/slither_config.md#cachepath)) then +the cache will be used. + +```shell +# Run slither and attempt to use cache, if available +medusa fuzz --use-slither +``` + +### `--use-slither-force` + +The `--use-slither-force` flag is similar to `--use-slither` except the cache at `slither.CachePath` will be +overwritten. + +```shell +# Run slither and overwrite the cache +medusa fuzz --use-slither-force +``` + ### `--fail-fast` The `--fail-fast` flag enables fast failure (equivalent to @@ -142,7 +164,8 @@ medusa fuzz --no-color ### `--explore` -The `--explore` flag enables exploration mode. This sets the [`StopOnFailedTest`](../project_configuration/testing_config.md#stoponfailedtest) and [`StopOnNoTests`](../project_configuration/testing_config.md#stoponnotests) fields to `false` and turns off assertion, property, and optimization testing. +The `--explore` flag enables exploration mode. This sets the [`StopOnFailedTest`](../project_configuration/testing_config.md#stoponfailedtest) and [`StopOnNoTests`](../project_configuration/testing_config.md#stoponnotests) +fields to `false` and turns off assertion, property, and optimization testing. ```shell # Enable exploration mode