-
Notifications
You must be signed in to change notification settings - Fork 63
/
kernel.go
348 lines (296 loc) · 9.55 KB
/
kernel.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package app
import (
"bytes"
"compress/gzip"
"context"
"debug/elf"
"encoding/binary"
"fmt"
"io"
"os"
"strings"
"kraftkit.sh/kconfig"
makefile "kraftkit.sh/make"
"kraftkit.sh/unikraft/core"
"kraftkit.sh/unikraft/lib"
)
var uk_ibinfo_section_name = ".uk_libinfo"
// This type contains informations about how the components of a unikernel were built.
// Those can be either the core(unikraft) or the libraries, the difference being that
// in the case of the core, the name of the component is empty.
type ComponentInfoRecord struct {
LibName string `yaml:"name,omitempty" json:"name,omitempty"`
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
License string `yaml:"license,omitempty" json:"license,omitempty"`
GitDesc string `yaml:"git_description,omitempty" json:"git_description,omitempty"`
UkVersion string `yaml:"uk_version,omitempty" json:"uk_version,omitempty"`
UkFullVersion string `yaml:"uk_full_version,omitempty" json:"uk_full_version,omitempty"`
UkCodeName string `yaml:"uk_code_name,omitempty" json:"uk_code_name,omitempty"`
UkConfig []byte `yaml:"uk_config,omitempty" json:"uk_config,omitempty"`
UkConfiggz []byte `yaml:"-" json:"-"`
Compiler string `yaml:"compiler,omitempty" json:"compiler,omitempty"`
CompileDate string `yaml:"compile_date,omitempty" json:"compile_date,omitempty"`
CompiledBy string `yaml:"compiled_by,omitempty" json:"compiled_by,omitempty"`
CompiledByAssoc string `yaml:"compiled_by_association,omitempty" json:"compiled_by_association,omitempty"`
CompileFlags []string `yaml:"compile_flags,omitempty" json:"compile_flags,omitempty"`
}
func libraryFromRecord(libRecord ComponentInfoRecord) (*lib.LibraryConfig, error) {
version := "unknown"
if libRecord.Version != "" {
version = libRecord.Version
}
if libRecord.UkVersion != "" {
version = libRecord.UkVersion
}
if libRecord.UkFullVersion != "" {
version = libRecord.UkFullVersion
}
// map the compile flags slice to a make.ConditionalValue slice
cFlags := make([]*makefile.ConditionalValue, 0)
for _, flag := range libRecord.CompileFlags {
cFlags = append(cFlags, &makefile.ConditionalValue{
DependsOn: nil,
Value: flag,
})
}
return lib.NewLibraryConfigFromOptions(lib.WithName(libRecord.LibName),
lib.WithVersion(version),
lib.WithLicense(libRecord.License),
lib.WithCompiler(libRecord.Compiler),
lib.WithCompileDate(libRecord.CompileDate),
lib.WithCompiledBy(libRecord.CompiledBy),
lib.WithCompiledByAssoc(libRecord.CompiledByAssoc),
lib.WithCFlags(cFlags),
)
}
func gunzipConfig(configgz []byte) ([]byte, error) {
buf := bytes.NewBuffer(configgz)
gz, err := gzip.NewReader(buf)
if err != nil {
return nil, err
}
defer gz.Close()
configData, err := io.ReadAll(gz)
if err != nil {
return nil, err
}
return configData, nil
}
func unikraftFromRecord(ctx context.Context, unikraftRecord ComponentInfoRecord) (*core.UnikraftConfig, error) {
var configData []byte
var err error
if unikraftRecord.UkConfig != nil {
configData = unikraftRecord.UkConfig
} else if unikraftRecord.UkConfiggz != nil {
configData, err = gunzipConfig(unikraftRecord.UkConfiggz)
if err != nil {
return nil, err
}
}
config, err := kconfig.ParseConfigData(configData)
if err != nil {
return nil, err
}
configMap := kconfig.KeyValueMap{}
for _, cfg := range config.Slice {
if cfg.Value == kconfig.No {
continue
}
configMap[cfg.Key] = &kconfig.KeyValue{
Key: cfg.Key,
Value: cfg.Value,
}
}
version := "Unknown"
if unikraftRecord.UkVersion != "" {
version = unikraftRecord.UkVersion
}
if unikraftRecord.UkFullVersion != "" {
version = unikraftRecord.UkFullVersion
}
return core.NewUnikraftFromOptions(ctx, core.WithVersion(version), core.WithKConfig(configMap))
}
func addFieldToRecord(record *ComponentInfoRecord, tnum int, data []byte, byteorder binary.ByteOrder, configFile string) error {
switch tnum {
case 0x0001:
record.LibName = string(data[:len(data)-1])
case 0x0002:
record.Comment = string(data[:len(data)-1])
case 0x0003:
record.Version = string(data[:len(data)-1])
case 0x0004:
record.License = string(data[:len(data)-1])
case 0x0005:
record.GitDesc = string(data[:len(data)-1])
case 0x0006:
record.UkVersion = string(data[:len(data)-1])
case 0x0007:
record.UkFullVersion = string(data[:len(data)-1])
case 0x0008:
record.UkCodeName = string(data[:len(data)-1])
case 0x0009:
record.UkConfig = data
if configFile != "" {
err := os.WriteFile(configFile, record.UkConfig, 0o644)
if err != nil {
return err
}
}
case 0x000A:
record.UkConfiggz = data
if configFile != "" {
config, err := gunzipConfig(record.UkConfiggz)
if err != nil {
return err
}
err = os.WriteFile(configFile, config, 0o644)
if err != nil {
return err
}
}
case 0x000B:
record.Compiler = string(data[:len(data)-1])
case 0x000C:
record.CompileDate = string(data[:len(data)-1])
case 0x000D:
record.CompiledBy = string(data[:len(data)-1])
case 0x000E:
record.CompiledByAssoc = string(data[:len(data)-1])
case 0x00F:
// convert the data to an integer based on the byterorder
flags := byteorder.Uint32(data)
if flags&0x01 == 0x01 {
flags ^= 0x01
record.CompileFlags = append(record.CompileFlags, "PIE")
}
if flags&0x02 == 0x02 {
flags ^= 0x02
record.CompileFlags = append(record.CompileFlags, "DCE")
}
if flags&0x04 == 0x04 {
flags ^= 0x04
record.CompileFlags = append(record.CompileFlags, "LTO")
}
if flags != 0 {
record.CompileFlags = append(record.CompileFlags, fmt.Sprintf("UNKNOWN-0x%x", flags))
}
default:
}
return nil
}
func parseUKLibInfo(ctx context.Context, elfName, configFile, kraftFile string, elfData []byte, ushortSize, uintSize int, byteorder binary.ByteOrder) (application, error) {
hdrHdrLen := uintSize + ushortSize
recHdrLen := ushortSize + uintSize
seek := 0
left := len(elfData)
var err error
var unikraft *core.UnikraftConfig
var libraries []*lib.LibraryConfig
for left >= hdrHdrLen {
hdrLen := int(byteorder.Uint32(elfData[seek : seek+uintSize]))
seek += uintSize
if hdrLen > left {
return application{}, fmt.Errorf("invalid header size at byte position %d", seek)
}
hdrVersion := int(byteorder.Uint16(elfData[seek : seek+ushortSize]))
seek += ushortSize
left -= hdrLen
recSeek := seek
recLeft := hdrLen - hdrHdrLen
seek += recLeft
if hdrVersion != 1 {
continue
}
record := ComponentInfoRecord{}
for recLeft >= recHdrLen {
recType := int(byteorder.Uint16(elfData[recSeek : recSeek+ushortSize]))
recSeek += ushortSize
recLen := int(byteorder.Uint32(elfData[recSeek : recSeek+uintSize]))
recSeek += uintSize
if recLen > recLeft {
return application{}, fmt.Errorf("invalid record size at byte position %d", recSeek)
}
recData := elfData[recSeek : recSeek+(recLen-recHdrLen)]
recSeek += len(recData)
recLeft -= recLen
err := addFieldToRecord(&record, recType, recData, byteorder, configFile)
if err != nil {
return application{}, err
}
}
if record.LibName == "" {
unikraft, err = unikraftFromRecord(ctx, record)
if err != nil {
return application{}, err
}
} else {
lib, err := libraryFromRecord(record)
if err != nil {
return application{}, err
}
libraries = append(libraries, lib)
}
}
libs := map[string]*lib.LibraryConfig{}
for _, lib := range libraries {
libs[lib.Name()] = lib
}
name := elfName
if defname, have := unikraft.KConfig().Get("UK_DEFNAME"); have {
name = defname.Value
}
if ukname, have := unikraft.KConfig().Get("UK_NAME"); have {
name = ukname.Value
}
// Go through all libraries and try to build their own kconfig
for i, lib := range libs {
libConfig := kconfig.KeyValueMap{}
// Iterate through all the configs in core
for _, cfg := range unikraft.KConfig().Slice() {
// If the prefix of the config is the upper case version of the library name
if strings.HasPrefix(strings.ToUpper(cfg.Key), strings.ToUpper(lib.Name())) {
// Add the config to the library config
libConfig[cfg.Key] = cfg
// Remove the config from the core config
delete(unikraft.KConfig(), cfg.Key)
}
}
libs[i].SetKConfig(libConfig)
}
return application{
name: name,
unikraft: unikraft,
libraries: libs,
kraftfile: &Kraftfile{path: kraftFile},
}, nil
}
// This function attempts to read the uk_libinfo section of an ELF binary and fill in the fields of an application on a
// best-effort basis.
func NewApplicationFromKernel(ctx context.Context, elfPath, configFile, kraftFile string) (Application, error) {
fe, err := elf.Open(elfPath)
if err != nil {
return application{}, err
}
var libinfo_section *elf.Section
for _, section := range fe.Sections {
if section.Name == uk_ibinfo_section_name {
libinfo_section = section
}
}
if libinfo_section == nil {
return application{}, fmt.Errorf("no %s section found", uk_ibinfo_section_name)
}
elfData, err := libinfo_section.Data()
if err != nil {
return application{}, err
}
elfName := strings.Split(elfPath, "/")[len(strings.Split(elfPath, "/"))-1]
// TODO: potentially look at the architecture to figure out those sizes
app, err := parseUKLibInfo(ctx, elfName, configFile, kraftFile, elfData, 2, 4, fe.ByteOrder)
return app, err
}