Skip to contentSkip to conversation

libbpf: hashmap.h update to fix build issues using LLVM14

PATCH Applied Eduard Zingerman 4 participants 8 messages Nov 10, 2022 – Nov 11, 2022

Subsystems (3)
lore
Share & tools
lore

Eduard Zingerman

author maintainer
#0 lore raw

PATCH 1/1 libbpf: hashmap.h update to fix build issues using LLVM14

A fix for the LLVM compilation error while building bpftool. Replaces the expression:

  _Static_assert((p) == NULL || ...)

by expression:

  _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) || ...)

When "p" is not a constant the former is not considered to be a constant expression by LLVM 14.

The error was introduced in the following patch-set: [1]. The error was reported here: [2].

[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

---
 tools/lib/bpf/hashmap.h   | 3 ++-
 tools/perf/util/hashmap.h | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
tools/lib/bpf/hashmap.h +2 -1
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
 #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			\
+				sizeof(*(p)) == sizeof(long),				\
 		       #p " pointee should be a long-sized integer or a pointer");	\
 	(long *)(p);									\
 })
tools/perf/util/hashmap.h +2 -1
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
 #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			\
+				sizeof(*(p)) == sizeof(long),				\
 		       #p " pointee should be a long-sized integer or a pointer");	\
 	(long *)(p);									\
 })
Signature · 1 line
2.34.1
#1 lore raw
Quoted 8 lines · Eduard Zingerman

A fix for the LLVM compilation error while building bpftool. Replaces the expression:

   _Static_assert((p) == NULL || ...)

by expression:

   _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) || ...)

IIUC, when __builtin_constant_p(p) returns false, we just ignore the NULL check? Do we have cases like that? If no, maybe it's safer to fail?

s/(p) == NULL : 0/(p) == NULL : 1/ ?

Quoted 46 lines

When "p" is not a constant the former is not considered to be a constant expression by LLVM 14.

The error was introduced in the following patch-set: [1]. The error was reported here: [2].

[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

---
  tools/lib/bpf/hashmap.h   | 3 ++-
  tools/perf/util/hashmap.h | 3 ++-
  2 files changed, 4 insertions(+), 2 deletions(-)
tools/lib/bpf/hashmap.h +2 -1
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
  };
 
  #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			\
+				sizeof(*(p)) == sizeof(long),				\
  		       #p " pointee should be a long-sized integer or a pointer");	\
  	(long *)(p);									\
  })
tools/perf/util/hashmap.h +2 -2
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
  };
 
  #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			\
+				sizeof(*(p)) == sizeof(long),				\
  		       #p " pointee should be a long-sized integer or a pointer");	\
  	(long *)(p);									\
  })
--

2.34.1

#2 lore raw
Quoted 6 lines · Stanislav Fomichev
Quoted 5 lines · Eduard Zingerman

A fix for the LLVM compilation error while building bpftool. Replaces the expression:

  _Static_assert((p) == NULL || ...)
Quoted 8 lines
Quoted 3 lines

by expression:

  _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) || ...)

IIUC, when __builtin_constant_p(p) returns false, we just ignore the NULL check? Do we have cases like that? If no, maybe it's safer to fail?

s/(p) == NULL : 0/(p) == NULL : 1/ ?

I'm probably missing something, can you pls clarify? So the error is as follows:

Quoted 1 line
Quoted 1 line

btf_dump.c:1546:2: error: static_assert expression is not an integral constant expression

