Skip to content

Commit 34e829c

Browse files
committed
fix_*_init.cocci: fix missing desgnated inits
The 'rcp_hal_init' filter needs recountdiff from patchutils! Signed-off-by: Mathias Krause <minipli@grsecurity.net>
1 parent 4451786 commit 34e829c

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/nvidia/cocci.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ pfunc_list_filter() {
5050
esac
5151
}
5252

53+
rpc_hal_init_filter() {
54+
# What a hack, take 2!
55+
#
56+
# To modify structure initialization to use designated initializers, we
57+
# post-process a cocci patch that simply removes the init to inject
58+
# lines that add the correct initialization, based on the fact that the
59+
# field member is mentioned in a trailing comment. To make that a valid
60+
# diff, we fix it up using recountdiff.
61+
case "$1" in
62+
pre) ;;
63+
diff) sed 's|^-\( *\)\([^ ]\+\), *// *\([^ ]\+\)$|&\n+\1.\3 = \2,|' | recountdiff; ;;
64+
post) ;;
65+
esac
66+
}
67+
5368
null_filter() {
5469
case "$1" in
5570
pre) ;;
@@ -82,7 +97,9 @@ fi
8297
SCRIPT=$1; shift
8398
SPATCH=$1; shift
8499

85-
if ! check_prog "$SPATCH" coccinelle; then
100+
if ! check_prog "$SPATCH" coccinelle || \
101+
! check_prog recountdiff patchutils;
102+
then
86103
echo >&2 "error: missing required programs!"
87104
exit 2
88105
fi
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Coccinelle script to fix structure initialization to use designated
2+
// initializer to make these compatible with Linux's RANDSTRUCT.
3+
//
4+
// Here we handle instances of HAL_IFACE_SETUP:
5+
//
6+
// typedef struct {
7+
// void (*rpcHalIfacesSetupFn)(PRPC_HAL_IFACES pRpcHal);
8+
// void (*rpcstructurecopyHalIfacesSetupFn)(PRPCSTRUCTURECOPY_HAL_IFACES pRpcstructurecopyHal);
9+
// } HAL_IFACE_SETUP
10+
//
11+
// We simply hard-code the field names.
12+
//
13+
// (c) 2026 Open Source Security, Inc. All Rights Reserved.
14+
15+
@@
16+
identifier his;
17+
identifier f1, f2;
18+
@@
19+
static HAL_IFACE_SETUP his = {
20+
- f1,
21+
- f2,
22+
+ .rpcHalIfacesSetupFn = f1,
23+
+ .rpcstructurecopyHalIfacesSetupFn = f2,
24+
};

src/nvidia/fix_rpc_hal_init.cocci

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Coccinelle script to fix structure initialization to use designated
2+
// initializer to make these compatible with Linux's RANDSTRUCT.
3+
//
4+
// As coccinelle has no support for referencing a field's member name, we
5+
// post-process the patch with a sed script to do just that (the field name is
6+
// mentioned by a trailing comment).
7+
//
8+
// (c) 2026 Open Source Security, Inc. All Rights Reserved.
9+
10+
@@
11+
typedef RPC_HAL_IFACES, RPCSTRUCTURECOPY_HAL_IFACES;
12+
identifier id;
13+
@@
14+
15+
static
16+
(
17+
RPC_HAL_IFACES
18+
|
19+
RPCSTRUCTURECOPY_HAL_IFACES
20+
)
21+
id = {
22+
- ...
23+
};

0 commit comments

Comments
 (0)