Home > Uncategorized > volatile handling sometimes overly volatile

volatile handling sometimes overly volatile

March 2nd, 2009 Derek-Jones

The contents of some storage locations, used by a program, might be modified outside of the control of that program, e.g., a real-time clock or the input port of a communications device. In some cases writing to particular storage locations has an external effect, e.g., a sequence of bits is sent down a communications channel. This kind of behavior commonly occurs in embedded systems

C and C++ support the existence of variables that have been mapped to such storage locations through the use of the volatile type qualifier.

volatile long flag;
volatile time_t timer;
 
struct {
                    int f1 : 2;
          volatile int f2 : 2;
                    int f3 : 3;
         } x;

When a variable is declared using volatile compilers must assume that its value can change in ways unknown to the compiler and that storing values into such a variable can have external effects. Consequently almost all optimizations involving such variables are off limits. Idioms such as timer = timer; are used to reset or refresh timers and are not dead code.

Volatile is a bit of an inconvenience for writers of code optimizers, requiring them to add checks to make sure the expression or statement they want to attempt to optimize does not contain a volatile qualified variable. In many environments the semantics of volatile are not applicable, which means that bugs in optimizers have a much smaller chance of being detected than faults in other language constructs.

There have been a number of research projects investigating the use of the const qualifier, but as far as I know only one that has investigated volatile. The ‘volatile’ project found that all of the compilers tested generated incorrect code for some of the tests. License agreements prevented the researchers giving details for some compilers. One interesting observation for gcc was that the number of volatile related faults increased with successive compiler releases; it looks like additional optimizations were being implemented and these were not checking for variables being volatile qualified.

One practical output from the project was a compiler stress tester targeting volatile qualified variables. The code generated by stress testers often causes compilers to crash and hang and the researchers reported the same experience with this tool.

There is one sentence in the C Standard whose overly broad wording is sometimes a source of uncertainty: What constitutes an access to an object that has volatile-qualified type is implementation-defined. This sentence applies in two cases: datatypes containing lots of bytes and bit-fields.

To save space/money/time/power some processors access storage a byte, or half-word, at a time. This means that, for instance, an access to a 32-bit storage location may occur as two separate 16-bit operations; from the volatile perspective does that constitute two accesses?

Most processors do not contain instructions capable of loading/storing a specified sequence of bits from a byte. This means that accesses to bit-fields involve one or more bytes being loaded and the appropriate bits extracted. In the definition of x above an access to field f1 is likely to result in field f2 (and f3) being accessed; from the volatile perspective does that constitute two accesses?

Unfortunately it is very difficult to obtain large quantities of source code for programs aimed at the embedded systems market. This means that it is not possible to obtain reliable information on common usage patterns of volatile variables. If anybody knows of any such code base please let me know.

  1. March 10th, 2009 at 05:15 | #1

    Derek,
    Thanks for visiting safercode. Your blog is quite interesting and i’d surely be watching the feed for more articles like this. I’m particularly interested in the C standard vs implementation details that your articles have.
    It is true that volatiles are quite dangerous beasts, which is why we don’t use them at all in our code at my company (and we produce humungous amounts of code for embedded devices).
    For code base, you could look at some of the devices which have open-sourced their firmwares (like Asus WL-500W routers) but in most of these cases, they are based on linux kernel which again doesn’t use much volatiles (all the kernel developers detest it)

  1. No trackbacks yet.
Comments are closed.
www.wenn.com
FireStats icon Powered by FireStatswww.tinynibbles.com cialis from canada

cheapest viagra usa

cialis pharmacy

mail online order propecia

levitra next day delivery

cialis price in canada

online propecia prescriptions

generic viagra made in usa

cialis one a day

buy fast propecia

cheap order prescription propecia

cialis no prescription

best price for generic cialis

discount generic propecia

buy generic viagra india rx

online viagra gel to buy

buy cheap generic propecia

levitra online sales

generic levitra purchase

buying cialis next day delivery

best price generic propecia

fda levitra

cialis delivered overnight

canadian viagra india

cialis overnight delivery

ordering propecia online

generic levitra canada

online pharmacy propecia viagra

generic viagra canada

online generic cialis 100 mg

buy generic cialis

levitra order prescription

buy propecia canada

canadian online pharmacy cialis

combine cialis and levitra

cheapest propecia prescription

buy viagra online

drug generic propecia

cialis refractory

canada viagra

buy propecia online from usa pharmacy

discount propecia propecia

cialis 50 mg

how much cialis

levitra from canadian pharmacy

cialis transdermal

lowest price propecia

cialis en mexico

cost of propecia

order cheapest propecia online

cialis 100 mg generic

buy generic propecia

cialis cheap

brand name cialis

order propecia

herbal propecia

overnight delivery cialis

generic levitra overnight delivery

mexico pharmacy cialis

cheap fast levitra

online cialis

can i get viagra in mexico

ganeric cialis

buying generic propecia

buy cheap generic levitra

5 mg original brand cialis

buy levitra overnight

cheapest viagra online

best price cialis

mexico levitra

