0
Fork 0
mirror of https://codeberg.org/ziglang/zig synced 2026-11-18 07:05:05 +00:00
zig/lib/std/debug.zig

2952 lines
115 KiB

2019-05-26 13:17:34 -04:00
2019-05-24 22:52:07 -04:00
2019-05-26 13:17:34 -04:00
2016-08-17 20:11:04 -07:00
2019-05-26 13:17:34 -04:00
2020-05-05 05:14:22 -06:00
2020-05-05 05:14:22 -06:00
2019-05-27 02:16:05 -04:00
2020-05-05 05:14:22 -06:00
2022-10-07 21:24:44 +03:30
2019-05-26 13:17:34 -04:00
2019-05-26 13:17:34 -04:00
2019-05-26 13:17:34 -04:00
2019-05-26 13:17:34 -04:00
2019-05-26 13:17:34 -04:00
2019-05-26 13:17:34 -04:00
2019-05-26 13:17:34 -04:00
2020-05-05 05:14:22 -06:00
2019-05-27 02:16:05 -04:00
2018-02-28 21:19:51 -05:00
2020-05-05 05:14:22 -06:00
2021-06-19 21:10:22 -04:00
2018-01-14 10:19:21 -05:00
2018-12-02 18:54:04 -05:00
2018-12-02 18:54:04 -05:00
2024-07-09 12:51:48 -04:00
2018-12-02 18:54:04 -05:00
2018-12-02 18:54:04 -05:00
compiler: preserve result type information through address-of operator This commit introduces the new `ref_coerced_ty` result type into AstGen. This represents a expression which we want to treat as an lvalue, and the pointer will be coerced to a given type. This change gives known result types to many expressions, in particular struct and array initializations. This allows certain casts to work which previously required explicitly specifying types via `@as`. It also eliminates our dependence on anonymous struct types for expressions of the form `&.{ ... }` - this paves the way for #16865, and also results in less Sema magic happening for such initializations, also leading to potentially better runtime code. As part of these changes, this commit also implements #17194 by disallowing RLS on explicitly-typed struct and array initializations. Apologies for linking these changes - it seemed rather pointless to try and separate them, since they both make big changes to struct and array initializations in AstGen. The rationale for this change can be found in the proposal - in essence, performing RLS whilst maintaining the semantics of the intermediary type is a very difficult problem to solve. This allowed the problematic `coerce_result_ptr` ZIR instruction to be completely eliminated, which in turn also simplified the logic for inferred allocations in Sema - thanks to this, we almost break even on line count! In doing this, the ZIR instructions surrounding these initializations have been restructured - some have been added and removed, and others renamed for clarity (and their semantics changed slightly). In order to optimize ZIR tag count, the `struct_init_anon_ref` and `array_init_anon_ref` instructions have been removed in favour of using `ref` on a standard anonymous value initialization, since these instructions are now virtually never used. Lastly, it's worth noting that this commit introduces a slightly strange source of generic poison types: in the expression `@as(*anyopaque, &x)`, the sub-expression `x` has a generic poison result type, despite no generic code being involved. This turns out to be a logical choice, because we don't know the result type for `x`, and the generic poison type represents precisely this case, providing the semantics we need. Resolves: #16512 Resolves: #17194
2023-09-18 14:49:18 +01:00
2018-12-02 18:54:04 -05:00
2018-07-29 23:25:40 -04:00
2018-12-02 18:54:04 -05:00
2024-07-09 12:51:48 -04:00
2024-07-09 12:51:48 -04:00
2024-07-09 12:51:48 -04:00
2021-02-05 00:28:07 +07:00
2024-07-09 12:51:48 -04:00
2018-12-02 18:54:04 -05:00
2024-07-09 12:51:48 -04:00
2024-07-09 12:51:48 -04:00
2018-12-02 18:54:04 -05:00
2024-07-09 12:51:48 -04:00
2018-12-02 18:54:04 -05:00
2024-07-09 12:51:48 -04:00
2024-07-09 12:51:48 -04:00
2024-07-09 12:51:48 -04:00
2024-07-09 12:51:48 -04:00
2024-07-09 12:51:48 -04:00
2023-07-11 01:06:41 -04:00
2023-07-11 01:06:41 -04:00
2021-02-05 00:28:07 +07:00
2024-07-09 12:51:48 -04:00
2021-02-05 00:28:07 +07:00
2024-07-09 12:51:48 -04:00
2024-07-09 12:51:48 -04:00
2024-07-09 12:51:48 -04:00
2024-07-09 12:51:48 -04:00
2018-12-02 18:54:04 -05:00
2018-07-29 23:25:40 -04:00
2018-12-02 18:54:04 -05:00
2022-10-07 21:24:44 +03:30
2018-01-14 10:19:21 -05:00
2018-09-12 14:26:21 -04:00
2018-09-12 14:26:21 -04:00
2020-02-22 11:51:45 +01:00
2020-02-21 12:11:04 +01:00
2020-02-20 20:48:54 +01:00
2020-02-09 11:55:43 +01:00
2020-02-22 12:44:21 +01:00
2020-02-22 12:44:21 +01:00
2020-05-05 05:14:22 -06:00
2020-05-05 05:14:22 -06:00
2020-10-11 08:23:36 +00:00
2020-10-12 14:29:43 +05:30
2020-02-09 11:55:43 +01:00
2020-02-10 13:18:28 +01:00
2020-05-05 05:14:22 -06:00
2023-05-04 01:35:43 -04:00
2023-05-04 01:35:43 -04:00
2023-05-04 01:35:43 -04:00
2024-01-27 07:11:03 +00:00
2020-02-18 13:06:17 +01:00
2020-05-05 05:14:22 -06:00
2024-01-28 01:04:38 +02:00
2020-02-20 20:18:51 +01:00
2023-05-04 01:35:43 -04:00
2023-05-04 01:35:43 -04:00
2023-06-25 02:47:48 -04:00
2023-05-04 01:35:43 -04:00
add deflate implemented from first principles Zig deflate compression/decompression implementation. It supports compression and decompression of gzip, zlib and raw deflate format. Fixes #18062. This PR replaces current compress/gzip and compress/zlib packages. Deflate package is renamed to flate. Flate is common name for deflate/inflate where deflate is compression and inflate decompression. There are breaking change. Methods signatures are changed because of removal of the allocator, and I also unified API for all three namespaces (flate, gzip, zlib). Currently I put old packages under v1 namespace they are still available as compress/v1/gzip, compress/v1/zlib, compress/v1/deflate. Idea is to give users of the current API little time to postpone analyzing what they had to change. Although that rises question when it is safe to remove that v1 namespace. Here is current API in the compress package: ```Zig // deflate fn compressor(allocator, writer, options) !Compressor(@TypeOf(writer)) fn Compressor(comptime WriterType) type fn decompressor(allocator, reader, null) !Decompressor(@TypeOf(reader)) fn Decompressor(comptime ReaderType: type) type // gzip fn compress(allocator, writer, options) !Compress(@TypeOf(writer)) fn Compress(comptime WriterType: type) type fn decompress(allocator, reader) !Decompress(@TypeOf(reader)) fn Decompress(comptime ReaderType: type) type // zlib fn compressStream(allocator, writer, options) !CompressStream(@TypeOf(writer)) fn CompressStream(comptime WriterType: type) type fn decompressStream(allocator, reader) !DecompressStream(@TypeOf(reader)) fn DecompressStream(comptime ReaderType: type) type // xz fn decompress(allocator: Allocator, reader: anytype) !Decompress(@TypeOf(reader)) fn Decompress(comptime ReaderType: type) type // lzma fn decompress(allocator, reader) !Decompress(@TypeOf(reader)) fn Decompress(comptime ReaderType: type) type // lzma2 fn decompress(allocator, reader, writer !void // zstandard: fn DecompressStream(ReaderType, options) type fn decompressStream(allocator, reader) DecompressStream(@TypeOf(reader), .{}) struct decompress ``` The proposed naming convention: - Compressor/Decompressor for functions which return type, like Reader/Writer/GeneralPurposeAllocator - compressor/compressor for functions which are initializers for that type, like reader/writer/allocator - compress/decompress for one shot operations, accepts reader/writer pair, like read/write/alloc ```Zig /// Compress from reader and write compressed data to the writer. fn compress(reader: anytype, writer: anytype, options: Options) !void /// Create Compressor which outputs the writer. fn compressor(writer: anytype, options: Options) !Compressor(@TypeOf(writer)) /// Compressor type fn Compressor(comptime WriterType: type) type /// Decompress from reader and write plain data to the writer. fn decompress(reader: anytype, writer: anytype) !void /// Create Decompressor which reads from reader. fn decompressor(reader: anytype) Decompressor(@TypeOf(reader) /// Decompressor type fn Decompressor(comptime ReaderType: type) type ``` Comparing this implementation with the one we currently have in Zig's standard library (std). Std is roughly 1.2-1.4 times slower in decompression, and 1.1-1.2 times slower in compression. Compressed sizes are pretty much same in both cases. More resutls in [this](https://github.com/ianic/flate) repo. This library uses static allocations for all structures, doesn't require allocator. That makes sense especially for deflate where all structures, internal buffers are allocated to the full size. Little less for inflate where we std version uses less memory by not preallocating to theoretical max size array which are usually not fully used. For deflate this library allocates 395K while std 779K. For inflate this library allocates 74.5K while std around 36K. Inflate difference is because we here use 64K history instead of 32K in std. If merged existing usage of compress gzip/zlib/deflate need some changes. Here is example with necessary changes in comments: ```Zig const std = @import("std"); // To get this file: // wget -nc -O war_and_peace.txt https://www.gutenberg.org/ebooks/2600.txt.utf-8 const data = @embedFile("war_and_peace.txt"); pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer std.debug.assert(gpa.deinit() == .ok); const allocator = gpa.allocator(); try oldDeflate(allocator); try new(std.compress.flate, allocator); try oldZlib(allocator); try new(std.compress.zlib, allocator); try oldGzip(allocator); try new(std.compress.gzip, allocator); } pub fn new(comptime pkg: type, allocator: std.mem.Allocator) !void { var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Compressor var cmp = try pkg.compressor(buf.writer(), .{}); _ = try cmp.write(data); try cmp.finish(); var fbs = std.io.fixedBufferStream(buf.items); // Decompressor var dcp = pkg.decompressor(fbs.reader()); const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } pub fn oldDeflate(allocator: std.mem.Allocator) !void { const deflate = std.compress.v1.deflate; // Compressor var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Remove allocator // Rename deflate -> flate var cmp = try deflate.compressor(allocator, buf.writer(), .{}); _ = try cmp.write(data); try cmp.close(); // Rename to finish cmp.deinit(); // Remove // Decompressor var fbs = std.io.fixedBufferStream(buf.items); // Remove allocator and last param // Rename deflate -> flate // Remove try var dcp = try deflate.decompressor(allocator, fbs.reader(), null); defer dcp.deinit(); // Remove const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } pub fn oldZlib(allocator: std.mem.Allocator) !void { const zlib = std.compress.v1.zlib; var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Compressor // Rename compressStream => compressor // Remove allocator var cmp = try zlib.compressStream(allocator, buf.writer(), .{}); _ = try cmp.write(data); try cmp.finish(); cmp.deinit(); // Remove var fbs = std.io.fixedBufferStream(buf.items); // Decompressor // decompressStream => decompressor // Remove allocator // Remove try var dcp = try zlib.decompressStream(allocator, fbs.reader()); defer dcp.deinit(); // Remove const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } pub fn oldGzip(allocator: std.mem.Allocator) !void { const gzip = std.compress.v1.gzip; var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); // Compressor // Rename compress => compressor // Remove allocator var cmp = try gzip.compress(allocator, buf.writer(), .{}); _ = try cmp.write(data); try cmp.close(); // Rename to finisho cmp.deinit(); // Remove var fbs = std.io.fixedBufferStream(buf.items); // Decompressor // Rename decompress => decompressor // Remove allocator // Remove try var dcp = try gzip.decompress(allocator, fbs.reader()); defer dcp.deinit(); // Remove const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize)); defer allocator.free(plain); try std.testing.expectEqualSlices(u8, data, plain); } ```
2024-02-13 22:14:40 +01:00
2023-05-04 01:35:43 -04:00
2018-08-25 03:07:37 -04:00
2021-06-10 18:20:18 +01:00
2020-05-29 16:39:47 -04:00
2020-02-20 20:18:51 +01:00
2020-02-22 11:51:45 +01:00
2023-05-23 15:33:12 +03:30
2020-02-22 11:51:45 +01:00
2020-02-18 12:37:25 +01:00
2020-02-20 20:18:51 +01:00
2020-02-22 11:51:45 +01:00
2024-01-28 01:04:38 +02:00
2017-04-24 12:01:19 -04:00
2017-04-24 12:01:19 -04:00
2017-04-24 12:01:19 -04:00
2017-04-24 12:01:19 -04:00
2017-04-24 12:01:19 -04:00
2020-06-08 16:33:35 -04:00
2021-06-19 21:10:22 -04:00
2020-05-29 16:39:47 -04:00
2020-05-05 05:14:22 -06:00
2020-02-20 20:18:51 +01:00
2020-02-20 20:18:51 +01:00
2020-02-20 20:18:51 +01:00
2020-02-09 11:55:43 +01:00
2020-02-22 11:51:45 +01:00
2020-02-09 11:55:43 +01:00
2020-02-09 11:55:43 +01:00
2020-02-22 11:51:45 +01:00
2020-02-09 11:55:43 +01:00
2020-02-09 11:55:43 +01:00
2020-02-09 11:55:43 +01:00
2020-02-09 11:55:43 +01:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2020-02-09 11:55:43 +01:00
2020-02-22 11:51:45 +01:00
2020-02-11 09:40:21 +01:00
2020-02-11 09:40:21 +01:00
2020-02-18 12:37:25 +01:00
2024-01-28 01:04:38 +02:00
2020-02-18 12:37:25 +01:00
2020-02-11 09:40:21 +01:00
2020-02-18 12:37:25 +01:00
2020-02-20 20:48:54 +01:00
2020-02-11 09:40:21 +01:00
2020-02-22 11:51:45 +01:00
2020-02-18 12:37:25 +01:00
2020-02-22 11:51:45 +01:00
2020-02-10 13:18:28 +01:00
2020-02-22 12:44:21 +01:00
2020-02-10 13:18:28 +01:00
2020-02-20 20:48:54 +01:00
2020-02-10 13:18:28 +01:00
2020-02-22 11:51:45 +01:00
2020-02-18 12:37:25 +01:00
2020-02-18 12:37:25 +01:00
2020-02-09 11:55:43 +01:00
2021-06-19 21:10:22 -04:00
2020-02-18 12:37:25 +01:00
2020-02-18 12:37:25 +01:00
2020-02-18 12:37:25 +01:00
2020-02-18 12:37:25 +01:00
2020-02-18 12:37:25 +01:00
2020-02-18 12:37:25 +01:00
2020-02-18 12:37:25 +01:00
2020-02-20 20:48:54 +01:00
2020-02-20 20:18:51 +01:00
2020-02-18 12:37:25 +01:00
2020-02-09 11:55:43 +01:00
reimplement std.HashMap * breaking changes to the API. Some of the weird decisions from before are changed to what would be more expected. - `get` returns `?V`, use `getEntry` for the old API. - `put` returns `!void`, use `fetchPut` for the old API. * HashMap now has a comptime parameter of whether to store hashes with entries. AutoHashMap has heuristics on whether to set this parameter. For example, for integers, it is false, since equality checking is cheap, but for strings, it is true, since equality checking is probably expensive. * The implementation has a separate array for entry_index / distance_from_start_index. Entries no longer has holes; it is an ArrayList, and iteration is simpler and more cache coherent. This is inspired by Python's new dictionaries. * HashMap is separated into an "unmanaged" and a "managed" API. The unmanaged API is where the actual implementation is; the managed API wraps it and provides a more convenient API, storing the allocator. * Memory usage: When there are less than or equal to 8 entries, HashMap now incurs only a single pointer-size integer as overhead, opposed to using an ArrayList. * Since the entries array is separate from the indexes array, the holes in the indexes array take up less room than the holes in the entries array otherwise would. However the entries array also allocates additional capacity for appending into the array. * HashMap now maintains insertion order. Deletion performs a "swap remove". It's now possible to modify the HashMap while iterating.
2020-07-03 23:57:24 +00:00
2020-02-09 11:55:43 +01:00
2020-02-22 11:51:45 +01:00
2020-02-09 11:55:43 +01:00
2020-02-20 20:18:51 +01:00
2020-02-09 11:55:43 +01:00
2020-02-22 12:44:21 +01:00
2020-02-09 11:55:43 +01:00
2021-06-19 21:10:22 -04:00
2020-02-09 11:55:43 +01:00
2020-02-11 09:40:21 +01:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2024-01-28 01:04:38 +02:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2020-05-05 05:14:22 -06:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2020-02-22 11:51:45 +01:00
2019-05-26 13:17:34 -04:00
2020-02-10 13:18:28 +01:00
2020-02-22 12:05:50 +01:00
2020-02-22 12:05:50 +01:00
2020-02-22 12:05:50 +01:00
2020-02-22 12:05:50 +01:00
2020-02-22 12:05:50 +01:00
2020-02-22 12:05:50 +01:00
2020-02-22 12:05:50 +01:00
2020-02-22 12:05:50 +01:00
2020-02-22 12:05:50 +01:00
2020-02-09 11:55:43 +01:00
2020-02-21 12:11:04 +01:00
2020-02-21 12:11:04 +01:00
2020-02-21 12:11:04 +01:00
2020-02-09 11:55:43 +01:00
2016-12-18 19:40:26 -05:00
2023-05-04 01:35:43 -04:00
Fix simple doc mistakes. (#17624) * Add missing period in Stack's description This looks fine in the source, but looks bad when seen on the documentation website. * Correct documentation for attachSegfaultHandler() The description for attachSegfaultHandler() looks pretty bad without indicating that the stuff at the end is code * Added missing 'the's in Queue.put's documentation * Fixed several errors in Stack's documentation `push()` and `pop()` were not styled as code There was no period after `pop()`, which looks bad on the documentation. * Fix multiple problems in base64.zig Both "invalid"s in Base64.decoder were not capitalized. Missing period in documentation of Base64DecoderWithIgnore.calcSizeUpperBound. * Fix capitalization typos in bit_set.zig In DynamicBitSetUnmanaged.deinit's and DynamicBitSet.deinit's documentation, "deinitializes" was uncapitalized. * Fix typos in fifo.zig's documentation Added a previously missing period to the end of the first line of LinearFifo.writableSlice's documentation. Added missing periods to both lines of LinearFifo.pump's documentation. * Fix typos in fmt.bufPrint's documentation The starts of both lines were not capitalized. * Fix minor documentation problems in fs/file.zig Missing periods in documentation for Permissions.setReadOnly, PermissionsWindows.setReadOnly, MetadataUnix.created, MetadataLinux.created, and MetadataWindows.created. * Fix a glaring typo in enums.zig * Correct errors in fs.zig * Fixed documentation problems in hash_map.zig The added empty line in verify_context's documentation is needed, otherwise autodoc for some reason assumes that the list hasn't been terminated and continues reading off the rest of the documentation as if it were part of the second list item. * Added lines between consecutive URLs in http.zig Makes the documentation conform closer to what was intended. * Fix wrongfully ended sentence in Uri.zig * Handle wrongly entered comma in valgrind.zig. * Add missing periods in wasm.zig's documentation * Fix odd spacing in event/loop.zig * Add missing period in http/Headers.zig * Added missing period in io/limited_reader.zig This isn't in the documentation due to what I guess is a limitation of autodoc, but it's clearly supposed to be. If it was, it would look pretty bad. * Correct documentation in math/big/int.zig * Correct formatting in math/big/rational.zig * Create an actual link to ZIGNOR's paper. * Fixed grammatical issues in sort/block.zig This will not show up in the documentation currently. * Fix typo in hash_map.zig
2023-10-21 17:24:55 -04:00
2024-03-28 20:41:58 -04:00
std.debug: detect general protection faults on x86_64-linux ```zig const std = @import("std"); pub fn main() !void { var addr: *u8 = @ptrFromInt(0xaaaaaaaaaaaaaaaa); addr.* = 1; } ``` On x86_64-linux: Before: ``` $ zig run x.zig Segmentation fault at address 0x0 /home/wooster/Desktop/zig/x.zig:5:5: 0x21d887 in main (x) addr.* = 1; ^ /home/wooster/Desktop/zig-linux-x86_64/lib/std/start.zig:583:37: 0x21d847 in posixCallMainAndExit (x) const result = root.main() catch |err| { ^ /home/wooster/Desktop/zig-linux-x86_64/lib/std/start.zig:251:5: 0x21d371 in _start (x) asm volatile (switch (native_arch) { ^ ???:?:?: 0x0 in ??? (???) Aborted (core dumped) ``` After: ``` $ zig run x.zig --zig-lib-dir lib General protection exception /home/wooster/Desktop/zig/x.zig:5:5: 0x21d907 in main (x) addr.* = 1; ^ /home/wooster/Desktop/zig/lib/std/start.zig:583:37: 0x21d8c7 in posixCallMainAndExit (x) const result = root.main() catch |err| { ^ /home/wooster/Desktop/zig/lib/std/start.zig:251:5: 0x21d3f1 in _start (x) asm volatile (switch (native_arch) { ^ ???:?:?: 0x0 in ??? (???) Aborted (core dumped) ``` As @IntegratedQuantum pointed out in <https://github.com/ziglang/zig/issues/17745#issuecomment-1783815386>, it seems that if `code` of the `siginfo_t` instance is a certain value (128), you are able to distinguish between a general protection exception and a segmentation fault. This does not seem to be documented on `man sigaction`: ``` The following values can be placed in si_code for a SIGSEGV signal: SEGV_MAPERR Address not mapped to object. SEGV_ACCERR Invalid permissions for mapped object. SEGV_BNDERR (since Linux 3.19) Failed address bound checks. SEGV_PKUERR (since Linux 4.6) Access was denied by memory protection keys. See pkeys(7). The protection key which applied to this access is available via si_pkey. ``` (those constants are 1, 2, 3, and 4; none of them are the 128) I can't find a lot of documentation about this but it seems to work consistently for me on x86_64-linux. Here is a gist which provides additional evidence that this is a reliable way of checking for a general protection fault: https://gist.github.com/ytoshima/5682393 (read comment in first line) See also: https://stackoverflow.com/questions/64309366/why-is-the-segfault-address-null-when-accessing-memory-that-has-any-of-the-16-mo This only seems to affect x86_64 and on 32-bit x86 this does not seem to be a problem. Helps with #17745 but doesn't close it because the issue still exists on Windows and other POSIX OSs. I also limited this to x86_64-linux for now because that's the only platform where I tested it. Might work on more POSIX OSs.
2023-10-29 19:21:32 +01:00
std.debug: detect general protection faults on x86_64-linux ```zig const std = @import("std"); pub fn main() !void { var addr: *u8 = @ptrFromInt(0xaaaaaaaaaaaaaaaa); addr.* = 1; } ``` On x86_64-linux: Before: ``` $ zig run x.zig Segmentation fault at address 0x0 /home/wooster/Desktop/zig/x.zig:5:5: 0x21d887 in main (x) addr.* = 1; ^ /home/wooster/Desktop/zig-linux-x86_64/lib/std/start.zig:583:37: 0x21d847 in posixCallMainAndExit (x) const result = root.main() catch |err| { ^ /home/wooster/Desktop/zig-linux-x86_64/lib/std/start.zig:251:5: 0x21d371 in _start (x) asm volatile (switch (native_arch) { ^ ???:?:?: 0x0 in ??? (???) Aborted (core dumped) ``` After: ``` $ zig run x.zig --zig-lib-dir lib General protection exception /home/wooster/Desktop/zig/x.zig:5:5: 0x21d907 in main (x) addr.* = 1; ^ /home/wooster/Desktop/zig/lib/std/start.zig:583:37: 0x21d8c7 in posixCallMainAndExit (x) const result = root.main() catch |err| { ^ /home/wooster/Desktop/zig/lib/std/start.zig:251:5: 0x21d3f1 in _start (x) asm volatile (switch (native_arch) { ^ ???:?:?: 0x0 in ??? (???) Aborted (core dumped) ``` As @IntegratedQuantum pointed out in <https://github.com/ziglang/zig/issues/17745#issuecomment-1783815386>, it seems that if `code` of the `siginfo_t` instance is a certain value (128), you are able to distinguish between a general protection exception and a segmentation fault. This does not seem to be documented on `man sigaction`: ``` The following values can be placed in si_code for a SIGSEGV signal: SEGV_MAPERR Address not mapped to object. SEGV_ACCERR Invalid permissions for mapped object. SEGV_BNDERR (since Linux 3.19) Failed address bound checks. SEGV_PKUERR (since Linux 4.6) Access was denied by memory protection keys. See pkeys(7). The protection key which applied to this access is available via si_pkey. ``` (those constants are 1, 2, 3, and 4; none of them are the 128) I can't find a lot of documentation about this but it seems to work consistently for me on x86_64-linux. Here is a gist which provides additional evidence that this is a reliable way of checking for a general protection fault: https://gist.github.com/ytoshima/5682393 (read comment in first line) See also: https://stackoverflow.com/questions/64309366/why-is-the-segfault-address-null-when-accessing-memory-that-has-any-of-the-16-mo This only seems to affect x86_64 and on 32-bit x86 this does not seem to be a problem. Helps with #17745 but doesn't close it because the issue still exists on Windows and other POSIX OSs. I also limited this to x86_64-linux for now because that's the only platform where I tested it. Might work on more POSIX OSs.
2023-10-29 19:21:32 +01:00
improvements targeted at improving async functions * Reuse bytes of async function frames when non-async functions make `noasync` calls. This prevents explosive stack growth. * Zig now passes a stack size argument to the linker when linking ELF binaries. Linux ignores this value, but it is available as a program header called GNU_STACK. I prototyped some code that memory maps extra space to the stack using this program header, but there was still a problem when accessing stack memory very far down. Stack probing is needed or not working or something. I also prototyped using `@newStackCall` to call main and that does work around the issue but it also brings its own issues. That code is commented out for now in std/special/start.zig. I'm on a plane with no Internet, but I plan to consult with the musl community for advice when I get a chance. * Added `noasync` to a bunch of function calls in std.debug. It's very messy but it's a workaround that makes stack traces functional with evented I/O enabled. Eventually these will be cleaned up as the root bugs are found and fixed. Programs built in blocking mode are unaffected. * Lowered the default stack size of std.io.InStream (for the async version) to 1 MiB instead of 4. Until we figure out how to get choosing a stack size working (see 2nd bullet point above), 4 MiB tends to cause segfaults due to stack size running out, or usage of stack memory too far apart, or something like that. * Default thread stack size is bumped from 8 MiB to 16 to match the size we give for the main thread. It's planned to eventually remove this hard coded value and have Zig able to determine this value during semantic analysis, with call graph analysis and function pointer annotations and extern function annotations.
2019-09-11 20:22:49 -04:00
improvements targeted at improving async functions * Reuse bytes of async function frames when non-async functions make `noasync` calls. This prevents explosive stack growth. * Zig now passes a stack size argument to the linker when linking ELF binaries. Linux ignores this value, but it is available as a program header called GNU_STACK. I prototyped some code that memory maps extra space to the stack using this program header, but there was still a problem when accessing stack memory very far down. Stack probing is needed or not working or something. I also prototyped using `@newStackCall` to call main and that does work around the issue but it also brings its own issues. That code is commented out for now in std/special/start.zig. I'm on a plane with no Internet, but I plan to consult with the musl community for advice when I get a chance. * Added `noasync` to a bunch of function calls in std.debug. It's very messy but it's a workaround that makes stack traces functional with evented I/O enabled. Eventually these will be cleaned up as the root bugs are found and fixed. Programs built in blocking mode are unaffected. * Lowered the default stack size of std.io.InStream (for the async version) to 1 MiB instead of 4. Until we figure out how to get choosing a stack size working (see 2nd bullet point above), 4 MiB tends to cause segfaults due to stack size running out, or usage of stack memory too far apart, or something like that. * Default thread stack size is bumped from 8 MiB to 16 to match the size we give for the main thread. It's planned to eventually remove this hard coded value and have Zig able to determine this value during semantic analysis, with call graph analysis and function pointer annotations and extern function annotations.
2019-09-11 20:22:49 -04:00
2019-09-25 18:17:29 +03:00
2019-09-25 18:17:29 +03:00
improvements targeted at improving async functions * Reuse bytes of async function frames when non-async functions make `noasync` calls. This prevents explosive stack growth. * Zig now passes a stack size argument to the linker when linking ELF binaries. Linux ignores this value, but it is available as a program header called GNU_STACK. I prototyped some code that memory maps extra space to the stack using this program header, but there was still a problem when accessing stack memory very far down. Stack probing is needed or not working or something. I also prototyped using `@newStackCall` to call main and that does work around the issue but it also brings its own issues. That code is commented out for now in std/special/start.zig. I'm on a plane with no Internet, but I plan to consult with the musl community for advice when I get a chance. * Added `noasync` to a bunch of function calls in std.debug. It's very messy but it's a workaround that makes stack traces functional with evented I/O enabled. Eventually these will be cleaned up as the root bugs are found and fixed. Programs built in blocking mode are unaffected. * Lowered the default stack size of std.io.InStream (for the async version) to 1 MiB instead of 4. Until we figure out how to get choosing a stack size working (see 2nd bullet point above), 4 MiB tends to cause segfaults due to stack size running out, or usage of stack memory too far apart, or something like that. * Default thread stack size is bumped from 8 MiB to 16 to match the size we give for the main thread. It's planned to eventually remove this hard coded value and have Zig able to determine this value during semantic analysis, with call graph analysis and function pointer annotations and extern function annotations.
2019-09-11 20:22:49 -04:00
2022-04-21 10:14:23 +02:00
2022-04-21 10:14:23 +02:00
2022-04-21 10:14:23 +02:00
2022-04-21 10:14:23 +02:00