Skip to content

Commit caa4bfa

Browse files
committed
Catch writes to protected memory in mmapped arrays (fix #3434)
1 parent 57b18ef commit caa4bfa

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/init.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,18 @@ void segv_handler(int sig, siginfo_t *info, void *context)
140140
{
141141
sigset_t sset;
142142

143-
if (in_jl_ || is_addr_on_stack(info->si_addr)) {
143+
if (in_jl_ || is_addr_on_stack(info->si_addr)) { // stack overflow
144144
sigemptyset(&sset);
145145
sigaddset(&sset, SIGSEGV);
146146
sigprocmask(SIG_UNBLOCK, &sset, NULL);
147147
jl_throw(jl_stackovf_exception);
148148
}
149+
else if (info->si_code == SEGV_ACCERR) { // writing to read-only memory (e.g., mmap)
150+
sigemptyset(&sset);
151+
sigaddset(&sset, SIGSEGV);
152+
sigprocmask(SIG_UNBLOCK, &sset, NULL);
153+
jl_throw(jl_memory_exception);
154+
}
149155
else {
150156
uv_tty_reset_mode();
151157
sigfillset(&sset);

test/file.jl

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ c = mmap_array(Uint8, (11,), s)
138138
@test c == "Hello World".data
139139
c = mmap_array(Uint8, (uint16(11),), s)
140140
@test c == "Hello World".data
141+
@test_throws MemoryError c[1] = 0 # read-only memory
141142
@test_throws ErrorException mmap_array(Uint8, (int16(-11),), s)
142143
@test_throws ErrorException mmap_array(Uint8, (typemax(Uint),), s)
143144
close(s)

0 commit comments

Comments
 (0)