Skip to content

IRGen: Thin function values may carry a precise pointer to function type rather than i8* #63143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/IRGen/GenFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ namespace {
return new ThinFuncTypeInfo(formalType, storageType, size, align,
spareBits);
}
void initialize(IRGenFunction &IGF, Explosion &src, Address addr,
bool isOutlined) const override {
auto *fn = src.claimNext();

// We might be presented with a value of the more precise pointer to
// function type "void(*)*" rather than the generic "i8*". Downcast to the
// more general expected type.
if (fn->getContext().supportsTypedPointers() &&
fn->getType()->getNonOpaquePointerElementType()->isFunctionTy())
fn = IGF.Builder.CreateBitCast(fn, getStorageType());

Explosion tmp;
tmp.add(fn);
PODSingleScalarTypeInfo<ThinFuncTypeInfo,LoadableTypeInfo>::initialize(IGF, tmp, addr, isOutlined);
}

TypeLayoutEntry *buildTypeLayoutEntry(IRGenModule &IGM,
SILType T) const override {
Expand Down
10 changes: 10 additions & 0 deletions test/IRGen/Inputs/fnptr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef fnptr_h
#define fnptr_h

#include <stddef.h>

typedef struct {
void (* _Nonnull fnptr)(size_t *);
} Container;

#endif
15 changes: 15 additions & 0 deletions test/IRGen/fnptr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-swift-frontend -import-objc-header %S/Inputs/fnptr.h -swift-version 5 -target arm64e-apple-ios13.0 %s -emit-ir -module-name test -use-clang-function-types -Xcc -Xclang -Xcc -fptrauth-function-pointer-type-discrimination | %FileCheck %s

// REQUIRES: CPU=arm64e
// REQUIRES: OS=ios

// This test used to crash in IRGen because of mismatching pointer types.

// CHECK: define{{.*}} swiftcc void @"$s4testAAyyF"()
// CHECK: store i8* bitcast {{.*}} @"$s4testAAyyFySpySiGSgcfU_To.ptrauth" to i8*), i8**

public func test() {
var tmp = Container(
fnptr: { (x) in return }
)
}