Skip to content

JAVA-7394 Add the SemSitter frontend module and its protocol client - #6568

Open
rombirli wants to merge 2 commits into
rombirli/semsitter-parser-providerfrom
rombirli/semsitter-protocol-client
Open

rombirli wants to merge 2 commits into
rombirli/semsitter-parser-providerfrom
rombirli/semsitter-protocol-client

Conversation

@rombirli

@rombirli rombirli commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part 2 of 17 of JAVA-7392. Ticket: JAVA-7394.

Intent

Add the new java-frontend-semsitter module and the client that talks to the SemSitter server. Nothing calls it yet.

sequenceDiagram
  participant C as SemSitterClient
  participant S as udg-java-server
  C->>S: hello (JSON line)
  S-->>C: protocol 2.x, anything else is refused
  C->>S: set-settings, full-gen
  C->>S: get-file p/A.java
  S-->>C: JSON header + protobuf Document of the announced byte count
  C->>S: get-types [names]
  S-->>C: JSON header + JSON type declarations
Loading
try (SemSitterClient client = SemSitterClient.start(executable, projectRoot)) {
  client.setSettings(Map.of("jvm.java_version", List.of("17")));
  Document document = client.getFile("p/A.java", 0);
}
  • The protobuf model comes from org.sonarsource.semsitter:analyzer-wire:2.0.0.90994, built with build-maven and promoted by SonarSource/semsitter#5821
  • Maven resolves it from the sonarsource virtual repository on Repox, so CI needs no install step

State on master

Nothing in the plugin calls the client. ECJ parses exactly as today.

Test plan

  • SemSitterClientTest against an in-process fake server: every command, errors, malformed answers, nested documents, and a close that never waits on an unanswered quit
  • ProtocolTest on the protobuf messages, SemSitterClientTest on the framing (header, payload size, truncation, depth limit)
  • SemSitterClientE2ETest runs only with -Dsemsitter.executable

@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

JAVA-7394

Comment thread .github/workflows/build.yml Resolved
@rombirli
rombirli force-pushed the rombirli/semsitter-protocol-client branch from ec0a1c5 to 6ea490c Compare November 25, 2026 10:05
@rombirli
rombirli force-pushed the rombirli/semsitter-protocol-client branch from 6ea490c to 0d30589 Compare November 25, 2026 10:11
@rombirli
rombirli added this pull request to stack #6570 November 25, 2026 10:36
@rombirli
rombirli force-pushed the rombirli/semsitter-protocol-client branch 3 times, most recently from e1dd447 to 7f19e82 Compare November 25, 2026 16:31
@rombirli
rombirli force-pushed the rombirli/semsitter-protocol-client branch from 7f19e82 to 1feaaeb Compare November 25, 2026 17:16
@rombirli
rombirli force-pushed the rombirli/semsitter-protocol-client branch from 1feaaeb to 556a74d Compare November 25, 2026 20:45
@rombirli
rombirli removed this pull request from stack #6570 November 25, 2026 20:47
@rombirli
rombirli added this pull request to stack #6589 November 25, 2026 20:48
@rombirli
rombirli force-pushed the rombirli/semsitter-protocol-client branch from 556a74d to db43847 Compare November 27, 2026 07:55
@rombirli
rombirli force-pushed the rombirli/semsitter-protocol-client branch from db43847 to 0b15779 Compare November 27, 2026 09:24
@gitar-bot

gitar-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 🟡 Medium risk 7 closed / 7 findings

The new SemSitter client launches a server process and decodes its protocol payloads, so framing or lifecycle errors could interrupt parsing when integrated.

Adds the java-frontend-semsitter module with SemSitterClient, which speaks the SemSitter server protocol over JSON-line headers and protobuf payloads, and installs analyzer-wire in CI before dependency resolution. Nothing calls the client yet, so ECJ parsing is unchanged. Resolved this session: lock bypass in getFile/getTypes, missing analyzer-wire in license-check and dogfooding builds, unchecked truncated payloads, unbounded protobuf recursion, ProtocolTest covering unused delimited framing, close() blocking on an unanswered quit, and malformed headers surfacing as RuntimeException instead of IOException. No open issues remain.

