JuliaLang/julia — pull request #6877 by timholy (2014-05-18 13:54:50+00:00)
Title: Catch writes to protected memory in mmapped arrays (fix #3434)

Starting from the example in #6703,
```
julia> s = open("/tmp/a.bin", "r")
IOStream()

julia> m = mmap_array(Float64, (10,1), s)
10x1 Array{Float64,2}:
0.751866
0.832087
0.239773
0.120287
0.867469
0.957427
0.941344
0.642588
0.56932
0.464532

julia> m[3] = 2.0
ERROR: MemoryError()
in setindex! at array.jl:305

julia> m = 5
5

julia> gc()

```
If desired I'd be happy to change the error message to something more descriptive (e.g., `"Memory-access error. Did you try to set the value of a read-only mmapped array?"`). I seem to remember some discussion about problems that can arise from more verbose error message, which is why I started with something minimal.

Summary of design: store begin/end pointers of blocks of memory that are write-protected; if you get a SIGSEGV, check the address to see if it falls within one of these protected pages. If it doesn't use the default SIGSEGV handling.

I can't test this on Windows or OSX (and IIUC the AppVeyor testing is still not finished?), but there's relatively little of this that's OS-specific.