From 4dbd403fd0978b91ffe3bb2f2a749511591644dd Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 8 May 2024 12:24:46 +0200 Subject: pkg/csource: replace go:generate with go:embed go:embed is a more modern way to do this and it does not require a special Makefile step. Since go:embed cannot use paths that contains "..", the actual embeding is moved to executor package. --- executor/embed.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ executor/stub.go | 7 ------ 2 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 executor/embed.go delete mode 100644 executor/stub.go (limited to 'executor') diff --git a/executor/embed.go b/executor/embed.go new file mode 100644 index 000000000..a02413a12 --- /dev/null +++ b/executor/embed.go @@ -0,0 +1,73 @@ +// Copyright 2024 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package executor + +import ( + "bytes" + "embed" + "fmt" + "io/fs" + "path" + "regexp" +) + +//go:embed common*.h kvm*.h android/*.h +var src embed.FS + +// CommonHeader contains all executor common headers used by pkg/csource to generate C reproducers. +// All includes in common.h are transitively replaced with their contents so that it's just a single blob. +var CommonHeader = func() []byte { + data, err := src.ReadFile("common.h") + if err != nil { + panic(err) + } + headers := make(map[string]bool) + for _, glob := range []string{"*.h", "android/*.h"} { + files, err := fs.Glob(src, glob) + if err != nil { + panic(err) + } + for _, file := range files { + if file == "common.h" || file == "common_ext_example.h" { + continue + } + headers[file] = true + } + } + // To not hardcode concrete order in which headers need to be replaced + // we just iteratively try to replace whatever headers can be replaced + // on the current step. + for len(headers) != 0 { + relacedSomething := false + for file := range headers { + replace := []byte("#include \"" + path.Base(file) + "\"") + if !bytes.Contains(data, replace) { + replace = []byte("#include \"android/" + path.Base(file) + "\"") + if !bytes.Contains(data, replace) { + continue + } + } + contents, err := src.ReadFile(file) + if err != nil { + panic(err) + } + data = bytes.ReplaceAll(data, replace, contents) + delete(headers, file) + relacedSomething = true + } + if !relacedSomething { + panic(fmt.Sprintf("can't find includes for %v", headers)) + } + } + // Remove `//` comments, but keep lines which start with `//%`. + for _, remove := range []string{ + "(\n|^)\\s*//$", + "(\n|^)\\s*//[^%].*", + "\\s*//$", + "\\s*//[^%].*", + } { + data = regexp.MustCompile(remove).ReplaceAll(data, nil) + } + return data +}() diff --git a/executor/stub.go b/executor/stub.go deleted file mode 100644 index 9b6ab882e..000000000 --- a/executor/stub.go +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright 2017 syzkaller project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -// Just to make cross-compilation tests work, -// otherwise they fail with "no buildable Go source files". - -package executor -- cgit mrf-deployment