Skip to content

Commit 2285eb2

Browse files
authored
Merge pull request #68851 from kubamracek/embedded-serialize-access-enforcement
[embedded] Add an embedded test for -enable-import-ptrauth-field-function-pointers, fix deserialization
2 parents 8605da8 + 8bb2e34 commit 2285eb2

File tree

6 files changed

+90
-16
lines changed

6 files changed

+90
-16
lines changed

lib/Serialization/DeserializeSIL.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,9 +2368,9 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
23682368
SILValue op = getLocalValue(
23692369
ValID, getSILType(MF->getType(TyID), (SILValueCategory)TyCategory, Fn));
23702370
auto accessKind = SILAccessKind(Attr & 0x3);
2371-
auto enforcement = SILAccessEnforcement((Attr >> 2) & 0x3);
2372-
bool noNestedConflict = (Attr >> 4) & 0x01;
2373-
bool fromBuiltin = (Attr >> 5) & 0x01;
2371+
auto enforcement = SILAccessEnforcement((Attr >> 2) & 0x07);
2372+
bool noNestedConflict = (Attr >> 5) & 0x01;
2373+
bool fromBuiltin = (Attr >> 6) & 0x01;
23742374
ResultInst = Builder.createBeginAccess(Loc, op, accessKind, enforcement,
23752375
noNestedConflict, fromBuiltin);
23762376
break;
@@ -2410,9 +2410,9 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
24102410
getLocalValue(ValID2, getSILType(MF->getType(TyID2),
24112411
(SILValueCategory)TyCategory2, Fn));
24122412
auto accessKind = SILAccessKind(Attr & 0x3);
2413-
auto enforcement = SILAccessEnforcement((Attr >> 2) & 0x03);
2414-
bool noNestedConflict = (Attr >> 4) & 0x01;
2415-
bool fromBuiltin = (Attr >> 5) & 0x01;
2413+
auto enforcement = SILAccessEnforcement((Attr >> 2) & 0x07);
2414+
bool noNestedConflict = (Attr >> 5) & 0x01;
2415+
bool fromBuiltin = (Attr >> 6) & 0x01;
24162416
ResultInst = Builder.createBeginUnpairedAccess(
24172417
Loc, source, buffer, accessKind, enforcement, noNestedConflict,
24182418
fromBuiltin);
@@ -2422,8 +2422,8 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
24222422
SILValue op = getLocalValue(
24232423
ValID, getSILType(MF->getType(TyID), (SILValueCategory)TyCategory, Fn));
24242424
bool aborted = Attr & 0x1;
2425-
auto enforcement = SILAccessEnforcement((Attr >> 1) & 0x03);
2426-
bool fromBuiltin = (Attr >> 3) & 0x01;
2425+
auto enforcement = SILAccessEnforcement((Attr >> 1) & 0x07);
2426+
bool fromBuiltin = (Attr >> 4) & 0x01;
24272427
ResultInst = Builder.createEndUnpairedAccess(Loc, op, enforcement, aborted,
24282428
fromBuiltin);
24292429
break;

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 809; // typed throws
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 810; // access enforcement
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///

