mbox series

[v4,0/8] Add printf attribute to kselftest functions

Message ID cover.1696846568.git.maciej.wieczor-retman@intel.com (mailing list archive)
Headers show
Series Add printf attribute to kselftest functions | expand

Message

Maciej Wieczor-Retman Oct. 9, 2023, 10:28 a.m. UTC
Kselftest.h declares many variadic functions that can print some
formatted message while also executing selftest logic. These
declarations don't have any compiler mechanism to verify if passed
arguments are valid in comparison with format specifiers used in
printf() calls.

Attribute addition can make debugging easier, the code more consistent
and prevent mismatched or missing variables.

Add a __printf() macro that validates types of variables passed to the
format string. The macro is similarly used in other tools in the kernel.

Add __printf() attributes to function definitions inside kselftest.h that
use printing.

Adding the __printf() macro exposes some mismatches in format strings
across different selftests.

Fix the mismatched format specifiers in multiple tests.

Series is based on kselftests next branch.

Changelog v4:
- Fix patch 1/8 subject typo.
- Add Reinette's reviewed-by tags.
- Rebased onto updated kselftests next branch.

Changelog v3:
- Changed git signature from Wieczor-Retman Maciej to Maciej
  Wieczor-Retman.
- Added one review tag.
- Rebased onto updated kselftests next branch.

Changelog v2:
- Add review and fixes tags to patches.
- Add two patches with mismatch fixes.
- Fix missed attribute in selftests/kvm. (Andrew)
- Fix previously missed issues in selftests/mm (Ilpo)

[v3] https://lore.kernel.org/all/cover.1695373131.git.maciej.wieczor-retman@intel.com/
[v2] https://lore.kernel.org/all/cover.1693829810.git.maciej.wieczor-retman@intel.com/
[v1] https://lore.kernel.org/all/cover.1693216959.git.maciej.wieczor-retman@intel.com/

Maciej Wieczor-Retman (8):
  selftests: Add printf attribute to kselftest prints
  selftests/cachestat: Fix print_cachestat format
  selftests/openat2: Fix wrong format specifier
  selftests/pidfd: Fix ksft print formats
  selftests/sigaltstack: Fix wrong format specifier
  selftests/kvm: Replace attribute with macro
  selftests/mm: Substitute attribute with a macro
  selftests/resctrl: Fix wrong format specifier

 .../selftests/cachestat/test_cachestat.c       |  2 +-
 tools/testing/selftests/kselftest.h            | 18 ++++++++++--------
 .../testing/selftests/kvm/include/test_util.h  |  8 ++++----
 tools/testing/selftests/mm/mremap_test.c       |  2 +-
 tools/testing/selftests/mm/pkey-helpers.h      |  2 +-
 tools/testing/selftests/openat2/openat2_test.c |  2 +-
 .../selftests/pidfd/pidfd_fdinfo_test.c        |  2 +-
 tools/testing/selftests/pidfd/pidfd_test.c     | 12 ++++++------
 tools/testing/selftests/resctrl/cache.c        |  2 +-
 tools/testing/selftests/sigaltstack/sas.c      |  2 +-
 10 files changed, 27 insertions(+), 25 deletions(-)


base-commit: f1020c687153609f246f3314db5b74821025c185

Comments

Shuah Khan Oct. 9, 2023, 5:28 p.m. UTC | #1
On 10/9/23 04:28, Maciej Wieczor-Retman wrote:
> Kselftest.h declares many variadic functions that can print some
> formatted message while also executing selftest logic. These
> declarations don't have any compiler mechanism to verify if passed
> arguments are valid in comparison with format specifiers used in
> printf() calls.
> 
> Attribute addition can make debugging easier, the code more consistent
> and prevent mismatched or missing variables.
> 
> Add a __printf() macro that validates types of variables passed to the
> format string. The macro is similarly used in other tools in the kernel.
> 
> Add __printf() attributes to function definitions inside kselftest.h that
> use printing.
> 
> Adding the __printf() macro exposes some mismatches in format strings
> across different selftests.
> 
> Fix the mismatched format specifiers in multiple tests.
> 
> Series is based on kselftests next branch.

How did you find these problems? I don't see any information
how these problems are found in the commit logs.

thanks,
-- Shuah
Maciej Wieczor-Retman Oct. 10, 2023, 5:53 a.m. UTC | #2
On 2023-10-09 at 11:28:30 -0600, Shuah Khan wrote:
>On 10/9/23 04:28, Maciej Wieczor-Retman wrote:
>> Kselftest.h declares many variadic functions that can print some
>> formatted message while also executing selftest logic. These
>> declarations don't have any compiler mechanism to verify if passed
>> arguments are valid in comparison with format specifiers used in
>> printf() calls.
>> 
>> Attribute addition can make debugging easier, the code more consistent
>> and prevent mismatched or missing variables.
>> 
>> Add a __printf() macro that validates types of variables passed to the
>> format string. The macro is similarly used in other tools in the kernel.
>> 
>> Add __printf() attributes to function definitions inside kselftest.h that
>> use printing.
>> 
>> Adding the __printf() macro exposes some mismatches in format strings
>> across different selftests.
>> 
>> Fix the mismatched format specifiers in multiple tests.
>> 
>> Series is based on kselftests next branch.
>
>How did you find these problems? I don't see any information
>how these problems are found in the commit logs.
>
>thanks,
>-- Shuah

I wrote the first patch that adds the check to functions with format
specifiers and I compiled all selftests. Then I just corrected any
warnings that were found by the __printf attribute.

Should I mention the methodology in the cover letter?