Skip to content

RequirementMachine: Add more limits to catch runaway computation, and fix a bug #82321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Jun 17, 2025

The fuzzer found a couple of issues where we were missing termination limits to prevent runaway computation.

Also, we incorrectly accepted this protocol below, and proceeded to prove nonsense about it, because of a book-keeping bug in Knuth-Bendix completion. If two rules overlap at more than one position, we would only resolve the critical pair at the first position. Oops!

In a hypothetical world where this protocol is accepted, all of the same() calls should also be accepted because each equivalence holds under the protocol's same-type requirements. (You can work this out by hand even). Today, we diagnose that the last three calls to same() have mismatched argument types.

The correct behavior is to reject M3 because it doesn't actually have a finite complete presentation over any alphabet:

protocol M3 {
  associatedtype A: M3
  associatedtype B: M3
    where A.A.A == A, A.B.B.A == B.B  
}

func same<T>(_: T.Type, _: T.Type) {}

func f<T: M3>(_: T.Type) {
  same(T.A.A.A.self, T.A.self)
  same(T.A.B.B.A.self, T.B.B.self)
  same(T.B.B.A.self, T.A.B.B.self)
  same(T.A.A.B.B.self, T.B.B.self)
  same(T.A.A.B.A.B.B.self, T.B.A.B.B.self)
  same(T.A.A.B.A.B.A.B.B.self, T.B.A.B.A.B.B.self)
  same(T.A.A.B.A.B.A.B.A.B.B.self, T.B.A.B.A.B.A.B.B.self)  
}

…ncrete type requirements

The concrete nesting limit, which defaults to 30, catches
things like A == G<A>. However, with something like
A == (A, A), you end up with an exponential problem size
before you hit the limit.

Add two new limits.

The first is the total size of the concrete type, counting
all leaves, which defaults to 4000. It can be set with the
-requirement-machine-max-concrete-size= frontend flag.

The second avoids an assertion in addTypeDifference() which
can be hit if a certain counter overflows before any other
limit is breached. This also defaults to 4000 and can be set
with the -requirement-machine-max-type-differences= frontend flag.
The implementation of Knuth-Bendix completion has had a subtle
bookkeeping bug since I first wrote the code in 2021.

It is possible for two rules to overlap in more than one position,
but the ResolvedOverlaps set was a set of pairs (i, j), where
i and j are the index of the two rules. So overlaps other than
the first were not considered. Fix this by changing ResolvedOverlaps
to a set of triples (i, j, k), where k is the position in the
left-hand side of the first rule.

The end result is that we would incorrectly accept the protocol M3
shown in the test case. I'm pretty sure the monoid that M3 encodes
does not have a complete presentation over any alphabet, so of
course it should not be accepted here.
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test macOS

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.

1 participant