Skip to contentSkip to conversation

xenstore: Check format printf

PATCH In review Julien Grall 5 participants 16 messages Mar 5, 2021 – Mar 11, 2021

lore
Share & tools
lore
#0 lore raw

cover xenstore: Check format printf

From: Julien Grall <[email protected]>

This patch is a follow-up to Norbert's series [1].

The first patch will define PRINTF_ATTRIBUTE the same way everywhere. The second patch will check the format printf on a few more calls.

Both patches are candidate for 4.15. They only affects the build system and therefore would consider them low-risk.

Cheers,

[1] https://lore.kernel.org/xen-devel/[email protected]/

Julien Grall (2):
tools/xenstore: Consolidate PRINTF_ATTRIBUTE() in utils.h tools/xenstore: Check the format printf for xprintf() and

    barf{,_perror}()
 tools/xenstore/talloc.h | 15 ++-------------
 tools/xenstore/tdb.h    |  6 ++----
 tools/xenstore/utils.h  | 10 +++++++---
 3 files changed, 11 insertions(+), 20 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 tools/xenstore: Consolidate PRINTF_ATTRIBUTE() in utils.h

From: Julien Grall <[email protected]>

At the moment PRINTF_ATTRIBUTE() is defined in two places:
- tdb.h: Defined as a NOP
- talloc.h: Defined as a NOP for GCC older than 3.0 otherwise will add the attribute to check the printf format

Xen requires to build with minimum GCC 4.1 and we want to check the printf format for all the printf-like functions.

Only implement PRINTF_ATTRIBUTE() once in utils.h and drop the conditional check for GCC < 3.0.

---
 tools/xenstore/talloc.h | 15 ++-------------
 tools/xenstore/tdb.h    |  6 ++----
 tools/xenstore/utils.h  |  2 ++
 3 files changed, 6 insertions(+), 17 deletions(-)
tools/xenstore/talloc.h +2 -13
index 71a36e7be06b..a0f4bff25788 100644
@@ -26,6 +26,8 @@
 
 #include <sys/types.h>
 
+#include "utils.h"
+
 /* this is only needed for compatibility with the old talloc */
 typedef void TALLOC_CTX;
 
@@ -84,19 +86,6 @@ typedef void TALLOC_CTX;
 #define talloc_destroy(ctx) talloc_free(ctx)
 #endif
 
-#ifndef PRINTF_ATTRIBUTE
-#if (__GNUC__ >= 3)
-/** Use gcc attribute to check printf fns.  a1 is the 1-based index of
- * the parameter containing the format, and a2 the index of the first
- * argument. Note that some gcc 2.x versions don't handle this
- * properly **/
-#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
-#else
-#define PRINTF_ATTRIBUTE(a1, a2)
-#endif
-#endif
-
-
 /* The following definitions come from talloc.c  */
 void *_talloc(const void *context, size_t size);
 void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
tools/xenstore/tdb.h +2 -4
index 557cf727b869..ce3c7339f884 100644
@@ -1,6 +1,8 @@
 #ifndef __TDB_H__
 #define __TDB_H__
 
+#include "utils.h"
+
 /* 
    Unix SMB/CIFS implementation.
 
@@ -84,10 +86,6 @@ struct tdb_traverse_lock {
 	uint32_t hash;
 };
 
-#ifndef PRINTF_ATTRIBUTE
-#define PRINTF_ATTRIBUTE(a,b)
-#endif
-
 /* this is the context structure that is returned from a db open */
 typedef struct tdb_context {
 	char *name; /* the name of the database */
tools/xenstore/utils.h +2 -0
index df1cb9a3bac6..3dfb96b556dd 100644
@@ -27,6 +27,8 @@ static inline bool strends(const char *a, const char *b)
  */
 const char *dump_state_align(FILE *fp);
 
+#define PRINTF_ATTRIBUTE(a1, a2) __attribute__((format (printf, a1, a2)))
+
 void barf(const char *fmt, ...) __attribute__((noreturn));
 void barf_perror(const char *fmt, ...) __attribute__((noreturn));
 
Signature · 1 line
2.17.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH for-4.15 1/2] tools/xenstore: Consolidate PRINTF_ATTRIBUTE() in utils.h

Quoted 7 lines · Julien Grall

From: Julien Grall <[email protected]>

At the moment PRINTF_ATTRIBUTE() is defined in two places:
- tdb.h: Defined as a NOP
- talloc.h: Defined as a NOP for GCC older than 3.0 otherwise will add the attribute to check the printf format

Quoted 7 lines

Xen requires to build with minimum GCC 4.1 and we want to check the printf format for all the printf-like functions.

Only implement PRINTF_ATTRIBUTE() once in utils.h and drop the conditional check for GCC < 3.0.

Juergen

#3 lore raw

PATCH 1/2 tools/xenstore: Consolidate PRINTF_ATTRIBUTE() in utils.h

In reply to Julien Grall

Julien Grall writes ("[PATCH for-4.15 1/2] tools/xenstore: Consolidate PRINTF_ATTRIBUTE() in utils.h"):

