Debian Codegraph @

btf/strings.go

raw symbols SCIP
1 package btf
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "io"
8 "maps"
9 "strings"
10 "sync"
11 )
12
13 // stringTable contains a sequence of null-terminated strings.
14 //
15 // It is safe for concurrent use.
16 type stringTable struct {
17 base *stringTable
18 bytes []byte
19
20 mu sync.Mutex
21 cache map[uint32]string
22 }
23
24 // sizedReader is implemented by bytes.Reader, io.SectionReader, strings.Reader, etc.
25 type sizedReader interface {
26 io.Reader
27 Size() int64
28 }
29
30 func readStringTable(r sizedReader, base *stringTable) (*stringTable, error) {
31 bytes := make([]byte, r.Size())
32 if _, err := io.ReadFull(r, bytes); err != nil {
33 return nil, err
34 }
35
36 return newStringTable(bytes, base)
37 }
38
39 func newStringTable(bytes []byte, base *stringTable) (*stringTable, error) {
40 // When parsing split BTF's string table, the first entry offset is derived
41 // from the last entry offset of the base BTF.
42 firstStringOffset := uint32(0)
43 if base != nil {
44 firstStringOffset = uint32(len(base.bytes))
45 }
46
47 if len(bytes) > 0 {
48 if bytes[len(bytes)-1] != 0 {
49 return nil, errors.New("string table isn't null terminated")
50 }
51
52 if firstStringOffset == 0 && bytes[0] != 0 {
53 return nil, errors.New("first item in string table is non-empty")
54 }
55 }
56
57 return &stringTable{base: base, bytes: bytes}, nil
58 }
59
60 func (st *stringTable) Lookup(offset uint32) (string, error) {
61 // Fast path: zero offset is the empty string, looked up frequently.
62 if offset == 0 {
63 return "", nil
64 }
65
66 b, err := st.lookupSlow(offset)
67 return string(b), err
68 }
69
70 func (st *stringTable) LookupBytes(offset uint32) ([]byte, error) {
71 // Fast path: zero offset is the empty string, looked up frequently.
72 if offset == 0 {
73 return nil, nil
74 }
75
76 return st.lookupSlow(offset)
77 }
78
79 func (st *stringTable) lookupSlow(offset uint32) ([]byte, error) {
80 if st.base != nil {
81 n := uint32(len(st.base.bytes))
82 if offset < n {
83 return st.base.lookupSlow(offset)
84 }
85 offset -= n
86 }
87
88 if offset > uint32(len(st.bytes)) {
89 return nil, fmt.Errorf("offset %d is out of bounds of string table", offset)
90 }
91
92 if offset > 0 && st.bytes[offset-1] != 0 {
93 return nil, fmt.Errorf("offset %d is not the beginning of a string", offset)
94 }
95
96 i := bytes.IndexByte(st.bytes[offset:], 0)
97 if i == -1 {
98 return nil, fmt.Errorf("string at offset %d is not null terminated", offset)
99 }
100 return st.bytes[offset : offset+uint32(i)], nil
101 }
102
103 // LookupCache returns the string at the given offset, caching the result
104 // for future lookups.
105 func (cst *stringTable) LookupCached(offset uint32) (string, error) {
106 // Fast path: zero offset is the empty string, looked up frequently.
107 if offset == 0 {
108 return "", nil
109 }
110
111 cst.mu.Lock()
112 defer cst.mu.Unlock()
113
114 if str, ok := cst.cache[offset]; ok {
115 return str, nil
116 }
117
118 str, err := cst.Lookup(offset)
119 if err != nil {
120 return "", err
121 }
122
123 if cst.cache == nil {
124 cst.cache = make(map[uint32]string)
125 }
126 cst.cache[offset] = str
127 return str, nil
128 }
129
130 // stringTableBuilder builds BTF string tables.
131 type stringTableBuilder struct {
132 length uint32
133 strings map[string]uint32
134 }
135
136 // newStringTableBuilder creates a builder with the given capacity.
137 //
138 // capacity may be zero.
139 func newStringTableBuilder(capacity int) *stringTableBuilder {
140 var stb stringTableBuilder
141
142 if capacity == 0 {
143 // Use the runtime's small default size.
144 stb.strings = make(map[string]uint32)
145 } else {
146 stb.strings = make(map[string]uint32, capacity)
147 }
148
149 // Ensure that the empty string is at index 0.
150 stb.append("")
151 return &stb
152 }
153
154 // Add a string to the table.
155 //
156 // Adding the same string multiple times will only store it once.
157 func (stb *stringTableBuilder) Add(str string) (uint32, error) {
158 if strings.IndexByte(str, 0) != -1 {
159 return 0, fmt.Errorf("string contains null: %q", str)
160 }
161
162 offset, ok := stb.strings[str]
163 if ok {
164 return offset, nil
165 }
166
167 return stb.append(str), nil
168 }
169
170 func (stb *stringTableBuilder) append(str string) uint32 {
171 offset := stb.length
172 stb.length += uint32(len(str)) + 1
173 stb.strings[str] = offset
174 return offset
175 }
176
177 // Lookup finds the offset of a string in the table.
178 //
179 // Returns an error if str hasn't been added yet.
180 func (stb *stringTableBuilder) Lookup(str string) (uint32, error) {
181 offset, ok := stb.strings[str]
182 if !ok {
183 return 0, fmt.Errorf("string %q is not in table", str)
184 }
185
186 return offset, nil
187 }
188
189 // Length returns the length in bytes.
190 func (stb *stringTableBuilder) Length() int {
191 return int(stb.length)
192 }
193
194 // AppendEncoded appends the string table to the end of the provided buffer.
195 func (stb *stringTableBuilder) AppendEncoded(buf []byte) []byte {
196 n := len(buf)
197 buf = append(buf, make([]byte, stb.Length())...)
198 strings := buf[n:]
199 for str, offset := range stb.strings {
200 copy(strings[offset:], str)
201 }
202 return buf
203 }
204
205 // Copy the string table builder.
206 func (stb *stringTableBuilder) Copy() *stringTableBuilder {
207 return &stringTableBuilder{
208 stb.length,
209 maps.Clone(stb.strings),
210 }
211 }