August 30th, 2004

Importance of alignment even on x86 machines, part 2

The various Interlocked functions (InterlockedIncrement, and so on) require that the variable being updated be properly aligned, even on x86, a platform where the CPU silently fixes unaligned memory access invisibly.

If you pass an unaligned pointer to one of the Interlocked functions, the operation will still succeed, but the result won’t be atomic. Another processor may see a partially-completed update.

This is a particularly insidious bug since it happens only on multiprocessor machines under very tight timing conditions. You will be hard-pressed to reproduce this in the laboratory.

(A commenter stole my thunder and remarked on it yesterday.)

Moral of the story: Same as yesterday. Mind your alignment.

Topics
History

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.