Skip to content

Fix two memory corruption bugs when modifying SwiftSyntax nodes #1708

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 4 commits into from
Jun 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ let parserEntryFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
"""
extension \(node.kind.syntaxType): SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer { withExtendedLifetime(parser) {} }
let node = parser.\(raw: parserFunction)()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func syntaxNode(emitKind: SyntaxNodeKind) -> SourceFileSyntax {
"""
)
}
StmtSyntax("return SyntaxData.forRoot(raw)")
StmtSyntax("return SyntaxData.forRoot(raw, rawNodeArena: arena)")
}
)

Expand Down Expand Up @@ -173,7 +173,7 @@ func syntaxNode(emitKind: SyntaxNodeKind) -> SourceFileSyntax {
AccessorDeclSyntax(
"""
set(value) {
self = \(node.kind.syntaxType)(data.replacingChild(at: \(raw: index), with: value\(raw: child.isOptional ? "?" : "").raw, arena: SyntaxArena()))
self = \(node.kind.syntaxType)(data.replacingChild(at: \(raw: index), with: value\(raw: child.isOptional ? "?" : "").data, arena: SyntaxArena()))
}
"""
)
Expand Down Expand Up @@ -207,7 +207,7 @@ func syntaxNode(emitKind: SyntaxNodeKind) -> SourceFileSyntax {
collection = RawSyntax.makeLayout(kind: SyntaxKind.\(raw: childNode.varOrCaseName),
from: [element.raw], arena: arena)
}
let newData = data.replacingChild(at: \(raw: index), with: collection, arena: arena)
let newData = data.replacingChild(at: \(raw: index), with: collection, rawNodeArena: arena, allocationArena: arena)
return \(node.kind.syntaxType)(newData)
}
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
let arena = SyntaxArena()
let newRaw = node.raw.layoutView!.replacingLayout(with: Array(newLayout), arena: arena)
// 'withExtendedLifetime' to keep 'SyntaxArena's of them alive until here.
return withExtendedLifetime((arena, rewrittens)) {
Syntax(raw: newRaw).cast(SyntaxType.self)
return withExtendedLifetime(rewrittens) {
Syntax(raw: newRaw, rawNodeArena: arena).cast(SyntaxType.self)
}
} else {
// No child node was rewritten. So no need to change this node as well.
Expand All @@ -342,8 +342,9 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
/// a parent if `node` had one.
public func rewrite(_ node: Syntax) -> Syntax {
let rewritten = self.visit(node)
let arena = SyntaxArena()
return Syntax(node.data.replacingSelf(rewritten.raw, arena: arena))
return withExtendedLifetime(rewritten) {
return Syntax(node.data.replacingSelf(rewritten.raw, rawNodeArena: rewritten.raw.arena, allocationArena: SyntaxArena()))
}
}
"""
)
Expand Down
192 changes: 176 additions & 16 deletions Sources/SwiftParser/generated/Parser+Entry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,129 +43,289 @@ public protocol SyntaxParseable: SyntaxProtocol {

extension AccessorDeclSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseAccessorDecl()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension AttributeSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseAttribute()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension CatchClauseSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseCatchClause()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension ClosureParameterSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseClosureParameter()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension CodeBlockItemSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseNonOptionalCodeBlockItem()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension DeclSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseDeclaration()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension EnumCaseParameterSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseEnumCaseParameter()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension ExprSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseExpression()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension FunctionParameterSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseFunctionParameter()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension GenericParameterClauseSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseGenericParameters()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension MemberDeclBlockSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseMemberDeclList()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension PatternSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parsePattern()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension SourceFileSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseSourceFile()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension StmtSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseStatement()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension SwitchCaseSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseSwitchCase()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

extension TypeSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
// Keep the parser alive so that the arena in which `raw` is allocated
// doesn’t get deallocated before we have a chance to create a syntax node
// from it. We can’t use `parser.arena` as the parameter to
// `Syntax(raw:arena:)` because the node might have been re-used during an
// incremental parse and would then live in a different arena than
// `parser.arena`.
defer {
withExtendedLifetime(parser) {
}
}
let node = parser.parseType()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
return Syntax(raw: raw, rawNodeArena: raw.arena).cast(Self.self)
}
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftSyntax/Raw/RawSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ public struct RawSyntax {
rawData.arenaReference
}

internal var arena: SyntaxArena {
@_spi(RawSyntax)
public var arena: SyntaxArena {
rawData.arenaReference.value
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftSyntax/Raw/RawSyntaxLayoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public struct RawSyntaxLayoutView {
}

/// Creates a new node of the same kind but with children replaced by `elements`.
///
/// The newly created syntax node is allocated in `arena`.
@_spi(RawSyntax)
public func replacingLayout(
with elements: some Collection<RawSyntax?>,
Expand Down
Loading