Skip to content

Commit 450a48f

Browse files
authored
fix: added impact and referance (#35)
1 parent 92e6862 commit 450a48f

7 files changed

Lines changed: 60 additions & 44 deletions

File tree

analyzer/analyzer.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Analyzer interface {
88
Analyze(path string, withTrace bool) ([]*Issue, error)
99

1010
// TraceStack generates callstack for a function to debug
11-
TraceStack(path string, function string) (*IssueSource, error)
11+
TraceStack(path string, function string) (*CallStack, error)
1212
}
1313

1414
// IssueSeverity represents the severity level of an issue.
@@ -21,32 +21,34 @@ const (
2121

2222
// Issue represents a single issue found by the analyzer.
2323
type Issue struct {
24-
Sources *IssueSource `json:"sources"`
25-
Message string `json:"message"` // A description of the issue.
26-
Severity IssueSeverity `json:"severity"`
24+
CallStack *CallStack `json:"callStack"`
25+
Message string `json:"message"` // A description of the issue.
26+
Severity IssueSeverity `json:"severity"`
27+
Impact string `json:"impact,omitempty"`
28+
Reference string `json:"reference,omitempty"`
2729
}
2830

29-
// IssueSource represents a location in the code where the issue originates.
30-
type IssueSource struct {
31-
File string `json:"file"`
32-
Line int `json:"line"` // The line number where the issue was found.
33-
Function string `json:"function"` // The function where the issue was found.
34-
AbsPath string `json:"absPath"` // The absolute file path.
35-
CallStack *IssueSource `json:"callStack,omitempty"` // The trace of calls leading to this source.
31+
// CallStack represents a location in the code where the issue originates.
32+
type CallStack struct {
33+
File string `json:"file"`
34+
Line int `json:"line"` // The line number where the issue was found.
35+
Function string `json:"function"` // The function where the issue was found.
36+
AbsPath string `json:"absPath"` // The absolute file path.
37+
CallStack *CallStack `json:"callStack,omitempty"` // The trace of calls leading to this source.
3638
}
3739

38-
// Copy creates a deep copy of the IssueSource.
39-
func (src *IssueSource) Copy() *IssueSource {
40+
// Copy creates a deep copy of the CallStack.
41+
func (src *CallStack) Copy() *CallStack {
4042
if src == nil {
4143
return nil
4244
}
4345
// Recursively copy the CallStack
44-
var copiedCallStack *IssueSource
46+
var copiedCallStack *CallStack
4547
if src.CallStack != nil {
4648
copiedCallStack = src.CallStack.Copy()
4749
}
4850

49-
return &IssueSource{
51+
return &CallStack{
5052
File: src.File,
5153
Line: src.Line,
5254
Function: src.Function,
@@ -56,7 +58,7 @@ func (src *IssueSource) Copy() *IssueSource {
5658
}
5759

5860
// AddCallStack add a call stack to the stack et end
59-
func (src *IssueSource) AddCallStack(stack *IssueSource) {
61+
func (src *CallStack) AddCallStack(stack *CallStack) {
6062
// Recursively copy the CallStack
6163
if src.CallStack == nil {
6264
src.CallStack = stack

analyzer/opcode/opcode.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ func (op *opcode) Analyze(path string, withTrace bool) ([]*analyzer.Issue, error
4444
source.CallStack = nil
4545
}
4646
issues = append(issues, &analyzer.Issue{
47-
Severity: analyzer.IssueSeverityCritical,
48-
Sources: source,
47+
Severity: analyzer.IssueSeverityCritical,
48+
CallStack: source,
4949
Message: fmt.Sprintf("Incompatible Opcode Detected: Opcode: %s, Funct: %s",
5050
instruction.OpcodeHex(), instruction.Funct()),
5151
})
@@ -75,7 +75,7 @@ func (op *opcode) buildCallGraph(path string) (asmparser.CallGraph, error) {
7575
}
7676

7777
// TraceStack generates callstack for a function to debug
78-
func (op *opcode) TraceStack(path string, function string) (*analyzer.IssueSource, error) {
78+
func (op *opcode) TraceStack(path string, function string) (*analyzer.CallStack, error) {
7979
graph, err := op.buildCallGraph(path)
8080
if err != nil {
8181
return nil, err

analyzer/syscall/asm_syscall.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import (
1414
"github.com/ChainSafe/vm-compat/profile"
1515
)
1616

17+
const (
18+
analyzerWorkingPrincipalURL = "https://github.com/ChainSafe/vm-compat?tab=readme-ov-file#how-it-works"
19+
potentialImpactMsg = `This syscall is present in the program, but its execution depends on the actual runtime behavior.
20+
If the execution path does not reach this syscall, it may not affect execution.`
21+
)
22+
1723
// asmSyscallAnalyser analyzes system calls in assembly files.
1824
type asmSyscallAnalyser struct {
1925
profile *profile.VMProfile
@@ -69,9 +75,11 @@ func (a *asmSyscallAnalyser) Analyze(path string, withTrace bool) ([]*analyzer.I
6975
}
7076

7177
issues = append(issues, &analyzer.Issue{
72-
Severity: severity,
73-
Message: message,
74-
Sources: source,
78+
Severity: severity,
79+
Message: message,
80+
CallStack: source,
81+
Impact: potentialImpactMsg,
82+
Reference: analyzerWorkingPrincipalURL,
7583
})
7684
}
7785
}
@@ -99,7 +107,7 @@ func (a *asmSyscallAnalyser) buildCallGraph(path string) (asmparser.CallGraph, e
99107
}
100108

101109
// TraceStack generates callstack for a function to debug
102-
func (a *asmSyscallAnalyser) TraceStack(path string, function string) (*analyzer.IssueSource, error) {
110+
func (a *asmSyscallAnalyser) TraceStack(path string, function string) (*analyzer.CallStack, error) {
103111
graph, err := a.buildCallGraph(path)
104112
if err != nil {
105113
return nil, err

analyzer/syscall/go_syscall.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ func (a *goSyscallAnalyser) Analyze(path string, withTrace bool) ([]*analyzer.Is
6464
}
6565

6666
issues = append(issues, &analyzer.Issue{
67-
Severity: severity,
68-
Sources: stackTrace,
69-
Message: message,
67+
Severity: severity,
68+
CallStack: stackTrace,
69+
Message: message,
7070
})
7171
}
7272

7373
return issues, nil
7474
}
7575

76-
func (a *goSyscallAnalyser) TraceStack(path string, function string) (*analyzer.IssueSource, error) {
76+
func (a *goSyscallAnalyser) TraceStack(path string, function string) (*analyzer.CallStack, error) {
7777
cg, fset, err := a.buildCallGraph(path)
7878
if err != nil {
7979
return nil, err
@@ -131,15 +131,15 @@ func (a *goSyscallAnalyser) extractSyscalls(cg *callgraph.Graph) []*syscallSourc
131131
return syscalls
132132
}
133133

134-
func (a *goSyscallAnalyser) edgeToCallStack(stack *lifo.Stack[*callgraph.Edge], fset *token.FileSet, fullStack bool) *analyzer.IssueSource {
135-
var issueSource *analyzer.IssueSource
134+
func (a *goSyscallAnalyser) edgeToCallStack(stack *lifo.Stack[*callgraph.Edge], fset *token.FileSet, fullStack bool) *analyzer.CallStack {
135+
var issueSource *analyzer.CallStack
136136
for !stack.IsEmpty() {
137137
edge, _ := stack.Pop()
138138
if edge.Site == nil {
139139
continue
140140
}
141141
position := fset.Position(edge.Site.Pos())
142-
src := &analyzer.IssueSource{
142+
src := &analyzer.CallStack{
143143
File: position.Filename,
144144
Line: position.Line,
145145
Function: edge.Caller.Func.String(),
@@ -157,18 +157,18 @@ func (a *goSyscallAnalyser) edgeToCallStack(stack *lifo.Stack[*callgraph.Edge],
157157
return issueSource
158158
}
159159

160-
func (a *goSyscallAnalyser) buildCallStack(cg *callgraph.Graph, fset *token.FileSet, functions []string) map[string]*analyzer.IssueSource {
161-
sources := make(map[string]*lifo.Stack[*analyzer.IssueSource])
162-
currentStack := lifo.Stack[*analyzer.IssueSource]{}
160+
func (a *goSyscallAnalyser) buildCallStack(cg *callgraph.Graph, fset *token.FileSet, functions []string) map[string]*analyzer.CallStack {
161+
sources := make(map[string]*lifo.Stack[*analyzer.CallStack])
162+
currentStack := lifo.Stack[*analyzer.CallStack]{}
163163
seen := make(map[*callgraph.Node]bool)
164164
var visit func(n *callgraph.Node, edge *callgraph.Edge)
165165

166166
visit = func(n *callgraph.Node, edge *callgraph.Edge) {
167-
var src *analyzer.IssueSource
167+
var src *analyzer.CallStack
168168
if edge != nil && edge.Caller != nil && edge.Site != nil {
169169
position := fset.Position(edge.Site.Pos())
170170
fn := edge.Caller.Func.String()
171-
src = &analyzer.IssueSource{
171+
src = &analyzer.CallStack{
172172
File: position.Filename,
173173
Line: position.Line,
174174
Function: fn,
@@ -200,7 +200,7 @@ func (a *goSyscallAnalyser) buildCallStack(cg *callgraph.Graph, fset *token.File
200200
visit(n, nil)
201201
}
202202
}
203-
issuesSources := make(map[string]*analyzer.IssueSource)
203+
issuesSources := make(map[string]*analyzer.CallStack)
204204
for fn, stack := range sources {
205205
source, _ := stack.Pop()
206206
for !stack.IsEmpty() {

cmd/trace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TraceCaller(ctx *cli.Context) error {
7373
return nil
7474
}
7575

76-
func printCallStack(source *analyzer.IssueSource, str string) string {
76+
func printCallStack(source *analyzer.CallStack, str string) string {
7777
fileInfo := fmt.Sprintf(
7878
" \033[94m\033]8;;file://%s:%d\033\\%s:%d\033]8;;\033\\\033[0m",
7979
source.AbsPath, source.Line, source.File, source.Line,

common/stack_tracer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TraceAsmCaller(
1414
graph asmparser.CallGraph,
1515
function string,
1616
endCond func(string) bool,
17-
) (*analyzer.IssueSource, error) {
17+
) (*analyzer.CallStack, error) {
1818
var segment asmparser.Segment
1919
for _, seg := range graph.Segments() {
2020
if seg.Label() == function {
@@ -26,15 +26,15 @@ func TraceAsmCaller(
2626
return nil, fmt.Errorf("could not find %s in %s", function, filePath)
2727
}
2828
seen := make(map[asmparser.Segment]bool)
29-
var visit func(graph asmparser.CallGraph, segment asmparser.Segment) *analyzer.IssueSource
29+
var visit func(graph asmparser.CallGraph, segment asmparser.Segment) *analyzer.CallStack
3030

31-
visit = func(graph asmparser.CallGraph, segment asmparser.Segment) *analyzer.IssueSource {
31+
visit = func(graph asmparser.CallGraph, segment asmparser.Segment) *analyzer.CallStack {
3232
if seen[segment] {
3333
return nil
3434
}
3535
seen[segment] = true
3636

37-
source := &analyzer.IssueSource{
37+
source := &analyzer.CallStack{
3838
File: filepath.Base(filePath),
3939
Line: segment.Instructions()[0].Line() - 1, // function start line
4040
AbsPath: filePath,

renderer/text.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ func (r *TextRenderer) Render(issues []*analyzer.Issue, output io.Writer) error
7676
for _, msg := range sortedMessages {
7777
groupedIssue := groupedIssues[msg]
7878
report.WriteString(fmt.Sprintf("%d. [%s] %s\n", issueCounter, groupedIssue[0].Severity, msg))
79-
report.WriteString(" - Sources:")
79+
if len(groupedIssue[0].Impact) > 0 {
80+
report.WriteString(fmt.Sprintf(" - Impact: %s \n", groupedIssue[0].Impact))
81+
}
82+
if len(groupedIssue[0].Reference) > 0 {
83+
report.WriteString(fmt.Sprintf(" - Referance: %s \n", groupedIssue[0].Reference))
84+
}
85+
report.WriteString(" - CallStack:")
8086

8187
for _, issue := range groupedIssue {
82-
report.WriteString(fmt.Sprintf("%s\n", buildCallStack(output, issue.Sources, "")))
88+
report.WriteString(fmt.Sprintf("%s\n", buildCallStack(output, issue.CallStack, "")))
8389
}
8490
issueCounter++
8591
}
@@ -96,7 +102,7 @@ func (r *TextRenderer) Render(issues []*analyzer.Issue, output io.Writer) error
96102
return err
97103
}
98104

99-
func buildCallStack(output io.Writer, source *analyzer.IssueSource, str string) string {
105+
func buildCallStack(output io.Writer, source *analyzer.CallStack, str string) string {
100106
var fileInfo string
101107
if output == os.Stdout {
102108
fileInfo = fmt.Sprintf(

0 commit comments

Comments
 (0)