How does the MMU detect double pointer-free?

How does the memory management module (MMU) detect double pointer-free?

I know that it is good practice to make a NULL pointer immediately after it is released, but let the programmer not do it. Is there any MMU mechanism to detect it?

+4
source share
4 answers

MMU has nothing to do with it. If you free the pointer allocated twice in malloc, you are likely to mess up a bunch of C runtimes. A heap (not an MMU) can basically protect itself from such things, but most of them don't. Note that this has nothing to do with the operating system - neither malloc () nor free () are system calls.

+13
source
How does the memory management unit (MMU) detect double pointer-free?

MMU is just a virtual address space β†’ physical memory mapping, it does not know anything about how the heap is organized / how distribution works / ..., that is, a working system / allocator works.

How does the OS detect double free sweat? What is the mechanism?

He looks through the list / bitmap / ... of the selected blocks, sees that there is no allocated block with the address that you gave him, so he discovers that it is double free.

However, if this block is already redistributed, it finds it and correctly releases it =>, but now the code that used the redistributed block will go out, because the memory that it acquired correctly and that it doesn’t get free.

If the allocator protects unallocated memory, marking it as unreadable and not writing / deleting it from fixed pages of the virtual address space, the program will die as soon as this memory is accessed again (but the code that apparently caused the crash will be virtually innocent, because he did nothing wrong).

Otherwise, the application may work for some time until this block of memory is transferred to some other part of the code that would request some memory. At this point, two parts of the same application will try to work with the same memory block with all the unrest that may arise from this.

(Thanks to Pascal Cuoc for pointing out my mistake.)

+5
source

No, there is no MMU mechanism to detect it. It is generally accepted that calling free on an already free address causes the program to crash, since introducing free does something unexpected and causes a segmentation error.

Running valgrind is a good way to check for memory management issues, such as double freeing a pointer.

+3
source

Setting it to NULL is not really good practice; it hides errors. Especially double free () s. Check the OS memory card, something like 0xfeeefeee or 0xdeadbeef is generally good.

You can diagnose double free () s with a debug allocator. Most other decent CRTs have one.

+1
source

Source: https://habr.com/ru/post/1301187/


All Articles