✅ 7 closed
✅ Bug: getFile/getTypes bypass the lock that send() takes

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:125 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:139 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:161-168
send is synchronized, so the client is clearly meant to be shared between threads. But getFile and getTypes call exchange directly and never take the monitor. exchange increments nextId and does a write-flush-read on the shared streams that must not be interrupted. If getFile runs at the same time as hasPackage/fullGen/close (or another getFile), the threads can get the same id, their request lines can interleave, or one thread can read the other's header and payload bytes. That throws the 'answered request X instead of Y' error, or leaves the stream out of sync for every later call. Move synchronized onto exchange, or make every public entry point synchronized.

✅ Bug: License-check and dogfooding builds don't install analyzer-wire

📄 .github/workflows/build.yml:45-50 📄 .github/workflows/build.yml:469-471
The PR adds java-frontend-semsitter to the root reactor, and that module depends on analyzer-wire:2.0.0. The PR's premise is that this artifact has to be installed by hand from sonarsource-public-qa. Only the build, test-analyze and qa-os-win jobs got the new install step. Two other jobs still resolve the root reactor without it: custom-rules-license-check runs mvn org.codehaus.mojo:license-maven-plugin:aggregate-add-third-party from the repo root, which resolves the dependencies of every module, and unified-dogfooding.yml runs build-maven on the root project. Both jobs will fail to resolve org.sonarsource.semsitter:analyzer-wire:2.0.0. Add the install step to both. In the license job it can go after config-maven, which already exports ARTIFACTORY_URL/USERNAME/ACCESS_TOKEN. In the dogfooding job it needs the vault credentials, as in build.

✅ Edge Case: Truncated payload is accepted without a length check

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:176-178
fromServer.readNBytes(n) returns fewer than n bytes when the server's stdout ends early, for example when the server crashes in the middle of a write. The client never compares the returned length with payload_bytes. getTypes then hands truncated JSON to its caller. getFile can even parse a valid but incomplete Document if the cut lands on a field boundary, which silently drops nodes or symbols. Throw an IOException when payload.length != payload_bytes.

✅ Edge Case: Unlimited protobuf recursion turns deep trees into StackOverflowError

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:125-127
setRecursionLimit(Integer.MAX_VALUE) removes protobuf's depth guard. Protobuf parses nested messages recursively, so a deep enough tree now overflows the thread stack instead of failing with InvalidProtocolBufferException. Real Java code can produce such trees, for example generated code with thousands of chained + concatenations, which nest left-deep as binary expressions. StackOverflowError is an Error, not an IOException, so it escapes the throws IOException contract that callers will handle. The test only covers depth 1,000. Use a large but finite limit, or catch the overflow and rethrow it as an IOException.

✅ Quality: ProtocolTest tests delimited framing, which the client never uses

📄 java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/ProtocolTest.java:78 📄 java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/ProtocolTest.java:90 📄 java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/ProtocolTest.java:95-99 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterClient.java:125-127
The PR says ProtocolTest covers the wire framing. But it round-trips messages with writeDelimitedTo/parseDelimitedFrom, which put a varint length before the message. The real protocol sends a JSON header with payload_bytes followed by the raw message bytes, and SemSitterClient.getFile reads them with Document.parseFrom and no length prefix. So the test only checks that the generated protobuf classes serialize and parse back, not the framing the client relies on. It would still pass if the client's framing were wrong.

...and 2 more closed from earlier reviews

Verification Rules 3/3 met PRs 1 inspected

Functional validation No results

Rules 3 passed 2 not applicable

✅ Internal artifact registries
✅ Release secrets usage
✅ Repository license compliance

Broader impact PRs 1 inspected
Cross-PR 1 PR inspected 0 open impacts

PR 6569: JAVA-7395 Run a SemSitter parsing session with a per-file ECJ fallback Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown
Contributor

This branch has not been deployed

No deployments
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