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

Custom logger for unstructured and structured logging #171

Merged
merged 30 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8f3c1de
introduction of logging object
anishnaik Mar 29, 2023
7766a73
integrated logger into cmd and fuzzing packages
anishnaik Mar 29, 2023
7904aa8
small bug fix
anishnaik Mar 29, 2023
d15846b
small todo change
anishnaik Mar 29, 2023
5a3c839
Merge remote-tracking branch 'origin/master' into dev/multi-logger
Xenomega May 23, 2023
08ed280
add the color functions
anishnaik Jun 27, 2023
c2b32c3
update logging API
anishnaik Jun 27, 2023
75773d8
update fuzzer and execution tracer to use new logging API
anishnaik Jun 27, 2023
0072553
fix fuzzer test
anishnaik Jun 28, 2023
2bd2dab
Merge branch 'master' into dev/multi-logger
anishnaik Jun 29, 2023
1ce9415
fix cmd package logging
anishnaik Jul 5, 2023
b5511b3
update panics to use logger
anishnaik Jul 5, 2023
de1dd4a
add `String()` capabilities back
anishnaik Jul 6, 2023
cb23cca
fix log formatting and update log API
anishnaik Jul 6, 2023
f0e1cba
fix main
anishnaik Jul 6, 2023
0dd081d
minor fixes
anishnaik Jul 6, 2023
8961ddd
fix bug
anishnaik Jul 6, 2023
40705bf
bug fixes
anishnaik Jul 6, 2023
4a72ff6
update logging api
anishnaik Jul 11, 2023
347e76a
fix log messages based on new API
anishnaik Jul 11, 2023
1c402e4
add additional validation and fix comments
anishnaik Jul 11, 2023
13cac1d
Merge branch 'master' into dev/multi-logger
anishnaik Jul 11, 2023
4a9ab6e
Update execution_trace.go
anishnaik Jul 11, 2023
f9746c1
Merge branch 'master' into dev/multi-logger
anishnaik Jul 11, 2023
6a48502
fix lint
anishnaik Jul 11, 2023
79b8771
Merge branch 'dev/multi-logger' of github.com:crytic/medusa into dev/…
anishnaik Jul 11, 2023
1a88ae1
Fix for windows color formatting issues
Xenomega Jul 13, 2023
fb1284d
additional small updates
anishnaik Jul 13, 2023
75f4289
fix to uintptr
anishnaik Jul 13, 2023
8d98f3b
another windows fix
anishnaik Jul 13, 2023
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: 4 additions & 2 deletions chain/cheat_code_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package chain

