Skip to contentSkip to conversation

C undefined behavior fix

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

lore
Share & tools
lore
#0 lore raw

<<No, in fact the kernel isn't written in ANSI C. :)
If nothing else, the fact that it uses a lot of gcc-specific extensions rules that out. And it assumes that you can freely cast pointers to unsigned longs and back again. I'm sure others can add to this list.

Quoted 1 line
Quoted 1 line

Most certainly this list should exist in precise defined form. It is not unreasonable to have a specific list of features that

a) significant programs rely on, and are allowed to rely on
b) gcc promises to implement as specified, regardless of the standard

What is not reasonable is to have various people informally guess at things that "obviously" can be expected to work in any "reasonable" C implementation. It is this kind of informality that is asking for trouble.

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

<<No, in fact the kernel isn't written in ANSI C. :)
If nothing else, the fact that it uses a lot of gcc-specific extensions rules that out. And it assumes that you can freely cast pointers to unsigned longs and back again. I'm sure others can add to this list.

Quoted 1 line
Quoted 1 line

Most certainly this list should exist in precise defined form.

C99 includes a list of additional guarantees made by many C implementations (in an informative index). I think we really should check this list (and the list of implementation-defined behavior) and document the choices made by GCC. In fact, this documentation is required by the standard.

Quoted 1 line

It is this kind of informality that is asking for trouble.

We have seen much trouble in this area before, but I doubt we can avoid all of them by proper documentation. Quite a few people seem to write some C code, check that it works, look at the generated machine code, and if it seems to be correct, the C code is considered to be correct.

#2 lore raw
Quoted 5 lines · Florian Weimer

C99 includes a list of additional guarantees made by many C implementations (in an informative index). I think we really should check this list (and the list of implementation-defined behavior) and document the choices made by GCC. In fact, this documentation is required by the standard.

The list of implementation-defined behavior is already in the manual (in extend.texi). However, the actual documentation isn't yet there, only the headings - except for the preprocessor, where it is covered in cpp.texi, and the conversion between pointers and integers:

@item
@cite{The result of converting a pointer to an integer or vice versa (6.3.2.3).}

A cast from pointer to integer discards most-significant bits if the pointer representation is larger than the integer type, sign-extends@footnote{Future versions of GCC may zero-extend, or use a target-defined @code{ptr_extend} pattern. Do not rely on sign extension.} if the pointer representation is smaller than the integer type, otherwise the bits are unchanged.
@c ??? We've always claimed that pointers were unsigned entities. @c Shouldn't we therefore be doing zero-extension? If so, the bug @c is in convert_to_integer, where we call type_for_size and request @c a signed integral type. On the other hand, it might be most useful @c for the target if we extend according to POINTERS_EXTEND_UNSIGNED.

A cast from integer to pointer discards most-significant bits if the pointer representation is smaller than the integer type, extends according to the signedness of the integer type if the pointer representation is larger than the integer type, otherwise the bits are unchanged.

When casting from pointer to integer and back again, the resulting pointer must reference the same object as the original pointer, otherwise the behavior is undefined. That is, one may not use integer arithmetic to avoid the undefined behavior of pointer arithmetic as proscribed in 6.5.6/8.

Signature · 2 lines
Joseph S. Myers
[email protected]
#3 lore raw
Quoted 35 lines · Joseph S. Myers
Quoted 5 lines · Florian Weimer

C99 includes a list of additional guarantees made by many C implementations (in an informative index). I think we really should check this list (and the list of implementation-defined behavior) and document the choices made by GCC. In fact, this documentation is required by the standard.

The list of implementation-defined behavior is already in the manual (in extend.texi). However, the actual documentation isn't yet there, only the headings - except for the preprocessor, where it is covered in cpp.texi, and the conversion between pointers and integers:

@item
@cite{The result of converting a pointer to an integer or vice versa (6.3.2.3).}