Quoted 7 lines

From: Julien Grall <[email protected]>

At the moment PRINTF_ATTRIBUTE() is defined in two places:
- tdb.h: Defined as a NOP
- talloc.h: Defined as a NOP for GCC older than 3.0 otherwise will add the attribute to check the printf format

Quoted 5 lines

Xen requires to build with minimum GCC 4.1 and we want to check the printf format for all the printf-like functions.

Only implement PRINTF_ATTRIBUTE() once in utils.h and drop the conditional check for GCC < 3.0.

#4 lore raw

PATCH 2/2 tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

In reply to Julien Grall

From: Julien Grall <[email protected]>

Allow GCC to analyze the format printf for xprintf() and barf{,_perror}().

Take the opportunity to define __noreturn to make the prototype for barf{,_perror})() easier to read.

---
 tools/xenstore/utils.h | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/xenstore/utils.h +5 -3
index 3dfb96b556dd..ccfb9b8fb699 100644
@@ -29,10 +29,12 @@ const char *dump_state_align(FILE *fp);
 
 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__((format (printf, a1, a2)))
 
-void barf(const char *fmt, ...) __attribute__((noreturn));
-void barf_perror(const char *fmt, ...) __attribute__((noreturn));
+#define __noreturn __attribute__((noreturn))
 
-extern void (*xprintf)(const char *fmt, ...);
+void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+
+extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
 
 #define eprintf(_fmt, _args...) xprintf("[ERR] %s" _fmt, __FUNCTION__, ##_args)
 
Signature · 1 line
2.17.1
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

Quoted 9 lines · Julien Grall

From: Julien Grall <[email protected]>

Allow GCC to analyze the format printf for xprintf() and barf{,_perror}().

Take the opportunity to define __noreturn to make the prototype for barf{,_perror})() easier to read.

But I would prefer, if ...

Quoted 11 lines
---
  tools/xenstore/utils.h | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
tools/xenstore/utils.h +0 -0
index 3dfb96b556dd..ccfb9b8fb699 100644
@@ -29,10 +29,12 @@ const char *dump_state_align(FILE *fp);
 
  #define PRINTF_ATTRIBUTE(a1, a2) __attribute__((format (printf, a1, a2)))

tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37

Quoted 10 lines
tools/xenstore/utils.h +5 -3
 
-void barf(const char *fmt, ...) __attribute__((noreturn));
-void barf_perror(const char *fmt, ...) __attribute__((noreturn));
+#define __noreturn __attribute__((noreturn))
 
-extern void (*xprintf)(const char *fmt, ...);
+void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+
+extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);

... the extern here would be dropped.

Juergen

#6 lore raw

Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

Jürgen Groß writes ("Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()"):

Quoted 8 lines · Julien Grall

tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37

Quoted 5 lines
tools/xenstore/utils.h +4 -1
-extern void (*xprintf)(const char *fmt, ...);
+void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+
+extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);

... the extern here would be dropped.

With my RM hat on I don't have an opinion on that and my R-A can stand.

With my maintainer hat on I agree with Jürgen's style opinion - it's nicer without the "extern", but I'm also happy with the patch as is.

Ian.

#7 lore raw

Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

Hi Ian,

Quoted 10 lines · Ian Jackson

Jürgen Groß writes ("Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()"):

Quoted 8 lines · Julien Grall

tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37

Quoted 5 lines
tools/xenstore/utils.h +4 -1
-extern void (*xprintf)(const char *fmt, ...);
+void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+
+extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);

... the extern here would be dropped.

Quoted 5 lines

With my RM hat on I don't have an opinion on that and my R-A can stand.

With my maintainer hat on I agree with Jürgen's style opinion - it's nicer without the "extern", but I'm also happy with the patch as is.

I agree with Juergen's style opinion. I will do the modification on commit. I will also update the last sentence of the commit to:

"
Take the opportunity to:
* remove extern from the function prototype for consistency
* define __noreturn to make the prototype for
barf{,_perror})() easier to read.
"

Cheers,

Quoted 2 lines

Ian.

Signature · 1 line
Julien Grall
#8 lore raw

Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

In reply to Jürgen Groß
Quoted 12 lines · Jürgen Groß
Quoted 11 lines · Julien Grall

From: Julien Grall <[email protected]>

tools/xenstore/utils.h +1 -2
@@ -29,10 +29,12 @@ const char *dump_state_align(FILE *fp);
 
  #define PRINTF_ATTRIBUTE(a1, a2) __attribute__((format (printf, a1, a2)))
 
-void barf(const char *fmt, ...) __attribute__((noreturn));
-void barf_perror(const char *fmt, ...) __attribute__((noreturn));
+#define __noreturn __attribute__((noreturn))
 
Quoted 7 lines

tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37

Quoted 5 lines
tools/xenstore/utils.h +4 -1
-extern void (*xprintf)(const char *fmt, ...);
+void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+
+extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);

... the extern here would be dropped.

But this isn't a function declaration, but that of a data object. With the extern dropped, a common symbol will appear in every CU.

Jan

#9 lore raw

Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

Hi Jan,

