Skip to content

[cxx-interop] Check the safety of C++ template arguments #78522

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 20, 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
40 changes: 40 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2194,6 +2194,39 @@ namespace {
dc);
Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}] = result;

// We have to do this after populating ImportedDecls to avoid importing
// the same multiple times.
if (Impl.SwiftContext.LangOpts.hasFeature(Feature::SafeInterop) &&
Impl.SwiftContext.LangOpts.hasFeature(
Feature::AllowUnsafeAttribute)) {
if (const auto *ctsd =
dyn_cast<clang::ClassTemplateSpecializationDecl>(decl)) {
for (auto arg : ctsd->getTemplateArgs().asArray()) {
llvm::SmallVector<clang::TemplateArgument, 1> nonPackArgs;
if (arg.getKind() == clang::TemplateArgument::Pack) {
auto pack = arg.getPackAsArray();
nonPackArgs.assign(pack.begin(), pack.end());
} else {
nonPackArgs.push_back(arg);
}
for (auto realArg : nonPackArgs) {
if (realArg.getKind() != clang::TemplateArgument::Type)
continue;
auto SwiftType = Impl.importTypeIgnoreIUO(
realArg.getAsType(), ImportTypeKind::Abstract,
[](Diagnostic &&diag) {}, false, Bridgeability::None,
ImportTypeAttrs());
if (SwiftType && SwiftType->isUnsafe()) {
auto attr =
new (Impl.SwiftContext) UnsafeAttr(/*implicit=*/true);
result->getAttrs().add(attr);
break;
}
}
}
}
}

if (recordHasMoveOnlySemantics(decl)) {
if (decl->isInStdNamespace() && decl->getName() == "promise") {
// Do not import std::promise.
Expand Down Expand Up @@ -8404,6 +8437,13 @@ static bool importAsUnsafe(ClangImporter::Implementation &impl,
if (isa<ClassDecl>(MappedDecl))
return false;

// Most STL containers have std::allocator as their default allocator. We need
// to consider std::allocator safe for the STL containers to be ever
// considered safe.
if (decl->isInStdNamespace() && decl->getIdentifier() &&
decl->getName() == "allocator")
return false;

if (const auto *record = dyn_cast<clang::RecordDecl>(decl))
return evaluateOrDefault(
context.evaluator,
Expand Down
22 changes: 21 additions & 1 deletion test/Interop/Cxx/class/safe-interop-mode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module Test {
//--- Inputs/nonescapable.h
#include "swift/bridging"
#include <span>
#include <vector>
#include <tuple>

struct SWIFT_NONESCAPABLE View {
__attribute__((swift_attr("@lifetime(immortal)")))
Expand Down Expand Up @@ -54,6 +56,10 @@ struct MyContainer {

using SpanOfInt = std::span<int>;
using SpanOfIntAlias = SpanOfInt;
using VecOfPtr = std::vector<int*>;
using VecOfInt = std::vector<int>;
using SafeTuple = std::tuple<int, int, int>;
using UnsafeTuple = std::tuple<int, int*, int>;

//--- test.swift

Expand Down Expand Up @@ -85,7 +91,21 @@ func useCfType(x: CFArray) {
func useString(x: std.string) {
}

// expected-warning@+1{{global function 'useCppSpan' has an interface that is not memory-safe}}
// expected-warning@+1{{global function 'useVecOfPtr' has an interface that is not memory-safe; use '@unsafe' to indicate that its use is unsafe}}
func useVecOfPtr(x: VecOfPtr) { // expected-note{{reference to unsafe type alias 'VecOfPtr'}}
}

func useVecOfInt(x: VecOfInt) {
}

func useSafeTuple(x: SafeTuple) {
}

// expected-warning@+1{{global function 'useUnsafeTuple' has an interface that is not memory-safe; use '@unsafe' to indicate that its use is unsafe}}
func useUnsafeTuple(x: UnsafeTuple) { // expected-note{{reference to unsafe type alias 'UnsafeTuple'}}
}

// expected-warning@+1{{global function 'useCppSpan' has an interface that is not memory-safe; use '@unsafe' to indicate that its use is unsafe}}
func useCppSpan(x: SpanOfInt) { // expected-note{{reference to unsafe type alias 'SpanOfInt'}}
}

Expand Down