import (
"encoding/binary"
"fmt"
"github.com/crytic/medusa/logging"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -100,12 +102,12 @@ func (c *CheatCodeContract) Abi() *abi.ABI {
func (c *CheatCodeContract) addMethod(name string, inputs abi.Arguments, outputs abi.Arguments, handler cheatCodeMethodHandler) {
// Verify a method name was provided
if name == "" {
panic("could not add method to precompiled cheatcode contract, empty method name provided")
logging.GlobalLogger.Panic("Failed to add method to precompile cheatcode contract", fmt.Errorf("empty method name provided"))
}

// Verify a method handler was provided
if handler == nil {
panic("could not add method to precompiled cheatcode contract, nil method handler provided")
logging.GlobalLogger.Panic("Failed to add method to precompile cheatcode contract", fmt.Errorf("nil method handler provided"))
}

// Set the method information in our method lookup
Expand Down
35 changes: 24 additions & 11 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ var supportedShells = []string{"bash", "zsh", "powershell"}

// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion <shell>",
Short: "generate the autocompletion script for medusa for the specific shell",
Long: generalComDesc,
Args: cmdValidateCompletionArgs,
RunE: cmdRunCompletion,
Use: "completion <shell>",
Short: "Generate the autocompletion script for medusa for the specific shell",
Long: generalComDesc,
Args: cmdValidateCompletionArgs,
RunE: cmdRunCompletion,
SilenceUsage: true,
SilenceErrors: true,
}

func init() {
Expand All @@ -62,12 +64,16 @@ func init() {
func cmdValidateCompletionArgs(cmd *cobra.Command, args []string) error {
// Make sure we have exactly 1 argument
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return fmt.Errorf("completion requires only 1 shell argument (options: %s)", strings.Join(supportedShells, ", "))
err = fmt.Errorf("completion requires only 1 shell argument (options: %s)", strings.Join(supportedShells, ", "))
cmdLogger.Error("Failed to validate args for completion command", err)
return err
}

// Make sure that the shell is a supported type
if contains := slices.Contains(supportedShells, args[0]); !contains {
return fmt.Errorf("%s is not a supported shell", args[0])
err := fmt.Errorf("%s is not a supported shell", args[0])
cmdLogger.Error("Failed to validate args for completion command", err)
return err
}

return nil
Expand All @@ -76,15 +82,22 @@ func cmdValidateCompletionArgs(cmd *cobra.Command, args []string) error {
// cmdRunCompletion executes the completion CLI command
func cmdRunCompletion(cmd *cobra.Command, args []string) error {
// NOTE: Please be aware that if the supported shells changes, then this switch statement must also change
var err error
switch args[0] {
case "bash":
return cmd.Root().GenBashCompletionV2(os.Stdout, true)
err = cmd.Root().GenBashCompletionV2(os.Stdout, true)
case "zsh":
return cmd.Root().GenZshCompletion(os.Stdout)
err = cmd.Root().GenZshCompletion(os.Stdout)
case "powershell":
return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
default:
// We are throwing a panic here because our validation function should have handled this and something is wrong.
panic(fmt.Errorf("%s is not a supported shell type", args[0]))
cmdLogger.Panic("Failed to run the completion command", fmt.Errorf("%s is not a supported shell type", args[0]))
}

// Log an error if we encountered one
if err != nil {
cmdLogger.Error("Failed to run the completion command", err)
}
return err
}
89 changes: 52 additions & 37 deletions cmd/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/crytic/medusa/logging/colors"
"os"
"os/signal"
"path/filepath"
Expand All @@ -14,53 +15,59 @@ import (

// fuzzCmd represents the command provider for fuzzing
var fuzzCmd = &cobra.Command{
Use: "fuzz",
Short: "Starts a fuzzing campaign",
Long: `Starts a fuzzing campaign`,
Args: cmdValidateFuzzArgs,
RunE: cmdRunFuzz,
// Run dynamic completion of nouns
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// Gather a list of flags that are available to be used in the current command but have not been used yet
var unusedFlags []string

// Examine all the flags, and add any flags that have not been set in the current command line
// to a list of unused flags
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
if !flag.Changed {
// When adding a flag to a command, include the "--" prefix to indicate that it is a flag
// and not a positional argument. Additionally, when the user presses the TAB key twice after typing
// a flag name, the "--" prefix will appear again, indicating that more flags are available and that
// none of the arguments are positional.
unusedFlags = append(unusedFlags, "--"+flag.Name)
}
})
// Provide a list of flags that can be used in the current command (but have not been used yet)
// for autocompletion suggestions
return unusedFlags, cobra.ShellCompDirectiveNoFileComp
},
}

// cmdValidateFuzzArgs makes sure that there are no positional arguments provided to the fuzz command
func cmdValidateFuzzArgs(cmd *cobra.Command, args []string) error {
// Make sure we have no positional args
if err := cobra.NoArgs(cmd, args); err != nil {
return fmt.Errorf("fuzz does not accept any positional arguments, only flags and their associated values")
}
return nil
Use: "fuzz",
Short: "Starts a fuzzing campaign",
Long: `Starts a fuzzing campaign`,
Args: cmdValidateFuzzArgs,
ValidArgsFunction: cmdValidFuzzArgs,
RunE: cmdRunFuzz,
SilenceUsage: true,
SilenceErrors: true,
}

func init() {
// Add all the flags allowed for the fuzz command
err := addFuzzFlags()
if err != nil {
panic(err)
cmdLogger.Panic("Failed to initialize the fuzz command", err)
}

// Add the fuzz command and its associated flags to the root command
rootCmd.AddCommand(fuzzCmd)
}

// cmdValidFuzzArgs will return which flags and sub-commands are valid for dynamic completion for the fuzz command
func cmdValidFuzzArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// Gather a list of flags that are available to be used in the current command but have not been used yet
var unusedFlags []string

// Examine all the flags, and add any flags that have not been set in the current command line
// to a list of unused flags
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
if !flag.Changed {
// When adding a flag to a command, include the "--" prefix to indicate that it is a flag
// and not a positional argument. Additionally, when the user presses the TAB key twice after typing
// a flag name, the "--" prefix will appear again, indicating that more flags are available and that
// none of the arguments are positional.
unusedFlags = append(unusedFlags, "--"+flag.Name)
}
})
// Provide a list of flags that can be used in the current command (but have not been used yet)
// for autocompletion suggestions
return unusedFlags, cobra.ShellCompDirectiveNoFileComp
}

