Closed xiaxinmeng closed 1 year ago
If you overwrite the contents of the file, it gets propagated to the mapping and your mm[2]
is indeed invalid memory access. I get a segfault even with CPython. If you really want to write different contents to the file and keep the mapping pointing to the old one, os.unlink
the old one before writing the new one:
import mmap
import os
if __name__ == '__main__':
with open('test.dat', 'wb') as fh:
fh.write(b'abcdefghijklmnopqrstuvwxyz')
with open('test.dat', 'rb') as fh:
mm = mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ)
os.unlink('test.dat')
with open('test.dat', 'wb') as fh:
pass
mm[2]
See the following example. We access a mmap file, but this file is overwritten. graalpython reports a segfault
test.py
Error message:
Environment: 'graalpy-22.3.1-linux-amd64/bin/graalpy' Python 3.8.5 [Graal, GraalVM CE, Java 19.0.2] on Ubuntu 18.04