ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.09k stars 2.49k forks source link

Panic: reached unreachable code under aarch64 Ampere A1 cpu (Oracle Linux 8) #20038

Open hashfall opened 4 months ago

hashfall commented 4 months ago

Zig Version

0.13.0-dev.242+6635360db

Steps to Reproduce and Observed Behavior

Zig doesn't support Ampere A1 cpu from OCI cloud machines, trying to run a pre-built binary results in the errors below. Trying to either bootstrap or to build from source without LLVM support, results in the missing "id_aa64mmfr2_el1" cpu instruction error.

Result of zig env command (using a pre-built aarch64 binary)

{
 "zig_exe": "/home/opc/bin/zig/zig",
 "lib_dir": "bin/zig/lib",
 "std_dir": "bin/zig/lib/std",
 "global_cache_dir": "/home/opc/.cache/zig",
 "version": "0.13.0-dev.242+6635360db",
 "target": "aarch64-linux.5.4.17...5.4.17-gnu.2.28",
 "env": {
  "ZIG_GLOBAL_CACHE_DIR": null,
  "ZIG_LOCAL_CACHE_DIR": null,
  "ZIG_LIB_DIR": null,
  "ZIG_LIBC": null,
  "ZIG_BUILD_RUNNER": null,
  "ZIG_VERBOSE_LINK": null,
  "ZIG_VERBOSE_CC": null,
  "ZIG_BTRFS_WORKAROUND": null,
  "ZIG_DEBUG_CMD": null,
  "CC": null,
  "NO_COLOR": null,
  "XDG_CACHE_HOME": null,
  "HOME": "/home/opc"
 }
}

Trying to run zls

info : ( main ): Starting ZLS 0.12.0 @ 'zls'
thread 140051 panic: reached unreachable code
Panicked during a panic. Aborting.
[1]    140051 abort (core dumped)  zls

Trying to build the ziglings.org exercises (using a pre-built aarch64 binary). But doing "zig run" individual .zig files works ok

thread 141737 panic: reached unreachable code
Panicked during a panic. Aborting.
error: the following build command crashed:
/home/opc/code-server/config/workspace/ziglings.org/zig-cache/o/639179193b9afe87500327e8ffcdee86/build /home/opc/bin/zig/zig /home/opc/code-server/config/workspace/ziglings.org /home/opc/code-server/config/workspace/ziglings.org/zig-cache /home/opc/.cache/zig --seed 0xf276afdb -Z0a11a10068cb4e45

Expected Behavior

Be able to build projects using zig language under an Arm Ampere based machine.

mikdusan commented 4 months ago

if it doesn't crash, can you paste output of zig detect-cpu

hashfall commented 4 months ago

if it doesn't crash, can you paste output of zig detect-cpu

This is the output of the command:

neoverse-n1
-a510
-a520
-a65
-a710
-a720
-a76
-a78
-a78c
-addr-lsl-fast
+aes
-aggressive-fma
-alternate-sextload-cvt-f32-pattern
-altnzcv
-alu-lsl-fast
-am
-amvs
-arith-bcc-fusion
-arith-cbz-fusion
-ascend-store-address
-b16b16
-balance-fp-ops
-bf16
-brbe
-bti
-call-saved-x10
-call-saved-x11
-call-saved-x12
-call-saved-x13
-call-saved-x14
-call-saved-x15
-call-saved-x18
-call-saved-x8
-call-saved-x9
-ccdp
-ccidx
+ccpp
-chk
-clrbhb
-cmp-bcc-fusion
-complxnum
-CONTEXTIDREL2
-cortex-r82
-cpa
+crc
-crypto
-cssc
-d128
-disable-latency-sched-heuristic
-disable-ldp
-disable-stp
-dit
+dotprod
-ecv
-el2vmsa
-el3
-enable-select-opt
-ete
-exynos-cheap-as-move
-f32mm
-f64mm
-faminmax
-fgt
-fix-cortex-a53-835769
-flagm
-fmv
-force-32bit-jump-tables
-fp16fml
-fp8
-fp8dot2
-fp8dot4
-fp8fma
+fp-armv8
-fpmr
-fptoint
+fullfp16
-fuse-address
-fuse-addsub-2reg-const1
-fuse-adrp-add
-fuse-aes
-fuse-arith-logic
-fuse-crypto-eor
-fuse-csel
-fuse-literals
-gcs
-harden-sls-blr
-harden-sls-nocomdat
-harden-sls-retbr
-hbc
-hcx
-i8mm
-ite
-jsconv
-ldp-aligned-only
-lor
-ls64
+lse
-lse128
-lse2
-lut
-mec
-mops
-mpam
-mte
+neon
-nmi
-no-bti-at-return-twice
-no-neg-immediates
-no-sve-fp-ld1r
-no-zcz-fp
-nv
-outline-atomics
-pan
-pan-rwv
-pauth
-pauth-lr
-perfmon
-predictable-select-expensive
-predres
-prfm-slc-target
-rand
-ras
-rasv2
+rcpc
-rcpc3
-rcpc-immo
+rdm
-reserve-x1
-reserve-x10
-reserve-x11
-reserve-x12
-reserve-x13
-reserve-x14
-reserve-x15
-reserve-x18
-reserve-x2
-reserve-x20
-reserve-x21
-reserve-x22
-reserve-x23
-reserve-x24
-reserve-x25
-reserve-x26
-reserve-x27
-reserve-x28
-reserve-x3
-reserve-x30
-reserve-x4
-reserve-x5
-reserve-x6
-reserve-x7
-reserve-x9
-rme
-sb
-sel2
+sha2
-sha3
-slow-misaligned-128store
-slow-paired-128
-slow-strqro-store
-sm4
-sme
-sme2
-sme2p1
-sme-f16f16
-sme-f64f64
-sme-f8f16
-sme-f8f32
-sme-fa64
-sme-i16i64
-sme-lutv2
-spe
-spe-eef
-specres2
-specrestrict
+ssbs
-ssve-fp8dot2
-ssve-fp8dot4
-ssve-fp8fma
-store-pair-suppress
-stp-aligned-only
-strict-align
-sve
-sve2
-sve2-aes
-sve2-bitperm
-sve2-sha3
-sve2-sm4
-sve2p1
-tagged-globals
-the
-tlb-rmi
-tlbiw
-tme
-tpidr-el1
-tpidr-el2
-tpidr-el3
-tpidrro-el0
-tracev8.4
-trbe
-uaops
-use-experimental-zeroing-pseudos
-use-postra-scheduler
-use-reciprocal-square-root
-use-scalar-inc-vl
-v8.1a
-v8.2a
-v8.3a
-v8.4a
-v8.5a
-v8.6a
-v8.7a
-v8.8a
-v8.9a
-v8a
-v8r
-v9.1a
-v9.2a
-v9.3a
-v9.4a
-v9.5a
-v9a
-vh
-wfxt
-xs
-zcm
-zcz
-zcz-fp-workaround
-zcz-gp
WeijieH commented 2 months ago

