authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-16 11:23:38-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-16 11:23:38-04:00
logdd5b2d1b04197903489f5da9c178e17e61bb55e3
tree4c0e4cfd9774163cb0412e8f0b8edb7dc5756dfd
parent780e5674467ebac4534cd3d3f2199ccaf1d0922c
signaturelock-open Commit is signed but in an unrecognized format.

fix crash when pointer casting a runtime extern function


2 files changed, 13 insertions(+), 3 deletions(-)

src/analyze.cpp+4-3
......@@ -3992,9 +3992,10 @@ uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
39923992 return (ptr_type->data.pointer.explicit_alignment == 0) ?
39933993 get_abi_alignment(g, ptr_type->data.pointer.child_type) : ptr_type->data.pointer.explicit_alignment;
39943994 } else if (ptr_type->id == ZigTypeIdFn) {
3995 return (ptr_type->data.fn.fn_type_id.alignment == 0) ?
3996 LLVMABIAlignmentOfType(g->target_data_ref, ptr_type->data.fn.raw_type_ref) :
3997 ptr_type->data.fn.fn_type_id.alignment;
3995 // I tried making this use LLVMABIAlignmentOfType but it trips this assertion in LLVM:
3996 // "Cannot getTypeInfo() on a type that is unsized!"
3997 // when getting the alignment of `?extern fn() void`.
3998 return (ptr_type->data.fn.fn_type_id.alignment == 0) ? 1 : ptr_type->data.fn.fn_type_id.alignment;
39983999 } else if (ptr_type->id == ZigTypeIdPromise) {
39994000 return get_coro_frame_align_bytes(g);
40004001 } else {
test/cases/align.zig+9
......@@ -219,3 +219,12 @@ test "alignment of structs" {
219219 b: *i32,
220220 }) == @alignOf(usize));
221221}
222
223test "alignment of extern() void" {
224 var runtime_nothing = nothing;
225 const casted1 = @ptrCast(*const u8, runtime_nothing);
226 const casted2 = @ptrCast(extern fn () void, casted1);
227 casted2();
228}
229
230extern fn nothing() void {}