Skip to contentSkip to conversation

Fix undefined/miscompiled construct in kernel parameters

PATCH Andi Kleen 5 participants 6 messages Jun 15, 2003 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

Andi Kleen

author
#2 lore raw
Quoted 9 lines · Linus Torvalds
Quoted 4 lines · Andi Kleen

The parse_args call in init/main.c does pointer arithmetic between two different external symbols. This is undefined in C (only pointer arthmetic in the same symbol is defined) and gets miscompiled on AMD64 with gcc 3.2,

That's silly. You're making the code less readable, with some silly parameters. Why does it get miscompiled on amd64, and let's just fix that one.

Because &arbitary_symbol_a - &arbitary_symbol_b is undefined in C and the amd64 gcc 3.2 choses to miscompile it (it results in a very big number because it converts the 56/40 division to an inversed multiplication in a wrong way). I actually wrote a compiler bug report first, but the compiler developers rightly pointed out that it is undefined.

Note this is not the first time we had such problems, it happened with __pa(&symbol) too (amd64 has a special macro to work around that). I don't know why it happens more often on amd64 than on i386, perhaps it has something to do with the RIP relative addressing or the negative kernel code model. ppc seems to have similar problems.

Passing an end pointer is not too different from passing an max index in my opinion.

Another way would be to use the anonymous asm trick proposed by rth some time ago for a similar problem (asm("" : "=g" (output) : "0" (input)) to hide the symbols from the compiler), but I didn't do that because it looked more ugly to me.

If you prefer to do it with the anonymous asm I can do it, but I don't see a big problem with passing an end pointer.

-Andi

#3 lore raw
Quoted 6 lines · Andi Kleen

Because &arbitary_symbol_a - &arbitary_symbol_b is undefined in C and the amd64 gcc 3.2 choses to miscompile it (it results in a very big number because it converts the 56/40 division to an inversed multiplication in a wrong way). I actually wrote a compiler bug report first, but the compiler developers rightly pointed out that it is undefined.

They are not arbitrary symbols. They are symbols in the same data structure, set up by the linker script. Gcc doesn't know that, but the fact that gcc doesn't know doesn't mean that gcc should be lazy and doesn't really excuse buggy code.

The gcc developers you talked to are picking their legalistic noses, and it's sad that this isn't exactly the first time it has happened.

Linus

#4 lore raw
Quoted 7 lines

They are not arbitrary symbols. They are symbols in the same data structure, set up by the linker script. Gcc doesn't know that, but the fact that gcc doesn't know doesn't mean that gcc should be lazy and doesn't really excuse buggy code.

The gcc developers you talked to are picking their legalistic noses, and it's sad that this isn't exactly the first time it has happened.

I tend to agree, feel free to flame them. But it doesn't help me right now when I want to get a booting kernel. Could you merge that change or if you prefer I can rewrite it to anonymous asm (but it will be probably more ugly). I just need some workaround.

Thanks.

-Andi

#5 lore raw

Hi,

Quoted 4 lines · Andi Kleen

I tend to agree, feel free to flame them. But it doesn't help me right now when I want to get a booting kernel. Could you merge that change or if you prefer I can rewrite it to anonymous asm (but it will be probably more ugly). I just need some workaround.

Does the patch below work better?

bye, Roman

init/main.c +3 -3
@@ -383,7 +383,7 @@ asmlinkage void __init start_kernel(void
 {
 	char * command_line;
 	extern char saved_command_line[];
-	extern struct kernel_param __start___param, __stop___param;
+	extern struct kernel_param __start___param[], __stop___param[];
 /*
  * Interrupts are still disabled. Do necessary setups, then
  * enable them
@@ -403,8 +403,8 @@ asmlinkage void __init start_kernel(void
 	build_all_zonelists();
 	page_alloc_init();
 	printk("Kernel command line: %s\n", saved_command_line);
-	parse_args("Booting kernel", command_line, &__start___param,
-		   &__stop___param - &__start___param,
+	parse_args("Booting kernel", command_line, __start___param,
+		   __stop___param - __start___param,
 		   &unknown_bootoption);
 	trap_init();
 	rcu_init();
#6 lore raw
Quoted 10 lines · Roman Zippel

Hi,

Quoted 4 lines · Andi Kleen

I tend to agree, feel free to flame them. But it doesn't help me right now when I want to get a booting kernel. Could you merge that change or if you prefer I can rewrite it to anonymous asm (but it will be probably more ugly). I just need some workaround.

Does the patch below work better?

Nope, generates exactly the same code.

-Andi

#7 lore raw
In reply to Roman Zippel
Quoted 16 lines · Roman Zippel

Does the patch below work better?

bye, Roman

init/main.c +1 -1
@@ -383,7 +383,7 @@ asmlinkage void __init start_kernel(void
 {
 	char * command_line;
 	extern char saved_command_line[];
-	extern struct kernel_param __start___param, __stop___param;
+	extern struct kernel_param __start___param[], __stop___param[];
 /*
  * Interrupts are still disabled. Do necessary setups, then
  * enable them

init/main.c asmlinkage void __init start_kernel(void · line 410

Quoted 10 lines
init/main.c +2 -2
@@ -403,8 +403,8 @@ asmlinkage void __init start_kernel(void
 	build_all_zonelists();
 	page_alloc_init();
 	printk("Kernel command line: %s\n", saved_command_line);
-	parse_args("Booting kernel", command_line, &__start___param,
-		   &__stop___param - &__start___param,
+	parse_args("Booting kernel", command_line, __start___param,
+		   __stop___param - __start___param,
 		   &unknown_bootoption);
 	trap_init();
 	rcu_init();

Linus, I'd REALLY prefer this patch be applied, even though the problem turned out to be one of amd64 alignment, which can be worked around.

Even if struct kernel_param doesn't suffer from .sdata problems, this formulation is closer to Correct. I'd really prefer that all such linker-script generated arrays used the [] form, and not worry about the size of the data object involved.

r~