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{...@@ -37,7 +37,7 @@ var c_allocator_state = Allocator{
37 .resizeFn = cResize,37 .resizeFn = cResize,
38};38};
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 {
41 assert(ptr_align <= @alignOf(c_longdouble));41 assert(ptr_align <= @alignOf(c_longdouble));
42 const ptr = @ptrCast([*]u8, c.malloc(len) orelse return error.OutOfMemory);42 const ptr = @ptrCast([*]u8, c.malloc(len) orelse return error.OutOfMemory);
43 if (len_align == 0) {43 if (len_align == 0) {
...@@ -54,7 +54,14 @@ fn cAlloc(self: *Allocator, len: usize, ptr_align: u29, len_align: u29) Allocato...@@ -54,7 +54,14 @@ fn cAlloc(self: *Allocator, len: usize, ptr_align: u29, len_align: u29) Allocato
54 return ptr[0..mem.alignBackwardAnyAlign(full_len, len_align)];54 return ptr[0..mem.alignBackwardAnyAlign(full_len, len_align)];
55}55}
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 {
58 if (new_len == 0) {65 if (new_len == 0) {
59 c.free(buf.ptr);66 c.free(buf.ptr);
60 return 0;67 return 0;
lib/std/heap/general_purpose_allocator.zig+4-1
...@@ -102,9 +102,12 @@ const StackTrace = std.builtin.StackTrace;...@@ -102,9 +102,12 @@ const StackTrace = std.builtin.StackTrace;
102/// Integer type for pointing to slots in a small allocation102/// Integer type for pointing to slots in a small allocation
103const SlotIndex = std.meta.Int(false, math.log2(page_size) + 1);103const 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
105pub const Config = struct {108pub const Config = struct {
106 /// Number of stack frames to capture.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),
108111
109 /// If true, the allocator will have two fields:112 /// If true, the allocator will have two fields:
110 /// * `total_requested_bytes` which tracks the total allocated bytes of memory requested.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,6 +5886,12 @@ static LLVMValueRef ir_render_breakpoint(CodeGen *g, IrExecutableGen *executable
5886static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutableGen *executable,5886static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutableGen *executable,
5887 IrInstGenReturnAddress *instruction)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 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);5895 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);
5890 LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");5896 LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
5891 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, "");5897 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, "");