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

644 commits

Author SHA1 Message Date
Jacob Young
ad3cb85c05 test/stack_traces: convert to new runner 2026-11-02 03:28:06 -05:00
B. Crist
72c7ff4b8e std.debug: Support FP unwinding on thumb-* targets (#37482)
When compiling Arm Thumb code, GCC and LLVM ([including zig](https://godbolt.org/z/sfxd37P3x)) use R7 as the frame pointer (except when `-fomit-frame-pointer` is specified, of course) rather than R11 which is used in 32-bit mode. This is mainly due to Thumb1's `PUSH`/`POP` instructions only supporting R0-R7 and R14/R15.  Armv7-M does have Thumb2 `PUSH.W`/`POP.W` which can do it, but LLVM still uses R7, even if you explicitly use e.g. `-mcpu cortex_m3`. Note that AAPCS32 doesn't mention using R7 as FP in Thumb mode at all, but it also only barely mentions using R11 as FP: "In some variants r11 (FP) may be used as a frame pointer in order to chain frame activation records into a linked list."

`std.debug.cpu_context.Arm` currently always assumes R11 is the frame pointer for both `arm` and `thumb` targets, which means FP unwinding simply doesn't work on thumb targets, including most arm microcontrollers.  It would be nice to have stack tracing available through the FP on these constrained platforms since you often don't have enough ROM space to waste on the `.debug_frame` section, and [`.ARM.exidx`/`.ARM.extab` unwinding](https://codeberg.org/ziglang/zig/issues/30931) isn't implemented yet.

On some platforms, it's possible to have both thumb and arm functions interleaved within a single call stack.  In these cases, [compilers use R11 for the FP in arm functions, and R7 in thumb functions](https://godbolt.org/z/EsaYW8rhh), so neither blindly using R11 nor R7 will guarantee a correct stack unwinding in the general case.  The most reliable way to determine which register is the FP for the currently executing stack frame would be by reading `CPSR` using the `MRS` instruction and checking the `T` bit, but since zig currently doesn't have an equivalent to `__attribute__((target("thumb")))` that can be applied on a per-function basis, the thumb-ness of all non-extern functions will always match `@import("builtin").target.arch.isThumb()`.

For deeper stack frames, it doesn't matter what register the FP was stored in, since when we unwind we're getting it from the stack instead.  As long as all functions were compiled with `-fno-omit-frame-pointer`, the FP will always have been pushed immediately after the LR.

So the first change is to have `std.debug.cpu_context.Arm.getFp()` use R7 when `native_arch` is thumb.

The current inline assembly for grabbing the CPU context mentions only using `STM` for R0-R12 for thumb compatibility, but it still uses several 32-bit Thumb2 instructions that don't exist on architectures like Cortex M0/M0+.  This PR adds a slightly more verbose alternative version that only uses 16-bit instructions if the target doesn't support Thumb2. I thought about skipping R8-R12 entirely on targets that only support Thumb1, since it's highly unlikely that they'll actually be used for anything, but for now they are still being captured.

With these two changes, and appropriate `debug` declarations in the root module to enable symbol lookup using a custom minified string table tacked onto the end of the firmware blob, I can get stack traces on an RP2040 microcontroller (Cortex M0+):
```
src/main.zig:135:37: 0x10000b5a in dumpCurrentStack (firmware)
src/main.zig:131:5: 0x10000b50 in iAmAFunctionYesIAm (firmware)
src/main.zig💯21: 0x10000a7c in main (firmware)
$rpi/src/rp2040/boot.zig:112:16: 0x10000310 in _start (firmware)
$rpi/src/rp2040/boot.zig:31:11: 0x100002b6 in _boot3 (firmware)
```

But there's still one more problem: all of the reported addresses point to the instruction immediately following the `BL`/`BLX` instruction that we would expect.  For example, here's the disassembly for the top two functions:

```
main.iAmAFunctionYesIAm:
10000b48  80b5       push    {r7, lr}
10000b4a  00af       add     r7, sp, #0
10000b4c  00f001f8   bl      #main.dumpCurrentStack
10000b50  80bd       pop     {r7, pc}

main.dumpCurrentStack:
10000b52  80b5       push    {r7, lr}
10000b54  00af       add     r7, sp, #0
10000b56  00f001f8   bl      #debug.writeCurrentStackTrace
10000b5a  80bd       pop     {r7, pc}
```

In this case, none of the resolved symbols or source locations are affected by the incorrect instruction addresses, but if the next instruction after the subroutine happened to be an inlined function, they could be erroneous.  We need to make sure that the thumb bit is cleared so that when `std.debug.StackIterator` subtracts 1 from the return address, it ends up in the previous call instruction.  I put this in `std.debug.stripInstructionPtrAuthCode` and renamed it to `std.debug.stripInstructionPtrMarkerBits` since I believe everywhere that's currently used should have the thumb bit stripped as well (or at least it shouldn't harm anything to do so).

With all of the changes, my RP2040 test now looks correct:
```
src/main.zig:135:37: 0x10000b59 in dumpCurrentStack (firmware)
src/main.zig:131:5: 0x10000b4f in iAmAFunctionYesIAm (firmware)
src/main.zig💯21: 0x10000a7b in main (firmware)
$rpi/src/rp2040/boot.zig:112:16: 0x1000030f in _start (firmware)
$rpi/src/rp2040/boot.zig:31:11: 0x100002b5 in _boot3 (firmware)
```

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37482
Reviewed-by: Alex Rønne Petersen <alex@alexrp.com>
2026-10-31 12:49:51 +01:00
Andrew Kelley
def373ed78 Merge pull request 'llvm: pass some behavior tests (30%) for spirv target' (#37300) from alichraghi/zig:llvm-spirv into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37300
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-10-24 22:34:19 +02:00
Ali Cheraghi
b60a4af44c llvm: fix blocking issues to get tests running 2026-10-23 09:18:03 +03:30
CMDRZero
64e4f9db8f Implement partially ordered operator precedence 2026-10-22 12:21:43 +02:00
Jacob Young
f08a8f0fc2 main: reimplement compiler debug flags
* Add `.llvmbc`/`.llvmir` to `ObjectFormat` for emitting unoptimized IR
* Replace `--verbose-air`, `--verbose-intern-pool`,
  `--verbose-generic-instances`, `--verbose-llvm-ir`, and
  `--verbose-llvm-bc` with various `--debug-log` scopes
  * `--debug-log illegal-air`: pre-legalized Air
  * `--debug-log legal-air`: post-legalized Air
  * `--debug-log air`: final Air with liveness
  * `--debug-log ip-stats`: InternPool statistics
  * `--debug-log ip-values`: InternPool values
  * `--debug-log ip-generic-instances`: InternPool generic instances
  * `--debug-log llvm-ir`: pre-optimized LLVM IR
  * `--debug-log opt-llvm-ir`: post-optimized LLVM IR
* Delete `--verbose-cimport` which does nothing
* Delete `--verbose-llvm-cpu-features` which is equivalent to looking at
  the outputs of `--show-builtin` and `--debug-log llvm-ir`
2026-10-21 10:04:01 -07:00
Andrew Kelley
6b2ac8fa5c std.debug.assert: remove @disableInstrumentation
This was included as part of 5d58306162
but there was no mention of it in the commit message and I didn't notice
it in the review.

This is a partial (one-line) revert. Let's discuss this change carefully
and separately.
2026-10-09 17:46:28 -07:00
Alex Rønne Petersen
0375ec45ab
std.debug: remove sys_can_stack_trace prong for avr
https://github.com/llvm/llvm-project/pull/228967

closes https://codeberg.org/ziglang/zig/issues/31127
2026-10-07 06:48:23 +02:00
Jay Petacat
d7148f7d05 std.debug.SafetyLock: add support for shared locking
The existing `lock` and `unlock` methods continue to act exclusively.
They should be used when data may be mutated. New methods, `lockShared`
and `unlockShared`, may be used for shared locking in situations where
multiple independent users are reading but not mutating data.
2026-09-09 02:18:52 +02:00
Alex Rønne Petersen
fb1e767ff8
std.debug: apply stack bias to the frame pointer fetched from the CPU context
Unbreaks segfault stack tracing on SPARC.
2026-08-28 14:16:50 +02:00
Alex Rønne Petersen
9ae517714e
std.debug: remove SPARC workaround in handleSegfaultPosix()
This no longer appears to be needed.
2026-08-28 14:16:16 +02:00
marximimus
5a625d5f3c fix memory leak in StackIterator
partial fix for #36533
2026-08-28 09:45:15 +02:00
Andrew Kelley
c6204ba3c2 Merge pull request 'spork8 backend' (#36627) from spork8 into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36627
2026-08-27 21:18:14 +02:00
Kleshzz
c3723c05f0 std.Target: add gba os tag 2026-08-27 14:35:44 +02:00
Andrew Kelley
03cc522fc0 spork8: object format is .bin extension (raw machine code) 2026-08-26 20:54:49 -07:00
Andrew Kelley
3cd05f8a1b init spork8 backend 2026-08-26 20:54:49 -07:00
xtex
4605f7fbbf
std: support loongarch backend 2026-08-09 11:10:26 +08:00
Meghan Denny
e88566253d sema: improve @errorCast safety check message 2026-08-05 03:29:37 +02:00
Matthew Lugg
5f74e4f3f8 Sema: make loading uninstantiable types a runtime safety panic
For generic code, it is far more useful to consider this equivalent to
`unreachable`. Meanwhile, it is difficult to accidentally write concrete
code which performs this operation, so very little is actually lost from
not having a compile error.

If we accept that this operation does not trigger a compile error, then
it already has the potential to invoke Illegal Behavior today, because
uninstantiable types have an unspecified size in memory, so the
dereference always potentially exceeds the pointer's provenance. (That
said, this only means it is *possible* for the operation to invoke IB,
so the langspec should nonetheless explicitly specify that dereferencing
a pointer to an uninstantiable type is itself Illegal Behavior.)

Storing uninstantiable types into memory does not require any specific
language rules, because that operation can never be reached anyway due
to it requiring an operand (the RHS of `a = b`) whose type is the store
type, which is (by definition) impossible for uninstantiable types.

Resolves: https://codeberg.org/ziglang/zig/issues/36247
2026-08-04 22:29:50 +02:00
Andrew Kelley
f2c2ee28cb std.lang.Optimize: provide alternative to std.debug.runtime_safety 2026-07-20 17:26:50 -07:00
Andrew Kelley
ae4e835a66 std.lang: rename OptimizeMode to Optimize
and remove "release" from the enum tag names.

no functional change, however, despite the addition of
backwards-compatibile declarations in this patch, it is breaking because
expressions that use `==` or `!=` operators will not able to use the
deprecated names.

I predict these names to survive until the 1.0 tag without any more
breakage.
2026-07-20 17:26:50 -07:00
Justus Klausecker
69a26976ed zig fmt
Converts usages of `@intFromEnum` and `@enumFromInt` to instead use the
new `@backingInt` and `@fromBackingInt` builtins.
2026-07-17 23:30:26 +02:00
Andrew Kelley
de7fdfecd8 Merge pull request 'compiler: implement @divCeil builtin' (#36043) from pavelverigo/zig:div-ceil into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36043
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-07-08 21:15:24 +02:00
Alex Rønne Petersen
164578636c
std.Target: add switch (Nintendo Switch) Os tag
contributes to https://codeberg.org/ziglang/zig/issues/31341
2026-07-07 15:34:40 +02:00
Pavel Verigo
efcaf6a72d lib: use @divCeil 2026-07-06 20:49:09 +02:00
Pavel Verigo
ba5420896d Enable std tests for wasm backend again + enable field reordering
Additionally remove some skips, closes #30575
2026-06-29 13:22:20 +02:00
Leon Lombar
84f84267c7 std.Target: add PSX
Adds target info for the PSX. Builds fine with the following:
```zig
const target = b.resolveTargetQuery(.{
    .os_tag = .psx,
    .cpu_arch = .mipsel,
});
```
the only "problem" being that it spits out an error from LLVM even
though generating an object file succeeds:
```sh
❯ zig build
install
└─ install generated to main.o
   └─ compile obj obj Debug mipsel-psx failure
error: warning: MIPS-I support is experimental
```
2026-06-19 04:22:55 +02:00
Jari Vetoniemi
7255f3e724 std.debug: add TargetInfo function which returns the default SelfInfo
If one overrides `SelfInfo` in their `root.debug`, it is no longer possible
to refer to the default `SelfInfo` that the std.debug uses.

This allows the developer to still refer to the std's default without
copying the SelfInfo zig files to their project.
2026-06-17 23:17:01 +02:00
ARandomOSDever
eedf7d9299
std.Target: add wiiu OS tag 2026-06-17 22:45:00 +02:00
Lukas Lalinsky
041ae87b98 Avoid error.Canceled swallowing in `std.debug.* 2026-06-11 06:05:18 +02:00
Alex Rønne Petersen
68d2bef8c0
std.debug: FP unwinding is useless on xtensa
Like MIPS, we'd have to do backwards code scanning for it to be useful, and at
least for now, we're not willing to do that.
2026-05-26 08:11:04 +02:00
Alex Rønne Petersen
6e6df11027
std.debug: trap on psp in defaultPanic() 2026-05-23 05:47:34 +02:00
Andrew Kelley
3d56df1716 Merge pull request 'std.fmt, std.mem.Allocator: Remove bufPrintZ()/dupeZ() in favor of bufPrintSentinel()/dupeSentinel()' (#35190) from linus/zig:deprecated-std-fmt-mem into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35190
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-05-06 19:34:52 +02:00
Alex Rønne Petersen
994547d19a
std.debug: disable stack traces on avr
https://codeberg.org/ziglang/zig/issues/31127
2026-05-06 16:03:56 +02:00
Linus Groh
fcc0a5a913 std.mem.Allocator: Remove dupeZ() in favor of dupeSentinel() 2026-05-03 21:42:16 +01:00
Matthew Lugg
fdac89d6cd
remove uses of array multiplication
In preparation for its removal as accepted in
https://github.com/ziglang/zig/issues/24738.
2026-04-30 08:57:51 +01:00
Mason Remaley
e2c3920fb1 Renames buffer first allocator in compiler and std 2026-04-18 14:51:49 -07:00
Elaine Gibson
db63e4725a std.debug: fix handleSegfaultPosix for haiku 2026-04-14 19:10:21 +01:00
Mason Remaley
6707a5efee Arena allocates text 2026-04-13 01:30:14 -07:00
Mason Remaley
4ad665d3c8 Writes symbols to array list argument 2026-04-12 23:50:24 -07:00
Mason Remaley
e968e6d004 Fixes typos/out of date comments, switches to unstable sort 2026-04-12 14:09:22 -07:00
Mason Remaley
df2413cf69 Removes dead code, updates some tests, fixes typos in comments, formats 2026-04-12 04:01:30 -07:00
Mason Remaley
541bd6c369 Updates more failing tests 2026-04-12 04:01:30 -07:00
Mason Remaley
ac207073f3 Reverts renaming of builtin.StackTrace -> ErrorReturnTrace
We can defer this change until the next time zig1 needs to be updated
2026-04-12 04:01:30 -07:00
Mason Remaley
4efbb27aa2 Don't bother resolving symbol names that won't be used
Also fixes some memory management issues
2026-04-12 04:01:30 -07:00
Mason Remaley
f6a3a0ca72 Replaces the inline symbol iterator with an array of symbols
The intention behind the iterator was to avoid needing to allocate the
symbols, but in practice we need to allocate them anyway since we need
to reverse their order and don't have random access. The alternative
would be an N^2 algorithm.

In practice this isn't that bad, because even if the allocation fails,
we'll still end up printing the address, so the user still ends up with
the necessary information to reconstruct the crash. I don't think it's
worth it to try to set up some kind of ring buffer or return partial
results on failure, but may revisit this.
2026-04-12 04:01:30 -07:00
Mason Remaley
c2cbb944ba Further improvements to stack trace type 2026-04-12 04:01:29 -07:00
Mason Remaley
6bf583c4ba Further separation of stack trace and error return trace 2026-04-12 04:01:29 -07:00
Mason Remaley
94ff38af87 Separates error return traces from stack traces
Doesn't commit the changes to stage1, we can generate those at the end
once we're not making any more changes to it to avoid wasting storage.
2026-04-12 04:01:29 -07:00
Mason Remaley
094e841f09 Don't print column if column info is missing 2026-04-12 04:01:29 -07:00