From d0405298b24db0e2a6b2abfdc8c7e5ebbe49d1a0 Mon Sep 17 00:00:00 2001 From: Hrutvik Kanabar Date: Tue, 25 Oct 2022 10:13:51 +0000 Subject: prog, pkg/compiler: add `BufferCompressed` buffer type & `compressed_image` builtin Create the `BufferCompressed` kind of `BufferType`, which will be used to represent compressed data. Create the corresponding `compressed_image` syzlang builtin, which is backed by `BufferCompressed`. For now, no syscalls use this feature - this will be introduced in future commits. We have to be careful to decompress the data before mutating, and re-compress before storing. We make sure that any deserialised `BufferCompressed` data is valid too. `BufferCompressed` arguments are mutated using a generic heatmap. In future, we could add variants of `BufferCompressed` or populate the `BufferType` sub-kind, using it to choose different kinds of heatmap for different uncompressed data formats. Various operations on compressed data must be forbidden, so we check for `BufferCompressed` in key places. We also have to ensure `compressed_image` can only be used in syscalls that are marked `no_{generate,minimize}`. Therefore, we add a generic compiler check which allows type descriptions to require attributes on the syscalls which use them. --- pkg/compiler/check.go | 40 ++++++++++++++++++++++++++++++++++++ pkg/compiler/testdata/all.txt | 16 +++++++++++++++ pkg/compiler/testdata/errors.txt | 3 +++ pkg/compiler/testdata/errors2.txt | 14 +++++++++++++ pkg/compiler/types.go | 43 +++++++++++++++++++++++++++++++-------- 5 files changed, 108 insertions(+), 8 deletions(-) (limited to 'pkg/compiler') diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go index 402e9a84e..4b2dd1330 100644 --- a/pkg/compiler/check.go +++ b/pkg/compiler/check.go @@ -341,8 +341,48 @@ func (comp *compiler) checkAttributeValues() { comp.error(f.Pos, "%v type must not be used as output", f.Type.Ident) } } + case *ast.Call: + attrNames := make(map[string]bool) + descAttrs := comp.parseAttrs(callAttrs, n, n.Attrs) + for desc := range descAttrs { + attrNames[prog.CppName(desc.Name)] = true + } + + checked := make(map[string]bool) + for _, a := range n.Args { + comp.checkRequiredCallAttrs(n, attrNames, a.Type, checked) + } + } + } +} + +func (comp *compiler) checkRequiredCallAttrs(call *ast.Call, callAttrNames map[string]bool, + t *ast.Type, checked map[string]bool) { + desc := comp.getTypeDesc(t) + for attr := range desc.RequiresCallAttrs { + if !callAttrNames[attr] { + comp.error(call.Pos, "call %v refers to type %v and so must be marked %s", call.Name.Name, t.Ident, attr) } } + + if desc == typeStruct { + s := comp.structs[t.Ident] + // Prune recursion, can happen even on correct tree via opt pointers. + if checked[s.Name.Name] { + return + } + checked[s.Name.Name] = true + fields := s.Fields + for _, fld := range fields { + comp.checkRequiredCallAttrs(call, callAttrNames, fld.Type, checked) + } + } else if desc == typeArray { + typ := t.Args[0] + comp.checkRequiredCallAttrs(call, callAttrNames, typ, checked) + } else if desc == typePtr { + typ := t.Args[1] + comp.checkRequiredCallAttrs(call, callAttrNames, typ, checked) + } } func (comp *compiler) checkLenTargets() { diff --git a/pkg/compiler/testdata/all.txt b/pkg/compiler/testdata/all.txt index d03e35def..6fd9f7cc6 100644 --- a/pkg/compiler/testdata/all.txt +++ b/pkg/compiler/testdata/all.txt @@ -322,3 +322,19 @@ struct$fmt0 { } flags_with_one_value = 0 + +# Compressed images. + +struct_compressed { + f0 compressed_image +} + +union_compressed [ + f0 compressed_image + f1 int32 +] [varlen] + +compressed$1(a compressed_image) (no_generate, no_minimize) +compressed$2(a ptr[in, compressed_image]) (no_generate, no_minimize) +compressed$3(a ptr[in, struct_compressed]) (no_generate, no_minimize) +compressed$4(a ptr[in, union_compressed]) (no_generate, no_minimize) \ No newline at end of file diff --git a/pkg/compiler/testdata/errors.txt b/pkg/compiler/testdata/errors.txt index 1201fa361..9d625a34b 100644 --- a/pkg/compiler/testdata/errors.txt +++ b/pkg/compiler/testdata/errors.txt @@ -422,3 +422,6 @@ union$directions [ f1 int32 (in) ### unknown arg/field f1 attribute in f2 int32 (out) ### unknown arg/field f2 attribute out ] + + +compressed$test(a int32) compressed_image ### compressed_image can't be syscall return diff --git a/pkg/compiler/testdata/errors2.txt b/pkg/compiler/testdata/errors2.txt index 482314b4a..3cd63ebd2 100644 --- a/pkg/compiler/testdata/errors2.txt +++ b/pkg/compiler/testdata/errors2.txt @@ -407,3 +407,17 @@ struct$out0 { f2 proc[0, 1, int32] (out) ### proc type must not be used as output f3 bytesize[f1, int32] (out) ### bytesize type must not be used as output } + +struct_non_generatable { + f0 ptr[in, array[compressed_image]] +} + +union_non_minimizable [ + f0 struct_non_generatable + f2 int32 +] + +foo$non_generatable(a compressed_image) (no_minimize) ### call foo$non_generatable refers to type compressed_image and so must be marked no_generate +foo$non_minimizable(a compressed_image) (no_generate) ### call foo$non_minimizable refers to type compressed_image and so must be marked no_minimize +foo$non_generatable_via_struct(a ptr[in, struct_non_generatable]) (no_minimize) ### call foo$non_generatable_via_struct refers to type compressed_image and so must be marked no_generate +foo$non_minimizable_via_union(a ptr[in, union_non_minimizable]) (no_generate) ### call foo$non_minimizable_via_union refers to type compressed_image and so must be marked no_minimize diff --git a/pkg/compiler/types.go b/pkg/compiler/types.go index 9e75de18d..095791adb 100644 --- a/pkg/compiler/types.go +++ b/pkg/compiler/types.go @@ -16,14 +16,15 @@ import ( // typeDesc is arg/field type descriptor. type typeDesc struct { - Names []string - CanBeTypedef bool // can be type alias target? - CantBeOpt bool // can't be marked as opt? - CantBeOut bool // can't be used as an explicitly output argument - NeedBase bool // needs base type when used as field? - MaxColon int // max number of colons (int8:2) on fields - OptArgs int // number of optional arguments in Args array - Args []namedArg // type arguments + Names []string + CanBeTypedef bool // can be type alias target? + CantBeOpt bool // can't be marked as opt? + CantBeOut bool // can't be used as an explicitly output argument? + NeedBase bool // needs base type when used as field? + MaxColon int // max number of colons (int8:2) on fields + OptArgs int // number of optional arguments in Args array + Args []namedArg // type arguments + RequiresCallAttrs map[string]bool // calls using this type must have these attrs. // CanBeArgRet returns if this type can be syscall argument/return (false if nil). CanBeArgRet func(comp *compiler, t *ast.Type) (bool, bool) // CanBeResourceBase returns if this type can be a resource base type (false if nil. @@ -830,6 +831,31 @@ var typeFmtFormat = &typeArg{ Kind: kindIdent, } +// typeCompressedImage is used for compressed disk images. +var typeCompressedImage = &typeDesc{ + Names: []string{"compressed_image"}, + CantBeOpt: true, + CantBeOut: true, + RequiresCallAttrs: map[string]bool{ + "no_generate": true, + "no_minimize": true, + }, + CanBeArgRet: func(comp *compiler, t *ast.Type) (bool, bool) { + return true, false + }, + Varlen: func(comp *compiler, t *ast.Type, args []*ast.Type) bool { + return true + }, + Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) prog.Type { + base.TypeSize = 0 + base.TypeAlign = 1 + return &prog.BufferType{ + TypeCommon: base.TypeCommon, + Kind: prog.BufferCompressed, + } + }, +} + // typeArgType is used as placeholder for any type (e.g. ptr target type). var typeArgType = &typeArg{} @@ -1116,6 +1142,7 @@ func init() { typeText, typeString, typeFmt, + typeCompressedImage, } for _, desc := range builtins { for _, name := range desc.Names { -- cgit mrf-deployment