tools/sparse: Add static check
cover tools/sparse: Add static check
Hello,
This teaches 'make check' to flag missing static keywords. Note that I am not sure how this avoids flagging symbols only declared in header files. However I haven't noticed any false positives and I think false negatives will only occur when the author uses function prototypes to reference private symbols before their definition.
The statx patch is just included to show one issue it caught.
Richard Palethorpe (3):
tools/sparse: Add static check
doc: Add LTP-003 and LTP-004 static and tst API prefix rules
statx: Add missing static keyword to tcase variable
doc/library-api-writing-guidelines.txt | 10 +++++ doc/rules.tsv | 2 + doc/test-writing-guidelines.txt | 8 ++++ testcases/kernel/syscalls/statx/statx01.c | 2 +- testcases/kernel/syscalls/statx/statx02.c | 2 +- testcases/kernel/syscalls/statx/statx04.c | 2 +- testcases/kernel/syscalls/statx/statx05.c | 2 +- testcases/kernel/syscalls/statx/statx07.c | 2 +- tools/sparse/sparse-ltp.c | 53 +++++++++++++++++++++++ 9 files changed, 78 insertions(+), 5 deletions(-)
Signature · 5 lines
2.33.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 1/3 tools/sparse: Add static check
This was adapted from Sparse's inbuilt check_duplicates (-Wdecl). The original check appears to print a warning whenever a symbol is non-static, but has no prototype. It appears to work because library symbols are usually declared first in a header file and then again with their definition in a source file.
The LTP version also checks for the various library prefixes, but should otherwise be the same.
--- tools/sparse/sparse-ltp.c | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
tools/sparse/sparse-ltp.c +53 -0
@@ -82,6 +82,57 @@ static void do_entrypoint_checks(struct entrypoint *ep) }END_FOR_EACH_PTR (bb); } +/* Check for LTP-003 and LTP-004 +* +* Try to find cases where the static keyword was forgotten. +*/ +static void check_symbol_visibility (struct symbol *sym) +{ +struct symbol *next = sym; +unsigned long mod = sym->ctype.modifiers; +const char *name =show_ident (sym->ident); +int has_lib_prefix = !strncmp ("tst_" , name,4 ) || + !strncmp ("TST_" , name,4 ) || + !strncmp ("ltp_" , name,4 ) || + !strncmp ("safe_" , name,5 ); + +if (!(mod &MOD_TOPLEVEL )) +return ; + +if (has_lib_prefix && (mod &MOD_STATIC )) { +warning (sym->pos, +"LTP-003: Symbol '%s' has the LTP public library prefix, but is static (private)." , + name); +return ; + } + +if ((mod &MOD_STATIC )) +return ; + +if (tu_kind ==LTP_LIB && !has_lib_prefix) { +warning (sym->pos, +"LTP-003: Symbol '%s' is a public library function, but is missing the 'tst_' prefix" , + name); +return ; + } + +if (next->same_symbol) +return ; + +if (sym->ident == &main_ident) +return ; + +warning (sym->pos, +"Symbol '%s' has no prototype or library ('tst_') prefix. Should it be static?" , + name); +} + +/* AST level checks */ +static void do_symbol_checks (struct symbol *sym) +{ +check_symbol_visibility (sym); +} +/* Compile the AST into a graph of basicblocks */ static void process_symbols (struct symbol_list *list) {
@@ -90,6 +141,8 @@ static void process_symbols(struct symbol_list *list) FOR_EACH_PTR(list, sym) {struct entrypoint *ep; +do_symbol_checks (sym); +expand_symbol (sym); ep =linearize_symbol (sym);if (!ep || !ep->entry)
Signature · 5 lines
2.33.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Richie,
Quoted 8 lines
This was adapted from Sparse's inbuilt
check_duplicates(-Wdecl). The original check appears to print a warning whenever a symbol is non-static, but has no prototype. It appears to work because library symbols are usually declared first in a header file and then again with their definition in a source file.The LTP version also checks for the various library prefixes, but should otherwise be the same.
LGTM, thanks!
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hello Petr and Cyril,
Thanks! pushed. Please pull and try it out.
Quoted 11 lines · Petr Vorel
Hi Richie,
Quoted 8 lines
This was adapted from Sparse's inbuilt
check_duplicates(-Wdecl). The original check appears to print a warning whenever a symbol is non-static, but has no prototype. It appears to work because library symbols are usually declared first in a header file and then again with their definition in a source file.The LTP version also checks for the various library prefixes, but should otherwise be the same.
LGTM, thanks!
I took the liberty (;-)) of interpreting this as Acked-by.
Quoted 3 lines
Kind regards,
Petr
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 1 line
Thanks! pushed. Please pull and try it out.
Looks like it fails on fuzzy sync since it uses tst_ but it's in an header.
These definitions should be static inline and changing them so fixes the warnings. It looks like static inline functions does not make it into the symbol test at all.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 8 lines
Quoted 1 line
Thanks! pushed. Please pull and try it out.
Looks like it fails on fuzzy sync since it uses
tst_but it's in an header.These definitions should be static inline and changing them so fixes the warnings. It looks like static inline functions does not make it into the symbol test at all.
This is even stranger, the 'static inline void' functions does not make it into the check function, but anything that returns a non-void value does get there so we need:
tools/sparse/sparse-ltp.c +1 -1
@@ -98,7 +98,7 @@ static void check_symbol_visibility(const struct symbol *const sym)if (!(mod &MOD_TOPLEVEL ))return ; -if (has_lib_prefix && (mod &MOD_STATIC )) { +if (has_lib_prefix && (mod &MOD_STATIC ) && !(mod &MOD_INLINE )) {warning (sym->pos,"LTP-003: Symbol '%s' has the LTP public library prefix, but is static (private)." , name);
And:
include/tst_fuzzy_sync.h +7 -7
@@ -210,7 +210,7 @@ struct tst_fzsync_pair {* * @sa tst_fzsync_pair_reset() */ -static void tst_fzsync_pair_init (struct tst_fzsync_pair *pair) +static inline void tst_fzsync_pair_init (struct tst_fzsync_pair *pair) {CHK (avg_alpha,0 ,1 ,0.25 );CHK (min_samples,20 ,INT_MAX ,1024 );
@@ -230,7 +230,7 @@ static void tst_fzsync_pair_init(struct tst_fzsync_pair *pair)* * Call this from your cleanup function. */ -static void tst_fzsync_pair_cleanup (struct tst_fzsync_pair *pair) +static inline void tst_fzsync_pair_cleanup (struct tst_fzsync_pair *pair) {if (pair->thread_b) {/* Revoke thread B if parent hits accidental break */
@@ -254,7 +254,7 @@ struct tst_fzsync_run_thread {* Wrap run_b for tst_fzsync_pair_reset to enable pthread cancel * at the start of the thread B. */ -static void *tst_fzsync_thread_wrapper (void *run_thread) +static inline void *tst_fzsync_thread_wrapper (void *run_thread) {struct tst_fzsync_run_thread t = *(struct tst_fzsync_run_thread *)run_thread;
@@ -268,7 +268,7 @@ static void *tst_fzsync_thread_wrapper(void *run_thread)* * @relates tst_fzsync_stat */ -static void tst_init_stat (struct tst_fzsync_stat *s) +static inline void tst_init_stat (struct tst_fzsync_stat *s) { s->avg =0 ; s->avg_dev =0 ;
@@ -292,7 +292,7 @@ static void tst_init_stat(struct tst_fzsync_stat *s)* * @sa tst_fzsync_pair_init() */ -static void tst_fzsync_pair_reset (struct tst_fzsync_pair *pair, +static inline void tst_fzsync_pair_reset (struct tst_fzsync_pair *pair,void *(*run_b)(void *)) {tst_fzsync_pair_cleanup (pair);
@@ -340,7 +340,7 @@ static inline void tst_fzsync_stat_info(struct tst_fzsync_stat stat,* * @relates tst_fzsync_pair */ -static void tst_fzsync_pair_info (struct tst_fzsync_pair *pair) +static inline void tst_fzsync_pair_info (struct tst_fzsync_pair *pair) {tst_res (TINFO ,"loop = %d, delay_bias = %d" , pair->exec_loop, pair->delay_bias);
@@ -493,7 +493,7 @@ static inline void tst_upd_diff_stat(struct tst_fzsync_stat *s,* * @relates tst_fzsync_pair */ -static void tst_fzsync_pair_update (struct tst_fzsync_pair *pair) +static inline void tst_fzsync_pair_update (struct tst_fzsync_pair *pair) {float alpha = pair->avg_alpha;float per_spin_time, time_delay;
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Richard Palethorpe
Reviewed-byHello,
Quoted 9 lines · Cyril Hrubis
Hi!
Quoted 8 lines
Quoted 1 line
Thanks! pushed. Please pull and try it out.
Looks like it fails on fuzzy sync since it uses
tst_but it's in an header.These definitions should be static inline and changing them so fixes the warnings. It looks like static inline functions does not make it into the symbol test at all.
Ah, I vaguely remember now that "static inline" is the correct way to include functions in header files. Otherwise they shouldn't be in header files. We have LTO now as well so possibly Fuzzy sync shouldn't be all in the header, but that's another matter.
Quoted 4 lines
This is even stranger, the 'static inline void' functions does not make it into the check function, but anything that returns a non-void value does get there so we need:
I have no idea...
Quoted 7 lines
tools/sparse/sparse-ltp.c +0
-0
tools/sparse/sparse-ltp.c static void check_symbol_visibility(const struct symbol *const sym) · line 104
Quoted 8 lines
tools/sparse/sparse-ltp.c +1
-1@@ -98,7 +98,7 @@ static void check_symbol_visibility(const struct symbol *const sym)if (!(mod &MOD_TOPLEVEL ))return ; -if (has_lib_prefix && (mod &MOD_STATIC )) { +if (has_lib_prefix && (mod &MOD_STATIC ) && !(mod &MOD_INLINE )) {warning (sym->pos,"LTP-003: Symbol '%s' has the LTP public library prefix, but is static (private)." , name);
OK, this loooks good and below. Could you send a patch? Also can add
Quoted 69 lines
And:
include/tst_fuzzy_sync.h +7
-7@@ -210,7 +210,7 @@ struct tst_fzsync_pair {* * @sa tst_fzsync_pair_reset() */ -static void tst_fzsync_pair_init (struct tst_fzsync_pair *pair) +static inline void tst_fzsync_pair_init (struct tst_fzsync_pair *pair) {CHK (avg_alpha,0 ,1 ,0.25 );CHK (min_samples,20 ,INT_MAX ,1024 );@@ -230,7 +230,7 @@ static void tst_fzsync_pair_init(struct tst_fzsync_pair *pair)* * Call this from your cleanup function. */ -static void tst_fzsync_pair_cleanup (struct tst_fzsync_pair *pair) +static inline void tst_fzsync_pair_cleanup (struct tst_fzsync_pair *pair) {if (pair->thread_b) {/* Revoke thread B if parent hits accidental break */ @@ -254,7 +254,7 @@ struct tst_fzsync_run_thread {* Wrap run_b for tst_fzsync_pair_reset to enable pthread cancel * at the start of the thread B. */ -static void *tst_fzsync_thread_wrapper (void *run_thread) +static inline void *tst_fzsync_thread_wrapper (void *run_thread) {struct tst_fzsync_run_thread t = *(struct tst_fzsync_run_thread *)run_thread;@@ -268,7 +268,7 @@ static void *tst_fzsync_thread_wrapper(void *run_thread)* * @relates tst_fzsync_stat */ -static void tst_init_stat (struct tst_fzsync_stat *s) +static inline void tst_init_stat (struct tst_fzsync_stat *s) { s->avg =0 ; s->avg_dev =0 ;@@ -292,7 +292,7 @@ static void tst_init_stat(struct tst_fzsync_stat *s)* * @sa tst_fzsync_pair_init() */ -static void tst_fzsync_pair_reset (struct tst_fzsync_pair *pair, +static inline void tst_fzsync_pair_reset (struct tst_fzsync_pair *pair,void *(*run_b)(void *)) {tst_fzsync_pair_cleanup (pair);@@ -340,7 +340,7 @@ static inline void tst_fzsync_stat_info(struct tst_fzsync_stat stat,* * @relates tst_fzsync_pair */ -static void tst_fzsync_pair_info (struct tst_fzsync_pair *pair) +static inline void tst_fzsync_pair_info (struct tst_fzsync_pair *pair) {tst_res (TINFO ,"loop = %d, delay_bias = %d" , pair->exec_loop, pair->delay_bias);@@ -493,7 +493,7 @@ static inline void tst_upd_diff_stat(struct tst_fzsync_stat *s,* * @relates tst_fzsync_pair */ -static void tst_fzsync_pair_update (struct tst_fzsync_pair *pair) +static inline void tst_fzsync_pair_update (struct tst_fzsync_pair *pair) {float alpha = pair->avg_alpha;float per_spin_time, time_delay;
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Thanks! pushed. Please pull and try it out.
Looks like it fails on fuzzy sync since it uses
tst_but it's in an header.
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
These definitions should be static inline and changing them so fixes the warnings. It looks like static inline functions does not make it into the symbol test at all.
Ah, I vaguely remember now that "static inline" is the correct way to include functions in header files. Otherwise they shouldn't be in header files. We have LTO now as well so possibly Fuzzy sync shouldn't be all in the header, but that's another matter.
If I recall correctly LTO is currently broken on ARM and even if that was working we still have to support 10 years old compilers anyways...
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
I guess that we can also silence some of the useless output by skipping some old library crap as:
tools/sparse/sparse-ltp.c +3 -0
@@ -98,6 +98,9 @@ static void check_symbol_visibility(const struct symbol *const sym)if (!(mod &MOD_TOPLEVEL ))return ; +if (!strcmp (name,"TCID" ) || !strcmp (name,"TST_TOTAL" )) +return ; +if (has_lib_prefix && (mod &MOD_STATIC ) && !(mod &MOD_INLINE )) { warning(sym->pos, "LTP-003: Symbol '%s' has the LTP public library prefix, but is static (private).",
Will send another patch.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hello,
Quoted 19 lines · Richard Palethorpe
This was adapted from Sparse's inbuilt
check_duplicates(-Wdecl). The original check appears to print a warning whenever a symbol is non-static, but has no prototype. It appears to work because library symbols are usually declared first in a header file and then again with their definition in a source file.The LTP version also checks for the various library prefixes, but should otherwise be the same.
Signed-off-by Richard Palethorpe <[email protected]>--- tools/sparse/sparse-ltp.c | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)tools/sparse/sparse-ltp.c +0
-0
tools/sparse/sparse-ltp.c static void do_entrypoint_checks(struct entrypoint *ep) · line 91
Quoted 10 lines
tools/sparse/sparse-ltp.c +7
-0@@ -82,6 +82,57 @@ static void do_entrypoint_checks(struct entrypoint *ep) }END_FOR_EACH_PTR (bb); } +/* Check for LTP-003 and LTP-004 +* +* Try to find cases where the static keyword was forgotten. +*/ +static void check_symbol_visibility (struct symbol *sym) +{ +struct symbol *next = sym;
Ooops, we don't need next anymore.
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Looks good to me as well.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 2/3 doc: Add LTP-003 and LTP-004 static and tst API prefix rules
In reply to Richard Palethorpe via ltp--- doc/library-api-writing-guidelines.txt | 10 ++++++++++ doc/rules.tsv | 2 ++ doc/test-writing-guidelines.txt | 8 ++++++++ 3 files changed, 20 insertions(+)
doc/library-api-writing-guidelines.txt +10 -0
@@ -39,6 +39,16 @@ The macros which are clearly intended to update these variables. That is +TEST+ and those in 'tst_test_macros.h'. Are of course allowed to update these variables. +2.3 LTP-003: Externally visible library symbols have the tst_ prefix +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Functions, types and variables in the public test API should have the +tst_ prefix. With some exceptions for symbols already prefixed with +safe_ or ltp_. + +Static (private) symbols should not have the prefix. + + 3. Shell API ------------
doc/rules.tsv +2 -0
@@ -1,3 +1,5 @@ ID DESCRIPTION LTP-001 Library source files have tst_ prefix LTP-002 TST_RET and TST_ERR are never modified by test library functions +LTP-003 Externally visible library symbols have the tst_ prefix +LTP-004 Test executable symbols are marked static
doc/test-writing-guidelines.txt +8 -0
@@ -133,6 +133,14 @@ script from kernel git tree. NOTE: If `make check` does not report any problems, the code still may be wrong as all tools used for checking only look for common mistakes. +2.1.1 LTP-004: Test executable symbols are marked static +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Test executables should not export symbols unecessarily. This means +that all top-level variables and functions should be marked with the +static keyword. The only visible symbols should be those included from +shared object files. + 2.2 Shell coding style ^^^^^^^^^^^^^^^^^^^^^^
Signature · 5 lines
2.33.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/3] doc: Add LTP-003 and LTP-004 static and tst API prefix rules
Hi Richie,
...
doc/library-api-writing-guidelines.txt The macros which are clearly intended to update these variables. That · line 47
Quoted 6 lines
+2.3 LTP-003: Externally visible library symbols have the
tst_prefix
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~doc/library-api-writing-guidelines.txt +4
-0+ +Functions, types and variables in the public test API should have the +tst_ prefix. With some exceptions for symbols already prefixed with +safe_ or ltp_.
BTW It'd be nice to have some check for shell library (maybe shellcheck would be able to do it).
...
doc/test-writing-guidelines.txt script from kernel git tree. · line 139
Quoted 4 lines
+2.1.1 LTP-004: Test executable symbols are marked static +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
doc/test-writing-guidelines.txt +2
-0+ +Test executables should not export symbols unecessarily. This means
typo s/unecessarily/unnecessarily/
doc/test-writing-guidelines.txt script from kernel git tree. · line 143
Quoted 4 lines
doc/test-writing-guidelines.txt +4
-0+that all top-level variables and functions should be marked with the +static keyword. The only visible symbols should be those included from +shared object files. +
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/3] doc: Add LTP-003 and LTP-004 static and tst API prefix rules
Hi!
Quoted 2 lines
BTW It'd be nice to have some check for shell library (maybe shellcheck would be able to do it).
Actually the check is in the shell library script.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/3] doc: Add LTP-003 and LTP-004 static and tst API prefix rules
Quoted 5 lines
Hi!
Quoted 2 lines
BTW It'd be nice to have some check for shell library (maybe shellcheck would be able to do it).
Actually the check is in the shell library script.
I'm avare only about the check in tst_run() which check that tests are not using variables starting with TST or _tst_ prefix, which is important.
My remark was about "LTP-003: Externally visible library symbols have the tst_ prefix", which is slightly different think. Not sure if can be scripted, thus feel free to ignore it.
I have more remarks on shell checking, but post it as a separate thread.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/3] doc: Add LTP-003 and LTP-004 static and tst API prefix rules
In reply to Richard Palethorpe via ltpHi!
Withe the typos fixed:
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 3/3 statx: Add missing static keyword to tcase variable
In reply to Richard Palethorpe via ltpThis shows how easy it is to miss the static keyword.
--- testcases/kernel/syscalls/statx/statx01.c | 2 +- testcases/kernel/syscalls/statx/statx02.c | 2 +- testcases/kernel/syscalls/statx/statx04.c | 2 +- testcases/kernel/syscalls/statx/statx05.c | 2 +- testcases/kernel/syscalls/statx/statx07.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-)
testcases/kernel/syscalls/statx/statx01.c +1 -1
@@ -131,7 +131,7 @@ static void test_device_file(void) } -struct tcase { +static struct tcase {void (*tfunc)(void ); } tcases[] = { {&test_normal_file},
testcases/kernel/syscalls/statx/statx02.c +1 -1
@@ -90,7 +90,7 @@ static void test_sym_link(void) "Statx symlink flag failed to work as expected"); } -struct tcase { +static struct tcase {void (*tfunc)(void ); } tcases[] = { {&test_empty_path},
testcases/kernel/syscalls/statx/statx04.c +1 -1
@@ -133,7 +133,7 @@ static void test_unflagged(void)tst_res (TFAIL ,"STATX_ATTR_NODUMP flag is set" ); } -struct test_cases { +static struct test_cases {void (*tfunc)(void ); } tcases[] = { {&test_flagged},
testcases/kernel/syscalls/statx/statx05.c +1 -1
@@ -74,7 +74,7 @@ static void test_unflagged(void)tst_res (TFAIL ,"STATX_ATTR_ENCRYPTED flag is set" ); } -struct test_cases { +static struct test_cases {void (*tfunc)(void ); } tcases[] = { {&test_flagged},
testcases/kernel/syscalls/statx/statx07.c +1 -1
@@ -84,7 +84,7 @@ static int get_mode(char *file_name, int flag_type, char *flag_name)return buf.stx_mode; } -const struct test_cases { +static const struct test_cases { int flag; char *flag_name; char *server_file;
Signature · 5 lines
2.33.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 3/3] statx: Add missing static keyword to tcase variable
Hi Richie,
obviously correct. Thanks!
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Cyril Hrubis
Reviewed-byRe: [LTP] [PATCH 3/3] statx: Add missing static keyword to tcase variable
Hi!
Quoted 3 lines
obviously correct. Thanks!
Reviewed-by Petr Vorel <[email protected]>
Second to this.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp