-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
300 lines (263 loc) · 7.37 KB
/
main.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
package main
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"unsafe"
"github.com/pkujhd/goloader"
)
func main() {
// this is here to link the runtime, without which everything will fail to link
_ = runtime.Version()
if len(os.Args) < 2 {
fmt.Println(errors.New("no path to the objects dir was provided"))
return
}
implementationWrapper, err := NewImplementationWrapper(os.Args[1])
if err != nil {
fmt.Printf("failed to load implementation: %s\n", err)
return
}
stuff := "default test stuff"
if len(os.Args) > 2 {
stuff = os.Args[2]
}
processedStuff, err := implementationWrapper.ProcessStuff(stuff)
if err != nil {
fmt.Printf("failed to process stuff: %s\n", err)
}
fmt.Println(processedStuff)
}
type Interface interface {
ProcessStuff(string) (string, error)
}
type ImplementationWrapper struct {
module *goloader.CodeModule
implementation Interface
}
func NewImplementationWrapper(dir string) (*ImplementationWrapper, error) {
archivesPattern := filepath.Join(dir, "/*.a")
globbedArchives, err := filepath.Glob(archivesPattern)
if err != nil {
return nil, fmt.Errorf("failed to glob archive files: %w", err)
}
objectsPattern := filepath.Join(dir, "/*.o")
globbedObjects, err := filepath.Glob(objectsPattern)
if err != nil {
return nil, fmt.Errorf("failed to glob object files: %w", err)
}
allObjects := append(globbedArchives, globbedObjects...)
err = checkDependencies(allObjects)
if err != nil {
return nil, fmt.Errorf("failed to check dependencies: %w", err)
}
symPtr := map[string]uintptr{}
if err = goloader.RegSymbol(symPtr); err != nil {
return nil, fmt.Errorf("go object loader could not register symbol table: %w", err)
}
linker, err := goloader.ReadObjs(allObjects, make([]string, len(allObjects)))
if err != nil {
return nil, fmt.Errorf("failed to read objects: %w", err)
}
module, err := goloader.Load(linker, symPtr)
if err != nil {
return nil, fmt.Errorf("failed to dynamically link objects: %w", err)
}
// we expect the constructor function to be present in the main package
ctorPtr, err := getFncPtr(module, "main.NewImplementation")
if err != nil {
return nil, fmt.Errorf("failed to get implementation constructor fn: %w", err)
}
// unsafe-land!
// this case relies on the signature of the constructor to be the same
// for all the implementations
ctor := *(*func() Interface)(ctorPtr)
result := &ImplementationWrapper{
module: module,
implementation: ctor(),
}
return result, nil
}
func (iw *ImplementationWrapper) ProcessStuff(stuff string) (string, error) {
// implement the Interface to use the wrapper where we want it
if iw.module == nil {
return "", errors.New("no implementation loaded")
}
return iw.implementation.ProcessStuff(stuff)
}
func (iw *ImplementationWrapper) UnloadImplementation() {
if iw.module != nil {
iw.implementation = nil
iw.module.Unload()
iw.module = nil
}
}
func getFncPtr(module *goloader.CodeModule, fncName string) (unsafe.Pointer, error) {
fPtr, found := module.Syms[fncName]
if !found || fPtr == 0 {
return nil, fmt.Errorf("symbol not found: %s", fncName)
}
ptrTofPtr := (uintptr)(unsafe.Pointer(&fPtr))
return unsafe.Pointer(&ptrTofPtr), nil
}
func checkDependencies(objects []string) error {
// read own build info
buildInfo, err := getBuildInfo()
if err != nil {
return err
}
// we read the files twice since we couldn't find a way to easily access the modinfo
// as it is mapped to runtime.modinfo which is resolved by the linker
var implementationBuildInfo *debug.BuildInfo
for _, obj := range objects {
if implementationBuildInfo = getModInfoFromFile(obj); implementationBuildInfo != nil {
break
}
}
if implementationBuildInfo != nil {
// We need to make sure that what we load has the same deps as we are running with.
// If a dependency is missing the loader will already fail during dynamic linking,
// so what is left to us is to check the versions.
// The question is how strict we want to be? So for now we don't return an error
// from validateDependencies but only log some warnings
if err = validateDependencies(buildInfo, implementationBuildInfo); err != nil {
return err
}
}
return nil
}
func getBuildInfo() (*debug.BuildInfo, error) {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return nil, fmt.Errorf("failed to read debug build info")
}
return buildInfo, nil
}
func getModInfoFromFile(path string) *debug.BuildInfo {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
// see src/cmd/go/internal/modload/build.go
infoStart, _ := hex.DecodeString("3077af0c9274080241e1c107e6d618e6")
infoEnd, _ := hex.DecodeString("f932433186182072008242104116d8f2")
start := bytes.Index(data, infoStart)
end := bytes.Index(data, infoEnd)
if start <= 0 || end <= 0 && end <= start {
return nil
}
bi, ok := readBuildInfo(string(data[start:end]))
if !ok {
return nil
}
return bi
}
// keep in sync with src/runtime/debug/mod.go:readBuildInfo
func readBuildInfo(data string) (*debug.BuildInfo, bool) {
if len(data) < 32 {
return nil, false
}
data = data[16 : len(data)-16]
const (
pathLine = "path\t"
modLine = "mod\t"
depLine = "dep\t"
repLine = "=>\t"
)
readEntryFirstLine := func(elem []string) (debug.Module, bool) {
if len(elem) != 2 && len(elem) != 3 {
return debug.Module{}, false
}
sum := ""
if len(elem) == 3 {
sum = elem[2]
}
return debug.Module{
Path: elem[0],
Version: elem[1],
Sum: sum,
}, true
}
var (
info = &debug.BuildInfo{}
last *debug.Module
line string
ok bool
)
// Reverse of cmd/go/internal/modload.PackageBuildInfo
for len(data) > 0 {
i := strings.IndexByte(data, '\n')
if i < 0 {
break
}
line, data = data[:i], data[i+1:]
switch {
case strings.HasPrefix(line, pathLine):
elem := line[len(pathLine):]
info.Path = elem
case strings.HasPrefix(line, modLine):
elem := strings.Split(line[len(modLine):], "\t")
last = &info.Main
*last, ok = readEntryFirstLine(elem)
if !ok {
return nil, false
}
case strings.HasPrefix(line, depLine):
elem := strings.Split(line[len(depLine):], "\t")
last = new(debug.Module)
info.Deps = append(info.Deps, last)
*last, ok = readEntryFirstLine(elem)
if !ok {
return nil, false
}
case strings.HasPrefix(line, repLine):
elem := strings.Split(line[len(repLine):], "\t")
if len(elem) != 3 {
return nil, false
}
if last == nil {
return nil, false
}
last.Replace = &debug.Module{
Path: elem[0],
Version: elem[1],
Sum: elem[2],
}
last = nil
}
}
return info, true
}
func validateDependencies(buildInfo, needed *debug.BuildInfo) error {
having := make(map[string]*debug.Module)
for _, dep := range buildInfo.Deps {
having[dep.Path] = dep
// is a single level enough?
if dep.Replace != nil {
having[dep.Path] = dep.Replace
}
}
for _, dep := range needed.Deps {
have, found := having[dep.Path]
if !found {
// we should never reach this due to the dynamic linking
return fmt.Errorf("missing dependency: %s", dep.Path)
}
if have.Version != dep.Version {
// for now, we just print a warning
fmt.Printf(
"version mismatch while validating dynamically loaded obj %s@%s: dep %s mismatch have=%s want=%s\n",
needed.Main.Path, needed.Main.Version,
dep.Path, have.Version, dep.Version,
)
}
}
return nil
}