buy generic levitra

buy cialis once daily

levitra tabs

canadian pharmacy cialis

buy cialis for daily use

cialis woman

order cheap propecia

levitra viagra online

discount cialis india

mail order propecia

buy cialis 5 mg

cheap discount levitra

levitra online prescription

cialis vs levitra

levitra 10mg

bestellen levitra online

indian cialis

once daily cialis

china viagra

levitra online

cialis by mail

buy cialis online uk

buying cialis soft tabs 100 mg

generic propecia effective

lowest cost levitra

generic cialis sale

cheap cialis

levitra viagra cialis

cialis quick shipment

cheapest prices for viagra

generic levitra vardenafil

lowest price for propecia

cialis 100 mg

buy cialis canada

canadian propecia rx

buy propecia now

buy cialis fedex shipping

canadian viagra 50mg

online ordering propecia

buy levitra vardenafil

lowest propecia 1 mg

cheap propecia 5mg

buy branded viagra

canadian drugs propecia

buy canada levitra

best price for propecia

generic propecia sale

cialis dosage mg

cheap levitra

cialis on women

cialis professional 100 mg

cheapest price propecia cheap

generic levitra cheap

lowest priced propecia

generic levitra online

buy propecia cheap

levitra canadian

best way to use cialis

buy discount viagra

generic cialis from india

buy dosages levitra

get levitra

cheap propecia uk

canadian healthcare

canada meds viagra

hydrochlorothiazide cialis

cheapest propecia sale uk

for sale levitra

online propecia uk

cialis next day delivery

buy propecia generic

genuine cialis pills

canadian healthcare viagra

how much does cialis cost

buy propecia on line

name brand cialis

get cialis

canada viagra generic

buy cheap levitra

buy viagra online cheap us

internet pharmacy propecia

generic viagra canadian

canada cheap propecia

next day viagra

cialis discount

levitra in india

buy propecia in the uk

cialis daily in canada

cialis next day

buy cheapest propecia

generic viagra made in india

canadian healthcare pharmacy

cialis strenght mg

cheap viagra canada or india

cialis daily dosage pharmacy

cialis professional no prescription

next day delivery cialis

buy cheap levitra online

levitra where to buy

cialis purchase

discount levitra online

brand viagra over the net

buying propecia online

indian cialis generic

cheap levitra uk

buying viagra in canada

indian generic levitra

levitra buy online

how to buy cialis in canada

overnight delivery viagra

cialis overnight

generic cialis soft tabs

canadian viagra and healthcare

once a day viagra

canada generic propecia

levitra prescription

buying levitra online

levitra online no prescription

lowest propecia prices

buy viagra germany canadian meds

cialis from mexico

buy real viagra online

canada levitra

get cialis online

levitra mg

cialis and ketoconazole

low cost levitra

female viagra pills

cialis alternative

cialis 5 mg buy

canadian pharmacy

cialis generic 100 mg

buy prescription propecia without

buy viagra without prescription

cialis in mexico

ordering cialis gel

buy viagra mexico

levitra sales uk

buy levitra us

generic propecia alternative

best viagra

cheap levitra prescription

buy cialis in usa

buy cialis usa

cialis headaches

cheapest propecia uk

how much to buy viagra in pounds

buy propecia online prescription

how to get viagra

generic propecia finasteride

cialis soft pills

lowest propecia prices in canada

buy can from i propecia who

obtain viagra without prescription

canadian pharmacy viagra

cialis and canada custom

levitra cost

low cost propecia

lowest price on non generic levitra

cialis price

bio viagra herbal

getting cialis from canada

buy viagra china

levitra in canada

levitra online us

levitra pill

buying propecia

levitra now online

canada propecia prescription

cialis 5 mg

buy propecia prescriptions online

cheap propecia online prescription

cialis discounts

discount propecia online

buying online propecia

buy propecia where

cheap levitra tablets

lowest price levitra

generic propecia online pharmacy

cheap viagra from uk

5 mg daily cialis

cialis to buy

cheapest viagra

buy levitra online no prescription

buy propecia online pharmacy

generic propecia fda approved

how much is viagra

brand cialis for sale

cialis 20 mg

levitra discount

gele viagra

buy cialis cannada

cheap prescription propecia

buying generic cialis mexico rx

cheapest overnight cialis

how to get cialis in canada

lowest price propecia best

buy cialis online canada

canadian pharmacies cialis

generic propecia for sale

generic viagra india

canada online pharmacy propecia

get propecia online pharmacy

info levitra

cialis profesional

buy now propecia

does generic cialis work

discount drug propecia

discount us propecia

order generic levitra

discount levitra rx

online propecia prescription

canadian viagra

buy 5 mg cialis

cheap canadian viagra

cialis 5 mg italia

buying cialis

buy viagra on line

cialis prescription

buy propecia without prescription

generic cialis next day shipping

get levitra online

buy cialis without prescription

cialis price 100 mg

how strong is 5 mg of cialis

natural viagra

best price levitra

healthcare canadian pharmacy

i need to buy propecia

cialis online