Skip to contentSkip to conversation

C undefined behavior fix

PATCH [email protected] 3 participants 3 messages Jan 6, 2002

lore
Share & tools
lore
#0 lore raw

<<There are some C compilers that are useful for implementing a kernel, that's true. But when the maintainers of such a compiler say things that imply that they feel they are constrained only by the standard and not by the needs of their users, it is very discouraging.

Quoted 1 line
Quoted 1 line

What is important is for these users to *clearly* and at least semi-formally, state their needs. Saying general things about the need to be useful is hardly helpful!

You quote Florian:

Quoted 3 lines

You cannot manipulate machine addresses in C because C is defined as a high-level language, without backdoors to such low-level concepts as machine addresses.

Unfortunately Florian is right. The ability in C to manipulate low-level concepts such as machine addresses is NOT part of the language, but rather comes from exploiting aspects that are deliberately left implementation dependent. This is why it is so important to formally state the requirements that are being depended on.

I don't think anyone seriously objects to trying to formulate solutions to what is indeed a very important problem.

But it is hardly helpful for people to take the attitude "we wrote this kernel, and it worked, and any change to the compiler that stops it from working is unacceptable".

#1 lore raw
Quoted 3 lines · [email protected] writes:

What is important is for these users to *clearly* and at least semi-formally, state their needs. Saying general things about the need to be useful is hardly helpful!

Sure, here we go:

* Given an address as an int (of the appropriate size), I need a way to construct a pointer, which when dereferenced, will result in the CPU presenting that address to the MMU.

* I need a way to tell the compiler not to make any assumptions about what objects that such a pointer might or might not point to, so that the effect of dereferencing the pointer is simply to access the memory at the address I gave, and is not considered "undefined" regardless of how I might have constructed the address.

* Given a pointer, I need a way to determine the address (as an int of the appropriate size) that the CPU will present to the MMU when I dereference that pointer.

GCC already does the first and third, but there doesn't seem to be a clean and reliable way to do the second.

Quoted 2 lines

I don't think anyone seriously objects to trying to formulate solutions to what is indeed a very important problem.

There have been a variety of points of view expressed, and some of them (not yours) seemed to be saying that it was invalid for a C program to be trying to manipulate machine addresses at all.

Quoted 3 lines

But it is hardly helpful for people to take the attitude "we wrote this kernel, and it worked, and any change to the compiler that stops it from working is unacceptable".

No-one has taken that attitude that I have seen. You may be interested to know that I am now using -mrelocatable instead of the RELOC macro. That solves most of the problems (and cleans up the code as well) but there are still some instances where I need to relocate a
pointer: specifically, I generate a pointer when running at the initial address that I need to use when running at the final address. So I still need to be able to do the type of address arithmetic that, according to the caveat in the gcc doco, produces an undefined result, namely taking the address of an object, adding an offset, and storing the result in a pointer variable that I will dereference later (after moving the kernel and its data to its final location).

I find it hardly helpful to be told that doing that is invalid without anyone offering me a valid way to achieve the effect I want - and no-one has, other than to say that as long as I dereference the pointer some time later, in a different procedure, it should be fine. Which is fine from a pragmatic point of view but it doesn't alter the validity or invalidity of the operation.

Paul.

#2 lore raw
In reply to [email protected]
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Paul" == Paul Mackerras <[email protected]> writes:

Paul> I find it hardly helpful to be told that doing that is invalid without Paul> anyone offering me a valid way to achieve the effect I want - and Paul> no-one has, other than to say that as long as I dereference the

Number of people suggested using assembly for this, why you keep ignoring it and insist instead on changing the compiler, changing the C standard, switching to another compiler and similar unproductive ideas put forward solely for the sake of argument ?

Paul> pointer some time later, in a different procedure, it should be fine. Paul> Which is fine from a pragmatic point of view but it doesn't alter the Paul> validity or invalidity of the operation.

Quoted 1 line

From a _pragmatic_ point of view you can just keep the code as is,

fixing the _extremely_ rare cases where violations of the standard interfere with compiler optimizations. It is too much to ask to penalize _all_ the software by preventing the compiler from doing optimizations for the convenience of the maintainers of the PPC port of Linux. As for listening to the users of the compiler (you mentioned this in another mail), GCC maintainers (an C comitee, for that matter) do exactly this. You just have to realize that _we_ are minority, important, by nevertheless minority.

Regards,
-velco