Skip to content

[Linux][Backtracing] Fix a couple of backtracer issues. #67430

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
Jul 21, 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
6 changes: 5 additions & 1 deletion stdlib/public/Backtracing/Elf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,11 @@ class ElfImage<SomeImageSource: ImageSource,
done = true
}

prevState = state
if state.endSequence {
prevState = nil
} else {
prevState = state
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions stdlib/public/Backtracing/FramePointerUnwinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public struct FramePointerUnwinder<C: Context, M: MemoryReader>: Sequence, Itera
var asyncContext: Address
var first: Bool
var isAsync: Bool
var done: Bool

#if os(Linux)
var elf32Cache: [Int:Elf32Image<FileImageSource>] = [:]
Expand All @@ -44,6 +45,7 @@ public struct FramePointerUnwinder<C: Context, M: MemoryReader>: Sequence, Itera
fp = Address(context.framePointer)
first = true
isAsync = false
done = false
asyncContext = 0
reader = memoryReader

Expand Down Expand Up @@ -134,6 +136,10 @@ public struct FramePointerUnwinder<C: Context, M: MemoryReader>: Sequence, Itera
}

public mutating func next() -> Backtrace.Frame? {
if done {
return nil
}

if first {
first = false
pc = stripPtrAuth(pc)
Expand All @@ -149,6 +155,7 @@ public struct FramePointerUnwinder<C: Context, M: MemoryReader>: Sequence, Itera
if strippedFp == 0
|| !Context.isAlignedForStack(framePointer:
Context.Address(strippedFp)) {
done = true
return nil
}

Expand All @@ -158,10 +165,12 @@ public struct FramePointerUnwinder<C: Context, M: MemoryReader>: Sequence, Itera
as: Address.self))
next = try reader.fetch(from: Address(strippedFp), as: Address.self)
} catch {
done = true
return nil
}

if next <= fp || pc == 0 {
done = true
return nil
}

Expand All @@ -173,6 +182,7 @@ public struct FramePointerUnwinder<C: Context, M: MemoryReader>: Sequence, Itera

isAsync = true
if !fetchAsyncContext() {
done = true
return nil
}
}
Expand All @@ -183,6 +193,7 @@ public struct FramePointerUnwinder<C: Context, M: MemoryReader>: Sequence, Itera
let strippedCtx = stripPtrAuth(asyncContext)

if strippedCtx == 0 {
done = true
return nil
}

Expand All @@ -197,6 +208,7 @@ public struct FramePointerUnwinder<C: Context, M: MemoryReader>: Sequence, Itera
next = Address(next32)
pc = stripPtrAuth(Address(pc32))
} catch {
done = true
return nil
}
#else
Expand All @@ -206,6 +218,7 @@ public struct FramePointerUnwinder<C: Context, M: MemoryReader>: Sequence, Itera
next = try reader.fetch(from: strippedCtx, as: Address.self)
pc = stripPtrAuth(try reader.fetch(from: strippedCtx + 8, as: Address.self))
} catch {
done = true
return nil
}

Expand Down