Skip to contentSkip to conversation

selftests/vDSO: Explicit unsigned char conversion for elf_hash

PATCH In review Edward Liaw 3 participants 8 messages May 1, 2024 – May 6, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 selftests/vDSO: Explicit unsigned char conversion for elf_hash

Fixes clang compilation warnings by adding explicit unsigned conversion:

parse_vdso.c:206:22: warning: passing 'const char *' to parameter of
type 'const unsigned char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Wpointer-sign]

        ver_hash = elf_hash(version);
                            ^~~~~~~
parse_vdso.c:59:52: note: passing argument to parameter 'name' here
static unsigned long elf_hash(const unsigned char *name)
                                                   ^

parse_vdso.c:207:46: warning: passing 'const char *' to parameter of
type 'const unsigned char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Wpointer-sign]

        ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
                                                    ^~~~

parse_vdso.c:59:52: note: passing argument to parameter 'name' here static unsigned long elf_hash(const unsigned char *name)

Fixes 98eedc3a9dbf ("Document the vDSO and add a reference parser")

---
v2: update commit message with correct compiler warning
v3: fix checkpatch errors and indentation

---
 tools/testing/selftests/vDSO/parse_vdso.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/vDSO/parse_vdso.c +3 -2
index 413f75620a35..9e29ff0657ea 100644
@@ -203,8 +203,9 @@ void *vdso_sym(const char *version, const char *name)
 	if (!vdso_info.valid)
 		return 0;
 