hashmap__find(name_map, orig_name, &dup_cnt); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./hashmap.h:169:35: note: expanded from macro 'hashmap__find' hashmap_find((map), (long)(key), hashmap_cast_ptr(value)) ^~~~~~~~~~~~~~~~~~~~~~~
./hashmap.h:126:17: note: expanded from macro 'hashmap_cast_ptr' _Static_assert((p) == NULL || sizeof(*(p)) ==
sizeof(long), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
btf_dump.c:1546:2: note: cast from 'void *' is not allowed in a constant
expression
./hashmap.h:169:35: note: expanded from macro 'hashmap__find' hashmap_find((map), (long)(key), hashmap_cast_ptr(value))

This line in particular:

btf_dump.c:1546:2: note: cast from 'void *' is not allowed in a constant
expression

And the code does:

  size_t dup_cnt = 0;
  hashmap__find(name_map, orig_name, &dup_cnt);

So where is that cast from 'void *' is happening? Is it the NULL check itself?

Are we simply guarding against the user calling hashmap_cast_ptr with explicit NULL argument?

Quoted 48 lines
Quoted 10 lines

When "p" is not a constant the former is not considered to be a constant expression by LLVM 14.

The error was introduced in the following patch-set: [1]. The error was reported here: [2].

[1]

https://lore.kernel.org/bpf/[email protected]/

Quoted 37 lines

[2] https://lore.kernel.org/all/[email protected]/

---
 tools/lib/bpf/hashmap.h   | 3 ++-
 tools/perf/util/hashmap.h | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
tools/lib/bpf/hashmap.h +2 -1
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
 #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			\
+				sizeof(*(p)) == sizeof(long),				\
 		       #p " pointee should be a long-sized integer or a pointer");	\
 	(long *)(p);									\
 })
tools/perf/util/hashmap.h +2 -2
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
 #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			\
+				sizeof(*(p)) == sizeof(long),				\
 		       #p " pointee should be a long-sized integer or a pointer");	\
 	(long *)(p);									\
 })
--

2.34.1

author maintainer
#3 lore raw
Quoted 43 lines · On Thu, 2022-11-10 at 17:34 -0800, [email protected] wrote:
Quoted 8 lines · Stanislav Fomichev
Quoted 7 lines · Eduard Zingerman

A fix for the LLVM compilation error while building bpftool. Replaces the expression:

  _Static_assert((p) == NULL || ...)

by expression:

Quoted 7 lines
Quoted 1 line
  _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) || ...)

IIUC, when __builtin_constant_p(p) returns false, we just ignore the NULL check?
Do we have cases like that? If no, maybe it's safer to fail?

s/(p) == NULL : 0/(p) == NULL : 1/ ?

I'm probably missing something, can you pls clarify? So the error is as follows:

Quoted 2 lines
Quoted 2 lines

btf_dump.c:1546:2: error: static_assert expression is not an integral constant expression

    hashmap__find(name_map, orig_name, &dup_cnt);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ./hashmap.h:169:35: note: expanded from macro 'hashmap__find'
    hashmap_find((map), (long)(key), hashmap_cast_ptr(value))
    ^~~~~~~~~~~~~~~~~~~~~~~
    ./hashmap.h:126:17: note: expanded from macro 'hashmap_cast_ptr'
    _Static_assert((p) == NULL || sizeof(*(p)) ==  

sizeof(long),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
btf_dump.c:1546:2: note: cast from 'void *' is not allowed in a constant
expression

    ./hashmap.h:169:35: note: expanded from macro 'hashmap__find'
    hashmap_find((map), (long)(key), hashmap_cast_ptr(value))

This line in particular:

btf_dump.c:1546:2: note: cast from 'void *' is not allowed in a constant
expression

And the code does:

Quoted 8 lines
   size_t dup_cnt = 0;
   hashmap__find(name_map, orig_name, &dup_cnt);

So where is that cast from 'void *' is happening? Is it the NULL check itself?

Are we simply guarding against the user calling hashmap_cast_ptr with explicit NULL argument?

In case if (p) is not a constant I want the second part of the || to kick-in. The complete condition looks as follows:

  _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) || \
			sizeof(*(p)) == sizeof(long), "...error...")

The intent is to check that (p) is either NULL or a pointer to something of size long. So, if (p) is not a constant the expression would be equivalent to:

  _Static_assert(0 || sizeof(*(p)) == sizeof(long), "...error...")

I just tried the following:

	struct hashmap *name_map;
	char x; // not a constant, wrong pointer size
	...
	hashmap__find(name_map, orig_name, &x);

And it fails with an error message as intended:

btf_dump.c:1548:2: error: static_assert failed due to requirement '(__builtin_constant_p((&x)) ? (&x) == ((void *)0) : 0) || sizeof (*(&x)) == sizeof(long)' "&x pointee should be a long-sized integer or a pointer"

        hashmap__find(name_map, orig_name, &x);
