Commit Graph

47 Commits

Author SHA1 Message Date
dweiller
c4cff443b8 test/stack_traces.zig: add err union switch case 2024-01-18 15:10:32 +11:00
Andrew Kelley
c8c32a0569 update stack trace tests to account for new defaults
ReleaseSafe optimization mode now defaults error tracing to false.
2024-01-01 19:49:07 -07:00
yujiri8
68d2f68ed8
zig fmt: fix extra whitespace with multiline strings
Fixes #13937
2022-12-17 00:24:58 +02:00
Cody Tapscott
3fa226a80c AstGen: Pop error trace for continue
PR #12837 handled control flow for break and return, but I forgot
about `continue`. This is effectively another break, so we just
need another `.restore_err_ret_index` ZIR instruction.

Resolves #13618.
2022-11-22 14:00:41 +02:00
Cody Tapscott
d060cbbec7 stage2: Keep error return traces alive when storing to const
This change extends the "lifetime" of the error return trace associated
with an error to continue throughout the block of a `const` variable
that it is assigned to.

This is necessary to support patterns like this one in test_runner.zig:
```zig
const result = foo();
if (result) |_| {
    // ... success logic
} else |err| {
    // `foo()` should be included in the error trace here
    return error.TestFailed;
}
```

To make this happen, the majority of the error return trace popping logic
needed to move into Sema, since `const x = foo();` cannot be examined
syntactically to determine whether it modifies the error return trace. We
also have to make sure not to delete pertinent block information before it
makes it to Sema, so that Sema can pop/restore around blocks correctly.

* Why do this only for `const` and not `var`? *

There is room to relax things for `var`, but only a little bit. We could
do the same thing we do for const and keep the error trace alive for the
remainder of the block where the *assignment* happens. Any wider scope
would violate the stack discipline for traces, so it's not viable.

In the end, I decided the most consistent behavior for the user is just
to kill all error return traces assigned to a mutable `var`.
2022-10-21 12:40:29 -07:00
Cody Tapscott
b529d8e48f stage2: Propagate error return trace into fn call
This change extends the "lifetime" of the error return trace associated
with an error to include the duration of a function call it is passed
to.

This means that if a function returns an error, its return trace will
include the error return trace for any error inputs. This is needed to
support `testing.expectError` and similar functions.

If a function returns a non-error, we have to clean up any error return
traces created by error-able call arguments.
2022-10-21 11:22:49 -07:00
Cody Tapscott
3007fdde45 stage2: Pop error trace when storing error to var/const
In order to enforce a strict stack discipline for error return traces,
we cannot track error return traces that are stored in variables:

  ```zig
  const x = errorable(); // errorable()'s error return trace is killed here

  // v-- error trace starts here instead
  return x catch error.UnknownError;
  ```

In order to propagate error return traces, function calls need to be passed
directly to an error-handling expression (`if`, `catch`, `try` or `return`):

  ```zig
  // When passed directly to `catch`, the return trace is propagated
  return errorable() catch error.UnknownError;

  // Using a break also works
  return blk: {
      // code here
      break :blk errorable();
  } catch error.UnknownError;
  ```

Why do we need this restriction? Without it, multiple errors can co-exist
with their own error traces. Handling that situation correctly means either:
  a. Dynamically allocating trace memory and tracking lifetimes, OR
  b. Allowing the production of one error to interfere with the trace of another
     (which is the current status quo)

This is piece (3/3) of https://github.com/ziglang/zig/issues/1923#issuecomment-1218495574
2022-10-21 10:44:20 -07:00
Cody Tapscott
0c3a50fe1c stage2: Do not pop error trace if result is an error
This allows for errors to be "re-thrown" by yielding any error as the
result of a catch block. For example:

```zig
fn errorable() !void {
    return error.FallingOutOfPlane;
}

fn foo(have_parachute: bool) !void {
    return errorable() catch |err| b: {
        if (have_parachute) {
            // error trace will include the call to errorable()
            break :b error.NoParachute;
        } else {
            return;
        }
    };
}

pub fn main() !void {
    // Anything that returns a non-error does not pollute the error trace.
    try foo(true);

    // This error trace will still include errorable(), whose error was "re-thrown" by foo()
    try foo(false);
}
```

