Skip to content

Commit c88a420

Browse files
authored
Merge pull request #63076 from hborla/macro-use-after-free
2 parents ca6fd61 + 81492d1 commit c88a420

13 files changed

+213
-142
lines changed

include/swift/AST/Attr.h

-49
Original file line numberDiff line numberDiff line change
@@ -2613,55 +2613,6 @@ class DeclAttributes {
26132613
SourceLoc getStartLoc(bool forModifiers = false) const;
26142614
};
26152615

2616-
/// Semantic attributes that are applied to a declaration.
2617-
///
2618-
/// This attribute list can include attributes that are not written in
2619-
/// source on a declaration, such as attributes that are applied during
2620-
/// macro expansion or inferred from the declaration context.
2621-
class SemanticDeclAttributes {
2622-
llvm::SmallVector<DeclAttribute *, 4> attrList;
2623-
2624-
public:
2625-
SemanticDeclAttributes() {}
2626-
2627-
/// Add a constructed DeclAttribute to this list.
2628-
void add(DeclAttribute *attr) {
2629-
attrList.push_back(attr);
2630-
}
2631-
2632-
using SemanticAttrList = llvm::SmallVectorImpl<DeclAttribute *>;
2633-
2634-
template <typename AttrType, bool AllowInvalid>
2635-
using AttributeKindRange =
2636-
OptionalTransformRange<iterator_range<SemanticAttrList::const_iterator>,
2637-
ToAttributeKind<AttrType, AllowInvalid>,
2638-
SemanticAttrList::const_iterator>;
2639-
2640-
template <typename AttrType, bool AllowInvalid = false>
2641-
AttributeKindRange<AttrType, AllowInvalid> getAttributes() const {
2642-
return AttributeKindRange<AttrType, AllowInvalid>(
2643-
make_range(attrList.begin(), attrList.end()),
2644-
ToAttributeKind<AttrType, AllowInvalid>());
2645-
}
2646-
2647-
/// Retrieve the first attribute of the given attribute class.
2648-
template <typename AttrType>
2649-
const AttrType *getAttribute(bool allowInvalid = false) const {
2650-
for (auto attr : attrList)
2651-
if (auto *specificAttr = dyn_cast<AttrType>(attr))
2652-
if (specificAttr->isValid() || allowInvalid)
2653-
return specificAttr;
2654-
2655-
return nullptr;
2656-
}
2657-
2658-
/// Determine whether there is an attribute with the given attribute class.
2659-
template <typename AttrType>
2660-
bool hasAttribute(bool allowInvalid = false) const {
2661-
return getAttribute<AttrType>(allowInvalid) != nullptr;
2662-
}
2663-
};
2664-
26652616
/// TypeAttributes - These are attributes that may be applied to types.
26662617
class TypeAttributes {
26672618
// Get a SourceLoc for every possible attribute that can be parsed in source.

include/swift/AST/Decl.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -850,11 +850,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
850850
return Attrs;
851851
}
852852

853-
/// Returns the semantic attributes attached to this declaration.
854-
///
855-
/// \c getSemanticAttrs is intended to be a requestified replacement
856-
/// for \c getAttrs
857-
SemanticDeclAttributes getSemanticAttrs() const;
853+
/// Returns the semantic attributes attached to this declaration,
854+
/// including attributes that are generated as the result of member
855+
/// attribute macro expansion.
856+
DeclAttributes getSemanticAttrs() const;
858857

859858
/// Returns the innermost enclosing decl with an availability annotation.
860859
const Decl *getInnermostDeclWithAvailability() const;

include/swift/AST/SourceFile.h

