This is an archive of the discontinued LLVM Phabricator instance.

[Thumb] Validate branch target for CBZ/CBNZ instructions.
ClosedPublic

Authored by prakhar on Aug 9 2016, 5:27 AM.

Details

Summary

The assembler currently does not check the branch target for CBZ/CBNZ
instructions, which only permit branching forwards with a positive offset. This
adds validation for the branch target to ensure negative PC-relative offsets are
not encoded into the instruction, whether specified as a literal or as an
assembler symbol.

Diff Detail

Event Timeline

prakhar updated this revision to Diff 67323.Aug 9 2016, 5:27 AM
prakhar retitled this revision from to [Thumb] Validate branch target for CBZ/CBNZ instructions..
prakhar updated this object.
prakhar added reviewers: rengolin, t.p.northover.
prakhar added a subscriber: llvm-commits.
prakhar set the repository for this revision to rL LLVM.Aug 9 2016, 5:37 AM
olista01 added inline comments.
lib/Target/ARM/AsmParser/ARMAsmParser.cpp
863

Why don't you use isUnsignedOffset, which would also check the upper limit and alignment?

lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp
582

We should also check the upper bound and alignment here.

prakhar updated this revision to Diff 67331.Aug 9 2016, 6:52 AM
prakhar marked an inline comment as done.

Use isUnsignedOffset() directly and add upper bound checking with tests.

prakhar marked an inline comment as done.Aug 9 2016, 6:54 AM
olista01 added inline comments.Aug 9 2016, 7:34 AM
lib/Target/ARM/AsmParser/ARMAsmParser.cpp
6699

These instructions accept a 6-bit immediate (shifted by one bit), not 5-bit. This should allow all even numbers in the range 0 to 126 (inclusive at both ends).

lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp
582

We should also be checking that the low bit is clear (and see my comment above about the immediate being 6 bits).

test/MC/ARM/thumb-diagnostics.s
241

It would be better to test as close to the bounds as possible, and add some tests for valid immediates close to the boundary. I think these should cover it:
-2 (invalid)
0 (lowest valid)
17 (invalid due to alignment)
126 (highest valid)
128 (invalid)

prakhar updated this revision to Diff 67495.Aug 10 2016, 3:05 AM

Immediate operand is 6 bits and should have cleared LSB.

prakhar marked 3 inline comments as done.Aug 10 2016, 3:51 AM
olista01 accepted this revision.Aug 12 2016, 5:42 AM
olista01 added a reviewer: olista01.

LGTM

This revision is now accepted and ready to land.Aug 12 2016, 5:42 AM
This revision was automatically updated to reflect the committed changes.