How to prevent migration of volatiles

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    How to prevent migration of volatiles

    The paper Volatiles are Miscompiled, and What to Do about It by Eide and Regehr reports that mishandling of volatiles is a common category of compiler bugs. You should read it if you use volatiles for memory-mapped I/O or thread synchronization . Almost in passing they describe a common programmer error with volatiles:
    Originally posted by the Eide and Regehr paper
    For example, the following code illustrates a common mistake in which a volatile variable is used to signal a condition about a non-volatile data structure, perhaps to another thread:
    Code:
    volatile int buffer_ready;
    char buffer[BUF_SIZE];
    
    void buffer_init() {
       int i;
       for (i=0; i<BUF_SIZE; i++)
          buffer[i] = 0;
       buffer_ready = 1;
    }
    The for-loop does not access any volatile locations, nor does it perform any side-effecting operations. Therefore, the compiler is free to move the loop below the store to buffer_ready, defeating the developer's intent. Making the buffer volatile would prevent this transformation.
    Inserting a "side-effecting operation" into the loop would also prevent the undesired transformation.
    Would inserting any function call within the loop do the trick?
    If so, does the Standard require inline functions to do the same?
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    I think that function can generate side effect only if:
    1° It's return type is not void.
    2° It doesn't have a empty argument list
    3° It changes some global variable state.

    If any of these conditions is satisfied a function should generate a side-effect.As for the C standard,order in which the arguments are evaluated is also implementation defined, so everything is same as for the non-inlined functions except if that function's object code produces side effects once inlined the block in which it were inlined should generate a side effect too.

    However this is all just my opinion,I didn't find anywhere a quote from a C standard which defines how do the side effects related to the functions form.

    It would be very nice if someone could give us all some good input on this,because it is really an interesting topic which is sadly somehow poorly documented. :(

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by donbock
      The paper Volatiles are Miscompiled, and What to Do about It by Eide and Regehr reports that mishandling of volatiles is a common category of compiler bugs. You should read it if you use volatiles for memory-mapped I/O or thread synchronization . Almost in passing they describe a common programmer error with volatiles:

      Inserting a "side-effecting operation" into the loop would also prevent the undesired transformation.
      Would inserting any function call within the loop do the trick?
      If so, does the Standard require inline functions to do the same?
      I disagree here: volatile reads and writes may not be moved beyond sequence points; all the assignments in the loop are a sequence point and moving the volatile store before that point would be a compiler error. It doesn't matter whether or not the evaluations in the loop are volatile ... If a compiler still juggles with this volatile handling (in its optimizer?) it's just a compiler bug and should be reported. It surprises me that so many ANSI C 'Standard conforming' compilers have those bugs ...

      kind regards,

      Jos

      Comment

      Working...