-	ver_hash = elf_hash(version);
-	ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
+	ver_hash = elf_hash((const unsigned char *)version);
+	ELF(Word) chain = vdso_info.bucket[
+		elf_hash((const unsigned char *)name) % vdso_info.nbucket];
 
 	for (; chain != STN_UNDEF; chain = vdso_info.chain[chain]) {
 		ELF(Sym) *sym = &vdso_info.symtab[chain];

--
2.45.0.rc0.197.gbae5840b3b-goog

Acked-by on v3 1/1
#1 lore raw

Hi,

tools/testing/selftests/vDSO/parse_vdso.c void *vdso_sym(const char *version, const char *name)

Quoted 35 lines · Edward Liaw

Fixes clang compilation warnings by adding explicit unsigned conversion:

parse_vdso.c:206:22: warning: passing 'const char *' to parameter of
type 'const unsigned char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Wpointer-sign]

        ver_hash = elf_hash(version);
                            ^~~~~~~
parse_vdso.c:59:52: note: passing argument to parameter 'name' here
static unsigned long elf_hash(const unsigned char *name)
                                                   ^

parse_vdso.c:207:46: warning: passing 'const char *' to parameter of
type 'const unsigned char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Wpointer-sign]

        ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
                                                    ^~~~

parse_vdso.c:59:52: note: passing argument to parameter 'name' here static unsigned long elf_hash(const unsigned char *name)

Fixes 98eedc3a9dbf ("Document the vDSO and add a reference parser")

---
v2: update commit message with correct compiler warning
v3: fix checkpatch errors and indentation

---
 tools/testing/selftests/vDSO/parse_vdso.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/vDSO/parse_vdso.c +0 -0
index 413f75620a35..9e29ff0657ea 100644
@@ -203,8 +203,9 @@ void *vdso_sym(const char *version, const char *name)

Is it possible to just change the types of the parameters of vdso_sym() or does that trigger even more warnings on the callsites of vdso_sym()?

Either way, this looks ok to me.

Quoted 12 lines
tools/testing/selftests/vDSO/parse_vdso.c +3 -3
 	if (!vdso_info.valid)
 		return 0;
 
-	ver_hash = elf_hash(version);
-	ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
+	ver_hash = elf_hash((const unsigned char *)version);
+	ELF(Word) chain = vdso_info.bucket[
+		elf_hash((const unsigned char *)name) % vdso_info.nbucket];
 
 	for (; chain != STN_UNDEF; chain = vdso_info.chain[chain]) {
 		ELF(Sym) *sym = &vdso_info.symtab[chain];
--
Quoted 2 lines

2.45.0.rc0.197.gbae5840b3b-goog

Thanks
Justin

#2 lore raw
Quoted 34 lines · Justin Stitt

Hi,

Quoted 30 lines · Edward Liaw

Fixes clang compilation warnings by adding explicit unsigned conversion:

parse_vdso.c:206:22: warning: passing 'const char *' to parameter of
type 'const unsigned char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Wpointer-sign]

        ver_hash = elf_hash(version);
                            ^~~~~~~
parse_vdso.c:59:52: note: passing argument to parameter 'name' here
static unsigned long elf_hash(const unsigned char *name)
                                                   ^

parse_vdso.c:207:46: warning: passing 'const char *' to parameter of
type 'const unsigned char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Wpointer-sign]

        ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
                                                    ^~~~

parse_vdso.c:59:52: note: passing argument to parameter 'name' here static unsigned long elf_hash(const unsigned char *name)

Fixes 98eedc3a9dbf ("Document the vDSO and add a reference parser")

---
v2: update commit message with correct compiler warning
v3: fix checkpatch errors and indentation

---
 tools/testing/selftests/vDSO/parse_vdso.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
Quoted 8 lines

tools/testing/selftests/vDSO/parse_vdso.c void *vdso_sym(const char *version, const char *name)

Quoted 5 lines
tools/testing/selftests/vDSO/parse_vdso.c +0 -0
index 413f75620a35..9e29ff0657ea 100644
@@ -203,8 +203,9 @@ void *vdso_sym(const char *version, const char *name)

Is it possible to just change the types of the parameters of vdso_sym() or does that trigger even more warnings on the callsites of vdso_sym()?

It does trigger more warnings on the callsites unfortunately.

Quoted 22 lines

Either way, this looks ok to me.

Quoted 12 lines
tools/testing/selftests/vDSO/parse_vdso.c +3 -3
      if (!vdso_info.valid)
              return 0;
 
-     ver_hash = elf_hash(version);
-     ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
+     ver_hash = elf_hash((const unsigned char *)version);
+     ELF(Word) chain = vdso_info.bucket[
+             elf_hash((const unsigned char *)name) % vdso_info.nbucket];
 
      for (; chain != STN_UNDEF; chain = vdso_info.chain[chain]) {
              ELF(Sym) *sym = &vdso_info.symtab[chain];
--
Quoted 2 lines

2.45.0.rc0.197.gbae5840b3b-goog

Thanks
Justin

#3 lore raw
In reply to Justin Stitt

From: Justin Stitt

Quoted 1 line

Sent: 01 May 2024 20:55

...

Quoted 1 line
Quoted 1 line

static unsigned long elf_hash(const unsigned char *name)

...

Quoted 2 lines

Is it possible to just change the types of the parameters of vdso_sym() or does that trigger even more warnings on the callsites of vdso_sym()?

Isn't the problem the definition of elf_hash()?
A '\0' terminated string really ought to be 'char *' not 'unsigned char *'.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#4 lore raw
Quoted 11 lines · David Laight

From: Justin Stitt

Quoted 1 line

Sent: 01 May 2024 20:55

...

Quoted 1 line
Quoted 1 line

static unsigned long elf_hash(const unsigned char *name)

...

Quoted 2 lines

Is it possible to just change the types of the parameters of vdso_sym() or does that trigger even more warnings on the callsites of vdso_sym()?

Isn't the problem the definition of elf_hash()?
A '\0' terminated string really ought to be 'char *' not 'unsigned char *'.

Right, although note this comment just about its definition:

/* Straight from the ELF specification. */ static unsigned long elf_hash(const unsigned char *name) {

which indeed matches [1]

[1]: https://man.freebsd.org/cgi/man.cgi?query=elf_hash&sektion=3&apropos=0&manpath=FreeBSD+7.1-RELEASE

Quoted 7 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#5 lore raw
Quoted 16 lines · Justin Stitt
Quoted 11 lines · David Laight

From: Justin Stitt

Quoted 1 line

Sent: 01 May 2024 20:55

...

Quoted 1 line
Quoted 1 line

static unsigned long elf_hash(const unsigned char *name)

...

Quoted 2 lines

Is it possible to just change the types of the parameters of vdso_sym() or does that trigger even more warnings on the callsites of vdso_sym()?

Isn't the problem the definition of elf_hash()?
A '\0' terminated string really ought to be 'char *' not 'unsigned char *'.

Right, although note this comment just about its definition:

Quoted 7 lines

/* Straight from the ELF specification. */ static unsigned long elf_hash(const unsigned char *name) {

which indeed matches [1]

[1]: https://man.freebsd.org/cgi/man.cgi?query=elf_hash&sektion=3&apropos=0&manpath=FreeBSD+7.1-RELEASE

How about I move the type cast into elf_hash, like libelf does
https://sourceforge.net/p/elftoolchain/code/HEAD/tree/trunk/libelf/elf_hash.c#l47

(quoted diff) +0 -0

b/tools/testing/selftests/vDSO/parse_vdso.c
index 413f75620a35..33db8abd7d59 100644

tools/testing/selftests/vDSO/parse_vdso.c +6 -3
@@ -56,12 +56,15 @@ static struct vdso_info
 } vdso_info;
 
 /* Straight from the ELF specification. */
-static unsigned long elf_hash(const unsigned char *name)
+static unsigned long elf_hash(const char *name)
 {
  unsigned long h = 0, g;
- while (*name)
+ const unsigned char *s;
+
+ s = (const unsigned char *) name;
+ while (*s)
  {
- h = (h << 4) + *name++;
+ h = (h << 4) + *s++;
  if (g = h & 0xf0000000)
  h ^= g >> 24;
  h &= ~g;
Quoted 8 lines
Quoted 8 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#6 lore raw

From: Edward Liaw

Quoted 45 lines

Sent: 06 May 2024 18:34

Quoted 16 lines · Justin Stitt
Quoted 11 lines · David Laight

From: Justin Stitt

Quoted 1 line

Sent: 01 May 2024 20:55

...

Quoted 1 line
Quoted 1 line

static unsigned long elf_hash(const unsigned char *name)

...

Quoted 2 lines

Is it possible to just change the types of the parameters of vdso_sym() or does that trigger even more warnings on the callsites of vdso_sym()?

Isn't the problem the definition of elf_hash()?
A '\0' terminated string really ought to be 'char *' not 'unsigned char *'.

Right, although note this comment just about its definition:

Quoted 7 lines

/* Straight from the ELF specification. */ static unsigned long elf_hash(const unsigned char *name) {

which indeed matches [1]

[1]: https://man.freebsd.org/cgi/man.cgi?query=elf_hash&sektion=3&apropos=0&manpath=FreeBSD+7.1-

RELEASE

How about I move the type cast into elf_hash, like libelf does
https://sourceforge.net/p/elftoolchain/code/HEAD/tree/trunk/libelf/elf_hash.c#l47

tools/testing/selftests/vDSO/parse_vdso.c +1 -2
index 413f75620a35..33db8abd7d59 100644
@@ -56,12 +56,15 @@ static struct vdso_info
 } vdso_info;
 
 /* Straight from the ELF specification. */
-static unsigned long elf_hash(const unsigned char *name)
+static unsigned long elf_hash(const char *name)
 {
  unsigned long h = 0, g;
- while (*name)

tools/testing/selftests/vDSO/parse_vdso.c static struct vdso_info · line 70

Quoted 10 lines
tools/testing/selftests/vDSO/parse_vdso.c +5 -1
+ const unsigned char *s;
+
+ s = (const unsigned char *) name;
+ while (*s)
  {
- h = (h << 4) + *name++;
+ h = (h << 4) + *s++;
  if (g = h & 0xf0000000)
  h ^= g >> 24;
  h &= ~g;

That makes a lot more sense.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)