aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2019-12-18 14:44:12 +0100
committerDmitry Vyukov <dvyukov@google.com>2019-12-18 14:44:12 +0100
commit73c38454383e0daeb72d08b990e49bf4bd42ba87 (patch)
tree93ef810b42566c81c2881fa17feefc8aea4cf60f /pkg
parent42dc69277628779d9d1213f116665d86f5edc49b (diff)
pkg/compiler: fix incorrect alignment calculation for padding
We assumed that for ConstType alignment is equal to size, which is perfectly reasonable for normal int8/16/32/64/ptr. However, padding is also represented by ConstType of arbitrary size, so if we added 157 bytes of padding that becomes alignment of the padding field and as the result of the whole struct. This affects very few structs, but quite radically and quite important structs. Discovered thanks to syz-check. Update #590
Diffstat (limited to 'pkg')
-rw-r--r--pkg/compiler/gen.go12
-rw-r--r--pkg/compiler/testdata/all.txt11
2 files changed, 17 insertions, 6 deletions
diff --git a/pkg/compiler/gen.go b/pkg/compiler/gen.go
index bf7b7a46c..4b50958ac 100644
--- a/pkg/compiler/gen.go
+++ b/pkg/compiler/gen.go
@@ -349,15 +349,17 @@ func (comp *compiler) addAlignment(fields []prog.Type, varlen, packed bool, alig
}
func (comp *compiler) typeAlign(t0 prog.Type) uint64 {
- switch t0.(type) {
- case *prog.IntType, *prog.ConstType, *prog.LenType, *prog.FlagsType, *prog.ProcType,
+ switch t := t0.(type) {
+ case *prog.IntType, *prog.LenType, *prog.FlagsType, *prog.ProcType,
*prog.CsumType, *prog.PtrType, *prog.VmaType, *prog.ResourceType:
return t0.Size()
+ case *prog.ConstType:
+ if t.IsPad {
+ return 1
+ }
+ return t.Size()
case *prog.BufferType:
return 1
- }
-
- switch t := t0.(type) {
case *prog.ArrayType:
return comp.typeAlign(t.Type)
case *prog.StructType:
diff --git a/pkg/compiler/testdata/all.txt b/pkg/compiler/testdata/all.txt
index 7df52dd2f..380d65319 100644
--- a/pkg/compiler/testdata/all.txt
+++ b/pkg/compiler/testdata/all.txt
@@ -256,7 +256,16 @@ s1 {
f1 int8
} [size[C2]]
-foo$s0(a ptr[in, s0], b ptr[in, s1])
+s2 {
+ f1 int8
+ f2 s3
+} [size[101]]
+
+s3 {
+ f1 int8
+} [size[100]]
+
+foo$s0(a ptr[in, s0], b ptr[in, s1], c ptr[in, s2])
# Unions.