This is an archive of the discontinued LLVM Phabricator instance.

Fix UB in EmulateInstructionARM64.cpp
ClosedPublic

Authored by aprantl on Jun 1 2020, 2:22 PM.

Details

Summary

This fixes an unhandled signed integer overflow in AddWithCarry() by using the llvm::checkedAdd() function. Thats to @vsk for the suggestion!

Diff Detail

Event Timeline

aprantl created this revision.Jun 1 2020, 2:22 PM
vsk added a comment.Jun 1 2020, 2:48 PM

Any chance we can export AddWithCarry via EmulateInstructionARM64.h, then add a unit test in TestArm64InstEmulation.cpp?

vsk added a comment.Jun 1 2020, 2:53 PM

I should add: otherwise, this looks good!

lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
92

The docs [1] say 'integer signed_sum = SInt(x) + SInt(y) + UInt(carry_in)', but I bet checkedAdd doesn't support that, and anyway carry_in is 1-bit so it doesn't matter.

[1] https://developer.arm.com/docs/ddi0596/e/shared-pseudocode-functions/shared-functionsinteger-pseudocode#impl-shared.AddWithCarry.3

Nice cleanup of a long standing undefined behavior warning.

shafik added a subscriber: shafik.Jun 1 2020, 3:21 PM
shafik added inline comments.
lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
25

Why? also should be cstdlib in C++.

davide added a subscriber: davide.Jun 1 2020, 3:53 PM
davide added inline comments.
lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
25

It's just something shuffled around, probably a side effect of clang-format.

davide added a comment.Jun 1 2020, 3:55 PM

This is great Adrian. I feel like there are several other instances of additions that should use llvm::checkAdd here instead of +. Is this the only UB triggered in the testsuite?

aprantl marked an inline comment as done.Jun 1 2020, 5:00 PM
aprantl added inline comments.
lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
92

Am I misreading this or are we also setting the C(arry) flag inverted according to the documentation?

This is great Adrian. I feel like there are several other instances of additions that should use llvm::checkAdd here instead of +. Is this the only UB triggered in the testsuite?

It's the only UB caught by the green dragon / ci.swift.org bots.

This revision was not accepted when it landed; it landed in state Needs Review.Jun 1 2020, 6:26 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptJun 1 2020, 6:26 PM
Herald added a subscriber: mgorny. · View Herald Transcript
vsk added inline comments.Jun 2 2020, 5:57 PM
lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
92

Oh wow.. nice catch.