Skip to contentSkip to conversation

Using macro `randomized_struct_fields_end` as context?

cocci devnoname120 3 participants 13 messages Jun 29, 2025 – Jul 16, 2025 3 messages not in the archive Scheduler

lore
Share & tools
lore
#0 lore raw

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:

include/linux/sched.h +0 -0
index 9ea614e09f56..c153abffd4c5 100644
@@ -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

#1 lore raw
Quoted 35 lines · 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:

include/linux/sched.h +4 -0
index 9ea614e09f56..c153abffd4c5 100644
@@ -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 {
...

include/linux/sched.h +4 -0
+#ifdef CONFIG_KSU_SUSFS
+   u64 susfs_task_state;
+   u64 susfs_last_fake_mnt_id;
+#endif
Quoted 3 lines

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

Quoted 45 lines


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 {
...

include/linux/sched.h +4 -0
+#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

#2 lore raw

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

Quoted 97 lines · On Jun 29, 2025 at 19:26 +0200, Julia Lawall <[email protected]>, wrote:
Quoted 35 lines · 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:

include/linux/sched.h +4 -0
index 9ea614e09f56..c153abffd4c5 100644
@@ -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 {
...

include/linux/sched.h +4 -0
+#ifdef CONFIG_KSU_SUSFS
+   u64 susfs_task_state;
+   u64 susfs_last_fake_mnt_id;
+#endif
Quoted 3 lines

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

Quoted 44 lines

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 {
...

include/linux/sched.h +4 -0
+#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

#3 lore raw
Quoted 31 lines · 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 {
...

(quoted diff) +4 -0
+#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:

Quoted 8 lines

❯ 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

Quoted 104 lines


Paul

On Jun 29, 2025 at 19:26 +0200, Julia Lawall <[email protected]>, 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
#4 lore raw

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

Quoted 157 lines · On Jun 29, 2025 at 20:59 +0200, Julia Lawall <[email protected]>, wrote:
Quoted 31 lines · 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 {
...

(quoted diff) +4 -0
+#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:

Quoted 9 lines

❯ 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

Quoted 103 lines

Paul

On Jun 29, 2025 at 19:26 +0200, Julia Lawall <[email protected]>, 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:

include/linux/sched.h +4 -0
index 9ea614e09f56..c153abffd4c5 100644
@@ -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 {
...

include/linux/sched.h +4 -0
+#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 {
...

include/linux/sched.h +4 -0
+#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

#5 lore raw
In reply to devnoname120

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

#6 lore raw

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

tmp/cocci-output-56533-5a9482-sched.h renamed from linux/sched.h +0 -0
diff =
@@ -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

Quoted 42 lines · On Jun 29, 2025 at 21:18 +0200, Julia Lawall <[email protected]>, 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

#7 lore raw
Quoted 44 lines · 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

(quoted diff) +4 -0
+#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 =

tmp/cocci-output-56533-5a9482-sched.h renamed from linux/sched.h +4 -0
@@ -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
Quoted 3 lines



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

Quoted 50 lines


Thank you,
Paul
On Jun 29, 2025 at 21:18 +0200, Julia Lawall <[email protected]>, 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
       
#8 lore raw
In reply to devnoname120

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
...
};

A message that is not in the archive (sent off-list or not yet received): 5d6b297d-2139-4732-acbd-92270ecfd4e7@Spark

#10 lore raw

Hi,

Quoted 6 lines · 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/[email protected]/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?

Quoted 3 lines

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

I also cannot run this command manually.

Thanks,
Victor

Quoted 33 lines

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

Quoted 19 lines · On Jun 30, 2025 at 10:52 +0200, Julia Lawall <[email protected]>, 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
...
};

#11 lore raw
Quoted 5 lines

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:

Quoted 2 lines

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

Quoted 1 line · On Jul 7, 2025 at 12:15 +0200, Victor Gambier <[email protected]>, 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?

A message that is not in the archive (sent off-list or not yet received): 68edca4d-74e7-4b83-9813-03d8fd7eae4e@Spark

#13 lore raw
Quoted 22 lines · 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 {
...

(quoted diff) +4 -0
+#ifdef CONFIG_KSU_SUSFS
+    u64 susfs_task_state;
+    u64 susfs_last_fake_mnt_id;
+#endif
Quoted 6 lines

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

Quoted 30 lines

Thank you,
Paul
On Jun 30, 2025 at 10:52 +0200, Julia Lawall <[email protected]>, 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
      ...
      };

A message that is not in the archive (sent off-list or not yet received): 033af1c6-e410-4ccb-8bed-41a308d66f1e@Spark

#15 lore raw

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

Quoted 106 lines · 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 <[email protected]>
Original-Maintainer: Debian OCaml Maintainers <[email protected]>
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 <[email protected]>, 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
        <[email protected]>, 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
        ...
        };