[v5,1/8] selftests: Add printf attribute to kselftest prints

Message ID 13a47130763d109aa40de153ecbee9ede22d8356.1697012398.git.maciej.wieczor-retman@intel.com
State New
Headers
Series Add printf attribute to kselftest functions |

Commit Message

Maciej Wieczor-Retman Oct. 11, 2023, 8:23 a.m. UTC
  Kselftest header defines multiple variadic functions that use printf
along with other logic.

There is no format checking for the variadic functions that use
printing inside kselftest.h. Because of this the compiler won't
be able to catch instances of mismatched printf formats and debugging
tests might be more difficult.

Add the common __printf attribute macro to kselftest.h.

Add __printf attribute to every function using formatted printing with
variadic arguments.

Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>
---
Changelog v4:
- Fix typo in patch subject. (Reinette)
- Add Reinette's reviewed-by tag.

 tools/testing/selftests/kselftest.h | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
  

Comments

Shuah Oct. 11, 2023, 7:40 p.m. UTC | #1
On 10/11/23 02:23, Maciej Wieczor-Retman wrote:
> Kselftest header defines multiple variadic functions that use printf
> along with other logic.
> 
> There is no format checking for the variadic functions that use
> printing inside kselftest.h. Because of this the compiler won't
> be able to catch instances of mismatched printf formats and debugging
> tests might be more difficult.
> 
> Add the common __printf attribute macro to kselftest.h.
> 
> Add __printf attribute to every function using formatted printing with
> variadic arguments.
> 
> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
> Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>
> ---
> Changelog v4:
> - Fix typo in patch subject. (Reinette)
> - Add Reinette's reviewed-by tag.
> 

I still need information on how you found these problems. Please
add it to change log for each of these patches.

I am seeing checkpatch warning:

WARNING: Prefer __printf(a, b) over __attribute__((format(printf, a, b)))
#102: FILE: tools/testing/selftests/kselftest.h:81:
+#define __printf(a, b)   __attribute__((format(printf, a, b)))

thanks,
-- Shuah
  
Maciej Wieczor-Retman Oct. 12, 2023, 7:32 a.m. UTC | #2
On 2023-10-11 at 13:40:48 -0600, Shuah wrote:
>On 10/11/23 02:23, Maciej Wieczor-Retman wrote:
>> Kselftest header defines multiple variadic functions that use printf
>> along with other logic.
>> 
>> There is no format checking for the variadic functions that use
>> printing inside kselftest.h. Because of this the compiler won't
>> be able to catch instances of mismatched printf formats and debugging
>> tests might be more difficult.
>> 
>> Add the common __printf attribute macro to kselftest.h.
>> 
>> Add __printf attribute to every function using formatted printing with
>> variadic arguments.
>> 
>> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
>> Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
>> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>
>> ---
>> Changelog v4:
>> - Fix typo in patch subject. (Reinette)
>> - Add Reinette's reviewed-by tag.
>> 
>
>I still need information on how you found these problems. Please
>add it to change log for each of these patches.

Sure, I'll add notes on methodology to patches 2-8. I understand that
this patch (1/8) message doesn't need that addition since the problems
it exposes are in separate patches.

Or would you like me to also note here more specifically what effect it
has in the rest of the series?

>I am seeing checkpatch warning:
>
>WARNING: Prefer __printf(a, b) over __attribute__((format(printf, a, b)))
>#102: FILE: tools/testing/selftests/kselftest.h:81:
>+#define __printf(a, b)   __attribute__((format(printf, a, b)))

Running checkpatch.pl with --show-types shows the
PREFER_DEFINED_ATTRIBUTE_MACRO is raised. From looking at the error
message in the script it looks like a false positive:
	"Prefer $new over __attribute__(($orig_attr$params))\n"

Please correct me if my train of thought is wrong but I think checkpatch
sees __printf() macro defined and it sees it's raw version
"__attribute__((format(printf, a, b)))" which it wants to replace with
the macro. But since the raw version is found in the define line that is
obviously not possible.

>thanks,
>-- Shuah
  
Ilpo Järvinen Oct. 12, 2023, 1:06 p.m. UTC | #3
On Thu, 12 Oct 2023, Maciej Wieczór-Retman wrote:

