Re: [OT] 'volatile' in userspace



On Sun, Jul 09, 2006 at 10:40:06PM +0200, Rutger Nijlunsing wrote:
So if a userspace progam ever uses volatile, it's almost certainly a
bug, one way or another.

Without 'volatile' and disabling optimizations altogether, how do we
prevent gcc from optimizing away pointers? As can be seen on
http://wiki.rubygarden.org/Ruby/page/show/GCAndExtensions (at
'Compiler over-optimisations and "volatile"'), volatile is used to
prevent a specific type of optimization. This is because of the
garbage collector, which scans the stack and registers to find
referenced objects. So you don't want local variables containing
references to objects optimized away.

Well, if you look at the Wiki, it admits that this is a bug:

(Warning: This section is not strictly correct. volatile
instructs the C compiler that it should not do certain
optimisations to code that accesses the variable - the value
cannot be stored in a register and must be read from memory
each time it is accessed. It is still perfectly legal for the
compiler to overwrite the VALUE's stack location with other
data, if the compiler decides there are no further uses of the
VALUE. Fortunately, a side effect of volatile in common C
compilers like GCC and Visual Studio is to prevent the
dangerous optimisation described above. The Ruby source itself
uses volatile for this purpose, so it is an "accepted hack"
for Ruby C extensions.)

"Accepted hack" is basically another way of saying bug. Some day GCC
will be made smart enough to optimize the use of str on the stack, and
then the Ruby will be screwed. (Mental note to self: don't use Ruby
in any future project.)

This is really an architectural bug. RSTRING() should have explicitly
bumped a use pointer, which the C code should have then explicitly
decremented, to protect the underlying pointer from getting GC'ed
unexpectedly. It would have made RSTRING() more difficult to use, but
that's the price you pay when you try to graft a garbage-collected
paradigm into C code, since the C language really was never designed
for it.

So this would tend to confirm the rule of thumb: use of "volatile" in
a userspace progam tends to indicate a bug.

- Ted
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



Relevant Pages

  • Re: Substitute for volatile
    ... > I am a newbie w.r.t. compiler optimizations and I believe these ... "volatile" is a hint to the compiler that the contents of a variable may ... optimizations are at a function level, ...
    (comp.lang.c)
  • Re: Volatile + multithreading
    ... The volatile keyword has little use in multithreaded programming. ... > field or global seems to have no impact because it seems the compiler will ... operations as "special" and suppress optimizations around them. ... > majority of the multithreading issues that could have resulted from ...
    (microsoft.public.vc.language)
  • Re: /Od compiler option problem
    ... >> found out that when I had the compiler option /Od set the project would ... >> work properly but when it was not set the bug would appear. ... > When optimizations are enabled the compiler may do those things and more. ... > declare success again or you can try to find the cause. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: [64-bit Delphi] A msg from Hairy (van Tassel)
    ... > TeamB members telling the poster he's got it all wrong, ... Update to D7 introduced bugs in the compiler. ... No one yet has been able to point to a bug ... optimizations, but the removal of optimizations is /not/ a bug, despite ...
    (borland.public.delphi.non-technical)
  • Re: /Od compiler option problem
    ... > work properly but when it was not set the bug would appear. ... /Od means "disable the optimizing compiler". ... It is important to disable optimizations while ... declare success again or you can try to find the cause. ...
    (microsoft.public.dotnet.languages.vc)