This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho][nfc] define command UNWIND_MODE_MASK for convenience and rewrite mode-mask checking logic for clarity
ClosedPublic

Authored by oontvoo on Oct 6 2022, 6:15 AM.

Details

Summary

Rewrite the checking logic in InputFiles.cpp for clarity.
The previous form is currently "harmless" and happened to work but may not in the future:

Consider the struct: (for x86-64, but same issue can be said for the ARM/64 families):

UNWIND_X86_64_MODE_MASK                    = 0x0F000000,
UNWIND_X86_64_MODE_RBP_FRAME               = 0x01000000,
UNWIND_X86_64_MODE_STACK_IMMD              = 0x02000000,
UNWIND_X86_64_MODE_STACK_IND               = 0x03000000,
UNWIND_X86_64_MODE_DWARF                   = 0x04000000,

Previously, we were doing: (encoding & MODE_DWARF) == MODE_DWARF

As soon as a new UNWIND_X86_64_MODE_FOO = 0x05000000 is defined, then the check above would always return true for encoding=MODE_FOO (because (0b0101 & 0b0100) == 0b0100 )

Diff Detail

Event Timeline

oontvoo created this revision.Oct 6 2022, 6:15 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptOct 6 2022, 6:15 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
oontvoo requested review of this revision.Oct 6 2022, 6:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 6 2022, 6:15 AM
oontvoo updated this revision to Diff 465724.Oct 6 2022, 7:24 AM

moved static_assert to outside the loop

oontvoo updated this revision to Diff 465753.Oct 6 2022, 8:34 AM

added missing include

int3 added a subscriber: int3.Oct 6 2022, 10:41 AM

Please put the "drive-by cleanup" in a separate diff... it would have made it a lot easier to figure out what this diff is doing

because there's no 0x05000000 in the enum set, but as soon as it's defined, it'll stop working

I can see why there might be an issue if 0x11000000 was a defined mode, but I don't think 0x05000000 would be a problem?

lld/MachO/InputFiles.cpp
1072–1075

can we hoist to the top level scope and then define a constexpr UNWIND_MODE_MASK = ...?

then we can use in it canFoldEncoding too (and remove the first static_assert() in that function)

lld/MachO/UnwindInfoSection.cpp
423–426

Unless there's an obvious runtime issue, I would rather we keep things the way they are. Is there any observable behavioral difference with this change?

DWARF mode encodings will almost (always?) never get folded anyway since each one includes an offset to the corresponding EH frame. And since we don't do ICF on EH frames, they should all be distinct...

oontvoo updated this revision to Diff 467824.Oct 14 2022, 10:21 AM
oontvoo marked 2 inline comments as done.

updated diff per review

lld/MachO/UnwindInfoSection.cpp
423–426

(reverted this for now - will send a different patch w.r.t dwarf handling)

This patch will now be mostly NFC

oontvoo retitled this revision from [lld-macho] Don't fold DWARFs with CompactUnwinds to [lld-macho][nfc] define command UNWIND_MODE_MASK for convenience and rewrite mode-mask checking logic for clarity.Oct 14 2022, 10:21 AM
oontvoo edited the summary of this revision. (Show Details)
oontvoo updated this revision to Diff 467827.Oct 14 2022, 10:25 AM
oontvoo retitled this revision from [lld-macho][nfc] define command UNWIND_MODE_MASK for convenience and rewrite mode-mask checking logic for clarity to [lld-macho][nfc] define command UNWIND_MODE_MASK for convenience and rewrite mode-mask checking logic for clarity.

rebase

oontvoo updated this revision to Diff 467830.Oct 14 2022, 10:27 AM

use UNWIND_MODE_MASK everywhere

int3 accepted this revision.Oct 14 2022, 10:40 AM

The previous form is currently "harmless" and happened to work (because there's no 0x05000000 in the enum set, but as soon as it's defined, it'll stop working)

I guess we can remove this now? Or if you want to be more precise, I think it happens to work because we don't usually get values like 0x04111111 in the input, so there's usually nothing to mask out

lld/MachO/InputFiles.cpp
76–77

don't think this is needed any more

1072–1075

we can rm this now that we're doing it at the top level

This revision is now accepted and ready to land.Oct 14 2022, 10:40 AM
oontvoo marked 2 inline comments as done.Oct 14 2022, 11:20 AM

The previous form is currently "harmless" and happened to work (because there's no 0x05000000 in the enum set, but as soon as it's defined, it'll stop working)

I can see why there might be an issue if 0x11000000 was a defined mode, but I don't think 0x05000000 would be a problem?
<....>
I guess we can remove this now? Or if you want to be more precise, I think it happens to work because we don't usually get values like 0x04111111 in the input, so there's usually nothing to mask out

Sorry, I'd missed your question about the 0x05000000.
Consider the struct: (for x86-64, but same issue can be said for the ARM/64 families):

UNWIND_X86_64_MODE_MASK                              = 0x0F000000,
UNWIND_X86_64_MODE_RBP_FRAME                   = 0x01000000,
UNWIND_X86_64_MODE_STACK_IMMD                = 0x02000000,
UNWIND_X86_64_MODE_STACK_IND                    = 0x03000000,
UNWIND_X86_64_MODE_DWARF                          = 0x04000000,

Previously, we were doing: (encoding & MODE_DWARF) == MODE_DWARF

What I meant was, as soon as a new UNWIND_X86_64_MODE_FOO = 0x05000000 is defined, then the check above would always return true for encoding=MODE_FOO (because (0b0101 & 0b0100) == 0b0100
So no, the explanation still sticks :)

int3 added a comment.Oct 14 2022, 12:05 PM

d'oh, I get it now, thanks. Could you put that in the commit message? Thanks!

oontvoo edited the summary of this revision. (Show Details)Oct 14 2022, 12:14 PM
oontvoo updated this revision to Diff 467881.Oct 14 2022, 12:15 PM
oontvoo edited the summary of this revision. (Show Details)

updateded commit msg

This revision was landed with ongoing or failed builds.Oct 14 2022, 12:17 PM
This revision was automatically updated to reflect the committed changes.