On ARM after writing instructions to memory a memory barrier is needed before executing the instructions. Specifically clean the data cache, invalidate the instruction cache, then execute an instruction sync barrier (ISB
) on the CPU that will execute the code.
One can use cp
to copy an executable or shared library then execute without an explicit memory barrier. This amounts to:
- Open the file.
- Write to the file with
write
. - Close the file.
- Open the file.
- Map the file with
mmap
withPROT_READ | PROT_EXEC
. - Execute the code.
Likewise one can presumably use mmap
to write to the file:
- Open file.
- Map the file with
mmap
withPROT_READ | PROT_WRITE
anMAP_SHARED
. - Write to the with normal memory writes.
- Unmap the file with
munmap
. - Close the file.
- Open the file.
- Map the file with
mmap
withPROT_READ | PROT_EXEC
- Execute the code.
So where in above steps is the necessary cache manipulation hiding? Is it in munmap
or in mmap
? Assume there is no disk access.
Presumably if neither munmap
or mmap
are called between writing and execution explicit cache synchronisation is needed with a call to __clear_cache
, but can this done with either mapping?