-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.go
370 lines (310 loc) · 11.1 KB
/
compiler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package solc
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
"go.uber.org/zap"
)
// Compiler represents a Solidity compiler instance.
type Compiler struct {
ctx context.Context // The context for the compiler.
source string // The Solidity sources to compile.
solc *Solc // The solc instance.
config *CompilerConfig // The configuration for the compiler.
}
// NewCompiler creates a new Compiler instance with the given context, configuration, and source.
// It returns an error if the provided configuration, solc instance, or source is invalid.
func NewCompiler(ctx context.Context, solc *Solc, config *CompilerConfig, source string) (*Compiler, error) {
if config == nil {
return nil, fmt.Errorf("config must be provided to create new compiler")
}
if solc == nil {
return nil, fmt.Errorf("solc instance must be provided to create new compiler")
}
if source == "" {
return nil, fmt.Errorf("source code must be provided to create new compiler")
}
if config.JsonConfig == nil {
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("invalid compiler configuration: %w", err)
}
}
return &Compiler{
ctx: ctx,
source: source,
config: config,
solc: solc,
}, nil
}
// SetCompilerVersion sets the version of the solc compiler to use.
func (v *Compiler) SetCompilerVersion(version string) {
v.config.SetCompilerVersion(version)
}
// GetCompilerVersion returns the currently set version of the solc compiler.
func (v *Compiler) GetCompilerVersion() string {
return v.config.GetCompilerVersion()
}
// GetContext returns the context associated with the compiler.
func (v *Compiler) GetContext() context.Context {
return v.ctx
}
// GetSources returns the Solidity sources associated with the compiler.
func (v *Compiler) GetSources() string {
return v.source
}
// Compile compiles the Solidity sources using the configured compiler version and arguments.
// It returns the compilation results or an error if the compilation fails.
func (v *Compiler) Compile() (*CompilerResults, error) {
compilerVersion := v.GetCompilerVersion()
if compilerVersion == "" {
return nil, fmt.Errorf("no compiler version specified")
}
binaryPath, err := v.solc.GetBinary(compilerVersion)
if err != nil {
return nil, err
}
args := []string{}
sanitizedArgs, err := v.config.SanitizeArguments(v.config.Arguments)
if err != nil {
return nil, err
}
args = append(args, sanitizedArgs...)
if v.config.JsonConfig == nil {
if err := v.config.Validate(); err != nil {
return nil, err
}
}
// #nosec G204
// G204 (CWE-78): Subprocess launched with variable (Confidence: HIGH, Severity: MEDIUM)
// We did sanitization and verification of the arguments above, so we are safe to use them.
cmd := exec.Command(binaryPath, args...)
cmd.Stdin = strings.NewReader(v.source)
// Capture the output
var out bytes.Buffer
cmd.Stdout = &out
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
zap.L().Error(
"Failed to compile Solidity sources",
zap.String("version", compilerVersion),
zap.String("stdout", out.String()),
zap.String("stderr", stderr.String()),
)
var errors []CompilationError
// Parsing the error message to extract line and column information.
errorMessage := stderr.String()
errors = append(errors, CompilationError{Message: errorMessage})
// Construct the CompilerResults structure with errors and warnings.
results := &CompilerResult{
RequestedVersion: compilerVersion,
Errors: errors,
}
return &CompilerResults{Results: []*CompilerResult{results}}, err
}
if v.config.JsonConfig != nil {
return v.resultsFromJson(compilerVersion, out)
}
return v.resultsFromSimple(compilerVersion, out)
}
// resultsFromSimple parses the output from the solc compiler when the output is in a simple format.
// It extracts the compilation details such as bytecode, ABI, and any errors or warnings.
// The method returns a slice of CompilerResults or an error if the output cannot be parsed.
func (v *Compiler) resultsFromSimple(compilerVersion string, out bytes.Buffer) (*CompilerResults, error) {
// Parse the output
var compilationOutput struct {
Contracts map[string]struct {
Bin string `json:"bin"`
Abi interface{} `json:"abi"`
} `json:"contracts"`
Errors []string `json:"errors"`
Version string `json:"version"`
}
if err := json.Unmarshal(out.Bytes(), &compilationOutput); err != nil {
return nil, err
}
// Separate errors and warnings
var errors []CompilationError
for _, msg := range compilationOutput.Errors {
errors = append(errors, CompilationError{Message: msg})
}
var results []*CompilerResult
for key, output := range compilationOutput.Contracts {
isEntryContract := false
if v.config.GetEntrySourceName() != "" && key == "<stdin>:"+v.config.GetEntrySourceName() {
isEntryContract = true
}
abi, err := json.Marshal(output.Abi)
if err != nil {
return nil, err
}
results = append(results, &CompilerResult{
IsEntryContract: isEntryContract,
RequestedVersion: compilerVersion,
CompilerVersion: compilationOutput.Version,
Bytecode: output.Bin,
ABI: string(abi),
ContractName: strings.TrimLeft(key, "<stdin>:"),
Errors: errors,
})
}
return &CompilerResults{Results: results}, nil
}
// resultsFromJson parses the output from the solc compiler when the output is in a JSON format.
// It extracts detailed compilation information including bytecode, ABI, opcodes, and metadata.
// Additionally, it separates any errors and warnings from the compilation process.
// The method returns a slice of CompilerResults or an error if the output cannot be parsed.
func (v *Compiler) resultsFromJson(compilerVersion string, out bytes.Buffer) (*CompilerResults, error) {
var compilationOutput struct {
Contracts map[string]map[string]struct {
Abi interface{} `json:"abi"`
Evm struct {
Bytecode struct {
GeneratedSources []interface{} `json:"generatedSources"`
LinkReferences map[string]interface{} `json:"linkReferences"`
Object string `json:"object"`
Opcodes string `json:"opcodes"`
SourceMap string `json:"sourceMap"`
} `json:"bytecode"`
DeployedBytecode struct {
GeneratedSources []interface{} `json:"generatedSources"`
LinkReferences map[string]interface{} `json:"linkReferences"`
Object string `json:"object"`
Opcodes string `json:"opcodes"`
SourceMap string `json:"sourceMap"`
} `json:"deployedBytecode"`
} `json:"evm"`
Metadata string `json:"metadata"`
} `json:"contracts"`
Errors []CompilationError `json:"errors"`
Version string `json:"version"`
}
if err := json.Unmarshal(out.Bytes(), &compilationOutput); err != nil {
return nil, err
}
var results []*CompilerResult
for key := range compilationOutput.Contracts {
for key, output := range compilationOutput.Contracts[key] {
isEntryContract := false
if v.config.GetEntrySourceName() != "" && key == v.config.GetEntrySourceName() {
isEntryContract = true
}
abi, err := json.Marshal(output.Abi)
if err != nil {
return nil, err
}
results = append(results, &CompilerResult{
IsEntryContract: isEntryContract,
RequestedVersion: compilerVersion,
Bytecode: output.Evm.Bytecode.Object,
DeployedBytecode: output.Evm.DeployedBytecode.Object,
ABI: string(abi),
Opcodes: output.Evm.Bytecode.Opcodes,
ContractName: key,
Errors: compilationOutput.Errors,
Metadata: output.Metadata,
})
}
}
if len(compilationOutput.Errors) > 0 {
results = append(results, &CompilerResult{
RequestedVersion: compilerVersion,
Errors: compilationOutput.Errors,
})
}
return &CompilerResults{Results: results}, nil
}
type CompilationErrorSourceLocation struct {
File string `json:"file"`
Start int `json:"start"`
End int `json:"end"`
}
// CompilationError represents a compilation error.
type CompilationError struct {
Component string `json:"component"`
Formatted string `json:"formatted_message"`
Message string `json:"message"`
Severity string `json:"severity"`
Type string `json:"type"`
SourceLocation CompilationErrorSourceLocation `json:"sourceLocation"`
}
type CompilerResults struct {
Results []*CompilerResult `json:"results"`
}
func (cr *CompilerResults) GetResults() []*CompilerResult {
return cr.Results
}
func (cr *CompilerResults) GetEntryContract() *CompilerResult {
if cr == nil {
return nil
}
for _, result := range cr.Results {
if result.IsEntry() {
return result
}
}
return nil
}
// CompilerResults represents the results of a solc compilation.
type CompilerResult struct {
IsEntryContract bool `json:"is_entry_contract"`
RequestedVersion string `json:"requested_version"`
CompilerVersion string `json:"compiler_version"`
ContractName string `json:"contract_name"`
Bytecode string `json:"bytecode"`
DeployedBytecode string `json:"deployedBytecode"`
ABI string `json:"abi"`
Opcodes string `json:"opcodes"`
Metadata string `json:"metadata"`
Errors []CompilationError `json:"errors"`
}
// IsEntry returns true if the compiled contract is the entry contract.
func (v *CompilerResult) IsEntry() bool {
return v.IsEntryContract
}
// GetOpcodes returns the compiled contract's opcodes.
func (v *CompilerResult) GetOpcodes() string {
return v.Opcodes
}
// GetMetadata returns the compiled contract's metadata.
func (v *CompilerResult) GetMetadata() string {
return v.Metadata
}
// HasErrors returns true if there are compilation errors.
func (v *CompilerResult) HasErrors() bool {
if v == nil {
return false
}
return len(v.Errors) > 0
}
// GetErrors returns the compilation errors.
func (v *CompilerResult) GetErrors() []CompilationError {
return v.Errors
}
// GetABI returns the compiled contract's ABI (Application Binary Interface) in JSON format.
func (v *CompilerResult) GetABI() string {
return v.ABI
}
// GetBytecode returns the compiled contract's bytecode.
func (v *CompilerResult) GetBytecode() string {
return v.Bytecode
}
// GetDeployedBytecode returns the compiled contract's deployed bytecode.
func (v *CompilerResult) GetDeployedBytecode() string {
return v.DeployedBytecode
}
// GetContractName returns the name of the compiled contract.
func (v *CompilerResult) GetContractName() string {
return v.ContractName
}
// GetRequestedVersion returns the requested compiler version used for compilation.
func (v *CompilerResult) GetRequestedVersion() string {
return v.RequestedVersion
}
// GetCompilerVersion returns the actual compiler version used for compilation.
func (v *CompilerResult) GetCompilerVersion() string {
return v.CompilerVersion
}