A cast from pointer to integer discards most-significant bits if the pointer representation is larger than the integer type, sign-extends@footnote{Future versions of GCC may zero-extend, or use a target-defined @code{ptr_extend} pattern. Do not rely on sign extension.} if the pointer representation is smaller than the integer type, otherwise the bits are unchanged.
@c ??? We've always claimed that pointers were unsigned entities. @c Shouldn't we therefore be doing zero-extension? If so, the bug @c is in convert_to_integer, where we call type_for_size and request @c a signed integral type. On the other hand, it might be most useful @c for the target if we extend according to POINTERS_EXTEND_UNSIGNED.

A cast from integer to pointer discards most-significant bits if the pointer representation is smaller than the integer type, extends according to the signedness of the integer type if the pointer representation is larger than the integer type, otherwise the bits are unchanged.

Quoted 5 lines

When casting from pointer to integer and back again, the resulting pointer must reference the same object as the original pointer, otherwise the behavior is undefined. That is, one may not use integer arithmetic to avoid the undefined behavior of pointer arithmetic as proscribed in 6.5.6/8.

Wat this last bit added to the standard after ANSI/ISO 9899-1990? I'm looking through my copy and I can't find it. All I can find is that

in 6.3.6 Additive Operators

When an expression that has integral type is added to or subtracted from a pointer [...] Unless both the pointer operand and the result point to elements of the same array object [...] the behavior is undefined...

but

in 6.3.4 Cast operators

A pointer may be converted to an integral type. The size of the integer required and the result are implementation-defined. [...]

An arbitrary integer may be converted to a pointer.
^^^^^^^^^
The result is implementation defined.

I interpret this to mean that one MAY use integer arithmatic to
do move a pointer outside the bounds of an array. Specifically, as soon as I've cast the pointer to an integer, the compiler has an obligation to forget any assumptions it makes about that pointer. This is what casts from pointer to integer are for! when i say (int)p I'm saying that I understand the address structure of the machine and I want to manipulate the address directly.

If the satandard has changed so this is no longer possible, there
NEEDS to be some other way in the new standard to express the same concept, or large application domains where C is currently in use will stop working.

-JKL

#4 lore raw
Quoted 2 lines · On Sat, 5 Jan 2002 [email protected] wrote:

Wat this last bit added to the standard after ANSI/ISO 9899-1990? I'm looking through my copy and I can't find it. All I can find is that

I was quoting from the GCC manual (providing the documentation implementations are required to provide of implementation-defined behaviour); of course the subclause numbers are different in C99 (from which the subclause numbers in the GCC manual are given) from those in C90. Perhaps once all the documentation for implementation-defined behaviour in C99 is present I'll go over what's required to document it all relative to C90 as well.

Quoted 7 lines

I interpret this to mean that one MAY use integer arithmatic to
do move a pointer outside the bounds of an array. Specifically, as soon as I've cast the pointer to an integer, the compiler has an obligation to forget any assumptions it makes about that pointer. This is what casts from pointer to integer are for! when i say (int)p I'm saying that I understand the address structure of the machine and I want to manipulate the address directly.

Just because you've created a pointer P, and it compares bitwise equal to a valid pointer Q you can use to access an object, does not mean that P can be used to access that object. Look at DR#260, discussing the provenance of pointer values, which arose from extensive discussion in the UK C Panel, and if you think it should be resolved otherwise from how we (UK C Panel) proposed then raise your position on it at a meeting of your National Body before the next WG14 meeting.

http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_260.htm

Signature · 2 lines
Joseph S. Myers
[email protected]
#8 lore raw
In reply to [email protected]
Quoted 2 lines · [email protected] writes:

An arbitrary integer may be converted to a pointer.
^^^^^^^^^

This rule exists so that implementations are not forced to issue a diagnostic for (char *)1.

Quoted 7 lines

I interpret this to mean that one MAY use integer arithmatic to
do move a pointer outside the bounds of an array. Specifically, as soon as I've cast the pointer to an integer, the compiler has an obligation to forget any assumptions it makes about that pointer. This is what casts from pointer to integer are for! when i say (int)p I'm saying that I understand the address structure of the machine and I want to manipulate the address directly.

According to the standard, you say that you want to cast p to type int. 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.

The fact that quite a few implementations traditionally provide such backdoors in some cases does not mean that the C language is a low-level language, or that all implementations (even future ones) have to provide these backdoors.