> On 2023-10-11 at 13:40:48 -0600, Shuah wrote:
> >On 10/11/23 02:23, Maciej Wieczor-Retman wrote:
> >> Kselftest header defines multiple variadic functions that use printf
> >> along with other logic.
> >> 
> >> There is no format checking for the variadic functions that use
> >> printing inside kselftest.h. Because of this the compiler won't
> >> be able to catch instances of mismatched printf formats and debugging
> >> tests might be more difficult.
> >> 
> >> Add the common __printf attribute macro to kselftest.h.
> >> 
> >> Add __printf attribute to every function using formatted printing with
> >> variadic arguments.
> >> 
> >> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
> >> Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> >> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>
> >> ---
> >> Changelog v4:
> >> - Fix typo in patch subject. (Reinette)
> >> - Add Reinette's reviewed-by tag.
> >> 
> >
> >I still need information on how you found these problems. Please
> >add it to change log for each of these patches.
> 
> Sure, I'll add notes on methodology to patches 2-8. I understand that
> this patch (1/8) message doesn't need that addition since the problems
> it exposes are in separate patches.
> 
> Or would you like me to also note here more specifically what effect it
> has in the rest of the series?
> 
> >I am seeing checkpatch warning:
> >
> >WARNING: Prefer __printf(a, b) over __attribute__((format(printf, a, b)))
> >#102: FILE: tools/testing/selftests/kselftest.h:81:
> >+#define __printf(a, b)   __attribute__((format(printf, a, b)))
> 
> Running checkpatch.pl with --show-types shows the
> PREFER_DEFINED_ATTRIBUTE_MACRO is raised. From looking at the error
> message in the script it looks like a false positive:
> 	"Prefer $new over __attribute__(($orig_attr$params))\n"
> 
> Please correct me if my train of thought is wrong but I think checkpatch
> sees __printf() macro defined and it sees it's raw version
> "__attribute__((format(printf, a, b)))" which it wants to replace with
> the macro. But since the raw version is found in the define line that is
> obviously not possible.

Yes, this is clearly a false positive from checkpatch. Checkpatch's logic 
cannot differentiate the definition from the use of __printf(), it just 
assumes __printf() is there already, which is not true for selftests.

The patch adds the capability to use __printf() elsewhere in the 
selftests code but of course the definition of __printf() itself has to 
use __attribute__().
  
Shuah Khan Oct. 12, 2023, 2:30 p.m. UTC | #4
On 10/12/23 01:32, Maciej Wieczór-Retman wrote:
> On 2023-10-11 at 13:40:48 -0600, Shuah wrote:
>> On 10/11/23 02:23, Maciej Wieczor-Retman wrote:
>>> Kselftest header defines multiple variadic functions that use printf
>>> along with other logic.
>>>
>>> There is no format checking for the variadic functions that use
>>> printing inside kselftest.h. Because of this the compiler won't
>>> be able to catch instances of mismatched printf formats and debugging
>>> tests might be more difficult.
>>>
>>> Add the common __printf attribute macro to kselftest.h.
>>>
>>> Add __printf attribute to every function using formatted printing with
>>> variadic arguments.
>>>
>>> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
>>> Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
>>> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>
>>> ---
>>> Changelog v4:
>>> - Fix typo in patch subject. (Reinette)
>>> - Add Reinette's reviewed-by tag.
>>>
>>
>> I still need information on how you found these problems. Please
>> add it to change log for each of these patches.
> 
> Sure, I'll add notes on methodology to patches 2-8. I understand that
> this patch (1/8) message doesn't need that addition since the problems
> it exposes are in separate patches.
>

Yes please. As mentioned a couple of times, I would like to see how
the problem is found in each patch commit log.

> Or would you like me to also note here more specifically what effect it
> has in the rest of the series?
> 

Yes please.

>> I am seeing checkpatch warning:
>>
>> WARNING: Prefer __printf(a, b) over __attribute__((format(printf, a, b)))
>> #102: FILE: tools/testing/selftests/kselftest.h:81:
>> +#define __printf(a, b)   __attribute__((format(printf, a, b)))
> 
> Running checkpatch.pl with --show-types shows the
> PREFER_DEFINED_ATTRIBUTE_MACRO is raised. From looking at the error
> message in the script it looks like a false positive:
> 	"Prefer $new over __attribute__(($orig_attr$params))\n"
> 
> Please correct me if my train of thought is wrong but I think checkpatch
> sees __printf() macro defined and it sees it's raw version
> "__attribute__((format(printf, a, b)))" which it wants to replace with
> the macro. But since the raw version is found in the define line that is
> obviously not possible.
> 

This is fine.

thanks,
-- Shuah
  
