authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-28 12:47:10-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-28 12:47:10-04:00
log568dc56232d8a1bf53495dd1c742ffae383ceaab
treecc5d15a0a272ac17b50ad98eb8a22fb3131049d9
parentd1b6f29d225bbedd0afb97fce64f5d042df4d9c6
parentc518b7b8bf71ad77adc96b5c9dabea277fe3ee43
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'LemonBoy-guard-pages-in-threads'


2 files changed, 34 insertions(+), 11 deletions(-)

std/os.zig+11-2
...@@ -1883,20 +1883,29 @@ pub fn inotify_rm_watch(inotify_fd: i32, wd: i32) void {...@@ -1883,20 +1883,29 @@ pub fn inotify_rm_watch(inotify_fd: i32, wd: i32) void {
1883}1883}
18841884
1885pub const MProtectError = error{1885pub const MProtectError = error{
1886 /// The memory cannot be given the specified access. This can happen, for example, if you
1887 /// mmap(2) a file to which you have read-only access, then ask mprotect() to mark it
1888 /// PROT_WRITE.
1886 AccessDenied,1889 AccessDenied,
1890
1891 /// Changing the protection of a memory region would result in the total number of map‐
1892 /// pings with distinct attributes (e.g., read versus read/write protection) exceeding the
1893 /// allowed maximum. (For example, making the protection of a range PROT_READ in the mid‐
1894 /// dle of a region currently protected as PROT_READ|PROT_WRITE would result in three map‐
1895 /// pings: two read/write mappings at each end and a read-only mapping in the middle.)
1887 OutOfMemory,1896 OutOfMemory,
1888 Unexpected,1897 Unexpected,
1889};1898};
18901899
1891/// `memory.len` must be page-aligned.1900/// `memory.len` must be page-aligned.
1892pub fn mprotect(memory: [*]align(mem.page_size) u8, protection: u32) MProtectError!void {1901pub fn mprotect(memory: []align(mem.page_size) u8, protection: u32) MProtectError!void {
1893 assert(mem.isAligned(memory.len, mem.page_size));1902 assert(mem.isAligned(memory.len, mem.page_size));
1894 switch (errno(system.mprotect(memory.ptr, memory.len, protection))) {1903 switch (errno(system.mprotect(memory.ptr, memory.len, protection))) {
1895 0 => return,1904 0 => return,
1896 EINVAL => unreachable,1905 EINVAL => unreachable,
1897 EACCES => return error.AccessDenied,1906 EACCES => return error.AccessDenied,
1898 ENOMEM => return error.OutOfMemory,1907 ENOMEM => return error.OutOfMemory,
1899 else => return unexpectedErrno(err),1908 else => |err| return unexpectedErrno(err),
1900 }1909 }
1901}1910}
19021911
std/thread.zig+23-9
...@@ -223,15 +223,17 @@ pub const Thread = struct {...@@ -223,15 +223,17 @@ pub const Thread = struct {
223 }223 }
224 };224 };
225225
226 const MAP_GROWSDOWN = if (os.linux.is_the_target) os.linux.MAP_GROWSDOWN else 0;226 var guard_end_offset: usize = undefined;
227
228 var stack_end_offset: usize = undefined;227 var stack_end_offset: usize = undefined;
229 var thread_start_offset: usize = undefined;228 var thread_start_offset: usize = undefined;
230 var context_start_offset: usize = undefined;229 var context_start_offset: usize = undefined;
231 var tls_start_offset: usize = undefined;230 var tls_start_offset: usize = undefined;
232 const mmap_len = blk: {231 const mmap_len = blk: {
233 // First in memory will be the stack, which grows downwards.232 var l: usize = mem.page_size;
234 var l: usize = mem.alignForward(default_stack_size, mem.page_size);233 // Allocate a guard page right after the end of the stack region
234 guard_end_offset = l;
235 // The stack itself, which grows downwards.
236 l = mem.alignForward(l + default_stack_size, mem.page_size);
235 stack_end_offset = l;237 stack_end_offset = l;
236 // Above the stack, so that it can be in the same mmap call, put the Thread object.238 // Above the stack, so that it can be in the same mmap call, put the Thread object.
237 l = mem.alignForward(l, @alignOf(Thread));239 l = mem.alignForward(l, @alignOf(Thread));
...@@ -253,20 +255,32 @@ pub const Thread = struct {...@@ -253,20 +255,32 @@ pub const Thread = struct {
253 }255 }
254 break :blk l;256 break :blk l;
255 };257 };
258 // Map the whole stack with no rw permissions to avoid committing the
259 // whole region right away
256 const mmap_slice = os.mmap(260 const mmap_slice = os.mmap(
257 null,261 null,
258 mem.alignForward(mmap_len, mem.page_size),262 mem.alignForward(mmap_len, mem.page_size),
259 os.PROT_READ | os.PROT_WRITE,263 os.PROT_NONE,
260 os.MAP_PRIVATE | os.MAP_ANONYMOUS | MAP_GROWSDOWN,264 os.MAP_PRIVATE | os.MAP_ANONYMOUS,
261 -1,265 -1,
262 0,266 0,
263 ) catch |err| switch (err) {267 ) catch |err| switch (err) {
264 error.MemoryMappingNotSupported => unreachable, // no file descriptor268 error.MemoryMappingNotSupported => unreachable,
265 error.AccessDenied => unreachable, // no file descriptor269 error.AccessDenied => unreachable,
266 error.PermissionDenied => unreachable, // no file descriptor270 error.PermissionDenied => unreachable,
267 else => |e| return e,271 else => |e| return e,
268 };272 };
269 errdefer os.munmap(mmap_slice);273 errdefer os.munmap(mmap_slice);
274
275 // Map everything but the guard page as rw
276 os.mprotect(
277 mmap_slice,
278 os.PROT_READ | os.PROT_WRITE,
279 ) catch |err| switch (err) {
280 error.AccessDenied => unreachable,
281 else => |e| return e,
282 };
283
270 const mmap_addr = @ptrToInt(mmap_slice.ptr);284 const mmap_addr = @ptrToInt(mmap_slice.ptr);
271285
272 const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(*Thread, mmap_addr + thread_start_offset));286 const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(*Thread, mmap_addr + thread_start_offset));