aboutsummaryrefslogtreecommitdiff
path: root/lib/std/os/linux/tls.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-12-18 15:38:38 -0700
committerAndrew Kelley <andrew@ziglang.org>2020-12-18 15:54:01 -0700
commit53987c932c9d62cc9cdae3d523fb62756ce83ca9 (patch)
tree280817d390ef900cf590c40f47cca4230b354b0e /lib/std/os/linux/tls.zig
parent2b8dcc76eba92ad44bd88756218de8d2bd8a1c10 (diff)
downloadzig-53987c932c9d62cc9cdae3d523fb62756ce83ca9.tar.gz
zig-53987c932c9d62cc9cdae3d523fb62756ce83ca9.zip
std.crypto.random: introduce fork safety
Everybody gets what they want! * AT_RANDOM is completely ignored. * On Linux, MADV_WIPEONFORK is used to provide fork safety. * On pthread systems, `pthread_atfork` is used to provide fork safety. * For systems that do not have the capability to provide fork safety, the implementation falls back to calling getrandom() every time. * If madvise is unavailable or returns an error, or pthread_atfork fails for whatever reason, it falls back to calling getrandom() every time. * Applications may choose to opt-out of fork safety. * Applications may choose to opt-in to unconditionally calling getrandom() for every call to std.crypto.random.fillFn. * Added `std.meta.globalOption`. * Added `std.os.madvise` and related bits. * Bumped up the size of the main thread TLS buffer. See the comment there for justification. * Simpler hot path in TLS initialization.
Diffstat (limited to 'lib/std/os/linux/tls.zig')
-rw-r--r--lib/std/os/linux/tls.zig39
1 files changed, 24 insertions, 15 deletions
diff --git a/lib/std/os/linux/tls.zig b/lib/std/os/linux/tls.zig
index d956266114..6d61e60e95 100644
--- a/lib/std/os/linux/tls.zig
+++ b/lib/std/os/linux/tls.zig
@@ -327,34 +327,43 @@ pub fn prepareTLS(area: []u8) usize {
if (tls_tp_points_past_tcb) tls_image.data_offset else tls_image.tcb_offset;
}
-var main_thread_tls_buffer: [256]u8 = undefined;
+// The main motivation for the size chosen here is this is how much ends up being
+// requested for the thread local variables of the std.crypto.random implementation.
+// I'm not sure why it ends up being so much; the struct itself is only 64 bytes.
+// I think it has to do with being page aligned and LLVM or LLD is not smart enough
+// to lay out the TLS data in a space conserving way. Anyway I think it's fine
+// because it's less than 3 pages of memory, and putting it in the ELF like this
+// is equivalent to moving the mmap call below into the kernel, avoiding syscall
+// overhead.
+var main_thread_tls_buffer: [0x2100]u8 align(mem.page_size) = undefined;
pub fn initStaticTLS() void {
initTLS();
- const alloc_tls_area: []u8 = blk: {
- const full_alloc_size = tls_image.alloc_size + tls_image.alloc_align - 1;
-
+ const tls_area = blk: {
// Fast path for the common case where the TLS data is really small,
- // avoid an allocation and use our local buffer
- if (full_alloc_size < main_thread_tls_buffer.len)
- break :blk main_thread_tls_buffer[0..];
+ // avoid an allocation and use our local buffer.
+ if (tls_image.alloc_align <= mem.page_size and
+ tls_image.alloc_size <= main_thread_tls_buffer.len)
+ {
+ break :blk main_thread_tls_buffer[0..tls_image.alloc_size];
+ }
- break :blk os.mmap(
+ const alloc_tls_area = os.mmap(
null,
- full_alloc_size,
+ tls_image.alloc_size + tls_image.alloc_align - 1,
os.PROT_READ | os.PROT_WRITE,
os.MAP_PRIVATE | os.MAP_ANONYMOUS,
-1,
0,
) catch os.abort();
- };
- // Make sure the slice is correctly aligned
- const begin_addr = @ptrToInt(alloc_tls_area.ptr);
- const begin_aligned_addr = mem.alignForward(begin_addr, tls_image.alloc_align);
- const start = begin_aligned_addr - begin_addr;
- const tls_area = alloc_tls_area[start .. start + tls_image.alloc_size];
+ // Make sure the slice is correctly aligned.
+ const begin_addr = @ptrToInt(alloc_tls_area.ptr);
+ const begin_aligned_addr = mem.alignForward(begin_addr, tls_image.alloc_align);
+ const start = begin_aligned_addr - begin_addr;
+ break :blk alloc_tls_area[start .. start + tls_image.alloc_size];
+ };
const tp_value = prepareTLS(tls_area);
setThreadPointer(tp_value);