Skip to content

🍒 [6.2] [cxx-interop] Avoid querying layout of dependent types during symbolic import #81235

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
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
8 changes: 7 additions & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4472,11 +4472,17 @@ namespace {
}

Decl *VisitFieldDecl(const clang::FieldDecl *decl) {
if (decl->hasAttr<clang::NoUniqueAddressAttr>()) {
if (!Impl.importSymbolicCXXDecls &&
decl->hasAttr<clang::NoUniqueAddressAttr>()) {
if (const auto *rd = decl->getType()->getAsRecordDecl()) {
// Clang can store the next field in the padding of this one. Swift
// does not support this yet so let's not import the field and
// represent it with an opaque blob in codegen.
//
// This check is not relevant when importing the decl symbolically
// (since that isn't used for codegen). In fact, we need to avoid this
// check because symbolic imports can expose us to dependent types
// whose ASTRecordLayout cannot be queried.
const auto &fieldLayout =
decl->getASTContext().getASTRecordLayout(rd);
auto &clangCtx = decl->getASTContext();
Expand Down
40 changes: 36 additions & 4 deletions test/Interop/Cxx/class/Inputs/member-variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define TEST_INTEROP_CXX_CLASS_INPUTS_MEMBER_VARIABLES_H

#include <cstddef>
#include <type_traits>
#include <optional>

class MyClass {
Expand All @@ -27,25 +28,56 @@ struct HasZeroSizedField {
void set_c(short c) { this->c = c; }
};

struct ReuseFieldPadding {
struct ReuseOptionalFieldPadding {
[[no_unique_address]] std::optional<int> a = {2};
char c;
char get_c() const { return c; }
void set_c(char c) { this->c = c; }
int offset() const { return offsetof(ReuseFieldPadding, c); }
int offset() const { return offsetof(ReuseOptionalFieldPadding, c); }
std::optional<int> getOptional() { return a; }
};

using OptInt = std::optional<int>;

struct ReuseFieldPaddingWithTypedef {
struct ReuseOptionalFieldPaddingWithTypedef {
[[no_unique_address]] OptInt a;
char c;
char get_c() const { return c; }
void set_c(char c) { this->c = c; }
int offset() const { return offsetof(ReuseFieldPadding, c); }
int offset() const { return offsetof(ReuseOptionalFieldPadding, c); }
};

// Using a mix of private and public fields prevents this class from being
// standard-layout, which is necessary to allow clang to reuse its padding.
template<typename T>
struct NonStandardLayoutClass {
private:
T x;
public:
char pad_me;
};
static_assert(std::is_standard_layout_v<NonStandardLayoutClass<int>> == false);

struct ReuseNonStandardLayoutFieldPadding {
[[no_unique_address]] NonStandardLayoutClass<int> a;
char c;
char get_c() const { return c; }
void set_c(char c) { this->c = c; }
// C-style implementation of offsetof() to avoid non-standard-layout warning
int offset() const { return (char *) &this->c - (char *) this; }
};

template<typename T>
struct ReuseDependentFieldPadding {
[[no_unique_address]] struct { private: T x; public: char pad_me; } a;
char c;
char get_c() const { return c; }
void set_c(char c) { this->c = c; }
// C-style implementation of offsetof() to avoid non-standard-layout warning
int offset() const { return (char *) &this->c - (char *) this; }
};

typedef ReuseDependentFieldPadding<int> ReuseDependentFieldPaddingInt;

inline int takesZeroSizedInCpp(HasZeroSizedField x) {
return x.a;
Expand Down
43 changes: 36 additions & 7 deletions test/Interop/Cxx/class/zero-sized-field.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop -Xcc -std=c++20)
// RUN: %empty-directory(%t/index)
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop -Xcc -std=c++20 -Xfrontend -index-store-path -Xfrontend %t/index)
//
// REQUIRES: executable_test

Expand All @@ -24,12 +25,12 @@ FieldsTestSuite.test("Zero sized field") {
expectEqual(s.b.getNum(), 42)
}

FieldsTestSuite.test("Field padding reused") {
var s = ReuseFieldPadding()
FieldsTestSuite.test("Optional field padding reused") {
var s = ReuseOptionalFieldPadding()
let opt = s.getOptional()
expectEqual(Int(opt.pointee), 2)
s.c = 5
expectEqual(Int(s.offset()), MemoryLayout<ReuseFieldPadding>.offset(of: \.c)!)
expectEqual(Int(s.offset()), MemoryLayout<ReuseOptionalFieldPadding>.offset(of: \.c)!)
expectEqual(s.c, 5)
expectEqual(s.get_c(), 5)
s.set_c(6)
Expand All @@ -40,10 +41,38 @@ FieldsTestSuite.test("Field padding reused") {
expectEqual(s2.get_c(), 6)
}

FieldsTestSuite.test("Typedef'd field padding reused") {
var s = ReuseFieldPaddingWithTypedef()
FieldsTestSuite.test("Typedef'd optional field padding reused") {
var s = ReuseOptionalFieldPaddingWithTypedef()
s.c = 5
expectEqual(Int(s.offset()), MemoryLayout<ReuseFieldPadding>.offset(of: \.c)!)
expectEqual(Int(s.offset()), MemoryLayout<ReuseOptionalFieldPadding>.offset(of: \.c)!)
expectEqual(s.c, 5)
expectEqual(s.get_c(), 5)
s.set_c(6)
expectEqual(s.c, 6)
expectEqual(s.get_c(), 6)
let s2 = s
expectEqual(s2.c, 6)
expectEqual(s2.get_c(), 6)
}

FieldsTestSuite.test("Non-standard-layout field padding reused") {
var s = ReuseNonStandardLayoutFieldPadding()
s.c = 5
expectEqual(Int(s.offset()), MemoryLayout<ReuseNonStandardLayoutFieldPadding>.offset(of: \.c)!)
expectEqual(s.c, 5)
expectEqual(s.get_c(), 5)
s.set_c(6)
expectEqual(s.c, 6)
expectEqual(s.get_c(), 6)
let s2 = s
expectEqual(s2.c, 6)
expectEqual(s2.get_c(), 6)
}

FieldsTestSuite.test("Non-standard-layout field padding in templated class reused") {
var s = ReuseDependentFieldPaddingInt()
s.c = 5
expectEqual(Int(s.offset()), MemoryLayout<ReuseDependentFieldPaddingInt>.offset(of: \.c)!)
expectEqual(s.c, 5)
expectEqual(s.get_c(), 5)
s.set_c(6)
Expand Down