1

Is there a way to get Clang, GCC or Visual Studio to emit a runtime warning whenever memory access is misaligned and preferably also emit source code location for it?

I need to find all spots in my huge legacy sources (that I didn't write myself) which contain unaligned accesses and then wrap them in a filter explicitly, which makes them aligned.

I need this to port the sources to platforms that will break on misaligned access.

Tim
  • 41,901
  • 18
  • 127
  • 145
thesaint
  • 1,253
  • 1
  • 12
  • 22
  • Does the code have a lot of #pragma pack directives? They are the main cause. – cup May 08 '14 at 11:55
  • 1
    you can not be sure that your target compiler layouts structures or data elements in the same way as your pc compiler does. so what would be the value of this warnings? – vlad_tepesch May 08 '14 at 11:55
  • 3
    Can the "compiler emit a runtime warning"?! How is that supposed to work? – Kerrek SB May 08 '14 at 11:56
  • Add an LLVM pass that adds code that checks alignment for every pointer dereferencing. – nwp May 08 '14 at 11:58
  • @vlad_tepesch: 1) the target compiler is clang, so that would be sufficient 2) misalignment comes from the CPU emulator represented by the sources, not by the compiler I am using. So it should be the same with any standard compliant compiler as it is just "instructed" to do misaligned accesses – thesaint May 08 '14 at 12:06
  • @KerrekSB: Like print something to stdout?! – thesaint May 08 '14 at 12:06
  • 1
    @thesaint You missed the point. Generally compiler compiles not runs the code. – luk32 May 08 '14 at 12:07
  • @nwp: The value of that comment is determined by whether or not such a pass does already exist?! – thesaint May 08 '14 at 12:07
  • @cup: It contains no pragma pack – thesaint May 08 '14 at 12:08
  • @luk32: The point of what? A compiler is very well intended to add runtime instrumentation to code, at least for tha last 20 years. – thesaint May 08 '14 at 12:10
  • @thesaint CPU-Emulator? what are you talking about? you sad your target does require aligned memory access and you talked about 3 different compiler. even if you use the same compiler family on both systems the data layout may be different on different systems. – vlad_tepesch May 08 '14 at 12:11
  • @vlad_tepesch: Unlikely. None of these compilers will "accidentially" emit misaligned memory access if it wasn't instructed to do so. The misalignment comes from the fact that the sources represent a CPU emulator for a CPU that does misaligned accesses. And whenever it does misaligned accesses it will do them regardless of the compiler you used to produce the CPU emulator's executable because otherwise they would just have broken it. – thesaint May 08 '14 at 12:13
  • @thesaint Then the compiler does not emit the warning, but generates the code and adds it to the executable. How can a compiler do anything when the compiled binary runs... Nevermind. – luk32 May 08 '14 at 12:14
  • 1
    @luk32: Nevermind is probably the best. We are not in some corny high school class here. Of course the compiler doesn't run the code. But it can add instrumentation that is run at runtime OMG... Seriously – thesaint May 08 '14 at 12:16
  • Anyway can you please stick to my original question. Its not a discussion about my legacy sources or whatever. The question was pretty clear I think. – thesaint May 08 '14 at 12:17
  • @thesaint I am not aware that such a pass exists and I would be suprised if it does. I did hear, however, that adding a pass to LLVM is easy. Maybe there is an LLVM-tool already that finds C-casts which are usually difficult to find to go through the casts and check them manually if that can be done with acceptable effort. – nwp May 08 '14 at 13:01
  • There is a feature request for valgrind asking for this feature: [link](https://bugs.kde.org/show_bug.cgi?id=274858). Feel free to upvote the feature request. – farindk May 08 '14 at 14:22

2 Answers2

1

GCC has the -Wcast-align option. However, it will raise a compile-time warning, not a runtime one, because compilers can't raise runtime warnings or errors.

If you need more information, I suggest you to read this SO question, which talks about misaligned memory.

EDIT: Added the part about GCC raising compile-time and not runtime warnings.

Community
  • 1
  • 1
Teo Zec
  • 383
  • 2
  • 11
1

Another alternative is to raise an exception by setting bit 18 (alignment check) of the EFLAGS register. If you're developing on x86[-64], and the code coverage is sufficient, this might be a useful runtime mechanism to track down misaligned R/W.

With gcc/clang:

__asm__ ("pushf\n\t"
         "orl $ 0x40000, (%esp)\n\t"
         "popf\n\t");

Related questions here and here. Compile-time checks may be useful too, but cannot help with dynamic addresses.

Community
  • 1
  • 1
Brett Hale
  • 21,653
  • 2
  • 61
  • 90