Quoted 4 lines

If the satandard has changed so this is no longer possible, there
NEEDS to be some other way in the new standard to express the same concept, or large application domains where C is currently in use will stop working.

I don't think there are fundamental and conceptual changes in C99 in this area. Even with previous C reversions, you should have read the compiler manual carefully before doing address arithmetic.

#5 lore raw
In reply to Joseph S. Myers
Quoted 30 lines · Joseph S. Myers
Quoted 2 lines · On Sat, 5 Jan 2002 [email protected] wrote:

Wat this last bit added to the standard after ANSI/ISO 9899-1990? I'm looking through my copy and I can't find it. All I can find is that

I was quoting from the GCC manual (providing the documentation implementations are required to provide of implementation-defined behaviour); of course the subclause numbers are different in C99 (from which the subclause numbers in the GCC manual are given) from those in C90. Perhaps once all the documentation for implementation-defined behaviour in C99 is present I'll go over what's required to document it all relative to C90 as well.

Quoted 7 lines

I interpret this to mean that one MAY use integer arithmatic to
do move a pointer outside the bounds of an array. Specifically, as soon as I've cast the pointer to an integer, the compiler has an obligation to forget any assumptions it makes about that pointer. This is what casts from pointer to integer are for! when i say (int)p I'm saying that I understand the address structure of the machine and I want to manipulate the address directly.

Just because you've created a pointer P, and it compares bitwise equal to a valid pointer Q you can use to access an object, does not mean that P can be used to access that object. Look at DR#260, discussing the provenance of pointer values, which arose from extensive discussion in the UK C Panel, and if you think it should be resolved otherwise from how we (UK C Panel) proposed then raise your position on it at a meeting of your National Body before the next WG14 meeting.

Quoted 2 lines

http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_260.htm

I don't particularly like the interpretation stated in DR260, but that doesn't matter. It still doesn't give the compiler permission to make something "undefined behavior" that the standard says is "implementation defined". The standard says an arbitrary integer (arbitrary means there are no restrictions on how it was produced) can be converted to a pointer and that the implementation must define what the results of this are.
All DR260 is saying is that the compiler can do certain sorts of deep
analysis on the code to decide whether my pointer is valid. It doesn't apply to the case of casting to an integer, munging, then casting back because the the implementation is supposed to provide a rule for when such pointers are valid.

Of course the GCC folks are within their rights to say that the implementation defined behavior is "all pointers created from integers are broken" but this is THEIR DECISION not a consequence of the standard, and not a consequence of DR260. And it would be a very, very bad decision given that one of the major uses of GCC is to build Linux, and apparently Linux breaks when this happens.

Or to put it another way: The Linux kernel developers NEED an implementation defined way to build a pointer from an address. If arithmatic on byte pointers isn't it (which is perfectly understandable) and casting a pointer from an integer isn't it, then can someone please tell us (not just "that will work for now" or "that will probably work") how do we do that?

-JKL

#9 lore raw
In reply to Florian Weimer
Quoted 8 lines · Florian Weimer
Quoted 2 lines · [email protected] writes:

An arbitrary integer may be converted to a pointer.
^^^^^^^^^

This rule exists so that implementations are not forced to issue a diagnostic for (char *)1.

No. If the only goal was to avoid a diagnostic, the it could be undefined. The fact that it is implementation defined means that it's reasonable to expect it to be useful for something.

Quoted 13 lines
Quoted 7 lines

I interpret this to mean that one MAY use integer arithmatic to
do move a pointer outside the bounds of an array. Specifically, as soon as I've cast the pointer to an integer, the compiler has an obligation to forget any assumptions it makes about that pointer. This is what casts from pointer to integer are for! when i say (int)p I'm saying that I understand the address structure of the machine and I want to manipulate the address directly.

According to the standard, you say that you want to cast p to type int. 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.

Quoted 5 lines

The fact that quite a few implementations traditionally provide such backdoors in some cases does not mean that the C language is a low-level language, or that all implementations (even future ones) have to provide these backdoors.

