This'll be a long one, as to contextualise it and provide as much info as I can, I must meander through various links and quotes - as is often the only way once we enter the C/C++ Standard Rabbit Hole. If you have better citations or any other improvements to this post, please let me know. But to summarise up front, you can blame @zwol for me posting this ;-) and the aim is to find the truth from among two propositions:
- Do the C and (by import; see comments) C++ Standards require that accesses via
volatile *
orvolatile &
must refer to an object originally declaredvolatile
in order to havevolatile
semantics? - Or is accessing a non-
volatile
-qualified object through avolatile
pointer/reference sufficient/supposed to make said accesses behave as if the object was declaredvolatile
?
And either way, if (as it seems) the wording is somewhat ambiguous compared to the intent - can we get that made clear in the Standards themselves?
The 1st of these mutually exclusive interpretations is more commonly held, and that's not entirely without basis. However, I hope to show that there's a significant amount of "reasonable doubt" in favour of the 2nd - especially when we go back to some prior passages in the Rationale and WG Papers.
Accepted wisdom: the referred object itself must have been declared volatile
Yesterday's popular question Is the definition of “volatile” this volatile, or is GCC having some standard compliancy problems? arose by assuming a volatile
reference would confer volatile
behaviour on a non-volatile
referent - but finding that it did not, or did to varying degrees and in an unpredictable way.
The accepted answer initially concluded that only the declared type of the referent mattered. This and most comments seemed to agree that equivalent principles are in play as we know well for const
: the behaviour would only be volatile
(or defined at all) if the reference has the same cv-qualification as the referred object:
The key word in that passage is object.
volatile sig_atomic_t flag;
is a volatile object.*(volatile char *)foo
is merely an access through a volatile-qualified lvalue and the standard does not require that to have any special effects. – zwol
This interpretation appears to be quite widely held, as seen in the responses to this similar-but-hopefully-not-duplicate question: Requirements for behavior of pointer-to-volatile pointing to non-volatile object But there's uncertainty even there: right after the answer says 'no', it then says 'maybe'! Anyway...let's check the Standard to see what the 'no's are based on.
What the Standard says... or doesn't
C11, N1548, §6.7.3: Whereas it's clear that it's UB to access an object defined with volatile
or const
type via a pointer that doesn't share said qualifier...
6 If an attempt is made to modify an object defined with a
const
-qualified type through use of an lvalue with non-const
-qualified type, the behavior is undefined. If an attempt is made to refer to an object defined with avolatile
-qualified type through use of an lvalue with non-volatile-
qualified type, the behavior is undefined.(133)
...the Standard doesn't seem to explicitly mention the opposite scenario, namely for volatile
. Moreover, when summarising volatile
and operations thereon, it now talks about an object that has volatile
-qualified type:
7 An object that has
volatile
-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3. Furthermore, at every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine, except as modified by the unknown factors mentioned previously.(134) What constitutes an access to an object that hasvolatile
-qualified type is implementation-defined.
Are we to assume "has" is equivalent to "was defined with"? or can "has" refer to a combination of object and reference qualifiers?
A commenter summed up the issue with this sort of wording well:
From n1548 §6.7.3 ¶6 the standard uses the phrase "object defined with a volatile-qualified type" to distinguish it from "lvalue with volatile-qualified type". It's unfortunate that this "object defined with" versus "lvalue" distinction does not carry forward, and the standard then uses "object that has volatile-qualified type", and says that "what constitutes access to an object that has volatile-qualified type is implementation-defined" (which could have said "lvalue" or "object defined with" for clarity). Oh well. – Dietrich Epp
Paragraph 4 of the same section seems to be less frequently quoted but might well be relevant, as we'll see in the next section.
Reasonable doubt: Is/Was a volatile
pointer/reference intended to confer volatile
semantics on its dereference?
The aforementioned answer has a comment wherein the author cites an earlier statement by the Committee that cast doubt on the 'reference must match referent' idea:
Interestingly, there is one sentence in there [C99 Rationale for
volatile
] that implies that the committee meant for*(volatile T*)x
to force that one access tox
to be treated as volatile; but the actual wording of the standard does not achieve this. – zwol
We can find a bit more info on this bit of the Rationale, from the 2nd aforementioned thread: Requirements for behavior of pointer-to-volatile pointing to non-volatile object
On the other hand, this post quotes from the 6.7.3 of the Rationale for International Standard--Programming Languages--C:
A cast of a value to a qualified type has no effect; the qualification (volatile, say) can have no effect on the access since it has occurred prior to the case. If it is necessary to access a non-volatile object using volatile semantics, the technique is to cast the address of the object to the appropriate pointer-to-qualified type, then dereference that pointer.
—philipxy
And from that Bytes thread, we're referred to C99 s6.7.3 p3 - a.k.a. C11's p4 - and this analysis:
The paragraph in question is just before section 6.7.3.1 in the rationale document. If you also need to quote from the standard document itself, cite 6.7.3 p3:
The properties associated with qualified types are meaningful only for expressions that are lvalues.
The expression
(volatile WHATEVER) non_volatile_object_identifier
is not an lvalue, hence the 'volatile' qualifier is meaningless.Conversely, the expression
* (volatile WHATEVER *) & non_volatile_object_identifier
is an lvalue (it may be placed on the left side of an assignment statement), so the property of the 'volatile' qualifier has its intended meaning in this case.—Tim Rentsch
There is a very specific demonstration supporting this idea, with specific regard to the 1st linked question, in WG Paper N1381. This introduced the Annexed memset_s()
to do what that OP wanted - guarantee non-elided filling of memory. In discussing possible implementations, it seems to support the idea - by omitting to state any requirement - that using a volatile
pointer to alter a non-volatile
object should generate code based on the qualifier of the pointer, regardless of that of the referred object...
- Platform-independent ' secure-memset' solution:
void *secure_memset(void *v, int c , size_t n) {
volatile unsigned char *p = v;
while (n--) *p++ = c;
return v;
}
This approach will prevent the clearing of memory from being optimized away, and it should work on any standard-compliant platform.
...and that compilers not doing this are on notice...
There has been recent notice that some compilers violate the standard by not always respecting the
volatile
qualifier.
Who's right?
That was exhausting. There's certainly a lot of room for interpretation here, depending on which documents you happen to have read vs which not, and how you choose to interpret a lot of words that aren't specific enough. It seems clear that something is amiss: either:
- the Rationale and N1381 are wrongly or haphazardly worded, or
- they were specifically invalidated retroactively... or
- the Standard is wrongly or haphazardly worded.
I'd hope that we can do better than all the ambiguity and speculation that seems to have surrounded this in the past - and get a more conclusive statement put on record. To that end, any further sources and thoughts from experts would be very welcome.
volatile
basically means dealing with unusual memory. How should the compiler decide between unusual memory and normal memory based on pointer type/value. – Huonghupehvolatile
ity. But an intention that they would was clearly stated in multiple prior documents. So, again, a conclusive citation from the Standard - or correction of it - is the goal. I personally don't mind which interpretation is right. I just want to know what that is. – Averauint32_t volatile *p;
, the effect of*p=0x12345678;
should be to use a 32-bit store to write 0x12345678 into the address specified byp
. If the programmer writes+*p;
as a statement, the compiler should use a 32-bit load to read from the address specified byp
and ignore the result. The compiler shouldn't care why a programmer would want to do those things--it should simply do them. – Threapvolatile
access. While this isn't always necessary, it's a safe default. A compiler that wants to allow more efficient code generation can offer a way of waiving such semantics when they're not needed. I'm not sure what's difficult about any of this. – Threapvolatile
qualifier? – Threapvolatile
should ensure that the processor issues writes and reads as indicated; what happens between... – Threapvolatile
reads or writes need to be issued. Personally I think C would be greatly improved by a "semi-volatile" qualifier which would ensure logical reads and writes would be ordered with regard to "volatile", but not with regard to other "semi-volatile" variables. – Threapvolatile
should do based on what would make it useful to people on particular platforms. Just don't pretend that's what the standard says. And don't say that it should be the "order in which the CPU issues loads and stores" as if that weren't a platform-specific thing. (In fact, you can't even define what it means to "issue" a load or store without reference to particular hardware as the boundary between the CPU and memory is squishy and not a hard line like it is in an abstract machine.) – Aramaicvolatile
stores in a certain sequence, the compiler should generate code that performs those operations in the same logical sequence. – Threapvolatile
will make that possible. If such ordering would be needlessly and unacceptably detrimental to performance, a programmer can use a non-qualified pointer. If, however, the compiler wouldn't honor such ordering and it turned out to be necessary, there may be no efficient way to get needed semantics. – Threap