cocci.systeme.lip6.fr archive mirror
 help / color / mirror / Atom feed
* [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
@ 2025-06-29 17:04 devnoname120
  2025-06-29 17:26 ` Julia Lawall
  0 siblings, 1 reply; 13+ messages in thread
From: devnoname120 @ 2025-06-29 17:04 UTC (permalink / raw)
  To: cocci

[-- Attachment #1: Type: text/plain, Size: 2562 bytes --]

Hello,

I'm trying to insert two fields inside a struct, right before the use of the macro `randomized_struct_fields_end`. You can find more information about the macro here: https://stackoverflow.com/a/72675757

Here is the unified diff that I'm trying to convert to a semantic patch:

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 9ea614e09f56..c153abffd4c5 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1467,6 +1467,10 @@ struct task_struct {
      * New fields for task_struct should be added above here, so that
      * they are included in the randomized portion of task_struct.
      */
+#ifdef CONFIG_KSU_SUSFS
+    u64 susfs_task_state;
+    u64 susfs_last_fake_mnt_id;
+#endif
     randomized_struct_fields_end

     /* CPU-specific state of this task: */

Here is the file that it applies to: https://github.com/devnoname120/kernel_xiaomi_sm6150/blob/fce5582771e1254af065aaa2778940659e246611/include/linux/sched.h#L1470

I tried the following semantic patch:

@depends on file in "include/linux/sched.h" exists@
@@

struct task_struct {
...
+#ifdef CONFIG_KSU_SUSFS
+   u64 susfs_task_state;
+   u64 susfs_last_fake_mnt_id;
+#endif
randomized_struct_fields_end
...
}

However I have the following error:

❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
SPECIAL NAMES: adding u64 as a type
minus: parse error:
 File "include/linux/sched.h.cocci", line 11, column 0, charpos = 194
 around = '...',
 whole content = ...

I also tried the following code:

@depends on file in "include/linux/sched.h" exists@
identifier END =~ "randomized_struct_fields_end";
@@

struct task_struct {
...
+#ifdef CONFIG_KSU_SUSFS
+   u64 susfs_task_state;
+   u64 susfs_last_fake_mnt_id;
+#endif
END
...
}

But it yields the following error:

❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
SPECIAL NAMES: adding u64 as a type
minus: parse error:
 File "include/linux/sched.h.cocci", line 11, column 0, charpos = 209
 around = 'END',
 whole content = END

I'm not sure why I have this error and I couldn't find an answer after looking on the internet and reading the Coccinelle grammar.
Could you help me figure this out?


Thanks!
devnoname120

[-- Attachment #2: Type: text/html, Size: 6267 bytes --]

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
  2025-06-29 17:04 [cocci] [HELP] Using macro `randomized_struct_fields_end` as context? devnoname120
@ 2025-06-29 17:26 ` Julia Lawall
  2025-06-29 17:41   ` devnoname120
  0 siblings, 1 reply; 13+ messages in thread
From: Julia Lawall @ 2025-06-29 17:26 UTC (permalink / raw)
  To: devnoname120; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 2928 bytes --]



On Sun, 29 Jun 2025, devnoname120 wrote:

> Hello,
>  
> I'm trying to insert two fields inside a struct, right before the use of the macro `randomized_struct_fields_end`. You can find more information about the macro here: https://stackoverflow.com/a/72675757
>  
> Here is the unified diff that I'm trying to convert to a semantic patch:
>  
> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index 9ea614e09f56..c153abffd4c5 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -1467,6 +1467,10 @@ struct task_struct {
>       * New fields for task_struct should be added above here, so that
>       * they are included in the randomized portion of task_struct.
>       */
> +#ifdef CONFIG_KSU_SUSFS
> +    u64 susfs_task_state;
> +    u64 susfs_last_fake_mnt_id;
> +#endif
>      randomized_struct_fields_end
>  
>      /* CPU-specific state of this task: */
>  
> Here is the file that it applies to: https://github.com/devnoname120/kernel_xiaomi_sm6150/blob/fce5582771e1254af065aaa2778940659e246611/include/linux/sched.h#L1470
>  
> I tried the following semantic patch:
>  
> @depends on file in "include/linux/sched.h" exists@
> @@
>  
> struct task_struct {
> ...
> +#ifdef CONFIG_KSU_SUSFS
> +   u64 susfs_task_state;
> +   u64 susfs_last_fake_mnt_id;
> +#endif
> randomized_struct_fields_end
> ...
> }


You can try putting in the metavariable section

declarer name randomized_struct_fields_end;

But I suspect it will still object due to the lack of a ; after
randomized_struct_fields_end.

julia

>  
> However I have the following error:
>  
> ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
> init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> SPECIAL NAMES: adding u64 as a type
> minus: parse error: 
>  File "include/linux/sched.h.cocci", line 11, column 0, charpos = 194
>  around = '...',
>  whole content = ...
>  
> I also tried the following code:
>  
> @depends on file in "include/linux/sched.h" exists@
> identifier END =~ "randomized_struct_fields_end";
> @@
>  
> struct task_struct {
> ...
> +#ifdef CONFIG_KSU_SUSFS
> +   u64 susfs_task_state;
> +   u64 susfs_last_fake_mnt_id;
> +#endif
> END
> ...
> }
>  
> But it yields the following error:
>  
> ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
> init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> SPECIAL NAMES: adding u64 as a type
> minus: parse error: 
>  File "include/linux/sched.h.cocci", line 11, column 0, charpos = 209
>  around = 'END',
>  whole content = END
>  
> I'm not sure why I have this error and I couldn't find an answer after looking on the internet and reading the Coccinelle grammar.
> Could you help me figure this out?
>  
>  
> Thanks!
> devnoname120
>
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
  2025-06-29 17:26 ` Julia Lawall
@ 2025-06-29 17:41   ` devnoname120
  2025-06-29 18:58     ` Julia Lawall
  2025-06-29 19:18     ` Julia Lawall
  0 siblings, 2 replies; 13+ messages in thread
From: devnoname120 @ 2025-06-29 17:41 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 4521 bytes --]

Here is my new semantic patch:

@depends on file in "include/linux/sched.h" exists@
declarer name randomized_struct_fields_end;
@@


struct task_struct {
...
+#ifdef CONFIG_KSU_SUSFS
+ u64 susfs_task_state;
+ u64 susfs_last_fake_mnt_id;
+#endif
randomized_struct_fields_end
...
}

And here is the error:

❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
SPECIAL NAMES: adding randomized_struct_fields_end as a declarer
SPECIAL NAMES: adding u64 as a type
minus: parse error:
 File "include/linux/sched.h.cocci", line 12, column 0, charpos = 247
 around = '...',
 whole content = ...


If I add a semi-colon after `randomized_struct_fields_end` in the semantic patch:

❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
SPECIAL NAMES: adding randomized_struct_fields_end as a declarer
SPECIAL NAMES: adding u64 as a type
minus: parse error:
 File "include/linux/sched.h.cocci", line 11, column 28, charpos = 246
 around = ';',
 whole content = randomized_struct_fields_end;

Paul

On Jun 29, 2025 at 19:26 +0200, Julia Lawall <julia.lawall@inria.fr>, wrote:
>
>
> On Sun, 29 Jun 2025, devnoname120 wrote:
>
> > Hello,
> >
> > I'm trying to insert two fields inside a struct, right before the use of the macro `randomized_struct_fields_end`. You can find more information about the macro here: https://stackoverflow.com/a/72675757
> >
> > Here is the unified diff that I'm trying to convert to a semantic patch:
> >
> > diff --git a/include/linux/sched.h b/include/linux/sched.h
> > index 9ea614e09f56..c153abffd4c5 100644
> > --- a/include/linux/sched.h
> > +++ b/include/linux/sched.h
> > @@ -1467,6 +1467,10 @@ struct task_struct {
> >       * New fields for task_struct should be added above here, so that
> >       * they are included in the randomized portion of task_struct.
> >       */
> > +#ifdef CONFIG_KSU_SUSFS
> > +    u64 susfs_task_state;
> > +    u64 susfs_last_fake_mnt_id;
> > +#endif
> >      randomized_struct_fields_end
> >
> >      /* CPU-specific state of this task: */
> >
> > Here is the file that it applies to: https://github.com/devnoname120/kernel_xiaomi_sm6150/blob/fce5582771e1254af065aaa2778940659e246611/include/linux/sched.h#L1470
> >
> > I tried the following semantic patch:
> >
> > @depends on file in "include/linux/sched.h" exists@
> > @@
> >
> > struct task_struct {
> > ...
> > +#ifdef CONFIG_KSU_SUSFS
> > +   u64 susfs_task_state;
> > +   u64 susfs_last_fake_mnt_id;
> > +#endif
> > randomized_struct_fields_end
> > ...
> > }
>
>
> You can try putting in the metavariable section
>
> declarer name randomized_struct_fields_end;
>
> But I suspect it will still object due to the lack of a ; after
> randomized_struct_fields_end.
>
> julia
>
> >
> > However I have the following error:
> >
> > ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
> > init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> > SPECIAL NAMES: adding u64 as a type
> > minus: parse error:
> >  File "include/linux/sched.h.cocci", line 11, column 0, charpos = 194
> >  around = '...',
> >  whole content = ...
> >
> > I also tried the following code:
> >
> > @depends on file in "include/linux/sched.h" exists@
> > identifier END =~ "randomized_struct_fields_end";
> > @@
> >
> > struct task_struct {
> > ...
> > +#ifdef CONFIG_KSU_SUSFS
> > +   u64 susfs_task_state;
> > +   u64 susfs_last_fake_mnt_id;
> > +#endif
> > END
> > ...
> > }
> >
> > But it yields the following error:
> >
> > ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
> > init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> > SPECIAL NAMES: adding u64 as a type
> > minus: parse error:
> >  File "include/linux/sched.h.cocci", line 11, column 0, charpos = 209
> >  around = 'END',
> >  whole content = END
> >
> > I'm not sure why I have this error and I couldn't find an answer after looking on the internet and reading the Coccinelle grammar.
> > Could you help me figure this out?
> >
> >
> > Thanks!
> > devnoname120
> >

[-- Attachment #2: Type: text/html, Size: 7045 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
  2025-06-29 17:41   ` devnoname120
@ 2025-06-29 18:58     ` Julia Lawall
  2025-06-29 19:19       ` devnoname120
  2025-06-29 19:18     ` Julia Lawall
  1 sibling, 1 reply; 13+ messages in thread
From: Julia Lawall @ 2025-06-29 18:58 UTC (permalink / raw)
  To: devnoname120; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 5704 bytes --]



On Sun, 29 Jun 2025, devnoname120 wrote:

> Here is my new semantic patch:
>  
> @depends on file in "include/linux/sched.h" exists@
> declarer name randomized_struct_fields_end;
> @@
>
>  
> struct task_struct {
> ...
> +#ifdef CONFIG_KSU_SUSFS
> + u64 susfs_task_state;
> + u64 susfs_last_fake_mnt_id;
> +#endif
> randomized_struct_fields_end
> ...
> }
>  
> And here is the error:
>  
> ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
> init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> SPECIAL NAMES: adding randomized_struct_fields_end as a declarer
> SPECIAL NAMES: adding u64 as a type
> minus: parse error: 
>  File "include/linux/sched.h.cocci", line 12, column 0, charpos = 247
>  around = '...',
>  whole content = ...
>  
>  
> If I add a semi-colon after `randomized_struct_fields_end` in the semantic patch:
>  
> ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
> init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> SPECIAL NAMES: adding randomized_struct_fields_end as a declarer
> SPECIAL NAMES: adding u64 as a type
> minus: parse error: 
>  File "include/linux/sched.h.cocci", line 11, column 28, charpos = 246
>  around = ';',
>  whole content = randomized_struct_fields_end;

The problem is that getting the semantic patch to parse is not enough
anyway; the associated C code has to be parsed as well.

But I have the impression that you want to do something at one specific
place in sched.h.  Why not just make the change by hand?

julia

>  
> Paul
>  
> On Jun 29, 2025 at 19:26 +0200, Julia Lawall <julia.lawall@inria.fr>, wrote:
>
>
>       On Sun, 29 Jun 2025, devnoname120 wrote:
>
>             Hello,
>              
>             I'm trying to insert two fields inside a struct, right before the use of the macro `randomized_struct_fields_end`. You can find more information about the macro here: https://stackoverflow.com/a/72675757
>              
>             Here is the unified diff that I'm trying to convert to a semantic patch:
>              
>             diff --git a/include/linux/sched.h b/include/linux/sched.h
>             index 9ea614e09f56..c153abffd4c5 100644
>             --- a/include/linux/sched.h
>             +++ b/include/linux/sched.h
>             @@ -1467,6 +1467,10 @@ struct task_struct {
>                   * New fields for task_struct should be added above here, so that
>                   * they are included in the randomized portion of task_struct.
>                   */
>             +#ifdef CONFIG_KSU_SUSFS
>             +    u64 susfs_task_state;
>             +    u64 susfs_last_fake_mnt_id;
>             +#endif
>                  randomized_struct_fields_end
>              
>                  /* CPU-specific state of this task: */
>              
>             Here is the file that it applies to: https://github.com/devnoname120/kernel_xiaomi_sm6150/blob/fce5582771e1254af065aaa2778940659e246611/include/linux/sched.h#L1470
>              
>             I tried the following semantic patch:
>              
>             @depends on file in "include/linux/sched.h" exists@
>             @@
>              
>             struct task_struct {
>             ...
>             +#ifdef CONFIG_KSU_SUSFS
>             +   u64 susfs_task_state;
>             +   u64 susfs_last_fake_mnt_id;
>             +#endif
>             randomized_struct_fields_end
>             ...
>             }
>
>
>
>       You can try putting in the metavariable section
>
>       declarer name randomized_struct_fields_end;
>
>       But I suspect it will still object due to the lack of a ; after
>       randomized_struct_fields_end.
>
>       julia
>
>              
>             However I have the following error:
>              
>             ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
>             init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
>             SPECIAL NAMES: adding u64 as a type
>             minus: parse error: 
>              File "include/linux/sched.h.cocci", line 11, column 0, charpos = 194
>              around = '...',
>              whole content = ...
>              
>             I also tried the following code:
>              
>             @depends on file in "include/linux/sched.h" exists@
>             identifier END =~ "randomized_struct_fields_end";
>             @@
>              
>             struct task_struct {
>             ...
>             +#ifdef CONFIG_KSU_SUSFS
>             +   u64 susfs_task_state;
>             +   u64 susfs_last_fake_mnt_id;
>             +#endif
>             END
>             ...
>             }
>              
>             But it yields the following error:
>              
>             ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
>             init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
>             SPECIAL NAMES: adding u64 as a type
>             minus: parse error: 
>              File "include/linux/sched.h.cocci", line 11, column 0, charpos = 209
>              around = 'END',
>              whole content = END
>              
>             I'm not sure why I have this error and I couldn't find an answer after looking on the internet and reading the Coccinelle grammar.
>             Could you help me figure this out?
>              
>              
>             Thanks!
>             devnoname120
>
>
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
  2025-06-29 17:41   ` devnoname120
  2025-06-29 18:58     ` Julia Lawall
@ 2025-06-29 19:18     ` Julia Lawall
  2025-06-29 19:27       ` devnoname120
  1 sibling, 1 reply; 13+ messages in thread
From: Julia Lawall @ 2025-06-29 19:18 UTC (permalink / raw)
  To: devnoname120; +Cc: cocci

Coocinelle has a file standard.h thta gives dummy definitions for some
macros.

This file already contains

#define randomized_struct_fields_start

So it seems quite reasonable that it should also contain

#define randomized_struct_fields_end

which then enables sched.h to parse.

With that change, you can use the following solution:

@r@
field f;
@@

struct task_struct {
  ...
  f
};

@@
field r.f;
@@

struct task_struct {
  ...
  f
+ int something;
  ...
};

That is, first match the last field in the structure.  Then inherit thta
field into another rule and place the new stuff after it.  The final ...
is important to force the added code to be attached to f.  This will keep
it on top of the end marker, and on top of the associated comment as well.

julia

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
  2025-06-29 18:58     ` Julia Lawall
@ 2025-06-29 19:19       ` devnoname120
  0 siblings, 0 replies; 13+ messages in thread
From: devnoname120 @ 2025-06-29 19:19 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 6902 bytes --]

The reason is that I want to create a patch that applies to a broad variety of vendor kernels for Android. They all have minute differences, botched up backports, etc.

Previously I created semantic patches for KernelSU Next (Android rooting at the kernel level) that cleanly applies to all the 3.4 to 6.15 kernels that people threw at it (20+ of them). This would not have been possible without semantic patches and beforehand people were painstakingly trying to manually fix rejects when applying the traditional diff patches.

You can see the result here: https://github.com/devnoname120/kernelsu-coccinelle/blob/40c9c0caab5773680c07278e71d0fbaaeb39eccb/scope-minimized-hooks/kernelsu-scope-minimized.cocci

Now my new project is to port SUSFS (a series of kernel patches to hide KernelSU and other system modifications from user apps) to semantic patches so that they can also cleanly apply. Currently the SUSFS developer maintains 7 distinct branches to cover 7 different kernel versions, but even then people have a lot of rejects due to patches that vendors applied to the kernel of their device.

The full SUSFS patch that I'm trying to port over to semantic patches is here:
https://github.com/devnoname120/susfs4ksu-coccinelle/blob/7388e1613b3ee6267cc0d2784eebcce9fcaa8a09/50_add_susfs_in_kernel-4.14.patch

It's a big chunk but I'm convinced that I can get there with enough time. This would be a huge win for the community. I would like to keep it simple by having a single Coccinelle file that can be applied with spatch without additional patches and scripts, just like I've done for my kernelsu-coccinelle patches. The exception is the Makefile but it only requires to add 1 line in there so it's OK.

Thank you,
Paul
On Jun 29, 2025 at 20:59 +0200, Julia Lawall <julia.lawall@inria.fr>, wrote:



> On Sun, 29 Jun 2025, devnoname120 wrote:
>
>
> > Here is my new semantic patch:
> >
> > @depends on file in "include/linux/sched.h" exists@
> > declarer name randomized_struct_fields_end;
> > @@
> >
> >
> > struct task_struct {
> > ...
> > +#ifdef CONFIG_KSU_SUSFS
> > + u64 susfs_task_state;
> > + u64 susfs_last_fake_mnt_id;
> > +#endif
> > randomized_struct_fields_end
> > ...
> > }
> >
> > And here is the error:
> >
> > ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
> > init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> > SPECIAL NAMES: adding randomized_struct_fields_end as a declarer
> > SPECIAL NAMES: adding u64 as a type
> > minus: parse error:
> >  File "include/linux/sched.h.cocci", line 12, column 0, charpos = 247
> >  around = '...',
> >  whole content = ...
> >
> >
> > If I add a semi-colon after `randomized_struct_fields_end` in the semantic patch:
> >
> > ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
> > init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> > SPECIAL NAMES: adding randomized_struct_fields_end as a declarer
> > SPECIAL NAMES: adding u64 as a type
> > minus: parse error:
> >  File "include/linux/sched.h.cocci", line 11, column 28, charpos = 246
> >  around = ';',
> >  whole content = randomized_struct_fields_end;
> >
>
> The problem is that getting the semantic patch to parse is not enough
> anyway; the associated C code has to be parsed as well.
>
> But I have the impression that you want to do something at one specific
> place in sched.h. Why not just make the change by hand?
>
> julia
>
>

> > Paul
> >
> > On Jun 29, 2025 at 19:26 +0200, Julia Lawall <julia.lawall@inria.fr>, wrote:
> >
> >
> > On Sun, 29 Jun 2025, devnoname120 wrote:
> >
> > Hello,
> >
> > I'm trying to insert two fields inside a struct, right before the use of the macro `randomized_struct_fields_end`. You can find more information about the macro here: https://stackoverflow.com/a/72675757
> >
> > Here is the unified diff that I'm trying to convert to a semantic patch:
> >
> > diff --git a/include/linux/sched.h b/include/linux/sched.h
> > index 9ea614e09f56..c153abffd4c5 100644
> > --- a/include/linux/sched.h
> > +++ b/include/linux/sched.h
> > @@ -1467,6 +1467,10 @@ struct task_struct {
> >       * New fields for task_struct should be added above here, so that
> >       * they are included in the randomized portion of task_struct.
> >       */
> > +#ifdef CONFIG_KSU_SUSFS
> > +    u64 susfs_task_state;
> > +    u64 susfs_last_fake_mnt_id;
> > +#endif
> >      randomized_struct_fields_end
> >
> >      /* CPU-specific state of this task: */
> >
> > Here is the file that it applies to: https://github.com/devnoname120/kernel_xiaomi_sm6150/blob/fce5582771e1254af065aaa2778940659e246611/include/linux/sched.h#L1470
> >
> > I tried the following semantic patch:
> >
> > @depends on file in "include/linux/sched.h" exists@
> > @@
> >
> > struct task_struct {
> > ...
> > +#ifdef CONFIG_KSU_SUSFS
> > +   u64 susfs_task_state;
> > +   u64 susfs_last_fake_mnt_id;
> > +#endif
> > randomized_struct_fields_end
> > ...
> > }
> >
> >
> >
> > You can try putting in the metavariable section
> >
> > declarer name randomized_struct_fields_end;
> >
> > But I suspect it will still object due to the lack of a ; after
> > randomized_struct_fields_end.
> >
> > julia
> >
> >
> > However I have the following error:
> >
> > ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
> > init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> > SPECIAL NAMES: adding u64 as a type
> > minus: parse error:
> >  File "include/linux/sched.h.cocci", line 11, column 0, charpos = 194
> >  around = '...',
> >  whole content = ...
> >
> > I also tried the following code:
> >
> > @depends on file in "include/linux/sched.h" exists@
> > identifier END =~ "randomized_struct_fields_end";
> > @@
> >
> > struct task_struct {
> > ...
> > +#ifdef CONFIG_KSU_SUSFS
> > +   u64 susfs_task_state;
> > +   u64 susfs_last_fake_mnt_id;
> > +#endif
> > END
> > ...
> > }
> >
> > But it yields the following error:
> >
> > ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
> > init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> > SPECIAL NAMES: adding u64 as a type
> > minus: parse error:
> >  File "include/linux/sched.h.cocci", line 11, column 0, charpos = 209
> >  around = 'END',
> >  whole content = END
> >
> > I'm not sure why I have this error and I couldn't find an answer after looking on the internet and reading the Coccinelle grammar.
> > Could you help me figure this out?
> >
> >
> > Thanks!
> > devnoname120
> >
> >
> >

[-- Attachment #2: Type: text/html, Size: 9164 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
  2025-06-29 19:18     ` Julia Lawall
@ 2025-06-29 19:27       ` devnoname120
  2025-06-29 20:11         ` Julia Lawall
  2025-06-30  8:51         ` Julia Lawall
  0 siblings, 2 replies; 13+ messages in thread
From: devnoname120 @ 2025-06-29 19:27 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 2121 bytes --]

Unfortunately this solution adds it to the very end of the task_struct struct.

@r depends on file in "include/linux/sched.h" exists@
field f;
@@

struct task_struct {
...
f
};

@@
field r.f;
@@

struct task_struct {
...
f
+#ifdef CONFIG_KSU_SUSFS
+ u64 susfs_task_state;
+ u64 susfs_last_fake_mnt_id;
+#endif
...
};


❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
SPECIAL NAMES: adding u64 as a type
HANDLING: include/linux/sched.h
diff =
--- include/linux/sched.h
+++ /tmp/cocci-output-56533-5a9482-sched.h
@@ -1471,6 +1471,10 @@ struct task_struct {

       /* CPU-specific state of this task: */
       struct thread_struct            thread;
+#ifdef CONFIG_KSU_SUSFS
+       u64 susfs_task_state;
+       u64 susfs_last_fake_mnt_id;
+#endif

       /*
        * WARNING: on x86, 'thread_struct' contains a variable-sized


It needs instead to be added right above randomized_struct_fields_end, which is what closes the anonymous struct.

Thank you,
Paul
On Jun 29, 2025 at 21:18 +0200, Julia Lawall <julia.lawall@inria.fr>, wrote:

> Coocinelle has a file standard.h thta gives dummy definitions for some
> macros.
>
> This file already contains
>
> #define randomized_struct_fields_start
>
> So it seems quite reasonable that it should also contain
>
> #define randomized_struct_fields_end
>
> which then enables sched.h to parse.
>
> With that change, you can use the following solution:
>
> @r@
> field f;
> @@
>
> struct task_struct {
> ...
> f
> };
>
> @@
> field r.f;
> @@
>
> struct task_struct {
> ...
> f
> + int something;
> ...
> };
>
> That is, first match the last field in the structure. Then inherit thta
> field into another rule and place the new stuff after it. The final ...
> is important to force the added code to be attached to f. This will keep
> it on top of the end marker, and on top of the associated comment as well.
>
> julia
>

[-- Attachment #2: Type: text/html, Size: 4557 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
  2025-06-29 19:27       ` devnoname120
@ 2025-06-29 20:11         ` Julia Lawall
  2025-06-30  8:51         ` Julia Lawall
  1 sibling, 0 replies; 13+ messages in thread
From: Julia Lawall @ 2025-06-29 20:11 UTC (permalink / raw)
  To: devnoname120; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 2475 bytes --]



