add BUILD_BUG_DECL assertion (for 3.4??)
cover add BUILD_BUG_DECL assertion (for 3.4??)
0001-bug.h-add-BUILD_BUG_DECL-usable-at-file-scope.patch 0002-bug.h-add-test-demo-module.patch (DONT APPLY)
this patch (0001) adds new bug.h macro, BUILD_BUG_DECL(name, cond), which unlike other *BUG* assertions is usable at file scope. Its primary purpose is to enforce identical sizes of 2 separate arrays, which but for considerations of packing/padding/section, would be together in a struct.
const char const *names[] = { "bart", "lisa", "homer", "marge" }; int a[] = {1,2,3,4};
int b[] = {1,2,3,5};
long d[] = {1,2};
BUILD_BUG_DECL(foo, ARRAY_SIZE(a) != ARRAY_SIZE(b));BUILD_BUG_DECL(buz, sizeof(a) != sizeof(b)); // goodBUILD_BUG_DECL(a, sizeof(a) != sizeof(d)); // ok on x32, error x64BUILD_BUG_DECL(b, ARRAY_SIZE(a) != ARRAY_SIZE(names)); // good
macro expands as:
static __attribute__ ((__section__(".init.data"))) struct {
int BUILD_BUG_DECL_buz[1 - 2*!!(sizeof(a) != sizeof(b))];
} BUILD_BUG_DECL_buz[0] __attribute__((unused));I wanted to ask for this in 3.4, but see CAVEATS
= its low risk (famous last words)
= has many immediate bug-prevention applications
For example (perhaps a bad one, I only eyeballed the tables themselves): in drivers/net/wireless/b43/tables_lpphy.c, these 2 tables are the same size. Should that be enforced ??
static const u16 lpphy_rev0_ofdm_cck_gain_table[] = {...} static const u16 lpphy_rev1_ofdm_cck_gain_table[] = {...}
Whether or not this example is appropriate, I think its tautological that there are pairs of arrays in the code that must match on length for proper operation; this would enforce them, with trivial patches.
= other *BUG* assertions use do{}while, so they cant work at file scope.
BUILD_BUG_DECL is declarative, so it does work at file scope. It declares an unused 0 length array in __initdata, so shouldnt create storage.
I sent a similar patch previously as part of dynamic-debug patches, Jason Baron liked it, Rusty Russell thought it was redundant and not neccessary in that context. I dont think I adequately explained (or even mentioned) the file-scope aspect. Im not cc'g them, theyre both *busy* atm.
CAVEATS
Macro includes __attribute__((unused)), but it seems ineffective. I also tried deref, but compiler (cc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2)) warned about it, so I yanked it.
Macro does seem to create storage, so I put it in __initdata. However, referencing it from a non--init function doesnt give a compiler warning. Further, calling that function after boot gives a paging-request error, and traceback. See patch 0002.
IIUC, this is expected, since __initdata has been dropped after boot is complete. What I dont get is why the compiler allowed the references; I've seen __initdata/section warnings for similar problems in the past.
thanks
Jim Cromie
=?y?q?=5BPATCH=202/2=5D=20bug=2Eh=3A=20add=20test/demo=20module?=
add drivers/misc/build-asserts.c to test BUILD_BUG_DECL.
BUILD_BUG_DECL works as intended, but __attribute((unused)) appears to be ineffective in the macro:
Code run at __init time is able to reference the storage w/o warnings (no section complaints, even though those functions are not explicitly marked as __init - I guess thats inferred..).
Code run from mod-param getter/setter callbacks pukes badly. I guess this is to be expected, since __initdata is dropped once boot is complete. But why no compile warnings ??
root@voyage:~# cat /sys/module/build_asserts/parameters/* build_asserts.cbint_get: got bufp:c65cd000='' kp:c8bb967c build_asserts.cbint_get: kp-name:cbint build_asserts.cbint_get: kp-arg:c8bb9113 build_asserts.cbint_get: kp-ops:c8bb973c build_asserts.cbint_get: kp-ops-set:c8bb9179 build_asserts.cbint_get: kp-ops-get:c8bb9000 build_asserts.cbint_get: cbint_get me:c8bb9000 BUG: unable to handle kernel paging request at c8bbb000 IP: [<c8bb90d8>] cbint_get+0xd8/0x113 [build_asserts] *pde = 06c76067 *pte = 00000000 Oops: 0000 [#1] PREEMPT Modules linked in: build_asserts scx200_gpio scx200_hrt pc8736x_gpio nsc_gpio pc87360 hwmon_vid scx200_acb acx_mac80211(O) arc4 rtl8180 mac80211 eeprom_93cx6 scx200 cfg80211 ohci_hcd pata_sc1200 [last unloaded: build_asserts]
Pid: 1364, comm: cat Tainted: G O 3.4.0-rc1-ske+ #24
EIP: 0060:[<c8bb90d8>] EFLAGS: 00010282 CPU: 0
EIP is at cbint_get+0xd8/0x113 [build_asserts]
EAX: 00000035 EBX: c65cd000 ECX: c7450000 EDX: 00000003
ESI: c8bb967c EDI: c65cd000 EBP: c7451efc ESP: c7451ee0
DS: 007b ES: 007b FS: 0000 GS: 00e0 SS: 0068
CR0: 8005003b CR2: c8bbb000 CR3: 066b7000 CR4: 00000000
DR0: 00000000 DR1: 00000000 DR2: 00000000 DR3: 00000000
DR6: ffff0ff0 DR7: 00000400
Process cat (pid: 1364, ti=c7450000 task=c6524f80 task.ti=c7450000)
Stack: c8bb945e c8bb96bc c8bb9000 c65cd000 c8bb967c ffffffff c675c270 c7451f10 c012f536 c675c270 c8bb9790 c012f503 c7451f24 c012f35b c6507740 c60deb40 c043eb28 c7451f70 c02010d5 c6753680 00000000 c7451f44 c027fe90 c6753680 Call Trace: [<c8bb9000>] ? 0xc8bb8fff [<c012f536>] param_attr_show+0x33/0x57 [<c012f503>] ? __kernel_param_unlock+0x14/0x14 [<c012f35b>] module_attr_show+0x21/0x26 [<c02010d5>] sysfs_read_file+0x9e/0x154 [<c027fe90>] ? security_file_permission+0x27/0x2b [<c01bb5ee>] ? rw_verify_area+0xd0/0xf2 [<c01bb956>] vfs_read+0x8d/0xd3 [<c0201037>] ? sysfs_open_file+0x1fa/0x1fa [<c01bb9de>] sys_read+0x42/0x63 [<c043592c>] syscall_call+0x7/0xb Code: 04 24 3a 94 bb c8 89 44 24 08 e8 89 7c 87 f7 c7 44 24 08 00 90 bb c8 c7 44 24 04 bc 96 bb c8 c7 04 24 5e 94 bb c8 e8 6d 7c 87 f7 <a1> 00 b0 bb c8 c7 44 24 04 bc 96 bb c8 89 44 24 0c 89 44 24 08 EIP: [<c8bb90d8>] cbint_get+0xd8/0x113 [build_asserts] SS:ESP 0068:c7451ee0 CR2: 00000000c8bbb000 ---[ end trace bfbcc6aee803d03b ]---
--- drivers/misc/Makefile | 2 + drivers/misc/build-asserts.c | 239 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 241 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/build-asserts.c
drivers/misc/Makefile +2 -0
@@ -49,3 +49,5 @@ obj-y += carma/ obj-$(CONFIG_USB_SWITCH_FSA9480) += fsa9480.o obj-$(CONFIG_ALTERA_STAPL) +=altera-stapl/ obj-$(CONFIG_MAX8997_MUIC) += max8997-muic.o + +obj-m += build-asserts.o
drivers/misc/build-asserts.c new file +239 -0
239 lines not on this page · raw message
Signature · 1 line
1.7.7.6
PATCH 1/2 bug.h: add BUILD_BUG_DECL, usable at file scope
In reply to Jim CromieThis adds BUILD_BUG_DECL, primarily to check sizes of file-scoped arrays etc:
const char const *names[] = { "bart", "lisa", "homer", "marge" }; int a[] = {1,2,3,4}, b[] = {1,2,3,5}, c[] = {1,2}
long d[] = {1,2};
BUILD_BUG_DECL(Luke, sizeof(a) != sizeof(d)); // ok, but iffy usage
BUILD_BUG_DECL(Han, sizeof(a) != sizeof(b)); // good
BUILD_BUG_DECL(Obi, ARRAY_SIZE(a) != ARRAY_SIZE(b)); // better
BUILD_BUG_DECL(Yoda, ARRAY_SIZE(a) != ARRAY_SIZE(names)); // good, on different types
BUILD_BUG_DECL(Darth, sizeof(a) != sizeof(c)); // compile err
example 1 expands as:
static __attribute__ ((__section__(".init.data"))) struct {
int BUILD_BUG_DECL_Luke[1 - 2*!!(sizeof(a) != sizeof(b))];
} BUILD_BUG_DECL_Luke[0] __attribute__((unused));The name parameter distinguishes multiple uses in the same scope, but is otherwise arbitrary. You can reuse the name of one of the checked vars, or pick something easy to find on the rare occaision when the assertion breaks the build.
example 5 yields:
error: size of array ‘BUILD_BUG_DECL_Darth’ is negative
--- include/linux/bug.h | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-)
include/linux/bug.h +15 -0
@@ -17,6 +17,7 @@ struct pt_regs;#define BUILD_BUG_ON_NULL (e) ((void*)0)#define BUILD_BUG_ON (condition)#define BUILD_BUG () (0) +#define BUILD_BUG_DECL (name, condition)#else /* __CHECKER__ */ /* Force a compilation error if a constant expression is not a power of 2 */
@@ -70,6 +71,20 @@ extern int __build_bug_on_failed;__build_bug_failed (); \ } while (0) +/** +* BUILD_BUG_DECL - check declared objects +* @name: distinguishes multiple uses at same scope. +* @cond: false expr, typically like sizeof(a) != sizeof(b) +* +* This works at file-scope too, and supports checks like: +* BUILD_BUG_DECL(foo, sizeof(a) != sizeof(b)); +* BUILD_BUG_DECL(id_strings, ARRAY_SIZE(id_strings) != ARRAY_SIZE(id_vals)); + */ +#define BUILD_BUG_DECL (name, cond) \ + static __initdata struct { \ + int BUILD_BUG_DECL_ ## name[1 - 2*!!(cond)]; \ + } BUILD_BUG_DECL_ ##name[0] __attribute__((unused)) +#endif /* __CHECKER__ */ #ifdef CONFIG_GENERIC_BUG
Signature · 1 line
1.7.7.6
Quoted 16 lines · Jim Cromie
this patch (0001) adds new
bug.hmacro,BUILD_BUG_DECL(name, cond), which unlike other *BUG* assertions is usable at file scope. Its primary purpose is to enforce identical sizes of 2 separate arrays, which but for considerations of packing/padding/section, would be together in a struct.const char const *names[] = { "bart", "lisa", "homer", "marge" }; int a[] = {1,2,3,4};
int b[] = {1,2,3,5};
long d[] = {1,2};
BUILD_BUG_DECL(foo, ARRAY_SIZE(a) != ARRAY_SIZE(b));BUILD_BUG_DECL(buz, sizeof(a) != sizeof(b)); // goodBUILD_BUG_DECL(a, sizeof(a) != sizeof(d)); // ok on x32, error x64BUILD_BUG_DECL(b, ARRAY_SIZE(a) != ARRAY_SIZE(names)); // good
Quoted 5 lines
macro expands as:
static __attribute__ ((__section__(".init.data"))) struct { int BUILD_BUG_DECL_buz[1 - 2*!!(sizeof(a) != sizeof(b))]; } BUILD_BUG_DECL_buz[0] __attribute__((unused));
If possible, it might be better to wrap the
declarations themselves in a macro that ensures
the sizes are the same.
Something like:
declare_same_size_arrays(
typeof array1[] = {...},
typeof array2[] = {...});
Quoted 28 lines · Joe Perches
Quoted 16 lines · Jim Cromie
this patch (0001) adds new
bug.hmacro,BUILD_BUG_DECL(name, cond), which unlike other *BUG* assertions is usable at file scope. Its primary purpose is to enforce identical sizes of 2 separate arrays, which but for considerations of packing/padding/section, would be together in a struct.const char const *names[] = { "bart", "lisa", "homer", "marge" }; int a[] = {1,2,3,4};
int b[] = {1,2,3,5};
long d[] = {1,2};
BUILD_BUG_DECL(foo, ARRAY_SIZE(a) != ARRAY_SIZE(b));BUILD_BUG_DECL(buz, sizeof(a) != sizeof(b)); // goodBUILD_BUG_DECL(a, sizeof(a) != sizeof(d)); // ok on x32, error x64BUILD_BUG_DECL(b, ARRAY_SIZE(a) != ARRAY_SIZE(names)); // goodQuoted 5 lines
macro expands as:
static__attribute__((__section__(".init.data"))) struct { int BUILD_BUG_DECL_buz[1 - 2*!!(sizeof(a) != sizeof(b))]; } BUILD_BUG_DECL_buz[0] __attribute__((unused));If possible, it might be better to wrap the
declarations themselves in a macro that ensures
the sizes are the same.
Quoted 8 lines
Something like:
declare_same_size_arrays(
typeof array1[] = {...},
typeof array2[] = {...}
);
Unless Im mis-reading you, this has a couple disadvantages:
- bigger patches where its added.
granted these are mostly whitespace, but theyre less trivial to inspect.
- not useful for array definitions which are not contiguous granted thats a minority case, and the definitions could often be moved, but not always.
WAG: If *80211 code were to export tables that drivers could use, (say standardized frequency, channel stuff)
it would be helpful if the drivers could assert that their private tables match the exported ones. (I havent looked in *80211, or tried using BUILD_BUG_DECL this way)
Do you see advantages other than stylistic ones ?
Quoted 38 lines · Jim Cromie
Quoted 28 lines · Joe Perches
Quoted 16 lines · Jim Cromie
this patch (0001) adds new
bug.hmacro,BUILD_BUG_DECL(name, cond), which unlike other *BUG* assertions is usable at file scope. Its primary purpose is to enforce identical sizes of 2 separate arrays, which but for considerations of packing/padding/section, would be together in a struct.const char const *names[] = { "bart", "lisa", "homer", "marge" }; int a[] = {1,2,3,4};
int b[] = {1,2,3,5};
long d[] = {1,2};
BUILD_BUG_DECL(foo, ARRAY_SIZE(a) != ARRAY_SIZE(b));BUILD_BUG_DECL(buz, sizeof(a) != sizeof(b)); // goodBUILD_BUG_DECL(a, sizeof(a) != sizeof(d)); // ok on x32, error x64BUILD_BUG_DECL(b, ARRAY_SIZE(a) != ARRAY_SIZE(names)); // goodQuoted 5 lines
macro expands as:
static __attribute__ ((__section__(".init.data"))) struct { int BUILD_BUG_DECL_buz[1 - 2*!!(sizeof(a) != sizeof(b))]; } BUILD_BUG_DECL_buz[0] __attribute__((unused));If possible, it might be better to wrap the
declarations themselves in a macro that ensures
the sizes are the same.Quoted 8 lines
Something like:
declare_same_size_arrays(
typeof array1[] = {...}, typeof array2[] = {...});
Quoted 4 lines
Unless Im mis-reading you, this has a couple disadvantages:
- bigger patches where its added.
granted these are mostly whitespace, but theyre less trivial to inspect.
Not a problem in my view, it moves the
related declarations closer together.
Quoted 3 lines
- not useful for array definitions which are not contiguous granted thats a minority case, and the definitions could often be moved, but not always.
Discontiguous array definitions must be ugly.
Quoted 1 line
Do you see advantages other than stylistic ones ?
Not really.
Contiguous declarations.
No need for other markings.
Seems useful enough.
Quoted 32 lines · Joe Perches
Quoted 26 lines · Jim Cromie
Quoted 16 lines · Joe Perches
Quoted 8 lines · Jim Cromie
this patch (0001) adds new
bug.hmacro,BUILD_BUG_DECL(name, cond), which unlike other *BUG* assertions is usable at file scope. Its primary purpose is to enforce identical sizes of 2 separate arrays, which but for considerations of packing/padding/section, would be together in a struct.
BUILD_BUG_DECL(b, ARRAY_SIZE(a) != ARRAY_SIZE(names)); // goodIf possible, it might be better to wrap the
declarations themselves in a macro that ensures
the sizes are the same.Quoted 8 lines
Something like:
declare_same_size_arrays(
typeof array1[] = {...},
typeof array2[] = {...}
);Quoted 4 lines
Unless Im mis-reading you, this has a couple disadvantages:
- bigger patches where its added.
granted these are mostly whitespace, but theyre less trivial to inspect.
Quoted 8 lines
Not a problem in my view, it moves the
related declarations closer together.Quoted 3 lines
- not useful for array definitions which are not contiguous granted thats a minority case, and the definitions could often be moved, but not always.
Discontiguous array definitions must be ugly.
Ugly ?
too pejorative IMO
each array defn is a single statement.
there may be functions between the 2 defns being compared.
Maybe not ideal,
Quoted 10 lines
Quoted 2 lines
Do you see advantages other than stylistic ones ?
Not really.
Contiguous declarations.
No need for other markings.Seems useful enough.
OK. Id expect your construct to be built upon mine, we'd still need to start with something.
Also, mine is usable for things yours isnt.
I dont have a good example, but a simple/silly one is:
BUILD_BUG_DECL( wifi_channels_must_be_14,
ARRAY_SIZE(channels_table) == 14 );
Quoted 4 lines · Jim Cromie
Quoted 1 line · Joe Perches
Discontiguous array definitions must be ugly.
Ugly ?
too pejorative IMO
Ugly code is not a pejorative, it's more an
artifact of creation and always a beholder
issue.
Quoted 1 line
each array defn is a single statement.
I thought you meant the array entries not
the arrays themselves.
Quoted 15 lines
there may be functions between the 2 defns being compared.
Maybe not ideal,
Quoted 10 lines
Quoted 2 lines
Do you see advantages other than stylistic ones ?
Not really.
Contiguous declarations.
No need for other markings.Seems useful enough.
Quoted 8 lines
OK. Id expect your construct to be built upon mine, we'd still need to start with something.
Also, mine is usable for things yours isnt.
I dont have a good example, but a simple/silly one is:BUILD_BUG_DECL(
wifi_channels_must_be_14,ARRAY_SIZE(channels_table) == 14 );
Do what you think best.
I think it's a solution for a relatively
minor problem.
The BUILD_BUG_DECL marking might be improved.BUILD_BUG_DECL might be a bit short or not
descriptive enough.
Maybe BUILD_BUG_DIFF_SIZE or BUILD_BUG_SIZE_NE?