Sema: emit cast to null panics for function pointers

Closes #14676
This commit is contained in:
Veikka Tuominen 2023-04-23 13:00:33 +03:00
parent c3b30a0fd0
commit f1e43d1f4f
2 changed files with 26 additions and 3 deletions

View File

@ -19626,7 +19626,7 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
}
try sema.requireRuntimeBlock(block, src, operand_src);
if (block.wantSafety() and try sema.typeHasRuntimeBits(elem_ty)) {
if (block.wantSafety() and (try sema.typeHasRuntimeBits(elem_ty) or elem_ty.zigTypeTag() == .Fn)) {
if (!ptr_ty.isAllowzeroPtr()) {
const is_non_zero = try block.addBinOp(.cmp_neq, operand_coerced, .zero_usize);
try sema.addSafetyCheck(block, is_non_zero, .cast_to_null);
@ -19852,7 +19852,7 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
try sema.requireRuntimeBlock(block, src, null);
if (block.wantSafety() and operand_ty.ptrAllowsZero() and !dest_ty.ptrAllowsZero() and
try sema.typeHasRuntimeBits(dest_ty.elemType2()))
(try sema.typeHasRuntimeBits(dest_ty.elemType2()) or dest_ty.elemType2().zigTypeTag() == .Fn))
{
const ptr_int = try block.addUnOp(.ptrtoint, ptr);
const is_non_zero = try block.addBinOp(.cmp_neq, ptr_int, .zero_usize);
@ -27718,7 +27718,7 @@ fn coerceCompatiblePtrs(
try sema.requireRuntimeBlock(block, inst_src, null);
const inst_allows_zero = inst_ty.zigTypeTag() != .Pointer or inst_ty.ptrAllowsZero();
if (block.wantSafety() and inst_allows_zero and !dest_ty.ptrAllowsZero() and
try sema.typeHasRuntimeBits(dest_ty.elemType2()))
(try sema.typeHasRuntimeBits(dest_ty.elemType2()) or dest_ty.elemType2().zigTypeTag() == .Fn))
{
const actual_ptr = if (inst_ty.isSlice())
try sema.analyzeSlicePtr(block, inst_src, inst, inst_ty)

View File

@ -0,0 +1,23 @@
const std = @import("std");
pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
_ = stack_trace;
if (std.mem.eql(u8, message, "cast causes pointer to be null")) {
std.process.exit(0);
}
std.process.exit(1);
}
fn getNullPtr() ?*const anyopaque {
return null;
}
pub fn main() !void {
const null_ptr: ?*const anyopaque = getNullPtr();
const required_ptr: *align(1) const fn() void = @ptrCast(*align(1) const fn() void, null_ptr);
_ = required_ptr;
return error.TestFailed;
}
// run
// backend=llvm
// target=native