lib/Serialization/SILFormat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ namespace sil_block {
425425
using SILOneOperandExtraAttributeLayout = BCRecordLayout<
426426
SIL_ONE_OPERAND_EXTRA_ATTR,
427427
SILInstOpCodeField,
428-
BCFixed<6>, // Optional attributes
428+
BCFixed<7>, // Optional attributes
429429
TypeIDField, SILTypeCategoryField, ValueIDField
430430
>;
431431

@@ -452,7 +452,7 @@ namespace sil_block {
452452
using SILTwoOperandsExtraAttributeLayout = BCRecordLayout<
453453
SIL_TWO_OPERANDS_EXTRA_ATTR,
454454
SILInstOpCodeField,
455-
BCFixed<6>, // Optional attributes
455+
BCFixed<7>, // Optional attributes
456456
TypeIDField,
457457
SILTypeCategoryField,
458458
ValueIDField,

lib/Serialization/SerializeSIL.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,8 +2029,8 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
20292029
auto *BAI = cast<BeginAccessInst>(&SI);
20302030
unsigned attr = unsigned(BAI->getAccessKind())
20312031
+ (unsigned(BAI->getEnforcement()) << 2)
2032-
+ (BAI->hasNoNestedConflict() << 4)
2033-
+ (BAI->isFromBuiltin() << 5);
2032+
+ (BAI->hasNoNestedConflict() << 5)
2033+
+ (BAI->isFromBuiltin() << 6);
20342034
SILValue operand = BAI->getOperand();
20352035

20362036
SILOneOperandExtraAttributeLayout::emitRecord(
@@ -2094,8 +2094,8 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
20942094
auto *BAI = cast<BeginUnpairedAccessInst>(&SI);
20952095
unsigned attr = unsigned(BAI->getAccessKind())
20962096
+ (unsigned(BAI->getEnforcement()) << 2)
2097-
+ (unsigned(BAI->hasNoNestedConflict()) << 4)
2098-
+ (unsigned(BAI->isFromBuiltin()) << 5);
2097+
+ (unsigned(BAI->hasNoNestedConflict()) << 5)
2098+
+ (unsigned(BAI->isFromBuiltin()) << 6);
20992099
SILValue source = BAI->getSource();
21002100
SILValue buffer = BAI->getBuffer();
21012101

@@ -2115,7 +2115,7 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
21152115
auto *EAI = cast<EndUnpairedAccessInst>(&SI);
21162116
unsigned attr = unsigned(EAI->isAborting())
21172117
+ (unsigned(EAI->getEnforcement()) << 1)
2118-
+ (unsigned(EAI->isFromBuiltin()) << 3);
2118+
+ (unsigned(EAI->isFromBuiltin()) << 4);
21192119
SILValue operand = EAI->getOperand();
21202120

21212121
SILOneOperandExtraAttributeLayout::emitRecord(

test/embedded/ptrauth-fieldptr1.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend -enable-import-ptrauth-field-function-pointers -O -emit-ir %t/Main.swift -enable-experimental-feature Embedded -import-objc-header %t/header.h | %FileCheck %s
5+
6+
// REQUIRES: swift_in_compiler
7+
// REQUIRES: VENDOR=apple
8+
// REQUIRES: OS=macosx
9+
// REQUIRES: CPU=arm64e
10+
11+
// BEGIN header.h
12+
13+
struct MyStruct {
14+
void (*fptr1)(void);
15+
void (*__ptrauth(0, 0, 0x4242) fptr2)(void);
16+
};
17+
18+
// BEGIN Main.swift
19+
20+
public func test1(x: UnsafePointer<MyStruct>) {
21+
x.pointee.fptr1()
22+
}
23+
24+
public func test2(x: UnsafePointer<MyStruct>) {
25+
x.pointee.fptr2()
26+
}
27+
28+
// CHECK: define {{.*}}@"$s4Main5test11xySPySo8MyStructVG_tF"
29+
// CHECK: call {{.*}}[ "ptrauth"(i32 0, i64 0) ]
30+
// CHECK: define {{.*}}@"$s4Main5test21xySPySo8MyStructVG_tF"
31+
// CHECK: call {{.*}}[ "ptrauth"(i32 0, i64 16962) ]

test/embedded/ptrauth-fieldptr2.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend -enable-import-ptrauth-field-function-pointers -O -emit-module -o %t/Module.swiftmodule %t/Module.swift -enable-experimental-feature Embedded -import-objc-header %t/header.h
5+
6+
// RUN: %target-swift-frontend -enable-import-ptrauth-field-function-pointers -O -emit-ir %t/Main.swift -I%t -enable-experimental-feature Embedded -import-objc-header %t/header.h
7+
8+
// REQUIRES: swift_in_compiler
9+
// REQUIRES: VENDOR=apple
10+
// REQUIRES: OS=macosx
11+
// REQUIRES: CPU=arm64e
12+
13+
// BEGIN header.h
14+
15+
#pragma once
16+
struct MyStruct {
17+
void (*fptr1)(void);
18+
void (*__ptrauth(0, 0, 0x4242) fptr2)(void);
19+
};
20+
21+
// BEGIN Module.swift
22+
23+
public func test1(x: UnsafePointer<MyStruct>) {
24+
x.pointee.fptr1()
25+
}
26+
27+
public func test2(x: UnsafePointer<MyStruct>) {
28+
x.pointee.fptr2()
29+
}
30+
31+
// BEGIN Main.swift
32+
33+
import Module
34+
35+
public func mainfunc(x: UnsafePointer<MyStruct>) {
36+
test1(x: x)
37+
test2(x: x)
38+
}
39+
40+
// CHECK: define {{.*}}@"$s4Main5test11xySPySo8MyStructVG_tF"
41+
// CHECK: call {{.*}}[ "ptrauth"(i32 0, i64 0) ]
42+
// CHECK: define {{.*}}@"$s4Main5test21xySPySo8MyStructVG_tF"
43+
// CHECK: call {{.*}}[ "ptrauth"(i32 0, i64 16962) ]

0 commit comments

Comments
 (0)