authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-10-23 10:20:57+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-23 22:48:25-04:00
log1690b35770a97aec4b8a7b1b31a61b01e04f5656
tree33e221f8173d3732e0cb5899dc5e50fe176ace78
parent22b4c9e1a9595bd94ada4c500a430e2668ffcd07

std: Fix edge case in TLS tp calculation

The TLS area may be located in the upper part of the address space and, if the platform expects a constant offset to be applied, may make the tp register calculation overflow. Use +% instead of +, the overflow is harmless.

1 files changed, 4 insertions(+), 2 deletions(-)

lib/std/os/linux/tls.zig+4-2
......@@ -289,8 +289,10 @@ pub fn prepareTLS(area: []u8) usize {
289289 // Copy the data
290290 mem.copy(u8, area[tls_image.data_offset..], tls_image.init_data);
291291
292 // Return the corrected (if needed) value for the tp register
293 return @ptrToInt(area.ptr) + tls_tp_offset +
292 // Return the corrected value (if needed) for the tp register.
293 // Overflow here is not a problem, the pointer arithmetic involving the tp
294 // is done with wrapping semantics.
295 return @ptrToInt(area.ptr) +% tls_tp_offset +%
294296 if (tls_tp_points_past_tcb) tls_image.data_offset else tls_image.tcb_offset;
295297}
296298