| author | |
| committer | |
| log | 1b1921f0e26b9cb8ac78003c9061acac2da5e7ab |
| tree | 4008d0d0e7eecbc32f61af6e36077f01ec1a489a |
| parent | 9f5a7d5922f48170551e7f6938b6de9eadeba0ff |
This makes `@returnAddress()` return 0 for WebAssembly (when not using
the Emscripten OS) and avoids trying to capture stack traces for the
general purpose allocator on that target.3 files changed, 19 insertions(+), 3 deletions(-)
lib/std/heap.zig+9-2| ... | ... | @@ -37,7 +37,7 @@ var c_allocator_state = Allocator{ |
| 37 | 37 | .resizeFn = cResize, |
| 38 | 38 | }; |
| 39 | 39 | |
| 40 | fn cAlloc(self: *Allocator, len: usize, ptr_align: u29, len_align: u29) Allocator.Error![]u8 { | |
| 40 | fn cAlloc(self: *Allocator, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Allocator.Error![]u8 { | |
| 41 | 41 | assert(ptr_align <= @alignOf(c_longdouble)); |
| 42 | 42 | const ptr = @ptrCast([*]u8, c.malloc(len) orelse return error.OutOfMemory); |
| 43 | 43 | if (len_align == 0) { |
| ... | ... | @@ -54,7 +54,14 @@ fn cAlloc(self: *Allocator, len: usize, ptr_align: u29, len_align: u29) Allocato |
| 54 | 54 | return ptr[0..mem.alignBackwardAnyAlign(full_len, len_align)]; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | fn cResize(self: *Allocator, buf: []u8, old_align: u29, new_len: usize, len_align: u29) Allocator.Error!usize { | |
| 57 | fn cResize( | |
| 58 | self: *Allocator, | |
| 59 | buf: []u8, | |
| 60 | old_align: u29, | |
| 61 | new_len: usize, | |
| 62 | len_align: u29, | |
| 63 | ret_addr: usize, | |
| 64 | ) Allocator.Error!usize { | |
| 58 | 65 | if (new_len == 0) { |
| 59 | 66 | c.free(buf.ptr); |
| 60 | 67 | return 0; |
lib/std/heap/general_purpose_allocator.zig+4-1| ... | ... | @@ -102,9 +102,12 @@ const StackTrace = std.builtin.StackTrace; |
| 102 | 102 | /// Integer type for pointing to slots in a small allocation |
| 103 | 103 | const SlotIndex = std.meta.Int(false, math.log2(page_size) + 1); |
| 104 | 104 | |
| 105 | // WebAssembly doesn't support stack tracing yet. | |
| 106 | const default_stack_trace_frames: usize = if (std.Target.current.cpu.arch.isWasm()) 0 else 4; | |
| 107 | ||
| 105 | 108 | pub const Config = struct { |
| 106 | 109 | /// Number of stack frames to capture. |
| 107 | stack_trace_frames: usize = if (std.debug.runtime_safety) @as(usize, 4) else @as(usize, 0), | |
| 110 | stack_trace_frames: usize = if (std.debug.runtime_safety) default_stack_trace_frames else @as(usize, 0), | |
| 108 | 111 | |
| 109 | 112 | /// If true, the allocator will have two fields: |
| 110 | 113 | /// * `total_requested_bytes` which tracks the total allocated bytes of memory requested. |
src/codegen.cpp+6| ... | ... | @@ -5886,6 +5886,12 @@ static LLVMValueRef ir_render_breakpoint(CodeGen *g, IrExecutableGen *executable |
| 5886 | 5886 | static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutableGen *executable, |
| 5887 | 5887 | IrInstGenReturnAddress *instruction) |
| 5888 | 5888 | { |
| 5889 | if (target_is_wasm(g->zig_target) && g->zig_target->os != OsEmscripten) { | |
| 5890 | // I got this error from LLVM 10: | |
| 5891 | // "Non-Emscripten WebAssembly hasn't implemented __builtin_return_address" | |
| 5892 | return LLVMConstNull(get_llvm_type(g, instruction->base.value->type)); | |
| 5893 | } | |
| 5894 | ||
| 5889 | 5895 | LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type); |
| 5890 | 5896 | LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, ""); |
| 5891 | 5897 | return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, ""); |