Skip to content

[5.9] Fix 5.9 LSAN leak #68371

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 2 commits into from
Sep 7, 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
2 changes: 1 addition & 1 deletion lib/ASTGen/Sources/ASTGen/Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public func addQueuedDiagnostic(

/// Render the queued diagnostics into a UTF-8 string.
@_cdecl("swift_ASTGen_renderQueuedDiagnostics")
public func renterQueuedDiagnostics(
public func renderQueuedDiagnostics(
queuedDiagnosticsPtr: UnsafeMutablePointer<UInt8>,
contextSize: Int,
colorize: Int,
Expand Down
13 changes: 13 additions & 0 deletions lib/ASTGen/Sources/ASTGen/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ func allocateUTF8String(
}
}

@_cdecl("swift_ASTGen_freeString")
public func freeString(pointer: UnsafePointer<UInt8>?) {
pointer?.deallocate()
}

/// Diagnostics produced here.
enum ASTGenMacroDiagnostic: DiagnosticMessage, FixItMessage {
case thrownError(Error)
Expand Down Expand Up @@ -410,6 +415,14 @@ func checkMacroDefinition(
}
}

@_cdecl("swift_ASTGen_freeExpansionReplacements")
public func freeExpansionReplacements(
pointer: UnsafeMutablePointer<Int>?,
numReplacements: Int
) {
UnsafeMutableBufferPointer(start: pointer, count: numReplacements).deallocate()
}

// Make an expansion result for '@_cdecl' function caller.
func makeExpansionOutputResult(
expandedSource: String?,
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/PrintingDiagnosticConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extern "C" void swift_ASTGen_addQueuedDiagnostic(
extern "C" void swift_ASTGen_renderQueuedDiagnostics(
void *queued, ptrdiff_t contextSize, ptrdiff_t colorize,
char **outBuffer, ptrdiff_t *outBufferLength);
extern "C" void swift_ASTGen_freeString(const char *str);

// FIXME: Hack because we cannot easily get to the already-parsed source
// file from here. Fix this egregious oversight!
Expand Down Expand Up @@ -1198,6 +1199,7 @@ void PrintingDiagnosticConsumer::flush(bool includeTrailingBreak) {
&renderedString, &renderedStringLen);
if (renderedString) {
Stream.write(renderedString, renderedStringLen);
swift_ASTGen_freeString(renderedString);
}
swift_ASTGen_destroyQueuedDiagnostics(queuedDiagnostics);
queuedDiagnostics = nullptr;
Expand Down
13 changes: 9 additions & 4 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ using namespace swift;
extern "C" void *swift_ASTGen_resolveMacroType(const void *macroType);
extern "C" void swift_ASTGen_destroyMacro(void *macro);

extern "C" void swift_ASTGen_freeString(const char *str);

extern "C" void *swift_ASTGen_resolveExecutableMacro(
const char *moduleName, ptrdiff_t moduleNameLength,
const char *typeName, ptrdiff_t typeNameLength,
Expand All @@ -64,6 +66,9 @@ extern "C" ptrdiff_t swift_ASTGen_checkMacroDefinition(
ptrdiff_t **replacementsPtr,
ptrdiff_t *numReplacements
);
extern "C" void swift_ASTGen_freeExpansionReplacements(
ptrdiff_t *replacementsPtr,
ptrdiff_t numReplacements);

extern "C" ptrdiff_t swift_ASTGen_expandFreestandingMacro(
void *diagEngine, void *macro, uint8_t externalKind,
Expand Down Expand Up @@ -197,8 +202,8 @@ MacroDefinition MacroDefinitionRequest::evaluate(

// Clean up after the call.
SWIFT_DEFER {
free(externalMacroNamePtr);
free(replacements);
swift_ASTGen_freeString(externalMacroNamePtr);
swift_ASTGen_freeExpansionReplacements(replacements, numReplacements);
};

if (checkResult < 0 && ctx.CompletionCallback) {
Expand Down Expand Up @@ -1054,7 +1059,7 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion,
evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy(
{evaluatedSourceAddress, (size_t)evaluatedSourceLength},
adjustMacroExpansionBufferName(*discriminator));
free((void *)evaluatedSourceAddress);
swift_ASTGen_freeString(evaluatedSourceAddress);
break;
#else
ctx.Diags.diagnose(loc, diag::macro_unsupported);
Expand Down Expand Up @@ -1333,7 +1338,7 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy(
{evaluatedSourceAddress, (size_t)evaluatedSourceLength},
adjustMacroExpansionBufferName(*discriminator));
free((void *)evaluatedSourceAddress);
swift_ASTGen_freeString(evaluatedSourceAddress);
break;
#else
attachedTo->diagnose(diag::macro_unsupported);
Expand Down