+11
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,17 @@ class SourceFile final : public FileUnit {
500500
/// the \c SourceFileKind is \c MacroExpansion.
501501
ASTNode getMacroExpansion() const;
502502

503+
/// For source files created to hold the source code created by expanding
504+
/// an attached macro, this is the custom attribute that describes the macro
505+
/// expansion.
506+
///
507+
/// The source location of this attribute is the place in the source that
508+
/// triggered the creation of the macro expansion whose resulting source
509+
/// code is in this source file. This will only produce a non-null value when
510+
/// the \c SourceFileKind is \c MacroExpansion , and the macro is an attached
511+
/// macro.
512+
CustomAttr *getAttachedMacroAttribute() const;
513+
503514
/// When this source file is enclosed within another source file, for example
504515
/// because it describes a macro expansion, return the source file it was
505516
/// enclosed in.

include/swift/AST/TypeCheckRequests.h

+18-20
Original file line numberDiff line numberDiff line change
@@ -742,26 +742,6 @@ class AttachedPropertyWrappersRequest :
742742
bool isCached() const { return true; }
743743
};
744744

745-
/// Request the semantic attributes attached to the given declaration.
746-
class AttachedSemanticAttrsRequest :
747-
public SimpleRequest<AttachedSemanticAttrsRequest,
748-
SemanticDeclAttributes(Decl *),
749-
RequestFlags::Cached> {
750-
public:
751-
using SimpleRequest::SimpleRequest;
752-
753-
private:
754-
friend SimpleRequest;
755-
756-
// Evaluation.
757-
SemanticDeclAttributes
758-
evaluate(Evaluator &evaluator, Decl *decl) const;
759-
760-
public:
761-
// Caching
762-
bool isCached() const { return true; }
763-
};
764-
765745
/// Request the raw (possibly unbound generic) type of the property wrapper
766746
/// that is attached to the given variable.
767747
class AttachedPropertyWrapperTypeRequest :
@@ -3840,6 +3820,24 @@ class ExpandMacroExpansionDeclRequest
38403820
bool isCached() const { return true; }
38413821
};
38423822

3823+
/// Expand all member attribute macros attached to the given
3824+
/// declaration.
3825+
class ExpandMemberAttributeMacros
3826+
: public SimpleRequest<ExpandMemberAttributeMacros,
3827+
bool(Decl *),
3828+
RequestFlags::Cached> {
3829+
public:
3830+
using SimpleRequest::SimpleRequest;
3831+
3832+
private:
3833+
friend SimpleRequest;
3834+
3835+
bool evaluate(Evaluator &evaluator, Decl *decl) const;
3836+
3837+
public:
3838+
bool isCached() const { return true; }
3839+
};
3840+
38433841
/// Resolve an external macro given its module and type name.
38443842
class ExternalMacroDefinitionRequest
38453843
: public SimpleRequest<ExternalMacroDefinitionRequest,