./hashmap.h:170:35: note: expanded from macro 'hashmap__find'
        hashmap_find((map), (long)(key), hashmap_cast_ptr(value))
./hashmap.h:126:2: note: expanded from macro 'hashmap_cast_ptr'
        _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||
Quoted 49 lines
Quoted 49 lines
Quoted 11 lines

When "p" is not a constant the former is not considered to be a constant expression by LLVM 14.

The error was introduced in the following patch-set: [1]. The error was reported here: [2].

[1]

https://lore.kernel.org/bpf/[email protected]/

Quoted 37 lines

[2] https://lore.kernel.org/all/[email protected]/

---
 tools/lib/bpf/hashmap.h   | 3 ++-
 tools/perf/util/hashmap.h | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
tools/lib/bpf/hashmap.h +2 -1
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
 #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			\
+				sizeof(*(p)) == sizeof(long),				\
 		       #p " pointee should be a long-sized integer or a pointer");	\
 	(long *)(p);									\
 })
tools/perf/util/hashmap.h +2 -2
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
 #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			\
+				sizeof(*(p)) == sizeof(long),				\
 		       #p " pointee should be a long-sized integer or a pointer");	\
 	(long *)(p);									\
 })
--

2.34.1

#4 lore raw
Quoted 77 lines · Eduard Zingerman
Quoted 10 lines · On Thu, 2022-11-10 at 17:34 -0800, [email protected] wrote:
Quoted 9 lines · Stanislav Fomichev
Quoted 8 lines · Eduard Zingerman

A fix for the LLVM compilation error while building bpftool. Replaces the expression:

  _Static_assert((p) == NULL || ...)

by expression:

_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) |

| ...)

Quoted 2 lines
Quoted 2 lines

IIUC, when __builtin_constant_p(p) returns false, we just ignore the

NULL

Quoted 9 lines
Quoted 4 lines

check?
Do we have cases like that? If no, maybe it's safer to fail?

s/(p) == NULL : 0/(p) == NULL : 1/ ?

I'm probably missing something, can you pls clarify? So the error is as follows:

Quoted 1 line
Quoted 1 line

btf_dump.c:1546:2: error: static_assert expression is not an

integral

Quoted 11 lines
Quoted 1 line
Quoted 1 line

constant expression

    hashmap__find(name_map, orig_name, &dup_cnt);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ./hashmap.h:169:35: note: expanded from macro 'hashmap__find'
    hashmap_find((map), (long)(key), hashmap_cast_ptr(value))
    ^~~~~~~~~~~~~~~~~~~~~~~
    ./hashmap.h:126:17: note: expanded from macro 'hashmap_cast_ptr'
    _Static_assert((p) == NULL || sizeof(*(p)) ==

sizeof(long), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
btf_dump.c:1546:2: note: cast from 'void *' is not allowed in a

constant

Quoted 7 lines

expression

    ./hashmap.h:169:35: note: expanded from macro 'hashmap__find'
    hashmap_find((map), (long)(key), hashmap_cast_ptr(value))

This line in particular:

btf_dump.c:1546:2: note: cast from 'void *' is not allowed in a

constant

Quoted 4 lines

expression

And the code does:

Quoted 8 lines
   size_t dup_cnt = 0;
   hashmap__find(name_map, orig_name, &dup_cnt);

So where is that cast from 'void *' is happening? Is it the NULL check itself?

Are we simply guarding against the user calling hashmap_cast_ptr with explicit NULL argument?

In case if (p) is not a constant I want the second part of the || to kick-in. The complete condition looks as follows:

   _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) || \
			sizeof(*(p)) == sizeof(long), "...error...")

The intent is to check that (p) is either NULL or a pointer to something of size long. So, if (p) is not a constant the expression would be equivalent to:

   _Static_assert(0 || sizeof(*(p)) == sizeof(long), "...error...")

I just tried the following:

	struct hashmap *name_map;
	char x; // not a constant, wrong pointer size
	...
	hashmap__find(name_map, orig_name, &x);
Quoted 8 lines

And it fails with an error message as intended:

