IAIK/sweb

KMM misaligns objects after realloc with non-aligned size

Closed this issue · 3 comments

As opposed to allocateMemory, reallocateMemory does not align the size, and the KernelMemoryManager in general does not align heap allocations. Because of this, after calling krealloc with a non-aligned size (which ustl does quite often), future allocations may be non-aligned. This is a problem especially on ARM where misaligned Locks throw an exception when attempting to lock them.

We fixed this in our repo by just aligning the size of reallocs. I'm not sure if that's the best solution.

This test program manages to reproduce the problem on base SWEB, arm_icp quite reliably:

#include "nonstd.h"
#include "fcntl.h"

int main()
{
  // Use up some smaller free heap chunks
  for (int i = 0; i < 5; i++)
    open("/usr/README.txt", 0);

  // This will reallocate the buffer for the path with an odd size and misalign
  // more or less all objects belonging to the process, including the Loader and
  // its program_binary_lock_, which will cause a CPU exception in kernelspace.
  createprocess("///////////////////usr/mult.sweb", 1);

  return 0;
}

PR on the devel or master branch?