// cmdValidateFuzzArgs makes sure that there are no positional arguments provided to the fuzz command
func cmdValidateFuzzArgs(cmd *cobra.Command, args []string) error {
// Make sure we have no positional args
if err := cobra.NoArgs(cmd, args); err != nil {
err = fmt.Errorf("fuzz does not accept any positional arguments, only flags and their associated values")
cmdLogger.Error("Failed to validate args to the fuzz command", err)
return err
}
return nil
}

// cmdRunFuzz executes the CLI fuzz command and navigates through the following possibilities:
// #1: We will search for either a custom config file (via --config) or the default (medusa.json).
// If we find it, read it. If we can't read it, throw an error.
Expand All @@ -73,13 +80,15 @@ func cmdRunFuzz(cmd *cobra.Command, args []string) error {
configFlagUsed := cmd.Flags().Changed("config")
configPath, err := cmd.Flags().GetString("config")
if err != nil {
cmdLogger.Error("Failed to run the fuzz command", err)
return err
}

// If --config was not used, look for `medusa.json` in the current work directory
if !configFlagUsed {
workingDirectory, err := os.Getwd()
if err != nil {
cmdLogger.Error("Failed to run the fuzz command", err)
return err
}
configPath = filepath.Join(workingDirectory, DefaultProjectConfigFilename)
Expand All @@ -91,31 +100,36 @@ func cmdRunFuzz(cmd *cobra.Command, args []string) error {
// Possibility #1: File was found
if existenceError == nil {
// Try to read the configuration file and throw an error if something goes wrong
cmdLogger.Info("Reading the configuration file at: ", colors.Bold, configPath, colors.Reset)
projectConfig, err = config.ReadProjectConfigFromFile(configPath)
if err != nil {
cmdLogger.Error("Failed to run the fuzz command", err)
return err
}
}

// Possibility #2: If the --config flag was used, and we couldn't find the file, we'll throw an error
if configFlagUsed && existenceError != nil {
cmdLogger.Error("Failed to run the fuzz command", err)
return existenceError
}

// Possibility #3: --config flag was not used and medusa.json was not found, so use the default project config
if !configFlagUsed && existenceError != nil {
fmt.Printf("unable to find the config file at %v. will use the default project configuration for the "+
"%v compilation platform instead\n", configPath, DefaultCompilationPlatform)
cmdLogger.Warn(fmt.Sprintf("Unable to find the config file at %v, will use the default project configuration for the "+
"%v compilation platform instead", configPath, DefaultCompilationPlatform))

projectConfig, err = config.GetDefaultProjectConfig(DefaultCompilationPlatform)
if err != nil {
cmdLogger.Error("Failed to run the fuzz command", err)
return err
}
}

// Update the project configuration given whatever flags were set using the CLI
err = updateProjectConfigWithFuzzFlags(cmd, projectConfig)
if err != nil {
cmdLogger.Error("Failed to run the fuzz command", err)
return err
}

Expand All @@ -125,6 +139,7 @@ func cmdRunFuzz(cmd *cobra.Command, args []string) error {
// be in the config directory when running this.
err = os.Chdir(filepath.Dir(configPath))
if err != nil {
cmdLogger.Error("Failed to run the fuzz command", err)
return err
}

Expand Down
Loading