-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresolve.go
More file actions
283 lines (257 loc) · 7.36 KB
/
Copy pathresolve.go
File metadata and controls
283 lines (257 loc) · 7.36 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
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
// SPDX-FileCopyrightText: 2026 RizinOrg <info@rizin.re>
// SPDX-License-Identifier: LGPL-3.0-only
package main
import "fmt"
func functionSignature(bundle ProgramBundle, fn FunctionRecord) *Signature {
if len(fn.Digest) < 1 {
return nil
}
arch := fn.Arch
if arch == "" {
arch = bundle.Arch
}
length := fn.Length
if length < 1 {
length = fn.Size
}
bits := fn.Bits
if bits < 1 {
bits = bundle.Bits
}
return &Signature{
Arch: arch,
Bits: bits,
Length: length,
Digest: append([]byte(nil), fn.Digest...),
}
}
func functionSymbol(fn FunctionRecord, bundleBits uint32) *Symbol {
if fn.Name == "" {
return nil
}
bits := fn.Bits
if bits < 1 {
bits = bundleBits
}
return &Symbol{
Name: fn.Name,
Signature: fn.Signature,
Callconv: fn.Callconv,
Bits: bits,
}
}
func sectionLookupKey(name string, paddr uint64) string {
return fmt.Sprintf("%s|%016x", name, paddr)
}
func sectionOffsetKey(name string, paddr, offset uint64) string {
return fmt.Sprintf("%s|%016x|%016x", name, paddr, offset)
}
func hintedSymbolSize(hints []*Hint, currentOffset uint64, fallback uint32) uint32 {
const maxMaterialize = 256
best := uint64(0)
for _, hint := range hints {
if hint == nil || hint.Offset <= currentOffset {
continue
}
span := hint.Offset - currentOffset
if span == 0 {
continue
}
if best == 0 || span < best {
best = span
}
}
if fallback > 0 && (best == 0 || uint64(fallback) < best) {
best = uint64(fallback)
}
if best == 0 {
best = 16
}
if best > maxMaterialize {
best = maxMaterialize
}
return uint32(best)
}
func buildShareSections(bundle ProgramBundle) []*ShareSection {
sectionsByKey := make(map[string]SectionRecord, len(bundle.Sections))
sectionsByAddr := make(map[uint64]SectionRecord, len(bundle.Sections))
for _, section := range bundle.Sections {
sectionsByKey[sectionLookupKey(section.Name, section.Paddr)] = section
sectionsByAddr[section.Paddr] = section
}
shared := map[string]*ShareSection{}
for _, fn := range bundle.Functions {
section, ok := sectionsByKey[sectionLookupKey(fn.SectionName, fn.SectionPaddr)]
if !ok {
section, ok = sectionsByAddr[fn.SectionPaddr]
}
if !ok || len(section.Digest) < 1 {
continue
}
key := sectionLookupKey(section.Name, section.Paddr)
entry, ok := shared[key]
if !ok {
entry = &ShareSection{
Name: section.Name,
Section: &SectionHash{
Size: section.Size,
Paddr: section.Paddr,
Digest: append([]byte(nil), section.Digest...),
},
}
shared[key] = entry
}
bits := fn.Bits
if bits < 1 {
bits = bundle.Bits
}
entry.Hints = append(entry.Hints, &Hint{
Bits: bits,
Offset: fn.SectionOffset,
})
}
out := make([]*ShareSection, 0, len(shared))
for _, section := range shared {
out = append(out, section)
}
return out
}
func (s *Server) ResolveProgram(psk string, bundle ProgramBundle) ResolveProgramResult {
bundle = normalizeProgramBundle(bundle)
result := ResolveProgramResult{}
sectionMetas := make(map[string]*MetaHints, len(bundle.Sections))
for _, section := range bundle.Sections {
meta := s.GetHintsMeta(psk, bundle.BinaryType, bundle.OS, &SectionHash{
Size: section.Size,
Paddr: section.Paddr,
Digest: section.Digest,
}, nil)
if meta == nil {
continue
}
sectionMetas[sectionLookupKey(section.Name, section.Paddr)] = meta
for _, hint := range meta.Hints {
if hint == nil {
continue
}
result.Hints = append(result.Hints, HintMatch{
Bits: hint.Bits,
Offset: hint.Offset + section.Paddr,
})
}
}
queryOffsets := make(map[string]struct{}, len(bundle.Functions))
for _, fn := range bundle.Functions {
queryOffsets[sectionOffsetKey(fn.SectionName, fn.SectionPaddr, fn.SectionOffset)] = struct{}{}
sectionKey := sectionLookupKey(fn.SectionName, fn.SectionPaddr)
sectionMeta := sectionMetas[sectionKey]
if sectionMeta != nil && sectionMeta.BinaryID != "" {
meta := s.GetLocationSymbolMeta(psk, sectionMeta.BinaryID, fn)
if meta != nil && meta.Symbol != nil {
result.Symbols = append(result.Symbols, SymbolMatchRecord{
Addr: fn.Addr,
Symbol: normalizeSymbolRecord(symbolRecordFromProto(meta.Symbol)),
Exact: true,
MatchedBinaryID: meta.BinaryID,
MatchedBy: "exact_location",
Offset: fn.SectionPaddr + fn.SectionOffset,
Size: hintedSymbolSize(sectionMeta.Hints, fn.SectionOffset, meta.Size),
})
continue
}
}
sig := functionSignature(bundle, fn)
if sig == nil {
continue
}
meta := s.GetSymbolMeta(psk, sig, nil)
if meta != nil && meta.Symbol != nil {
result.Symbols = append(result.Symbols, SymbolMatchRecord{
Addr: fn.Addr,
Symbol: normalizeSymbolRecord(symbolRecordFromProto(meta.Symbol)),
Exact: true,
MatchedBinaryID: meta.BinaryID,
MatchedBy: "exact_signature",
})
}
}
for _, section := range bundle.Sections {
sectionKey := sectionLookupKey(section.Name, section.Paddr)
meta := sectionMetas[sectionKey]
if meta == nil || meta.BinaryID == "" {
continue
}
for _, hint := range meta.Hints {
if hint == nil {
continue
}
key := sectionOffsetKey(section.Name, section.Paddr, hint.Offset)
if _, exists := queryOffsets[key]; exists {
continue
}
locationMeta := DbGetLocationSymbolMetaSafe(s, psk, meta.BinaryID, section.Name, section.Paddr, hint.Offset)
if locationMeta == nil || locationMeta.Symbol == nil {
continue
}
result.Symbols = append(result.Symbols, SymbolMatchRecord{
Addr: 0,
Symbol: normalizeSymbolRecord(symbolRecordFromProto(locationMeta.Symbol)),
Exact: true,
MatchedBinaryID: meta.BinaryID,
MatchedBy: "exact_hint_location",
Offset: section.Paddr + hint.Offset,
Size: hintedSymbolSize(meta.Hints, hint.Offset, locationMeta.Size),
})
}
}
return result
}
func DbGetLocationSymbolMetaSafe(s *Server, psk, binaryID, sectionName string, sectionPaddr, sectionOffset uint64) *MetaSymbol {
key := FunctionLocationToKey(binaryID, sectionName, sectionPaddr, sectionOffset)
for _, db := range s.GetHintsDbs() {
meta, err := DbGetLocationSymbolMeta(db, key)
if err != nil {
continue
}
if meta != nil {
return meta
}
}
return nil
}
func (s *Server) ShareProgram(psk string, bundle ProgramBundle) ShareProgramResult {
bundle = normalizeProgramBundle(bundle)
shareBin := &ShareBin{
Type: bundle.BinaryType,
Os: bundle.OS,
Sections: buildShareSections(bundle),
}
for _, fn := range bundle.Functions {
sig := functionSignature(bundle, fn)
sym := functionSymbol(fn, bundle.Bits)
if sig != nil && sym != nil {
shareBin.Symbols = append(shareBin.Symbols, &ShareSymbol{
Symbol: sym,
Signature: sig,
})
}
}
if shareBin.Sections != nil {
for _, section := range shareBin.Sections {
s.SetHintsWithBinaryID(psk, shareBin, section, bundle.BinaryID)
}
}
if shareBin.Symbols != nil {
symbolIndex := 0
for _, fn := range bundle.Functions {
if functionSignature(bundle, fn) == nil || functionSymbol(fn, bundle.Bits) == nil {
continue
}
share := shareBin.Symbols[symbolIndex]
s.SetSymbolWithBinaryID(psk, share.Signature, share.Symbol, bundle.BinaryID)
s.SetLocationSymbolWithBinaryID(psk, fn, share.Symbol, bundle.BinaryID)
symbolIndex++
}
}
return ShareProgramResult{BinaryID: bundle.BinaryID}
}