From 5c7f2ab011f9c1cd522ab76a674d0acc5f397041 Mon Sep 17 00:00:00 2001 From: Kenta Iwasaki Date: Mon, 20 Dec 2021 00:30:49 +0900 Subject: [PATCH] stage1: deal with BPF not supporting @returnAddress() Make `@returnAddress()` return for the BPF target, as the BPF target for the time being does not support probing for the return address. Stack traces for the general purpose allocator for the BPF target is also set to not be captured. --- lib/std/heap/general_purpose_allocator.zig | 5 +++++ src/stage1/codegen.cpp | 2 +- src/stage1/target.cpp | 4 ++++ src/stage1/target.hpp | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/std/heap/general_purpose_allocator.zig b/lib/std/heap/general_purpose_allocator.zig index 9900823d5bda8d6e43ea32fbe00fb2dab6a76010..dfef8016d586fe444ae003307a86d50bc23a7e6e 100644 --- a/lib/std/heap/general_purpose_allocator.zig +++ b/lib/std/heap/general_purpose_allocator.zig @@ -118,6 +118,11 @@ const sys_can_stack_trace = switch (builtin.cpu.arch) { .wasm64, => builtin.os.tag == .emscripten, + // `@returnAddress()` is unsupported in LLVM 13. + .bpfel, + .bpfeb, + => false, + else => true, }; const default_test_stack_trace_frames: usize = if (builtin.is_test) 8 else 4; diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index ff4d44113ebc98871ef4ae741c0a007af8cc813d..335af423a8edd0b2fe682ba13c85cff2f4fc376b 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -6208,7 +6208,7 @@ static LLVMValueRef ir_render_breakpoint(CodeGen *g, Stage1Air *executable, Stag static LLVMValueRef ir_render_return_address(CodeGen *g, Stage1Air *executable, Stage1AirInstReturnAddress *instruction) { - if (target_is_wasm(g->zig_target) && g->zig_target->os != OsEmscripten) { + if ((target_is_wasm(g->zig_target) && g->zig_target->os != OsEmscripten) || target_is_bpf(g->zig_target)) { // I got this error from LLVM 10: // "Non-Emscripten WebAssembly hasn't implemented __builtin_return_address" return LLVMConstNull(get_llvm_type(g, instruction->base.value->type)); diff --git a/src/stage1/target.cpp b/src/stage1/target.cpp index 106c77854daf194c24b4f1fb63589232c0593565..feb2c7f143e1d1fe419f8de4af10b74c5ee5cc15 100644 --- a/src/stage1/target.cpp +++ b/src/stage1/target.cpp @@ -940,6 +940,10 @@ bool target_is_wasm(const ZigTarget *target) { return target->arch == ZigLLVM_wasm32 || target->arch == ZigLLVM_wasm64; } +bool target_is_bpf(const ZigTarget *target) { + return target->arch == ZigLLVM_bpfel || target->arch == ZigLLVM_bpfeb; +} + ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) { if (arch == ZigLLVM_wasm32 || arch == ZigLLVM_wasm64) { return ZigLLVM_Musl; diff --git a/src/stage1/target.hpp b/src/stage1/target.hpp index cbd86071af70186a7ea6ac4ae989344f3ed44977..6851d88618a0f229ac998f35a43f43e9583faf67 100644 --- a/src/stage1/target.hpp +++ b/src/stage1/target.hpp @@ -75,6 +75,7 @@ bool target_allows_addr_zero(const ZigTarget *target); bool target_has_valgrind_support(const ZigTarget *target); bool target_os_is_darwin(Os os); bool target_is_wasm(const ZigTarget *target); +bool target_is_bpf(const ZigTarget *target); bool target_is_riscv(const ZigTarget *target); bool target_is_sparc(const ZigTarget *target); bool target_is_android(const ZigTarget *target); -- 2.54.0