public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "D. Hugh Redelmeier" <hugh@mimosa.com>
To: gcc@gcc.gnu.org
Cc: Nathan Sidwell <nathan@codesourcery.com>,
	Dale Johannesen <dalej@apple.com>, Mike Stump <mrs@apple.com>
Subject: Re: volatile semantics
Date: Sat, 16 Jul 2005 16:51:00 -0000	[thread overview]
Message-ID: <Pine.LNX.4.61.0507161250120.1478@redshift.mimosa.com> (raw)
In-Reply-To: <b0f8ff9938df57bbfda0ec3aea4fc02b@apple.com>

Sorry for the very late response.  It is actually triggered by the
bugzilla entry
	http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22278

The motivating example, abstracted from a misbehaving part of X, is:
	void test (char *addr) {
        	*((volatile char *) addr);
	}
In this case, the fetch ("access") is required for the hardware to
behave.

| From: Dale Johannesen <dalej at apple dot com>
| Date: Tue, 3 May 2005 10:04:36 -0700
| 
| On May 3, 2005, at 7:41 AM, Nathan Sidwell wrote:
| > Mike Stump wrote:
| > > int avail;
| > > int main() {
| > >   while (*(volatile int *)&avail == 0)
| > >     continue;
| > >   return 0;
| > > }
| > > Ok, so, the question is, should gcc produce code that infinitely  loops,
| > > or should it be obligated to actually fetch from memory?   Hint, 3.3
| > > fetched.
| > 
| > I beleive the compiler is so licensed. [5.1.2.3/2] talks about accessing
| > a volatile object.  If the compiled can determine the actual object
| > being accessed through a series of pointer and volatile cast conversions,
| > then I see nothing in the std saying it must behave as-if the object
| > were volatile when it is not.

That turns out not to be the case.  See the chain of reasoning later
in this message.

| This is correct; the standard consistently talks about the type of the object,
| not the type of the lvalue, when describing volatile.
|
| However, as a QOI issue, I believe the compiler should treat the reference as
| volatile if either the object or the lvalue is volatile.  That is obviously
| the
| user's intent.

It appears to me that standards conformance requires the access to be
performed.  So GCC4 has a bug.  One that is subtle, so the damage it
is doing may not be evident.  The worst kind.