btf_dump.c:1548:2: error: static_assert failed due to requirement '(__builtin_constant_p((&x)) ? (&x) == ((void *)0) : 0) || sizeof (*(&x)) == sizeof(long)' "&x pointee should be a long-sized integer or a pointer"

         hashmap__find(name_map, orig_name, &x);
./hashmap.h:170:35: note: expanded from macro 'hashmap__find'
         hashmap_find((map), (long)(key), hashmap_cast_ptr(value))
./hashmap.h:126:2: note: expanded from macro 'hashmap_cast_ptr'
         _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||

Awesome, thanks for clarifying!

Quoted 53 lines
Quoted 25 lines
Quoted 25 lines
Quoted 11 lines

When "p" is not a constant the former is not considered to be a constant expression by LLVM 14.

The error was introduced in the following patch-set: [1]. The error was reported here: [2].

[1]

https://lore.kernel.org/bpf/[email protected]/

Quoted 13 lines

[2] https://lore.kernel.org/all/[email protected]/

---
 tools/lib/bpf/hashmap.h   | 3 ++-
 tools/perf/util/hashmap.h | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
tools/lib/bpf/hashmap.h +0 -0
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
Quoted 3 lines
Quoted 3 lines

tools/lib/bpf/hashmap.h enum hashmap_insert_strategy {

Quoted 3 lines
tools/lib/bpf/hashmap.h +1 -1
 #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			 

\

Quoted 2 lines
Quoted 2 lines

tools/lib/bpf/hashmap.h enum hashmap_insert_strategy { · line 128

Quoted 2 lines
tools/lib/bpf/hashmap.h +1 -0
+				sizeof(*(p)) == sizeof(long),				\
 		       #p " pointee should be a long-sized integer or a  

pointer"); \

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
tools/lib/bpf/hashmap.h +0 -0
 	(long *)(p);									\
 })
tools/perf/util/hashmap.h +0 -0
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
Quoted 3 lines
Quoted 3 lines

tools/perf/util/hashmap.h enum hashmap_insert_strategy { · line 126

Quoted 3 lines
tools/perf/util/hashmap.h +1 -1
 #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			 

\

Quoted 2 lines
Quoted 2 lines

tools/perf/util/hashmap.h enum hashmap_insert_strategy { · line 128

Quoted 2 lines
tools/perf/util/hashmap.h +1 -0
+				sizeof(*(p)) == sizeof(long),				\
 		       #p " pointee should be a long-sized integer or a  

pointer"); \

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/perf/util/hashmap.h +0 -1
 	(long *)(p);									\
 })
--

2.34.1

#5 lore raw
Quoted 92 lines · On Fri, Nov 11, 2022 at 9:19 AM <[email protected]> wrote:
Quoted 84 lines · Eduard Zingerman
Quoted 10 lines · On Thu, 2022-11-10 at 17:34 -0800, [email protected] wrote:
Quoted 9 lines · Stanislav Fomichev
Quoted 8 lines · Eduard Zingerman

A fix for the LLVM compilation error while building bpftool. Replaces the expression:

  _Static_assert((p) == NULL || ...)

by expression:

_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) |

| ...)

Quoted 2 lines
Quoted 2 lines

IIUC, when __builtin_constant_p(p) returns false, we just ignore the

NULL

Quoted 9 lines
Quoted 4 lines

check?
Do we have cases like that? If no, maybe it's safer to fail?

s/(p) == NULL : 0/(p) == NULL : 1/ ?

I'm probably missing something, can you pls clarify? So the error is as follows:

Quoted 1 line
Quoted 1 line

btf_dump.c:1546:2: error: static_assert expression is not an

integral

Quoted 11 lines
Quoted 1 line
Quoted 1 line