include/swift/AST/TypeCheckerTypeIDZone.def

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ SWIFT_REQUEST(TypeChecker, AttachedPropertyWrapperTypeRequest,
2929
SWIFT_REQUEST(TypeChecker, AttachedPropertyWrappersRequest,
3030
llvm::TinyPtrVector<CustomAttr *>(VarDecl *), Cached,
3131
NoLocationInfo)
32-
SWIFT_REQUEST(TypeChecker, AttachedSemanticAttrsRequest,
33-
SemanticDeclAttributes(Decl *), Cached,
34-
NoLocationInfo)
3532
SWIFT_REQUEST(TypeChecker, CallerSideDefaultArgExprRequest,
3633
Expr *(DefaultArgumentExpr *), SeparatelyCached, NoLocationInfo)
3734
SWIFT_REQUEST(TypeChecker, CheckInconsistentImplementationOnlyImportsRequest,
@@ -455,6 +452,9 @@ SWIFT_REQUEST(TypeChecker, ExternalMacroDefinitionRequest,
455452
SWIFT_REQUEST(TypeChecker, ExpandMacroExpansionDeclRequest,
456453
ArrayRef<Decl *>(MacroExpansionDecl *),
457454
Cached, NoLocationInfo)
455+
SWIFT_REQUEST(TypeChecker, ExpandMemberAttributeMacros,
456+
bool(Decl *),
457+
Cached, NoLocationInfo)
458458
SWIFT_REQUEST(TypeChecker, SynthesizeRuntimeMetadataAttrGenerator,
459459
Expr *(CustomAttr *, ValueDecl *),
460460
Cached, NoLocationInfo)

lib/AST/Decl.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,13 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
365365
llvm_unreachable("bad DescriptiveDeclKind");
366366
}
367367

368-
SemanticDeclAttributes Decl::getSemanticAttrs() const {
368+
DeclAttributes Decl::getSemanticAttrs() const {
369369
auto mutableThis = const_cast<Decl *>(this);
370-
return evaluateOrDefault(getASTContext().evaluator,
371-
AttachedSemanticAttrsRequest{mutableThis},
372-
SemanticDeclAttributes());
370+
evaluateOrDefault(getASTContext().evaluator,
371+
ExpandMemberAttributeMacros{mutableThis},
372+
false);
373+
374+
return getAttrs();
373375
}
374376

375377
const Decl *Decl::getInnermostDeclWithAvailability() const {

lib/AST/Module.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,15 @@ ASTNode SourceFile::getMacroExpansion() const {
887887
return ASTNode::getFromOpaqueValue(genInfo.astNode);
888888
}
889889

890+
CustomAttr *SourceFile::getAttachedMacroAttribute() const {
891+
if (Kind != SourceFileKind::MacroExpansion)
892+
return nullptr;
893+
894+
auto genInfo =
895+
*getASTContext().SourceMgr.getGeneratedSourceInfo(*getBufferID());
896+
return genInfo.attachedMacroCustomAttr;
897+
}
898+
890899
SourceFile *SourceFile::getEnclosingSourceFile() const {
891900
auto macroExpansion = getMacroExpansion();
892901
if (!macroExpansion)

lib/Sema/TypeCheckAttr.cpp

-39
Original file line numberDiff line numberDiff line change
@@ -7385,45 +7385,6 @@ ValueDecl *RenamedDeclRequest::evaluate(Evaluator &evaluator,
73857385
return renamedDecl;
73867386
}
73877387

7388-
SemanticDeclAttributes
7389-
AttachedSemanticAttrsRequest::evaluate(Evaluator &evaluator, Decl *decl) const {
7390-
// For now, this returns the same thing as 'getAttrs' using
7391-
// the SemanticDeclAttribtues representation.
7392-
SemanticDeclAttributes semanticAttrs;
7393-
for (auto attr : decl->getAttrs()) {
7394-
semanticAttrs.add(attr);
7395-
}
7396-
7397-
auto *parentDecl = decl->getDeclContext()->getAsDecl();
7398-
if (!parentDecl)
7399-
return semanticAttrs;
7400-
7401-
auto parentAttrs = parentDecl->getSemanticAttrs();
7402-
for (auto customAttrConst: parentAttrs.getAttributes<CustomAttr>()) {
7403-
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
7404-
auto customAttrDecl = evaluateOrDefault(
7405-
evaluator,
7406-
CustomAttrDeclRequest{
7407-
customAttr,
7408-
parentDecl->getInnermostDeclContext()
7409-
},
7410-
nullptr);
7411-
if (!customAttrDecl)
7412-
continue;
7413-
7414-
auto macroDecl = customAttrDecl.dyn_cast<MacroDecl *>();
7415-
if (!macroDecl)
7416-
continue;
7417-
7418-
if (!macroDecl->getMacroRoles().contains(MacroRole::MemberAttribute))
7419-
continue;
7420-
7421-
expandAttributes(customAttr, macroDecl, decl, semanticAttrs);
7422-
}
7423-
7424-
return semanticAttrs;
7425-
}
7426-
74277388
template <typename ATTR>
74287389
static void forEachCustomAttribute(
74297390
ValueDecl *decl,

0 commit comments

Comments
 (0)