0

I am currently working on an interpreter for educational purposes, and I want the interpreter to be able to allocate aligned heap memory.

The instructions of my interpreter may contain addresses to this heap memory, and since I want it to be a little secure I would like to restrict the user to only address memory which was allocated by the interpreter.

So I tried to write my own heap allocator, which works on top of malloc and realloc, with the goal to be able to allocate blocks within a contiguous memory region in order to restrict accesses to that single big heap block.

Now a problem arose: Since I use realloc, the interpreters memory may move around, and the paddings I added in the heap allocator may no longer be correct.

I am a bit lost; Is there a portable way how I could securely give an interpreter aligned memory allocation capabilities?

Julius
  • 1,155
  • 9
  • 19
  • 1
    `may no longer be correct` how would it be possible? The pointer returned by `malloc` and `realloc` is aligned to the biggest alignment on the platform (`_Alignas(max_align_t)`). – KamilCuk Apr 14 '20 at 08:03
  • 2
    Instead of storing addresses directly only store offsets from the base. Then it doesn't matter what the base is or how it might change. – Some programmer dude Apr 14 '20 at 08:05
  • @KamilCuk as far as I know [aligned allocations can be larger](https://stackoverflow.com/questions/39677063/different-between-aligned-malloc-and-standard-malloc) than mallocs, or am I missing something? – Julius Apr 14 '20 at 08:14
  • @Someprogrammerdude That's what I am doing, but as far as I see the alignment requirements may not be satisfied anymore if they are larger than those of malloc – Julius Apr 14 '20 at 08:17
  • 1
    C11's `aligned_alloc()` and don't use `realloc()`? – Shawn Apr 14 '20 at 08:18
  • @Shawn if the instructions stored addresses returned by aligned_alloc, I don't see a way how I could ensure that the interpreter doesn't access any memory which wasn't allocated by the interpreter – Julius Apr 14 '20 at 08:20
  • 1
    What are your requirements? Why do you need such large alignments? Anything beyond the native word-length is usually overkill (and wasteful). – Some programmer dude Apr 14 '20 at 08:20
  • @Someprogrammerdude my requirements for this case are to be able to interpret C11's aligned_alloc :-/ – Julius Apr 14 '20 at 08:22
  • What does alignment even matter if what you're really asking is 'how do I restrict what memory my program reads'? (Trying to use memory your program isn't allowed to because it's not allocated is a bug anyways and you shouldn't even be trying to do so). – Shawn Apr 14 '20 at 08:25
  • @Shawn the goal is to be able to interpret C11's aligned_alloc – Julius Apr 14 '20 at 08:28
  • That's very different from what your question is asking. – Some programmer dude Apr 14 '20 at 08:31
  • @Someprogrammerdude I rephrased the question itself to make it more clear – Julius Apr 14 '20 at 08:42
  • If I understood you correctly you want to interprete a C programm and all memory allocations are simulated by your interpreter... if that so, you should just do what Someprogrammerdude already suggested, store the values for pointer types as an offset of a base. That way when your interpreter needs to realloc the heap memory it just changes the base and nothing else needs to change – Ackdari Apr 14 '20 at 08:51

0 Answers0