authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-08 01:00:29-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-08 01:00:29-07:00
log1b1921f0e26b9cb8ac78003c9061acac2da5e7ab
tree4008d0d0e7eecbc32f61af6e36077f01ec1a489a
parent9f5a7d5922f48170551e7f6938b6de9eadeba0ff

stage1: deal with WebAssembly not supporting @returnAddress()

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{
3737 .resizeFn = cResize,
3838};
3939
40fn cAlloc(self: *Allocator, len: usize, ptr_align: u29, len_align: u29) Allocator.Error![]u8 {
40fn cAlloc(self: *Allocator, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) Allocator.Error![]u8 {
4141 assert(ptr_align <= @alignOf(c_longdouble));
4242 const ptr = @ptrCast([*]u8, c.malloc(len) orelse return error.OutOfMemory);
4343 if (len_align == 0) {
......@@ -54,7 +54,14 @@ fn cAlloc(self: *Allocator, len: usize, ptr_align: u29, len_align: u29) Allocato
5454 return ptr[0..mem.alignBackwardAnyAlign(full_len, len_align)];
5555}
5656
57fn cResize(self: *Allocator, buf: []u8, old_align: u29, new_len: usize, len_align: u29) Allocator.Error!usize {
57fn 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 {
5865 if (new_len == 0) {
5966 c.free(buf.ptr);
6067 return 0;
lib/std/heap/general_purpose_allocator.zig+4-1
......@@ -102,9 +102,12 @@ const StackTrace = std.builtin.StackTrace;
102102/// Integer type for pointing to slots in a small allocation
103103const SlotIndex = std.meta.Int(false, math.log2(page_size) + 1);
104104
105// WebAssembly doesn't support stack tracing yet.
106const default_stack_trace_frames: usize = if (std.Target.current.cpu.arch.isWasm()) 0 else 4;
107
105108pub const Config = struct {
106109 /// 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),
108111
109112 /// If true, the allocator will have two fields:
110113 /// * `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
58865886static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutableGen *executable,
58875887 IrInstGenReturnAddress *instruction)
58885888{
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
58895895 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);
58905896 LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
58915897 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, "");