When I asked Henry Spencer to look at this, he gave a convincing
argument from chapter and verse of C99.  I will quote it here.  I
added the identical text to the bugzilla entry (comment #43), so there
is no need to read the rest of this message if you read that comment.

================ start of Henry's comment

There is little room for compiler writers to maneuver here, unless they
have announced their intentions in advance in their documentation. 
Reading C99 carefully:

6.5.3.2:  applying `*' to a pointer of type `T *' which points to an
object yields an lvalue of type `T' designating that object.  So the
lvalue in the assignment has a volatile-qualified type. 

6.3.2.1:  when an object is said to have a particular type, the type is
specified by the lvalue used to designate the object.  So the lvalue
having a volatile-qualified type *means* that the object it designates has
a volatile-qualified type; "has type X" and "is designated by an lvalue of
type X" are synonymous (!).

6.7.3:  any expression referring to an object of volatile-qualified
type must be evaluated strictly by the rules of the abstract machine,
although precisely what constitutes an "access" to the object is
implementation-defined.  (Note, "implementation-defined" behavior is
required to be well-defined and *documented*.)  So if the reference in
question is an "access", it must occur where the abstract machine says
it should.

5.1.2.3:  the abstract machine evaluates all expressions as specified by
semantics and all side effects must occur, side effects including
"accessing a volatile object"; implementations are allowed to skip part
of expression evaluation only if they can deduce that no needed side
effects (notably "accessing a volatile object") are therefore skipped. 
So if the reference is an "access", it must occur, period.

I see no useful wiggle room in the difference between "access" and
"accessing", or the difference between "volatile object" and "object of
volatile-qualified type".  These appear to me to be minor accidents of
inconsistent terminology, not useful to a sane implementer.

6.7.3 says that to refer to an object "defined with" volatile-qualified
type "through use of an lvalue" with non-volatile-qualified type yields
undefined behavior.  However, the reference here uses a volatile-qualified
lvalue, so this is not relevant.  A pointer value is not an lvalue; there
is no lvalue present until the `*' operator is applied. 

Surprising though it might seem, I see no express or implied permission to
distinguish based on whether the object in question was *defined* with a
volatile-qualified type.  There are places in the standard where how an
object is defined is significant, e.g. the rules for `const' and the part
of 6.7.3 noted in the previous paragraph, but none of them is part of the
chain of reasoning above.

The only loophole is the definition of "access".  If GCC wishes to claim
standard conformance, GCC is required to document its definition.  I'm not
aware of any mention of this in the GCC documentation, although I haven't
dug hard for it. 

I see no room for a reasonable definition of "access" which retains some
useful meaning for `volatile' and doesn't cover the reference in question. 
(I agree that you can contrive a definition which contains special-case
wording to cover this case, but not that it's reasonable to do so.)

If GCC (a) wants to be C99-conforming, and (b) wants to provide useful
semantics for `volatile', this is a bug.

Henry

  parent reply	other threads:[~2005-07-16 16:51 UTC|newest]

Thread overview: 118+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-03  8:41 Mike Stump
2005-05-03  8:52 ` Paolo Bonzini
2005-05-03  9:53 ` Giovanni Bajo
2005-05-03 14:31 ` Dave Korn
2005-05-03 14:41 ` Nathan Sidwell
2005-05-03 17:04   ` Dale Johannesen
2005-05-03 18:03     ` Nathan Sidwell
2005-05-03 18:32       ` Paul Koning
2005-05-03 18:37         ` Dale Johannesen
2005-05-11 23:01         ` Geoffrey Keating
2005-05-12 14:46           ` Paul Koning
2005-05-03 18:35       ` Dale Johannesen
2005-05-03 18:54         ` Nathan Sidwell
2005-05-03 19:25           ` Dale Johannesen
2005-05-04  8:18             ` Nathan Sidwell
2005-05-04 10:23               ` Andrew Haley
2005-05-04 11:30                 ` Gabriel Dos Reis
2005-05-04 17:59                   ` Dale Johannesen
2005-05-04 18:01                     ` Paul Koning
2005-05-04 19:49                     ` Nathan Sidwell
2005-07-16 16:51     ` D. Hugh Redelmeier [this message]
2005-07-16 16:56       ` Daniel Berlin
2005-07-16 17:26         ` Nathan Sidwell
2005-07-16 17:32           ` Daniel Berlin
2005-07-16 18:35             ` Nathan Sidwell
2005-07-16 20:35               ` Daniel Berlin
2005-07-16 21:29                 ` Gabriel Dos Reis
2005-07-16 21:41                   ` Daniel Berlin
2005-07-16 21:59                     ` Gabriel Dos Reis
2005-07-16 21:07             ` Gabriel Dos Reis
2005-07-16 19:20         ` D. Hugh Redelmeier
2005-07-16 21:10           ` Gabriel Dos Reis
2005-07-16 20:52         ` Gabriel Dos Reis
2005-07-16 21:07           ` Daniel Berlin
2005-07-16 21:24             ` Gabriel Dos Reis
2005-07-16 21:30               ` Daniel Berlin
2005-07-16 22:19                 ` Gabriel Dos Reis
2005-07-16 21:36               ` Daniel Berlin
2005-07-16 22:06                 ` Gabriel Dos Reis
2005-07-16 22:17                   ` Daniel Berlin
2005-07-16 22:25                     ` Gabriel Dos Reis
2005-07-19  7:27                       ` Kai Henningsen
2005-07-19  9:25                         ` Gabriel Dos Reis
2005-07-16 22:34               ` D. Hugh Redelmeier
2005-07-16 22:57                 ` Gabriel Dos Reis
2005-07-17  1:37                   ` D. Hugh Redelmeier
2005-07-17  2:24                     ` Gabriel Dos Reis
2005-07-17  2:36                       ` Daniel Berlin
2005-07-17  3:08                         ` Gabriel Dos Reis
2005-07-17  4:32                           ` Michael Veksler
2005-07-17  5:19                           ` Michael Veksler
2005-07-17  5:31                             ` Gabriel Dos Reis
2005-07-17  7:33                               ` Michael Veksler
2005-07-17 14:33                             ` Daniel Berlin
2005-07-17 15:30                               ` Michael Veksler
2005-07-17  7:53                           ` Andrew Pinski
2005-07-17 11:41                             ` Gabriel Dos Reis
2005-07-17  7:40                       ` D. Hugh Redelmeier
2005-07-17 11:50                         ` Gabriel Dos Reis
2005-07-18 19:30                           ` Mike Stump
2005-07-18 20:05                             ` Gabriel Dos Reis
2005-07-17 15:45                         ` Richard Henderson
2005-07-17 16:04                           ` Nathan Sidwell
2005-07-17 16:18                             ` Richard Henderson
2005-07-17 16:54                               ` Gabriel Dos Reis
2005-07-17 16:06                           ` Falk Hueffner
2005-07-17 16:18                           ` Ian Lance Taylor
2005-07-17 16:44                             ` Richard Henderson
2005-07-17 12:49                       ` Joseph S. Myers
2005-07-17  2:27                     ` Daniel Berlin
2005-07-17  3:14                       ` Gabriel Dos Reis
2005-07-17  3:27                         ` Daniel Berlin
2005-07-17 20:34                           ` Mark Mitchell
2005-07-17  4:38                         ` D. Hugh Redelmeier
2005-07-17  5:27                           ` Gabriel Dos Reis
2005-07-18 13:13                           ` Gerald Pfeifer
2005-07-17  7:54                       ` D. Hugh Redelmeier
2005-07-17 10:11               ` Andrew Haley
2005-07-17 12:03                 ` Gabriel Dos Reis
2005-07-16 17:33       ` Andrew Haley
2005-07-16 17:53         ` Daniel Berlin
2005-07-17  8:25           ` Ian Lance Taylor
2005-07-22 23:20             ` Geoffrey Keating
2005-07-22 23:33               ` Ian Lance Taylor
2005-07-23  1:28                 ` Geoff Keating
2005-07-23  2:59                   ` Gabriel Dos Reis
2005-07-23  9:50                     ` Geoff Keating
2005-07-23  6:03                   ` Ian Lance Taylor
2005-07-23 16:03                   ` Mike Stump
2005-07-16 19:05         ` Dale Johannesen
2005-07-16 21:17           ` Gabriel Dos Reis
2005-07-22 23:20       ` Geoffrey Keating
2005-07-25 23:08         ` Olivier Galibert
2005-05-06  0:45   ` Kai Henningsen
2005-05-06  1:42     ` Paul Koning
2005-05-06  2:04     ` Gabriel Dos Reis
2005-05-06  2:57     ` Dale Johannesen
2005-05-03 21:19 ` Thorsten Glaser
2005-05-06  5:06 Paul Schlie
2005-07-17 17:58 Paul Schlie
2005-07-18  1:29 Paul Schlie
2005-07-18  6:36 ` Gabriel Dos Reis
2005-07-18 11:20   ` Paul Schlie
2005-07-18 12:12     ` Paolo Bonzini
2005-07-18 12:17     ` Paul Schlie
2005-07-18 12:27       ` Gabriel Dos Reis
2005-07-18 13:27         ` Paul Schlie
2005-07-18 15:47           ` D. Hugh Redelmeier
2005-07-18 12:24     ` Gabriel Dos Reis
2005-07-18 12:11 ` Jonathan Wakely
2005-07-18 12:31   ` Paul Schlie
2005-07-19 17:56 Paul Schlie
2005-07-19 18:13 ` Gabriel Dos Reis
2005-07-19 18:32   ` Paul Schlie
2005-07-23  2:15 Paul Schlie
2005-07-23  9:50 ` Geoff Keating
2005-07-23 11:39   ` Paul Schlie
2005-07-23 11:44   ` Paul Schlie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.61.0507161250120.1478@redshift.mimosa.com \
    --to=hugh@mimosa.com \
    --cc=dalej@apple.com \
    --cc=gcc@gcc.gnu.org \
    --cc=mrs@apple.com \
    --cc=nathan@codesourcery.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).