forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 367
[lldb] Don't invent an alignment if not encoded #13566
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
Open
augusto2112
wants to merge
2
commits into
swiftlang:stable/21.x
Choose a base branch
from
augusto2112:embed-multipayload-enum-alignment
base: stable/21.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
lldb/test/API/lang/swift/embedded/multipayload_enum_alignment/Makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| SWIFT_SOURCES := main.swift | ||
|
|
||
| include Makefile.rules |
77 changes: 77 additions & 0 deletions
77
.../swift/embedded/multipayload_enum_alignment/TestSwiftEmbeddedMultiPayloadEnumAlignment.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """ | ||
| Test that a multi-payload enum's alignment is not fabricated from its | ||
| DW_AT_byte_size in embedded Swift. | ||
| """ | ||
|
|
||
| import lldb | ||
| from lldbsuite.test.lldbtest import * | ||
| from lldbsuite.test.decorators import * | ||
| import lldbsuite.test.lldbutil as lldbutil | ||
|
|
||
|
|
||
| class TestSwiftEmbeddedMultiPayloadEnumAlignment(TestBase): | ||
| def setup_test(self): | ||
| self.build() | ||
| self.runCmd("setting set symbols.swift-enable-ast-context false") | ||
| _, _, thread, _ = lldbutil.run_to_source_breakpoint( | ||
| self, "break here", lldb.SBFileSpec("main.swift") | ||
| ) | ||
| return thread.GetSelectedFrame() | ||
|
|
||
| @skipUnlessDarwin | ||
| @skipUnlessEmbeddedSwift | ||
| # The expected sizes and offsets assume Int64 is 8-byte aligned, which holds | ||
| # on arm64 (including arm64_32) and x86_64, but not on every 32-bit ABI. | ||
| @skipIf(archs=no_match(["arm64", "x86_64"])) | ||
| @swiftTest | ||
| def test_enum_stride(self): | ||
| frame = self.setup_test() | ||
|
|
||
| wide_enum = frame.FindVariable("w").GetType() | ||
| self.assertIn("WideEnum", wide_enum.GetName()) | ||
| self.assertEqual(wide_enum.GetByteSize(), 34) | ||
|
|
||
| pairs = frame.FindVariable("pairs") | ||
| self.assertTrue(pairs.IsValid(), "pairs is valid") | ||
| self.assertEqual(pairs.GetType().GetByteSize(), 74) | ||
| self.assertEqual(pairs.GetNumChildren(), 2) | ||
|
|
||
| base = pairs.GetLoadAddress() | ||
| self.assertNotEqual(base, lldb.LLDB_INVALID_ADDRESS) | ||
| self.assertEqual(pairs.GetChildAtIndex(0).GetLoadAddress() - base, 0) | ||
| self.assertEqual( | ||
| pairs.GetChildAtIndex(1).GetLoadAddress() - base, | ||
| 40, | ||
| "WideEnum's stride is alignUp(34, 8) = 40, not alignUp(34, 34) = 66", | ||
| ) | ||
|
|
||
| self.expect( | ||
| "frame variable pairs", | ||
| substrs=["first", "pair", "0 = 7", "1 = 8", "second", "0 = 9", "1 = 10"], | ||
| ) | ||
|
|
||
| @skipUnlessDarwin | ||
| @skipUnlessEmbeddedSwift | ||
| @skipIf(archs=no_match(["arm64", "x86_64"])) | ||
| @swiftTest | ||
| def test_enum_alignment(self): | ||
| frame = self.setup_test() | ||
|
|
||
| prefixed = frame.FindVariable("prefixed") | ||
| self.assertTrue(prefixed.IsValid(), "prefixed is valid") | ||
| self.assertEqual(prefixed.GetType().GetByteSize(), 42) | ||
|
|
||
| base = prefixed.GetLoadAddress() | ||
| self.assertNotEqual(base, lldb.LLDB_INVALID_ADDRESS) | ||
| payload = prefixed.GetChildMemberWithName("payload") | ||
| self.assertTrue(payload.IsValid(), "payload is valid") | ||
| self.assertEqual( | ||
| payload.GetLoadAddress() - base, | ||
| 8, | ||
| "WideEnum's alignment is 8 (the max of the payload alignments), not 34", | ||
| ) | ||
|
|
||
| self.expect( | ||
| "frame variable prefixed", | ||
| substrs=["tag = 3", "payload", "pair", "0 = 11", "1 = 12"], | ||
| ) |
58 changes: 58 additions & 0 deletions
58
lldb/test/API/lang/swift/embedded/multipayload_enum_alignment/main.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Every size and offset this test checks is expressed in explicitly sized types | ||
| // (Int64/UInt8/Int8), so none of them depend on the pointer width. They do | ||
| // depend on Int64 being 8-byte aligned, which the test gates on. | ||
|
|
||
| // A 33-byte payload: four Int64s (alignment 8) plus a trailing UInt8. Its | ||
|
augusto2112 marked this conversation as resolved.
|
||
| // DW_AT_byte_size (33) is deliberately not a power of two and much larger than | ||
| // its real alignment (8). | ||
| struct Wide { | ||
| var a: Int64 = 1 | ||
| var b: Int64 = 2 | ||
| var c: Int64 = 3 | ||
| var d: Int64 = 4 | ||
| var e: UInt8 = 5 | ||
| } | ||
|
|
||
| // A multi-payload enum: emitted as a DW_TAG_structure_type whose first child is | ||
| // a DW_TAG_variant_part, with DW_AT_byte_size 34 and *no* DW_AT_alignment. Its | ||
| // authoritative layout is alignment 8 (the max of its payload alignments) and | ||
| // stride alignUp(34, 8) = 40. | ||
| enum WideEnum { | ||
| case wide(Wide) | ||
| case pair(Int64, Int64) | ||
| case none | ||
| } | ||
|
|
||
| // Two enums back to back: the offset of `second`, and the whole struct's size, | ||
| // are a direct readout of the enum's stride. Correct: 40 + 34 = 74. | ||
| struct EnumPair { | ||
| var first: WideEnum | ||
| var second: WideEnum | ||
| } | ||
|
|
||
| // A one-byte prefix followed by an enum: the offset of `payload` is a readout | ||
| // of the enum's alignment. Correct: the payload lands at alignUp(1, 8) = 8, so | ||
| // the struct's size is 8 + 34 = 42. A fabricated alignment of 34 rounded 1 up | ||
| // to 2 instead (the mask-based round-up is only valid for powers of two). | ||
| struct PrefixedEnum { | ||
| var tag: Int8 | ||
| var payload: WideEnum | ||
| } | ||
|
|
||
| @inline(never) | ||
| func blackHole(_ x: Int64) {} | ||
|
|
||
| func f() { | ||
| let w = WideEnum.wide(Wide()) | ||
| let pairs = EnumPair(first: .pair(7, 8), second: .pair(9, 10)) | ||
| let prefixed = PrefixedEnum(tag: 3, payload: .pair(11, 12)) | ||
|
|
||
| if case .pair(let x, _) = pairs.second { blackHole(x) } | ||
| if case .pair(let y, _) = prefixed.payload { blackHole(y) } | ||
| if case .wide(let z) = w { blackHole(z.a) } | ||
|
|
||
| let s = StaticString("break here") | ||
| print(s) // break here | ||
| } | ||
|
|
||
| f() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still confused by the logic here:
byte_size?:8but not on 606. I guess becuase 0 is still a valid valuealignTo(0, x)?I think this all gets clearer if you have an if (byte_size == 0) block that handles that case separately?