Skip to content

Commit 1dfb763

Browse files
authored
fix: Cannon Profile (#43)
* fix: thread specific profile * fix: profile fixed * fix: lint
1 parent 1ce5abd commit 1dfb763

7 files changed

Lines changed: 546 additions & 151 deletions

File tree

analyzer/opcode/opcode.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,29 @@ func (op *opcode) Analyze(path string, withTrace bool) ([]*analyzer.Issue, error
3636
for _, segment := range callGraph.Segments() {
3737
for _, instruction := range segment.Instructions() {
3838
if !op.isAllowedOpcode(instruction.OpcodeHex(), instruction.Funct()) {
39-
source, err := common.TraceAsmCaller(absPath, callGraph, segment.Label(), endCondition)
39+
source, err := common.TraceAsmCaller(
40+
absPath,
41+
callGraph,
42+
segment.Label(),
43+
common.ProgramEntrypoint(op.profile.GOARCH),
44+
)
4045
if err != nil { // non-reachable portion ignored
4146
continue
4247
}
43-
if common.ShouldIgnoreSource(source, op.profile.IgnoredFunctions) {
44-
continue
45-
}
4648
if !withTrace {
4749
source.CallStack = nil
4850
}
49-
issues = append(issues, &analyzer.Issue{
51+
52+
issue := &analyzer.Issue{
5053
Severity: analyzer.IssueSeverityCritical,
5154
CallStack: source,
52-
Message: fmt.Sprintf("Incompatible Opcode Detected: Opcode: %s, Funct: %s",
55+
Message: fmt.Sprintf("Potential Incompatible Opcode Detected: Opcode: %s, Funct: %s",
5356
instruction.OpcodeHex(), instruction.Funct()),
54-
})
57+
}
58+
if common.ShouldIgnoreSource(source, op.profile.IgnoredFunctions) {
59+
issue.Severity = analyzer.IssueSeverityWarning
60+
}
61+
issues = append(issues, issue)
5562
}
5663
}
5764
}
@@ -87,7 +94,7 @@ func (op *opcode) TraceStack(path string, function string) (*analyzer.CallStack,
8794
if err != nil {
8895
return nil, err
8996
}
90-
return common.TraceAsmCaller(absPath, graph, function, endCondition)
97+
return common.TraceAsmCaller(absPath, graph, function, common.ProgramEntrypoint(op.profile.GOARCH))
9198
}
9299
func (op *opcode) isAllowedOpcode(opcode, funct string) bool {
93100
return slices.ContainsFunc(op.profile.AllowedOpcodes, func(instr profile.OpcodeInstruction) bool {
@@ -102,10 +109,3 @@ func (op *opcode) isAllowedOpcode(opcode, funct string) bool {
102109
})
103110
})
104111
}
105-
106-
func endCondition(function string) bool {
107-
return function == "runtime.rt0_go" || // start point of a go program
108-
function == "main.main" || // main
109-
strings.Contains(function, ".init.") || // all init functions
110-
strings.HasSuffix(function, ".init") // vars
111-
}

analyzer/syscall/asm_syscall.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"path/filepath"
77
"slices"
8-
"strings"
98

109
"github.com/ChainSafe/vm-compat/analyzer"
1110
"github.com/ChainSafe/vm-compat/asmparser"
@@ -59,19 +58,23 @@ func (a *asmSyscallAnalyser) Analyze(path string, withTrace bool) ([]*analyzer.I
5958
if slices.Contains(a.profile.AllowedSycalls, syscall.Number) {
6059
continue
6160
}
62-
source, err := common.TraceAsmCaller(absPath, callGraph, syscall.Segment.Label(), endCondition)
61+
source, err := common.TraceAsmCaller(
62+
absPath,
63+
callGraph,
64+
syscall.Segment.Label(),
65+
common.ProgramEntrypoint(a.profile.GOARCH),
66+
)
6367
if err != nil { // non-reachable portion ignored
6468
continue
6569
}
66-
if common.ShouldIgnoreSource(source, a.profile.IgnoredFunctions) {
67-
continue
68-
}
69-
7070
if !withTrace {
7171
source.CallStack = nil
7272
}
7373

7474
severity := analyzer.IssueSeverityCritical
75+
if common.ShouldIgnoreSource(source, a.profile.IgnoredFunctions) {
76+
severity = analyzer.IssueSeverityWarning
77+
}
7578
message := fmt.Sprintf("Potential Incompatible Syscall Detected: %d", syscall.Number)
7679
if slices.Contains(a.profile.NOOPSyscalls, syscall.Number) {
7780
message = fmt.Sprintf("Potential NOOP Syscall Detected: %d", syscall.Number)
@@ -121,12 +124,5 @@ func (a *asmSyscallAnalyser) TraceStack(path string, function string) (*analyzer
121124
if err != nil {
122125
return nil, err
123126
}
124-
return common.TraceAsmCaller(absPath, graph, function, endCondition)
125-
}
126-
127-
func endCondition(function string) bool {
128-
return function == "runtime.rt0_go" || // start point of a go program
129-
function == "main.main" || // main
130-
strings.Contains(function, ".init.") || // all init functions
131-
strings.HasSuffix(function, ".init") // vars
127+
return common.TraceAsmCaller(absPath, graph, function, common.ProgramEntrypoint(a.profile.GOARCH))
132128
}

common/entrypoint.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package common
2+
3+
import "strings"
4+
5+
func ProgramEntrypoint(arch string) func(function string) bool {
6+
switch arch {
7+
case "mips":
8+
return func(function string) bool {
9+
// Ignoring rt0_go directly as it contains unreachable portion
10+
return function == "runtime.check" ||
11+
function == "runtime.args" ||
12+
function == "runtime.osinit" ||
13+
function == "runtime.schedinit" ||
14+
function == "runtime.newproc" ||
15+
function == "runtime.mstart" ||
16+
function == "main.main" || // main
17+
strings.Contains(function, ".init.") || // all init functions
18+
strings.HasSuffix(function, ".init") // vars
19+
}
20+
case "mips64":
21+
return func(function string) bool {
22+
return function == "runtime.rt0_go" || // start point of a go program
23+
function == "main.main" || // main
24+
strings.Contains(function, ".init.") || // all init functions
25+
strings.HasSuffix(function, ".init") // vars
26+
}
27+
}
28+
return func(function string) bool {
29+
return false
30+
}
31+
}

profile/cannon/cannon-32.yaml

Lines changed: 0 additions & 120 deletions
This file was deleted.

profile/cannon/cannon-multithreaded-32.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
vm: Cannon
22
goos: linux
3-
goarch: mips64
3+
goarch: mips
44
ignored_functions:
55
- 'syscall.setrlimit'
66
- 'runtime.morestack'
77
- 'runtime.abort'
8-
8+
- 'runtime.exitThread'
9+
- 'runtime.sigaltstack'
10+
- 'runtime.rtsigprocmask'
11+
- 'runtime.munmap'
12+
- 'runtime.exit'
913
allowed_opcodes:
1014
- opcode: '0x2'
1115
funct: []
@@ -157,3 +161,6 @@ noop_syscalls:
157161
- 4261
158162
- 4076
159163
- 4019
164+
- 4215
165+
- 4213
166+
- 4140
File renamed without changes.

0 commit comments

Comments
 (0)