title: Memory alignment today and 20 years ago
body: <p>In the famous paper "Smashing the Stack for Fun and Profit", its author takes a C function</p>
  
  <pre><code>void function(int a, int b, int c) {
    char buffer1[5];
    char buffer2[10];
  }
  </code></pre>
  
  <p>and generates the corresponding assembly code output</p>
  
  <pre><code>pushl %ebp
  movl %esp,%ebp
  subl $20,%esp
  </code></pre>
  
  <p>The author explains that since computers address memory in multiples of word size, the compiler reserved 20 bytes on the stack (8 bytes for buffer1, 12 bytes for buffer2). </p>
  
  <p>I tried to recreate this example and got the following</p>
  
  <pre><code>pushl   %ebp
  movl    %esp, %ebp
  subl    $16, %esp
  </code></pre>
  
  <p>A different result! I tried various combinations of sizes for buffer1 and buffer2, and it seems that modern gcc does not pad buffer sizes to multiples of word size anymore. Instead it abides the <code>-mpreferred-stack-boundary</code> option.</p>
  
  <p>As an illustration -- using the paper's arithmetic rules, for buffer1[5] and buffer2[13] I'd get 8+16 = 24 bytes reserved on the stack. But in reality I got 32 bytes.</p>
  
  <p>The paper is quite old and a lot of stuff happened since. I'd like to know, what exactly motivated this change of behavior? Is it the move towards 64bit machines? Or something else?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>The code is compiled on a x86_64 machine using gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) like that:</p>
  
  <p><code>$ gcc -S -o example1.s example1.c -fno-stack-protector -m32</code></p>