From 2b76b86c449fff4c26410164052c32f3b9cf56fe Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 17 Jan 2025 10:39:47 +0100 Subject: tools/syz-declextract: fix empty structs and arrays This fixes 2 bugs: 1. We completly remove empty structs, but they can have effect on parent struct layout if they have >1 alignment. Replace empty structs with a special auto_aligner type that preserves alignment. 2. Arrays of 0 size are currently emitted as dynamically-sized (we assume 0 size means "this is not a const-size array"). Add separate IsConstSize flag for arrays that marks const-size arrays. Additionally cross-check that generated structs have exactly the same size/alignment as the corresponding C structs. This allows to catch the above bugs. --- pkg/declextract/serialization.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'pkg/declextract/serialization.go') diff --git a/pkg/declextract/serialization.go b/pkg/declextract/serialization.go index 571336097..15e4a34a8 100644 --- a/pkg/declextract/serialization.go +++ b/pkg/declextract/serialization.go @@ -32,6 +32,10 @@ type auto_union[INFERRED, RAW] [ raw RAW ] +type auto_aligner[N] { + void void +} [align[N]] + ` func (ctx *context) fmt(msg string, args ...any) { @@ -76,6 +80,10 @@ func (ctx *context) serializeEnums() { func (ctx *context) serializeStructs() { for _, str := range ctx.Structs { + // Empty structs are not supported, but we also shouldn't emit references to them. + if str.ByteSize == 0 { + continue + } delims := "{}" if str.IsUnion { delims = "[]" @@ -89,8 +97,8 @@ func (ctx *context) serializeStructs() { if str.IsPacked { attrs = append(attrs, "packed") } - if str.Align != 0 { - attrs = append(attrs, fmt.Sprintf("align[%v]", str.Align)) + if str.AlignAttr != 0 { + attrs = append(attrs, fmt.Sprintf("align[%v]", str.AlignAttr)) } if len(attrs) != 0 { ctx.fmt(" [%v]", strings.Join(attrs, ", ")) -- cgit mrf-deployment