Whether C is a "low-level" or "high-level" language (whatever that means) is not the issue here. The issue here is that, as you say, quite a few implementations of C provide the ability to access arbitrary memory through pointers, and the standard provides some convenient places for the implementation to do it, and many users of GCC would really like to have this feature.

Forgive me if I haven't been reading the manual carefully enough. Now can we please get a straight answer to the question: Does GCC provide the ability to turn an arbitrary address into a pointer, and if so how do you do it?

-JKL

#10 lore raw
Quoted 4 lines · [email protected] writes:

Forgive me if I haven't been reading the manual carefully enough. Now can we please get a straight answer to the question: Does GCC provide the ability to turn an arbitrary address into a pointer, and if so how do you do it?

If you want to do more than what is permitted according to the documentation (quoted in the message by Joseph S. Myers you replied to), you have to use machine code insertions in order to get deterministic effects. At least this seems to be the consensus so far.

#6 lore raw
In reply to [email protected]
Quoted 11 lines · On Sat, 5 Jan 2002 [email protected] wrote:

I don't particularly like the interpretation stated in DR260, but that doesn't matter. It still doesn't give the compiler permission to make something "undefined behavior" that the standard says is "implementation defined". The standard says an arbitrary integer (arbitrary means there are no restrictions on how it was produced) can be converted to a pointer and that the implementation must define what the results of this are.
All DR260 is saying is that the compiler can do certain sorts of deep
analysis on the code to decide whether my pointer is valid. It doesn't apply to the case of casting to an integer, munging, then casting back because the the implementation is supposed to provide a rule for when such pointers are valid.

It says that a pointer is more than a sequence of bits, that the provenance of a pointer determines whether it can be used to access an object to which it points. (This notion is already explicit in the standard for "restrict" - which has various confusing issues when you go beyond its intended purpose of making code, to do the sort of things done in Fortran 77, as fast as in Fortran 77, see <URL:http://www.cbau.freeserve.co.uk/RestrictPointers.html>. Related are the exact meaning of "effective type" (see also the question about type-based aliasing rules in DR#236), and exactly how large the object that can be accessed through a pointer is (we know that in accesses to a multidimensional array, each index must be within its bounds, but there are other problems). See Nick Maclaren's "What is an object?" discussion
- WG14 reflector sequence 9350, but I don't think it's been more widely published.)

In the case of GCC, and casting integers to pointers, a rule has been
provided: the value of the pointer is determined by the bits, and the provenance, determining what can be accessed, is derived from that of the integer. This works fine with uses such as masking off a few bits to get a more aligned pointer, since there we are still accessing the original object. (I think the bounded pointers work also handles certain cast-adjust-cast-back cases specially, to preserve the bounds when such idioms are used rather than creating an unbounded pointer.) Simply documenting the bits of the pointer, and not what it can access, would not be sufficient documentation of the results of the cast, but the documentation describes both.

Signature · 2 lines
Joseph S. Myers
[email protected]
#7 lore raw
In reply to Joseph S. Myers
Quoted 3 lines · Joseph S. Myers

Just because you've created a pointer P, and it compares bitwise equal to a valid pointer Q you can use to access an object, does not mean that P can be used to access that object. Look at DR#260, discussing the

I looked at this, and it starts out with an example that includes a statement free(p); (where p was assigned a value returned from malloc) and then states that "After the call to free the value of p is indeterminate."!

This seems absolutely and completely bogus to me. Certainly, after the free, the value of *p is indeterminate, but the value of p itself *is* determinate; its value after the free is identical to its value before the free. Why do they say that the value of p itself is indeterminate after the free?

The two examples of why a compiler might want to change the value are also bogus; the compiler can avoid writing the value of p from a register back to memory only if the value is dead, and it isn't in the example given. As for the debugging opportunity, if I want p to be set to NULL or some other pattern for debugging I'll do it explicitly.

In general I think that when a pointer value has been obtained by a cast to an integer or by passing the address of a pointer to a function, the compiler should assume that the pointer can point anywhere. That means reduced opportunities for optimization, but so be it. Note that all of the examples in DR#260 involve passing &p to some function.

Paul.