constant expression

    hashmap__find(name_map, orig_name, &dup_cnt);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ./hashmap.h:169:35: note: expanded from macro 'hashmap__find'
    hashmap_find((map), (long)(key), hashmap_cast_ptr(value))
    ^~~~~~~~~~~~~~~~~~~~~~~
    ./hashmap.h:126:17: note: expanded from macro 'hashmap_cast_ptr'
    _Static_assert((p) == NULL || sizeof(*(p)) ==

sizeof(long), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
btf_dump.c:1546:2: note: cast from 'void *' is not allowed in a

constant

Quoted 7 lines

expression

    ./hashmap.h:169:35: note: expanded from macro 'hashmap__find'
    hashmap_find((map), (long)(key), hashmap_cast_ptr(value))

This line in particular:

btf_dump.c:1546:2: note: cast from 'void *' is not allowed in a

constant

Quoted 4 lines

expression

And the code does:

Quoted 8 lines
   size_t dup_cnt = 0;
   hashmap__find(name_map, orig_name, &dup_cnt);

So where is that cast from 'void *' is happening? Is it the NULL check itself?

Are we simply guarding against the user calling hashmap_cast_ptr with explicit NULL argument?

In case if (p) is not a constant I want the second part of the || to kick-in.
The complete condition looks as follows:

   _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) || \
                      sizeof(*(p)) == sizeof(long), "...error...")

The intent is to check that (p) is either NULL or a pointer to something of size long. So, if (p) is not a constant the expression would be equivalent to:

   _Static_assert(0 || sizeof(*(p)) == sizeof(long), "...error...")

I just tried the following:

      struct hashmap *name_map;
      char x; // not a constant, wrong pointer size
      ...
      hashmap__find(name_map, orig_name, &x);

And it fails with an error message as intended:

btf_dump.c:1548:2: error: static_assert failed due to requirement '(__builtin_constant_p((&x)) ? (&x) == ((void *)0) : 0) || sizeof (*(&x)) == sizeof(long)' "&x pointee should be a long-sized integer or a pointer"

Quoted 5 lines
         hashmap__find(name_map, orig_name, &x);
./hashmap.h:170:35: note: expanded from macro 'hashmap__find'
         hashmap_find((map), (long)(key), hashmap_cast_ptr(value))
./hashmap.h:126:2: note: expanded from macro 'hashmap_cast_ptr'
         _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||
Quoted 4 lines

Awesome, thanks for clarifying!

Added

Fixes c302378bc157 ("libbpf: Hashmap interface update to allow both

long and void* keys/values")

and moved links to be before Signed-off-by (they should be last).

Pushed to bpf-next, thanks.

Quoted 54 lines
Quoted 53 lines
Quoted 25 lines
Quoted 25 lines
Quoted 11 lines

When "p" is not a constant the former is not considered to be a constant expression by LLVM 14.

The error was introduced in the following patch-set: [1]. The error was reported here: [2].

[1]

https://lore.kernel.org/bpf/[email protected]/

Quoted 13 lines

[2] https://lore.kernel.org/all/[email protected]/

---
 tools/lib/bpf/hashmap.h   | 3 ++-
 tools/perf/util/hashmap.h | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
tools/lib/bpf/hashmap.h +0 -0
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
Quoted 3 lines
Quoted 3 lines

tools/lib/bpf/hashmap.h enum hashmap_insert_strategy {

Quoted 3 lines
tools/lib/bpf/hashmap.h +1 -1
 #define hashmap_cast_ptr(p) ({                                                         \
-       _Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),                     \
+       _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||

\

Quoted 2 lines
Quoted 2 lines

tools/lib/bpf/hashmap.h enum hashmap_insert_strategy { · line 128

Quoted 2 lines
tools/lib/bpf/hashmap.h +1 -0
+                               sizeof(*(p)) == sizeof(long),                           \
                       #p " pointee should be a long-sized integer or a

pointer"); \

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
tools/lib/bpf/hashmap.h +0 -0
        (long *)(p);                                                                    \
 })
tools/perf/util/hashmap.h +0 -0
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
Quoted 3 lines
Quoted 3 lines

tools/perf/util/hashmap.h enum hashmap_insert_strategy { · line 126

Quoted 3 lines
tools/perf/util/hashmap.h +1 -1
 #define hashmap_cast_ptr(p) ({                                                         \
-       _Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),                     \
+       _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||

\

Quoted 2 lines
Quoted 2 lines

tools/perf/util/hashmap.h enum hashmap_insert_strategy { · line 128

Quoted 2 lines
tools/perf/util/hashmap.h +1 -0
+                               sizeof(*(p)) == sizeof(long),                           \
                       #p " pointee should be a long-sized integer or a

pointer"); \

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/perf/util/hashmap.h +0 -1
        (long *)(p);                                                                    \
 })