Quoted 21 lines · Jan Beulich
Quoted 12 lines · Jürgen Groß
Quoted 11 lines · Julien Grall

From: Julien Grall <[email protected]>

tools/xenstore/utils.h +1 -2
@@ -29,10 +29,12 @@ const char *dump_state_align(FILE *fp);
 
   #define PRINTF_ATTRIBUTE(a1, a2) __attribute__((format (printf, a1, a2)))
 
-void barf(const char *fmt, ...) __attribute__((noreturn));
-void barf_perror(const char *fmt, ...) __attribute__((noreturn));
+#define __noreturn __attribute__((noreturn))
 
Quoted 7 lines

tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37

Quoted 5 lines
tools/xenstore/utils.h +4 -1
-extern void (*xprintf)(const char *fmt, ...);
+void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+
+extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);

... the extern here would be dropped.

Quoted 2 lines

But this isn't a function declaration, but that of a data object. With the extern dropped, a common symbol will appear in every CU.

Urgh, you are right. Actually, the extern was added recently by Anthony:

dacdbf7088d6a3705a9831e73991c2b14c519a65 ("tools/xenstore: mark variable in header as extern")

I completely forgot it despite I needed to backport the patch to our downstream Xen.

Cheers,

Signature · 1 line
Julien Grall
#10 lore raw

Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

Julien Grall writes ("Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()"):

Quoted 7 lines

Urgh, you are right. Actually, the extern was added recently by Anthony:

dacdbf7088d6a3705a9831e73991c2b14c519a65 ("tools/xenstore: mark variable in header as extern")

I completely forgot it despite I needed to backport the patch to our downstream Xen.

How horrible.

Maybe we could add a comment to the code, next to the declaration, about this crazy situation.

Ian.

#11 lore raw

Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

Hi Ian,

Quoted 6 lines · Ian Jackson

Julien Grall writes ("Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()"):

Quoted 5 lines

Urgh, you are right. Actually, the extern was added recently by Anthony:

dacdbf7088d6a3705a9831e73991c2b14c519a65 ("tools/xenstore: mark variable in header as extern")

Quoted 7 lines
Quoted 2 lines

I completely forgot it despite I needed to backport the patch to our downstream Xen.

How horrible.

Maybe we could add a comment to the code, next to the declaration, about this crazy situation.

Would the following comment work for you?

/* Function pointer as xprintf() can be configured at runtime. */

I can fold it in my patch while committing.

Cheers,

Quoted 3 lines

Ian.

Signature · 1 line
Julien Grall
Reviewed-by
#12 lore raw

Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

Julien Grall writes ("Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()"):

Quoted 5 lines

Would the following comment work for you?

/* Function pointer as xprintf() can be configured at runtime. */

I can fold it in my patch while committing.

Sure, thanks. FTAOD

to that comment addition.

Ian.

#13 lore raw

Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

Hi Ian,

Quoted 5 lines · Ian Jackson

Julien Grall writes ("Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()"):

Quoted 4 lines

Would the following comment work for you?

/* Function pointer as xprintf() can be configured at runtime. */

Quoted 8 lines
Quoted 1 line

I can fold it in my patch while committing.

Sure, thanks. FTAOD

to that comment addition.

Thanks! I have committed the two patches.

Cheers,

Signature · 1 line
Julien Grall
#14 lore raw

Re: [PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

In reply to Jan Beulich
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Jürgen Groß
Quoted 11 lines · Julien Grall

From: Julien Grall <[email protected]>

tools/xenstore/utils.h +1 -2
@@ -29,10 +29,12 @@ const char *dump_state_align(FILE *fp);
 
  #define PRINTF_ATTRIBUTE(a1, a2) __attribute__((format (printf, a1, a2)))
 
-void barf(const char *fmt, ...) __attribute__((noreturn));
-void barf_perror(const char *fmt, ...) __attribute__((noreturn));
+#define __noreturn __attribute__((noreturn))
 
Quoted 8 lines
Quoted 6 lines

tools/xenstore/utils.h const char *dump_state_align(FILE *fp); · line 37

Quoted 5 lines
tools/xenstore/utils.h +4 -1
-extern void (*xprintf)(const char *fmt, ...);
+void barf(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+void barf_perror(const char *fmt, ...) __noreturn PRINTF_ATTRIBUTE(1, 2);
+
+extern void (*xprintf)(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);

... the extern here would be dropped.

But this isn't a function declaration, but that of a data object. With the extern dropped, a common symbol will appear in every CU.

Correct, and some of the containers in Gitlab CI really ought to notice because GCC 9(? IIRC) defaulted to -fno-common.

~Andrew

#15 lore raw

PATCH 2/2 tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()

In reply to Julien Grall

Julien Grall writes ("[PATCH for-4.15 2/2] tools/xenstore: Check the format printf for xprintf() and barf{,_perror}()"):

Quoted 7 lines

From: Julien Grall <[email protected]>

Allow GCC to analyze the format printf for xprintf() and barf{,_perror}().

Take the opportunity to define __noreturn to make the prototype for barf{,_perror})() easier to read.