diff mbox series

[6/6] genksyms: use uint32_t instead of unsigned long for calculating CRC

Message ID 20250103073046.2609911-6-masahiroy@kernel.org (mailing list archive)
State New
Headers show
Series [1/6] genksyms: fix memory leak when the same symbol is added from source | expand

Commit Message

Masahiro Yamada Jan. 3, 2025, 7:30 a.m. UTC
Currently, 'unsigned long' is used for intermediate variables when
calculating CRCs.

The size of 'long' differs depending on the architecture: it is 32 bits
on 32-bit architectures and 64 bits on 64-bit architectures.

The CRC values generated by genksyms represent the compatibility of
exported symbols. Therefore, reproducibility is important. In other
words, we need to ensure that the output is the same when the kernel
source is identical, regardless of whether genksyms is running on a
32-bit or 64-bit build machine.

Fortunately, the output from genksyms is not affected by the build
machine's architecture because only the lower 32 bits of the
'unsigned long' variables are used.

To make it even clearer that the CRC calculation is independent of
the build machine's architecture, this commit explicitly uses the
fixed-width type, uint32_t.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/genksyms/genksyms.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

Comments

David Laight Jan. 3, 2025, 8:34 a.m. UTC | #1
On Fri,  3 Jan 2025 16:30:43 +0900
Masahiro Yamada <masahiroy@kernel.org> wrote:

> Currently, 'unsigned long' is used for intermediate variables when
> calculating CRCs.
> 
> The size of 'long' differs depending on the architecture: it is 32 bits
> on 32-bit architectures and 64 bits on 64-bit architectures.
> 
> The CRC values generated by genksyms represent the compatibility of
> exported symbols. Therefore, reproducibility is important. In other
> words, we need to ensure that the output is the same when the kernel
> source is identical, regardless of whether genksyms is running on a
> 32-bit or 64-bit build machine.
> 
> Fortunately, the output from genksyms is not affected by the build
> machine's architecture because only the lower 32 bits of the
> 'unsigned long' variables are used.
> 
> To make it even clearer that the CRC calculation is independent of
> the build machine's architecture, this commit explicitly uses the
> fixed-width type, uint32_t.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  scripts/genksyms/genksyms.c | 15 ++++++++-------
>  1 file changed, 8 insertions(+), 7 deletions(-)
> 
> diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c
> index e2cd3dcb469f..8b0d7ac73dbb 100644
> --- a/scripts/genksyms/genksyms.c
> +++ b/scripts/genksyms/genksyms.c
>...
> -	printf("#SYMVER %s 0x%08lx\n", name, crc);
> +	printf("#SYMVER %s 0x%08lx\n", name, (unsigned long)crc);

That should use PRIu32, but the whole patch could just use 'unsigned int'.
No one is going to try to build this where 'int' is 16bit.
All the hex constants assume that int is 32bits as well.

	David
diff mbox series

Patch

diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c
index e2cd3dcb469f..8b0d7ac73dbb 100644
--- a/scripts/genksyms/genksyms.c
+++ b/scripts/genksyms/genksyms.c
@@ -12,6 +12,7 @@ 
 
 #include <stdio.h>
 #include <string.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <assert.h>
@@ -60,7 +61,7 @@  static void print_type_name(enum symbol_type type, const char *name);
 
 /*----------------------------------------------------------------------*/
 
-static const unsigned int crctab32[] = {
+static const uint32_t crctab32[] = {
 	0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
 	0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
 	0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
@@ -115,19 +116,19 @@  static const unsigned int crctab32[] = {
 	0x2d02ef8dU
 };
 
-static unsigned long partial_crc32_one(unsigned char c, unsigned long crc)
+static uint32_t partial_crc32_one(uint8_t c, uint32_t crc)
 {
 	return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8);
 }
 
-static unsigned long partial_crc32(const char *s, unsigned long crc)
+static uint32_t partial_crc32(const char *s, uint32_t crc)
 {
 	while (*s)
 		crc = partial_crc32_one(*s++, crc);
 	return crc;
 }
 
-static unsigned long crc32(const char *s)
+static uint32_t crc32(const char *s)
 {
 	return partial_crc32(s, 0xffffffff) ^ 0xffffffff;
 }
@@ -517,7 +518,7 @@  static void print_list(FILE * f, struct string_list *list)
 	}
 }
 
-static unsigned long expand_and_crc_sym(struct symbol *sym, unsigned long crc)
+static uint32_t expand_and_crc_sym(struct symbol *sym, uint32_t crc)
 {
 	struct string_list *list = sym->defn;
 	struct string_list **e, **b;
@@ -624,7 +625,7 @@  static unsigned long expand_and_crc_sym(struct symbol *sym, unsigned long crc)
 void export_symbol(const char *name)
 {
 	struct symbol *sym;
-	unsigned long crc;
+	uint32_t crc;
 	int has_changed = 0;
 
 	sym = find_symbol(name, SYM_NORMAL, 0);
@@ -672,7 +673,7 @@  void export_symbol(const char *name)
 	if (flag_dump_defs)
 		fputs(">\n", debugfile);
 
-	printf("#SYMVER %s 0x%08lx\n", name, crc);
+	printf("#SYMVER %s 0x%08lx\n", name, (unsigned long)crc);
 }
 
 /*----------------------------------------------------------------------*/