regex: model Pattern/Matcher for JBMC's intercepted regex machinery - #40
Open
tautschnig wants to merge 1 commit into
Open
regex: model Pattern/Matcher for JBMC's intercepted regex machinery#40tautschnig wants to merge 1 commit into
tautschnig wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves the java.util.regex model to better align with JBMC’s intercepted regex machinery by making Pattern retain compile flags and by returning a stateful Matcher that can delegate matching operations to JBMC hooks/intercepts.
Changes:
- Store compile flags in
Patternand return a realMatcherfromPattern.matcher(...). - Rework
Pattern.matches(...)to delegate throughcompile(regex).matcher(input).matches(). - Model
Matcherstate (pattern, input text, and staleness flags) and implementmatches(),lookingAt(), andfind()with precision-degrading semantics.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/main/java/java/util/regex/Pattern.java | Stores compile flags, returns real Matcher, and rewires Pattern.matches to delegate through the matcher. |
| src/main/java/java/util/regex/Matcher.java | Introduces modeled matcher state and implements core query methods (matches, find, lookingAt) with staleness tracking. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Pattern now stores its flags and constructs a real Matcher (previously matcher() returned a field-less nondet object, and the constructor ASSUMED the pattern contained no regex metacharacters -- making downstream paths vacuous for any real regex). Pattern.matches delegates to compile(regex).matcher(input).matches(). Matcher carries (pattern, text-as-String) plus two staleness flags with JDK state semantics: cproverImprecise (the region no longer covers the whole input -> every query degrades to a sound nondet) and cproverFindConsumed (a find() advanced the unmodelled match position -> only find() degrades; matches()/lookingAt() are position-independent per the JDK). reset()/ reset(CharSequence) restore precision; usePattern retains the position (find degrades) but keeps the region; region()/find(int) poison their respective flags. matches() delegates to the intercepted String.matches; lookingAt()/find() delegate to the static cproverLookingAt/cproverFind hooks JBMC lowers to its Java-dialect regex solver functions. Non-zero compile flags fall back to nondet (flags change match semantics). Argument-validation exceptions are modelled fail-fast per the real JDK (all five verified against a real JVM): Pattern.compile(null) and hence Pattern.matches(null, ...) throw NullPointerException (the real constructor dereferences the pattern); Pattern.matcher(null) throws NullPointerException; Matcher.usePattern(null) throws IllegalArgumentException per its contract; Matcher.reset((CharSequence) null) throws NullPointerException. The null-FIELD guards inside the query methods are a separate regime and remain: they cover nondet-generated Matcher objects from the object factory, whose null fields are legitimate states (degraded to a sound nondet), not caller errors. Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
tautschnig
force-pushed
the
regex-matcher-model
branch
from
July 28, 2026 23:34
deabf32 to
373e8ac
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pattern now stores its flags and constructs a real Matcher (previously matcher() returned a field-less nondet object, and the constructor ASSUMED the pattern contained no regex metacharacters -- making downstream paths vacuous for any real regex). Pattern.matches delegates to compile(regex).matcher(input).matches().
Matcher carries (pattern, text-as-String) plus two staleness flags with JLS state semantics: cproverImprecise (the region no longer covers the whole input -> every query degrades to a sound nondet) and cproverFindConsumed (a find() advanced the unmodelled match position -> only find() degrades; matches()/lookingAt() are position-independent per the JLS). reset()/ reset(CharSequence) restore precision; usePattern retains the position (find degrades) but keeps the region; region()/find(int) poison their respective flags. matches() delegates to the intercepted String.matches; lookingAt()/find() delegate to the static cproverLookingAt/cproverFind hooks JBMC lowers to its Java-dialect regex solver functions. Non-zero compile flags fall back to nondet (flags change match semantics).