This is piece (2/3) of https://github.com/ziglang/zig/issues/1923#issuecomment-1218495574
2022-10-21 10:44:19 -07:00
Cody Tapscott
eda3eb1561 stage2: "Pop" error trace for break/return within catch
This implement trace "popping" for correctly handled errors within
`catch { ... }` and `else { ... }` blocks.

When breaking from these blocks with any non-error, we pop the error
trace frames corresponding to the operand. When breaking with an error,
we preserve the frames so that error traces "chain" together as usual.

```zig
fn foo(cond1: bool, cond2: bool) !void {
    bar() catch {
    	if (cond1) {
	    // If baz() result is a non-error, pop the error trace frames from bar()
	    // If baz() result is an error, leave the bar() frames on the error trace
            return baz();
	} else if (cond2) {
	    // If we break/return an error, then leave the error frames from bar() on the error trace
	    return error.Foo;
	}
    };

    // An error returned from here does not include bar()'s error frames in the trace
    return error.Bar;
}
```

Notice that if foo() does not return an error it, it leaves no extra
frames on the error trace.

This is piece (1/3) of https://github.com/ziglang/zig/issues/1923#issuecomment-1218495574
2022-10-21 10:43:42 -07:00
Andrew Kelley
80b8294bcc test-stack-traces: restore test coverage
reverts 1ce71c86bf
2022-08-24 12:57:31 -07:00
Andrew Kelley
1ce71c86bf std.debug: implement support for DWARFv5 2022-08-23 00:28:45 -07:00
Jakub Konka
070282a96e libstd: fix off-by-one error in def of ProcSym in pdb
Make sure `ProcSym` includes a single element byte-array which delimits
the start of the symbol's name as part of its definition. This makes
the code more elegant in that accessing the name is equivalent to taking
the address of this one element array.
2022-08-17 16:36:02 -04:00
Andrew Kelley
5e42b0821a disable failing stack traces tests on windows
See tracking issue #12422
2022-08-11 19:19:23 -07:00
Andrew Kelley
feb90f6ed4 AstGen: emit debug stmt for try
This improves the following test case:

```zig
pub fn main() !void {
    try foo();
}

fn foo() !void {
    return error.Bad;
}
```

The error return trace now points to the `try` token instead of pointing
to the foo() function call, matching stage1.

Closes #12308.
2022-08-08 21:12:04 -07:00
Michael Dusan
0f26120377 overhaul elf csu (c-runtime startup) logic
- more support for linux, android, freebsd, netbsd, openbsd, dragonfly
- centralize musl utils; musl logic is no longer intertwined with csu
- fix musl compilation to build crti/crtn for full archs list
- fix openbsd to support `zig build-lib -dynamic`
- initial dragonfly linking success (with a warning)

ancillary:

- fix emutls (openbsd) tests to use `try`
2021-05-23 15:38:57 -04:00
Veikka Tuominen
0a38f61d58 update usage of std.testing in behavior and standalone tests 2021-05-08 15:15:30 +03:00
Michael Dusan
d99dc21b9f test: overhaul stack_trace testing
- limit expected-output to main source file;
  ie. tolerate changes to start.zig
- when mode != .Debug the function name is now symbolically represented;
  ie. tolerate changes in llvm optimizer effects on the callstack
- cleanup how test cases are specified
- add test case predicates for excluding by arch, os or custom fn
2021-04-10 23:20:06 -04:00
Andrew Kelley
6d616d8257 update stack trace test cases to new start.zig line offsets 2021-04-08 21:56:19 -07:00
Andrew Kelley
53987c932c 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.
2020-12-18 15:54:01 -07:00
Andrew Kelley
d9368d7012 update test-stack-traces because start.zig updated 2020-12-18 12:22:46 -07:00
Andrew Kelley
500fbdad57 update stack trace test with new start.zig line number 2020-11-25 00:40:50 -07:00
LemonBoy
bfa7e5c743 Update stack_traces test 2020-11-23 12:36:03 +01:00
Koakuma
3a58b2330c Update stack traces testcases 2020-10-28 17:46:26 +07:00
Andrew Kelley
392e6da8a3 update stack trace test case 2020-10-22 21:57:30 -07:00
LemonBoy
39e34081ca Update the stack-traces tests 2020-10-19 20:09:43 +02:00
Andrew Kelley
0011def2b2 fix compilation error when building with io_mode evented
The merge of #5613 introduced a regression when building with io_mode
evented, fixed in this commit.

