|
| 1 | +package types |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "github.com/crytic/medusa/logging" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// SlitherConfig determines whether to run slither and whether and where to cache the results from slither |
| 13 | +type SlitherConfig struct { |
| 14 | + // UseSlither determines whether to use slither. If CachePath is non-empty, then the cached results will be |
| 15 | + // attempted to be used. Otherwise, slither will be run. |
| 16 | + UseSlither bool `json:"useSlither"` |
| 17 | + // CachePath determines the path where the slither cache file will be located |
| 18 | + CachePath string `json:"cachePath"` |
| 19 | +} |
| 20 | + |
| 21 | +// NewDefaultSlitherConfig provides a default configuration to run slither. The default configuration enables the |
| 22 | +// running of slither with the use of a cache. |
| 23 | +func NewDefaultSlitherConfig() (*SlitherConfig, error) { |
| 24 | + return &SlitherConfig{ |
| 25 | + UseSlither: true, |
| 26 | + CachePath: "slither_results.json", |
| 27 | + }, nil |
| 28 | +} |
| 29 | + |
| 30 | +// SlitherResults describes a data structures that holds the interesting constants returned from slither |
| 31 | +type SlitherResults struct { |
| 32 | + // Constants holds the constants extracted by slither |
| 33 | + Constants []Constant `json:"constantsUsed"` |
| 34 | +} |
| 35 | + |
| 36 | +// Constant defines a constant that was extracted by slither while parsing the compilation target |
| 37 | +type Constant struct { |
| 38 | + // Type represents the ABI type of the constant |
| 39 | + Type string `json:"type"` |
| 40 | + // Value represents the value of the constant |
| 41 | + Value string `json:"value"` |
| 42 | +} |
| 43 | + |
| 44 | +// RunSlither on the provided compilation target. RunSlither will use cached results if they exist and write to the |
| 45 | +// cache if we have not written to the cache already. A SlitherResults data structure is returned. |
| 46 | +func (s *SlitherConfig) RunSlither(target string) (*SlitherResults, error) { |
| 47 | + // Return early if we do not want to run slither |
| 48 | + if !s.UseSlither { |
| 49 | + return nil, nil |
| 50 | + } |
| 51 | + |
| 52 | + // Use the cached slither output if it exists |
| 53 | + var haveCachedResults bool |
| 54 | + var out []byte |
| 55 | + var err error |
| 56 | + if s.CachePath != "" { |
| 57 | + // Check to see if the file exists in the first place. |
| 58 | + // If not, we will re-run slither |
| 59 | + if _, err = os.Stat(s.CachePath); os.IsNotExist(err) { |
| 60 | + logging.GlobalLogger.Info("No Slither cached results found at ", s.CachePath) |
| 61 | + haveCachedResults = false |
| 62 | + } else { |
| 63 | + // We found the cached file |
| 64 | + if out, err = os.ReadFile(s.CachePath); err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + haveCachedResults = true |
| 68 | + logging.GlobalLogger.Info("Using cached Slither results found at ", s.CachePath) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Run slither if we do not have cached results, or we cannot find the cached results |
| 73 | + if !haveCachedResults { |
| 74 | + // Log the command |
| 75 | + cmd := exec.Command("slither", target, "--ignore-compile", "--print", "echidna", "--json", "-") |
| 76 | + logging.GlobalLogger.Info("Running Slither:\n", cmd.String()) |
| 77 | + |
| 78 | + // Run slither |
| 79 | + start := time.Now() |
| 80 | + out, err = cmd.CombinedOutput() |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + logging.GlobalLogger.Info("Finished running Slither in ", time.Since(start).Round(time.Second)) |
| 85 | + } |
| 86 | + |
| 87 | + // Capture the slither results |
| 88 | + var slitherResults SlitherResults |
| 89 | + err = json.Unmarshal(out, &slitherResults) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + // Cache the results if we have not cached before. We have also already checked that the output is well-formed |
| 95 | + // (through unmarshal) so we should be safe. |
| 96 | + if !haveCachedResults && s.CachePath != "" { |
| 97 | + // Cache the data |
| 98 | + err = os.WriteFile(s.CachePath, out, 0644) |
| 99 | + if err != nil { |
| 100 | + // If we are unable to write to the cache, we should log the error but continue |
| 101 | + logging.GlobalLogger.Warn("Failed to cache Slither results at ", s.CachePath, " due to an error:", err) |
| 102 | + // It is possible for os.WriteFile to create a partially written file so it is best to try to delete it |
| 103 | + if _, err = os.Stat(s.CachePath); err == nil { |
| 104 | + // We will not handle the error of os.Remove since we have already checked for the file's existence |
| 105 | + // and we have the right permissions. |
| 106 | + os.Remove(s.CachePath) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return &slitherResults, nil |
| 112 | +} |
| 113 | + |
| 114 | +// UnmarshalJSON unmarshals the slither output into a Slither type |
| 115 | +func (s *SlitherResults) UnmarshalJSON(d []byte) error { |
| 116 | + // Extract the top-level JSON object |
| 117 | + var obj map[string]json.RawMessage |
| 118 | + if err := json.Unmarshal(d, &obj); err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + // Decode success and error. They are always present in the slither output |
| 123 | + var success bool |
| 124 | + var slitherError string |
| 125 | + if err := json.Unmarshal(obj["success"], &success); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + if err := json.Unmarshal(obj["error"], &slitherError); err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + // If success is not true or there is a non-empty error string, return early |
| 134 | + if !success || slitherError != "" { |
| 135 | + if slitherError != "" { |
| 136 | + return errors.New(slitherError) |
| 137 | + } |
| 138 | + return errors.New("slither returned a failure during parsing") |
| 139 | + } |
| 140 | + |
| 141 | + // Now we will extract the constants |
| 142 | + s.Constants = make([]Constant, 0) |
| 143 | + |
| 144 | + // Iterate through the JSON object until we get to the constants_used key |
| 145 | + // First, retrieve the results |
| 146 | + var results map[string]json.RawMessage |
| 147 | + if err := json.Unmarshal(obj["results"], &results); err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + // Retrieve the printers data |
| 152 | + var printers []json.RawMessage |
| 153 | + if err := json.Unmarshal(results["printers"], &printers); err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + // Since we are running the echidna printer, we know that the first element is the one we care about |
| 158 | + var echidnaPrinter map[string]json.RawMessage |
| 159 | + if err := json.Unmarshal(printers[0], &echidnaPrinter); err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + // We need to de-serialize the description in two separate steps because go is dumb sometimes |
| 164 | + var descriptionString string |
| 165 | + if err := json.Unmarshal(echidnaPrinter["description"], &descriptionString); err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + var description map[string]json.RawMessage |
| 169 | + if err := json.Unmarshal([]byte(descriptionString), &description); err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + |
| 173 | + // Capture all the constants extracted across all the contracts in scope |
| 174 | + var constantsInContracts map[string]json.RawMessage |
| 175 | + if err := json.Unmarshal(description["constants_used"], &constantsInContracts); err != nil { |
| 176 | + return err |
| 177 | + } |
| 178 | + |
| 179 | + // Iterate across the constants in each contract |
| 180 | + for _, constantsInContract := range constantsInContracts { |
| 181 | + // Capture all the constants in a given function |
| 182 | + var constantsInFunctions map[string]json.RawMessage |
| 183 | + if err := json.Unmarshal(constantsInContract, &constantsInFunctions); err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + |
| 187 | + // Iterate across each function |
| 188 | + for _, constantsInFunction := range constantsInFunctions { |
| 189 | + // Each constant is provided as its own list, so we need to create a matrix |
| 190 | + var constants [][]Constant |
| 191 | + if err := json.Unmarshal(constantsInFunction, &constants); err != nil { |
| 192 | + return err |
| 193 | + } |
| 194 | + for _, constant := range constants { |
| 195 | + // Slither outputs the value of a constant as a list |
| 196 | + // However we know there can be only 1 so we take index 0 |
| 197 | + s.Constants = append(s.Constants, constant[0]) |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return nil |
| 203 | +} |
0 commit comments