Maciej Wieczor-Retman Oct. 13, 2023, 11:41 a.m. UTC | #5
On 2023-10-12 at 08:30:37 -0600, Shuah Khan wrote:
>On 10/12/23 01:32, Maciej Wieczór-Retman wrote:
>> On 2023-10-11 at 13:40:48 -0600, Shuah wrote:
>> > On 10/11/23 02:23, Maciej Wieczor-Retman wrote:
>> > > Kselftest header defines multiple variadic functions that use printf
>> > > along with other logic.
>> > > 
>> > > There is no format checking for the variadic functions that use
>> > > printing inside kselftest.h. Because of this the compiler won't
>> > > be able to catch instances of mismatched printf formats and debugging
>> > > tests might be more difficult.
>> > > 
>> > > Add the common __printf attribute macro to kselftest.h.
>> > > 
>> > > Add __printf attribute to every function using formatted printing with
>> > > variadic arguments.
>> > > 
>> > > Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
>> > > Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
>> > > Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>
>> > > ---
>> > > Changelog v4:
>> > > - Fix typo in patch subject. (Reinette)
>> > > - Add Reinette's reviewed-by tag.
>> > > 
>> > 
>> > I still need information on how you found these problems. Please
>> > add it to change log for each of these patches.
>> 
>> Sure, I'll add notes on methodology to patches 2-8. I understand that
>> this patch (1/8) message doesn't need that addition since the problems
>> it exposes are in separate patches.
>> 
>
>Yes please. As mentioned a couple of times, I would like to see how
>the problem is found in each patch commit log.
>
>> Or would you like me to also note here more specifically what effect it
>> has in the rest of the series?
>> 
>
>Yes please.

Thanks for confirming. I went through all the patches, made corrections
and sent v6 of the series.
  

Patch

diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h
index af9f1202d423..5696199c16f9 100644
--- a/tools/testing/selftests/kselftest.h
+++ b/tools/testing/selftests/kselftest.h
@@ -78,6 +78,8 @@ 
 #define KSFT_XPASS 3
 #define KSFT_SKIP  4
 
+#define __printf(a, b)   __attribute__((format(printf, a, b)))
+
 /* counters */
 struct ksft_count {
 	unsigned int ksft_pass;
@@ -144,7 +146,7 @@  static inline void ksft_print_cnts(void)
 		ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
 }
 
-static inline void ksft_print_msg(const char *msg, ...)
+static inline __printf(1, 2) void ksft_print_msg(const char *msg, ...)
 {
 	int saved_errno = errno;
 	va_list args;
@@ -169,7 +171,7 @@  static inline void ksft_perror(const char *msg)
 #endif
 }
 
-static inline void ksft_test_result_pass(const char *msg, ...)
+static inline __printf(1, 2) void ksft_test_result_pass(const char *msg, ...)
 {
 	int saved_errno = errno;
 	va_list args;
@@ -183,7 +185,7 @@  static inline void ksft_test_result_pass(const char *msg, ...)
 	va_end(args);
 }
 
-static inline void ksft_test_result_fail(const char *msg, ...)
+static inline __printf(1, 2) void ksft_test_result_fail(const char *msg, ...)
 {
 	int saved_errno = errno;
 	va_list args;
@@ -209,7 +211,7 @@  static inline void ksft_test_result_fail(const char *msg, ...)
 		ksft_test_result_fail(fmt, ##__VA_ARGS__);\
 	} while (0)
 
-static inline void ksft_test_result_xfail(const char *msg, ...)
+static inline __printf(1, 2) void ksft_test_result_xfail(const char *msg, ...)
 {
 	int saved_errno = errno;
 	va_list args;
@@ -223,7 +225,7 @@  static inline void ksft_test_result_xfail(const char *msg, ...)
 	va_end(args);
 }
 
-static inline void ksft_test_result_skip(const char *msg, ...)
+static inline __printf(1, 2) void ksft_test_result_skip(const char *msg, ...)
 {
 	int saved_errno = errno;
 	va_list args;
@@ -238,7 +240,7 @@  static inline void ksft_test_result_skip(const char *msg, ...)
 }
 
 /* TODO: how does "error" differ from "fail" or "skip"? */
-static inline void ksft_test_result_error(const char *msg, ...)
+static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...)
 {
 	int saved_errno = errno;
 	va_list args;
@@ -285,7 +287,7 @@  static inline int ksft_exit_fail(void)
 		  ksft_cnt.ksft_xfail +	\
 		  ksft_cnt.ksft_xskip)
 
-static inline int ksft_exit_fail_msg(const char *msg, ...)
+static inline __printf(1, 2) int ksft_exit_fail_msg(const char *msg, ...)
 {
 	int saved_errno = errno;
 	va_list args;
@@ -312,7 +314,7 @@  static inline int ksft_exit_xpass(void)
 	exit(KSFT_XPASS);
 }
 
-static inline int ksft_exit_skip(const char *msg, ...)
+static inline __printf(1, 2) int ksft_exit_skip(const char *msg, ...)
 {
 	int saved_errno = errno;
 	va_list args;