closes #6715
2020-10-17 15:46:36 -07:00
Andrew Kelley
d87bd3d8af fixups regarding windows wide strings
* remove GetModuleHandleA from kernel32.zig. use of A functions
   considered harmful.
 * make it a compile error to expose WinMain instead of wWinMain. same
   thing.
 * start code declares wWinMainCRTStartup instead of WinMainCRTStartup
   when it has the choice.
2020-10-15 19:37:55 -07:00
Andrew Kelley
c7c38e7279 Merge branch '5002-fix-entrypoint-with-winmain' of https://github.com/AnthonyYoManz/zig into AnthonyYoManz-5002-fix-entrypoint-with-winmain 2020-10-15 18:22:12 -07:00
Vignesh Rajagopalan
2ab0c7391a Rename .macosx to .macos 2020-10-12 18:56:25 -04:00
Andrew Kelley
800c5de2ae update the stack trace test case for lines added to start.zig 2020-09-10 14:40:33 -07:00
Andrew Kelley
4a69b11e74 add license header to all std lib files
add SPDX license identifier
copyright ownership is zig contributors
2020-08-20 16:07:04 -04:00
DixiE
68fe3e116d Update Stack Trace For start.zig Changes 2020-06-15 23:33:58 +01:00
Tadeo Kondrak
84a0a9688c
update docs/tests for async/extern fn removal 2020-05-05 10:31:32 -06:00
LemonBoy
d788b0cd8b std: Minor changes to TLS handling
* Always allocate an info block per-thread so that libc can store
  important stuff there.
* Respect ABI-mandated alignment in more places.
* Nicer code, use slices/pointers instead of raw addresses whenever
  possible.
2020-03-28 11:20:38 -04:00
Andrew Kelley
3bded9cf29
disable failing stack trace test for aarch64 2020-03-22 18:54:19 -04:00
Andrew Kelley
87b8b69eb0
update stack trace test expected output 2020-03-22 17:07:24 -04:00
Andrew Kelley
75bda408cd
update stack traces test expectations 2020-03-13 17:50:29 -04:00
Andrew Kelley
d1cb16aace
Merge remote-tracking branch 'origin/master' into llvm10 2020-03-03 09:44:13 -05:00
Andrew Kelley
0912484c4f
improve the "external executor" detection logic 2020-02-28 14:51:54 -05:00
Andrew Kelley
87b9e744dd
update std lib to new Target API 2020-02-28 14:51:54 -05:00
Andrew Kelley
e4180de2e9
update test expectations 2020-02-26 11:59:23 -05:00
LemonBoy
08047cd6d7 correct test expectations 2020-02-23 22:47:48 +01:00
LemonBoy
8d6536b50c codegen: Use the new frame-pointer fn attributes
no-frame-pointer-elim and no-frame-pointer-elim-non-leaf have been
deprecated for a while in favour of the newer (and clearer)
frame-pointer attribute.

Starting with LLVM10 the old attributes are silently ignored, leading to
no stack traces in debug mode.
2020-02-07 17:08:21 +01:00
LemonBoy
b8601e9252 Adjust tests & work around a nasty ICE 2020-01-21 23:17:02 +01:00
Andrew Kelley
1e4bae6692
update stack traces tests 2019-12-12 19:43:04 -05:00
Michael Dusan
0d9a78a852 test-stack-traces: add FreeBSD 2019-09-09 00:25:21 -04:00
Andrew Kelley
aba67ecf44
rename test-compare-panic to test-stack-traces 2019-09-03 10:08:39 -04:00