--

2.34.1

author maintainer
#6 lore raw
In reply to [email protected]
Quoted 41 lines · On Thu, 2022-11-10 at 17:34 -0800, [email protected] wrote:
Quoted 8 lines · Stanislav Fomichev
Quoted 7 lines · Eduard Zingerman

A fix for the LLVM compilation error while building bpftool. Replaces the expression:

  _Static_assert((p) == NULL || ...)

by expression:

Quoted 7 lines
Quoted 1 line
  _Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) || ...)

IIUC, when __builtin_constant_p(p) returns false, we just ignore the NULL check?
Do we have cases like that? If no, maybe it's safer to fail?

s/(p) == NULL : 0/(p) == NULL : 1/ ?

I'm probably missing something, can you pls clarify? So the error is as follows:

Quoted 2 lines
Quoted 2 lines

btf_dump.c:1546:2: error: static_assert expression is not an integral constant expression

    hashmap__find(name_map, orig_name, &dup_cnt);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ./hashmap.h:169:35: note: expanded from macro 'hashmap__find'
    hashmap_find((map), (long)(key), hashmap_cast_ptr(value))
    ^~~~~~~~~~~~~~~~~~~~~~~
    ./hashmap.h:126:17: note: expanded from macro 'hashmap_cast_ptr'
    _Static_assert((p) == NULL || sizeof(*(p)) ==  

sizeof(long),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
btf_dump.c:1546:2: note: cast from 'void *' is not allowed in a constant
expression

    ./hashmap.h:169:35: note: expanded from macro 'hashmap__find'
    hashmap_find((map), (long)(key), hashmap_cast_ptr(value))

This line in particular:

btf_dump.c:1546:2: note: cast from 'void *' is not allowed in a constant
expression

Quoted 7 lines

And the code does:

   size_t dup_cnt = 0;
   hashmap__find(name_map, orig_name, &dup_cnt);

So where is that cast from 'void *' is happening? Is it the NULL check itself?

The void * comes from the definition of NULL: ((void*)0). And, unfortunately, sizeoof(*((void*)0)) == 1, so two conditions are necessary to allow a typical use-case when some of the pointer parameters are omitted.

Quoted 52 lines

Are we simply guarding against the user calling hashmap_cast_ptr with explicit NULL argument?

Quoted 48 lines
Quoted 10 lines

When "p" is not a constant the former is not considered to be a constant expression by LLVM 14.

The error was introduced in the following patch-set: [1]. The error was reported here: [2].

[1]

https://lore.kernel.org/bpf/[email protected]/

Quoted 37 lines

[2] https://lore.kernel.org/all/[email protected]/

---
 tools/lib/bpf/hashmap.h   | 3 ++-
 tools/perf/util/hashmap.h | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
tools/lib/bpf/hashmap.h +2 -1
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
 #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			\
+				sizeof(*(p)) == sizeof(long),				\
 		       #p " pointee should be a long-sized integer or a pointer");	\
 	(long *)(p);									\
 })
tools/perf/util/hashmap.h +2 -2
index 3fe647477bad..0a5bf1937a7c 100644
@@ -123,7 +123,8 @@ enum hashmap_insert_strategy {
 };
 
 #define hashmap_cast_ptr(p) ({								\
-	_Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),			\
+	_Static_assert((__builtin_constant_p((p)) ? (p) == NULL : 0) ||			\
+				sizeof(*(p)) == sizeof(long),				\
 		       #p " pointee should be a long-sized integer or a pointer");	\
 	(long *)(p);									\
 })
--

2.34.1

#7 lore raw
In reply to Eduard Zingerman
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Fri, 11 Nov 2022 00:32:40 +0200 you wrote:

A fix for the LLVM compilation error while building bpftool. Replaces the expression:

  _Static_assert((p) == NULL || ...)

by expression:

[...]

Here is the summary with links:
- [bpf-next] libbpf: hashmap.h update to fix build issues using LLVM14

    https://git.kernel.org/bpf/bpf-next/c/42597aa372f5

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html