From 6e87cfa299c98d36e79e8b8718a4126899a3ba2f Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 17 Jan 2025 10:39:49 +0100 Subject: pkg/compiler: fix struct layout bug Currently we have a bug in struct layout that affects some corner cases that involve recursive structs. The result of this bug is that we use wrong alignment 1 (not yet calculated) for some structs when calculating layout of other structs. The root cause of this bug is that we calculate struct alignment too early in typeStruct.Gen when structs are not yet laid out. For this reason we moved struct size calculation to the later phase (after compiler.layoutStruct). Move alignment calculation from typeStruct.Gen to compiler.layoutStruct to fix this. --- tools/syz-declextract/testdata/types.c | 3 +-- tools/syz-declextract/testdata/types.c.json | 12 +++++++++++- tools/syz-declextract/testdata/types.c.txt | 6 +++++- 3 files changed, 17 insertions(+), 4 deletions(-) (limited to 'tools') diff --git a/tools/syz-declextract/testdata/types.c b/tools/syz-declextract/testdata/types.c index 014318d42..7a98394c1 100644 --- a/tools/syz-declextract/testdata/types.c +++ b/tools/syz-declextract/testdata/types.c @@ -46,8 +46,7 @@ struct various { }; struct recursive { - // This is not handled properly yet. - // struct various various; + struct various various; }; SYSCALL_DEFINE1(types_syscall, struct anon_struct* p, struct empty_struct* y, diff --git a/tools/syz-declextract/testdata/types.c.json b/tools/syz-declextract/testdata/types.c.json index 99d0adc2b..6c479dffb 100644 --- a/tools/syz-declextract/testdata/types.c.json +++ b/tools/syz-declextract/testdata/types.c.json @@ -758,7 +758,17 @@ }, { "name": "recursive", - "align": 1 + "byte_size": 64, + "align": 32, + "fields": [ + { + "name": "various", + "counted_by": -1, + "type": { + "struct": "various" + } + } + ] }, { "name": "various", diff --git a/tools/syz-declextract/testdata/types.c.txt b/tools/syz-declextract/testdata/types.c.txt index ea95b4bb5..085aee017 100644 --- a/tools/syz-declextract/testdata/types.c.txt +++ b/tools/syz-declextract/testdata/types.c.txt @@ -97,9 +97,13 @@ packed_t$auto { y int32 } [packed, align[32]] +recursive$auto { + various various$auto +} + various$auto { recursive ptr[inout, various$auto, opt] - next ptr[inout, auto_aligner[1], opt] + next ptr[inout, recursive$auto, opt] packed packed_t$auto } -- cgit mrf-deployment