From 44a5ca633e186c5836010366c515a4017836121b Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 29 Apr 2022 09:15:41 +0200 Subject: pkg/ast, pkg/compiler: support per-file metadata We have a bunch of hacks in syz-extract, syz-sysgen and syz-check with respect to description files unsupported on some arches, or that must not be part of make extract. Add 2 meta attribtues to files: meta noextract Tells `make extract` to not extract constants for this file. Though, `syz-extract` can still be invoked manually on this file. meta arches["arch1", "arch2"] Restricts this file only to the given set of architectures. `make extract` and ``make generate` will not use it on other architectures. Later we can potentially use meta attributes to specify git tree/commit that must be used for extraction. Maybe something else. Fixes #2754 --- pkg/ast/ast.go | 9 +++++++++ pkg/ast/clone.go | 7 +++++++ pkg/ast/format.go | 6 ++++++ pkg/ast/parser.go | 12 +++++++++++- pkg/ast/testdata/all.txt | 3 +++ pkg/ast/testdata/errors.txt | 6 +++++- pkg/ast/walk.go | 4 ++++ 7 files changed, 45 insertions(+), 2 deletions(-) (limited to 'pkg/ast') diff --git a/pkg/ast/ast.go b/pkg/ast/ast.go index 66dfc11bd..f0403a07b 100644 --- a/pkg/ast/ast.go +++ b/pkg/ast/ast.go @@ -46,6 +46,15 @@ func (n *Comment) Info() (Pos, string, string) { return n.Pos, tok2str[tokComment], "" } +type Meta struct { + Pos Pos + Value *Type +} + +func (n *Meta) Info() (Pos, string, string) { + return n.Pos, "meta", n.Value.Ident +} + type Include struct { Pos Pos File *String diff --git a/pkg/ast/clone.go b/pkg/ast/clone.go index 23e4f421c..0c9c831f0 100644 --- a/pkg/ast/clone.go +++ b/pkg/ast/clone.go @@ -24,6 +24,13 @@ func (n *Comment) Clone() Node { } } +func (n *Meta) Clone() Node { + return &Meta{ + Pos: n.Pos, + Value: n.Value.Clone().(*Type), + } +} + func (n *Include) Clone() Node { return &Include{ Pos: n.Pos, diff --git a/pkg/ast/format.go b/pkg/ast/format.go index 51538eaf1..f4a01d37b 100644 --- a/pkg/ast/format.go +++ b/pkg/ast/format.go @@ -74,6 +74,12 @@ func (n *Comment) serialize(w io.Writer) { fmt.Fprintf(w, "#%v\n", strings.TrimRight(n.Text, " \t")) } +func (n *Meta) serialize(w io.Writer) { + fmt.Fprintf(w, "meta ") + n.Value.serialize(w) + fmt.Fprintf(w, "\n") +} + func (n *Include) serialize(w io.Writer) { fmt.Fprintf(w, "include <%v>\n", n.File.Value) } diff --git a/pkg/ast/parser.go b/pkg/ast/parser.go index 20898169e..b2709e2ed 100644 --- a/pkg/ast/parser.go +++ b/pkg/ast/parser.go @@ -128,7 +128,10 @@ func (p *parser) parseTop() Node { return p.parseResource() case tokIdent: name := p.parseIdent() - if name.Name == "type" { + switch name.Name { + case "meta": + return p.parseMeta() + case "type": return p.parseTypeDef() } switch p.tok { @@ -190,6 +193,13 @@ func (p *parser) parseComment() *Comment { return c } +func (p *parser) parseMeta() *Meta { + return &Meta{ + Pos: p.pos, + Value: p.parseType(), + } +} + func (p *parser) parseDefine() *Define { pos0 := p.pos p.consume(tokDefine) diff --git a/pkg/ast/testdata/all.txt b/pkg/ast/testdata/all.txt index c9398a830..7b5fa7c37 100644 --- a/pkg/ast/testdata/all.txt +++ b/pkg/ast/testdata/all.txt @@ -1,4 +1,7 @@ # 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. +meta noextract +meta arches["foo", "bar", "386"] + incdir diff --git a/pkg/ast/testdata/errors.txt b/pkg/ast/testdata/errors.txt index 463158756..2b022c149 100644 --- a/pkg/ast/testdata/errors.txt +++ b/pkg/ast/testdata/errors.txt @@ -5,6 +5,10 @@ foo ### unexpected '\n', expecting '(', '{', '[', '=' % ### illegal character U+0025 '%' +meta ### unexpected '\n', expecting int, identifier, string +meta: foo ### unexpected ':', expecting int, identifier, string +meta foo, bar ### unexpected ',', expecting '\n' + int_flags0 = 0, 0x1, 0xab int_flags1 = 123ab0x ### bad integer "123ab0x" int_flags1 == 0, 1 ### unexpected '=', expecting int, identifier, string @@ -93,4 +97,4 @@ s5 { f0 int8 ( ### unexpected '\n', expecting int, identifier, string s6 { - f0 int8 () ### unexpected ')', expecting int, identifier, string \ No newline at end of file + f0 int8 () ### unexpected ')', expecting int, identifier, string diff --git a/pkg/ast/walk.go b/pkg/ast/walk.go index 84a3c67c3..b187769f3 100644 --- a/pkg/ast/walk.go +++ b/pkg/ast/walk.go @@ -35,6 +35,10 @@ func (n *Ident) walk(cb func(Node)) {} func (n *String) walk(cb func(Node)) {} func (n *Int) walk(cb func(Node)) {} +func (n *Meta) walk(cb func(Node)) { + cb(n.Value) +} + func (n *Include) walk(cb func(Node)) { cb(n.File) } -- cgit mrf-deployment