On Sun, 29 Jun 2025, devnoname120 wrote:

> Unfortunately this solution adds it to the very end of the task_struct struct.
>  
> @r depends on file in "include/linux/sched.h" exists@
> field f;
> @@
>  
> struct task_struct {
> ...
> f
> };
>
> @@
> field r.f;
> @@
>  
> struct task_struct {
> ...
> f
> +#ifdef CONFIG_KSU_SUSFS
> + u64 susfs_task_state;
> + u64 susfs_last_fake_mnt_id;
> +#endif
> ...
> };
>
>
> ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing include/linux/sched.h
> init_defs_builtins: /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> SPECIAL NAMES: adding u64 as a type
> HANDLING: include/linux/sched.h
> diff = 
> --- include/linux/sched.h
> +++ /tmp/cocci-output-56533-5a9482-sched.h
> @@ -1471,6 +1471,10 @@ struct task_struct {
>
>        /* CPU-specific state of this task: */
>        struct thread_struct            thread;
> +#ifdef CONFIG_KSU_SUSFS
> +       u64 susfs_task_state;
> +       u64 susfs_last_fake_mnt_id;
> +#endif
>
>        /*
>         * WARNING: on x86, 'thread_struct' contains a variable-sized
>  
>  
> It needs instead to be added right above randomized_struct_fields_end, which is what closes the anonymous struct.

OK, they were the same in the version of the kernel I had...

I will try one more thing...

julia


>  
> Thank you,
> Paul
> On Jun 29, 2025 at 21:18 +0200, Julia Lawall <julia.lawall@inria.fr>, wrote:
>  
>       Coocinelle has a file standard.h thta gives dummy definitions for some
>       macros.
>
>       This file already contains
>
>       #define randomized_struct_fields_start
>
>       So it seems quite reasonable that it should also contain
>
>       #define randomized_struct_fields_end
>
>       which then enables sched.h to parse.
>
>       With that change, you can use the following solution:
>
>       @r@
>       field f;
>       @@
>
>       struct task_struct {
>       ...
>       f
>       };
>
>       @@
>       field r.f;
>       @@
>
>       struct task_struct {
>       ...
>       f
>       + int something;
>       ...
>       };
>
>       That is, first match the last field in the structure. Then inherit thta
>       field into another rule and place the new stuff after it. The final ...
>       is important to force the added code to be attached to f. This will keep
>       it on top of the end marker, and on top of the associated comment as well.
>
>       julia
>        
>
>
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
  2025-06-29 19:27       ` devnoname120
  2025-06-29 20:11         ` Julia Lawall
@ 2025-06-30  8:51         ` Julia Lawall
       [not found]           ` <5d6b297d-2139-4732-acbd-92270ecfd4e7@Spark>
       [not found]           ` <68edca4d-74e7-4b83-9813-03d8fd7eae4e@Spark>
  1 sibling, 2 replies; 13+ messages in thread
From: Julia Lawall @ 2025-06-30  8:51 UTC (permalink / raw)
  To: devnoname120; +Cc: cocci

randomized_struct_fields_end will soon be supported in the C parser.  It
is not currently supported in the semantic patch parser, but you can get
the same effect as illustrated by the example below.

I will let you know when this is all available on github.

julia


@@
field f : script:python () { f == "randomized_struct_fields_end" };
@@

struct task_struct {
  ...
+ int something;
  f
  ...
};

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
       [not found]           ` <5d6b297d-2139-4732-acbd-92270ecfd4e7@Spark>
@ 2025-07-07 10:15             ` Victor Gambier
  2025-07-13 20:34               ` devnoname120
  0 siblings, 1 reply; 13+ messages in thread
From: Victor Gambier @ 2025-07-07 10:15 UTC (permalink / raw)
  To: devnoname120, Julia Lawall; +Cc: cocci

Hi,

On 05/07/2025 12:09, devnoname120 wrote:
> Interestingly I seem to have an error on pkg-config:
> ❯ spatch --sp-file include/linux/sched.h.cocci --in-place 
> --linux-spacing include/linux/sched.h
> init_defs_builtins: 
> /opt/homebrew/Cellar/coccinelle/1.3.0/lib/coccinelle/standard.h
> SPECIAL NAMES: adding u64 as a type
> Py.find_library_path: unable to parse the output of pkg-config 
> '-L/opt/homebrew/opt/python@3.13/Frameworks/Python.framework/Versions/3.13/lib'

I am looking into this error, but I can't reproduce it at the moment. 
Did you build coccinelle manually, or is this straight from your 
distribution package manager? If it's the former, can you let me know 
what your local changes are (the "-dirty" part of the --version output 
seems to indicate you have some uncommitted changes, they may be 
relevant). If it's the latter, could you tell me which one?

>  If I try to run this command manually:
> ❯ pkg-config 
> '-L/opt/homebrew/opt/python@3.13/Frameworks/Python.framework/Versions/3.13/lib'
> pkgconf: unknown option -- 
> L/opt/homebrew/opt/python@3.13/Frameworks/Python.framework/Versions/3.13/lib


I also cannot run this command manually.

Thanks,
Victor

> I'm using Coccinelle 1.3:
> ❯ spatch --version
> spatch version 1.3-dirty compiled with OCaml version 5.1.1
> Flags passed to the configure script: --disable-silent-rules 
> --enable-ocaml --enable-opt --without-pdflatex 
> --with-bash-completion=/opt/homebrew/Cellar/coccinelle/1.3.0/etc/bash_completion.d 
> --disable-debug --disable-dependency-tracking 
> --prefix=/opt/homebrew/Cellar/coccinelle/1.3.0 
> --libdir=/opt/homebrew/Cellar/coccinelle/1.3.0/lib
> OCaml scripting support: yes
> Python scripting support: yes
> Syntax of regular expressions: PCRE
> Paul
> On Jun 30, 2025 at 10:52 +0200, Julia Lawall <julia.lawall@inria.fr>, 
> wrote:
>> randomized_struct_fields_end will soon be supported in the C parser. It
>> is not currently supported in the semantic patch parser, but you can get
>> the same effect as illustrated by the example below.
>>
>> I will let you know when this is all available on github.
>>
>> julia
>>
>>
>> @@
>> field f : script:python () { f == "randomized_struct_fields_end" };
>> @@
>>
>> struct task_struct {
>> ...
>> + int something;
>> f
>> ...
>> };

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
  2025-07-07 10:15             ` Victor Gambier
@ 2025-07-13 20:34               ` devnoname120
  0 siblings, 0 replies; 13+ messages in thread
From: devnoname120 @ 2025-07-13 20:34 UTC (permalink / raw)
  To: Julia Lawall, Victor Gambier; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 1399 bytes --]

> Did you build coccinelle manually, or is this straight from your
> distribution package manager? If it's the former, can you let me know
> what your local changes are (the "-dirty" part of the --version output
> seems to indicate you have some uncommitted changes, they may be
> relevant). If it's the latter, could you tell me which one?

I'm running macOS Sequoia 15.5 (24F74) and I installed `spatch` using `brew install coccinelle`. I just reinstalled it to be sure, and I confirm that the `-dirty` suffix is still shown.

You can see the source of the package here:
https://github.com/Homebrew/homebrew-core/blob/7672fe8cd2689390ba6a1fb675b91d342ecb3d2a/Formula/c/coccinelle.rb

If I go to `/opt/homebrew/Cellar/coccinelle/1.3.0` and run `git status` it shows the following:

> On branch stable
> nothing to commit, working tree clean

If I use OrbStack to spin up an Ubuntu VM, run `apt install coccinelle`, then I can run the same spatch command without issues.

Thanks,
Paul
On Jul 7, 2025 at 12:15 +0200, Victor Gambier <victor.gambier@inria.fr>, wrote:

> Did you build coccinelle manually, or is this straight from your distribution package manager? If it's the former, can you let me know what your local changes are (the "-dirty" part of the --version output seems to indicate you have some uncommitted changes, they may be relevant). If it's the latter, could you tell me which one?

[-- Attachment #2: Type: text/html, Size: 2688 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
       [not found]           ` <68edca4d-74e7-4b83-9813-03d8fd7eae4e@Spark>
@ 2025-07-13 20:58             ` Julia Lawall
       [not found]               ` <033af1c6-e410-4ccb-8bed-41a308d66f1e@Spark>
  0 siblings, 1 reply; 13+ messages in thread
From: Julia Lawall @ 2025-07-13 20:58 UTC (permalink / raw)
  To: devnoname120; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 1537 bytes --]



On Sun, 13 Jul 2025, devnoname120 wrote:

> I just attempted to run this command on Ubuntu and although it didn't return
> any error, no change was done:
>  
> ❯ spatch --sp-file include/linux/sched.h.cocci --in-place --linux-spacing
> include/linux/sched.h
> init_defs_builtins: /usr/lib/coccinelle/standard.h
> SPECIAL NAMES: adding u64 as a type
> HANDLING: include/linux/sched.h
>  
> Here is the content of `include/linux/sched.h.cocci`:
>  
> ```
> @depends on file in "include/linux/sched.h" exists@
> field f : script:python () { f == "randomized_struct_fields_end" };
> @@
>  
> struct task_struct {
> ...
> +#ifdef CONFIG_KSU_SUSFS
> +    u64 susfs_task_state;
> +    u64 susfs_last_fake_mnt_id;
> +#endif
> f
> ...
> };
> ```
>  
> The `include/linux/sched.h` file remains the same as before.

It all works fine for me.  What version of Coccinelle do you have?

julia

>  
>
> Thank you,
> Paul
> On Jun 30, 2025 at 10:52 +0200, Julia Lawall <julia.lawall@inria.fr>, wrote:
>       randomized_struct_fields_end will soon be supported in the C
>       parser. It
>       is not currently supported in the semantic patch parser, but you
>       can get
>       the same effect as illustrated by the example below.
>
>       I will let you know when this is all available on github.
>
>       julia
>
>
>       @@
>       field f : script:python () { f == "randomized_struct_fields_end"
>       };
>       @@
>
>       struct task_struct {
>       ...
>       + int something;
>       f
>       ...
>       };
>
>
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [cocci] [HELP] Using macro `randomized_struct_fields_end` as context?
       [not found]               ` <033af1c6-e410-4ccb-8bed-41a308d66f1e@Spark>
@ 2025-07-16 12:02                 ` Victor Gambier
  0 siblings, 0 replies; 13+ messages in thread
From: Victor Gambier @ 2025-07-16 12:02 UTC (permalink / raw)
  To: devnoname120, Julia Lawall; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 4445 bytes --]

Thanks for all the information. You need two things:

1. A version of Coccinelle that has Julia's recent patch (b00c5adb1a48). 
It is available on GitLab, but not on Github, and you'd need to compile 
it yourself or wait until a new version is released. Let me know if you 
need help with that, it shouldn't be too time-consuming.

2. Remove the "depends" line. This script can work:

```
@@
field f : script:python () { f == "randomized_struct_fields_end" };
@@

struct task_struct {
...
+#ifdef CONFIG_KSU_SUSFS
+    u64 susfs_task_state;
+    u64 susfs_last_fake_mnt_id;
+#endif
f
...
};
```

(Maybe this was only necessary on my end, but I suggest you try that if 
it does not work.)

I can confirm that script with that version of Coccinelle works.

As for the original issue with the MacOS brew version, I unfortunately 
don't have reliable access to a MacOS machine to figure out the issue. 
Let me know if you'd like to spend some time troubleshooting that.

Victor

On 14/07/2025 20:00, devnoname120 wrote:
> Here is the information that you requested:
>
> ❯ paul@ubuntu:/Users/paul$ spatch --version
> spatch version 1.3 compiled with OCaml version 5.3.0
> Flags passed to the configure script: --prefix=/usr --sysconfdir=/etc 
> --libdir=/usr/lib --enable-ocaml --enable-python --with-python=python3 
> --enable-opt
> OCaml scripting support: yes
> Python scripting support: yes
> Syntax of regular expressions: Str
>
> ❯ paul@ubuntu:/Users/paul$ apt info coccinelle
> Package: coccinelle
> Version: 1.3.0.deb-1build2
> Priority: optional
> Section: universe/devel
> Origin: Ubuntu
> Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
> Original-Maintainer: Debian OCaml Maintainers 
> <debian-ocaml-maint@lists.debian.org>
> Bugs: https://bugs.launchpad.net/ubuntu/+filebug
> Installed-Size: 59.0 MB
> Depends: libparmap-ocaml, ocaml-findlib, libstdcompat-ocaml-iy6x7, 
> libstdlib-ocaml-fxjy5, python3:any, libc6 (>= 2.38), libzstd1 (>= 1.5.5)
> Suggests: coccinelle-doc, vim-addon-manager
> Homepage: http://coccinelle.lip6.fr
> Download-Size: 13.8 MB
> APT-Manual-Installed: yes
> APT-Sources: http://ports.ubuntu.com/ubuntu-ports plucky/universe 
> arm64 Packages
> Description: semantic patching tool for C
> Coccinelle is a program matching and transformation tool for C.
> The programmer describes the code to match and the transformation to
> perform as a semantic patch, which looks like a standard patch, but can
> transform multiple files at any number of code sites.
> Thank you,
> Paul
> On 13 Jul 2025 at 22:58 +0200, Julia Lawall <julia.lawall@inria.fr>, 
> wrote:
>
>
>     On Sun, 13 Jul 2025, devnoname120 wrote:
>
>         I just attempted to run this command on Ubuntu and although it
>         didn't return
>         any error, no change was done:
>
>         ❯ spatch --sp-file include/linux/sched.h.cocci --in-place
>         --linux-spacing
>         include/linux/sched.h
>         init_defs_builtins: /usr/lib/coccinelle/standard.h
>         SPECIAL NAMES: adding u64 as a type
>         HANDLING: include/linux/sched.h
>
>         Here is the content of `include/linux/sched.h.cocci`:
>
>         ```
>         @depends on file in "include/linux/sched.h" exists@
>         field f : script:python () { f ==
>         "randomized_struct_fields_end" };
>         @@
>
>         struct task_struct {
>         ...
>         +#ifdef CONFIG_KSU_SUSFS
>         +    u64 susfs_task_state;
>         +    u64 susfs_last_fake_mnt_id;
>         +#endif
>         f
>         ...
>         };
>         ```
>
>         The `include/linux/sched.h` file remains the same as before.
>
>
>     It all works fine for me. What version of Coccinelle do you have?
>
>     julia
>
>
>
>         Thank you,
>         Paul
>         On Jun 30, 2025 at 10:52 +0200, Julia Lawall
>         <julia.lawall@inria.fr>, wrote:
>         randomized_struct_fields_end will soon be supported in the C
>         parser. It
>         is not currently supported in the semantic patch parser, but you
>         can get
>         the same effect as illustrated by the example below.
>
>         I will let you know when this is all available on github.
>
>         julia
>
>
>         @@
>         field f : script:python () { f == "randomized_struct_fields_end"
>         };
>         @@
>
>         struct task_struct {
>         ...
>         + int something;
>         f
>         ...
>         };
>
>

[-- Attachment #2: Type: text/html, Size: 8280 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2025-07-16 12:02 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-29 17:04 [cocci] [HELP] Using macro `randomized_struct_fields_end` as context? devnoname120
2025-06-29 17:26 ` Julia Lawall
2025-06-29 17:41   ` devnoname120
2025-06-29 18:58     ` Julia Lawall
2025-06-29 19:19       ` devnoname120
2025-06-29 19:18     ` Julia Lawall
2025-06-29 19:27       ` devnoname120
2025-06-29 20:11         ` Julia Lawall
2025-06-30  8:51         ` Julia Lawall
     [not found]           ` <5d6b297d-2139-4732-acbd-92270ecfd4e7@Spark>
2025-07-07 10:15             ` Victor Gambier
2025-07-13 20:34               ` devnoname120
     [not found]           ` <68edca4d-74e7-4b83-9813-03d8fd7eae4e@Spark>
2025-07-13 20:58             ` Julia Lawall
     [not found]               ` <033af1c6-e410-4ccb-8bed-41a308d66f1e@Spark>
2025-07-16 12:02                 ` Victor Gambier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).