genksyms: fix syntax error for attribute after 'struct'
authorMasahiro Yamada <masahiroy@kernel.org>
Mon, 13 Jan 2025 15:00:52 +0000 (00:00 +0900)
committerMasahiro Yamada <masahiroy@kernel.org>
Sat, 18 Jan 2025 00:11:47 +0000 (09:11 +0900)
A longstanding issue with genksyms is that it has hidden syntax errors.

When a syntax error occurs, yyerror() is called. However,
error_with_pos() is a no-op unless the -w option is provided.

You can observe syntax errors by manually passing the -w option.

For example, with CONFIG_MODVERSIONS=y on v6.13-rc1:

    $ make -s KCFLAGS=-D__GENKSYMS__ arch/x86/kernel/cpu/mshyperv.i
    $ cat arch/x86/kernel/cpu/mshyperv.i | scripts/genksyms/genksyms -w
        [ snip ]
    ./arch/x86/include/asm/svm.h:122: syntax error

The syntax error occurs in the following code in arch/x86/include/asm/svm.h:

    struct __attribute__ ((__packed__)) vmcb_control_area {
            [ snip ]
    };

The issue arises from __attribute__ immediately after the 'struct'
keyword.

This commit allows the 'struct' keyword to be followed by attributes.

The lexer must be adjusted because dont_want_brace_phase should not be
decremented while processing attributes.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Acked-by: Nicolas Schier <n.schier@avm.de>
scripts/genksyms/lex.l
scripts/genksyms/parse.y

index e886133af5783fbf86190f682de7ae0d5d95eeab..a1f969dcf24f177ed25b82dda82090cfaec188b6 100644 (file)
@@ -438,7 +438,12 @@ fini:
 
   if (suppress_type_lookup > 0)
     --suppress_type_lookup;
-  if (dont_want_brace_phrase > 0)
+
+  /*
+   *  __attribute__() can be placed immediately after the 'struct' keyword.
+   *  e.g.) struct __attribute__((__packed__)) foo { ... };
+   */
+  if (token != ATTRIBUTE_PHRASE && dont_want_brace_phrase > 0)
     --dont_want_brace_phrase;
 
   yylval = &next_node->next;
index 82774df506421f9308ee18d8c48d05293d38a586..33639232a709ed2e4634a089e4685ab2c34e263b 100644 (file)
@@ -234,16 +234,16 @@ type_specifier:
 
        /* References to s/u/e's defined elsewhere.  Rearrange things
           so that it is easier to expand the definition fully later.  */
-       | STRUCT_KEYW IDENT
-               { remove_node($1); (*$2)->tag = SYM_STRUCT; $$ = $2; }
+       | STRUCT_KEYW attribute_opt IDENT
+               { remove_node($1); (*$3)->tag = SYM_STRUCT; $$ = $3; }
        | UNION_KEYW IDENT
                { remove_node($1); (*$2)->tag = SYM_UNION; $$ = $2; }
        | ENUM_KEYW IDENT
                { remove_node($1); (*$2)->tag = SYM_ENUM; $$ = $2; }
 
        /* Full definitions of an s/u/e.  Record it.  */
-       | STRUCT_KEYW IDENT class_body
-               { record_compound($1, $2, $3, SYM_STRUCT); $$ = $3; }
+       | STRUCT_KEYW attribute_opt IDENT class_body
+               { record_compound($1, $3, $4, SYM_STRUCT); $$ = $4; }
        | UNION_KEYW IDENT class_body
                { record_compound($1, $2, $3, SYM_UNION); $$ = $3; }
        | ENUM_KEYW IDENT enum_body
@@ -254,7 +254,7 @@ type_specifier:
        | ENUM_KEYW enum_body
                { add_symbol(NULL, SYM_ENUM, NULL, 0); $$ = $2; }
        /* Anonymous s/u definitions.  Nothing needs doing.  */
-       | STRUCT_KEYW class_body                        { $$ = $2; }
+       | STRUCT_KEYW attribute_opt class_body          { $$ = $3; }
        | UNION_KEYW class_body                         { $$ = $2; }
        ;