The code above writes the full length of the file from each and every page. So, even though a page is at most 4096 bytes, the write_page routine is trying to copy data from each page far past the end of the page boundary. This puts random data into the file.
The contents of memory mapped files created using mmap are not written correctly.
The following test program (adapted from https://stackoverflow.com/questions/26259421/use-mmap-in-c-to-write-into-memory) can reproduce the problem:
The following commands will run the test.
The result should be that testin and testout are the same. Instead testout is mostly filled with "random" data.
The root cause of the problem is this line of code: https://github.com/mikaku/Fiwix/blob/abc14a046e017331894c865beaa0977ec6b29e58/mm/mmap.c#L288
The code above writes the full length of the file from each and every page. So, even though a page is at most 4096 bytes, the write_page routine is trying to copy data from each page far past the end of the page boundary. This puts random data into the file.
A PR with a suggested fix is forthcoming.