Page MenuHomePhabricator

[PSV] Make the API for TargetCustom values not trigger UB
ClosedPublic

Authored by kariddi on Aug 17 2018, 10:16 AM.

Details

Summary

Hi,

this patch tries to fix a problem with the API for TargetCustom PseudoSourceValue types that is currently exhibiting UB if you try to use it in the intended way with more than 1 PseudoSourceValue custom kind.

How the API is intended to be used (by looking at getTargetCustom() or the printCustom() method) is that passing a value >= PSVKind::TargetCustom as the "Kind" parameter of the PseudoSourceValue constructor is a way to specify that our PSV is of a custom kind. So, if you have 2 different custom PSV types you should pass as the Kind parameter for the first one PSVKind::TargetCustom and for the second on "PSVKind::TargetCustom+1"

getTargetCustom() will return 0 if the PSV is not custom, 1 if it is the first custom PSV, 2 if it is the second

The problem is that PSVKind::TargetCustom+1 is not an entry of the PSVKind enum and passing that as a value to the constructor is theoretically undefined behavior and is reported as such by the UB sanitizer.

This results in the fact that having more than 1 PSV custom kind is actually not possible without triggering UB.

This patch tries to address it to use "unsigned" (the underlying value of the enum) to store and pass around the Kind instead of the PSVKind enum type.

This patch is NFC otherwise

Diff Detail

Repository
rL LLVM

Event Timeline

kariddi created this revision.Aug 17 2018, 10:16 AM
arsenm accepted this revision.Aug 20 2018, 9:41 AM

LGTM

This revision is now accepted and ready to land.Aug 20 2018, 9:41 AM
This revision was automatically updated to reflect the committed changes.

Thanks Matt, committed r340200