Repository navigation
Expand file tree
/
Copy pathbindiff.go
More file actions
111 lines (93 loc) · 2.39 KB
/
Copy pathbindiff.go
File metadata and controls
111 lines (93 loc) · 2.39 KB
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
package bindiff
import (
"fmt"
"os/exec"
"path/filepath"
"strings"
"github.com/blacktop/go-bindiff/pkg/binexport"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
const (
bindiffExePath = "/Applications/BinDiff/BinDiff.app/Contents/MacOS/bin/bindiff"
)
type BinDiff struct {
ExportPrev string
ExportNext string
DiffFile string
Output string
md Metadata
files []File
fmatches []FunctionMatch
bbmatches []BasicBlockMatch
imatches []Instruction
db *gorm.DB
}
func fileNameWithoutExtTrimSuffix(fileName string) string {
return strings.TrimSuffix(fileName, filepath.Ext(fileName))
}
func NewBinDiff(prev, next, output string) (*BinDiff, error) {
bx1, err := binexport.NewBinExport(prev)
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %w", prev, err)
}
bx2, err := binexport.NewBinExport(next)
if err != nil {
return nil, fmt.Errorf("failed to parse %s: %w", next, err)
}
return &BinDiff{
ExportPrev: prev,
ExportNext: next,
DiffFile: filepath.Join(output,
fmt.Sprintf("%s_vs_%s.BinDiff",
fileNameWithoutExtTrimSuffix(bx1.ExecutableName()),
fileNameWithoutExtTrimSuffix(bx2.ExecutableName()),
)),
Output: output,
}, nil
}
func (b *BinDiff) Close() error {
db, err := b.db.DB()
if err != nil {
return err
}
return db.Close()
}
func (b *BinDiff) Run() error {
cmd := exec.Command(bindiffExePath, "--primary", b.ExportPrev, "--secondary", b.ExportNext, "--output_dir", b.Output)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run bindiff: %v: cmd output - '%s'", err, out)
}
return nil
}
func (b *BinDiff) Read() (err error) {
b.db, err = gorm.Open(sqlite.Open(b.DiffFile), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
return fmt.Errorf("failed to connect bindiff sqlite database: %w", err)
}
b.db.Table("metadata").First(&b.md)
b.db.Table("file").Find(&b.files)
b.db.Table("function").Find(&b.fmatches)
b.db.Table("basicblock").Find(&b.bbmatches)
b.db.Table("instruction").Find(&b.imatches)
return nil
}
func (b *BinDiff) Metadata() Metadata {
return b.md
}
func (b *BinDiff) Files() []File {
return b.files
}
func (b *BinDiff) FunctionMatches() []FunctionMatch {
return b.fmatches
}
func (b *BinDiff) BasicBlockMatches() []BasicBlockMatch {
return b.bbmatches
}
func (b *BinDiff) Instructions() []Instruction {
return b.imatches
}