Skip to content

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

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

rombirli wants to merge 5 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: answers, the client does not look at the protocol version
  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, declared in the module only, at any version of major 2 ([2,3-alpha), which also excludes the pre-releases of 3). It is built with build-maven and promoted by SonarSource/semsitter#5821
  • The client never checks the protocol version. ProtocolTest checks that the wire classes still read a document written by the real server (a snapshot)
  • 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 reads a snapshot of a real server document, 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 2 times, most recently from 0b15779 to b267f48 Compare November 27, 2026 10:46
@datadog-sonarsource

datadog-sonarsource Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Pipelines

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: a5e6114 | Docs | Give us feedback!

@rombirli
rombirli force-pushed the rombirli/semsitter-protocol-client branch from 04712e9 to 1230f53 Compare November 30, 2026 07:24
Comment thread pom.xml Resolved

@rombirli rombirli left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread pom.xml Resolved
@rombirli
rombirli force-pushed the rombirli/semsitter-protocol-client branch from 476c2ff to a5e6114 Compare November 30, 2026 15:51
@sonarqube-next

Copy link
Copy Markdown
Contributor

@gitar-bot

gitar-bot Bot commented Nov 30, 2026 •

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

The new SemSitter subprocess client adds non-trivial protocol and document handling, but no callers are added, limiting its current runtime impact.

Adds the java-frontend-semsitter module and SemSitterClient for speaking the SemSitter server protocol, with analyzer-wire declared in the module only. Seven findings were resolved, including the getFile/getTypes lock bypass, the unchecked truncated payload, and close() blocking on an unanswered quit. No open issues remain.

✅ 8 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 3 more closed from earlier reviews

Verification Rules 3/3 met Knowledge ✅ 1 Repos 4 inspected · PRs 4 inspected

Functional validation No results

Rules 3 passed

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

Knowledge ✅ 1 expectation met

Expectation met SemSitter protobuf payload framing expectation met

Broader impact Repos 4 inspected PRs 4 inspected
Cross-repo 4 repositories inspected 0 open impacts

SonarSource/sonarlint-core Inspected
SonarSource/sonarlint-language-server Inspected
SonarSource/sonarlint-intellij Inspected
SonarSource/sonar-iac Inspected

Cross-PR 4 PRs inspected 0 open impacts

PR 6583: JAVA-7406 Offer the SemSitter frontend behind a flag Inspected
PR 6542: SONARJAVA-7339 Add the SemSitter client Inspected
PR 6541: SONARJAVA-7339 Add the SemSitter front-end module with its protobuf data model Inspected
PR 6573: JAVA-7396 Add the SemSitter semantic model of types, symbols and members 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

@rombirli
rombirli removed this pull request from stack #6589 December 1, 2026 11:07
@rombirli
rombirli added this pull request to stack #6607 December 1, 2026 11:08

@david-schroeter david-schroeter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for this PR!
I left a few comments that I think should be addressed before merging.

Comment on lines +93 to +98
JsonObject values = new JsonObject();
settings.forEach((key, list) -> {
JsonArray array = new JsonArray();
list.forEach(array::add);
values.add(key, array);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
JsonObject values = new JsonObject();
settings.forEach((key, list) -> {
JsonArray array = new JsonArray();
list.forEach(array::add);
values.add(key, array);
});
var gson = new Gson();
gson.toJsonTree(settings);

Here could we use the suggested Gson library to transform the map into a JsonElement? I think that would avoid boilerplate code and make it easier to understand the implementation.

The same logic could be applied to the other methods in the class that creates JsonElement.

return line.toString(StandardCharsets.UTF_8);
}

private static final class Answer {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: Could Answer be transformed into a record? It just carries header and payload.

return id;
}

private synchronized Answer exchange(JsonObject request) throws IOException {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can exchange and quit be called from different threads? It looks like the usage is sequential so I'm wondering if there is a concurrent case where the synchronized is required or if it could be dropped?


private final Process process;
private final BufferedWriter toServer;
private final InputStream fromServer;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The fromServer InputStream is never closed. Could we explicitly close it in the close() method maybe?

JsonArray array = new JsonArray();
names.forEach(array::add);
request.add("names", array);
Map<String, Boolean> found = new LinkedHashMap<>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: Is preserving the server’s response order intentional here? If callers aren’t meant to rely on it, could we use a simple HashMap?

* Drives the real udg-java-server, whose path is given by {@code -Dsemsitter.executable}.
*/
@EnabledIfSystemProperty(named = "semsitter.executable", matches = ".+")
class SemSitterClientE2ETest {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test is currently only run on the user machine if we properly configure the semsitter.executable property. Would there be a possibility to add this test as part of the CI run to ensure that we actually run against the real semsitter version we intend to ship and ensure that everything works as expected?

return client;
}

static List<String> command(List<String> launcher, Path projectRoot) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: Could we rename this method to buildServerCommand? That would make it clearer what the returned arguments are for.

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.

2 participants