-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathstructfield.go
388 lines (330 loc) · 9.3 KB
/
structfield.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Package structfield provides types to track struct field offsets.
package structfield
import (
"encoding/json"
"fmt"
"sort"
"strings"
"sync"
"github.com/hashicorp/go-version"
)
// Index holds all struct field offsets.
type Index struct {
dataMu sync.RWMutex
data map[ID]*Offsets
}
// NewIndex returns a new empty Index.
func NewIndex() *Index {
return &Index{data: make(map[ID]*Offsets)}
}
// Get returns the Offsets and true for an id contained in the Index i. It will
// return nil and false for any id not contained in i.
func (i *Index) Get(id ID) (*Offsets, bool) {
i.dataMu.RLock()
defer i.dataMu.RUnlock()
return i.get(id)
}
func (i *Index) get(id ID) (*Offsets, bool) {
o, ok := i.data[id]
return o, ok
}
// GetOffset returns the offset value and true for the version ver of id
// contained in the Index i. It will return zero and false for any id not
// contained in i.
func (i *Index) GetOffset(id ID, ver *version.Version) (OffsetKey, bool) {
i.dataMu.RLock()
defer i.dataMu.RUnlock()
return i.getOffset(id, ver)
}
// GetLatestOffset returns the latest known offset value and version for id
// contained in the Index i.
func (i *Index) GetLatestOffset(id ID) (OffsetKey, *version.Version) {
i.dataMu.RLock()
defer i.dataMu.RUnlock()
offs, ok := i.get(id)
if !ok {
return OffsetKey{}, nil
}
off, ver := offs.getLatest()
return off, ver.ToVersion()
}
func (i *Index) getOffset(id ID, ver *version.Version) (OffsetKey, bool) {
offs, ok := i.get(id)
if !ok {
return OffsetKey{}, false
}
o, ok := offs.Get(ver)
return o, ok
}
// Put stores offsets in the Index i for id.
//
// Any existing offsets stored for id will be replaced. Use PutOffset if you
// would like to update existing offsets for id with an offset value.
func (i *Index) Put(id ID, offsets *Offsets) {
i.dataMu.Lock()
defer i.dataMu.Unlock()
i.put(id, offsets)
}
func (i *Index) put(id ID, offsets *Offsets) {
i.data[id] = offsets
}
// PutOffset stores the offset value for version ver of id within the Index i.
//
// This will update any existing offsets stored for id with offset. If ver
// already exists within those offsets it will overwrite that value.
func (i *Index) PutOffset(id ID, ver *version.Version, offset uint64, valid bool) {
i.dataMu.Lock()
defer i.dataMu.Unlock()
i.putOffset(id, ver, offset, valid)
}
func (i *Index) putOffset(id ID, ver *version.Version, offset uint64, valid bool) {
off, ok := i.get(id)
if !ok {
off = NewOffsets()
i.put(id, off)
}
off.Put(ver, OffsetKey{Offset: offset, Valid: valid})
}
// UnmarshalJSON unmarshals the offset JSON data into i.
func (i *Index) UnmarshalJSON(data []byte) error {
var mods []*jsonModule
err := json.Unmarshal(data, &mods)
if err != nil {
return err
}
m := make(map[ID]*Offsets)
for _, mod := range mods {
for _, p := range mod.Packages {
for _, s := range p.Structs {
for _, f := range s.Fields {
for _, o := range f.Offsets {
for _, v := range o.Versions {
key := ID{
ModPath: mod.Module,
PkgPath: p.Package,
Struct: s.Struct,
Field: f.Field,
}
off, ok := m[key]
if !ok {
off = new(Offsets)
m[key] = off
}
if o.Offset == nil {
off.Put(v, OffsetKey{Valid: false})
} else {
off.Put(v, OffsetKey{Offset: *o.Offset, Valid: true})
}
}
}
}
}
}
}
i.dataMu.Lock()
i.data = m
i.dataMu.Unlock()
return nil
}
// MarshalJSON marshals i into JSON data.
func (i *Index) MarshalJSON() ([]byte, error) {
i.dataMu.RLock()
defer i.dataMu.RUnlock()
var out []*jsonModule
for id, off := range i.data {
jm := find(&out, func(p *jsonModule) bool {
return id.ModPath == p.Module
})
jm.Module = id.ModPath
jm.addOffsets(id.PkgPath, id.Struct, id.Field, off)
}
// Ensure repeatability by sorting.
for _, m := range out {
for _, p := range m.Packages {
for _, s := range p.Structs {
for _, f := range s.Fields {
sort.Slice(f.Offsets, func(i, j int) bool {
if f.Offsets[i].Offset == nil {
return true
}
if f.Offsets[j].Offset == nil {
return false
}
return *f.Offsets[i].Offset < *f.Offsets[j].Offset
})
}
sort.Slice(s.Fields, func(i, j int) bool {
return s.Fields[i].Field < s.Fields[j].Field
})
}
sort.Slice(p.Structs, func(i, j int) bool {
return p.Structs[i].Struct < p.Structs[j].Struct
})
}
sort.Slice(m.Packages, func(i, j int) bool {
return m.Packages[i].Package < m.Packages[j].Package
})
}
sort.Slice(out, func(i, j int) bool {
return out[i].Module < out[j].Module
})
return json.Marshal(out)
}
// ID is a struct field identifier for an offset.
type ID struct {
// ModPath is the module path containing the struct field package.
//
// If set to "std", the struct field belongs to the standard Go library.
ModPath string
// PkgPath package import path containing the struct field.
PkgPath string
// Struct is the name of the struct containing the field.
Struct string
// Field is the field name.
Field string
}
// NewID returns a new ID using pkg for the PkgPath, strct for the Struct, and
// field for the Field.
func NewID(mod, pkg, strct, field string) ID {
return ID{ModPath: mod, PkgPath: pkg, Struct: strct, Field: field}
}
func (i ID) String() string {
return fmt.Sprintf("%s.%s:%s", i.PkgPath, i.Struct, i.Field)
}
// Offsets are the byte offsets for a struct field at specific versions of the
// package containing struct.
type Offsets struct {
// mu ensures synchronous access to all Offsets fields.
mu sync.RWMutex
// values is a map between version and offset value.
values map[verKey]offsetVersion
// uo is the single offset in the values map.
// If there is only one offset, this will be that offset and valid will be true.
// Otherwise, valid is false
uo uniqueOffset
}
type uniqueOffset struct {
value uint64
valid bool
}
// NewOffsets returns a new empty *Offsets.
func NewOffsets() *Offsets {
return &Offsets{values: make(map[verKey]offsetVersion)}
}
// Get returns the offset in bytes and true if known. Otherwise, 0 and false
// are returned.
func (o *Offsets) Get(ver *version.Version) (OffsetKey, bool) {
if o == nil {
return OffsetKey{}, false
}
o.mu.RLock()
v, ok := o.values[newVerKey(ver)]
o.mu.RUnlock()
if strings.HasPrefix(ver.String(), "0.0.0") && !ok && o.uo.valid {
// If we don't have the exact version, but we only have one offset, we
// fallback to use that offset. This can happen when a non official version is being used
// which contains commit hash in the version string.
return OffsetKey{Offset: o.uo.value, Valid: true}, true
}
return v.offset, ok
}
// getLatest returns the latest known offset value and version.
func (o *Offsets) getLatest() (OffsetKey, verKey) {
o.mu.RLock()
defer o.mu.RUnlock()
latestVersion := verKey{}
val := OffsetKey{}
for verKey, ov := range o.values {
if verKey.GreaterThan(latestVersion) && ov.offset.Valid {
latestVersion = verKey
val = ov.offset
}
}
return val, latestVersion
}
// Put sets the offset value for ver. If an offset for ver is already known
// (i.e. ver.Equal(other) == true), this will overwrite that value.
func (o *Offsets) Put(ver *version.Version, offset OffsetKey) {
ov := offsetVersion{offset: offset, version: ver}
o.mu.Lock()
defer o.mu.Unlock()
if o.values == nil {
o.values = map[verKey]offsetVersion{newVerKey(ver): ov}
o.uo.valid = ov.offset.Valid
o.uo.value = ov.offset.Offset
return
}
o.values[newVerKey(ver)] = ov
if o.uo.valid && o.uo.value != ov.offset.Offset {
o.uo.valid = false
}
}
func (v verKey) GreaterThan(other verKey) bool {
if v.major != other.major {
return v.major > other.major
}
if v.minor != other.minor {
return v.minor > other.minor
}
if v.patch != other.patch {
return v.patch > other.patch
}
return false
}
func (v verKey) ToVersion() *version.Version {
vs := fmt.Sprintf("%d.%d.%d", v.major, v.minor, v.patch)
if v.prerelease != "" {
vs += "-" + v.prerelease
}
ver, _ := version.NewVersion(vs)
return ver
}
func (o *Offsets) index() map[OffsetKey][]*version.Version {
o.mu.RLock()
defer o.mu.RUnlock()
out := make(map[OffsetKey][]*version.Version)
for _, ov := range o.values {
vers, ok := out[ov.offset]
if ok {
i := sort.Search(len(vers), func(i int) bool {
return vers[i].GreaterThanOrEqual(ov.version)
})
vers = append(vers, nil)
copy(vers[i+1:], vers[i:])
vers[i] = ov.version
} else {
vers = append(vers, ov.version)
}
out[ov.offset] = vers
}
return out
}
type verKey struct {
major, minor, patch uint64
prerelease string
metadata string
}
// OffsetKey is the offset of a specific struct field in a specific version.
// If Valid is false, the offset is not known for the struct field at the
// specified version.
type OffsetKey struct {
Offset uint64
Valid bool
}
func newVerKey(v *version.Version) verKey {
var segs [3]int
copy(segs[:], v.Segments())
return verKey{
major: uint64(max(segs[0], 0)), // nolint: gosec // Bounded.
minor: uint64(max(segs[1], 0)), // nolint: gosec // Bounded.
patch: uint64(max(segs[2], 0)), // nolint: gosec // Bounded.
prerelease: v.Prerelease(),
metadata: v.Metadata(),
}
}
type offsetVersion struct {
offset OffsetKey
version *version.Version
}