Skip to content

[cxx-interop] Fix a crash with [[no_unique_address]] #80786

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
Apr 25, 2025
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
23 changes: 23 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjCCommon.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/Type.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Basic/TargetInfo.h"
Expand Down Expand Up @@ -4479,6 +4480,28 @@ namespace {
}

Decl *VisitFieldDecl(const clang::FieldDecl *decl) {
if (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.
const auto &fieldLayout =
decl->getASTContext().getASTRecordLayout(rd);
auto &clangCtx = decl->getASTContext();
if (!decl->isZeroSize(clangCtx) &&
fieldLayout.getDataSize() != fieldLayout.getSize()) {
const auto *parent = decl->getParent();
auto currIdx = decl->getFieldIndex();
auto nextIdx = currIdx + 1;
const auto &parentLayout = clangCtx.getASTRecordLayout(parent);
if (parentLayout.getFieldCount() > nextIdx &&
parentLayout.getFieldOffset(nextIdx) <
(parentLayout.getFieldOffset(currIdx) +
clangCtx.toBits(fieldLayout.getSize())))
return nullptr;
}
}
}
// Fields are imported as variables.
std::optional<ImportedName> correctSwiftName;
ImportedName importedName;
Expand Down
13 changes: 12 additions & 1 deletion lib/IRGen/GenStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "GenStruct.h"

#include "IRGen.h"
#include "swift/AST/ClangModuleLoader.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/Decl.h"
Expand All @@ -42,6 +43,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/Error.h"
#include <iterator>

#include "GenDecl.h"
Expand Down Expand Up @@ -1459,6 +1461,14 @@ class ClangRecordLowering {
unsigned fieldOffset = layout.getFieldOffset(clangField->getFieldIndex());
assert(!clangField->isBitField());
Size offset( SubobjectAdjustment.getValue() + fieldOffset / 8);
std::optional<Size> dataSize;
if (clangField->hasAttr<clang::NoUniqueAddressAttr>()) {
if (const auto *rd = clangField->getType()->getAsRecordDecl()) {
// Clang can store the next field in the padding of this one.
const auto &fieldLayout = ClangContext.getASTRecordLayout(rd);
dataSize = Size(fieldLayout.getDataSize().getQuantity());
}
}

// If we have a Swift import of this type, use our lowered information.
if (swiftField) {
Expand All @@ -1471,7 +1481,8 @@ class ClangRecordLowering {

// Otherwise, add it as an opaque blob.
auto fieldSize = isZeroSized ? clang::CharUnits::Zero() : ClangContext.getTypeSizeInChars(clangField->getType());
return addOpaqueField(offset, Size(fieldSize.getQuantity()));
return addOpaqueField(offset,
dataSize.value_or(Size(fieldSize.getQuantity())));
}

/// Add opaque storage for bitfields spanning the given range of bits.
Expand Down
23 changes: 23 additions & 0 deletions test/Interop/Cxx/class/Inputs/member-variables.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef TEST_INTEROP_CXX_CLASS_INPUTS_MEMBER_VARIABLES_H
#define TEST_INTEROP_CXX_CLASS_INPUTS_MEMBER_VARIABLES_H

#include <cstddef>
#include <optional>

class MyClass {
public:
const int const_member = 23;
Expand All @@ -24,6 +27,26 @@ struct HasZeroSizedField {
void set_c(short c) { this->c = c; }
};

struct ReuseFieldPadding {
[[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); }
std::optional<int> getOptional() { return a; }
};

using OptInt = std::optional<int>;

struct ReuseFieldPaddingWithTypedef {
[[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); }
};


inline int takesZeroSizedInCpp(HasZeroSizedField x) {
return x.a;
}
Expand Down
32 changes: 31 additions & 1 deletion test/Interop/Cxx/class/zero-sized-field.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ FieldsTestSuite.test("Zero sized field") {
s.set_c(7)
takeTypeWithZeroSizedMember(s)
let s2 = s
let myInt : Empty.type = 6
let _ : Empty.type = 6
expectEqual(s.a, 5)
expectEqual(s.a, s.get_a())
expectEqual(s2.c, 7)
Expand All @@ -24,4 +24,34 @@ FieldsTestSuite.test("Zero sized field") {
expectEqual(s.b.getNum(), 42)
}

FieldsTestSuite.test("Field padding reused") {
var s = ReuseFieldPadding()
let opt = s.getOptional()
expectEqual(Int(opt.pointee), 2)
s.c = 5
expectEqual(Int(s.offset()), MemoryLayout<ReuseFieldPadding>.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("Typedef'd field padding reused") {
var s = ReuseFieldPaddingWithTypedef()
s.c = 5
expectEqual(Int(s.offset()), MemoryLayout<ReuseFieldPadding>.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)
}

runAllTests()