bump this up because I encountered the same issue on 0.13.0. zls crash immediately after start. Any zig code calling allocator will crash. Sample code as test.zig:

const std = @import("std");
const print = std.debug.print;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer print("leak: {any}\n", .{gpa.deinit()});
    const allocator = gpa.allocator();

    var a = std.ArrayList(i16).init(allocator);
    try a.append(10);
}

I'm able to build it to an executable. $ zig build-exe test.zig Generate test file successfully. $ file ./test Output is: ./test: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, with debug_info, not stripped But run it:

thread 1527209 panic: reached unreachable code
Panicked during a panic. Aborting.
Aborted (core dumped)

After some test, this is the line causing problem: try a.append(10);

DylanLukes commented 2 weeks ago

I'm getting a different (more informative) error on my OCI box, building either zls or a freshly initd project.

I'm on zig-0.14.0-dev.1550+4fba7336a.

❯ mkdir ziginit && pushd ziginit && zig init && zig build
~/src/ziginit ~/src ~ ~/src/zls
info: created build.zig
info: created build.zig.zon
info: created src/main.zig
info: created src/root.zig
info: see `zig build --help` for a menu of options
thread 3205231 panic: reached unreachable code
/home/opc/.asdf/installs/zig/master/lib/std/posix.zig:4651:23: 0x1429eaf in mprotect (build)
            .INVAL => unreachable,
                      ^
/home/opc/.asdf/installs/zig/master/lib/std/Thread.zig:1348:23: 0x145e4cf in spawn__anon_52284 (build)
        posix.mprotect(
                      ^
/home/opc/.asdf/installs/zig/master/lib/std/Thread.zig:342:32: 0x1421a7f in spawn__anon_9433 (build)
    const impl = try Impl.spawn(config, function, args);
                               ^
/home/opc/.asdf/installs/zig/master/lib/std/Progress.zig:422:55: 0x140a4b3 in start (build)
                .ansi_escape_codes => std.Thread.spawn(.{}, updateThreadRun, .{}),
                                                      ^
/home/opc/.asdf/installs/zig/master/lib/compiler/build_runner.zig:325:50: 0x1405ed3 in main (build)
    const main_progress_node = std.Progress.start(.{
                                                 ^
/home/opc/.asdf/installs/zig/master/lib/std/start.zig:617:37: 0x13e55c7 in posixCallMainAndExit (build)
            const result = root.main() catch |err| {
                                    ^
???:?:?: 0x0 in ??? (???)
error: the following build command crashed:
/home/opc/src/ziginit/.zig-cache/o/48a1196bb2d33da843c2edd86e9796c5/build /home/opc/.asdf/installs/zig/master/zig /home/opc/.asdf/installs/zig/master/lib /home/opc/src/ziginit /home/opc/src/ziginit/.zig-cache /home/opc/.cache/zig --seed 0x62cd278a -Z59092fa914885c6b
alexrp commented 2 weeks ago

I'm getting a different (more informative) error on my OCI box, building either zls or a freshly initd project.

This just looks like another page size issue; see #20511.

mikdusan commented 2 weeks ago

@DylanLukes can you run command:

getconf PAGESIZE
DylanLukes commented 2 weeks ago

@mikdusan Sure, it reports:

❯ getconf PAGESIZE
65536
pfgithub commented 2 weeks ago

The workaround for now is to edit lib/std/mem.zig next to your zig installation with this patch (no need to recompile zig):

pub const page_size = switch (builtin.cpu.arch) {
    .wasm32, .wasm64 => 64 * 1024,
    .aarch64 => switch (builtin.os.tag) {
        .macos, .ios, .watchos, .tvos, .visionos => 16 * 1024,
+       .linux => 65536, // or your page size, run `getconf PAGESIZE` to find it
        else => 4 * 1024,
    },

Once runtime page size detection is implemented, this won't be necessary anymore.

zsxawerdu commented 2 weeks ago

The workaround for now is to edit lib/std/mem.zig next to your zig installation with this patch (no need to recompile zig):

pub const page_size = switch (builtin.cpu.arch) {
    .wasm32, .wasm64 => 64 * 1024,
    .aarch64 => switch (builtin.os.tag) {
        .macos, .ios, .watchos, .tvos, .visionos => 16 * 1024,
+       .linux => 65536,
        else => 4 * 1024,
    },

Once runtime page size detection is implemented, this won't be necessary anymore.

if anyone is having this issue, this is a workaround. modify the above file, and things start working. Example Ziglings.

thank you sir @pfgithub