2

Problems caused by insufficient memory alignment can often seem to appear at random, since things can be correctly aligned by coincidence until you change something unrelated. Is there a compiler flag or some other way to bring all of these problems out of hiding? I'd want it to make sure that types are never more aligned than they have to be. For example, on x86, the address of chars would always be odd, and the address of ints would always be ≡ 4 (mod 8). (Obviously, this would waste memory and probably make the program slower too, so it's not something I'd leave enabled in a release build.)

Edit: For clarification, this should only affect the starting addresses of arrays and structs, not their internal layouts (the latter would break compatibility with basically everything, which I definitely don't want).

  • Putting a `char` only on odd addresses would make no sense. Think of a normal string, it would have holes in it if only every second byte of the string was used. – Some programmer dude Jun 15 '20 at 16:21
  • @Someprogrammerdude For a `char[]`, I'd just care that it starts on an odd address, not that all of its elements were at odd addresses. – Joseph Sible-Reinstate Monica Jun 15 '20 at 16:26
  • Why odd though? As everything else, memory addresses are zero-based, which means the first address starts at `0`. Why not every even address? And in a structure where a compiler might omit padding between e.g. `char` members, it will be a waste of space adding padding between such members when it's not really needed. Alignment of `char` is (on x86) 1, which means it can safely be stored on *every* address, odd or even. – Some programmer dude Jun 15 '20 at 16:33
  • " it can safely be stored on *every* address, odd or even" Some buggy code might be making an assumption that some particular `char` will always be at an even address. I'd like to force all individual `char`s to have odd addresses to break any code making that assumption early. – Joseph Sible-Reinstate Monica Jun 15 '20 at 16:37
  • "adding padding between such members" I don't want to add any padding to structures. I only want this to affect things that aren't inside of `struct`s or arrays (see my edit). – Joseph Sible-Reinstate Monica Jun 15 '20 at 16:38
  • You say that "[s]ome buggy code might be making an assumption that some particular `char` will always be at an even address". Assumptions are bad, and if it's in someone else's code there's really not much you can do about it other than file a bug-report. If you write the best possible code, following all standards for the language and operating system, and it turns out to break some other code it's not your fault, and you shouldn't really go into a project bending your code and its behavior to fit faulty code (unless directed to by your superiors). – Some programmer dude Jun 15 '20 at 17:21
  • 1
    @Someprogrammerdude I don't want to bend my code to fit the bad assumptions. I want to do the exact opposite, breaking as many bad assumptions as I can, so that I can find them and get them fixed. – Joseph Sible-Reinstate Monica Jun 15 '20 at 17:50
  • Maybe for globals, you could use GCC's attribute and place each global variable in a separate section. Then in the Linker file you could locate each section at specific address (odd, even, etc). Similarly to what is described here: https://mcuoneclipse.com/2012/11/01/defining-variables-at-absolute-addresses-with-gcc/ . Local variables are trickier... – Alex Lop. Jun 15 '20 at 18:56
  • @AlexLop. That would require me to either know ahead of time which variables need it, or do it to every single variable in my program, both of which are impractical. – Joseph Sible-Reinstate Monica Jun 15 '20 at 21:35
  • @JosephSible-ReinstateMonica it can be done by a python/perl script after the code is s written. Probably as part of the build process – Alex Lop. Jun 16 '20 at 03:24

0 Answers0