Commit graph

449 commits

Author SHA1 Message Date
Jacob Young
bf5ab54510 test: test with -fstrip and fix failures
Closes #17513
2023-12-01 04:34:50 +00:00
David Rubin
1e42a3de89
Remove all usages of std.mem.copy and remove std.mem.set (#18143) 2023-11-29 16:03:02 -05:00
tjog
22d7c7d295
std.debug: optimized printLineFromFileAnyOs (#18142)
* std.debug: optimized printLineFromFileAnyOs

Uses mem.indexOfScalar to speed line iteration instead of byte for byte.
Also prints the whole line in a single write (or up to a page size at a
time)

Closes #18099

* add test cases for printLineFromFileAnyOs
2023-11-29 14:58:56 -05:00
Michael Dusan
50bbb9d960 bsd: debitrot atomic and debug
closes #18119
2023-11-26 14:28:22 -05:00
Andrew Kelley
2bffd81015
Merge pull request #18085 from ziglang/std-atomics
rework std.atomic
2023-11-23 04:55:28 -05:00
Ryan Liptak
fcc071c9bd Remove unnecessary branch on absolute path
Also remove the TODO--it was added in fd067fbe8b, but the current code no longer used that workaround, so this TODO didn't seem relevant anymore.
2023-11-23 03:45:17 -05:00
Andrew Kelley
70931dbdea rework std.atomic
* move std.atomic.Atomic to std.atomic.Value
* fix incorrect argument order passed to testing.expectEqual
* make the functions be a thin wrapper over the atomic builtins and
  stick to the naming conventions.
* remove pointless functions loadUnchecked and storeUnchecked. Instead,
  name the field `raw` instead of `value` (which is redundant with the
  type name).
* simplify the tests by not passing every possible combination. Many
  cases were iterating over every possible combinations but then not
  even using the for loop element value!
* remove the redundant compile errors which are already implemented by
  the language itself.
* remove dead x86 inline assembly. this should be implemented in the
  language if at all.
2023-11-22 19:08:55 -07:00
Wooster
5d241a1478 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-11-21 14:22:11 +02:00
mlugg
51595d6b75
lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
Andrew Kelley
3fc6fc6812 std.builtin.Endian: make the tags lower case
Let's take this breaking change opportunity to fix the style of this
enum.
2023-10-31 21:37:35 -04:00
Jacob Young
d890e81761 mem: fix ub in writeInt
Use inline to vastly simplify the exposed API.  This allows a
comptime-known endian parameter to be propogated, making extra functions
for a specific endianness completely unnecessary.
2023-10-31 21:37:35 -04:00
Jacob Young
a440cf6d44 x86_64: fix c abi test failures 2023-10-27 23:31:20 -04:00
Jacob Young
b55377a5ab x86_64: pass more tests
* 128-bit integer multiplication with overflow
 * more instruction encodings used by std inline asm
 * implement the `try_ptr` air instruction
 * follow correct stack frame abi
 * enable full panic handler
 * enable stack traces
2023-10-25 04:28:30 -04:00
Jacob Young
27fe945a00 Revert "Revert "Merge pull request #17637 from jacobly0/x86_64-test-std""
This reverts commit 6f0198cadb.
2023-10-22 15:46:43 -04:00
Andrew Kelley
6f0198cadb Revert "Merge pull request #17637 from jacobly0/x86_64-test-std"
This reverts commit 0c99ba1eab, reversing
changes made to 5f92b070bf.

This caused a CI failure when it landed in master branch due to a
128-bit `@byteSwap` in std.mem.
2023-10-22 12:16:35 -07:00
Jacob Young
0c99ba1eab
Merge pull request #17637 from jacobly0/x86_64-test-std
x86_64: start to enable `test-std` and `test-compiler-rt` testing
2023-10-22 08:06:47 -04:00
JustinWayland
c45af2af61
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 21:24:55 +00:00
Jacob Young
32e85d44eb x86_64: disable failing tests, enable test-std testing 2023-10-21 10:55:41 -04:00
Stephen Gregoratto
285970982a Add illumos OS tag
- Adds `illumos` to the `Target.Os.Tag` enum. A new function,
  `isSolarish` has been added that returns true if the tag is either
  Solaris or Illumos. This matches the naming convention found in Rust's
  `libc` crate[1].
- Add the tag wherever `.solaris` is being checked against.
- Check for the C pre-processor macro `__illumos__` in CMake to set the
  proper target tuple. Illumos distros patch their compilers to have
  this in the "built-in" set (verified with `echo | cc -dM -E -`).

  Alternatively you could check the output of `uname -o`.

Right now, both Solaris and Illumos import from `c/solaris.zig`. In the
future it may be worth putting the shared ABI bits in a base file, and
mixing that in with specific `c/solaris.zig`/`c/illumos.zig` files.

[1]: 6e02a329a2/src/unix/solarish
2023-10-02 15:31:49 -06:00
mlugg
09a57583a4
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-23 22:01:08 +01:00
Andrew Kelley
5ae5dc507b
Merge pull request #16824 from mikdusan/bsd
de-bitrot the BSDs
2023-08-17 12:14:30 -07:00
Michael Dusan
e288c54699
std.debug: openbsd does not have getcontext 2023-08-17 10:55:39 -04:00
Michael Dusan
c685b675e5
std.debug: avoid os.getFdPath
Commit ea9917d9bd introduced usage
of fs.Dir.realpath which eventually calls os.getFdpath which is
forbidden to be used by the compiler. It causes building zig to fail on
OpenBsd, NetBSD and older versions of FreeBSD and DragonFly.

This patch substitutes with os.realpath on libc targets and eventually
calls c.realpath and allows zig to build. Any use of realpath is not
desired but this is the lesser evil.
2023-08-15 17:29:35 -04:00
kcbanner
8a5f331ec8 coff: handle the case of there being no PDB path 2023-08-15 10:20:11 -04:00
kcbanner
5b86180ae3 debug: support looking up debug symbols in both PDB and DWARF debug info, instead of only using DWARF if .debug_info is present
coff: support reading from memory loaded by the loader, or a mapped file
2023-08-15 10:20:11 -04:00
Zachary Raineri
49244dc0ca
std: remove some unused imports (#16710) 2023-08-06 15:18:50 -04:00
David Gonzalez Martin
9c05810be6 debug: expose module debug info deinitialization
Before this commit, you could use readElfDebugInfo independently with
one catch: the data is not freed since the deinitialization functions
for ModuleDebugInfo are private. This change makes them public so the
    users of such function and similar can free the memory after the
    debug symbols have been used.
2023-08-04 09:24:11 +02:00
kcbanner
78449b6d98 debug: skip unwind error printing on platforms that don't have_ucontext 2023-07-26 22:10:22 -04:00
kcbanner
a84826115f debug: print unwind errors if they occur on the first iteration, and differentiate between missing info and an actual unwind error in the message 2023-07-26 20:58:29 -04:00
kcbanner
b1d86db7b4 dwarf: move macho unwind code from macho -> dwarf
dwarf: fixup unchecked .eh_frame CIE offset subtraction
2023-07-20 22:58:16 -04:00
kcbanner
8e6a62ba10 test: disable omit_frame_pointer unwinding tests on aarch64-macos
dwarf: handle signal frame CIE flag
2023-07-20 22:58:16 -04:00
kcbanner
6d87bb370a debug: disable the new unwinder on aarch64-macos 2023-07-20 22:58:16 -04:00
kcbanner
97bda56306 macho: don't scan all eh_frame entries, instead follow the offset from the __unwind_info directly 2023-07-20 22:58:16 -04:00
kcbanner
774dc2fdb7 dwarf: add explicit_fde_offset to support more optimal __unwind_info dwarf lookups 2023-07-20 22:58:16 -04:00
kcbanner
2c76020e77 debug: load the macho unwind sections from the already-mapped image 2023-07-20 22:58:16 -04:00
kcbanner
618b0eb3d3 dwarf: fixup integer overflow in readEhPointer
debug: handle the possibility of eh_frame / debug_frame being mapped in memory or loaded from disk
2023-07-20 22:58:16 -04:00
kcbanner
5e399d97d7 use eh_frame from the mapped binary if available 2023-07-20 22:58:16 -04:00
kcbanner
9549b4acf6 debug: fixup an inconsistency in the getcontext implementation on aarch64-macos 2023-07-20 22:58:16 -04:00
kcbanner
b18031335a dwarf: use cie.return_address_register instead of assuming it's in the IP register 2023-07-20 22:58:16 -04:00
kcbanner
9b25bee42c debug: fixup have_getcontext 2023-07-20 22:58:15 -04:00
kcbanner
e5aa2bb224 debug: fixup last_error being printed too many times 2023-07-20 22:58:15 -04:00
kcbanner
891fa3b8b5 debug: fix initialization of the optional fields on StackIterator
dwarf: documentation fixups
target: enable unwind tables on macho
2023-07-20 22:58:15 -04:00
kcbanner
5dfb159e15 macho: add aarch64 implementation to unwindFrame
dwarf: map the V registers in abi.regBytes
test: add test case that exercises the stack-indirect __unwind_info mode in x86_64
2023-07-20 22:58:15 -04:00
kcbanner
203d96ae97 debug: add relocateContext
dwarf: fixup tests that used a ThreadContext
2023-07-20 22:58:15 -04:00
kcbanner
94354aa6aa macho: add unwindFrame which can unwind stack frames using the __unwind_info section
dwarf: fixup missing error
2023-07-20 22:58:15 -04:00
kcbanner
d226b74ae8 dwarf: add ExpressionError to work around the compiler not being able to infer it
dwarf: implement OP.entry_value, add tests
2023-07-20 22:58:15 -04:00
kcbanner
21d0154139 dwarf: skip register tests on unimplemented arch / os, add tests for type convesions
debug: dupeContext -> copyContext
2023-07-20 22:58:15 -04:00
kcbanner
54ca62fef4 dwarf: fixup regBytes for the case where there is no context support
expressions: add more tests, fix tests for mipsel
debug: add lookupModuleName implementation for macos
2023-07-20 22:58:15 -04:00
kcbanner
5c0d4cef1a debug: add dupeContext, store a pointer to a copy of ThreadContext on UnwindContext 2023-07-20 22:58:15 -04:00
kcbanner
5f72c6508d debug: rename StackTraceContext to ThreadContext
dwarf: use ThreadContext instead of os.ucontext_t
dwarf: add regBytes impl for windows
dwarf: fixup expression types for non-native
2023-07-20 22:58:15 -04:00