This is an archive of the discontinued LLVM Phabricator instance.

AArch64: avoid error when truncating MCExprs
ClosedPublic

Authored by t.p.northover on Sep 22 2020, 3:51 AM.

Details

Summary

A while back, a diagnostic was added to object file generation codepath for when an MCExpr overflows its size. So for assembly something like

.long a - b + 5000000000

(except produced from CodeGen because the assembler would reject that). Unfortunately I don't think it's really valid when looked at from the IR level. We've seen an obfuscator (naturally) produce code looking something like this:

@other = global i32 42
@var = global i32 sub(i32 646102975,
                      i32 add (i32 trunc(i64 sub(i64 ptrtoint(i32* @var to i64),
                                                         i64 ptrtoint(i32* @other to i64)) to i32),
                               i32 3432360802))

where the resulting SymB - SymA + Val has a Val outside the 32-bit range (because MCExprs are always evaluated at 64-bits precision). But LLVM IR is 2s-complement unless you add no-wrap flags, so I think that calculation is legitimate and has to be allowed.

Diff Detail

Event Timeline

t.p.northover created this revision.Sep 22 2020, 3:51 AM
t.p.northover requested review of this revision.Sep 22 2020, 3:51 AM

Bother, forgot to add the test.

fhahn accepted this revision.Sep 22 2020, 6:04 AM
fhahn added a subscriber: fhahn.

It looks like it is indeed possible to generate fixup values that wrap around in valid LLVM IR and truncating the computed value to the fixup width should match the computation in LLVM IR.

I am not sure there is a more targeted way to detect invalid transforms in the backend that might lead to invalid offsets, but I think for now it would be best to remove the check to make sure we can handle valid IR.

LGTM, but please wait a day or so in case there is additional feedback.

This revision is now accepted and ready to land.Sep 22 2020, 6:04 AM
t.p.northover closed this revision.Oct 8 2020, 3:55 AM

Thanks Florian, committed as 38348fa2654.