Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp790324pxj; Thu, 13 May 2021 17:39:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzgKHye7TU/7NkuKC5jpUiyL+U/wyigoQHqrq27j7Lpy3e/oP0et3p8B52Nduli/BpB/V+w X-Received: by 2002:a05:6402:204c:: with SMTP id bc12mr52530418edb.35.1620952749032; Thu, 13 May 2021 17:39:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620952749; cv=none; d=google.com; s=arc-20160816; b=FaGQpy6fnqxTA8727h4dMpXj7+3hLN+HipSvUL8ZHWazjVfQuER0fqBHJHekLyumc0 c0ZgxuUAfgG+hBlIQNtuIs6t/HYoJmAA4Vkg+l1YOrTUAd1nbWkfoK3JfBYo1+TdUHEv /fGyUbIVZJluNcVNW4uuoc3E2Q4g1wyox5x9kxvzhFpLn9YhkUsGI/PZPzmriCIPGqKK cXvAOTPqmiBn77wDRUp1ALVl4QamEhVsA77qBA87HkCXF7UHiWTVENfc9+DRIffcE0Oe aih3ob8R8xCa/zwh6Q62N4QN4l8BDOZ6eStkWY/kHdkl2QJP5pAZbI54PVggi8PmGrWv e2Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=dGB51QrtZPdSxwxWBoGg4WEVZZlkiB0hLTHFQQSVKgw=; b=bjHYa+ufBEU2KYxxsWLah0CxTcy8WWGM8bYOQTUgn3I86whp4jxzLSsQpcEjRGn8SO /yYdcQAJ8XBRqV2lgQmKYsuOW7Aqm44fHUHKAOBSQ6QM1JsKxVY/Xf3ltfk6jkP/KiGl 0b5DHt6I7n+HqQUb0Koa7TVVr4Y/Zig2QuI/7vTxgTDoSRvMfGVOjI1ppNPAv2Pbgc4Z 3fuOk7uYlhRP//BQswK7iOo/6XZIOu0Zz43bA5G6oAKPE5KRxC/ZrKBQHPRWVEokBWYZ c0AhEDOiMT1SGrD3uCjue5kSh4FBJ1QVPr06iFnRHsARhGLyvM7f6fcQBYphbpAy1Fft ZlMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=KQWMq1IP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f25si4244291eja.351.2021.05.13.17.38.46; Thu, 13 May 2021 17:39:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=KQWMq1IP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233153AbhEMUtp (ORCPT + 99 others); Thu, 13 May 2021 16:49:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233149AbhEMUtk (ORCPT ); Thu, 13 May 2021 16:49:40 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43407C061574 for ; Thu, 13 May 2021 13:48:29 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id l18-20020a1ced120000b029014c1adff1edso419106wmh.4 for ; Thu, 13 May 2021 13:48:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=dGB51QrtZPdSxwxWBoGg4WEVZZlkiB0hLTHFQQSVKgw=; b=KQWMq1IP3OP2LY2wCYR9fJWa7W2Qy4qETloqS25tPcBiaqyvBBvNJebqlMWRJ21mAd mOJ6dnl85TULZhCwrIxtrph11kQ0dB1+UoNwqZhYwEo1xxpSB3apZUlS/DrhXjJ9jYCw VigbGH9yTCH+kmCKd/CMaYkquhzqXHiZC/r2BbM2f5jN8zDNiOyN4ncHqME4Zvb6pvMC NNclnZCrbnL9U3iDV7AM81S0vOquzgy4v01wkGyc2MKm2osJ45Lgd6tA9LBJH4ToOxOh h7jjyEWxW1Ewc8HBEeoeHReunSrl/XT6DLQPiifOIQKUkVUnQZWgwU2Tvb3PEHa2pbRS Lcng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=dGB51QrtZPdSxwxWBoGg4WEVZZlkiB0hLTHFQQSVKgw=; b=iTVUJi+iMpN/viMDtWihSEFQcHS01S0WnfWrJYyECRPQdsSPmuoip+XpDYN4uGoLUP H3fkCukmaonAOK1yYF3F+fNZrMlaG0B4aPMBcZ6u8fWSCACI90NlFIdXuNrT+4Mu2nna RmJCxMUVsZrFHO4ALXJE33NJi/csLFV784bKSPgS+E5OuEoDoXTkX4+bEYOyoLmS22GA 8rZNebvoIPUpV14rQ8o7uysKDXCo3vKbbitsk9TqiBRtWfqDbMBQxUW9G6TmG4UCjIGv SKC+mJ+SsnRMWHdBifc+i5LpO23mUvZQEkOCI0Bd5eLbsj0r6QXv6wqeuy28XHV0SuHR lQuQ== X-Gm-Message-State: AOAM531B9h/j/GxGwXzLD+csBI2dJDxq7CHOio/QsLzLSsrozqUubKTm VuiOTuPjMoE4i4FMmGFMGQWblHwhOw7GdExviNEwQw== X-Received: by 2002:a1c:cc17:: with SMTP id h23mr26925463wmb.129.1620938907795; Thu, 13 May 2021 13:48:27 -0700 (PDT) MIME-Version: 1.0 References: <20210513200350.854429-1-davidgow@google.com> In-Reply-To: From: David Gow Date: Fri, 14 May 2021 04:48:16 +0800 Message-ID: Subject: Re: [PATCH] kunit: Add gnu_printf specifiers To: Daniel Latypov Cc: Brendan Higgins , Shuah Khan , "open list:KERNEL SELFTEST FRAMEWORK" , KUnit Development , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 14, 2021 at 4:25 AM Daniel Latypov wrote: > > On Thu, May 13, 2021 at 1:03 PM 'David Gow' via KUnit Development > wrote: > > > > Some KUnit functions use variable arguments to implement a printf-like > > format string. Use the __printf() attribute to let the compiler warn if > > invalid format strings are passed in. > > > > If the kernel is build with W=3D1, it complained about the lack of thes= e > > specifiers, e.g.: > > ../lib/kunit/test.c:72:2: warning: function =E2=80=98kunit_log_append= =E2=80=99 might be a candidate for =E2=80=98gnu_printf=E2=80=99 format attr= ibute [-Wsuggest-attribute=3Dformat] > > > > Signed-off-by: David Gow > > Reviewed-by: Daniel Latypov > > As noted below, these additions don't really do anything. > Unfortunately, they just make compiler warnings noisier in the case of > kunit_log_append(). > > But if this silences a W=3D1 warning, then we should probably add them in= . > I guess it also serves as documentation that we're using the same > standard format specifiers and not something custom, which is nice. > Yeah: I did this to get rid of the W=3D1 warnings. I don't know if there's a way of doing this which would be less verbose: I do think that the format checking is worthwhile in general, even if we're hitting a few nasty cases where things are nested in macros. > > --- > > include/kunit/test.h | 2 +- > > lib/kunit/string-stream.h | 6 +++--- > > 2 files changed, 4 insertions(+), 4 deletions(-) > > > > diff --git a/include/kunit/test.h b/include/kunit/test.h > > index 49601c4b98b8..af2e386b867c 100644 > > --- a/include/kunit/test.h > > +++ b/include/kunit/test.h > > @@ -610,7 +610,7 @@ static inline void *kunit_kzalloc(struct kunit *tes= t, size_t size, gfp_t gfp) > > > > void kunit_cleanup(struct kunit *test); > > > > -void kunit_log_append(char *log, const char *fmt, ...); > > +void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...); > > Before this patch: > ../lib/kunit/kunit-example-test.c: In function =E2=80=98example_simple_te= st=E2=80=99: > ../include/linux/kern_levels.h:5:18: warning: format =E2=80=98%s=E2=80=99= expects > argument of type =E2=80=98char *=E2=80=99, but argument 3 has type =E2=80= =98int=E2=80=99 [-Wformat=3D] > 5 | #define KERN_SOH "\001" /* ASCII Start Of Header */ > | ^~~~~~ > ../include/kunit/test.h:622:10: note: in definition of macro =E2=80=98kun= it_log=E2=80=99 > 622 | printk(lvl fmt, ##__VA_ARGS__); \ > | ^~~ > ../include/kunit/test.h:641:2: note: in expansion of macro =E2=80=98kunit= _printk=E2=80=99 > 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) > | ^~~~~~~~~~~~ > ../include/linux/kern_levels.h:14:19: note: in expansion of macro =E2=80= =98KERN_SOH=E2=80=99 > 14 | #define KERN_INFO KERN_SOH "6" /* informational */ > | ^~~~~~~~ > ../include/kunit/test.h:641:15: note: in expansion of macro =E2=80=98KERN= _INFO=E2=80=99 > 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) > | ^~~~~~~~~ > ../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro =E2= =80=98kunit_info=E2=80=99 > 23 | kunit_info(test, "invalid: %s", 42); > > After this patch, it gets noisier: > In file included from ../lib/kunit/kunit-example-test.c:9: > ../lib/kunit/kunit-example-test.c: In function =E2=80=98example_simple_te= st=E2=80=99: > ../include/linux/kern_levels.h:5:18: warning: format =E2=80=98%s=E2=80=99= expects > argument of type =E2=80=98char *=E2=80=99, but argument 3 has type =E2=80= =98int=E2=80=99 [-Wformat=3D] > 5 | #define KERN_SOH "\001" /* ASCII Start Of Header */ > | ^~~~~~ > ../include/kunit/test.h:622:10: note: in definition of macro =E2=80=98kun= it_log=E2=80=99 > 622 | printk(lvl fmt, ##__VA_ARGS__); \ > | ^~~ > ../include/kunit/test.h:641:2: note: in expansion of macro =E2=80=98kunit= _printk=E2=80=99 > 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) > | ^~~~~~~~~~~~ > ../include/linux/kern_levels.h:14:19: note: in expansion of macro =E2=80= =98KERN_SOH=E2=80=99 > 14 | #define KERN_INFO KERN_SOH "6" /* informational */ > | ^~~~~~~~ > ../include/kunit/test.h:641:15: note: in expansion of macro =E2=80=98KERN= _INFO=E2=80=99 > 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) > | ^~~~~~~~~ > ../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro =E2= =80=98kunit_info=E2=80=99 > 23 | kunit_info(test, "invalid: %s", 42); > | ^~~~~~~~~~ > ../include/kunit/test.h:105:31: warning: format =E2=80=98%s=E2=80=99 expe= cts argument > of type =E2=80=98char *=E2=80=99, but argument 4 has type =E2=80=98int=E2= =80=99 [-Wformat=3D] > 105 | #define KUNIT_SUBTEST_INDENT " " > | ^~~~~~ > ../include/kunit/test.h:623:42: note: in definition of macro =E2=80=98kun= it_log=E2=80=99 > 623 | kunit_log_append((test_or_suite)->log, fmt "\n", \ > | ^~~ > ../include/kunit/test.h:628:23: note: in expansion of macro > =E2=80=98KUNIT_SUBTEST_INDENT=E2=80=99 > 628 | kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \ > | ^~~~~~~~~~~~~~~~~~~~ > ../include/kunit/test.h:641:2: note: in expansion of macro =E2=80=98kunit= _printk=E2=80=99 > 641 | kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) > | ^~~~~~~~~~~~ > ../lib/kunit/kunit-example-test.c:23:2: note: in expansion of macro =E2= =80=98kunit_info=E2=80=99 > 23 | kunit_info(test, "invalid: %s", 42); > | ^~~~~~~~~~ > > Yeah: that is pretty ugly. TBH, it was pretty ugly beforehand, and this does make it worse. I guess that's the price we pay for having so many nested macros, as well. Personally, I suspect this is still worth it to get rid of the compiler warnings, but only just. > > > > /* > > * printk and log to per-test or per-suite log buffer. Logging only d= one > > diff --git a/lib/kunit/string-stream.h b/lib/kunit/string-stream.h > > index fe98a00b75a9..5e94b623454f 100644 > > --- a/lib/kunit/string-stream.h > > +++ b/lib/kunit/string-stream.h > > @@ -35,9 +35,9 @@ struct string_stream *alloc_string_stream(struct kuni= t *test, gfp_t gfp); > > int __printf(2, 3) string_stream_add(struct string_stream *stream, > > const char *fmt, ...); > > > > -int string_stream_vadd(struct string_stream *stream, > > - const char *fmt, > > - va_list args); > > +int __printf(2, 0) string_stream_vadd(struct string_stream *stream, > > + const char *fmt, > > + va_list args); > > This is never called with a literal `fmt` string. > It's currently only ever called through the _add variant, which does > have __printf(2,3). > > So this can't catch any mistakes currently. > And I think it's hard to imagine we'd ever pass in a literal format > string w/ a va_list. > Yeah: I was tempted to leave this one out, but it was triggering warnings with the "you should use __printf()" heuristic. In fact, it had two warnings. The __printf() specifier documentation does specifically call out cases where a va_list is passed in as a case to use '0' for the positional argument, but only the format string is checked for validity: there's no typechecking. > > > > char *string_stream_get_string(struct string_stream *stream); > > > > -- > > 2.31.1.751.gd2f1c929bd-goog > > > > -- > > You received this message because you are subscribed to the Google Grou= ps "KUnit Development" group. > > To unsubscribe from this group and stop receiving emails from it, send = an email to kunit-dev+unsubscribe@googlegroups.com. > > To view this discussion on the web visit https://groups.google.com/d/ms= gid/kunit-dev/20210513200350.854429-1-davidgow%40google.com.