Skip to content

Conversation

nvjle
Copy link
Contributor

@nvjle nvjle commented Mar 19, 2024

Currently operand constraint checks on "$dest = $src" are inadvertently accepting any token that contains "=". This has surprising results, e.g, "$dest != $src" is accepted as a constraint but then treated as "=".

This patch ensures that only exactly the token "=" is accepted.

Currently operand constraint checks on "$dest = $src" are inadvertently
accepting any token that contains "=". This has surprising results, e.g,
"$dest != $src" is accepted as a constraint but then treated as "=".

This patch ensures that only exactly the token "=" is accepted.
@nvjle nvjle requested review from davidchisnall and topperc March 19, 2024 20:12
StringRef::size_type pos = CStr.find_first_of('=');
if (pos == StringRef::npos)
if (pos == StringRef::npos || CStr.find_first_of(" \t", pos) != (pos + 1) ||
CStr.find_last_of(" \t", pos) != (pos - 1))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for pos to be 0 if the constraint starts with =? Would CStr.find_last_of(" \t", pos) return StringRef::npos in that case? Is that equal to (pos-1) since StringRef::npos is the largest unsigned number.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won’t this be caught by a different check finding the two operands of an infix operator? If a constraint starts with =, the LHS is missing.

Copy link
Contributor Author

@nvjle nvjle Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right @topperc -- for the corner case =, the behavior is as you describe, so that npos == pos-1 and the condition on pos+1 fires instead. In other cases, we'll drop through to the following check on the operands as @davidchisnall mentions.

I've added these and other corner cases to the ConstraintChecking8.td test file (consolidated).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wrapping of pos - 1 to alias with StringRef::npos feels weird to me. Should we add pos == 0 as one of the conditions in this if?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that's fine too. Commit coming shortly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (pos == 0 check).

nvjle added 2 commits March 20, 2024 09:17
And remove ConstraintChecking9.td.
unsigned wraparound behavior for the `pos-1` check.
Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nvjle nvjle merged commit f676e84 into llvm:main Mar 20, 2024
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
Currently operand constraint checks on "$dest = $src" are inadvertently
accepting any token that contains "=". This has surprising results, e.g,
"$dest != $src" is accepted as a constraint but then treated as "=".

This patch ensures that only exactly the token "=" is accepted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants