From 16d1ad190a7563aea403f6cf987c40a7879786c0 Mon Sep 17 00:00:00 2001 From: slayerjain Date: Thu, 19 Nov 2026 21:46:30 +0530 Subject: [PATCH 01/14] perf(dedup): cache the app class set and time each step of a command Every END built a new set of the app's class names from the index, to keep only the app's classes from JaCoCo's dump. The index never changes once it is loaded, so the set is now built once. In a profile with 50k generated app classes, building it again took 1.4 to 2.2 ms of each test. KEPLOY_JAVA_DEDUP_DIAGNOSTICS now logs one line per START and END: how long the command took, and how long each step did. A START has warmup and reset. An END has the JaCoCo dump, the parse, the filter to the app's classes, the JSON and the send, plus the check for new classes (and the class-zip frame) on TCP. A step only takes the time when it ends; the line is formatted once the command is done, so no step is charged with formatting. A run on the real app then says where its per-test time goes. With diagnostics off nothing is logged and no step is timed. The warmup's log line now says how long the warmup took. The README lists both the diagnostics and the warmup switches. Signed-off-by: slayerjain --- README.md | 2 + .../io/keploy/dedup/KeployDedupAgent.java | 193 +++++++++---- .../test/java/io/keploy/dedup/FakeJacoco.java | 10 +- .../test/java/io/keploy/dedup/PhasesTest.java | 268 ++++++++++++++++++ 4 files changed, 423 insertions(+), 50 deletions(-) create mode 100644 keploy-sdk/src/test/java/io/keploy/dedup/PhasesTest.java diff --git a/README.md b/README.md index 12e4edb2..7d504814 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,8 @@ ceiling does not quietly fall behind again. - `KEPLOY_JAVA_CLASS_DIRS`: optional comma-separated class, jar, war, ear, or zip locations of the app's own classes. You rarely need it: see "Which classes count as the app's" below - `KEPLOY_JAVA_CLASSPATH_FALLBACK`: scans the full classpath when neither the app's own classes nor the executable jar give any class. `false` leaves the index empty then, and no test's coverage is measured. Default: `true` - `KEPLOY_JAVA_DEDUP_DISABLED`: disables the Java dedup agent when set to `true`, `1`, or `yes` +- `KEPLOY_JAVA_DEDUP_DIAGNOSTICS`: when `true`, `1`, or `yes`, logs one line per START and END that says how long the command took and how long each step did: `warmup` and `reset` for a START; `dump` (JaCoCo's dump), `parse`, `filter` (to the app's classes), `json` and `send` for an END, plus `classes` on TCP (the check for new app classes, and the class-zip frame when there are some). A step that did not run is left out: an empty dump has only `dump`. The app's class files are read once, on the first START under `warmup`, or, with the warmup off, on the first END whose dump is not empty, under `filter`. On the unix socket `took` ends before the answer is written; on TCP it includes the answer. Use it to see where a test's time goes on your app +- `KEPLOY_JAVA_DEDUP_WARMUP_DISABLED`: when `true`, `1`, or `yes`, skips the warmup, which runs the static initializers of the app's own classes once, on the first START, so that no test is charged with them. The log says how long the warmup took ## Which classes count as the app's diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java index e6c26e44..62d7afb0 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java @@ -38,9 +38,11 @@ import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.Set; @@ -501,6 +503,53 @@ private static void diagnostic(String message) { LOGGER.log(Level.INFO, formatted); } + // Phases times the steps of one command for KEPLOY_JAVA_DEDUP_DIAGNOSTICS, + // so a run on the real app says where its per-test time goes: the JaCoCo + // dump, the parse, the filter to the app's classes, the JSON and the send + // for an END; the warmup and the reset for a START. One line per command, + // and none when diagnostics are off. A mark only takes the time: report + // formats every step once the command is done, so no step is charged with + // the formatting of the one before it. + private static final class Phases { + + private final boolean on = diagnosticsEnabled(); + private final long began = System.nanoTime(); + private final List names = on ? new ArrayList<>(8) : null; + private final List ends = on ? new ArrayList<>(8) : null; + + // mark ends the step named step, which began at the last mark, or when + // the command did. + void mark(String step) { + if (!on) { + return; + } + ends.add(System.nanoTime()); + names.add(step); + } + + // report says how long the command took, and each of its steps. + void report(CoverageCommand command) { + if (!on) { + return; + } + long now = System.nanoTime(); + StringBuilder line = new StringBuilder(); + line.append(command.action).append(' ').append(command.testId) + .append(" took ").append(millis(now - began)).append(" ms:"); + long last = began; + for (int i = 0; i < names.size(); i++) { + long end = ends.get(i); + line.append(' ').append(names.get(i)).append('=').append(millis(end - last)).append("ms"); + last = end; + } + diagnostic(line.toString()); + } + + static String millis(long nanos) { + return String.format(Locale.ROOT, "%.2f", nanos / 1e6); + } + } + // A transport worker: run() serves Keploy's commands until close(). private interface CoverageWorker extends Runnable, Closeable { // close asks run() to end; it does not wait for it. @@ -634,52 +683,65 @@ private void handle(Socket socket) { } // run runs one command line ("START id", "END id") and says whether it - // was taken. + // was taken. With diagnostics on, it says how long each step took. private boolean run(String line) { CoverageCommand command = CoverageCommand.parse(line); if (command == null) { return false; } synchronized (testCaseLock) { - if (command.action == CommandAction.START) { - // Set only once the counters are reset: counters not reset - // hold the lines of the tests before this one, so this START - // is not taken and its END is ignored too. - activeTestId = ""; - // Warm up app classes once before the first measured window so - // one-time lines aren't charged to the first test. - collector.warmup(); - if (!collector.reset()) { - return false; - } - activeTestId = command.testId; - return true; + Phases phases = new Phases(); + try { + return run(command, phases); + } finally { + phases.report(command); } + } + } - if (!command.testId.equals(activeTestId)) { - log(Level.SEVERE, - "Ignoring mismatched END command. expected=" + activeTestId + ", actual=" - + command.testId, - null); - // Not ACK: this END publishes nothing, and an ACK told - // Keploy it had, so it waited for a record that never came. + // run runs one parsed command under testCaseLock, and marks its steps. + private boolean run(CoverageCommand command, Phases phases) { + if (command.action == CommandAction.START) { + // Set only once the counters are reset: counters not reset + // hold the lines of the tests before this one, so this START + // is not taken and its END is ignored too. + activeTestId = ""; + // Warm up app classes once before the first measured window so + // one-time lines aren't charged to the first test. + collector.warmup(); + phases.mark("warmup"); + boolean reset = collector.reset(); + phases.mark("reset"); + if (!reset) { return false; } + activeTestId = command.testId; + return true; + } - try { - Map> executedProbesByClass = collector.capture(); - if (executedProbesByClass.isEmpty()) { - log(Level.FINE, "No Java coverage collected for " + command.testId, null); - } - publisher.publish(command.testId, executedProbesByClass); - return true; - } catch (Exception e) { - // Not ACK for the same reason: nothing reached Keploy. - log(Level.SEVERE, "Failed to collect Java coverage for " + command.testId, e); - return false; - } finally { - activeTestId = ""; + if (!command.testId.equals(activeTestId)) { + log(Level.SEVERE, + "Ignoring mismatched END command. expected=" + activeTestId + ", actual=" + + command.testId, + null); + // Not ACK: this END publishes nothing, and an ACK told + // Keploy it had, so it waited for a record that never came. + return false; + } + + try { + Map> executedProbesByClass = collector.capture(phases); + if (executedProbesByClass.isEmpty()) { + log(Level.FINE, "No Java coverage collected for " + command.testId, null); } + publisher.publish(command.testId, executedProbesByClass, phases); + return true; + } catch (Exception e) { + // Not ACK for the same reason: nothing reached Keploy. + log(Level.SEVERE, "Failed to collect Java coverage for " + command.testId, e); + return false; + } finally { + activeTestId = ""; } } @@ -808,14 +870,19 @@ private void connectAndServe() throws IOException { if (command == null) { continue; } - dispatch(command, out); + Phases phases = new Phases(); + try { + dispatch(command, out, phases); + } finally { + phases.report(command); + } } } finally { socket = null; } } - private void dispatch(CoverageCommand command, OutputStream out) throws IOException { + private void dispatch(CoverageCommand command, OutputStream out, Phases phases) throws IOException { synchronized (testCaseLock) { if (command.action == CommandAction.START) { // As on the unix socket, the test is set only once the @@ -828,7 +895,9 @@ private void dispatch(CoverageCommand command, OutputStream out) throws IOExcept // Warm up app classes once before the first measured window so // one-time lines aren't charged to the first test. collector.warmup(); + phases.mark("warmup"); reset = collector.reset(); + phases.mark("reset"); } catch (RuntimeException e) { // Not taken, and the connection goes on: thrown past // here, it ended this thread, and no later command @@ -868,7 +937,7 @@ private void dispatch(CoverageCommand command, OutputStream out) throws IOExcept // exception would shift the collector onto the next test's frames. Map> executedProbesByClass; try { - executedProbesByClass = collector.capture(); + executedProbesByClass = collector.capture(phases); } catch (Exception e) { // Not taken, as on the unix socket: an empty COV is // the frame of a test that ran no line of the app. @@ -885,8 +954,10 @@ private void dispatch(CoverageCommand command, OutputStream out) throws IOExcept // Always emit exactly one COV before ACK — even when empty — // so the line-oriented collector stays in lockstep (the unix // transport likewise always publishes). - writeLine(out, "COV " + GSON.toJson( - new DedupPayload(command.testId, executedProbesByClass))); + String cov = "COV " + GSON.toJson(new DedupPayload(command.testId, executedProbesByClass)); + phases.mark("json"); + writeLine(out, cov); + phases.mark("send"); // Ride the bytecode+manifest over this same raw-TCP channel // (the only app->proxy path the replay agent doesn't mock- // intercept), when the manifest has grown. A failure building @@ -895,6 +966,7 @@ private void dispatch(CoverageCommand command, OutputStream out) throws IOExcept if (classesFrame != null) { writeLine(out, classesFrame); } + phases.mark("classes"); } catch (Exception e) { log(Level.FINE, "Failed to send CLASSES frame for " + command.testId, e); } finally { @@ -969,6 +1041,8 @@ private static final class CoverageCollector { private final JacocoClient jacocoClient; private final CoverageIndex coverageIndex; private final AtomicBoolean warmed = new AtomicBoolean(false); + // The VM names of the app's classes; see indexedClassNames. + private volatile Set appClassNames; // Coverage manifest ({vmClassName -> {classId, probeCount}}) accumulated // from the SAME per-test capture() path that produces dedup fingerprints — // so its class matching is proven correct (a separate post-warmup dump @@ -1041,6 +1115,7 @@ private void warmup() { + " (from " + coverageIndex.source + ")", null); return; } + long began = System.nanoTime(); ClassLoader[] loaders = warmupLoaders(); int initialized = 0; int failed = 0; @@ -1052,7 +1127,8 @@ private void warmup() { } } log(Level.INFO, "Keploy dedup: warmed up application classes (initialized=" - + initialized + ", skipped=" + failed + ")", null); + + initialized + ", skipped=" + failed + ") in " + Phases.millis(System.nanoTime() - began) + + " ms", null); } private boolean isWarmupDisabled() { @@ -1090,8 +1166,9 @@ private boolean initializeClass(String binaryName, ClassLoader[] loaders) { return false; } - private Map> capture() throws IOException { + private Map> capture(Phases phases) throws IOException { byte[] dump = jacocoClient.dump(true, true); + phases.mark("dump"); if (dump.length == 0) { if (diagnosticsEnabled()) { diagnostic("JaCoCo dump returned 0 bytes"); @@ -1102,6 +1179,7 @@ private Map> capture() throws IOException { ExecFileLoader loader = new ExecFileLoader(); loader.load(new ByteArrayInputStream(dump)); ExecutionDataStore executionDataStore = loader.getExecutionDataStore(); + phases.mark("parse"); // BRANCH coverage: fingerprint by the set of executed JaCoCo PROBES per // class, NOT just executed lines. Each branch is instrumented as a @@ -1152,20 +1230,30 @@ private Map> capture() throws IOException { } } + Map> sorted = toSortedMap(raw); + phases.mark("filter"); + if (diagnosticsEnabled()) { diagnostic("classesWithProbes=" + raw.size() + ", sampleClasses=" + summarizeStrings(raw.keySet(), 5)); } - - return toSortedMap(raw); + return sorted; } // indexedClassNames returns the VM names of the app's own classes (from the - // CoverageIndex) so probe collection can skip JDK/library classes. + // CoverageIndex) so probe collection can skip JDK/library classes. Built + // once: the index never changes once loaded, and building the set again + // on every END took 1.4-2.2 ms per test at 50k classes. Two ENDs never + // race here (testCaseLock), and a set built twice would be the same set. private Set indexedClassNames() { - Set names = new LinkedHashSet<>(); - for (ClassEntry entry : coverageIndex.entries()) { - names.add(entry.className); + Set names = appClassNames; + if (names == null) { + List entries = coverageIndex.entries(); + names = new HashSet<>(entries.size() * 2); + for (ClassEntry entry : entries) { + names.add(entry.className); + } + appClassNames = names; } return names; } @@ -1884,9 +1972,16 @@ synchronized void sendIndex() { } } - private void publish(String testId, Map> executedProbesByClass) throws IOException { + // publish sends the test's coverage, after the INDEX report when the + // receiver does not have it yet: the report's time counts in "send". + private void publish(String testId, Map> executedProbesByClass, Phases phases) + throws IOException { + byte[] payload = GSON.toJson(new DedupPayload(testId, executedProbesByClass)) + .getBytes(StandardCharsets.UTF_8); + phases.mark("json"); sendIndex(); - send(GSON.toJson(new DedupPayload(testId, executedProbesByClass)).getBytes(StandardCharsets.UTF_8)); + send(payload); + phases.mark("send"); } // send writes payload on a connection of its own to the data socket: diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/FakeJacoco.java b/keploy-sdk/src/test/java/io/keploy/dedup/FakeJacoco.java index dfdd28e4..6a8da1c8 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/FakeJacoco.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/FakeJacoco.java @@ -6,11 +6,13 @@ import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.atomic.AtomicInteger; +import org.jacoco.core.data.ExecutionData; import org.jacoco.core.runtime.RemoteControlReader; import org.jacoco.core.runtime.RemoteControlWriter; /** - * A JaCoCo tcpserver: it answers each dump or reset with an empty dump. + * A JaCoCo tcpserver: it answers each dump or reset with an empty dump, but + * a dump holds hit alone when hit is set. * dropNext closes that many connections first with no answer, as a JaCoCo * that went away does. */ @@ -22,6 +24,8 @@ final class FakeJacoco implements Closeable { final AtomicInteger accepted = new AtomicInteger(); // holdMillis holds each answer that long, as a JaCoCo busy with a big dump does. volatile long holdMillis; + // hit, when set, is the class execution each dump returns. + volatile ExecutionData hit; FakeJacoco() throws IOException { server = new ServerSocket(0, 50, InetAddress.getLoopbackAddress()); @@ -43,6 +47,10 @@ private void serve() { reader.setRemoteCommandVisitor((dump, reset) -> { hold(); served.incrementAndGet(); + ExecutionData data = hit; + if (dump && data != null) { + writer.visitClassExecution(data); + } writer.sendCmdOk(); }); reader.read(); diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/PhasesTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/PhasesTest.java new file mode 100644 index 00000000..f382da68 --- /dev/null +++ b/keploy-sdk/src/test/java/io/keploy/dedup/PhasesTest.java @@ -0,0 +1,268 @@ +package io.keploy.dedup; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.logging.Handler; +import java.util.logging.LogRecord; +import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.jacoco.core.data.ExecutionData; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.newsclub.net.unix.AFUNIXServerSocket; +import org.newsclub.net.unix.AFUNIXSocket; +import org.newsclub.net.unix.AFUNIXSocketAddress; + +/** + * KEPLOY_JAVA_DEDUP_DIAGNOSTICS says, for each command, how long it took and + * how long each of its steps did, so a run on the real app shows where its + * per-test time goes. Diagnostics off, it says nothing. + */ +public class PhasesTest { + + // The warmup must be on for the START rows, and no build tag set, or a + // CLASSES frame comes between the TCP END's COV and its ACK. + private static final String[] PROPERTIES = {"keploy.java.dedup.diagnostics", "keploy.java.class.dirs", + "keploy.jacoco.port", "keploy.coverage.endpoint", "keploy.java.dedup.warmup.disabled", + "keploy.build.tag"}; + private static final String TAKEN = "ACK proto=2\n"; + private static final String MS = "\\d+\\.\\d\\d"; + // The one app class, and the probes a test ran in it. + private static final String SHOP = "com/acme/Shop"; + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + private String savedControlPath; + private String savedDataPath; + private FakeJacoco jacoco; + private final List logged = new CopyOnWriteArrayList<>(); + private final Handler handler = new Handler() { + @Override + public void publish(LogRecord record) { + logged.add(record.getMessage()); + } + + @Override + public void flush() { + } + + @Override + public void close() { + } + }; + + @Before + public void setUp() throws IOException { + for (String name : PROPERTIES) { + System.clearProperty(name); + } + File classes = tmp.newFolder("classes"); + File shop = new File(classes, SHOP + ".class"); + shop.getParentFile().mkdirs(); + Files.write(shop.toPath(), new byte[] {1}); + System.setProperty("keploy.java.class.dirs", classes.getPath()); + savedControlPath = KeployDedupAgent.controlSocketPath; + savedDataPath = KeployDedupAgent.dataSocketPath; + File sockets = tmp.newFolder("sockets"); + KeployDedupAgent.controlSocketPath = new File(sockets, "control.sock").getAbsolutePath(); + KeployDedupAgent.dataSocketPath = new File(sockets, "data.sock").getAbsolutePath(); + Logger.getLogger(KeployDedupAgent.class.getName()).addHandler(handler); + jacoco = new FakeJacoco(); + jacoco.hit = new ExecutionData(0x1234L, SHOP, new boolean[] {true, false, true}); + System.setProperty("keploy.jacoco.port", String.valueOf(jacoco.server.getLocalPort())); + } + + @After + public void tearDown() throws Exception { + KeployDedupAgent.stop(); + Logger.getLogger(KeployDedupAgent.class.getName()).removeHandler(handler); + KeployDedupAgent.controlSocketPath = savedControlPath; + KeployDedupAgent.dataSocketPath = savedDataPath; + for (String name : PROPERTIES) { + System.clearProperty(name); + } + jacoco.close(); + } + + @Test + public void withDiagnosticsOnEachCommandSaysHowLongEachStepTook() throws Exception { + System.setProperty("keploy.java.dedup.diagnostics", "true"); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + String payload = data.next(); + assertNotNull("the END's coverage reached the data socket", payload); + assertTrue(payload, payload.contains("\"" + SHOP + "\":[0,2]")); + } + awaitLogged("Keploy dedup: warmed up application classes \\(initialized=0, skipped=1\\) in " + MS + " ms"); + assertStepsAddUp(awaitLogged("Java dedup diagnostics: START test-set-0/test-1 took " + MS + " ms: warmup=" + + MS + "ms reset=" + MS + "ms")); + assertStepsAddUp(awaitLogged("Java dedup diagnostics: END test-set-0/test-1 took " + MS + " ms: dump=" + MS + + "ms parse=" + MS + "ms filter=" + MS + "ms json=" + MS + "ms send=" + MS + "ms")); + } + + @Test + public void overTcpEachCommandSaysHowLongEachStepTookToo() throws Exception { + System.setProperty("keploy.java.dedup.diagnostics", "true"); + try (ServerSocket collector = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) { + collector.setSoTimeout(30000); + System.setProperty("keploy.coverage.endpoint", "127.0.0.1:" + collector.getLocalPort()); + assertTrue(KeployDedupAgent.start()); + try (Socket sdk = collector.accept()) { + sdk.setSoTimeout(30000); + OutputStream out = sdk.getOutputStream(); + BufferedReader in = new BufferedReader( + new InputStreamReader(sdk.getInputStream(), StandardCharsets.UTF_8)); + out.write("START test-set-0/test-1\n".getBytes(StandardCharsets.UTF_8)); + out.flush(); + assertEquals("ACK", in.readLine()); + out.write("END test-set-0/test-1\n".getBytes(StandardCharsets.UTF_8)); + out.flush(); + String cov = in.readLine(); + assertTrue(cov, cov != null && cov.startsWith("COV ") && cov.contains("\"" + SHOP + "\":[0,2]")); + assertEquals("ACK", in.readLine()); + } + } + // Said once the command is answered: waited for. + assertStepsAddUp(awaitLogged("Java dedup diagnostics: START test-set-0/test-1 took " + MS + " ms: warmup=" + + MS + "ms reset=" + MS + "ms")); + assertStepsAddUp(awaitLogged("Java dedup diagnostics: END test-set-0/test-1 took " + MS + " ms: dump=" + MS + + "ms parse=" + MS + "ms filter=" + MS + "ms json=" + MS + "ms send=" + MS + "ms classes=" + MS + + "ms")); + } + + @Test + public void withDiagnosticsOffNoCommandSaysHowLongItTook() throws Exception { + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertNotNull("the END's coverage reached the data socket", data.next()); + } + // On the unix socket the time is said before the answer, so it would be here by now. + for (String line : logged) { + assertTrue("logged: " + line, !line.contains(" took ")); + } + } + + // awaitLogged waits for a logged line that matches regex, and returns it. + private String awaitLogged(String regex) throws InterruptedException { + Pattern pattern = Pattern.compile(regex); + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10); + while (System.nanoTime() < deadline) { + for (String line : logged) { + if (pattern.matcher(line).matches()) { + return line; + } + } + Thread.sleep(20); + } + throw new AssertionError("nothing logged matches: " + regex + "\nlogged: " + logged); + } + + // assertStepsAddUp checks that a command's steps add up to no more than the + // time it took: each step is timed from the one before it, not from the + // command's start. Each number is rounded to 0.01 ms, so the steps may be + // over by up to that much each. + private static void assertStepsAddUp(String line) { + Matcher took = Pattern.compile(" took (" + MS + ") ms:").matcher(line); + assertTrue(line, took.find()); + double total = Double.parseDouble(took.group(1)); + Matcher step = Pattern.compile(" [a-z]+=(" + MS + ")ms").matcher(line); + double sum = 0; + int steps = 0; + while (step.find()) { + sum += Double.parseDouble(step.group(1)); + steps++; + } + assertTrue(line, steps > 0); + assertTrue("the steps add up to more than the command took: " + line, sum <= total + 0.01 * steps); + } + + private static String send(String command) throws IOException { + try (AFUNIXSocket socket = AFUNIXSocket.newInstance()) { + socket.connect(AFUNIXSocketAddress.of(new File(KeployDedupAgent.controlSocketPath)), 3000); + socket.setSoTimeout(30000); + OutputStream out = socket.getOutputStream(); + out.write((command + "\n").getBytes(StandardCharsets.UTF_8)); + out.flush(); + return new String(readAll(socket.getInputStream()), StandardCharsets.UTF_8); + } + } + + private static byte[] readAll(InputStream in) throws IOException { + ByteArrayOutputStream all = new ByteArrayOutputStream(); + byte[] buffer = new byte[8192]; + int n; + while ((n = in.read(buffer)) != -1) { + all.write(buffer, 0, n); + } + return all.toByteArray(); + } + + // DataReceiver is Keploy's data socket: it keeps each payload sent to it. + private static final class DataReceiver implements Closeable { + + private final AFUNIXServerSocket server; + private final BlockingQueue payloads = new LinkedBlockingQueue<>(); + + DataReceiver(String path) throws IOException { + server = AFUNIXServerSocket.newInstance(); + server.bind(AFUNIXSocketAddress.of(new File(path)), 50); + Thread thread = new Thread(this::serve, "data-receiver"); + thread.setDaemon(true); + thread.start(); + } + + private void serve() { + while (!server.isClosed()) { + try (Socket socket = server.accept()) { + socket.setSoTimeout(30000); + String payload = new String(readAll(socket.getInputStream()), StandardCharsets.UTF_8); + // The INDEX report (ControlSocketTest's rows) is not a test's coverage. + if (!payload.startsWith("{\"kind\":\"index\",")) { + payloads.add(payload); + } + } catch (IOException e) { + // Closed. + } + } + } + + // next waits for the next payload: the SDK sends it before it answers the END. + String next() throws InterruptedException { + return payloads.poll(30, TimeUnit.SECONDS); + } + + @Override + public void close() throws IOException { + server.close(); + } + } +} From bf3ecdfaee5a9f6ebf940b47b6f0725ebb3f650d Mon Sep 17 00:00:00 2001 From: slayerjain Date: Thu, 19 Nov 2026 23:27:19 +0530 Subject: [PATCH 02/14] perf(dedup): read and clear the app's probes in one parallel pass With JaCoCo's agent in the app's JVM, each test took three passes over the probe arrays of every class JaCoCo had seen, library classes too: the START's reset, then the END's dump and its reset. On large apps those passes are most of the SDK's time per test. A START and an END now make one pass each, over the probe arrays of the app's classes only, read in place from JaCoCo's runtime: - The arrays are kept sorted by class name and added to only when JaCoCo's store has grown (a class was loaded); a pass with no class loaded since does not walk the store. - The pass is split across min(4, cores / 2) threads, the caller's included, once there are 2048 classes or more for each; the others are daemon threads that stop after a minute idle. A part no thread can be made for is scanned on the caller's thread. - A probe read as hit is noted and cleared in the same step, so a hit made while the pass runs is in that pass or the next one. JaCoCo's dump then reset loses a hit made between the two. - What a START clears ran between two tests (a health check, a scheduled job, the warmup's static initializers): it is background, never counted in a test's hits. With diagnostics on, the START logs how many classes and probes it was. It is not sent to Keploy: no frame of the current protocol carries it. - Library classes are neither read nor cleared. JaCoCo's runtime is reached by reflection, through the types of its fields, as the agent shades them under a package of its own. When JaCoCo is reached over TCP, or its agent is not laid out that way, the SDK uses JaCoCo's reset and dump, as before. When a pass fails, the SDK uses them for the rest of the run. A pass that failed before it cleared a probe leaves the test's hits to JaCoCo's dump. One that failed after (an Error in a part, such as out of memory) has lost hits, so its END is not taken; it waits for every part to end first. Of two classes of one name (loaded twice, from other bytes), the hits sent are those of the one the coverage manifest names when that one ran, else those of the lowest id, as JaCoCo's dump sends a name's hits whichever copy made them. The diagnostics line names the new step `scan`. The smoke test checks that the real JaCoCo agent is read in place, both one the command attaches and the one keploy-sdk.jar starts itself. Rows: a hit is reported once and cleared; background never reaches the next test; a class loaded after a pass is scanned by the next; order and completeness across four parts; a writer racing the passes has each of its 16384 hits reported exactly once, over at least two passes for each 512 writes; a failed part ends the pass only once every part has; a part the pool cannot take is scanned by the caller; an Error reading JaCoCo's classes clears nothing; agents not laid out as JaCoCo's are refused; and, through the control socket, the background is left out, a class loaded twice is reported once, as the copy the manifest names when it ran and as the other copy when only that one ran, a pass that lost hits leaves its END not taken, an Error reading JaCoCo's classes or an agent's fields keeps the server and falls back, and a failed pass falls back to JaCoCo's dump and reset for good. Signed-off-by: slayerjain --- README.md | 8 +- .../io/keploy/dedup/KeployDedupAgent.java | 195 +++++- .../main/java/io/keploy/dedup/ProbeScan.java | 373 +++++++++++ .../io/keploy/dedup/InProcessScanTest.java | 523 +++++++++++++++ .../java/io/keploy/dedup/JacocoRtStub.java | 23 + .../test/java/io/keploy/dedup/PhasesTest.java | 10 + .../java/io/keploy/dedup/ProbeScanTest.java | 594 ++++++++++++++++++ scripts/smoke-javaagent.sh | 39 ++ 8 files changed, 1738 insertions(+), 27 deletions(-) create mode 100644 keploy-sdk/src/main/java/io/keploy/dedup/ProbeScan.java create mode 100644 keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java create mode 100644 keploy-sdk/src/test/java/io/keploy/dedup/JacocoRtStub.java create mode 100644 keploy-sdk/src/test/java/io/keploy/dedup/ProbeScanTest.java diff --git a/README.md b/README.md index 7d504814..ae1713a5 100644 --- a/README.md +++ b/README.md @@ -17,14 +17,16 @@ from and how to raise it. Keploy Enterprise drives dynamic dedup per testcase. 1. Enterprise sends `START /` on `/tmp/coverage_control.sock`. -2. The Java agent resets JaCoCo coverage counters for that testcase. +2. The Java agent clears what the application's classes ran since the last testcase. What it clears is background (a health check, a scheduled job): it is never counted in a testcase's coverage. 3. Enterprise replays the testcase. 4. Enterprise sends `END /` on `/tmp/coverage_control.sock`. -5. The Java agent dumps JaCoCo execution data and sends the executed probe indices per class (`{className -> [probeIdx]}`) as JSON on `/tmp/coverage_data.sock`. Probes capture branch-level coverage (which branch a test took), so they distinguish tests that run the same lines but take different branches. +5. The Java agent reads, and clears, the probes of the application's classes that the testcase hit, and sends the executed probe indices per class (`{className -> [probeIdx]}`) as JSON on `/tmp/coverage_data.sock`. Probes capture branch-level coverage (which branch a test took), so they distinguish tests that run the same lines but take different branches. 6. Enterprise writes the result to `dedupData.yaml` and uses it to identify duplicates. Coverage is collected at per-testcase granularity, not process granularity. +With the JaCoCo agent in the application's JVM (the usual case), steps 2 and 5 are one pass over the probe arrays of the application's classes alone, split across up to four threads (half the cores): a probe is cleared only once it was read as hit, so a hit made while the pass runs is reported by that pass or the next one, never lost. Library classes are neither read nor cleared. If JaCoCo's runtime data cannot be read that way, or the pass fails once, the agent uses JaCoCo's own reset and dump for the rest of the run, as it does when JaCoCo is reached over TCP. + Before the first testcase's coverage, the agent sends Enterprise one INDEX record on `/tmp/coverage_data.sock`: a JSON object starting with `{"kind":"index",` that says how it found the application's own classes (`source`: `override` when the dirs and jars in `KEPLOY_JAVA_CLASS_DIRS` held them, `derived` from the launch or from `target/classes` or `build/classes/java/main` under the working directory, `whole-jar` or `classpath` when it could not tell them from the libraries, `none`), what it found them from in words (`launch`), the dirs and jars it read them from (`roots`, at most 64, and `rootCount`), the package roots it kept (`packages`), how many classes it indexed, the build id of those classes (`jv1-` and 32 hex), which JaCoCo agent measures them (`jacoco`: `embedded`, the one keploy-sdk.jar starts, `attached` by the command, `none`, or `unchecked` when the agent was not started as a `-javaagent`), that embedded agent's `includes=`, and two versions as their jars' manifests name them, or `""` when not known: keploy-sdk.jar's (`sdkVersion`) and that of the JaCoCo agent that measures the classes, embedded or attached (`jacocoVersion`). It carries no class list. It is sent as the agent starts when that path's socket file is there (Enterprise's receiver makes it), and otherwise, or when that send fails, right before the first testcase's coverage; a receiver Enterprise binds anew on that path gets it again before its first record. On the TCP transport used in Kubernetes no INDEX record is sent. The agent binds `/tmp/coverage_control.sock` before the application's `main` runs, so the socket is listening before the application can answer a health check. It answers `ACK` to each command it took, and `IGNORED` to an `END` that no `START` for that testcase came before: nothing is published for it, and Enterprise keeps that testcase out of the comparison. On the TCP transport used in Kubernetes the same `END` is answered with `IGNORED ` and then `ACK`. @@ -199,7 +201,7 @@ ceiling does not quietly fall behind again. - `KEPLOY_JAVA_CLASS_DIRS`: optional comma-separated class, jar, war, ear, or zip locations of the app's own classes. You rarely need it: see "Which classes count as the app's" below - `KEPLOY_JAVA_CLASSPATH_FALLBACK`: scans the full classpath when neither the app's own classes nor the executable jar give any class. `false` leaves the index empty then, and no test's coverage is measured. Default: `true` - `KEPLOY_JAVA_DEDUP_DISABLED`: disables the Java dedup agent when set to `true`, `1`, or `yes` -- `KEPLOY_JAVA_DEDUP_DIAGNOSTICS`: when `true`, `1`, or `yes`, logs one line per START and END that says how long the command took and how long each step did: `warmup` and `reset` for a START; `dump` (JaCoCo's dump), `parse`, `filter` (to the app's classes), `json` and `send` for an END, plus `classes` on TCP (the check for new app classes, and the class-zip frame when there are some). A step that did not run is left out: an empty dump has only `dump`. The app's class files are read once, on the first START under `warmup`, or, with the warmup off, on the first END whose dump is not empty, under `filter`. On the unix socket `took` ends before the answer is written; on TCP it includes the answer. Use it to see where a test's time goes on your app +- `KEPLOY_JAVA_DEDUP_DIAGNOSTICS`: when `true`, `1`, or `yes`, logs one line per START and END that says how long the command took and how long each step did: `warmup`, then `scan` (the pass that clears the background) for a START; `scan` (the pass that reads and clears the test's hits), `filter` (the hits made into the test's probes by class), `json` and `send` for an END, plus `classes` on TCP (the check for new app classes, and the class-zip frame when there are some). Where JaCoCo's own reset and dump are used instead, a START has `reset`, and an END has `dump` (JaCoCo's dump), `parse` and `filter` (to the app's classes) in place of `scan` and `filter`; a step that did not run is left out, so an empty dump has only `dump`. A START that cleared background hits also logs how many classes and probes they were. The app's class files are read once: before the app starts, when keploy-sdk.jar starts JaCoCo itself; otherwise on the first START under `warmup`, or, with the warmup off, under that START's `scan` (or, with JaCoCo's dump, on the first END whose dump is not empty, under `filter`). On the unix socket `took` ends before the answer is written; on TCP it includes the answer. Use it to see where a test's time goes on your app - `KEPLOY_JAVA_DEDUP_WARMUP_DISABLED`: when `true`, `1`, or `yes`, skips the warmup, which runs the static initializers of the app's own classes once, on the first START, so that no test is charged with them. The log says how long the warmup took ## Which classes count as the app's diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java index 62d7afb0..7cc1d666 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java @@ -100,15 +100,16 @@ static String encodeBytecodeFrame(String buildTag, String manifestJson, byte[] z private static final int DEFAULT_JACOCO_PORT = 36320; private static final int SOCKET_TIMEOUT_MILLIS = 3000; private static final int SOCKET_BACKLOG = 50; - // The answer to a command not taken (a START whose reset failed, an END - // with no START for that test, as after a restart): nothing is published + // The answer to a command not taken (a START that could not clear the + // counters, an END with no START for that test, as after a restart, an + // END whose scan lost hits it had cleared): nothing is published // for that test, so Keploy must not use its coverage. On the unix socket // it starts the whole answer (UNIX_REPLY_IGNORED); on TCP it is a frame // naming the test, and ACK still follows (CoverageTcpClient). static final String IGNORED_REPLY = "IGNORED"; // The answers on the unix control socket: one line for every command the // SDK reads, written before it closes the connection. ACK says it took the - // command: it reset the counters for a START, or sent the test's coverage + // command: it cleared the counters for a START, or sent the test's coverage // for an END. IGNORED says it read the command and did not take it. // proto=2 says this SDK answers every command it reads, so Keploy can read // a close with no answer as a command not taken: on macOS a connection @@ -504,9 +505,10 @@ private static void diagnostic(String message) { } // Phases times the steps of one command for KEPLOY_JAVA_DEDUP_DIAGNOSTICS, - // so a run on the real app says where its per-test time goes: the JaCoCo - // dump, the parse, the filter to the app's classes, the JSON and the send - // for an END; the warmup and the reset for a START. One line per command, + // so a run on the real app says where its per-test time goes: the probe + // scan (or JaCoCo's dump and the parse), the filter to the app's classes, + // the JSON and the send for an END; the warmup and the scan (or JaCoCo's + // reset) for a START. One line per command, // and none when diagnostics are off. A mark only takes the time: report // formats every step once the command is done, so no step is charged with // the formatting of the one before it. @@ -702,17 +704,15 @@ private boolean run(String line) { // run runs one parsed command under testCaseLock, and marks its steps. private boolean run(CoverageCommand command, Phases phases) { if (command.action == CommandAction.START) { - // Set only once the counters are reset: counters not reset - // hold the lines of the tests before this one, so this START - // is not taken and its END is ignored too. + // Set only once the counters are cleared: counters not + // cleared hold the lines of the tests before this one, so + // this START is not taken and its END is ignored too. activeTestId = ""; // Warm up app classes once before the first measured window so // one-time lines aren't charged to the first test. collector.warmup(); phases.mark("warmup"); - boolean reset = collector.reset(); - phases.mark("reset"); - if (!reset) { + if (!collector.begin(phases)) { return false; } activeTestId = command.testId; @@ -790,11 +790,12 @@ private static void closeQuietly(Closeable closeable) { * but inverts the roles — here the SDK is the client. Wire protocol: *
      *   collector -> SDK : "START <id>" | "END <id>"
-     *   SDK -> collector : "ACK"                        (after START reset)
-     *                      "COV <compact-json>" + "ACK" (after END dump)
-     *                      "IGNORED <id>" + "ACK"         (after a START whose reset
-     *                                                     failed, an END with no START,
-     *                                                     or an END whose dump failed)
+     *   SDK -> collector : "ACK"                        (after a START cleared the counters)
+     *                      "COV <compact-json>" + "ACK" (after an END read the test's hits)
+     *                      "IGNORED <id>" + "ACK"         (after a START that could not
+     *                                                     clear them, an END with no START,
+     *                                                     or an END whose hits could not
+     *                                                     be read)
      * 
* The collector starts listening only when replay begins, so the connect loop * retries until it is reachable. @@ -886,9 +887,9 @@ private void dispatch(CoverageCommand command, OutputStream out, Phases phases) synchronized (testCaseLock) { if (command.action == CommandAction.START) { // As on the unix socket, the test is set only once the - // counters are reset: counters not reset hold the lines of - // the tests before this one, so this START is not taken and - // its END is ignored too. + // counters are cleared: counters not cleared hold the lines + // of the tests before this one, so this START is not taken + // and its END is ignored too. activeTestId = ""; boolean reset; try { @@ -896,13 +897,12 @@ private void dispatch(CoverageCommand command, OutputStream out, Phases phases) // one-time lines aren't charged to the first test. collector.warmup(); phases.mark("warmup"); - reset = collector.reset(); - phases.mark("reset"); + reset = collector.begin(phases); } catch (RuntimeException e) { // Not taken, and the connection goes on: thrown past // here, it ended this thread, and no later command // was answered. - log(Level.SEVERE, "Failed to reset Java coverage for " + command.testId, e); + log(Level.SEVERE, "Failed to clear Java coverage for " + command.testId, e); reset = false; } if (reset) { @@ -1077,6 +1077,77 @@ private CoverageCollector(JacocoClient jacocoClient, CoverageIndex coverageIndex this.coverageIndex = coverageIndex; } + // The one-pass scan of the app classes' probes, when JaCoCo's agent + // runs in this JVM and its runtime data could be read; null when it + // is reached over TCP, or the scan failed once. Looked up on the + // first command; both are guarded by the collector. + private ProbeScan probeScan; + private boolean probeScanLookedUp; + + private synchronized ProbeScan probeScan() { + if (!probeScanLookedUp) { + probeScanLookedUp = true; + Object agent = jacocoClient.runtimeAgent(); + if (agent != null) { + try { + probeScan = ProbeScan.locate(agent); + } catch (Throwable e) { + // Throwable, as for the in-process agent's lookup: a + // LinkageError from an agent not laid out as JaCoCo's + // must not end the control server. + log(Level.INFO, "Keploy dedup: JaCoCo's probes cannot be read in place, so each test" + + " takes JaCoCo's dump and reset", e); + } + } + } + return probeScan; + } + + // dropProbeScan stops using the scan after it failed: every later + // command takes JaCoCo's dump and reset. + private synchronized void dropProbeScan(Throwable cause) { + probeScan = null; + log(Level.WARNING, "Keploy dedup: the probe scan failed; each test now takes JaCoCo's dump and reset", + cause); + } + + // begin clears what the app's classes ran since the last test ended, + // so that none of it is charged to the next test, and says whether + // it did. With the scan, that is one pass over the app's classes, and + // what it clears is background (a scheduled job, a health check): + // counted in the diagnostics, never in a test's hits. Without it, or + // when the pass fails (in any way: a START only has to clear), + // JaCoCo resets every class. + private boolean begin(Phases phases) { + ProbeScan scan = probeScan(); + if (scan != null) { + try { + List background = scan.pass(indexedClassNames()); + phases.mark("scan"); + if (diagnosticsEnabled() && !background.isEmpty()) { + diagnostic("background hits cleared at START: " + describe(background)); + } + return true; + } catch (ReflectiveOperationException | RuntimeException e) { + dropProbeScan(e); + } + } + boolean reset = reset(); + phases.mark("reset"); + return reset; + } + + // describe says how many classes and probes hits holds, and names a few classes. + private String describe(List hits) { + int probes = 0; + List names = new ArrayList<>(hits.size()); + for (ProbeScan.Hit hit : hits) { + probes += hit.probes.length; + names.add(hit.name); + } + return "classes=" + hits.size() + ", probes=" + probes + ", sampleClasses=" + summarizeStrings(names, 5); + } + // reset zeroes JaCoCo's counters and says whether it did. private boolean reset() { try { @@ -1095,7 +1166,7 @@ private boolean reset() { * the one-time class-init cost, and JaCoCo charges those <clinit> * lines to whichever test ran first — making the duplicate set * non-deterministic run-to-run. Running them now (then letting the START - * reset clear the counters) means every test sees only the lines its own + * clear the counters) means every test sees only the lines its own * request executes. Called once, on the first START, when the app is * fully started. Best-effort: per-class failures are ignored (the class * just falls back to lazy init). Disable with @@ -1167,6 +1238,26 @@ private boolean initializeClass(String binaryName, ClassLoader[] loaders) { } private Map> capture(Phases phases) throws IOException { + ProbeScan scan = probeScan(); + if (scan != null) { + List hits = null; + try { + hits = scan.pass(indexedClassNames()); + } catch (ProbeScan.LostHitsException e) { + // Hits this test made were cleared and lost: JaCoCo's + // dump would ACK a partial set, so the END is not taken. + // The next START takes JaCoCo's reset. + dropProbeScan(e); + throw e; + } catch (ReflectiveOperationException | RuntimeException e) { + // Nothing was cleared: JaCoCo's dump holds this test's hits. + dropProbeScan(e); + } + if (hits != null) { + phases.mark("scan"); + return probesByClass(hits, phases); + } + } byte[] dump = jacocoClient.dump(true, true); phases.mark("dump"); if (dump.length == 0) { @@ -1240,6 +1331,55 @@ private Map> capture(Phases phases) throws IOException { return sorted; } + // probesByClass turns the scan's hits (sorted by name, then id) into + // the test's probes by class, and adds each class to the coverage + // manifest, as the dump's path does. Of two classes of one name + // (loaded twice, from different bytes) it reports the one whose id + // the manifest holds when that one was hit, else the lowest id. In + // that last case, when the manifest already names the other copy, + // the probes sent are not those of the class the manifest names, as + // with JaCoCo's dump: a test's record names a class, not its id. + private Map> probesByClass(List hits, Phases phases) { + Map> byClass = new LinkedHashMap<>(); + for (int i = 0; i < hits.size(); ) { + String name = hits.get(i).name; + int end = i + 1; + while (end < hits.size() && hits.get(end).name.equals(name)) { + end++; + } + ProbeScan.Hit chosen = hits.get(i); + ManifestEntry known = liveManifest.get(name); + if (known != null) { + for (int k = i; k < end; k++) { + if (Long.toHexString(hits.get(k).id).equals(known.id)) { + chosen = hits.get(k); + break; + } + } + } + i = end; + final int probeCount = chosen.probeCount; + final long classId = chosen.id; + liveManifest.computeIfAbsent(name, k -> { + ManifestEntry m = new ManifestEntry(); + m.id = Long.toHexString(classId); + m.probeCount = probeCount; + return m; + }); + List probes = new ArrayList<>(chosen.probes.length); + for (int probe : chosen.probes) { + probes.add(probe); + } + byClass.put(name, probes); + } + phases.mark("filter"); + if (diagnosticsEnabled()) { + diagnostic("classesWithProbes=" + byClass.size() + + ", sampleClasses=" + summarizeStrings(byClass.keySet(), 5)); + } + return byClass; + } + // indexedClassNames returns the VM names of the app's own classes (from the // CoverageIndex) so probe collection can skip JDK/library classes. Built // once: the index never changes once loaded, and building the set again @@ -1384,6 +1524,13 @@ private byte[] dump(boolean dump, boolean reset) throws IOException { return dumpOverTcp(dump, reset); } + // runtimeAgent is JaCoCo's agent object in this JVM, or null when + // JaCoCo is reached over TCP. + private Object runtimeAgent() { + InProcessAgent agent = inProcessAgent(); + return agent == null ? null : agent.agent; + } + private InProcessAgent inProcessAgent() { if (inProcessUnavailable) { return null; diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/ProbeScan.java b/keploy-sdk/src/main/java/io/keploy/dedup/ProbeScan.java new file mode 100644 index 00000000..ed6c0ad8 --- /dev/null +++ b/keploy-sdk/src/main/java/io/keploy/dedup/ProbeScan.java @@ -0,0 +1,373 @@ +package io.keploy.dedup; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.FutureTask; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * ProbeScan reads the hits of the app's classes straight from the probe arrays + * that JaCoCo's agent holds in this JVM, in one pass: for each class it notes + * the probes that were hit and clears them. JaCoCo's own API takes two passes + * over every class it ever saw, library classes too: a dump, then a reset. At + * 50k app classes those two passes, and the START's reset, cost 20-44 ms a + * test; one pass over the app classes alone, split across a few threads, is + * meant to bring that down. + * + *

It keeps the probe arrays of the app's classes in an array sorted by + * class name, and adds to it only when JaCoCo's store has grown (a class was + * loaded). The arrays are the ones the instrumented code writes to: JaCoCo + * never replaces them, its reset only fills them with false. + * + *

A probe hit while the pass runs is the same race JaCoCo's dump and reset + * have, made narrower: a probe is cleared only right after it was read as + * hit, so a hit is either in this pass or left for the next one. JaCoCo's + * reset clears every probe after the dump has read them all, and a hit + * between the two is lost. + * + *

It reads JaCoCo's runtime by reflection (the agent's RuntimeData, its + * ExecutionDataStore, and that store's map of ExecutionData), found by the + * types of their fields, as the agent shades them under a package of its own. + * Not found, {@link #locate} throws, and the caller uses JaCoCo's dump and + * reset. + */ +final class ProbeScan { + + // MIN_CLASSES_PER_WORKER: below this many classes a worker, handing a part + // to a pool thread costs more than scanning it on the caller's thread. + static final int MIN_CLASSES_PER_WORKER = 2048; + + // MAX_WORKERS bounds the threads that scan, the caller's included. + static final int MAX_WORKERS = 4; + + /** A class that was hit: the probes hit, in ascending order. */ + static final class Hit { + + final String name; + final long id; + final int probeCount; + final int[] probes; + + private Hit(String name, long id, int probeCount, int[] probes) { + this.name = name; + this.id = id; + this.probeCount = probeCount; + this.probes = probes; + } + } + + // One of the app's classes as JaCoCo holds it. + private static final class Slot { + + final String name; + final long id; + final boolean[] probes; + + private Slot(String name, long id, boolean[] probes) { + this.name = name; + this.id = id; + this.probes = probes; + } + } + + private static final Comparator BY_NAME_THEN_ID = new Comparator() { + @Override + public int compare(Slot a, Slot b) { + int byName = a.name.compareTo(b.name); + return byName != 0 ? byName : Long.compare(a.id, b.id); + } + }; + + // JaCoCo's ExecutionDataStore: JaCoCo locks on it to add a class or to + // read or reset them all, and so does refresh. + private final Object store; + // Its map of class id to ExecutionData. + private final Map entries; + private final int workers; + private final int minPerWorker; + // The threads that scan the parts other than the caller's: workers - 1 + // of them, made on the first pass that splits, and stopped after a + // minute idle. + private ThreadPoolExecutor pool; + + // Read and written under this object's lock (pass is synchronized). + private Method getName; + private Method getId; + private Method getProbes; + // The store's ids already looked at, the app's classes and the others. + private final Set seen = new HashSet<>(); + // The store's size when refresh last looked at it: classes are only + // ever added to it. + private int known; + // The app's classes JaCoCo has seen, sorted by name then id, and their + // probe arrays at the same index. + private Slot[] slots = new Slot[0]; + private boolean[][] arrays = new boolean[0][]; + private int maxProbes; + + ProbeScan(Object store, Map entries, int workers, int minPerWorker) { + this.store = store; + this.entries = entries; + this.workers = Math.max(1, Math.min(MAX_WORKERS, workers)); + this.minPerWorker = Math.max(1, minPerWorker); + } + + /** + * locate finds the store of the JaCoCo agent object that RT.getAgent() + * returned, and makes a ProbeScan over it that uses min(4, cores / 2) + * threads. It throws a ReflectiveOperationException or a + * RuntimeException when the agent is not laid out as JaCoCo's. + */ + static ProbeScan locate(Object agent) throws ReflectiveOperationException { + Object data = fieldOfType(agent, "RuntimeData"); + Object store = fieldOfType(data, "ExecutionDataStore"); + // A field of a type named Map that is not java.util.Map throws a + // ClassCastException here, a refusal too. + Map entries = (Map) fieldOfType(store, "Map"); + return new ProbeScan(store, entries, defaultWorkers(), MIN_CLASSES_PER_WORKER); + } + + // defaultWorkers is min(4, cores / 2), and at least 1: the app runs the + // test's request on the other cores, and the scan must not slow it down. + static int defaultWorkers() { + return Math.max(1, Math.min(MAX_WORKERS, Runtime.getRuntime().availableProcessors() / 2)); + } + + // fieldOfType returns the value of owner's one instance field whose type + // has the simple name typeName, looking in its class and + // then its superclasses. Two such fields in one class, or none, or a null + // value, throw: the layout is not the one this was written for. + private static Object fieldOfType(Object owner, String typeName) throws ReflectiveOperationException { + for (Class type = owner.getClass(); type != null; type = type.getSuperclass()) { + Field found = null; + for (Field field : type.getDeclaredFields()) { + if (Modifier.isStatic(field.getModifiers()) || !field.getType().getSimpleName().equals(typeName)) { + continue; + } + if (found != null) { + throw new NoSuchFieldException("two " + typeName + " fields in " + type.getName()); + } + found = field; + } + if (found != null) { + found.setAccessible(true); + Object value = found.get(owner); + if (value == null) { + throw new NoSuchFieldException(type.getName() + "." + found.getName() + " is null"); + } + return value; + } + } + throw new NoSuchFieldException("no " + typeName + " field in " + owner.getClass().getName()); + } + + /** + * LostHitsException says a pass failed after it may have cleared probes: + * the hits it cleared are gone, so the END it served must not be taken, + * and the scan must not be used again (JaCoCo's reset clears the rest). + * Every part of the pass has ended when it is thrown. + */ + static final class LostHitsException extends IllegalStateException { + + private static final long serialVersionUID = 1L; + + LostHitsException(Throwable cause) { + super("a part of the probe scan failed after it may have cleared probes", cause); + } + } + + /** + * pass returns the app's classes that were hit since the last pass, sorted + * by name then id, and clears those hits. app is the VM names of the app's + * classes; it must be the same set on every call, as a class is looked up + * in it once, when JaCoCo first holds it. + * + *

It fails in one of two ways. Reading JaCoCo's classes fails before + * any probe is cleared: a ReflectiveOperationException, or a + * RuntimeException other than LostHitsException (an Error there is + * wrapped in one), and JaCoCo's dump still holds every hit. A part of the + * scan fails (an Error, such as out of memory): LostHitsException, once + * every part has ended. After either failure the scan must not be used + * again: a failed read of JaCoCo's classes leaves out classes it will + * not look up again. + */ + synchronized List pass(Set app) throws ReflectiveOperationException { + try { + refresh(app); + } catch (Error e) { + throw new IllegalStateException("reading JaCoCo's classes failed", e); + } + final Slot[] slots = this.slots; + final boolean[][] arrays = this.arrays; + final int maxProbes = this.maxProbes; + // The caller scans the first part, and the pool the others. A part + // the pool cannot take (no thread can be made) is scanned on the + // caller's thread: a FutureTask runs once, so a part the pool queued + // anyway is not scanned twice. + int parts = Math.max(1, Math.min(workers, slots.length / minPerWorker)); + int size = (slots.length + parts - 1) / parts; + List>> tasks = new ArrayList<>(parts); + for (int part = 0; part < parts; part++) { + final int from = Math.min(slots.length, part * size); + final int to = Math.min(slots.length, from + size); + tasks.add(new FutureTask<>(() -> scan(slots, arrays, maxProbes, from, to))); + } + for (int part = 1; part < parts; part++) { + submit(tasks.get(part)); + } + tasks.get(0).run(); + return awaitAll(tasks); + } + + // submit hands task to the pool, or runs it here when the pool cannot + // take it: creating the pool or its thread fails (out of memory, or the + // container's limit on threads). + private void submit(FutureTask> task) { + try { + pool().execute(task); + } catch (Throwable t) { + task.run(); + } + } + + // awaitAll waits for every part, so that no part still clears probes once + // pass has returned, and returns their hits in the order of the parts. + // When a part failed, it throws LostHitsException with the first failure, + // once every part has ended. + private static List awaitAll(List>> parts) { + List hits = new ArrayList<>(); + Throwable failure = null; + boolean interrupted = false; + try { + for (FutureTask> part : parts) { + while (true) { + try { + hits.addAll(part.get()); + break; + } catch (InterruptedException e) { + // Wait on: the part is clearing probes. Interrupted + // again once every part is done. + interrupted = true; + } catch (ExecutionException e) { + if (failure == null) { + failure = e.getCause(); + } + break; + } + } + } + } finally { + if (interrupted) { + Thread.currentThread().interrupt(); + } + } + if (failure != null) { + throw new LostHitsException(failure); + } + return hits; + } + + // scan notes and clears the hits of slots [from, to). A probe is cleared + // only once read as hit, so a hit that races the scan is in it or is + // left for the next one. maxProbes is the most probes any class has. + private static List scan(Slot[] slots, boolean[][] arrays, int maxProbes, int from, int to) { + List hits = null; + int[] scratch = new int[maxProbes]; + for (int k = from; k < to; k++) { + boolean[] probes = arrays[k]; + int count = 0; + for (int i = 0; i < probes.length; i++) { + if (probes[i]) { + probes[i] = false; + scratch[count++] = i; + } + } + if (count > 0) { + if (hits == null) { + hits = new ArrayList<>(); + } + Slot slot = slots[k]; + hits.add(new Hit(slot.name, slot.id, probes.length, Arrays.copyOf(scratch, count))); + } + } + return hits == null ? Collections.emptyList() : hits; + } + + // refresh adds the app's classes JaCoCo has come to hold since the last + // call. The store only grows, so an unchanged size means nothing new. + private void refresh(Set app) throws ReflectiveOperationException { + List added; + synchronized (store) { + int size = entries.size(); + if (size == known) { + return; + } + added = new ArrayList<>(Math.max(0, size - known)); + for (Map.Entry entry : entries.entrySet()) { + if (seen.add(entry.getKey())) { + added.add(entry.getValue()); + } + } + known = size; + } + List fresh = new ArrayList<>(); + for (Object data : added) { + if (getName == null) { + getName = data.getClass().getMethod("getName"); + getId = data.getClass().getMethod("getId"); + getProbes = data.getClass().getMethod("getProbes"); + } + String name = (String) getName.invoke(data); + if (app.contains(name)) { + boolean[] probes = (boolean[]) getProbes.invoke(data); + fresh.add(new Slot(name, (Long) getId.invoke(data), probes)); + maxProbes = Math.max(maxProbes, probes.length); + } + } + Slot[] grown = Arrays.copyOf(slots, slots.length + fresh.size()); + for (int i = 0; i < fresh.size(); i++) { + grown[slots.length + i] = fresh.get(i); + } + Arrays.sort(grown, BY_NAME_THEN_ID); + boolean[][] grownArrays = new boolean[grown.length][]; + for (int i = 0; i < grown.length; i++) { + grownArrays[i] = grown[i].probes; + } + slots = grown; + arrays = grownArrays; + } + + // classes is how many of the app's classes the scan holds. + synchronized int classes() { + return slots.length; + } + + private ExecutorService pool() { + if (pool == null) { + final AtomicInteger made = new AtomicInteger(); + ThreadPoolExecutor created = new ThreadPoolExecutor(workers - 1, workers - 1, 60, TimeUnit.SECONDS, + new LinkedBlockingQueue(), runnable -> { + Thread thread = new Thread(runnable, "keploy-java-dedup-scan-" + made.incrementAndGet()); + thread.setDaemon(true); + return thread; + }); + created.allowCoreThreadTimeOut(true); + pool = created; + } + return pool; + } +} diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java new file mode 100644 index 00000000..3bdfc388 --- /dev/null +++ b/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java @@ -0,0 +1,523 @@ +package io.keploy.dedup; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.logging.Handler; +import java.util.logging.LogRecord; +import java.util.logging.Logger; +import org.jacoco.core.data.ExecutionDataWriter; +import org.jacoco.core.runtime.RuntimeData; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.newsclub.net.unix.AFUNIXServerSocket; +import org.newsclub.net.unix.AFUNIXSocket; +import org.newsclub.net.unix.AFUNIXSocketAddress; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.commons.ClassRemapper; +import org.objectweb.asm.commons.SimpleRemapper; + +/** + * With JaCoCo's agent in the app's JVM, a START clears what ran since the last + * test as background and an END reports the test's own hits, each in one pass + * over the app's classes. When that pass fails, the SDK goes back to JaCoCo's + * dump and reset for good. + */ +public class InProcessScanTest { + + private static final String[] PROPERTIES = {"keploy.java.dedup.diagnostics", "keploy.java.class.dirs", + "keploy.jacoco.port", "keploy.coverage.endpoint", "keploy.java.dedup.warmup.disabled", + "keploy.build.tag"}; + private static final String TAKEN = "ACK proto=2\n"; + private static final String IGNORED = "IGNORED proto=2\n"; + private static final String SHOP = "com/acme/Shop"; + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + private String savedControlPath; + private String savedDataPath; + private ClassLoader savedContextLoader; + private Agent agent; + private final List logged = new CopyOnWriteArrayList<>(); + private final Handler handler = new Handler() { + @Override + public void publish(LogRecord record) { + logged.add(record.getMessage()); + } + + @Override + public void flush() { + } + + @Override + public void close() { + } + }; + + // Agent is JaCoCo's agent as the SDK sees it: getExecutionData and reset, + // over a RuntimeData whose probe arrays the app's classes write to. + public static final class Agent { + + private final RuntimeData data = new RuntimeData(); + + public byte[] getExecutionData(boolean reset) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ExecutionDataWriter writer = new ExecutionDataWriter(out); + data.collect(writer, writer, reset); + return out.toByteArray(); + } + + public void reset() { + data.reset(); + } + + boolean[] probes(long id, String name, int count) { + return data.getExecutionData(id, name, count).getProbes(); + } + } + + @Before + public void setUp() throws IOException, ReflectiveOperationException { + for (String name : PROPERTIES) { + System.clearProperty(name); + } + File classes = tmp.newFolder("classes"); + File shop = new File(classes, SHOP + ".class"); + shop.getParentFile().mkdirs(); + Files.write(shop.toPath(), new byte[] {1}); + System.setProperty("keploy.java.class.dirs", classes.getPath()); + // A port nothing listens on: no command may reach JaCoCo over TCP. + System.setProperty("keploy.jacoco.port", "1"); + savedControlPath = KeployDedupAgent.controlSocketPath; + savedDataPath = KeployDedupAgent.dataSocketPath; + File sockets = tmp.newFolder("sockets"); + KeployDedupAgent.controlSocketPath = new File(sockets, "control.sock").getAbsolutePath(); + KeployDedupAgent.dataSocketPath = new File(sockets, "data.sock").getAbsolutePath(); + Logger.getLogger(KeployDedupAgent.class.getName()).addHandler(handler); + agent = new Agent(); + // JaCoCo's RT, whose getAgent() returns this agent, in a loader of + // its own: the context loader of this thread and so of the worker + // KeployDedupAgent.start() makes, which the SDK looks for RT in. + savedContextLoader = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(new RtLoader(agent, getClass().getClassLoader())); + } + + @After + public void tearDown() { + KeployDedupAgent.stop(); + Thread.currentThread().setContextClassLoader(savedContextLoader); + Logger.getLogger(KeployDedupAgent.class.getName()).removeHandler(handler); + KeployDedupAgent.controlSocketPath = savedControlPath; + KeployDedupAgent.dataSocketPath = savedDataPath; + for (String name : PROPERTIES) { + System.clearProperty(name); + } + } + + @Test + public void eachTestGetsItsOwnHitsAndNoneOfTheBackground() throws Exception { + System.setProperty("keploy.java.dedup.diagnostics", "true"); + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] lib = agent.probes(0x99L, "org/lib/Lib", 2); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + + // Before the test: a health check runs Shop's probe 0, and a library. + shop[0] = true; + lib[0] = true; + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertTrue(logged(" START test-set-0/test-1 took ").contains(" scan=")); + assertFalse(logged(" START test-set-0/test-1 took ").contains("reset=")); + assertNotNull(logged("background hits cleared at START: classes=1, probes=1, sampleClasses=[" + + SHOP + "]")); + + shop[2] = true; + shop[3] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[2,3]}}", + data.next()); + String end = logged(" END test-set-0/test-1 took "); + assertTrue(end, end.contains(" scan=") && end.contains(" filter=") && !end.contains("dump=")); + assertTrue("a library class is left alone", lib[0]); + + // Nothing ran between the tests, or in the second one. + assertEquals(TAKEN, send("START test-set-0/test-2")); + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals("{\"id\":\"test-set-0/test-2\",\"executedLinesByFile\":{}}", data.next()); + assertEquals("one background line, for the first START", 1, count("background hits cleared")); + } + } + + @Test + public void aClassLoadedTwiceReportsTheOneWithTheLowerId() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + // The same class from other bytes, in a second class loader. + boolean[] twin = agent.probes(0x99L, SHOP, 3); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + shop[3] = true; + twin[1] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1]}}", + data.next()); + assertFalse("both were cleared", shop[3] || twin[1]); + } + } + + @Test + public void withDiagnosticsOffTheBackgroundIsNotLogged() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + shop[0] = true; + assertEquals(TAKEN, send("START test-set-0/test-1")); + shop[1] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1]}}", + data.next()); + assertEquals(0, count("Java dedup diagnostics")); + } + } + + @Test + public void anEndWhoseScanFailsTakesJacocosDumpFromThenOn() throws Exception { + System.setProperty("keploy.java.dedup.diagnostics", "true"); + ProbeScanTest.CountingMap entries = ProbeScanTest.CountingMap.swapInto(agent.data); + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertTrue(logged(" START test-set-0/test-1 took ").contains(" scan=")); + + // The test loads a class, so the END walks JaCoCo's store, and the walk fails. + shop[1] = true; + entries.fail = true; + agent.probes(0x77L, "org/lib/Loaded", 2); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1]}}", + data.next()); + assertTrue(logged(" END test-set-0/test-1 took ").contains("dump=")); + assertNotNull(logged("Keploy dedup: the probe scan failed; each test now takes JaCoCo's dump and reset")); + + // The store could be walked again: the scan stays dropped all the same. + entries.fail = false; + assertEquals(TAKEN, send("START test-set-0/test-2")); + assertTrue(logged(" START test-set-0/test-2 took ").contains(" reset=")); + shop[2] = true; + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals("{\"id\":\"test-set-0/test-2\",\"executedLinesByFile\":{\"" + SHOP + "\":[2]}}", + data.next()); + assertTrue(logged(" END test-set-0/test-2 took ").contains("dump=")); + } + } + + @Test + public void aStartWhoseScanFailsResetsInstead() throws Exception { + System.setProperty("keploy.java.dedup.diagnostics", "true"); + ProbeScanTest.CountingMap entries = ProbeScanTest.CountingMap.swapInto(agent.data); + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + entries.fail = true; + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + shop[0] = true; + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertTrue(logged(" START test-set-0/test-1 took ").contains(" reset=")); + assertFalse("JaCoCo's reset cleared the background", shop[0]); + shop[3] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[3]}}", + data.next()); + } + } + + @Test + public void aClassLoadedTwiceReportsTheOneTheManifestNames() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] twin = agent.probes(0x99L, SHOP, 3); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + // Only the copy with the higher id runs first: the manifest names it. + assertEquals(TAKEN, send("START test-set-0/test-1")); + shop[3] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[3]}}", + data.next()); + // Both run: the probes sent are still that copy's. + assertEquals(TAKEN, send("START test-set-0/test-2")); + shop[2] = true; + twin[0] = true; + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals("{\"id\":\"test-set-0/test-2\",\"executedLinesByFile\":{\"" + SHOP + "\":[2]}}", + data.next()); + } + } + + @Test + public void aClassLoadedTwiceSendsTheOtherCopysProbesWhenOnlyItRan() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] twin = agent.probes(0x99L, SHOP, 3); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + // Only the copy with the higher id runs first: the manifest names it. + assertEquals(TAKEN, send("START test-set-0/test-1")); + shop[3] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[3]}}", + data.next()); + // Only the other copy runs: its probes are sent under the class's + // name, as JaCoCo's dump sends them, not dropped. + assertEquals(TAKEN, send("START test-set-0/test-2")); + twin[2] = true; + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals("{\"id\":\"test-set-0/test-2\",\"executedLinesByFile\":{\"" + SHOP + "\":[2]}}", + data.next()); + assertFalse("the copy's hit was cleared", twin[2]); + } + } + + @Test + public void anEndWhoseScanLostHitsIsNotTaken() throws Exception { + System.setProperty("keploy.java.dedup.diagnostics", "true"); + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + shop[1] = true; + ProbeScanTest.breakSlot(scanOf(), 0); + // JaCoCo's dump would hold only what the scan did not clear: not taken. + assertEquals(IGNORED, send("END test-set-0/test-1")); + assertNotNull(logged("Keploy dedup: the probe scan failed; each test now takes JaCoCo's dump and reset")); + + // JaCoCo's reset clears what the failed pass left. + assertEquals(TAKEN, send("START test-set-0/test-2")); + assertTrue(logged(" START test-set-0/test-2 took ").contains(" reset=")); + shop[2] = true; + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals("no record for the END not taken", + "{\"id\":\"test-set-0/test-2\",\"executedLinesByFile\":{\"" + SHOP + "\":[2]}}", + data.next()); + } + } + + @Test + public void anErrorReadingJacocosClassesAtAnEndTakesJacocosDump() throws Exception { + ProbeScanTest.CountingMap entries = ProbeScanTest.CountingMap.swapInto(agent.data); + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + shop[1] = true; + entries.error = new NoClassDefFoundError("org/jacoco/core/data/ExecutionData"); + agent.probes(0x77L, "org/lib/Loaded", 2); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1]}}", + data.next()); + entries.error = null; + assertEquals(TAKEN, send("START test-set-0/test-2")); + } + } + + @Test + public void anAgentWhoseFieldsCannotBeReadKeepsTheServer() throws Exception { + Thread.currentThread().setContextClassLoader(new RtLoader(brokenAgent(), getClass().getClassLoader())); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertNotNull(logged("Keploy dedup: JaCoCo's probes cannot be read in place")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{}}", data.next()); + } + } + + // brokenAgent is an agent with JaCoCo's getExecutionData and reset, and a + // field of a type that is not there: reading its fields throws + // NoClassDefFoundError. + private static Object brokenAgent() throws ReflectiveOperationException { + ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); + writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, "test/BrokenAgent", null, + "java/lang/Object", null); + writer.visitField(Opcodes.ACC_PRIVATE, "data", "Lmissing/RuntimeData;", null, null).visitEnd(); + MethodVisitor init = writer.visitMethod(Opcodes.ACC_PUBLIC, "", "()V", null, null); + init.visitCode(); + init.visitVarInsn(Opcodes.ALOAD, 0); + init.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V", false); + init.visitInsn(Opcodes.RETURN); + init.visitMaxs(0, 0); + init.visitEnd(); + MethodVisitor dump = writer.visitMethod(Opcodes.ACC_PUBLIC, "getExecutionData", "(Z)[B", null, null); + dump.visitCode(); + dump.visitInsn(Opcodes.ICONST_0); + dump.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_BYTE); + dump.visitInsn(Opcodes.ARETURN); + dump.visitMaxs(0, 0); + dump.visitEnd(); + MethodVisitor reset = writer.visitMethod(Opcodes.ACC_PUBLIC, "reset", "()V", null, null); + reset.visitCode(); + reset.visitInsn(Opcodes.RETURN); + reset.visitMaxs(0, 0); + reset.visitEnd(); + writer.visitEnd(); + final byte[] bytes = writer.toByteArray(); + ClassLoader loader = new ClassLoader(InProcessScanTest.class.getClassLoader()) { + @Override + protected Class findClass(String name) throws ClassNotFoundException { + if (!name.equals("test.BrokenAgent")) { + throw new ClassNotFoundException(name); + } + return defineClass(name, bytes, 0, bytes.length); + } + }; + return Class.forName("test.BrokenAgent", true, loader).getConstructor().newInstance(); + } + + // scanOf is the ProbeScan the running agent's collector looked up. + private static ProbeScan scanOf() throws ReflectiveOperationException { + Object worker = read(KeployDedupAgent.class, null, "coverageWorker"); + Object collector = read(worker.getClass(), worker, "collector"); + ProbeScan scan = (ProbeScan) read(collector.getClass(), collector, "probeScan"); + assertNotNull("the collector scans JaCoCo's probes in place", scan); + return scan; + } + + private static Object read(Class type, Object owner, String name) throws ReflectiveOperationException { + java.lang.reflect.Field field = type.getDeclaredField(name); + field.setAccessible(true); + return field.get(owner); + } + + // logged waits for the log line holding text, and returns it. + private String logged(String text) throws InterruptedException { + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10); + while (System.nanoTime() < deadline) { + for (String line : logged) { + if (line.contains(text)) { + return line; + } + } + Thread.sleep(10); + } + throw new AssertionError("no log line holds \"" + text + "\" in " + logged); + } + + private int count(String text) { + int n = 0; + for (String line : logged) { + if (line.contains(text)) { + n++; + } + } + return n; + } + + private static String send(String command) throws IOException { + try (AFUNIXSocket socket = AFUNIXSocket.newInstance()) { + socket.connect(AFUNIXSocketAddress.of(new File(KeployDedupAgent.controlSocketPath)), 3000); + socket.setSoTimeout(30000); + OutputStream out = socket.getOutputStream(); + out.write((command + "\n").getBytes(StandardCharsets.UTF_8)); + out.flush(); + return new String(readAll(socket.getInputStream()), StandardCharsets.UTF_8); + } + } + + private static byte[] readAll(InputStream in) throws IOException { + ByteArrayOutputStream all = new ByteArrayOutputStream(); + byte[] buffer = new byte[8192]; + int n; + while ((n = in.read(buffer)) != -1) { + all.write(buffer, 0, n); + } + return all.toByteArray(); + } + + // RtLoader defines JacocoRtStub as org.jacoco.agent.rt.RT, with its agent + // set, and leaves every other class to its parent, which has no RT. + private static final class RtLoader extends ClassLoader { + + private static final String RT = "org.jacoco.agent.rt.RT"; + + RtLoader(Object agent, ClassLoader parent) throws ReflectiveOperationException { + super(parent); + Class.forName(RT, true, this).getField("agent").set(null, agent); + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + if (!name.equals(RT)) { + throw new ClassNotFoundException(name); + } + String stub = JacocoRtStub.class.getName().replace('.', '/'); + try (InputStream in = getParent().getResourceAsStream(stub + ".class")) { + ClassWriter writer = new ClassWriter(0); + new ClassReader(readAll(in)).accept( + new ClassRemapper(writer, new SimpleRemapper(stub, RT.replace('.', '/'))), 0); + byte[] bytes = writer.toByteArray(); + return defineClass(name, bytes, 0, bytes.length); + } catch (IOException e) { + throw new ClassNotFoundException(name, e); + } + } + } + + private static final class DataReceiver implements Closeable { + + private final AFUNIXServerSocket server; + private final BlockingQueue payloads = new LinkedBlockingQueue<>(); + + DataReceiver(String path) throws IOException { + server = AFUNIXServerSocket.newInstance(); + server.bind(AFUNIXSocketAddress.of(new File(path)), 50); + Thread thread = new Thread(this::serve, "data-receiver"); + thread.setDaemon(true); + thread.start(); + } + + private void serve() { + while (!server.isClosed()) { + try (Socket socket = server.accept()) { + socket.setSoTimeout(30000); + String payload = new String(readAll(socket.getInputStream()), StandardCharsets.UTF_8); + // The INDEX report (ControlSocketTest's rows) is not a test's coverage. + if (!payload.startsWith("{\"kind\":\"index\",")) { + payloads.add(payload); + } + } catch (IOException e) { + // Closed. + } + } + } + + String next() throws InterruptedException { + return payloads.poll(30, TimeUnit.SECONDS); + } + + @Override + public void close() throws IOException { + server.close(); + } + } +} diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/JacocoRtStub.java b/keploy-sdk/src/test/java/io/keploy/dedup/JacocoRtStub.java new file mode 100644 index 00000000..dc218062 --- /dev/null +++ b/keploy-sdk/src/test/java/io/keploy/dedup/JacocoRtStub.java @@ -0,0 +1,23 @@ +package io.keploy.dedup; + +/** + * A stand-in for JaCoCo's org.jacoco.agent.rt.RT in the tests: the SDK finds + * JaCoCo's agent in its own JVM through RT.getAgent(). It is never on the + * test classpath under RT's name, since a class there named + * org/jacoco/agent/rt/RT.class tells the SDK, in this JVM and in every JVM a + * test starts with this classpath, that a JaCoCo agent is attached + * (EmbeddedJacoco.attached). InProcessScanTest defines it as RT in a class + * loader of its own, the thread's context loader, which the SDK looks in. + */ +public final class JacocoRtStub { + + // agent is what getAgent returns. + public static volatile Object agent; + + private JacocoRtStub() { + } + + public static Object getAgent() { + return agent; + } +} diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/PhasesTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/PhasesTest.java index f382da68..d5872f97 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/PhasesTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/PhasesTest.java @@ -125,6 +125,7 @@ public void withDiagnosticsOnEachCommandSaysHowLongEachStepTook() throws Excepti + MS + "ms reset=" + MS + "ms")); assertStepsAddUp(awaitLogged("Java dedup diagnostics: END test-set-0/test-1 took " + MS + " ms: dump=" + MS + "ms parse=" + MS + "ms filter=" + MS + "ms json=" + MS + "ms send=" + MS + "ms")); + assertNoProbeScan(); } @Test @@ -155,6 +156,15 @@ public void overTcpEachCommandSaysHowLongEachStepTookToo() throws Exception { assertStepsAddUp(awaitLogged("Java dedup diagnostics: END test-set-0/test-1 took " + MS + " ms: dump=" + MS + "ms parse=" + MS + "ms filter=" + MS + "ms json=" + MS + "ms send=" + MS + "ms classes=" + MS + "ms")); + assertNoProbeScan(); + } + + // With JaCoCo reached over TCP there is no probe scan to look up or to + // drop, and nothing says there was. + private void assertNoProbeScan() { + for (String line : logged) { + assertTrue("logged: " + line, !line.contains("probe scan") && !line.contains("read in place")); + } } @Test diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/ProbeScanTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/ProbeScanTest.java new file mode 100644 index 00000000..b5a04623 --- /dev/null +++ b/keploy-sdk/src/test/java/io/keploy/dedup/ProbeScanTest.java @@ -0,0 +1,594 @@ +package io.keploy.dedup; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import org.jacoco.core.runtime.RuntimeData; +import org.junit.Test; + +/** + * One pass over the app classes' live probe arrays: each hit is reported by + * exactly one pass and cleared by it, library classes are left alone, and a + * hit made while a pass runs is in that pass or the next, never lost and + * never twice. + */ +public class ProbeScanTest { + + private static final Set APP = new HashSet<>(Arrays.asList("app/A", "app/B", "app/C")); + + // An agent as JaCoCo's: its RuntimeData holds the store the instrumented + // classes get their probe arrays from. + static final class Agent { + private final RuntimeData data = new RuntimeData(); + } + + // probes is the array JaCoCo hands the instrumented class name (id) to write its hits into. + private static boolean[] probes(Agent agent, long id, String name, int count) { + return agent.data.getExecutionData(id, name, count).getProbes(); + } + + private static Map> asMap(List hits) { + Map> byName = new HashMap<>(); + for (ProbeScan.Hit hit : hits) { + List probes = new ArrayList<>(); + for (int probe : hit.probes) { + probes.add(probe); + } + byName.put(hit.name, probes); + } + return byName; + } + + private static List names(List hits) { + List names = new ArrayList<>(); + for (ProbeScan.Hit hit : hits) { + names.add(hit.name); + } + return names; + } + + @Test + public void aHitIsReportedOnceThenCleared() throws Exception { + Agent agent = new Agent(); + boolean[] a = probes(agent, 1, "app/A", 5); + boolean[] b = probes(agent, 2, "app/B", 3); + boolean[] lib = probes(agent, 3, "lib/L", 4); + // More probes than any other class: its hits need the most room. + boolean[] c = probes(agent, 4, "app/C", 300); + ProbeScan scan = ProbeScan.locate(agent); + + a[1] = true; + a[3] = true; + lib[0] = true; + c[5] = true; + c[299] = true; + List hits = scan.pass(APP); + + assertEquals(2, hits.size()); + assertEquals("app/C", hits.get(1).name); + assertArrayEquals(new int[] {5, 299}, hits.get(1).probes); + assertArrayEquals(new boolean[300], c); + ProbeScan.Hit hit = hits.get(0); + assertEquals("app/A", hit.name); + assertEquals(1L, hit.id); + assertEquals(5, hit.probeCount); + assertArrayEquals(new int[] {1, 3}, hit.probes); + assertArrayEquals("the pass cleared what it reported", new boolean[5], a); + assertArrayEquals(new boolean[3], b); + assertTrue("a library class is neither reported nor cleared", lib[0]); + assertTrue("nothing was hit since", scan.pass(APP).isEmpty()); + assertEquals(3, scan.classes()); + } + + @Test + public void backgroundHitsNeverReachTheNextTest() throws Exception { + Agent agent = new Agent(); + boolean[] a = probes(agent, 1, "app/A", 4); + boolean[] b = probes(agent, 2, "app/B", 4); + ProbeScan scan = ProbeScan.locate(agent); + + // Between two tests: a scheduled job runs A's probe 0 and B's 2. + a[0] = true; + b[2] = true; + Map> background = asMap(scan.pass(APP)); + assertEquals(Arrays.asList(0), background.get("app/A")); + assertEquals(Arrays.asList(2), background.get("app/B")); + + // The test runs A's probes 1 and 2 only. + a[1] = true; + a[2] = true; + Map> test = asMap(scan.pass(APP)); + assertEquals(Collections.singletonMap("app/A", Arrays.asList(1, 2)), test); + } + + @Test + public void aClassLoadedAfterAPassIsScannedByTheNext() throws Exception { + Agent agent = new Agent(); + probes(agent, 1, "app/A", 2); + ProbeScan scan = ProbeScan.locate(agent); + assertTrue(scan.pass(APP).isEmpty()); + + boolean[] c = probes(agent, 7, "app/C", 3); + boolean[] lib = probes(agent, 8, "lib/M", 3); + c[2] = true; + lib[1] = true; + List hits = scan.pass(APP); + assertEquals(1, hits.size()); + assertEquals(Collections.singletonMap("app/C", Arrays.asList(2)), asMap(hits)); + assertEquals(2, scan.classes()); + assertTrue(lib[1]); + } + + @Test + public void aPassWithNoClassLoadedSinceDoesNotWalkJacocosStore() throws Exception { + Agent agent = new Agent(); + CountingMap entries = CountingMap.swapInto(agent.data); + boolean[] a = probes(agent, 1, "app/A", 2); + probes(agent, 2, "lib/L", 2); + ProbeScan scan = ProbeScan.locate(agent); + + scan.pass(APP); + assertEquals(1, entries.walks.get()); + a[0] = true; + assertEquals(1, scan.pass(APP).size()); + assertEquals("the store did not grow", 1, entries.walks.get()); + probes(agent, 3, "lib/M", 2); + scan.pass(APP); + assertEquals(2, entries.walks.get()); + } + + // CountingMap stands in for the map of JaCoCo's store: it counts the + // walks over it, and fails them when told to. + static final class CountingMap extends HashMap { + + final java.util.concurrent.atomic.AtomicInteger walks = new java.util.concurrent.atomic.AtomicInteger(); + volatile boolean fail; + // error, when set, is thrown by the walk in place of fail's exception. + volatile Error error; + + // swapInto puts an empty CountingMap in place of the map of data's + // store, before any class is added to it. + static CountingMap swapInto(RuntimeData data) throws ReflectiveOperationException { + Object store = storeOf(data); + java.lang.reflect.Field entries = store.getClass().getDeclaredField("entries"); + entries.setAccessible(true); + CountingMap map = new CountingMap(); + entries.set(store, map); + return map; + } + + @Override + public Set> entrySet() { + walks.incrementAndGet(); + if (error != null) { + throw error; + } + if (fail) { + throw new IllegalStateException("the walk over the store failed"); + } + return super.entrySet(); + } + } + + @Test + public void hitsComeSortedByNameThenIdAcrossEveryPart() throws Exception { + Agent agent = new Agent(); + Set app = new HashSet<>(); + List arrays = new ArrayList<>(); + // 103 classes, added out of order, split in 4 parts of 26, 26, 26 and 25. + for (int i = 102; i >= 0; i--) { + String name = String.format("app/K%03d", i); + app.add(name); + arrays.add(probes(agent, 1000 + i, name, 2)); + } + // A second class of one name (loaded twice, from other bytes). + boolean[] twin = probes(agent, 1, "app/K050", 3); + ProbeScan scan = new ProbeScan(storeOf(agent), entriesOf(agent), 4, 1); + for (boolean[] probes : arrays) { + probes[1] = true; + } + twin[2] = true; + + List hits = scan.pass(app); + + assertEquals(104, hits.size()); + List expected = new ArrayList<>(); + for (int i = 0; i <= 102; i++) { + expected.add(String.format("app/K%03d", i)); + if (i == 50) { + expected.add("app/K050"); + } + } + assertEquals(expected, names(hits)); + assertEquals("the twin's lower id comes first", 1L, hits.get(50).id); + assertEquals(1050L, hits.get(51).id); + for (boolean[] probes : arrays) { + assertFalse(probes[1]); + } + assertTrue(scan.pass(app).isEmpty()); + } + + @Test + public void aHitRacingThePassesIsReportedExactlyOnce() throws Exception { + final Agent agent = new Agent(); + final int classes = 128; + final int probeCount = 128; + Set app = new HashSet<>(); + final boolean[][] arrays = new boolean[classes][]; + for (int i = 0; i < classes; i++) { + String name = "app/R" + i; + app.add(name); + arrays[i] = probes(agent, 100 + i, name, probeCount); + } + Set before = Thread.getAllStackTraces().keySet(); + ProbeScan scan = new ProbeScan(storeOf(agent), entriesOf(agent), 4, 8); + assertTrue(scan.pass(app).isEmpty()); + + // The app thread sets each probe once, in a random order, while the + // passes run. + final List order = new ArrayList<>(); + for (int c = 0; c < classes; c++) { + for (int p = 0; p < probeCount; p++) { + order.add(new int[] {c, p}); + } + } + Collections.shuffle(order, new Random(42)); + // The writes go in batches, and after each batch the writer waits for + // two passes that ended after the batch began: at least one of them + // runs after some of the batch's writes, however busy the box is. + final int batch = 512; + final AtomicInteger passCount = new AtomicInteger(); + final AtomicBoolean done = new AtomicBoolean(); + final AtomicBoolean stop = new AtomicBoolean(); + Thread app1 = new Thread(() -> { + int begun = passCount.get(); + for (int i = 0; i < order.size(); i++) { + int[] hit = order.get(i); + arrays[hit[0]][hit[1]] = true; + // About a microsecond between two hits, so that the passes + // also run while a batch is written. + long until = System.nanoTime() + 1000; + while (System.nanoTime() < until) { + Thread.yield(); + } + if ((i + 1) % batch == 0) { + while (passCount.get() < begun + 2 && !stop.get()) { + Thread.yield(); + } + begun = passCount.get(); + } + } + done.set(true); + }, "probe-writer"); + app1.setDaemon(true); + + Map reported = new HashMap<>(); + app1.start(); + int passes = 0; + try { + while (!done.get()) { + count(scan.pass(app), reported); + passes = passCount.incrementAndGet(); + } + } finally { + stop.set(true); + } + app1.join(); + count(scan.pass(app), reported); + assertFalse("no pass leaves the caller interrupted", Thread.interrupted()); + int scanThreads = 0; + for (Thread thread : Thread.getAllStackTraces().keySet()) { + if (!before.contains(thread) && thread.getName().startsWith("keploy-java-dedup-scan-")) { + scanThreads++; + assertTrue(thread.isDaemon()); + } + } + assertTrue(scanThreads + " scan threads over " + passes + " passes", scanThreads >= 1 && scanThreads <= 3); + assertTrue("the passes ran through the writes: " + passes, passes >= 2 * order.size() / batch); + + for (int c = 0; c < classes; c++) { + int[] times = reported.get("app/R" + c); + for (int p = 0; p < probeCount; p++) { + assertEquals("app/R" + c + " probe " + p + " (over " + passes + " passes)", 1, + times == null ? 0 : times[p]); + } + } + } + + private static void count(List hits, Map reported) { + for (ProbeScan.Hit hit : hits) { + int[] times = reported.get(hit.name); + if (times == null) { + times = new int[hit.probeCount]; + reported.put(hit.name, times); + } + for (int probe : hit.probes) { + times[probe]++; + } + } + } + + @Test + public void anAgentNotLaidOutAsJacocosIsRefused() { + assertRefused(new Object(), "no RuntimeData field"); + assertRefused(new TwoData(), "two RuntimeData fields"); + assertRefused(new NullData(), "is null"); + assertRefused(new NoStore(), "no ExecutionDataStore field"); + } + + // NoStore's RuntimeData is not JaCoCo's: it holds no store. + static final class NoStore { + private final RuntimeData data = new RuntimeData(); + + static final class RuntimeData { + private final String store = "none"; + } + } + + @Test + public void aStaticFieldOfTheTypeIsNotTheAgents() throws Exception { + StaticToo agent = new StaticToo(); + agent.data.getExecutionData(1L, "app/A", 2).getProbes()[1] = true; + assertEquals(Collections.singletonMap("app/A", Arrays.asList(1)), asMap(ProbeScan.locate(agent).pass(APP))); + } + + static final class StaticToo { + private static final RuntimeData SHARED = new RuntimeData(); + private final RuntimeData data = new RuntimeData(); + } + + @Test + public void aFieldInASuperclassIsFound() throws Exception { + Sub agent = new Sub(); + agent.data.getExecutionData(1L, "app/B", 2).getProbes()[0] = true; + assertEquals(Collections.singletonMap("app/B", Arrays.asList(0)), asMap(ProbeScan.locate(agent).pass(APP))); + } + + static class Base { + final RuntimeData data = new RuntimeData(); + } + + static final class Sub extends Base { + private final String name = "sub"; + } + + static final class TwoData { + private final RuntimeData data = new RuntimeData(); + private final RuntimeData other = new RuntimeData(); + } + + static final class NullData { + private final RuntimeData data = null; + } + + private static void assertRefused(Object agent, String why) { + try { + ProbeScan.locate(agent); + fail("located a store in " + agent.getClass().getName()); + } catch (ReflectiveOperationException e) { + assertTrue(e.getMessage(), e.getMessage().contains(why)); + } + } + + // breakSlot puts a null in place of the probe array of the class at index + // of the scan's sorted classes, so the part that scans it fails there. + static void breakSlot(ProbeScan scan, int index) throws ReflectiveOperationException { + java.lang.reflect.Field arrays = ProbeScan.class.getDeclaredField("arrays"); + arrays.setAccessible(true); + ((boolean[][]) arrays.get(scan))[index] = null; + } + + // classesF makes the classes app/F000..app/F127 in agent, two probes + // each, adds them to app, and returns their probe arrays. + private static boolean[][] classesF(Agent agent, Set app) { + boolean[][] arrays = new boolean[128][]; + for (int i = 0; i < arrays.length; i++) { + String name = String.format("app/F%03d", i); + app.add(name); + arrays[i] = probes(agent, 500 + i, name, 2); + } + return arrays; + } + + // usePool gives scan a pool of 3 threads made by threads. + private static void usePool(ProbeScan scan, java.util.concurrent.ThreadFactory threads) + throws ReflectiveOperationException { + java.lang.reflect.Field pool = ProbeScan.class.getDeclaredField("pool"); + pool.setAccessible(true); + java.util.concurrent.ThreadPoolExecutor executor = new java.util.concurrent.ThreadPoolExecutor(3, 3, 60, + java.util.concurrent.TimeUnit.SECONDS, new java.util.concurrent.LinkedBlockingQueue(), + threads); + executor.allowCoreThreadTimeOut(true); + pool.set(scan, executor); + } + + // useLatePool gives scan a pool of 3 threads that starts the tasks it is + // handed 0, step and 2 * step ms late, in turn, on every pass: the delay + // is the task's, not its thread's, so threads kept from an earlier pass + // keep it. + private static void useLatePool(ProbeScan scan, final long step) throws ReflectiveOperationException { + java.lang.reflect.Field pool = ProbeScan.class.getDeclaredField("pool"); + pool.setAccessible(true); + final Map late = new java.util.concurrent.ConcurrentHashMap<>(); + final AtomicInteger handed = new AtomicInteger(); + java.util.concurrent.ThreadPoolExecutor executor = new java.util.concurrent.ThreadPoolExecutor(3, 3, 60, + java.util.concurrent.TimeUnit.SECONDS, new java.util.concurrent.LinkedBlockingQueue(), + runnable -> { + Thread thread = new Thread(runnable, "late-scan"); + thread.setDaemon(true); + return thread; + }) { + @Override + public void execute(Runnable task) { + late.put(task, step * (handed.getAndIncrement() % 3)); + super.execute(task); + } + + @Override + protected void beforeExecute(Thread thread, Runnable task) { + Long millis = late.remove(task); + if (millis != null && millis > 0) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + } + }; + executor.allowCoreThreadTimeOut(true); + pool.set(scan, executor); + } + + @Test + public void aFailedPartLosesItsHitsAndEndsThePassOnlyOnceEveryPartHas() throws Exception { + // 4 parts of 32 classes; the second, a pool thread's, fails at its 9th class, + // and then the caller's. + for (int broken : new int[] {40, 8}) { + Agent agent = new Agent(); + Set app = new HashSet<>(); + boolean[][] arrays = classesF(agent, app); + ProbeScan scan = new ProbeScan(storeOf(agent), entriesOf(agent), 4, 8); + // Each pass's pool parts start 0, 300 and 600 ms late, so the + // parts after the one that fails are still to run when it does. + useLatePool(scan, 300); + assertTrue(scan.pass(app).isEmpty()); + for (boolean[] probes : arrays) { + probes[0] = true; + } + breakSlot(scan, broken); + try { + scan.pass(app); + fail("a pass whose part failed returned its hits"); + } catch (ProbeScan.LostHitsException e) { + assertTrue(String.valueOf(e.getCause()), e.getCause() instanceof NullPointerException); + } + int part = broken / 32; + for (int i = 0; i < arrays.length; i++) { + boolean leftByTheFailure = i >= broken && i < (part + 1) * 32; + assertEquals("broken " + broken + ", app/F" + i, leftByTheFailure, arrays[i][0]); + } + assertFalse("no pass leaves the caller interrupted", Thread.interrupted()); + } + } + + @Test + public void anInterruptedPassStillWaitsForEveryPartAndKeepsTheInterrupt() throws Exception { + Agent agent = new Agent(); + Set app = new HashSet<>(); + boolean[][] arrays = classesF(agent, app); + ProbeScan scan = new ProbeScan(storeOf(agent), entriesOf(agent), 4, 8); + // The pool's threads start 300 ms late: the caller waits on them. + usePool(scan, runnable -> { + Thread thread = new Thread(() -> { + try { + Thread.sleep(300); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + runnable.run(); + }, "late-scan"); + thread.setDaemon(true); + return thread; + }); + for (boolean[] probes : arrays) { + probes[1] = true; + } + Thread.currentThread().interrupt(); + List hits; + try { + hits = scan.pass(app); + } finally { + assertTrue("the caller is interrupted again", Thread.interrupted()); + } + assertEquals(128, hits.size()); + for (boolean[] probes : arrays) { + assertFalse(probes[1]); + } + } + + @Test + public void aPartThePoolCannotTakeIsScannedOnTheCallersThread() throws Exception { + Agent agent = new Agent(); + Set app = new HashSet<>(); + boolean[][] arrays = classesF(agent, app); + ProbeScan scan = new ProbeScan(storeOf(agent), entriesOf(agent), 4, 8); + // A pool that can make no thread, as at a container's limit on them. + final AtomicInteger asked = new AtomicInteger(); + usePool(scan, runnable -> { + asked.incrementAndGet(); + throw new OutOfMemoryError("unable to create native thread"); + }); + assertTrue(scan.pass(app).isEmpty()); + for (int i = 0; i < arrays.length; i++) { + arrays[i][i % 2] = true; + } + List hits = scan.pass(app); + assertEquals(128, hits.size()); + for (int i = 0; i < hits.size(); i++) { + assertEquals(String.format("app/F%03d", i), hits.get(i).name); + assertArrayEquals(new int[] {i % 2}, hits.get(i).probes); + assertFalse(arrays[i][i % 2]); + } + assertTrue("the pool was asked for a thread: " + asked.get(), asked.get() >= 3); + } + + @Test + public void anErrorReadingJacocosClassesClearsNothing() throws Exception { + Agent agent = new Agent(); + CountingMap entries = CountingMap.swapInto(agent.data); + Set app = new HashSet<>(); + boolean[][] arrays = classesF(agent, app); + arrays[3][1] = true; + ProbeScan scan = new ProbeScan(storeOf(agent), entries, 4, 8); + entries.error = new NoClassDefFoundError("org/jacoco/core/data/ExecutionData"); + try { + scan.pass(app); + fail("a pass that could not read JaCoCo's classes returned"); + } catch (ProbeScan.LostHitsException e) { + fail("nothing was cleared, yet the pass said hits were lost"); + } catch (IllegalStateException e) { + assertTrue(String.valueOf(e.getCause()), e.getCause() instanceof NoClassDefFoundError); + } + assertTrue("the hit is left for JaCoCo's dump", arrays[3][1]); + } + + @Test + public void defaultWorkersIsHalfTheCoresUpToFour() { + int cores = Runtime.getRuntime().availableProcessors(); + assertEquals(Math.max(1, Math.min(4, cores / 2)), ProbeScan.defaultWorkers()); + } + + // storeOf and entriesOf reach the same objects locate does, for a scan + // made with other worker counts. + private static Object storeOf(Agent agent) throws ReflectiveOperationException { + return storeOf(agent.data); + } + + static Object storeOf(RuntimeData data) throws ReflectiveOperationException { + java.lang.reflect.Field store = RuntimeData.class.getDeclaredField("store"); + store.setAccessible(true); + return store.get(data); + } + + private static Map entriesOf(Agent agent) throws ReflectiveOperationException { + Object store = storeOf(agent); + java.lang.reflect.Field entries = store.getClass().getDeclaredField("entries"); + entries.setAccessible(true); + return (Map) entries.get(store); + } +} diff --git a/scripts/smoke-javaagent.sh b/scripts/smoke-javaagent.sh index 274d64d3..b1b75908 100755 --- a/scripts/smoke-javaagent.sh +++ b/scripts/smoke-javaagent.sh @@ -332,6 +332,27 @@ run_smoke "classpath-fallback" \ -cp "${work_dir}/classes:${agent_jar}" \ smoke.SmokeHarness "classpath-fallback" derived attached +# With JaCoCo's agent in the JVM, a START and an END read and clear the app's +# probes in place, in one pass, instead of taking JaCoCo's reset and dump. +# This is the real (shaded) JaCoCo runtime, which the unit tests do not load: +# its layout must be the one the pass reads. The END's diagnostics line names +# the step: `scan=` for the pass, `dump=` for JaCoCo's dump. +scan_log="${work_dir}/in-place-scan.log" +scan_rc=0 +KEPLOY_JAVA_DEDUP_DIAGNOSTICS=1 run_smoke "in-place-scan" \ + java_with_agents "in-place-scan" \ + -cp "${work_dir}/classes:${agent_jar}" \ + smoke.SmokeHarness "in-place-scan" derived attached 2>"${scan_log}" || scan_rc=$? +if [[ "${scan_rc}" -ne 0 ]] \ + || ! grep -q 'diagnostics: START test-set-0/in-place-scan took .* scan=' "${scan_log}" \ + || ! grep -q 'diagnostics: END test-set-0/in-place-scan took .* scan=' "${scan_log}" \ + || grep -q 'cannot be read in place\|the probe scan failed' "${scan_log}"; then + cat "${scan_log}" >&2 + echo "in-place-scan: the agent did not read JaCoCo's probes in place (exit ${scan_rc})" >&2 + exit 1 +fi +echo "in-place-scan: START and END read JaCoCo's probes in place" + mkdir -p "${work_dir}/jar/lib" cp "${agent_jar}" "${work_dir}/jar/lib/keploy-sdk.jar" cat > "${work_dir}/jar/MANIFEST.MF" <&2 + echo "sdk-only-in-place-scan: the agent did not read its own JaCoCo's probes in place" >&2 + exit 1 +fi +echo "sdk-only-in-place-scan: START and END read the probes of the JaCoCo keploy-sdk.jar started in place" + mkdir -p "${work_dir}/boot-root/BOOT-INF/classes" "${work_dir}/war-root/WEB-INF/classes" cp -R "${work_dir}/classes/." "${work_dir}/boot-root/BOOT-INF/classes/" cp -R "${work_dir}/classes/." "${work_dir}/war-root/WEB-INF/classes/" From 922618e18e4a2ebdd32a2ef93fd05de76ca08d3c Mon Sep 17 00:00:00 2001 From: slayerjain Date: Fri, 20 Nov 2026 01:10:05 +0530 Subject: [PATCH 03/14] fix(dedup): warm up a fat jar's classes, and never hold a START long The first START runs the static initializers of the app's own classes, so that no test is charged with their lines. It looked the classes up through the control thread's context loader, the system loader and the SDK's own loader. A Spring Boot fat jar's classes are reachable only through the loader its launcher makes, so the warmup initialized none of them ("initialized=0") and the first tests were charged with them. It also ran on the START's own thread, so the START was answered only once every class was initialized, however long that took; Keploy's k8s collector gives up on an answer after 30 s by default. The warmup now: - finds the app's loaders: those that defined the app's classes already loaded (from premain's Instrumentation), the one that defined the most first; then the live threads' context loaders, the oldest thread first (a launcher sets its main thread's to the app's loader), read from the root thread group, which pauses no thread; then the system loader and the SDK's own. A class already loaded is initialized in the loader that loaded it; of a class loaded in two loaders, in the one that defined more of the app's classes. A class not loaded yet goes on to the next loader only when a loader cannot find it: one that a loader found but could not link or initialize is left to lazy init, never loaded again in another loader, where its static initializer would run twice. Each class is initialized with the thread's context loader set to the loader it goes through, as on the app's own threads. - runs on a thread of its own, which also reads the app's classes when premain has not. The first START waits for it for at most 10 s, then goes on, and the warmup stops after the class it is initializing. The WARNING then names that class (or the step before the classes). Its static initializer goes on while the tests run, and each line it runs from then on counts in the test running at the time (between two tests, in none). The classes it did not reach initialize when the app first uses them, as with the warmup off, and the log says how many ("not reached=N"). If the thread cannot start (a pids limit), the START goes on with no warmup and says so. Fat jars are now warmed up for the first time: every app class not loaded yet is loaded, instrumented by JaCoCo and initialized at the first START, as other apps' classes already were. On an app with many classes it never uses, that is more metaspace than the app alone would take. KEPLOY_JAVA_DEDUP_WARMUP_DISABLED=true turns the warmup off. It still runs only when the index is the app's own classes, and never initializes a library class itself. When premain has not read the app's classes (a JaCoCo agent attached separately), the first START's scan still waits for that read, as it needs the class set. Rows: a fat jar's classes warmed up through its launcher's loader; premain's Instrumentation finding the loader of the app's loaded classes with no thread holding it; a class the app loaded in a second loader initialized there; of two copies of a class, the copy of the loader that defined more initialized, whichever the JVM lists first; a class whose static initializer throws run once, not again in a second loader that sees it; the loaders' order; every live thread seen from the root group; a slow static initializer holding the first START only for the wait, the WARNING naming it, the warmup stopped after it and a second START not waiting; a warmup thread that cannot start leaving the START and the control server working; the WARNING naming each step; an empty class list asking the JVM nothing. Signed-off-by: slayerjain --- README.md | 4 +- .../main/java/io/keploy/dedup/AppWarmup.java | 290 ++++++++ .../io/keploy/dedup/KeployDedupAgent.java | 136 ++-- .../test/java/io/keploy/dedup/WarmupTest.java | 639 ++++++++++++++++++ 4 files changed, 1014 insertions(+), 55 deletions(-) create mode 100644 keploy-sdk/src/main/java/io/keploy/dedup/AppWarmup.java create mode 100644 keploy-sdk/src/test/java/io/keploy/dedup/WarmupTest.java diff --git a/README.md b/README.md index ae1713a5..92c7eb79 100644 --- a/README.md +++ b/README.md @@ -201,8 +201,8 @@ ceiling does not quietly fall behind again. - `KEPLOY_JAVA_CLASS_DIRS`: optional comma-separated class, jar, war, ear, or zip locations of the app's own classes. You rarely need it: see "Which classes count as the app's" below - `KEPLOY_JAVA_CLASSPATH_FALLBACK`: scans the full classpath when neither the app's own classes nor the executable jar give any class. `false` leaves the index empty then, and no test's coverage is measured. Default: `true` - `KEPLOY_JAVA_DEDUP_DISABLED`: disables the Java dedup agent when set to `true`, `1`, or `yes` -- `KEPLOY_JAVA_DEDUP_DIAGNOSTICS`: when `true`, `1`, or `yes`, logs one line per START and END that says how long the command took and how long each step did: `warmup`, then `scan` (the pass that clears the background) for a START; `scan` (the pass that reads and clears the test's hits), `filter` (the hits made into the test's probes by class), `json` and `send` for an END, plus `classes` on TCP (the check for new app classes, and the class-zip frame when there are some). Where JaCoCo's own reset and dump are used instead, a START has `reset`, and an END has `dump` (JaCoCo's dump), `parse` and `filter` (to the app's classes) in place of `scan` and `filter`; a step that did not run is left out, so an empty dump has only `dump`. A START that cleared background hits also logs how many classes and probes they were. The app's class files are read once: before the app starts, when keploy-sdk.jar starts JaCoCo itself; otherwise on the first START under `warmup`, or, with the warmup off, under that START's `scan` (or, with JaCoCo's dump, on the first END whose dump is not empty, under `filter`). On the unix socket `took` ends before the answer is written; on TCP it includes the answer. Use it to see where a test's time goes on your app -- `KEPLOY_JAVA_DEDUP_WARMUP_DISABLED`: when `true`, `1`, or `yes`, skips the warmup, which runs the static initializers of the app's own classes once, on the first START, so that no test is charged with them. The log says how long the warmup took +- `KEPLOY_JAVA_DEDUP_DIAGNOSTICS`: when `true`, `1`, or `yes`, logs one line per START and END that says how long the command took and how long each step did: `warmup`, then `scan` (the pass that clears the background) for a START; `scan` (the pass that reads and clears the test's hits), `filter` (the hits made into the test's probes by class), `json` and `send` for an END, plus `classes` on TCP (the check for new app classes, and the class-zip frame when there are some). Where JaCoCo's own reset and dump are used instead, a START has `reset`, and an END has `dump` (JaCoCo's dump), `parse` and `filter` (to the app's classes) in place of `scan` and `filter`; a step that did not run is left out, so an empty dump has only `dump`. A START that cleared background hits also logs how many classes and probes they were. The app's class files are read once: before the app starts, when keploy-sdk.jar starts JaCoCo itself; otherwise on the first START, by the warmup's thread (timed under `warmup`, and, past the 10 seconds that START waits for the warmup, under its `scan`, which waits for the read, or, with JaCoCo's reset and dump, which never wait for it, on the first END whose dump is not empty, under `filter`), or, with the warmup off, under that START's `scan` (or, with JaCoCo's dump, on the first END whose dump is not empty, under `filter`). On the unix socket `took` ends before the answer is written; on TCP it includes the answer. Use it to see where a test's time goes on your app +- `KEPLOY_JAVA_DEDUP_WARMUP_DISABLED`: when `true`, `1`, or `yes`, skips the warmup, which loads and initializes the app's own classes once, on the first START, so that no test is charged with their static initializers. It loads them through the class loader the app runs them with, such as a Spring Boot fat jar's own loader (since 2.2.0; before, a fat jar's classes were not warmed up), so every app class not loaded yet is loaded then, and JaCoCo instruments it: on an app with many classes it never uses, that is more class metadata (metaspace) than the app alone would load. It runs on a thread of its own: the first START waits for it for at most 10 seconds, then goes on, and the warmup stops after the class it is initializing. Its static initializer goes on while the tests run, and each line it runs from then on counts in the test running at the time (between two tests, in none: the next START clears it); classes it did not reach initialize when the app first uses them, as with the warmup off. The log says how long the warmup took, and, when the START stopped waiting, which class it was initializing ## Which classes count as the app's diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/AppWarmup.java b/keploy-sdk/src/main/java/io/keploy/dedup/AppWarmup.java new file mode 100644 index 00000000..f183426a --- /dev/null +++ b/keploy-sdk/src/main/java/io/keploy/dedup/AppWarmup.java @@ -0,0 +1,290 @@ +package io.keploy.dedup; + +import java.lang.instrument.Instrumentation; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * AppWarmup runs the static initializers of the app's own classes once, + * before the first test's coverage window, through the class loaders the app + * loads them with. It runs on a thread of its own: the first START waits for + * it for a bounded time, then calls {@link #stop()}, which ends it after the + * class it is initializing, so a slow initializer never holds a START's + * answer longer than that. + * + *

The loaders, in the order a class not loaded yet is tried with: + *

    + *
  1. the loaders that defined the app's classes already loaded (from the + * JVM's {@link Instrumentation}), the one that defined the most first: a + * Spring Boot fat jar's own loader, or a servlet container's web app + * loader, which the system loader cannot reach;
  2. + *
  3. the context loaders of the live threads, the oldest thread first (the + * app's main thread, when it is still running): the launcher sets them to the + * app's loader, and they are all there is without an Instrumentation;
  4. + *
  5. the system loader, then the loader of this SDK.
  6. + *
+ * A class already loaded is initialized through the loader that defined it; + * of a class loaded in two loaders, the copy of the loader that defined more + * of the app's classes. A class not loaded yet is tried with the next loader + * only when a loader cannot find it: one a loader found but could not link + * or initialize is skipped, never loaded again in another loader, where its + * static initializer would run a second time. + * Each class is initialized with the thread's context loader set to the + * loader it is initialized through, as on the app's own threads: a static + * initializer that looks services up through the context loader finds the + * app's. + */ +final class AppWarmup { + + private final Instrumentation instrumentation; + private volatile boolean stopped; + private volatile int initialized; + private volatile int skipped; + private volatile int notReached; + // What the warmup is doing, for the log when a START stops waiting: + // -2 before run, -1 while it finds the loaders, else the index in names + // of the class it is initializing. names is set before at leaves -2. + private volatile List names; + private volatile int at = -2; + + // instrumentation is null when the SDK was not started as a -javaagent. + AppWarmup(Instrumentation instrumentation) { + this.instrumentation = instrumentation; + } + + /** stop ends the warmup after the class it is initializing. */ + void stop() { + stopped = true; + } + + int initialized() { + return initialized; + } + + int skipped() { + return skipped; + } + + /** notReached is how many classes a {@link #stop()} left uninitialized. */ + int notReached() { + return notReached; + } + + /** + * doing says what the warmup is doing now, for a log line: reading the + * app's class files (before {@link #run}), finding the loaders, or the + * class it is initializing. + */ + String doing() { + int i = at; + if (i == -2) { + return "reading the app's class files"; + } + if (i == -1) { + return "finding the app's class loaders"; + } + List all = names; + return "initializing " + all.get(i) + " (class " + (i + 1) + " of " + all.size() + ")"; + } + + /** + * run initializes classNames (binary names, com.acme.Shop$Item) in order, + * on the calling thread, until done or stopped. Only that thread writes + * the counts. + */ + void run(List classNames) { + if (classNames.isEmpty()) { + return; + } + Thread self = Thread.currentThread(); + ClassLoader saved = self.getContextClassLoader(); + try { + names = classNames; + at = -1; + Map> loaded = new HashMap<>(); + List loaders = loaders(loadedClasses(), new HashSet<>(classNames), loaded, + contextLoaders(), ClassLoader.getSystemClassLoader(), AppWarmup.class.getClassLoader()); + for (int i = 0; i < classNames.size(); i++) { + if (stopped) { + notReached = classNames.size() - i; + return; + } + String name = classNames.get(i); + at = i; + Class type = loaded.get(name); + boolean done = type != null + ? initialize(self, name, type.getClassLoader()) == Outcome.INITIALIZED + : initializeWithAny(self, name, loaders); + if (done) { + initialized++; + } else { + skipped++; + } + } + } finally { + self.setContextClassLoader(saved); + } + } + + private Class[] loadedClasses() { + if (instrumentation == null) { + return new Class[0]; + } + try { + return instrumentation.getAllLoadedClasses(); + } catch (RuntimeException e) { + return new Class[0]; + } + } + + private enum Outcome { INITIALIZED, NOT_FOUND, FAILED } + + // initializeWithAny tries the next loader only for a class the last one + // could not find. One it found, and could not link or initialize, is + // left to lazy init: another loader that sees its bytes would define a + // second copy and run its static initializer again. + private static boolean initializeWithAny(Thread self, String name, List loaders) { + for (ClassLoader loader : loaders) { + Outcome outcome = initialize(self, name, loader); + if (outcome != Outcome.NOT_FOUND) { + return outcome == Outcome.INITIALIZED; + } + } + return false; + } + + private static Outcome initialize(Thread self, String name, ClassLoader loader) { + try { + self.setContextClassLoader(loader); + Class.forName(name, true, loader); + return Outcome.INITIALIZED; + } catch (ClassNotFoundException e) { + return Outcome.NOT_FOUND; + } catch (Throwable ignored) { + // Its threw, or it could not be linked: left to lazy + // init. + return Outcome.FAILED; + } + } + + /** + * loaders orders the loaders a class not loaded yet is tried with (see + * the class comment), each once, and puts each of the app's classes + * already loaded in {@code loadedApp}, by name: of a name loaded in two + * loaders, the copy of the loader that comes first. + * + * @param loadedClasses the classes the JVM has loaded + * @param appClasses the app's classes, by binary name + * @param loadedApp filled with the app's classes found in loadedClasses + * @param contextLoaders the live threads' context loaders, in order + * @param last the loaders to try after all others + */ + static List loaders(Class[] loadedClasses, Set appClasses, + Map> loadedApp, List contextLoaders, ClassLoader... last) { + final Map defined = new HashMap<>(); + List definers = new ArrayList<>(); + List> app = new ArrayList<>(); + for (Class type : loadedClasses) { + ClassLoader loader = type.getClassLoader(); + if (loader == null || !appClasses.contains(type.getName())) { + continue; + } + app.add(type); + Integer count = defined.get(loader); + if (count == null) { + definers.add(loader); + count = 0; + } + defined.put(loader, count + 1); + } + // Stable: loaders that defined as many keep the order they were met. + Collections.sort(definers, new Comparator() { + @Override + public int compare(ClassLoader a, ClassLoader b) { + return Integer.compare(defined.get(b), defined.get(a)); + } + }); + // A name loaded in two loaders (Spring Boot DevTools' restart loader + // and the system loader, or two web apps): the copy of the loader + // that defined more of the app's classes is the app's. + Map rank = new HashMap<>(); + for (int i = 0; i < definers.size(); i++) { + rank.put(definers.get(i), i); + } + for (Class type : app) { + Class kept = loadedApp.get(type.getName()); + if (kept == null || rank.get(type.getClassLoader()) < rank.get(kept.getClassLoader())) { + loadedApp.put(type.getName(), type); + } + } + List ordered = new ArrayList<>(definers); + for (ClassLoader loader : contextLoaders) { + addOnce(ordered, loader); + } + for (ClassLoader loader : last) { + addOnce(ordered, loader); + } + return ordered; + } + + private static void addOnce(List loaders, ClassLoader loader) { + if (loader != null && !loaders.contains(loader)) { + loaders.add(loader); + } + } + + // contextLoaders is the live threads' context loaders, the oldest thread + // (lowest id) first, each once. + static List contextLoaders() { + List threads; + try { + threads = liveThreads(); + } catch (RuntimeException e) { + return Collections.emptyList(); + } + Collections.sort(threads, new Comparator() { + @Override + public int compare(Thread a, Thread b) { + return Long.compare(a.getId(), b.getId()); + } + }); + List loaders = new ArrayList<>(); + for (Thread thread : threads) { + addOnce(loaders, thread.getContextClassLoader()); + } + return loaders; + } + + // liveThreads enumerates the root thread group: unlike + // Thread.getAllStackTraces, it takes no safepoint and walks no stack, + // which on an app with a thousand threads is a pause of the whole app. + static List liveThreads() { + ThreadGroup root = Thread.currentThread().getThreadGroup(); + while (root.getParent() != null) { + root = root.getParent(); + } + return liveThreads(root, root.activeCount() + 16); + } + + // liveThreads enumerates root's threads into an array of size, doubled + // while the threads fill it (some may have started since it was sized). + static List liveThreads(ThreadGroup root, int size) { + Thread[] threads = new Thread[size]; + int n = root.enumerate(threads, true); + while (n == threads.length) { + threads = new Thread[threads.length * 2]; + n = root.enumerate(threads, true); + } + List live = new ArrayList<>(n); + for (int i = 0; i < n; i++) { + live.add(threads[i]); + } + return live; + } +} diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java index 7cc1d666..6dcf73fc 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java @@ -46,6 +46,7 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicBoolean; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -133,6 +134,17 @@ static String encodeBytecodeFrame(String buildTag, String manifestJson, byte[] z // How long stop(), and a start() after it, wait for the worker's thread. // Not final: the tests shorten it. static volatile long workerStopWaitMillis = 5000; + // How long the first START waits for the warmup before it goes on + // without it: well inside the 30 s Keploy's k8s collector waits for an + // answer by default. Not final: the tests shorten it. + static volatile long warmupWaitMillis = 10000; + // Makes the warmup's thread. Not final: a test makes one that cannot + // start. + static volatile ThreadFactory warmupThreads = runnable -> { + Thread thread = new Thread(runnable, "keploy-java-dedup-warmup"); + thread.setDaemon(true); + return thread; + }; private KeployDedupAgent() { } @@ -188,7 +200,7 @@ private static boolean start(CoverageIndex index, Instrumentation instrumentatio startedIndex = index; CoverageCollector collector = new CoverageCollector( - new JacocoClient(resolveHost(), resolvePort()), index); + new JacocoClient(resolveHost(), resolvePort()), index, instrumentation); CoverageWorker worker; String threadName; @@ -1040,6 +1052,9 @@ private static final class CoverageCollector { private final JacocoClient jacocoClient; private final CoverageIndex coverageIndex; + // premain's, for the warmup to find the app's class loaders; null + // when the SDK was not started as a -javaagent. + private final Instrumentation instrumentation; private final AtomicBoolean warmed = new AtomicBoolean(false); // The VM names of the app's classes; see indexedClassNames. private volatile Set appClassNames; @@ -1072,9 +1087,11 @@ void resetBytecodeSentForNewConnection() { lastUploadedManifestSize.set(-1); } - private CoverageCollector(JacocoClient jacocoClient, CoverageIndex coverageIndex) { + private CoverageCollector(JacocoClient jacocoClient, CoverageIndex coverageIndex, + Instrumentation instrumentation) { this.jacocoClient = jacocoClient; this.coverageIndex = coverageIndex; + this.instrumentation = instrumentation; } // The one-pass scan of the app classes' probes, when JaCoCo's agent @@ -1167,39 +1184,82 @@ private boolean reset() { * lines to whichever test ran first — making the duplicate set * non-deterministic run-to-run. Running them now (then letting the START * clear the counters) means every test sees only the lines its own - * request executes. Called once, on the first START, when the app is - * fully started. Best-effort: per-class failures are ignored (the class + * request executes. Called on the first START, when the app is fully + * started. Best-effort: per-class failures are ignored (the class * just falls back to lazy init). Disable with * KEPLOY_JAVA_DEDUP_WARMUP_DISABLED=true if a static initializer has * harmful side effects. + * + *

The warmup runs on a thread of its own, through the app's class + * loaders (AppWarmup). This START waits for it at most + * warmupWaitMillis, then tells it to stop after the class it is + * initializing and goes on: that class's static initializer goes on + * while the tests run, and each line it runs from then on counts in + * the test running at the time (between two tests, in none; the next + * START clears it). The classes it did not reach initialize when the + * app first uses them, as with no warmup. */ private void warmup() { if (isWarmupDisabled() || !warmed.compareAndSet(false, true)) { return; } - // Only the app's own classes: a library's static initializer can - // start threads, open connections or self-attach an agent (Mockito - // through byte-buddy), and the whole classpath of a large app took - // the warmup to 54 s (105k classes on Fineract). - if (!coverageIndex.isAppOnly()) { - log(Level.INFO, "Keploy dedup: no warmup, as the index is not the app's own classes alone" - + " (from " + coverageIndex.source + ")", null); + final long began = System.nanoTime(); + final AppWarmup warmup = new AppWarmup(instrumentation); + Thread thread; + try { + thread = warmupThreads.newThread(() -> runWarmup(warmup, began)); + thread.start(); + } catch (Throwable t) { + // Out of threads (a pids or ulimit -u limit): no warmup, and + // this START, and the server that runs it, go on. + log(Level.WARNING, "Keploy dedup: no warmup, as its thread could not start; the app's classes" + + " initialize when the app first uses them", t); return; } - long began = System.nanoTime(); - ClassLoader[] loaders = warmupLoaders(); - int initialized = 0; - int failed = 0; - for (ClassEntry entry : coverageIndex.entries()) { - if (initializeClass(entry.className.replace('/', '.'), loaders)) { - initialized++; - } else { - failed++; + try { + thread.join(warmupWaitMillis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + if (thread.isAlive()) { + warmup.stop(); + log(Level.WARNING, "Keploy dedup: the warmup of the app's classes was still " + warmup.doing() + + " after " + Phases.millis(System.nanoTime() - began) + " ms, so the first test goes on" + + " without it, and the warmup stops after that step. A static initializer it is running" + + " goes on while the tests run, and each line it runs from then on counts in the test" + + " running at the time (between two tests, in none); the classes it did not reach" + + " initialize when the app first uses them, and their lines count in the test that does", + null); + } + } + + // runWarmup is the warmup's thread: it reads the app's classes here, + // not on the START's thread, and never names a library's class. + private void runWarmup(AppWarmup warmup, long began) { + try { + // Only the app's own classes: a library's static initializer can + // start threads, open connections or self-attach an agent (Mockito + // through byte-buddy), and the whole classpath of a large app took + // the warmup to 54 s (105k classes on Fineract). + if (!coverageIndex.isAppOnly()) { + log(Level.INFO, "Keploy dedup: no warmup, as the index is not the app's own classes alone" + + " (from " + coverageIndex.source + ")", null); + return; } + List names = new ArrayList<>(); + for (ClassEntry entry : coverageIndex.entries()) { + names.add(entry.className.replace('/', '.')); + } + warmup.run(names); + log(Level.INFO, "Keploy dedup: warmed up application classes (initialized=" + + warmup.initialized() + ", skipped=" + warmup.skipped() + + (warmup.notReached() > 0 ? ", not reached=" + warmup.notReached() : "") + + ") in " + Phases.millis(System.nanoTime() - began) + " ms", null); + } catch (Throwable t) { + // Never let the warmup end anything but itself. + log(Level.WARNING, "Keploy dedup: the warmup of the app's classes failed; they initialize when" + + " the app first uses them", t); } - log(Level.INFO, "Keploy dedup: warmed up application classes (initialized=" - + initialized + ", skipped=" + failed + ") in " + Phases.millis(System.nanoTime() - began) - + " ms", null); } private boolean isWarmupDisabled() { @@ -1207,36 +1267,6 @@ private boolean isWarmupDisabled() { "keploy.java.dedup.warmup.disabled", "")); } - private ClassLoader[] warmupLoaders() { - List loaders = new ArrayList<>(3); - ClassLoader ctx = Thread.currentThread().getContextClassLoader(); - if (ctx != null) { - loaders.add(ctx); - } - ClassLoader sys = ClassLoader.getSystemClassLoader(); - if (sys != null && !loaders.contains(sys)) { - loaders.add(sys); - } - ClassLoader own = KeployDedupAgent.class.getClassLoader(); - if (own != null && !loaders.contains(own)) { - loaders.add(own); - } - return loaders.toArray(new ClassLoader[0]); - } - - private boolean initializeClass(String binaryName, ClassLoader[] loaders) { - for (ClassLoader loader : loaders) { - try { - Class.forName(binaryName, true, loader); - return true; - } catch (Throwable ignored) { - // Try the next loader; a class that no loader can initialize - // (or whose throws) is simply left to lazy init. - } - } - return false; - } - private Map> capture(Phases phases) throws IOException { ProbeScan scan = probeScan(); if (scan != null) { diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/WarmupTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/WarmupTest.java new file mode 100644 index 00000000..63d1c64c --- /dev/null +++ b/keploy-sdk/src/test/java/io/keploy/dedup/WarmupTest.java @@ -0,0 +1,639 @@ +package io.keploy.dedup; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.instrument.Instrumentation; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.logging.Handler; +import java.util.logging.LogRecord; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.newsclub.net.unix.AFUNIXSocket; +import org.newsclub.net.unix.AFUNIXSocketAddress; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +/** + * The first START's warmup initializes the app's classes through the loader + * the app runs them with (a fat jar's, which the system loader cannot reach), + * on a thread of its own that the START waits for only so long. + */ +public class WarmupTest { + + private static final String[] PROPERTIES = {"keploy.java.class.dirs", "keploy.java.dedup.warmup.disabled", + "keploy.jacoco.port", "keploy.coverage.endpoint", "keploy.java.dedup.diagnostics"}; + private static final String MAIN = "com/acme/shop/Main"; + private static final String CONTROLLER = "com/acme/shop/web/Controller"; + + // What the generated classes' static initializers saw: class name to the + // initializing thread's context loader. + private static final Map INITIALIZED = new ConcurrentHashMap<>(); + // Each run of a generated static initializer: the class name and the + // loader that defined the copy it ran in. + private static final List> RUNS = new CopyOnWriteArrayList<>(); + private static volatile CountDownLatch slow = new CountDownLatch(0); + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + private final Map saved = new HashMap<>(); + private String savedControlPath; + private String savedDataPath; + private long savedWait; + private java.util.concurrent.ThreadFactory savedThreads; + private FakeJacoco jacoco; + private Thread appMain; + private final CountDownLatch appEnds = new CountDownLatch(1); + private final List logged = new CopyOnWriteArrayList<>(); + private final Handler handler = new Handler() { + @Override + public void publish(LogRecord record) { + logged.add(record.getLevel() + " " + record.getMessage()); + } + + @Override + public void flush() { + } + + @Override + public void close() { + } + }; + + /** Called by the generated classes' static initializers. */ + public static void initialized(String name, Class self) throws InterruptedException { + INITIALIZED.put(name, Thread.currentThread().getContextClassLoader()); + RUNS.add(new AbstractMap.SimpleImmutableEntry<>(name, self.getClassLoader())); + if (name.endsWith("Slow")) { + slow.await(20, TimeUnit.SECONDS); + } + if (name.endsWith("Broken")) { + throw new IllegalStateException("Broken's static initializer fails"); + } + } + + @Before + public void setUp() throws IOException { + for (String name : PROPERTIES) { + saved.put(name, System.getProperty(name)); + System.clearProperty(name); + } + INITIALIZED.clear(); + RUNS.clear(); + savedWait = KeployDedupAgent.warmupWaitMillis; + savedThreads = KeployDedupAgent.warmupThreads; + savedControlPath = KeployDedupAgent.controlSocketPath; + savedDataPath = KeployDedupAgent.dataSocketPath; + File sockets = tmp.newFolder("sockets"); + KeployDedupAgent.controlSocketPath = new File(sockets, "control.sock").getAbsolutePath(); + KeployDedupAgent.dataSocketPath = new File(sockets, "data.sock").getAbsolutePath(); + Logger.getLogger(KeployDedupAgent.class.getName()).addHandler(handler); + jacoco = new FakeJacoco(); + System.setProperty("keploy.jacoco.port", String.valueOf(jacoco.server.getLocalPort())); + } + + @After + public void tearDown() throws Exception { + slow.countDown(); + KeployDedupAgent.stop(); + appEnds.countDown(); + if (appMain != null) { + appMain.join(5000); + } + Logger.getLogger(KeployDedupAgent.class.getName()).removeHandler(handler); + KeployDedupAgent.warmupWaitMillis = savedWait; + KeployDedupAgent.warmupThreads = savedThreads; + KeployDedupAgent.controlSocketPath = savedControlPath; + KeployDedupAgent.dataSocketPath = savedDataPath; + for (String name : PROPERTIES) { + if (saved.get(name) == null) { + System.clearProperty(name); + } else { + System.setProperty(name, saved.get(name)); + } + } + jacoco.close(); + } + + @Test + public void aFatJarsClassesWarmUpThroughTheLoaderTheAppRunsThem() throws Exception { + // As a Spring Boot launcher does: the app's classes are reachable only + // through the loader it made, set as its main thread's context loader. + File classes = appClasses(MAIN, CONTROLLER); + URLClassLoader app = new URLClassLoader(new URL[] {classes.toURI().toURL()}, + WarmupTest.class.getClassLoader()); + runAppMain(app); + System.setProperty("keploy.java.class.dirs", classes.getPath()); + + assertTrue(KeployDedupAgent.start()); + assertEquals("ACK proto=2\n", send("START test-set-0/test-1")); + + assertLogged("INFO Keploy dedup: warmed up application classes (initialized=2, skipped=0) in "); + assertNotLogged("the warmup of the app's classes was still"); + assertSame(app, INITIALIZED.get(MAIN)); + assertSame(app, INITIALIZED.get(CONTROLLER)); + app.close(); + } + + @Test + public void aSlowStaticInitializerHoldsTheFirstStartOnlySoLong() throws Exception { + // Sorted by name, Slow is warmed up first; its runs until + // the test lets it end, past the START's wait. + String slowName = "com/acme/a/Slow"; + String next = "com/acme/b/Next"; + File classes = appClasses(slowName, next); + URLClassLoader app = new URLClassLoader(new URL[] {classes.toURI().toURL()}, + WarmupTest.class.getClassLoader()); + runAppMain(app); + System.setProperty("keploy.java.class.dirs", classes.getPath()); + slow = new CountDownLatch(1); + long waitMillis = 500; + KeployDedupAgent.warmupWaitMillis = waitMillis; + + assertTrue(KeployDedupAgent.start()); + long began = System.nanoTime(); + assertEquals("ACK proto=2\n", send("START test-set-0/test-1")); + long tookMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - began); + + assertTrue("the START was answered after " + tookMillis + " ms", + tookMillis >= waitMillis && tookMillis < 10000); + // It names what it was doing when the START stopped waiting: Slow, + // or, on a box too busy to reach it in the wait, an earlier step. + String warning = loggedStartingWith("WARNING Keploy dedup: the warmup of the app's classes was still "); + assertTrue(warning, warning.matches("WARNING Keploy dedup: the warmup of the app's classes was still" + + " (initializing com\\.acme\\.a\\.Slow \\(class 1 of 2\\)|finding the app's class loaders" + + "|reading the app's class files) after [0-9]+\\.[0-9]{2} ms, so the first test goes on without it," + + " and the warmup stops after that step\\. A static initializer it is running goes on while" + + " the tests run, and each line it runs from then on counts in the test running at the time" + + " \\(between two tests, in none\\); .*")); + // A second START neither warms up again nor waits for the warmup: + // it is answered while Slow's initializer still holds (it holds for + // 20 s), and no second bounded wait ends in a second WARNING. Not a + // wall-clock bound under the wait: a busy box can delay any answer + // that long. With the wait raised to 60 s, any wait on the warmup, + // bounded or not, lasts until Slow's 20 s hold ends. + KeployDedupAgent.warmupWaitMillis = 60000; + began = System.nanoTime(); + assertEquals("ACK proto=2\n", send("START test-set-0/test-2")); + tookMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - began); + assertTrue("the second START was answered after " + tookMillis + " ms", tookMillis < 10000); + assertEquals(1, countLogged("WARNING Keploy dedup: the warmup of the app's classes was still ")); + + slow.countDown(); + awaitLogged("INFO Keploy dedup: warmed up application classes ("); + // Slow's initializer had begun by the stop; else, on a box too busy + // to reach it in the wait, the warmup stopped before it. + boolean slowBegan = INITIALIZED.containsKey(slowName); + // The warmup names a class only once it has passed the stop check + // for it, so a WARNING that names Slow means Slow ran. + assertTrue(warning, slowBegan || !warning.contains("com.acme.a.Slow")); + assertLogged("INFO Keploy dedup: warmed up application classes (" + + (slowBegan ? "initialized=1, skipped=0, not reached=1)" : "initialized=0, skipped=0, not reached=2)")); + assertFalse("the warmup went on past the class it was initializing: " + INITIALIZED, + INITIALIZED.containsKey(next)); + app.close(); + } + + @Test + public void premainsInstrumentationFindsTheLoaderOfTheAppsLoadedClasses() throws Exception { + // Started as a -javaagent, with no live thread holding the app's + // loader: the app's Main, loaded, names it. The stand-in + // Instrumentation cannot start a JaCoCo, which premain says, and + // goes on. + File classes = appClasses(MAIN, CONTROLLER); + try (URLClassLoader app = new URLClassLoader(new URL[] {classes.toURI().toURL()}, + WarmupTest.class.getClassLoader())) { + Class main = Class.forName(binary(MAIN), false, app); + System.setProperty("keploy.java.class.dirs", classes.getPath()); + + KeployDedupAgent.premain(null, instrumentation(main)); + assertTrue(KeployDedupAgent.isStarted()); + assertEquals("ACK proto=2\n", send("START test-set-0/test-1")); + + assertLogged("INFO Keploy dedup: warmed up application classes (initialized=2, skipped=0) in "); + assertSame(app, INITIALIZED.get(MAIN)); + assertSame(app, INITIALIZED.get(CONTROLLER)); + } + } + + @Test + public void aClassTheAppLoadedIsInitializedThroughItsOwnLoader() throws Exception { + // With premain's Instrumentation and no thread holding the app's + // loader: Main is loaded, not initialized; its loader is the one + // Controller, not loaded yet, is found with. + File classes = appClasses(MAIN, CONTROLLER); + try (URLClassLoader app = new URLClassLoader(new URL[] {classes.toURI().toURL()}, + WarmupTest.class.getClassLoader())) { + Class main = Class.forName(binary(MAIN), false, app); + assertTrue(INITIALIZED.isEmpty()); + + AppWarmup warmup = new AppWarmup(instrumentation(main, String.class)); + warmup.run(Arrays.asList(binary(MAIN), binary(CONTROLLER))); + + assertEquals(2, warmup.initialized()); + assertEquals(0, warmup.skipped()); + assertSame(app, INITIALIZED.get(MAIN)); + assertSame(app, INITIALIZED.get(CONTROLLER)); + } + } + + @Test + public void aLoadedClassIsInitializedInTheLoaderThatLoadedItNotInAnother() throws Exception { + // Two loaders can load every class; Cart is loaded in the one that + // defined fewer of them, and is initialized there, not loaded again. + String cart = "com/acme/shop/Cart"; + File classes = appClasses(MAIN, CONTROLLER, cart); + ClassLoader parent = WarmupTest.class.getClassLoader(); + try (URLClassLoader one = new URLClassLoader(new URL[] {classes.toURI().toURL()}, parent); + URLClassLoader two = new URLClassLoader(new URL[] {classes.toURI().toURL()}, parent)) { + Class main = Class.forName(binary(MAIN), false, one); + Class controller = Class.forName(binary(CONTROLLER), false, one); + Class cartInTwo = Class.forName(binary(cart), false, two); + + AppWarmup warmup = new AppWarmup(instrumentation(main, controller, cartInTwo)); + warmup.run(Arrays.asList(binary(cart), binary(MAIN), binary(CONTROLLER))); + + assertEquals(3, warmup.initialized()); + assertSame(two, INITIALIZED.get(cart)); + assertSame(one, INITIALIZED.get(MAIN)); + } + } + + @Test + public void theLoaderThatDefinedMostOfTheAppsClassesIsTriedFirst() throws Exception { + File classes = appClasses(MAIN, CONTROLLER, "com/acme/shop/Cart"); + File library = appClasses("org/lib/Util"); + ClassLoader parent = WarmupTest.class.getClassLoader(); + try (URLClassLoader one = new URLClassLoader(new URL[] {classes.toURI().toURL()}, parent); + URLClassLoader two = new URLClassLoader(new URL[] {classes.toURI().toURL()}, parent); + URLClassLoader lib = new URLClassLoader(new URL[] {library.toURI().toURL()}, parent); + URLClassLoader context = new URLClassLoader(new URL[0], parent)) { + Class cartInOne = Class.forName(binary("com/acme/shop/Cart"), false, one); + Class mainInTwo = Class.forName(binary(MAIN), false, two); + Class controllerInTwo = Class.forName(binary(CONTROLLER), false, two); + Class util = Class.forName(binary("org/lib/Util"), false, lib); + Map> loadedApp = new HashMap<>(); + + List loaders = AppWarmup.loaders( + new Class[] {cartInOne, util, mainInTwo, String.class, controllerInTwo}, + // String, named as an app class, has no loader: none is added for it. + new HashSet<>(Arrays.asList(binary(MAIN), binary(CONTROLLER), binary("com/acme/shop/Cart"), + "java.lang.String")), + loadedApp, Arrays.asList(context, one), parent, null); + + assertEquals(Arrays.asList(two, one, context, parent), loaders); + assertEquals(3, loadedApp.size()); + assertSame(mainInTwo, loadedApp.get(binary(MAIN))); + assertSame(cartInOne, loadedApp.get(binary("com/acme/shop/Cart"))); + } + } + + @Test + public void contextLoadersAreOrderedOldestThreadFirst() throws Exception { + try (URLClassLoader app = new URLClassLoader(new URL[0], WarmupTest.class.getClassLoader())) { + runAppMain(app); + List loaders = AppWarmup.contextLoaders(); + assertTrue(loaders.contains(app)); + assertEquals(new HashSet<>(loaders).size(), loaders.size()); + // The oldest live thread's loader leads; this JVM's main thread + // is older than the app's stand-in. + Thread oldest = null; + for (Thread thread : Thread.getAllStackTraces().keySet()) { + if (thread.getContextClassLoader() != null && (oldest == null || thread.getId() < oldest.getId())) { + oldest = thread; + } + } + assertSame(oldest.getContextClassLoader(), loaders.get(0)); + } + } + + @Test + public void ofAClassLoadedTwiceTheCopyOfTheLoaderThatDefinedMoreIsInitialized() throws Exception { + // Cart and Order are loaded in both loaders; one defined more of the + // app's classes. Whichever copy the JVM lists first, one's is + // initialized, and two's never is. + String cart = "com/acme/shop/Cart"; + String order = "com/acme/shop/Order"; + File classes = appClasses(MAIN, CONTROLLER, cart, order); + ClassLoader parent = WarmupTest.class.getClassLoader(); + try (URLClassLoader one = new URLClassLoader(new URL[] {classes.toURI().toURL()}, parent); + URLClassLoader two = new URLClassLoader(new URL[] {classes.toURI().toURL()}, parent)) { + Class main = Class.forName(binary(MAIN), false, one); + Class controller = Class.forName(binary(CONTROLLER), false, one); + Class cartInOne = Class.forName(binary(cart), false, one); + Class cartInTwo = Class.forName(binary(cart), false, two); + Class orderInOne = Class.forName(binary(order), false, one); + Class orderInTwo = Class.forName(binary(order), false, two); + + AppWarmup warmup = new AppWarmup( + instrumentation(cartInTwo, main, orderInOne, cartInOne, controller, orderInTwo)); + warmup.run(Arrays.asList(binary(cart), binary(order), binary(MAIN), binary(CONTROLLER))); + + assertEquals(4, warmup.initialized()); + assertEquals(Arrays.asList(one), runsOf(cart)); + assertEquals(Arrays.asList(one), runsOf(order)); + } + } + + @Test + public void aClassFoundButNotInitializedIsNotLoadedAgainInAnotherLoader() throws Exception { + // Broken's static initializer throws. one, tried first, finds it + // and runs it; two, which also sees its bytes, is not tried, so it + // runs once. Found, which only two sees, is tried there. + String broken = "com/acme/shop/Broken"; + String found = "com/acme/shop/Found"; + String loadedName = "com/acme/shop/Loaded"; + File first = appClasses(broken, loadedName); + File second = appClasses(broken, found); + ClassLoader parent = WarmupTest.class.getClassLoader(); + try (URLClassLoader one = new URLClassLoader(new URL[] {first.toURI().toURL()}, parent); + URLClassLoader two = new URLClassLoader(new URL[] {second.toURI().toURL()}, parent)) { + // one defined a class of the app's, so it is tried first; two is + // the context loader of a live thread, tried after. + Class loaded = Class.forName(binary(loadedName), false, one); + Thread holder = new Thread(() -> { + try { + appEnds.await(60, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }, "app-worker"); + holder.setContextClassLoader(two); + holder.setDaemon(true); + holder.start(); + try { + AppWarmup warmup = new AppWarmup(instrumentation(loaded)); + warmup.run(Arrays.asList(binary(broken), binary(found), binary(loadedName))); + assertEquals(2, warmup.initialized()); + assertEquals(1, warmup.skipped()); + assertEquals(Arrays.asList(one), runsOf(broken)); + assertEquals(Arrays.asList(two), runsOf(found)); + assertEquals(Arrays.asList(one), runsOf(loadedName)); + } finally { + appEnds.countDown(); + holder.join(5000); + } + } + } + + @Test + public void aWarmupThreadThatCannotStartEndsNeitherTheStartNorTheServer() throws Exception { + File classes = appClasses(MAIN); + System.setProperty("keploy.java.class.dirs", classes.getPath()); + KeployDedupAgent.warmupThreads = runnable -> new Thread(runnable) { + @Override + public synchronized void start() { + throw new OutOfMemoryError("unable to create native thread: possibly out of memory"); + } + }; + + assertTrue(KeployDedupAgent.start()); + assertEquals("ACK proto=2\n", send("START test-set-0/test-1")); + assertLogged("WARNING Keploy dedup: no warmup, as its thread could not start"); + assertTrue(INITIALIZED.isEmpty()); + // The control server still answers. + assertEquals("ACK proto=2\n", send("START test-set-0/test-2")); + } + + @Test + public void anEmptyClassListAsksTheJvmNothing() { + final List asked = new CopyOnWriteArrayList<>(); + Instrumentation instrumentation = (Instrumentation) Proxy.newProxyInstance( + WarmupTest.class.getClassLoader(), new Class[] {Instrumentation.class}, + (proxy, method, args) -> { + asked.add(method.getName()); + return new Class[0]; + }); + AppWarmup warmup = new AppWarmup(instrumentation); + warmup.run(new ArrayList()); + assertEquals(0, warmup.initialized()); + assertTrue("asked: " + asked, asked.isEmpty()); + } + + @Test + public void itSaysWhatItIsDoing() throws Exception { + String slowName = "com/acme/a/Slow"; + File classes = appClasses(slowName, MAIN); + slow = new CountDownLatch(1); + try (URLClassLoader app = new URLClassLoader(new URL[] {classes.toURI().toURL()}, + WarmupTest.class.getClassLoader())) { + runAppMain(app); + // The JVM's list of loaded classes comes only once the test has + // seen the warmup ask for it. + final CountDownLatch asked = new CountDownLatch(1); + final CountDownLatch answer = new CountDownLatch(1); + Instrumentation instrumentation = (Instrumentation) Proxy.newProxyInstance( + WarmupTest.class.getClassLoader(), new Class[] {Instrumentation.class}, + (proxy, method, args) -> { + asked.countDown(); + answer.await(10, TimeUnit.SECONDS); + return new Class[0]; + }); + final AppWarmup warmup = new AppWarmup(instrumentation); + assertEquals("reading the app's class files", warmup.doing()); + Thread thread = new Thread(() -> warmup.run(Arrays.asList(binary(slowName), binary(MAIN)))); + thread.start(); + assertTrue(asked.await(10, TimeUnit.SECONDS)); + assertEquals("finding the app's class loaders", warmup.doing()); + answer.countDown(); + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10); + while (!INITIALIZED.containsKey(slowName) && System.nanoTime() < deadline) { + Thread.sleep(5); + } + assertEquals("initializing com.acme.a.Slow (class 1 of 2)", warmup.doing()); + warmup.stop(); + slow.countDown(); + thread.join(10000); + assertEquals(1, warmup.initialized()); + assertEquals(1, warmup.notReached()); + } + } + + @Test + public void everyLiveThreadIsSeenFromTheRootGroup() throws Exception { + try (URLClassLoader app = new URLClassLoader(new URL[0], WarmupTest.class.getClassLoader())) { + runAppMain(app); + Thread referenceHandler = null; + for (Thread thread : Thread.getAllStackTraces().keySet()) { + if (thread.getName().equals("Reference Handler")) { + referenceHandler = thread; + } + } + // The JVM's own threads are in the root group, above this one's. + List live = AppWarmup.liveThreads(); + assertTrue(live.contains(referenceHandler)); + assertTrue(live.contains(appMain)); + assertTrue(live.contains(Thread.currentThread())); + // An array too small at first grows until every thread fits. + ThreadGroup root = Thread.currentThread().getThreadGroup(); + while (root.getParent() != null) { + root = root.getParent(); + } + List grown = AppWarmup.liveThreads(root, 1); + assertTrue(grown.contains(referenceHandler)); + assertTrue(grown.contains(appMain)); + assertTrue(grown.contains(Thread.currentThread())); + } + } + + private static List runsOf(String name) { + List loaders = new ArrayList<>(); + for (Map.Entry run : RUNS) { + if (run.getKey().equals(name)) { + loaders.add(run.getValue()); + } + } + return loaders; + } + + // runAppMain starts the app's stand-in main thread, holding app as its + // context loader until the test ends. + private void runAppMain(ClassLoader app) { + appMain = new Thread(() -> { + try { + appEnds.await(60, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }, "app-main"); + appMain.setContextClassLoader(app); + appMain.setDaemon(true); + appMain.start(); + } + + // appClasses writes, under a new dir not on the test's classpath, a class + // per name whose calls initialized(name). + private File appClasses(String... names) throws IOException { + File dir = tmp.newFolder(); + for (String name : names) { + ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); + writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, name, null, "java/lang/Object", null); + MethodVisitor clinit = writer.visitMethod(Opcodes.ACC_STATIC, "", "()V", null, null); + clinit.visitCode(); + clinit.visitLdcInsn(name); + clinit.visitLdcInsn(org.objectweb.asm.Type.getObjectType(name)); + clinit.visitMethodInsn(Opcodes.INVOKESTATIC, "io/keploy/dedup/WarmupTest", "initialized", + "(Ljava/lang/String;Ljava/lang/Class;)V", false); + clinit.visitInsn(Opcodes.RETURN); + clinit.visitMaxs(0, 0); + clinit.visitEnd(); + writer.visitEnd(); + File file = new File(dir, name + ".class"); + file.getParentFile().mkdirs(); + Files.write(file.toPath(), writer.toByteArray()); + } + return dir; + } + + private static String binary(String name) { + return name.replace('/', '.'); + } + + // instrumentation answers getAllLoadedClasses with loaded, as premain's + // would; nothing else is called. + private static Instrumentation instrumentation(final Class... loaded) { + return (Instrumentation) Proxy.newProxyInstance(WarmupTest.class.getClassLoader(), + new Class[] {Instrumentation.class}, new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, Object[] args) { + if (method.getName().equals("getAllLoadedClasses")) { + return loaded.clone(); + } + throw new UnsupportedOperationException(method.getName()); + } + }); + } + + private void assertLogged(String prefix) { + for (String line : logged) { + if (line.startsWith(prefix)) { + return; + } + } + throw new AssertionError("nothing logged starts with: " + prefix + "\nlogged: " + logged); + } + + private int countLogged(String prefix) { + int count = 0; + for (String line : logged) { + if (line.startsWith(prefix)) { + count++; + } + } + return count; + } + + private String loggedStartingWith(String prefix) { + for (String line : logged) { + if (line.startsWith(prefix)) { + return line; + } + } + throw new AssertionError("nothing logged starts with: " + prefix + "\nlogged: " + logged); + } + + private void assertNotLogged(String part) { + for (String line : logged) { + assertFalse("logged: " + line, line.contains(part)); + } + } + + private void awaitLogged(String prefix) throws InterruptedException { + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10); + while (System.nanoTime() < deadline) { + for (String line : logged) { + if (line.startsWith(prefix)) { + return; + } + } + Thread.sleep(20); + } + assertLogged(prefix); + } + + private static String send(String command) throws IOException { + try (AFUNIXSocket socket = AFUNIXSocket.newInstance()) { + socket.connect(AFUNIXSocketAddress.of(new File(KeployDedupAgent.controlSocketPath)), 3000); + socket.setSoTimeout(30000); + OutputStream out = socket.getOutputStream(); + out.write((command + "\n").getBytes(StandardCharsets.UTF_8)); + out.flush(); + InputStream in = socket.getInputStream(); + StringBuilder answer = new StringBuilder(); + int b; + while ((b = in.read()) != -1) { + answer.append((char) b); + } + return answer.toString(); + } + } +} From c22b88b56737244060d2edefcfcfc449e4de0109 Mon Sep 17 00:00:00 2001 From: slayerjain Date: Fri, 20 Nov 2026 03:00:17 +0530 Subject: [PATCH 04/14] feat(dedup): send Keploy the hits between tests, when it asks What a START clears ran between two tests: a health check, a scheduled job, a cache refresh. It never counts in a test's hits, but the same threads also run while a test does, so their classes' probes land in some tests and not others from one run to the next. Keploy can leave those probes out of what it compares only if it knows them. Keploy now asks with the line BACKGROUND on the control socket. The SDK answers ACK when it reads JaCoCo's probes in place and a receiver is on the data socket, else IGNORED, as an older SDK answers a line it does not know. From then on, each START that clears hits made since the last test's END (an END answered ACK) sends one record on the data socket, right after that START is answered, after the INDEX record when that receiver does not have it yet: {"kind":"background","v":1,"build":"", "after":"","before":"", "executedLinesByFile":{class:[probe...]}} plus "lost":N when the records of the N windows before it could not be made or sent (each is tried once; one WARNING while the sends fail). The probes are under a test's record's key and in its shape, so Keploy reads both alike. Of two copies of one class, the copy the coverage manifest names, else the lowest id. - Not background, never sent: what ran before the first test (the app's start, the warmup), and what ran after an END that was not taken, or after a START whose END never came (a test's own hits). - A test's own work that goes on after its END (an async task it started) lands in the next window and is reported as background; the record names both tests so that Keploy can weigh that. - The ask holds for one run of the app and one receiver: an app started again (native keploy test does so for each test set) and a receiver bound anew on the data socket's path (the next docker-compose test set) get none until Keploy asks again, so Keploy asks after each INDEX record. A Keploy that never asks gets none, short of a new receiver bound within one file-system clock tick of the one that asked (receivers are told apart by the socket file's inode and modification time, and a socket file's modification time is its bind time). - If the in-place read fails after the ask, the records stop (the read's WARNING says so): Keploy then leaves fewer probes out, never more. - No BACKGROUND on the TCP transport used in Kubernetes. KEPLOY_JAVA_SCAN=off (any case) turns the in-place read off: each test takes JaCoCo's reset and dump, as when JaCoCo is reached over TCP, and BACKGROUND is answered IGNORED. Rows (InProcessScanTest): a window's hits reach a receiver that asked once, and never a test; a class loaded twice is reported as a test's record would; no record for a receiver that did not ask; none after a START with no END, an END for another test (with or without a START before it), or an END whose record could not be sent (skipped as root); a receiver bound anew gets none until it asks, and the INDEX record before its first window; with no receiver BACKGROUND is IGNORED and a window goes nowhere; records that could not be sent are counted in the next, said once, kept across a re-ask by the same receiver, and not carried to a new receiver (skipped as root); with the scan off each test takes JaCoCo's dump and BACKGROUND is IGNORED; any other value keeps the scan. Signed-off-by: slayerjain --- README.md | 3 + .../io/keploy/dedup/KeployDedupAgent.java | 267 ++++++++++-- .../io/keploy/dedup/InProcessScanTest.java | 408 +++++++++++++++++- 3 files changed, 648 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 92c7eb79..02c7ece2 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ With the JaCoCo agent in the application's JVM (the usual case), steps 2 and 5 a Before the first testcase's coverage, the agent sends Enterprise one INDEX record on `/tmp/coverage_data.sock`: a JSON object starting with `{"kind":"index",` that says how it found the application's own classes (`source`: `override` when the dirs and jars in `KEPLOY_JAVA_CLASS_DIRS` held them, `derived` from the launch or from `target/classes` or `build/classes/java/main` under the working directory, `whole-jar` or `classpath` when it could not tell them from the libraries, `none`), what it found them from in words (`launch`), the dirs and jars it read them from (`roots`, at most 64, and `rootCount`), the package roots it kept (`packages`), how many classes it indexed, the build id of those classes (`jv1-` and 32 hex), which JaCoCo agent measures them (`jacoco`: `embedded`, the one keploy-sdk.jar starts, `attached` by the command, `none`, or `unchecked` when the agent was not started as a `-javaagent`), that embedded agent's `includes=`, and two versions as their jars' manifests name them, or `""` when not known: keploy-sdk.jar's (`sdkVersion`) and that of the JaCoCo agent that measures the classes, embedded or attached (`jacocoVersion`). It carries no class list. It is sent as the agent starts when that path's socket file is there (Enterprise's receiver makes it), and otherwise, or when that send fails, right before the first testcase's coverage; a receiver Enterprise binds anew on that path gets it again before its first record. On the TCP transport used in Kubernetes no INDEX record is sent. +Since 2.2.0 Keploy can also ask for that background, to keep it out of what it compares: it sends the line `BACKGROUND` on `/tmp/coverage_control.sock`. The agent answers `ACK` when it reads JaCoCo's probes in place and a receiver is on `/tmp/coverage_data.sock`, and `IGNORED` otherwise (`KEPLOY_JAVA_SCAN=off`, JaCoCo reached over TCP, the in-place read failed, or no receiver), as an older agent answers a line it does not know. The ask holds for this run of the application and that receiver only: an application started again (native `keploy test` starts it again for each test set) and a receiver Enterprise binds anew on that path (the next docker-compose test set) get no record until Keploy asks again, so Keploy asks after each INDEX record, which comes once per start of the application and once per new receiver. From then on, each `START` that cleared hits made since the previous testcase's `END` sends one BACKGROUND record on `/tmp/coverage_data.sock`, right after that `START` is answered (after the INDEX record, when that receiver does not have it yet): `{"kind":"background","v":1,"build":"","after":"","before":"","executedLinesByFile":{className:[probeIdx]}}`, under the key and in the shape of a testcase's record, plus `"lost":N` when the records of the N windows before it could not be made or sent. A window with no hits sends nothing. What ran before the first testcase (the application's start, the warmup) is not background, and neither is what ran after an `END` that was not taken or a `START` whose `END` never came (a testcase's own hits): none of it is sent. A test's own work that goes on after its `END` (an async task it started) lands in the next window, and so is reported as background. A Keploy that never asks gets no record, short of a new receiver that replaces the one that asked within the same instant (the agent tells receivers apart by the socket file's inode and modification time). On the TCP transport used in Kubernetes there is no BACKGROUND. + The agent binds `/tmp/coverage_control.sock` before the application's `main` runs, so the socket is listening before the application can answer a health check. It answers `ACK` to each command it took, and `IGNORED` to an `END` that no `START` for that testcase came before: nothing is published for it, and Enterprise keeps that testcase out of the comparison. On the TCP transport used in Kubernetes the same `END` is answered with `IGNORED ` and then `ACK`. ## How to Use @@ -202,6 +204,7 @@ ceiling does not quietly fall behind again. - `KEPLOY_JAVA_CLASSPATH_FALLBACK`: scans the full classpath when neither the app's own classes nor the executable jar give any class. `false` leaves the index empty then, and no test's coverage is measured. Default: `true` - `KEPLOY_JAVA_DEDUP_DISABLED`: disables the Java dedup agent when set to `true`, `1`, or `yes` - `KEPLOY_JAVA_DEDUP_DIAGNOSTICS`: when `true`, `1`, or `yes`, logs one line per START and END that says how long the command took and how long each step did: `warmup`, then `scan` (the pass that clears the background) for a START; `scan` (the pass that reads and clears the test's hits), `filter` (the hits made into the test's probes by class), `json` and `send` for an END, plus `classes` on TCP (the check for new app classes, and the class-zip frame when there are some). Where JaCoCo's own reset and dump are used instead, a START has `reset`, and an END has `dump` (JaCoCo's dump), `parse` and `filter` (to the app's classes) in place of `scan` and `filter`; a step that did not run is left out, so an empty dump has only `dump`. A START that cleared background hits also logs how many classes and probes they were. The app's class files are read once: before the app starts, when keploy-sdk.jar starts JaCoCo itself; otherwise on the first START, by the warmup's thread (timed under `warmup`, and, past the 10 seconds that START waits for the warmup, under its `scan`, which waits for the read, or, with JaCoCo's reset and dump, which never wait for it, on the first END whose dump is not empty, under `filter`), or, with the warmup off, under that START's `scan` (or, with JaCoCo's dump, on the first END whose dump is not empty, under `filter`). On the unix socket `took` ends before the answer is written; on TCP it includes the answer. Use it to see where a test's time goes on your app +- `KEPLOY_JAVA_SCAN`: `off` (in any case) turns the one-pass read of the application's probes off: each testcase then takes JaCoCo's own reset and dump, as when JaCoCo is reached over TCP, a `START` no longer sees background hits, and `BACKGROUND` is answered `IGNORED`. Use it if the in-place read misbehaves on your JVM; at 50k application classes JaCoCo's reset and dump took about 20 ms per testcase in our profile, against under 2 ms with the in-place read. Default: on - `KEPLOY_JAVA_DEDUP_WARMUP_DISABLED`: when `true`, `1`, or `yes`, skips the warmup, which loads and initializes the app's own classes once, on the first START, so that no test is charged with their static initializers. It loads them through the class loader the app runs them with, such as a Spring Boot fat jar's own loader (since 2.2.0; before, a fat jar's classes were not warmed up), so every app class not loaded yet is loaded then, and JaCoCo instruments it: on an app with many classes it never uses, that is more class metadata (metaspace) than the app alone would load. It runs on a thread of its own: the first START waits for it for at most 10 seconds, then goes on, and the warmup stops after the class it is initializing. Its static initializer goes on while the tests run, and each line it runs from then on counts in the test running at the time (between two tests, in none: the next START clears it); classes it did not reach initialize when the app first uses them, as with the warmup off. The log says how long the warmup took, and, when the START stopped waiting, which class it was initializing ## Which classes count as the app's diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java index 6dcf73fc..c368d052 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java @@ -48,6 +48,7 @@ import java.util.Set; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Supplier; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.zip.ZipEntry; @@ -117,6 +118,12 @@ static String encodeBytecodeFrame(String buildTag, String manifestJson, byte[] z // closed with its command unread gives Keploy no error, only the close. static final String UNIX_REPLY_TAKEN = "ACK proto=2"; static final String UNIX_REPLY_IGNORED = IGNORED_REPLY + " proto=2"; + // The unix control command a Keploy that reads BACKGROUND records sends + // to get them: ACK when this SDK will send them to the receiver on the + // data socket now, IGNORED when it sees no hits between tests (JaCoCo's + // probes not read in place) or no receiver is there. An older SDK + // answers IGNORED too, as to any line that is no command. + static final String BACKGROUND_COMMAND = "BACKGROUND"; private static final AtomicBoolean STARTED = new AtomicBoolean(false); private static final AtomicBoolean SHUTDOWN_HOOK_REGISTERED = new AtomicBoolean(false); @@ -587,6 +594,17 @@ private static final class CommandServer implements CoverageWorker { // deletes the file only while it is still this socket's. private volatile Object socketFileKey; private String activeTestId = ""; + // The id of the test whose END last sent its coverage, set once that + // record is sent and cleared by every START and every other END: what + // the next START clears then ran between two tests. BACKGROUND and + // lines that are no command leave it (a receiver may ask between an + // END and a START). Like activeTestId, only the worker thread uses it. + private String lastEndedTestId; + // The between-tests hits the last START cleared, for a receiver that + // asked for them: made into a record and sent once that START's + // answer is written and its connection closed, off the time Keploy + // waits for the answer. + private BackgroundWindow pendingBackground; CommandServer(CoverageCollector collector, CoveragePublisher publisher, File controlSocket) { this.collector = collector; @@ -694,11 +712,20 @@ private void handle(Socket socket) { } catch (IOException e) { log(Level.SEVERE, "Failed to handle Java dedup coverage command", e); } + BackgroundWindow window = pendingBackground; + if (window != null) { + pendingBackground = null; + publisher.sendBackground(window.after, window.before, + () -> collector.backgroundProbes(window.cleared)); + } } // run runs one command line ("START id", "END id") and says whether it // was taken. With diagnostics on, it says how long each step took. private boolean run(String line) { + if (BACKGROUND_COMMAND.equals(line)) { + return askBackground(); + } CoverageCommand command = CoverageCommand.parse(line); if (command == null) { return false; @@ -720,17 +747,31 @@ private boolean run(CoverageCommand command, Phases phases) { // cleared hold the lines of the tests before this one, so // this START is not taken and its END is ignored too. activeTestId = ""; + String after = lastEndedTestId; + lastEndedTestId = null; // Warm up app classes once before the first measured window so // one-time lines aren't charged to the first test. collector.warmup(); phases.mark("warmup"); - if (!collector.begin(phases)) { + List cleared = new ArrayList<>(); + if (!collector.begin(phases, cleared)) { return false; } activeTestId = command.testId; + // Only what ran since a test's coverage was read: before the + // first test it is the app's start and the warmup, and after + // a START with no END, or an END not taken, that test's own + // hits. + if (after != null && !cleared.isEmpty() && publisher.backgroundAsked()) { + pendingBackground = new BackgroundWindow(after, command.testId, cleared); + } return true; } + // Set again only once this END's record is sent: after an END not + // taken (another test's id, its hits lost, its send failed), what + // the next START clears may be a test's own hits. + lastEndedTestId = null; if (!command.testId.equals(activeTestId)) { log(Level.SEVERE, "Ignoring mismatched END command. expected=" + activeTestId + ", actual=" @@ -747,6 +788,7 @@ private boolean run(CoverageCommand command, Phases phases) { log(Level.FINE, "No Java coverage collected for " + command.testId, null); } publisher.publish(command.testId, executedProbesByClass, phases); + lastEndedTestId = command.testId; return true; } catch (Exception e) { // Not ACK for the same reason: nothing reached Keploy. @@ -757,6 +799,19 @@ private boolean run(CoverageCommand command, Phases phases) { } } + // askBackground answers BACKGROUND: ACK when the hits each START + // clears are seen (the scan) and a receiver is on the data socket, + // which from then on gets them; IGNORED otherwise. + private boolean askBackground() { + if (collector.probeScan() == null) { + log(Level.INFO, "Keploy dedup: BACKGROUND is not taken: JaCoCo's probes are not read in place" + + " (KEPLOY_JAVA_SCAN off, JaCoCo over TCP, or the scan failed), so the hits between" + + " two tests are not seen", null); + return false; + } + return publisher.askBackground(); + } + private void writeReply(OutputStream outputStream, String reply) { try { outputStream.write((reply + "\n").getBytes(StandardCharsets.UTF_8)); @@ -909,7 +964,7 @@ private void dispatch(CoverageCommand command, OutputStream out, Phases phases) // one-time lines aren't charged to the first test. collector.warmup(); phases.mark("warmup"); - reset = collector.begin(phases); + reset = collector.begin(phases, new ArrayList()); } catch (RuntimeException e) { // Not taken, and the connection goes on: thrown past // here, it ended this thread, and no later command @@ -1095,15 +1150,21 @@ private CoverageCollector(JacocoClient jacocoClient, CoverageIndex coverageIndex } // The one-pass scan of the app classes' probes, when JaCoCo's agent - // runs in this JVM and its runtime data could be read; null when it - // is reached over TCP, or the scan failed once. Looked up on the - // first command; both are guarded by the collector. + // runs in this JVM and its runtime data could be read; null with + // KEPLOY_JAVA_SCAN=off, when JaCoCo is reached over TCP, or once the + // scan failed. Looked up on the first START, END or BACKGROUND; both + // are guarded by the collector. private ProbeScan probeScan; private boolean probeScanLookedUp; private synchronized ProbeScan probeScan() { if (!probeScanLookedUp) { probeScanLookedUp = true; + if (isScanTurnedOff()) { + log(Level.INFO, "Keploy dedup: KEPLOY_JAVA_SCAN is off, so each test takes JaCoCo's dump" + + " and reset, and no BACKGROUND record is sent", null); + return null; + } Object agent = jacocoClient.runtimeAgent(); if (agent != null) { try { @@ -1132,10 +1193,10 @@ private synchronized void dropProbeScan(Throwable cause) { // so that none of it is charged to the next test, and says whether // it did. With the scan, that is one pass over the app's classes, and // what it clears is background (a scheduled job, a health check): - // counted in the diagnostics, never in a test's hits. Without it, or - // when the pass fails (in any way: a START only has to clear), - // JaCoCo resets every class. - private boolean begin(Phases phases) { + // added to cleared, counted in the diagnostics, never in a test's + // hits. Without it, or when the pass fails (in any way: a START only + // has to clear), JaCoCo resets every class, and cleared stays empty. + private boolean begin(Phases phases, List cleared) { ProbeScan scan = probeScan(); if (scan != null) { try { @@ -1144,6 +1205,7 @@ private boolean begin(Phases phases) { if (diagnosticsEnabled() && !background.isEmpty()) { diagnostic("background hits cleared at START: " + describe(background)); } + cleared.addAll(background); return true; } catch (ReflectiveOperationException | RuntimeException e) { dropProbeScan(e); @@ -1262,6 +1324,12 @@ private void runWarmup(AppWarmup warmup, long began) { } } + // isScanTurnedOff says whether KEPLOY_JAVA_SCAN (or keploy.java.scan) + // is off, in any case: the one-pass scan is then never used. + private boolean isScanTurnedOff() { + return "off".equalsIgnoreCase(envOrProperty("KEPLOY_JAVA_SCAN", "keploy.java.scan", "")); + } + private boolean isWarmupDisabled() { return isTruthy(envOrProperty("KEPLOY_JAVA_DEDUP_WARMUP_DISABLED", "keploy.java.dedup.warmup.disabled", "")); @@ -1373,20 +1441,8 @@ private Map> probesByClass(List hits, Phase Map> byClass = new LinkedHashMap<>(); for (int i = 0; i < hits.size(); ) { String name = hits.get(i).name; - int end = i + 1; - while (end < hits.size() && hits.get(end).name.equals(name)) { - end++; - } - ProbeScan.Hit chosen = hits.get(i); - ManifestEntry known = liveManifest.get(name); - if (known != null) { - for (int k = i; k < end; k++) { - if (Long.toHexString(hits.get(k).id).equals(known.id)) { - chosen = hits.get(k); - break; - } - } - } + int end = copiesEnd(hits, i); + ProbeScan.Hit chosen = chosenCopy(hits, i, end); i = end; final int probeCount = chosen.probeCount; final long classId = chosen.id; @@ -1396,11 +1452,7 @@ private Map> probesByClass(List hits, Phase m.probeCount = probeCount; return m; }); - List probes = new ArrayList<>(chosen.probes.length); - for (int probe : chosen.probes) { - probes.add(probe); - } - byClass.put(name, probes); + byClass.put(name, probeList(chosen)); } phases.mark("filter"); if (diagnosticsEnabled()) { @@ -1410,6 +1462,55 @@ private Map> probesByClass(List hits, Phase return byClass; } + // backgroundProbes turns the hits a START cleared into probes by + // class, of two copies of one class the one probesByClass would + // report, and adds none of them to the coverage manifest: no test + // ran them. + private Map> backgroundProbes(List hits) { + Map> byClass = new LinkedHashMap<>(); + for (int i = 0; i < hits.size(); ) { + int end = copiesEnd(hits, i); + byClass.put(hits.get(i).name, probeList(chosenCopy(hits, i, end))); + i = end; + } + return byClass; + } + + // copiesEnd returns the end of the run of hits, sorted by name, that + // share the name of hits[from]: the copies of one class loaded from + // different bytes. + private int copiesEnd(List hits, int from) { + String name = hits.get(from).name; + int end = from + 1; + while (end < hits.size() && hits.get(end).name.equals(name)) { + end++; + } + return end; + } + + // chosenCopy picks, of the copies hits[from, to) of one class, the + // one whose id the coverage manifest holds, else the first (the + // lowest id). + private ProbeScan.Hit chosenCopy(List hits, int from, int to) { + ManifestEntry known = liveManifest.get(hits.get(from).name); + if (known != null) { + for (int k = from; k < to; k++) { + if (Long.toHexString(hits.get(k).id).equals(known.id)) { + return hits.get(k); + } + } + } + return hits.get(from); + } + + private List probeList(ProbeScan.Hit hit) { + List probes = new ArrayList<>(hit.probes.length); + for (int probe : hit.probes) { + probes.add(probe); + } + return probes; + } + // indexedClassNames returns the VM names of the app's own classes (from the // CoverageIndex) so probe collection can skip JDK/library classes. Built // once: the index never changes once loaded, and building the set again @@ -2120,6 +2221,15 @@ private static final class CoveragePublisher { // new run, a docker-compose test set, a restarted Keploy) gets it // again before its first record. private Object indexSentTo; + // The receiver that asked this JVM for BACKGROUND records, null when + // none has, or the receiver there now is another: a new run, a + // restarted Keploy, or an older Keploy, which would read the record + // as a test's coverage, get none until they ask. The ask lives in + // this JVM only: an app started again has none until Keploy asks it. + private Object backgroundAskedBy; + // The between-tests windows whose record could not be sent since the + // last one that was, told to the receiver in that next record. + private int backgroundLost; private CoveragePublisher(File socketFile, CoverageIndex index) { this.socketFile = socketFile; @@ -2149,6 +2259,64 @@ synchronized void sendIndex() { } } + // askBackground records that the receiver on the data socket now + // asked for BACKGROUND records, and says whether there is one. + synchronized boolean askBackground() { + Object key = receiverKey(socketFile); + if (key == null) { + log(Level.INFO, "Keploy dedup: BACKGROUND is not taken: no receiver is on " + + socketFile.getAbsolutePath(), null); + return false; + } + if (!key.equals(backgroundAskedBy)) { + backgroundLost = 0; + } + backgroundAskedBy = key; + return true; + } + + synchronized boolean backgroundAsked() { + return backgroundAskedBy != null; + } + + // sendBackground sends a between-tests window's hits (classes makes + // them, only when they go out) to the receiver that asked for them, + // after the INDEX report when that receiver does not have it yet. + // When the receiver there is another (or none), nothing is sent and + // none goes to it until it asks. A record that could not be made or + // sent is not tried again: the next one that goes through counts it + // in "lost". It never fails the caller. + synchronized void sendBackground(String after, String before, + Supplier>> classes) { + Object key = receiverKey(socketFile); + if (key == null || !key.equals(backgroundAskedBy)) { + backgroundAskedBy = null; + backgroundLost = 0; + return; + } + try { + BackgroundRecord record = new BackgroundRecord(index.report().build, after, before, classes.get()); + if (backgroundLost > 0) { + record.lost = backgroundLost; + } + sendIndex(); + send(GSON.toJson(record).getBytes(StandardCharsets.UTF_8)); + backgroundLost = 0; + if (diagnosticsEnabled()) { + diagnostic("BACKGROUND record sent: after=" + after + ", before=" + before + + ", classes=" + record.executedLinesByFile.size()); + } + } catch (Throwable t) { + // Throwable, as for the INDEX report: an Error here must not + // end the worker. Said once while the sends fail, not once + // per test. + backgroundLost++; + log(backgroundLost == 1 ? Level.WARNING : Level.FINE, + "Keploy dedup: the BACKGROUND record of the hits before " + before + + " could not be sent to " + socketFile.getAbsolutePath(), t); + } + } + // publish sends the test's coverage, after the INDEX report when the // receiver does not have it yet: the report's time counts in "send". private void publish(String testId, Map> executedProbesByClass, Phases phases) @@ -2194,6 +2362,49 @@ private DedupPayload(String id, Map> executedProbesByClass } } + // BackgroundWindow is what a START cleared after the END of `after` + // sent that test's coverage, up to the START of `before`, until it is + // made into a BackgroundRecord. + private static final class BackgroundWindow { + + private final String after; + private final String before; + private final List cleared; + + private BackgroundWindow(String after, String before, List cleared) { + this.after = after; + this.before = before; + this.cleared = cleared; + } + } + + // BackgroundRecord is what the app's classes ran between two tests: the + // hits the START of `before` cleared, made after the END of `after` read + // that test's. Sent on the data socket, one per connection, only to a + // receiver that asked with BACKGROUND, and only for a window with hits. + // build is the INDEX report's. executedLinesByFile is {vmClassName -> + // [probeIdx]} under a test's record's key and in its shape (DedupPayload), + // names in order and probes ascending, so that Keploy reads both alike. + // lost, when set, counts the windows before this one whose record could + // not be made or sent. + static final class BackgroundRecord { + + final String kind = "background"; + final int v = 1; + final String build; + final String after; + final String before; + final Map> executedLinesByFile; + Integer lost; + + BackgroundRecord(String build, String after, String before, Map> classes) { + this.build = build; + this.after = after; + this.before = before; + this.executedLinesByFile = classes; + } + } + private static final class ClassEntry { private final String className; diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java index 3bdfc388..0c1c767c 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; @@ -14,17 +15,20 @@ import java.net.Socket; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.util.Arrays; import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Handler; import java.util.logging.LogRecord; import java.util.logging.Logger; import org.jacoco.core.data.ExecutionDataWriter; import org.jacoco.core.runtime.RuntimeData; import org.junit.After; +import org.junit.Assume; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -49,10 +53,14 @@ public class InProcessScanTest { private static final String[] PROPERTIES = {"keploy.java.dedup.diagnostics", "keploy.java.class.dirs", "keploy.jacoco.port", "keploy.coverage.endpoint", "keploy.java.dedup.warmup.disabled", - "keploy.build.tag"}; + "keploy.build.tag", "keploy.java.scan"}; private static final String TAKEN = "ACK proto=2\n"; private static final String IGNORED = "IGNORED proto=2\n"; private static final String SHOP = "com/acme/Shop"; + private static final String CART = "com/acme/Cart"; + // The build id of the two classes setUp writes, as the INDEX report has it. + private static final String BUILD = IndexReport.buildId( + new long[] {IndexReport.classId(new byte[] {1}), IndexReport.classId(new byte[] {2})}); @Rule public TemporaryFolder tmp = new TemporaryFolder(); @@ -108,6 +116,7 @@ public void setUp() throws IOException, ReflectiveOperationException { File shop = new File(classes, SHOP + ".class"); shop.getParentFile().mkdirs(); Files.write(shop.toPath(), new byte[] {1}); + Files.write(new File(classes, CART + ".class").toPath(), new byte[] {2}); System.setProperty("keploy.java.class.dirs", classes.getPath()); // A port nothing listens on: no command may reach JaCoCo over TCP. System.setProperty("keploy.jacoco.port", "1"); @@ -203,6 +212,354 @@ public void withDiagnosticsOffTheBackgroundIsNotLogged() throws Exception { } } + @Test + public void betweenTwoTestsTheHitsGoOnceToAReceiverThatAskedAndNeverIntoATest() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] cart = agent.probes(0x4321L, CART, 3); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + // The app's start: not between two tests. + shop[0] = true; + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("BACKGROUND")); + shop[2] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[2]}}", + data.next()); + assertNull("no window before the first test", data.background.poll()); + + // A scheduled job between the tests. + shop[1] = true; + cart[2] = true; + cart[0] = true; + assertEquals(TAKEN, send("START test-set-0/test-2")); + assertEquals(background("test-set-0/test-1", "test-set-0/test-2", + probes(CART, "0,2") + "," + probes(SHOP, "1")), data.nextBackground()); + assertFalse("cleared", shop[1] || cart[0] || cart[2]); + shop[3] = true; + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals("{\"id\":\"test-set-0/test-2\",\"executedLinesByFile\":{\"" + SHOP + "\":[3]}}", + data.next()); + + // Nothing between these two: no record. + assertEquals(TAKEN, send("START test-set-0/test-3")); + assertEquals(TAKEN, send("END test-set-0/test-3")); + assertEquals("{\"id\":\"test-set-0/test-3\",\"executedLinesByFile\":{}}", data.next()); + assertNull("one record, for the one window with hits", data.background.poll()); + } + } + + @Test + public void betweenTwoTestsAClassLoadedTwiceIsReportedAsATestsHitsWouldBe() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] twin = agent.probes(0x99L, SHOP, 3); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("BACKGROUND")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{}}", data.next()); + shop[3] = true; + twin[1] = true; + assertEquals(TAKEN, send("START test-set-0/test-2")); + assertEquals("the copy with the lower id", + background("test-set-0/test-1", "test-set-0/test-2", probes(SHOP, "1")), data.nextBackground()); + assertFalse("both were cleared", shop[3] || twin[1]); + } + } + + @Test + public void noBackgroundRecordReachesAReceiverThatDidNotAsk() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{}}", data.next()); + shop[1] = true; + assertEquals(TAKEN, send("START test-set-0/test-2")); + assertFalse("cleared all the same", shop[1]); + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals("{\"id\":\"test-set-0/test-2\",\"executedLinesByFile\":{}}", data.next()); + assertNull(data.background.poll()); + } + } + + @Test + public void whatAStartClearsAfterNoTakenEndIsNoBackground() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("BACKGROUND")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{}}", data.next()); + assertEquals(TAKEN, send("START test-set-0/test-2")); + // test-2's END never comes: what test-2 ran is its own, not background. + shop[1] = true; + assertEquals(TAKEN, send("START test-set-0/test-3")); + // test-3's END names another test: not taken. + shop[2] = true; + assertEquals(IGNORED, send("END test-set-0/test-9")); + assertEquals(TAKEN, send("START test-set-0/test-4")); + shop[3] = true; + assertEquals(TAKEN, send("END test-set-0/test-4")); + assertEquals("{\"id\":\"test-set-0/test-4\",\"executedLinesByFile\":{\"" + SHOP + "\":[3]}}", + data.next()); + assertNull(data.background.poll()); + } + } + + @Test + public void anEndNotTakenWithNoStartBeforeItOpensNoWindow() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("BACKGROUND")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{}}", data.next()); + // test-2's START never reached the app, and Keploy ran the test + // and sent its END all the same: what test-2 ran is its own. + shop[1] = true; + assertEquals(IGNORED, send("END test-set-0/test-2")); + assertEquals(TAKEN, send("START test-set-0/test-3")); + assertFalse("cleared all the same", shop[1]); + assertEquals(TAKEN, send("END test-set-0/test-3")); + assertEquals("{\"id\":\"test-set-0/test-3\",\"executedLinesByFile\":{}}", data.next()); + assertNull(data.background.poll()); + // After an END taken, the next START's hits are background again. + shop[2] = true; + assertEquals(TAKEN, send("START test-set-0/test-4")); + assertEquals(background("test-set-0/test-3", "test-set-0/test-4", probes(SHOP, "2")), + data.nextBackground()); + } + } + + @Test + public void anEndWhoseRecordCouldNotBeSentOpensNoWindow() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + File socket = new File(KeployDedupAgent.dataSocketPath); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("BACKGROUND")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{}}", data.next()); + assertEquals(TAKEN, send("START test-set-0/test-2")); + shop[1] = true; + assertTrue(socket.setWritable(false, false) && socket.setReadable(false, false)); + Assume.assumeFalse("root connects to any socket", socket.canWrite()); + // test-2's coverage reaches no one: not taken. + assertEquals(IGNORED, send("END test-set-0/test-2")); + assertTrue(socket.setWritable(true, false) && socket.setReadable(true, false)); + shop[2] = true; + assertEquals(TAKEN, send("START test-set-0/test-3")); + assertFalse("cleared all the same", shop[1] || shop[2]); + assertEquals(TAKEN, send("END test-set-0/test-3")); + assertEquals("{\"id\":\"test-set-0/test-3\",\"executedLinesByFile\":{}}", data.next()); + assertNull("no window after an END whose record was not sent", data.background.poll()); + shop[3] = true; + assertEquals(TAKEN, send("START test-set-0/test-4")); + assertEquals(background("test-set-0/test-3", "test-set-0/test-4", probes(SHOP, "3")), + data.nextBackground()); + } finally { + socket.setWritable(true, false); + socket.setReadable(true, false); + } + } + + @Test + public void aReceiverBoundAnewGetsTheIndexBeforeABackgroundRecord() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + assertTrue(KeployDedupAgent.start()); + try (DataReceiver first = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{}}", first.next()); + } + new File(KeployDedupAgent.dataSocketPath).delete(); + try (DataReceiver second = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + // It asks before its first START: its first record is a window's. + assertEquals(TAKEN, send("BACKGROUND")); + shop[1] = true; + assertEquals(TAKEN, send("START test-set-1/test-1")); + assertEquals(background("test-set-0/test-1", "test-set-1/test-1", probes(SHOP, "1")), + second.nextBackground()); + assertEquals(Arrays.asList("index", "background"), second.kinds); + String index = second.index.poll(); + assertNotNull(index); + assertTrue(index, index.contains(",\"build\":\"" + BUILD + "\",")); + } + } + + @Test + public void aReceiverBoundAnewGetsNoBackgroundRecordUntilItAsks() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + assertTrue(KeployDedupAgent.start()); + try (DataReceiver first = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("BACKGROUND")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{}}", first.next()); + } + // The next test set's receiver, on the same path: it may be an older + // Keploy, which reads any record as a test's coverage. + new File(KeployDedupAgent.dataSocketPath).delete(); + try (DataReceiver second = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + shop[1] = true; + assertEquals(TAKEN, send("START test-set-1/test-1")); + assertEquals(TAKEN, send("END test-set-1/test-1")); + assertEquals("{\"id\":\"test-set-1/test-1\",\"executedLinesByFile\":{}}", second.next()); + assertNull(second.background.poll()); + + assertEquals(TAKEN, send("BACKGROUND")); + shop[2] = true; + assertEquals(TAKEN, send("START test-set-1/test-2")); + assertEquals(background("test-set-1/test-1", "test-set-1/test-2", probes(SHOP, "2")), + second.nextBackground()); + } + } + + @Test + public void withNoReceiverBackgroundIsNotTakenAndAWindowGoesNowhere() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals("no receiver to send it to", IGNORED, send("BACKGROUND")); + assertNotNull(logged("Keploy dedup: BACKGROUND is not taken: no receiver is on ")); + assertEquals("said, not thrown", 0, count("Failed to run Java dedup coverage command")); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertEquals(TAKEN, send("BACKGROUND")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{}}", data.next()); + } + // The receiver is gone: the window's record goes nowhere, and the server goes on. + new File(KeployDedupAgent.dataSocketPath).delete(); + shop[1] = true; + assertEquals(TAKEN, send("START test-set-0/test-2")); + assertFalse(shop[1]); + assertEquals(IGNORED, send("END test-set-0/test-2")); + assertEquals(TAKEN, send("START test-set-0/test-3")); + } + + @Test + public void aBackgroundRecordThatCouldNotBeSentIsCountedInTheNext() throws Exception { + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + File socket = new File(KeployDedupAgent.dataSocketPath); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("BACKGROUND")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{}}", data.next()); + + // The same receiver, which no one may connect to for a while: two + // windows' records are lost, and said once. + for (int test = 2; test <= 3; test++) { + assertTrue(socket.setWritable(false, false) && socket.setReadable(false, false)); + Assume.assumeFalse("root connects to any socket", socket.canWrite()); + shop[1] = true; + assertEquals(TAKEN, send("START test-set-0/test-" + test)); + // The window's record is sent once the START is answered and + // before the next command is read: this one's answer says it was. + assertEquals(IGNORED, send("NO-COMMAND")); + assertTrue(socket.setWritable(true, false) && socket.setReadable(true, false)); + assertEquals(TAKEN, send("END test-set-0/test-" + test)); + assertEquals("{\"id\":\"test-set-0/test-" + test + "\",\"executedLinesByFile\":{}}", + data.next()); + } + assertEquals(1, count("Keploy dedup: the BACKGROUND record of the hits before")); + assertNotNull(logged("Keploy dedup: the BACKGROUND record of the hits before test-set-0/test-2 could" + + " not be sent to ")); + + shop[2] = true; + assertEquals(TAKEN, send("START test-set-0/test-4")); + assertEquals(background("test-set-0/test-3", "test-set-0/test-4", probes(SHOP, "2"), 2), + data.nextBackground()); + assertEquals(TAKEN, send("END test-set-0/test-4")); + assertEquals("{\"id\":\"test-set-0/test-4\",\"executedLinesByFile\":{}}", data.next()); + shop[3] = true; + assertEquals(TAKEN, send("START test-set-0/test-5")); + assertEquals("counted once", background("test-set-0/test-4", "test-set-0/test-5", probes(SHOP, "3")), + data.nextBackground()); + + // Lost again, and the same receiver asks again: it is still told. + assertEquals(TAKEN, send("END test-set-0/test-5")); + assertEquals("{\"id\":\"test-set-0/test-5\",\"executedLinesByFile\":{}}", data.next()); + assertTrue(socket.setWritable(false, false) && socket.setReadable(false, false)); + shop[1] = true; + assertEquals(TAKEN, send("START test-set-0/test-6")); + assertEquals(IGNORED, send("NO-COMMAND")); + assertTrue(socket.setWritable(true, false) && socket.setReadable(true, false)); + assertEquals(TAKEN, send("BACKGROUND")); + assertEquals(TAKEN, send("END test-set-0/test-6")); + assertEquals("{\"id\":\"test-set-0/test-6\",\"executedLinesByFile\":{}}", data.next()); + shop[2] = true; + assertEquals(TAKEN, send("START test-set-0/test-7")); + assertEquals(background("test-set-0/test-6", "test-set-0/test-7", probes(SHOP, "2"), 1), + data.nextBackground()); + + // Lost again, then the receiver is replaced: the new one, once it + // asks, is told of no window lost before it. + assertEquals(TAKEN, send("END test-set-0/test-7")); + assertEquals("{\"id\":\"test-set-0/test-7\",\"executedLinesByFile\":{}}", data.next()); + assertTrue(socket.setWritable(false, false) && socket.setReadable(false, false)); + shop[1] = true; + assertEquals(TAKEN, send("START test-set-0/test-8")); + assertEquals(IGNORED, send("NO-COMMAND")); + assertTrue(socket.setWritable(true, false) && socket.setReadable(true, false)); + assertEquals(TAKEN, send("END test-set-0/test-8")); + assertEquals("{\"id\":\"test-set-0/test-8\",\"executedLinesByFile\":{}}", data.next()); + } finally { + socket.setWritable(true, false); + socket.setReadable(true, false); + } + socket.delete(); + try (DataReceiver next = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertEquals(TAKEN, send("BACKGROUND")); + shop[2] = true; + assertEquals(TAKEN, send("START test-set-1/test-1")); + assertEquals(background("test-set-0/test-8", "test-set-1/test-1", probes(SHOP, "2")), + next.nextBackground()); + } + } + + @Test + public void withTheScanOffEachTestTakesJacocosDumpAndBackgroundIsNotTaken() throws Exception { + System.setProperty("keploy.java.dedup.diagnostics", "true"); + System.setProperty("keploy.java.scan", "Off"); + boolean[] shop = agent.probes(0x1234L, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + shop[0] = true; + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertTrue(logged(" START test-set-0/test-1 took ").contains(" reset=")); + assertFalse("JaCoCo's reset cleared it", shop[0]); + assertEquals(IGNORED, send("BACKGROUND")); + assertNotNull(logged("Keploy dedup: KEPLOY_JAVA_SCAN is off")); + shop[1] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1]}}", + data.next()); + String end = logged(" END test-set-0/test-1 took "); + assertTrue(end, end.contains("dump=") && !end.contains(" scan=")); + assertEquals(0, count("background hits cleared")); + } + } + + @Test + public void anyOtherScanValueKeepsTheScan() throws Exception { + System.setProperty("keploy.java.dedup.diagnostics", "true"); + System.setProperty("keploy.java.scan", "on"); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertTrue(logged(" START test-set-0/test-1 took ").contains(" scan=")); + assertEquals(TAKEN, send("BACKGROUND")); + } + } + @Test public void anEndWhoseScanFailsTakesJacocosDumpFromThenOn() throws Exception { System.setProperty("keploy.java.dedup.diagnostics", "true"); @@ -433,6 +790,23 @@ private int count(String text) { return n; } + // background is the BACKGROUND record of the window from the END of + // after to the START of before, whose probes by class are classes + // (probes(...) joined by ","), with lost when it is more than 0. + private static String background(String after, String before, String classes, int lost) { + return "{\"kind\":\"background\",\"v\":1,\"build\":\"" + BUILD + "\",\"after\":\"" + after + + "\",\"before\":\"" + before + "\",\"executedLinesByFile\":{" + classes + "}" + + (lost > 0 ? ",\"lost\":" + lost : "") + "}"; + } + + private static String background(String after, String before, String classes) { + return background(after, before, classes, 0); + } + + private static String probes(String className, String probes) { + return "\"" + className + "\":[" + probes + "]"; + } + private static String send(String command) throws IOException { try (AFUNIXSocket socket = AFUNIXSocket.newInstance()) { socket.connect(AFUNIXSocketAddress.of(new File(KeployDedupAgent.controlSocketPath)), 3000); @@ -485,12 +859,31 @@ protected Class findClass(String name) throws ClassNotFoundException { private static final class DataReceiver implements Closeable { + private static final AtomicInteger BOUND = new AtomicInteger(); + private final AFUNIXServerSocket server; private final BlockingQueue payloads = new LinkedBlockingQueue<>(); + // The BACKGROUND records, in the order they came. + final BlockingQueue background = new LinkedBlockingQueue<>(); + // The INDEX reports, in the order they came. + final BlockingQueue index = new LinkedBlockingQueue<>(); + // The kind of each record ("index", "background", "test"), in the + // order they came. + final List kinds = new CopyOnWriteArrayList<>(); DataReceiver(String path) throws IOException { server = AFUNIXServerSocket.newInstance(); server.bind(AFUNIXSocketAddress.of(new File(path)), 50); + // The socket file keeps a second name until the row's folder is + // removed, so that a receiver bound anew on the path never gets + // this one's inode back. Deleted and bound again within one tick + // of the file system's clock, it would have the same mtime too, + // and the SDK, which tells receivers apart by the file's inode + // and mtime, would take it for this one: a stated limit, which a + // receiver bound anew for a new run or the next test set, long + // after, never meets. + Files.createLink(new File(path + "." + BOUND.incrementAndGet() + ".held").toPath(), + new File(path).toPath()); Thread thread = new Thread(this::serve, "data-receiver"); thread.setDaemon(true); thread.start(); @@ -502,7 +895,14 @@ private void serve() { socket.setSoTimeout(30000); String payload = new String(readAll(socket.getInputStream()), StandardCharsets.UTF_8); // The INDEX report (ControlSocketTest's rows) is not a test's coverage. - if (!payload.startsWith("{\"kind\":\"index\",")) { + if (payload.startsWith("{\"kind\":\"background\",")) { + kinds.add("background"); + background.add(payload); + } else if (payload.startsWith("{\"kind\":\"index\",")) { + kinds.add("index"); + index.add(payload); + } else { + kinds.add("test"); payloads.add(payload); } } catch (IOException e) { @@ -515,6 +915,10 @@ String next() throws InterruptedException { return payloads.poll(30, TimeUnit.SECONDS); } + String nextBackground() throws InterruptedException { + return background.poll(30, TimeUnit.SECONDS); + } + @Override public void close() throws IOException { server.close(); From 9d04275f70676492e048408ef52780819c38d99f Mon Sep 17 00:00:00 2001 From: slayerjain Date: Fri, 20 Nov 2026 04:57:13 +0530 Subject: [PATCH 05/14] feat(dedup): name classes JaCoCo saw changed, write ids as 16 hex Keploy reads a test's probes against the classes the SDK indexed: the class files it read from the app's dirs and jars. When a class loader that weaves the bytes it defines (load-time weaving), or an agent listed before JaCoCo's that does not ask to retransform, changed a class before JaCoCo saw it, or the JVM loaded a multi-release jar's versioned copy, JaCoCo's probes are those of other bytes, and their indices do not belong to the indexed class. Only the SDK holds both ids: JaCoCo's (ExecutionData's id) and the CRC64 of the indexed bytes. A test's record now names each such class after its probes: {"id":"","executedLinesByFile":{class:[probe...]}, "classMismatch":[class...]} The key is left out when there is none, so a test that ran one copy of each class, the one the SDK indexed, sends the same record as before, and receivers that do not know the key ignore it (Keploy's Go receivers decode it with encoding/json). The CRC64 of a class's indexed bytes is worked out, and kept, the first time a record reports the class. A BACKGROUND record names such classes in a classMismatch of its own, left out when empty: Keploy takes background out of a test's hits probe by probe, so it must know which probes are not the indexed class's. Of two copies of one class (loaded twice, from other bytes) that a test ran, the record now reports the copy the SDK indexed, else the copy the coverage manifest names, else the lowest id compared as an unsigned number (before: the manifest's, else the lowest id compared as a signed long). A BACKGROUND record chooses alike. JaCoCo's dump path (KEPLOY_JAVA_SCAN=off, or JaCoCo reached over TCP) now goes through the same choice: before, of two copies it kept whichever the dump listed last, and added the first to the manifest. Every class id the SDK writes (the coverage manifest's id) is now 16 lowercase hex digits, zero-padded, as the INDEX record's build id already hashes them. Older SDKs wrote Long.toHexString's unpadded form, so readers parse the id as a hex number. Rows: ids as 16 hex digits; the manifest's id over the TCP transport, with classMismatch in its COV; a class JaCoCo saw other bytes of named, and none for a test that ran only indexed classes; of two copies, the indexed one over the manifest's and over a lower id, the manifest's over a lower id (also when its id has fewer than 16 hex digits), the lower id compared unsigned; JaCoCo's dump path choosing alike and leaving library classes out; a BACKGROUND record naming a copy JaCoCo saw other bytes of, and none for the indexed copy; the record's shape with and without the key. The Java agent smoke test now fails a record that names any class when the app ran only its own, and runs a test in which only another copy of a class runs (other source, its own class loader), whose record must name that class alone: on the in-place read with the JaCoCo agent the command attaches and the one keploy-sdk.jar starts, and on JaCoCo's dump (KEPLOY_JAVA_SCAN=off). Signed-off-by: slayerjain --- README.md | 4 +- .../io/keploy/dedup/CoverageReporter.java | 7 +- .../dedup/CoverageReporterSelfTest.java | 4 +- .../java/io/keploy/dedup/IndexReport.java | 19 +- .../io/keploy/dedup/KeployDedupAgent.java | 268 ++++++++++------- .../main/java/io/keploy/dedup/ProbeScan.java | 35 ++- .../keploy/dedup/CoverageTcpClientTest.java | 46 +++ .../io/keploy/dedup/DedupWireFormatTest.java | 13 + .../io/keploy/dedup/InProcessScanTest.java | 276 ++++++++++++++---- .../java/io/keploy/dedup/IndexReportTest.java | 9 + scripts/smoke-javaagent.sh | 127 ++++++-- 11 files changed, 612 insertions(+), 196 deletions(-) diff --git a/README.md b/README.md index 02c7ece2..bda78c72 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,13 @@ Keploy Enterprise drives dynamic dedup per testcase. Coverage is collected at per-testcase granularity, not process granularity. +Since 2.2.0 a testcase's record also names, in `"classMismatch":[className, ...]` after its probes, each class whose probes come from bytes other than the ones the agent indexed: a class loader that weaves the bytes it defines (load-time weaving) or an agent listed before JaCoCo's that does not ask to retransform changed the class before JaCoCo saw it, or the JVM loaded a multi-release jar's versioned copy. JaCoCo's id for such a class is not the CRC64 of the indexed bytes, so its probe indices do not belong to the indexed class, and Keploy must not read them against it. The key is left out when there is no such class. Of two copies of one class (loaded twice, from different bytes) that a testcase ran, the record reports the copy the agent indexed, else the one the coverage manifest already names, else the lowest class id compared as an unsigned number; a BACKGROUND record chooses alike, and names such classes in a `classMismatch` of its own. Every class id the agent writes (the coverage manifest's `id`) is 16 lowercase hex digits since 2.2.0; older agents wrote it unpadded, so read it as a hex number. + With the JaCoCo agent in the application's JVM (the usual case), steps 2 and 5 are one pass over the probe arrays of the application's classes alone, split across up to four threads (half the cores): a probe is cleared only once it was read as hit, so a hit made while the pass runs is reported by that pass or the next one, never lost. Library classes are neither read nor cleared. If JaCoCo's runtime data cannot be read that way, or the pass fails once, the agent uses JaCoCo's own reset and dump for the rest of the run, as it does when JaCoCo is reached over TCP. Before the first testcase's coverage, the agent sends Enterprise one INDEX record on `/tmp/coverage_data.sock`: a JSON object starting with `{"kind":"index",` that says how it found the application's own classes (`source`: `override` when the dirs and jars in `KEPLOY_JAVA_CLASS_DIRS` held them, `derived` from the launch or from `target/classes` or `build/classes/java/main` under the working directory, `whole-jar` or `classpath` when it could not tell them from the libraries, `none`), what it found them from in words (`launch`), the dirs and jars it read them from (`roots`, at most 64, and `rootCount`), the package roots it kept (`packages`), how many classes it indexed, the build id of those classes (`jv1-` and 32 hex), which JaCoCo agent measures them (`jacoco`: `embedded`, the one keploy-sdk.jar starts, `attached` by the command, `none`, or `unchecked` when the agent was not started as a `-javaagent`), that embedded agent's `includes=`, and two versions as their jars' manifests name them, or `""` when not known: keploy-sdk.jar's (`sdkVersion`) and that of the JaCoCo agent that measures the classes, embedded or attached (`jacocoVersion`). It carries no class list. It is sent as the agent starts when that path's socket file is there (Enterprise's receiver makes it), and otherwise, or when that send fails, right before the first testcase's coverage; a receiver Enterprise binds anew on that path gets it again before its first record. On the TCP transport used in Kubernetes no INDEX record is sent. -Since 2.2.0 Keploy can also ask for that background, to keep it out of what it compares: it sends the line `BACKGROUND` on `/tmp/coverage_control.sock`. The agent answers `ACK` when it reads JaCoCo's probes in place and a receiver is on `/tmp/coverage_data.sock`, and `IGNORED` otherwise (`KEPLOY_JAVA_SCAN=off`, JaCoCo reached over TCP, the in-place read failed, or no receiver), as an older agent answers a line it does not know. The ask holds for this run of the application and that receiver only: an application started again (native `keploy test` starts it again for each test set) and a receiver Enterprise binds anew on that path (the next docker-compose test set) get no record until Keploy asks again, so Keploy asks after each INDEX record, which comes once per start of the application and once per new receiver. From then on, each `START` that cleared hits made since the previous testcase's `END` sends one BACKGROUND record on `/tmp/coverage_data.sock`, right after that `START` is answered (after the INDEX record, when that receiver does not have it yet): `{"kind":"background","v":1,"build":"","after":"","before":"","executedLinesByFile":{className:[probeIdx]}}`, under the key and in the shape of a testcase's record, plus `"lost":N` when the records of the N windows before it could not be made or sent. A window with no hits sends nothing. What ran before the first testcase (the application's start, the warmup) is not background, and neither is what ran after an `END` that was not taken or a `START` whose `END` never came (a testcase's own hits): none of it is sent. A test's own work that goes on after its `END` (an async task it started) lands in the next window, and so is reported as background. A Keploy that never asks gets no record, short of a new receiver that replaces the one that asked within the same instant (the agent tells receivers apart by the socket file's inode and modification time). On the TCP transport used in Kubernetes there is no BACKGROUND. +Since 2.2.0 Keploy can also ask for that background, to keep it out of what it compares: it sends the line `BACKGROUND` on `/tmp/coverage_control.sock`. The agent answers `ACK` when it reads JaCoCo's probes in place and a receiver is on `/tmp/coverage_data.sock`, and `IGNORED` otherwise (`KEPLOY_JAVA_SCAN=off`, JaCoCo reached over TCP, the in-place read failed, or no receiver), as an older agent answers a line it does not know. The ask holds for this run of the application and that receiver only: an application started again (native `keploy test` starts it again for each test set) and a receiver Enterprise binds anew on that path (the next docker-compose test set) get no record until Keploy asks again, so Keploy asks after each INDEX record, which comes once per start of the application and once per new receiver. From then on, each `START` that cleared hits made since the previous testcase's `END` sends one BACKGROUND record on `/tmp/coverage_data.sock`, right after that `START` is answered (after the INDEX record, when that receiver does not have it yet): `{"kind":"background","v":1,"build":"","after":"","before":"","executedLinesByFile":{className:[probeIdx]}}`, under the key and in the shape of a testcase's record (with its `classMismatch`, left out when empty), plus `"lost":N` when the records of the N windows before it could not be made or sent. A window with no hits sends nothing. What ran before the first testcase (the application's start, the warmup) is not background, and neither is what ran after an `END` that was not taken or a `START` whose `END` never came (a testcase's own hits): none of it is sent. A test's own work that goes on after its `END` (an async task it started) lands in the next window, and so is reported as background. A Keploy that never asks gets no record, short of a new receiver that replaces the one that asked within the same instant (the agent tells receivers apart by the socket file's inode and modification time). On the TCP transport used in Kubernetes there is no BACKGROUND. The agent binds `/tmp/coverage_control.sock` before the application's `main` runs, so the socket is listening before the application can answer a health check. It answers `ACK` to each command it took, and `IGNORED` to an `END` that no `START` for that testcase came before: nothing is published for it, and Enterprise keeps that testcase out of the comparison. On the TCP transport used in Kubernetes the same `END` is answered with `IGNORED ` and then `ACK`. diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/CoverageReporter.java b/keploy-sdk/src/main/java/io/keploy/dedup/CoverageReporter.java index 8cad2b46..27cd6564 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/CoverageReporter.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/CoverageReporter.java @@ -32,8 +32,9 @@ * auto-replay cycles). Reuses the exact {@code dedupFingerprints} probe * sets already persisted for cross-cycle dedup; *

  • manifest — {@code {vmClassName -> {id, probeCount}}}: the JaCoCo - * class id (CRC64, as an unsigned-hex string) and total probe count per - * class. These are build-constant and captured by the SDK from the live + * class id (CRC64, as unsigned hex: 16 digits from SDK 2.2.0, unpadded + * before) and total probe count per class. These are build-constant and + * captured by the SDK from the live * {@code ExecutionData} at record time ({@code getId()} / * {@code getProbes().length}), so the reporter never has to touch JaCoCo * internal APIs to derive them.
  • @@ -67,7 +68,7 @@ private CoverageReporter() { /** JaCoCo class id + total probe count for one class (build-constant). */ static final class ClassMeta { - String id; // CRC64 class id, unsigned-hex (from ExecutionData.getId()) + String id; // CRC64 class id, unsigned hex, padded or not (from ExecutionData.getId()) int probeCount; // ExecutionData.getProbes().length } diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/CoverageReporterSelfTest.java b/keploy-sdk/src/main/java/io/keploy/dedup/CoverageReporterSelfTest.java index 7f51d19b..8afa6968 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/CoverageReporterSelfTest.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/CoverageReporterSelfTest.java @@ -71,8 +71,8 @@ public static void main(String[] args) throws Exception { firedProbes++; } } - // manifest.id = data.getId() (round-tripped through unsigned-hex, like the real pipeline) - long id = Long.parseUnsignedLong(Long.toHexString(data.getId()), 16); + // manifest.id = data.getId() (round-tripped through 16-hex, like the real pipeline) + long id = Long.parseUnsignedLong(IndexReport.hexId(data.getId()), 16); rebuilt.put(new ExecutionData(id, data.getName(), copy)); } long[] rebuiltCov = analyze(rebuilt, classesPath); diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/IndexReport.java b/keploy-sdk/src/main/java/io/keploy/dedup/IndexReport.java index 71ac0cd0..84095e49 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/IndexReport.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/IndexReport.java @@ -129,6 +129,19 @@ static long classId(byte[] bytes) { return CRC64.classId(bytes); } + /** + * hexId writes a class id as every id the SDK sends is written: the id + * as an unsigned number in 16 lowercase hex digits, zero-padded. + */ + static String hexId(long id) { + String hex = Long.toHexString(id); + StringBuilder padded = new StringBuilder(16); + for (int pad = hex.length(); pad < 16; pad++) { + padded.append('0'); + } + return padded.append(hex).toString(); + } + /** * buildId is the build's id from its classes' JaCoCo ids: "jv1-" and the * first 32 hex of the sha256 over the ids sorted as unsigned numbers, @@ -154,11 +167,7 @@ static String buildId(long[] ids) { if (joined.length() > 0) { joined.append('\n'); } - String hex = Long.toHexString(sorted[i] ^ Long.MIN_VALUE); - for (int pad = hex.length(); pad < 16; pad++) { - joined.append('0'); - } - joined.append(hex); + joined.append(hexId(sorted[i] ^ Long.MIN_VALUE)); } byte[] sum; try { diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java index c368d052..19cdcbdb 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java @@ -38,7 +38,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; -import java.util.HashSet; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -74,7 +74,11 @@ public final class KeployDedupAgent { // GSON + DedupPayload), so tests can lock the wire shape. The historical // "executedLinesByFile" key now carries {vmClassName -> [probeIdx]}. static String toDedupJson(String id, Map> probesByClass) { - return GSON.toJson(new DedupPayload(id, probesByClass)); + return toDedupJson(id, probesByClass, Collections.emptyList()); + } + + static String toDedupJson(String id, Map> probesByClass, List classMismatch) { + return GSON.toJson(new DedupPayload(id, new TestCoverage(probesByClass, classMismatch))); } // encodeBytecodeFrame builds the bytecode frame the SDK writes to the @@ -454,10 +458,13 @@ private static String normalizePath(String path) { // Build-constant coverage metadata for one class, serialized into the // manifest the offline CoverageReporter consumes. id = CRC64 class id - // (unsigned-hex), probeCount = JaCoCo probe array length. + // (IndexReport.hexId), probeCount = JaCoCo probe array length. classId + // is that id as JaCoCo has it, for chosenCopy to compare with a copy's + // id as a number; transient, so it is never sent. private static final class ManifestEntry { private String id; private int probeCount; + private transient long classId; } // fileKey is the file's identity (its device and inode on Linux and @@ -595,10 +602,11 @@ private static final class CommandServer implements CoverageWorker { private volatile Object socketFileKey; private String activeTestId = ""; // The id of the test whose END last sent its coverage, set once that - // record is sent and cleared by every START and every other END: what - // the next START clears then ran between two tests. BACKGROUND and - // lines that are no command leave it (a receiver may ask between an - // END and a START). Like activeTestId, only the worker thread uses it. + // record is sent and cleared by every START, and by every END until + // its record is sent: what the next START clears then ran between + // two tests. BACKGROUND and lines that are no command leave it (a + // receiver may ask between an END and a START). Like activeTestId, + // only the worker thread uses it. private String lastEndedTestId; // The between-tests hits the last START cleared, for a receiver that // asked for them: made into a record and sent once that START's @@ -783,11 +791,11 @@ private boolean run(CoverageCommand command, Phases phases) { } try { - Map> executedProbesByClass = collector.capture(phases); - if (executedProbesByClass.isEmpty()) { + TestCoverage coverage = collector.capture(phases); + if (coverage.probesByClass.isEmpty()) { log(Level.FINE, "No Java coverage collected for " + command.testId, null); } - publisher.publish(command.testId, executedProbesByClass, phases); + publisher.publish(command.testId, coverage, phases); lastEndedTestId = command.testId; return true; } catch (Exception e) { @@ -1002,9 +1010,9 @@ private void dispatch(CoverageCommand command, OutputStream out, Phases phases) // collector reads COV and ACK off the same line-oriented stream, // so exactly one COV must precede every ACK — a missing COV on an // exception would shift the collector onto the next test's frames. - Map> executedProbesByClass; + TestCoverage coverage; try { - executedProbesByClass = collector.capture(phases); + coverage = collector.capture(phases); } catch (Exception e) { // Not taken, as on the unix socket: an empty COV is // the frame of a test that ran no line of the app. @@ -1014,14 +1022,14 @@ private void dispatch(CoverageCommand command, OutputStream out, Phases phases) writeLine(out, "ACK"); return; } - if (executedProbesByClass.isEmpty()) { + if (coverage.probesByClass.isEmpty()) { log(Level.FINE, "No Java coverage collected for " + command.testId, null); } try { // Always emit exactly one COV before ACK — even when empty — // so the line-oriented collector stays in lockstep (the unix // transport likewise always publishes). - String cov = "COV " + GSON.toJson(new DedupPayload(command.testId, executedProbesByClass)); + String cov = "COV " + GSON.toJson(new DedupPayload(command.testId, coverage)); phases.mark("json"); writeLine(out, cov); phases.mark("send"); @@ -1111,8 +1119,14 @@ private static final class CoverageCollector { // when the SDK was not started as a -javaagent. private final Instrumentation instrumentation; private final AtomicBoolean warmed = new AtomicBoolean(false); - // The VM names of the app's classes; see indexedClassNames. + // The VM names of the app's classes, and the classes by those + // names; see indexedClassNames. private volatile Set appClassNames; + private volatile Map appClassesByName; + // The JaCoCo ids of the indexed classes looked up so far; see + // indexedId. + private final java.util.concurrent.ConcurrentMap indexedIds = + new java.util.concurrent.ConcurrentHashMap<>(); // Coverage manifest ({vmClassName -> {classId, probeCount}}) accumulated // from the SAME per-test capture() path that produces dedup fingerprints — // so its class matching is proven correct (a separate post-warmup dump @@ -1335,7 +1349,7 @@ private boolean isWarmupDisabled() { "keploy.java.dedup.warmup.disabled", "")); } - private Map> capture(Phases phases) throws IOException { + private TestCoverage capture(Phases phases) throws IOException { ProbeScan scan = probeScan(); if (scan != null) { List hits = null; @@ -1362,7 +1376,7 @@ private Map> capture(Phases phases) throws IOException { if (diagnosticsEnabled()) { diagnostic("JaCoCo dump returned 0 bytes"); } - return Collections.emptyMap(); + return TestCoverage.NONE; } ExecFileLoader loader = new ExecFileLoader(); @@ -1385,60 +1399,38 @@ private Map> capture(Phases phases) throws IOException { if (diagnosticsEnabled()) { diagnostic("coverage index has no app classes"); } - return Collections.emptyMap(); + return TestCoverage.NONE; } - Map> raw = new LinkedHashMap<>(); + // The app's classes the dump holds hits of, sorted as the scan + // returns them, so that both choose of two copies of one class + // alike. + List hits = new ArrayList<>(); for (ExecutionData executionData : executionDataStore.getContents()) { - if (!executionData.hasHits()) { - continue; - } - if (!appClasses.contains(executionData.getName())) { - continue; - } - boolean[] probes = executionData.getProbes(); - // Record this class's build-constant (classId, probeCount) for the - // coverage manifest. Same matching as the fingerprint below, so the - // manifest can never miss a class that appears in the union. - final int probeCount = probes.length; - final long classId = executionData.getId(); - liveManifest.computeIfAbsent(executionData.getName(), k -> { - ManifestEntry m = new ManifestEntry(); - m.id = Long.toHexString(classId); - m.probeCount = probeCount; - return m; - }); - Set fired = new LinkedHashSet<>(); - for (int i = 0; i < probes.length; i++) { - if (probes[i]) { - fired.add(i); - } + if (executionData.hasHits() && appClasses.contains(executionData.getName())) { + hits.add(ProbeScan.Hit.of(executionData.getName(), executionData.getId(), + executionData.getProbes())); } - if (!fired.isEmpty()) { - raw.put(executionData.getName(), fired); - } - } - - Map> sorted = toSortedMap(raw); - phases.mark("filter"); - - if (diagnosticsEnabled()) { - diagnostic("classesWithProbes=" + raw.size() - + ", sampleClasses=" + summarizeStrings(raw.keySet(), 5)); } - return sorted; - } - - // probesByClass turns the scan's hits (sorted by name, then id) into - // the test's probes by class, and adds each class to the coverage - // manifest, as the dump's path does. Of two classes of one name - // (loaded twice, from different bytes) it reports the one whose id - // the manifest holds when that one was hit, else the lowest id. In - // that last case, when the manifest already names the other copy, - // the probes sent are not those of the class the manifest names, as - // with JaCoCo's dump: a test's record names a class, not its id. - private Map> probesByClass(List hits, Phases phases) { + Collections.sort(hits, ProbeScan.HITS_BY_NAME_THEN_ID); + return probesByClass(hits, phases); + } + + // probesByClass turns a test's hits (sorted by name, then id) into + // its probes by class, and adds each class to the coverage manifest. + // Of two classes of one name (loaded twice, from different bytes) + // it reports chosenCopy's. In its first and last cases, when the + // manifest already names another copy, the probes sent are not those + // of the class the manifest names: a test's record names a class, + // not its id. The legacy CoverageReporter then credits nothing when + // the manifest names a copy the index did not read, and credits the + // other copy's probe indices to the indexed class when the manifest + // names the indexed copy. Each class whose reported copy is not the + // class the index read is named in the record's classMismatch, so + // that Keploy does not read its probes against the indexed bytes. + private TestCoverage probesByClass(List hits, Phases phases) { Map> byClass = new LinkedHashMap<>(); + List mismatch = new ArrayList<>(); for (int i = 0; i < hits.size(); ) { String name = hits.get(i).name; int end = copiesEnd(hits, i); @@ -1448,32 +1440,48 @@ private Map> probesByClass(List hits, Phase final long classId = chosen.id; liveManifest.computeIfAbsent(name, k -> { ManifestEntry m = new ManifestEntry(); - m.id = Long.toHexString(classId); + m.id = IndexReport.hexId(classId); + m.classId = classId; m.probeCount = probeCount; return m; }); byClass.put(name, probeList(chosen)); + if (!isIndexed(name, classId)) { + mismatch.add(name); + } } phases.mark("filter"); if (diagnosticsEnabled()) { diagnostic("classesWithProbes=" + byClass.size() + ", sampleClasses=" + summarizeStrings(byClass.keySet(), 5)); + if (!mismatch.isEmpty()) { + diagnostic("classMismatch=" + mismatch.size() + + ", sampleClasses=" + summarizeStrings(mismatch, 5)); + } } - return byClass; + return new TestCoverage(byClass, mismatch); } // backgroundProbes turns the hits a START cleared into probes by // class, of two copies of one class the one probesByClass would - // report, and adds none of them to the coverage manifest: no test - // ran them. - private Map> backgroundProbes(List hits) { + // report, and names in classMismatch, as probesByClass does, each + // whose reported copy is not the class the index read: Keploy takes + // background out of a test's hits probe by probe, so it must know + // which are not the indexed class's. It adds none of them to the + // coverage manifest: no test ran them. + private TestCoverage backgroundProbes(List hits) { Map> byClass = new LinkedHashMap<>(); + List mismatch = new ArrayList<>(); for (int i = 0; i < hits.size(); ) { int end = copiesEnd(hits, i); - byClass.put(hits.get(i).name, probeList(chosenCopy(hits, i, end))); + ProbeScan.Hit chosen = chosenCopy(hits, i, end); i = end; + byClass.put(chosen.name, probeList(chosen)); + if (!isIndexed(chosen.name, chosen.id)) { + mismatch.add(chosen.name); + } } - return byClass; + return new TestCoverage(byClass, mismatch); } // copiesEnd returns the end of the run of hits, sorted by name, that @@ -1489,18 +1497,49 @@ private int copiesEnd(List hits, int from) { } // chosenCopy picks, of the copies hits[from, to) of one class, the - // one whose id the coverage manifest holds, else the first (the - // lowest id). + // class the index read (its id is the CRC64 of the indexed bytes), + // else the one whose id the coverage manifest holds, else the first + // (the lowest id, compared as unsigned numbers). private ProbeScan.Hit chosenCopy(List hits, int from, int to) { - ManifestEntry known = liveManifest.get(hits.get(from).name); - if (known != null) { - for (int k = from; k < to; k++) { - if (Long.toHexString(hits.get(k).id).equals(known.id)) { - return hits.get(k); - } + String name = hits.get(from).name; + long indexed = indexedId(name); + ManifestEntry known = liveManifest.get(name); + ProbeScan.Hit named = null; + for (int k = from; k < to; k++) { + ProbeScan.Hit hit = hits.get(k); + if (hit.id == indexed) { + return hit; + } + // At most one copy has the manifest's id: JaCoCo holds one + // class per id. + if (known != null && known.classId == hit.id) { + named = hit; } } - return hits.get(from); + return named != null ? named : hits.get(from); + } + + // isIndexed says whether the class JaCoCo measured under this name + // and id is the class the index read: false for one another agent + // changed before JaCoCo saw it, and for a multi-release jar's + // META-INF/versions copy the JVM loaded in place of the indexed one. + private boolean isIndexed(String name, long id) { + return indexedId(name) == id; + } + + // indexedId is the JaCoCo id of the class the index read under this + // VM name, the CRC64 of its bytes, worked out the first time a hit + // of the class is reported: not every indexed class at the first + // END. Every hit reported is of an indexed class (the scan and the + // dump keep only those), so the index holds the name. + private long indexedId(String name) { + Long id = indexedIds.get(name); + if (id == null) { + indexedClassNames(); + id = IndexReport.classId(appClassesByName.get(name).bytes); + indexedIds.put(name, id); + } + return id; } private List probeList(ProbeScan.Hit hit) { @@ -1520,10 +1559,14 @@ private Set indexedClassNames() { Set names = appClassNames; if (names == null) { List entries = coverageIndex.entries(); - names = new HashSet<>(entries.size() * 2); + Map byName = new HashMap<>(entries.size() * 2); for (ClassEntry entry : entries) { - names.add(entry.className); + byName.putIfAbsent(entry.className, entry); } + // appClassesByName first: a thread that sees appClassNames + // set sees it too. + appClassesByName = byName; + names = byName.keySet(); appClassNames = names; } return names; @@ -1540,19 +1583,6 @@ private String summarizeStrings(Iterable values, int limit) { return sample.toString(); } - private Map> toSortedMap(Map> raw) { - List files = new ArrayList<>(raw.keySet()); - Collections.sort(files); - - Map> sorted = new LinkedHashMap<>(); - for (String file : files) { - List lines = new ArrayList<>(raw.get(file)); - Collections.sort(lines); - sorted.put(file, lines); - } - return sorted; - } - // pollBytecodeFrame returns a "CLASSES " // line to send over the collector channel (:36340) IF the coverage manifest // has grown since the last send, else null. The raw-TCP collector channel @@ -2286,8 +2316,7 @@ synchronized boolean backgroundAsked() { // none goes to it until it asks. A record that could not be made or // sent is not tried again: the next one that goes through counts it // in "lost". It never fails the caller. - synchronized void sendBackground(String after, String before, - Supplier>> classes) { + synchronized void sendBackground(String after, String before, Supplier classes) { Object key = receiverKey(socketFile); if (key == null || !key.equals(backgroundAskedBy)) { backgroundAskedBy = null; @@ -2319,9 +2348,8 @@ synchronized void sendBackground(String after, String before, // publish sends the test's coverage, after the INDEX report when the // receiver does not have it yet: the report's time counts in "send". - private void publish(String testId, Map> executedProbesByClass, Phases phases) - throws IOException { - byte[] payload = GSON.toJson(new DedupPayload(testId, executedProbesByClass)) + private void publish(String testId, TestCoverage coverage, Phases phases) throws IOException { + byte[] payload = GSON.toJson(new DedupPayload(testId, coverage)) .getBytes(StandardCharsets.UTF_8); phases.mark("json"); sendIndex(); @@ -2350,15 +2378,40 @@ private void send(byte[] payload) throws IOException { // this exact name (enterprise dedup #2188, k8s-proxy #574), which treat the // value opaquely as key -> int-set. Renaming the JSON key is a coordinated // cross-repo break and must land in lockstep on all three sides. + // + // classMismatch names, in the order of executedLinesByFile, each class + // there whose JaCoCo id is not the id of the bytes the index read: its + // probes are not those of the indexed class. Left out when there is + // none, so that the record is the same as before for every other test. + // Receivers that do not know the key ignore it. private static final class DedupPayload { private final String id; // Probes-by-class (see class note); serialized under the historical key. private final Map> executedLinesByFile; + private final List classMismatch; - private DedupPayload(String id, Map> executedProbesByClass) { + private DedupPayload(String id, TestCoverage coverage) { this.id = id; - this.executedLinesByFile = executedProbesByClass; + this.executedLinesByFile = coverage.probesByClass; + this.classMismatch = coverage.classMismatch.isEmpty() ? null : coverage.classMismatch; + } + } + + // TestCoverage is a test's coverage as its END read it: the probes hit + // by class, and the classes among them that are not the class the index + // read. + private static final class TestCoverage { + + static final TestCoverage NONE = new TestCoverage(Collections.>emptyMap(), + Collections.emptyList()); + + final Map> probesByClass; + final List classMismatch; + + TestCoverage(Map> probesByClass, List classMismatch) { + this.probesByClass = probesByClass; + this.classMismatch = classMismatch; } } @@ -2384,7 +2437,8 @@ private BackgroundWindow(String after, String before, List cleare // receiver that asked with BACKGROUND, and only for a window with hits. // build is the INDEX report's. executedLinesByFile is {vmClassName -> // [probeIdx]} under a test's record's key and in its shape (DedupPayload), - // names in order and probes ascending, so that Keploy reads both alike. + // names in order and probes ascending, and classMismatch is as in a + // test's record, left out when empty, so that Keploy reads both alike. // lost, when set, counts the windows before this one whose record could // not be made or sent. static final class BackgroundRecord { @@ -2395,13 +2449,15 @@ static final class BackgroundRecord { final String after; final String before; final Map> executedLinesByFile; + final List classMismatch; Integer lost; - BackgroundRecord(String build, String after, String before, Map> classes) { + BackgroundRecord(String build, String after, String before, TestCoverage coverage) { this.build = build; this.after = after; this.before = before; - this.executedLinesByFile = classes; + this.executedLinesByFile = coverage.probesByClass; + this.classMismatch = coverage.classMismatch.isEmpty() ? null : coverage.classMismatch; } } diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/ProbeScan.java b/keploy-sdk/src/main/java/io/keploy/dedup/ProbeScan.java index ed6c0ad8..f5be4218 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/ProbeScan.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/ProbeScan.java @@ -68,8 +68,35 @@ private Hit(String name, long id, int probeCount, int[] probes) { this.probeCount = probeCount; this.probes = probes; } + + // of is the Hit of a class in JaCoCo's dump: the probes set in its + // probe array. + static Hit of(String name, long id, boolean[] probes) { + int count = 0; + for (boolean probe : probes) { + if (probe) { + count++; + } + } + int[] hit = new int[count]; + for (int i = 0, k = 0; k < count; i++) { + if (probes[i]) { + hit[k++] = i; + } + } + return new Hit(name, id, probes.length, hit); + } } + // HITS_BY_NAME_THEN_ID sorts hits as pass returns them. + static final Comparator HITS_BY_NAME_THEN_ID = new Comparator() { + @Override + public int compare(Hit a, Hit b) { + int byName = a.name.compareTo(b.name); + return byName != 0 ? byName : Long.compareUnsigned(a.id, b.id); + } + }; + // One of the app's classes as JaCoCo holds it. private static final class Slot { @@ -88,7 +115,7 @@ private Slot(String name, long id, boolean[] probes) { @Override public int compare(Slot a, Slot b) { int byName = a.name.compareTo(b.name); - return byName != 0 ? byName : Long.compare(a.id, b.id); + return byName != 0 ? byName : Long.compareUnsigned(a.id, b.id); } }; @@ -113,8 +140,8 @@ public int compare(Slot a, Slot b) { // The store's size when refresh last looked at it: classes are only // ever added to it. private int known; - // The app's classes JaCoCo has seen, sorted by name then id, and their - // probe arrays at the same index. + // The app's classes JaCoCo has seen, sorted by name then id (compared + // as unsigned numbers), and their probe arrays at the same index. private Slot[] slots = new Slot[0]; private boolean[][] arrays = new boolean[0][]; private int maxProbes; @@ -192,7 +219,7 @@ static final class LostHitsException extends IllegalStateException { /** * pass returns the app's classes that were hit since the last pass, sorted - * by name then id, and clears those hits. app is the VM names of the app's + * by name then id (compared as unsigned numbers), and clears those hits. app is the VM names of the app's * classes; it must be the same set on every call, as a class is looked up * in it once, when JaCoCo first holds it. * diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/CoverageTcpClientTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/CoverageTcpClientTest.java index 1d48f5f4..6e9c3324 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/CoverageTcpClientTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/CoverageTcpClientTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertTrue; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; @@ -11,9 +12,14 @@ import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Base64; +import org.jacoco.core.data.ExecutionData; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; /** * The TCP transport, with the test as the collector the SDK dials (k8s). A @@ -26,6 +32,11 @@ public class CoverageTcpClientTest { private static final String ENDPOINT_PROPERTY = "keploy.coverage.endpoint"; private static final String JACOCO_PORT_PROPERTY = "keploy.jacoco.port"; private static final String WARMUP_DISABLED_PROPERTY = "keploy.java.dedup.warmup.disabled"; + private static final String CLASS_DIRS_PROPERTY = "keploy.java.class.dirs"; + private static final String BUILD_TAG_PROPERTY = "keploy.build.tag"; + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); private ServerSocket collector; private FakeJacoco jacoco; @@ -45,6 +56,8 @@ public void tearDown() throws IOException { System.clearProperty(ENDPOINT_PROPERTY); System.clearProperty(JACOCO_PORT_PROPERTY); System.clearProperty(WARMUP_DISABLED_PROPERTY); + System.clearProperty(CLASS_DIRS_PROPERTY); + System.clearProperty(BUILD_TAG_PROPERTY); collector.close(); if (jacoco != null) { jacoco.close(); @@ -164,6 +177,39 @@ public void aStartThatThrowsNamesTheTestThenAnswersAckAndTheConnectionGoesOn() t } } + @Test + public void theManifestWritesAClassIdAs16HexDigitsAndTheCovNamesAClassJacocoSawOtherBytesOf() + throws Exception { + File shop = new File(tmp.newFolder("classes"), "com/acme/Shop.class"); + shop.getParentFile().mkdirs(); + Files.write(shop.toPath(), new byte[] {1}); + System.setProperty(CLASS_DIRS_PROPERTY, shop.getParentFile().getParentFile().getParent()); + System.setProperty(BUILD_TAG_PROPERTY, "tag"); + jacoco = new FakeJacoco(); + System.setProperty(JACOCO_PORT_PROPERTY, String.valueOf(jacoco.server.getLocalPort())); + // JaCoCo saw other bytes of Shop than the index reads: its id is not + // theirs, and has fewer than 16 hex digits. + jacoco.hit = new ExecutionData(0x99L, "com/acme/Shop", new boolean[] {true, false}); + assertTrue(KeployDedupAgent.start()); + try (Socket sdk = collector.accept()) { + sdk.setSoTimeout(30000); + OutputStream out = sdk.getOutputStream(); + BufferedReader in = new BufferedReader( + new InputStreamReader(sdk.getInputStream(), StandardCharsets.UTF_8)); + + send(out, "START test-set-0/test-1"); + assertEquals("ACK", in.readLine()); + send(out, "END test-set-0/test-1"); + assertEquals("COV {\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"com/acme/Shop\":[0]}," + + "\"classMismatch\":[\"com/acme/Shop\"]}", in.readLine()); + String classes = in.readLine(); + assertTrue(classes, classes.startsWith("CLASSES ")); + String manifest = new String(Base64.getDecoder().decode(classes.split(" ")[2]), StandardCharsets.UTF_8); + assertEquals("{\"com/acme/Shop\":{\"id\":\"0000000000000099\",\"probeCount\":2}}", manifest); + assertEquals("ACK", in.readLine()); + } + } + private static void send(OutputStream out, String command) throws IOException { out.write((command + "\n").getBytes(StandardCharsets.UTF_8)); out.flush(); diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/DedupWireFormatTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/DedupWireFormatTest.java index 6ce49dfd..c44f1adc 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/DedupWireFormatTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/DedupWireFormatTest.java @@ -44,6 +44,19 @@ public void covPayloadCarriesIdAndProbesUnderHistoricalKey() { assertTrue("probe indices for Bar", json.contains("[0,1,5]")); } + @Test + public void covPayloadNamesTheMismatchedClassesAfterTheProbesAndOnlyWhenThereAreSome() { + Map> probes = new LinkedHashMap<>(); + probes.put("com/foo/Bar", Arrays.asList(0, 1)); + probes.put("com/foo/Baz", Collections.singletonList(2)); + + assertEquals("{\"id\":\"t\",\"executedLinesByFile\":{\"com/foo/Bar\":[0,1],\"com/foo/Baz\":[2]}," + + "\"classMismatch\":[\"com/foo/Baz\"]}", + KeployDedupAgent.toDedupJson("t", probes, Collections.singletonList("com/foo/Baz"))); + assertEquals("{\"id\":\"t\",\"executedLinesByFile\":{\"com/foo/Bar\":[0,1],\"com/foo/Baz\":[2]}}", + KeployDedupAgent.toDedupJson("t", probes, Collections.emptyList())); + } + @Test public void covPayloadWithNoCoverageSerializesEmptyMap() { String json = KeployDedupAgent.toDedupJson( diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java index 0c1c767c..84f3c085 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java @@ -58,9 +58,17 @@ public class InProcessScanTest { private static final String IGNORED = "IGNORED proto=2\n"; private static final String SHOP = "com/acme/Shop"; private static final String CART = "com/acme/Cart"; + // The JaCoCo ids of the bytes setUp writes for the two classes: the ids + // JaCoCo gives them when it sees the bytes the index reads. + private static final long SHOP_ID = IndexReport.classId(new byte[] {1}); + private static final long CART_ID = IndexReport.classId(new byte[] {2}); // The build id of the two classes setUp writes, as the INDEX report has it. - private static final String BUILD = IndexReport.buildId( - new long[] {IndexReport.classId(new byte[] {1}), IndexReport.classId(new byte[] {2})}); + private static final String BUILD = IndexReport.buildId(new long[] {SHOP_ID, CART_ID}); + // Ids of copies of a class JaCoCo saw other bytes of (another agent + // changed them first): one past the sign bit, and one below every id + // above as an unsigned number. + private static final long HIGH_ID = 0x8000000000000001L; + private static final long LOW_ID = 0x99L; @Rule public TemporaryFolder tmp = new TemporaryFolder(); @@ -149,7 +157,7 @@ public void tearDown() { @Test public void eachTestGetsItsOwnHitsAndNoneOfTheBackground() throws Exception { System.setProperty("keploy.java.dedup.diagnostics", "true"); - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); boolean[] lib = agent.probes(0x99L, "org/lib/Lib", 2); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); @@ -181,25 +189,98 @@ public void eachTestGetsItsOwnHitsAndNoneOfTheBackground() throws Exception { } @Test - public void aClassLoadedTwiceReportsTheOneWithTheLowerId() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); - // The same class from other bytes, in a second class loader. - boolean[] twin = agent.probes(0x99L, SHOP, 3); + public void aClassLoadedTwiceReportsTheCopyTheIndexRead() throws Exception { + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); + // The same class from other bytes, in a second class loader, with + // the lower id. + boolean[] twin = agent.probes(LOW_ID, SHOP, 3); + assertTrue(Long.compareUnsigned(LOW_ID, SHOP_ID) < 0); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); assertEquals(TAKEN, send("START test-set-0/test-1")); shop[3] = true; twin[1] = true; assertEquals(TAKEN, send("END test-set-0/test-1")); - assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1]}}", + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[3]}}", data.next()); assertFalse("both were cleared", shop[3] || twin[1]); } } + @Test + public void ofTwoCopiesTheIndexDidNotReadTheLowerIdAsAnUnsignedNumberIsReported() throws Exception { + // Signed, HIGH_ID is the lower. + boolean[] high = agent.probes(HIGH_ID, SHOP, 4); + boolean[] low = agent.probes(LOW_ID, SHOP, 3); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + high[3] = true; + low[1] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals(test("test-set-0/test-1", probes(SHOP, "1"), SHOP), data.next()); + } + } + + @Test + public void aClassJacocoSawOtherBytesOfIsNamedInClassMismatch() throws Exception { + System.setProperty("keploy.java.dedup.diagnostics", "true"); + boolean[] cart = agent.probes(CART_ID, CART, 3); + // Shop as another agent changed it before JaCoCo saw it. + boolean[] shop = agent.probes(HIGH_ID, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + cart[0] = true; + shop[2] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals(test("test-set-0/test-1", probes(CART, "0") + "," + probes(SHOP, "2"), SHOP), + data.next()); + assertNotNull(logged("classMismatch=1, sampleClasses=[" + SHOP + "]")); + // A test that runs only the class the index read names none. + assertEquals(TAKEN, send("START test-set-0/test-2")); + cart[1] = true; + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals(test("test-set-0/test-2", probes(CART, "1"), null), data.next()); + assertEquals("said for the first test alone", 1, count("classMismatch=")); + } + } + + @Test + public void withTheScanOffTheDumpChoosesOfTwoCopiesAsTheScanDoes() throws Exception { + System.setProperty("keploy.java.scan", "off"); + boolean[] cart = agent.probes(CART_ID, CART, 3); + boolean[] cartTwin = agent.probes(LOW_ID, CART, 2); + boolean[] shopHigh = agent.probes(HIGH_ID, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); + boolean[] shopLow = agent.probes(LOW_ID + 1, SHOP, 3); + boolean[] lib = agent.probes(0x77L, "org/lib/Lib", 2); + assertTrue(Long.compareUnsigned(LOW_ID, CART_ID) < 0 && Long.compareUnsigned(LOW_ID + 1, SHOP_ID) < 0); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + lib[1] = true; + cart[2] = true; + cartTwin[0] = true; + shopHigh[0] = true; + shop[3] = true; + shopLow[1] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals("the copies the index read", + test("test-set-0/test-1", probes(CART, "2") + "," + probes(SHOP, "3"), null), data.next()); + // Of two copies the index did not read, the lower id as an + // unsigned number, named in classMismatch. + assertEquals(TAKEN, send("START test-set-0/test-2")); + shopHigh[1] = true; + shopLow[2] = true; + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals(test("test-set-0/test-2", probes(SHOP, "2"), SHOP), data.next()); + } + } + @Test public void withDiagnosticsOffTheBackgroundIsNotLogged() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); shop[0] = true; @@ -214,8 +295,8 @@ public void withDiagnosticsOffTheBackgroundIsNotLogged() throws Exception { @Test public void betweenTwoTestsTheHitsGoOnceToAReceiverThatAskedAndNeverIntoATest() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); - boolean[] cart = agent.probes(0x4321L, CART, 3); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); + boolean[] cart = agent.probes(CART_ID, CART, 3); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); // The app's start: not between two tests. @@ -251,8 +332,8 @@ public void betweenTwoTestsTheHitsGoOnceToAReceiverThatAskedAndNeverIntoATest() @Test public void betweenTwoTestsAClassLoadedTwiceIsReportedAsATestsHitsWouldBe() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); - boolean[] twin = agent.probes(0x99L, SHOP, 3); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); + boolean[] twin = agent.probes(LOW_ID, SHOP, 3); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); assertEquals(TAKEN, send("START test-set-0/test-1")); @@ -262,15 +343,44 @@ public void betweenTwoTestsAClassLoadedTwiceIsReportedAsATestsHitsWouldBe() thro shop[3] = true; twin[1] = true; assertEquals(TAKEN, send("START test-set-0/test-2")); - assertEquals("the copy with the lower id", - background("test-set-0/test-1", "test-set-0/test-2", probes(SHOP, "1")), data.nextBackground()); + assertEquals("the copy the index read", + background("test-set-0/test-1", "test-set-0/test-2", probes(SHOP, "3")), data.nextBackground()); assertFalse("both were cleared", shop[3] || twin[1]); } } + @Test + public void betweenTwoTestsAClassJacocoSawOtherBytesOfIsNamedInClassMismatch() throws Exception { + boolean[] cart = agent.probes(CART_ID, CART, 3); + // Shop as another agent changed it before JaCoCo saw it: only this + // copy runs between the tests. + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); + boolean[] woven = agent.probes(HIGH_ID, SHOP, 3); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("BACKGROUND")); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals(test("test-set-0/test-1", "", null), data.next()); + cart[2] = true; + woven[1] = true; + assertEquals(TAKEN, send("START test-set-0/test-2")); + assertEquals(background("test-set-0/test-1", "test-set-0/test-2", + probes(CART, "2") + "," + probes(SHOP, "1"), SHOP, 0), data.nextBackground()); + // Between the next two, only the class the index read: none named. + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals(test("test-set-0/test-2", "", null), data.next()); + shop[0] = true; + assertEquals(TAKEN, send("START test-set-0/test-3")); + assertEquals(background("test-set-0/test-2", "test-set-0/test-3", probes(SHOP, "0")), + data.nextBackground()); + assertFalse("all were cleared", cart[2] || woven[1] || shop[0]); + } + } + @Test public void noBackgroundRecordReachesAReceiverThatDidNotAsk() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); assertEquals(TAKEN, send("START test-set-0/test-1")); @@ -287,7 +397,7 @@ public void noBackgroundRecordReachesAReceiverThatDidNotAsk() throws Exception { @Test public void whatAStartClearsAfterNoTakenEndIsNoBackground() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); assertEquals(TAKEN, send("START test-set-0/test-1")); @@ -312,7 +422,7 @@ public void whatAStartClearsAfterNoTakenEndIsNoBackground() throws Exception { @Test public void anEndNotTakenWithNoStartBeforeItOpensNoWindow() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); assertEquals(TAKEN, send("START test-set-0/test-1")); @@ -338,7 +448,7 @@ public void anEndNotTakenWithNoStartBeforeItOpensNoWindow() throws Exception { @Test public void anEndWhoseRecordCouldNotBeSentOpensNoWindow() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); File socket = new File(KeployDedupAgent.dataSocketPath); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); @@ -371,7 +481,7 @@ public void anEndWhoseRecordCouldNotBeSentOpensNoWindow() throws Exception { @Test public void aReceiverBoundAnewGetsTheIndexBeforeABackgroundRecord() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); assertTrue(KeployDedupAgent.start()); try (DataReceiver first = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertEquals(TAKEN, send("START test-set-0/test-1")); @@ -395,7 +505,7 @@ public void aReceiverBoundAnewGetsTheIndexBeforeABackgroundRecord() throws Excep @Test public void aReceiverBoundAnewGetsNoBackgroundRecordUntilItAsks() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); assertTrue(KeployDedupAgent.start()); try (DataReceiver first = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertEquals(TAKEN, send("START test-set-0/test-1")); @@ -423,7 +533,7 @@ public void aReceiverBoundAnewGetsNoBackgroundRecordUntilItAsks() throws Excepti @Test public void withNoReceiverBackgroundIsNotTakenAndAWindowGoesNowhere() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); assertTrue(KeployDedupAgent.start()); assertEquals(TAKEN, send("START test-set-0/test-1")); assertEquals("no receiver to send it to", IGNORED, send("BACKGROUND")); @@ -445,7 +555,7 @@ public void withNoReceiverBackgroundIsNotTakenAndAWindowGoesNowhere() throws Exc @Test public void aBackgroundRecordThatCouldNotBeSentIsCountedInTheNext() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); File socket = new File(KeployDedupAgent.dataSocketPath); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); @@ -529,7 +639,7 @@ public void aBackgroundRecordThatCouldNotBeSentIsCountedInTheNext() throws Excep public void withTheScanOffEachTestTakesJacocosDumpAndBackgroundIsNotTaken() throws Exception { System.setProperty("keploy.java.dedup.diagnostics", "true"); System.setProperty("keploy.java.scan", "Off"); - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); shop[0] = true; @@ -564,7 +674,7 @@ public void anyOtherScanValueKeepsTheScan() throws Exception { public void anEndWhoseScanFailsTakesJacocosDumpFromThenOn() throws Exception { System.setProperty("keploy.java.dedup.diagnostics", "true"); ProbeScanTest.CountingMap entries = ProbeScanTest.CountingMap.swapInto(agent.data); - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); assertEquals(TAKEN, send("START test-set-0/test-1")); @@ -596,7 +706,7 @@ public void anEndWhoseScanFailsTakesJacocosDumpFromThenOn() throws Exception { public void aStartWhoseScanFailsResetsInstead() throws Exception { System.setProperty("keploy.java.dedup.diagnostics", "true"); ProbeScanTest.CountingMap entries = ProbeScanTest.CountingMap.swapInto(agent.data); - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); entries.fail = true; try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); @@ -612,46 +722,91 @@ public void aStartWhoseScanFailsResetsInstead() throws Exception { } @Test - public void aClassLoadedTwiceReportsTheOneTheManifestNames() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); - boolean[] twin = agent.probes(0x99L, SHOP, 3); + public void ofTwoCopiesTheIndexDidNotReadTheOneTheManifestNamesIsReported() throws Exception { + boolean[] high = agent.probes(HIGH_ID, SHOP, 4); + boolean[] low = agent.probes(LOW_ID, SHOP, 3); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); // Only the copy with the higher id runs first: the manifest names it. assertEquals(TAKEN, send("START test-set-0/test-1")); - shop[3] = true; + high[3] = true; assertEquals(TAKEN, send("END test-set-0/test-1")); - assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[3]}}", - data.next()); + assertEquals(test("test-set-0/test-1", probes(SHOP, "3"), SHOP), data.next()); // Both run: the probes sent are still that copy's. assertEquals(TAKEN, send("START test-set-0/test-2")); - shop[2] = true; + high[2] = true; + low[0] = true; + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals(test("test-set-0/test-2", probes(SHOP, "2"), SHOP), data.next()); + } + } + + @Test + public void theCopyTheManifestNamesIsFoundWhenItsIdHasFewerThan16HexDigits() throws Exception { + // Neither copy is the one the index read. The one the manifest will + // name has a 4-digit id and is not the lowest: a hex form compared + // with the manifest's padded one would never find it. + long named = 0x1234L; + boolean[] high = agent.probes(named, SHOP, 4); + boolean[] low = agent.probes(LOW_ID, SHOP, 3); + assertTrue(Long.compareUnsigned(LOW_ID, named) < 0); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + high[3] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals(test("test-set-0/test-1", probes(SHOP, "3"), SHOP), data.next()); + // Both run: the probes sent are still the named copy's, every time. + for (int n = 2; n <= 3; n++) { + assertEquals(TAKEN, send("START test-set-0/test-" + n)); + high[2] = true; + low[0] = true; + assertEquals(TAKEN, send("END test-set-0/test-" + n)); + assertEquals(test("test-set-0/test-" + n, probes(SHOP, "2"), SHOP), data.next()); + } + } + } + + @Test + public void aClassLoadedTwiceReportsTheCopyTheIndexReadOverTheOneTheManifestNames() throws Exception { + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); + boolean[] twin = agent.probes(HIGH_ID, SHOP, 3); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + // Only the other copy runs first: the manifest names it. + assertEquals(TAKEN, send("START test-set-0/test-1")); + twin[2] = true; + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertEquals(test("test-set-0/test-1", probes(SHOP, "2"), SHOP), data.next()); + // Both run: the copy the index read. + assertEquals(TAKEN, send("START test-set-0/test-2")); + shop[1] = true; twin[0] = true; assertEquals(TAKEN, send("END test-set-0/test-2")); - assertEquals("{\"id\":\"test-set-0/test-2\",\"executedLinesByFile\":{\"" + SHOP + "\":[2]}}", - data.next()); + assertEquals(test("test-set-0/test-2", probes(SHOP, "1"), null), data.next()); } } @Test public void aClassLoadedTwiceSendsTheOtherCopysProbesWhenOnlyItRan() throws Exception { - boolean[] shop = agent.probes(0x1234L, SHOP, 4); - boolean[] twin = agent.probes(0x99L, SHOP, 3); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); + boolean[] twin = agent.probes(LOW_ID, SHOP, 3); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); - // Only the copy with the higher id runs first: the manifest names it. + // Only the copy the index read, with the higher id, runs first: + // the manifest names it. assertEquals(TAKEN, send("START test-set-0/test-1")); shop[3] = true; assertEquals(TAKEN, send("END test-set-0/test-1")); assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[3]}}", data.next()); // Only the other copy runs: its probes are sent under the class's - // name, as JaCoCo's dump sends them, not dropped. + // name, as JaCoCo's dump sends them, not dropped, and the class + // is named in classMismatch. assertEquals(TAKEN, send("START test-set-0/test-2")); twin[2] = true; assertEquals(TAKEN, send("END test-set-0/test-2")); - assertEquals("{\"id\":\"test-set-0/test-2\",\"executedLinesByFile\":{\"" + SHOP + "\":[2]}}", - data.next()); + assertEquals(test("test-set-0/test-2", probes(SHOP, "2"), SHOP), data.next()); assertFalse("the copy's hit was cleared", twin[2]); } } @@ -659,7 +814,7 @@ public void aClassLoadedTwiceSendsTheOtherCopysProbesWhenOnlyItRan() throws Exce @Test public void anEndWhoseScanLostHitsIsNotTaken() throws Exception { System.setProperty("keploy.java.dedup.diagnostics", "true"); - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); assertEquals(TAKEN, send("START test-set-0/test-1")); @@ -683,7 +838,7 @@ public void anEndWhoseScanLostHitsIsNotTaken() throws Exception { @Test public void anErrorReadingJacocosClassesAtAnEndTakesJacocosDump() throws Exception { ProbeScanTest.CountingMap entries = ProbeScanTest.CountingMap.swapInto(agent.data); - boolean[] shop = agent.probes(0x1234L, SHOP, 4); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { assertTrue(KeployDedupAgent.start()); assertEquals(TAKEN, send("START test-set-0/test-1")); @@ -793,12 +948,17 @@ private int count(String text) { // background is the BACKGROUND record of the window from the END of // after to the START of before, whose probes by class are classes // (probes(...) joined by ","), with lost when it is more than 0. - private static String background(String after, String before, String classes, int lost) { + private static String background(String after, String before, String classes, String mismatch, int lost) { return "{\"kind\":\"background\",\"v\":1,\"build\":\"" + BUILD + "\",\"after\":\"" + after + "\",\"before\":\"" + before + "\",\"executedLinesByFile\":{" + classes + "}" + + (mismatch == null ? "" : ",\"classMismatch\":[\"" + mismatch + "\"]") + (lost > 0 ? ",\"lost\":" + lost : "") + "}"; } + private static String background(String after, String before, String classes, int lost) { + return background(after, before, classes, null, lost); + } + private static String background(String after, String before, String classes) { return background(after, before, classes, 0); } @@ -807,6 +967,13 @@ private static String probes(String className, String probes) { return "\"" + className + "\":[" + probes + "]"; } + // test is a test's coverage record as the SDK sends it: classes as in + // background, and classMismatch naming mismatch when it is not null. + private static String test(String id, String classes, String mismatch) { + return "{\"id\":\"" + id + "\",\"executedLinesByFile\":{" + classes + "}" + + (mismatch == null ? "" : ",\"classMismatch\":[\"" + mismatch + "\"]") + "}"; + } + private static String send(String command) throws IOException { try (AFUNIXSocket socket = AFUNIXSocket.newInstance()) { socket.connect(AFUNIXSocketAddress.of(new File(KeployDedupAgent.controlSocketPath)), 3000); @@ -876,14 +1043,19 @@ private static final class DataReceiver implements Closeable { server.bind(AFUNIXSocketAddress.of(new File(path)), 50); // The socket file keeps a second name until the row's folder is // removed, so that a receiver bound anew on the path never gets - // this one's inode back. Deleted and bound again within one tick - // of the file system's clock, it would have the same mtime too, - // and the SDK, which tells receivers apart by the file's inode - // and mtime, would take it for this one: a stated limit, which a - // receiver bound anew for a new run or the next test set, long - // after, never meets. - Files.createLink(new File(path + "." + BOUND.incrementAndGet() + ".held").toPath(), - new File(path).toPath()); + // this one's inode back. Bound anew within one tick of the file + // system's clock after this one's bind, it would have the same + // mtime too, and the SDK, which tells receivers apart by the + // file's inode and mtime, would take it for this one: a stated + // limit, which a receiver bound anew for a new run or the next + // test set, long after, never meets. + try { + Files.createLink(new File(path + "." + BOUND.incrementAndGet() + ".held").toPath(), + new File(path).toPath()); + } catch (IOException | RuntimeException e) { + server.close(); + throw e; + } Thread thread = new Thread(this::serve, "data-receiver"); thread.setDaemon(true); thread.start(); diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/IndexReportTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/IndexReportTest.java index 06e3033f..2a7464fb 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/IndexReportTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/IndexReportTest.java @@ -49,6 +49,15 @@ public void noClassHasNoBuildId() { assertEquals("", IndexReport.buildId(new long[0])); } + @Test + public void aClassIdIsWrittenAs16LowercaseHexDigits() { + assertEquals("0000000000000000", IndexReport.hexId(0)); + assertEquals("0000000000000099", IndexReport.hexId(0x99L)); + assertEquals("0123456789abcdef", IndexReport.hexId(0x0123456789abcdefL)); + assertEquals("8000000000000001", IndexReport.hexId(0x8000000000000001L)); + assertEquals("ffffffffffffffff", IndexReport.hexId(-1L)); + } + @Test public void theClassIdIsJacocos() { byte[] bytes = {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe, 0, 0, 0, 52, 1, 2, 3}; diff --git a/scripts/smoke-javaagent.sh b/scripts/smoke-javaagent.sh index b1b75908..be40fa3b 100755 --- a/scripts/smoke-javaagent.sh +++ b/scripts/smoke-javaagent.sh @@ -65,9 +65,9 @@ import java.io.OutputStream; import java.net.URL; import java.net.URLClassLoader; import java.nio.charset.StandardCharsets; -import java.util.concurrent.CountDownLatch; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; import java.util.jar.JarFile; import java.util.jar.JarInputStream; import java.util.jar.Manifest; @@ -97,25 +97,17 @@ public final class SmokeHarness { // after the START: no socket file was there when the SDK bound its // control socket, so it sent no INDEX report then, and it must send // it here first, before the test's coverage. - AtomicReference index = new AtomicReference(); - AtomicReference payload = new AtomicReference(); - CountDownLatch received = new CountDownLatch(1); + final BlockingQueue records = new LinkedBlockingQueue(); Thread receiver = new Thread(new Runnable() { @Override public void run() { try (AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()) { server.bind(AFUNIXSocketAddress.of(DATA_SOCKET), 8); - for (int n = 0; payload.get() == null; n++) { + for (;;) { try (AFUNIXSocket socket = server.accept()) { - String record = readAll(socket.getInputStream()); - if (n == 0 && record.startsWith("{\"kind\":\"index\",")) { - index.set(record); - } else { - payload.set(record); - } + records.add(readAll(socket.getInputStream())); } } - received.countDown(); } catch (Exception e) { e.printStackTrace(); } @@ -131,19 +123,22 @@ public final class SmokeHarness { } command("END test-set-0/" + mode); - if (!received.await(10, TimeUnit.SECONDS)) { - throw new IllegalStateException("timed out waiting for coverage payload in " + mode); + String json = next(records, mode); + String report = null; + if (json.startsWith("{\"kind\":\"index\",")) { + report = json; + json = next(records, mode); } - - String json = payload.get(); // The fingerprint is keyed by VM class name (probe-based coverage), - // e.g. "smoke/Work" — not a source file like "Work.java". - if (json == null - || !json.contains("\"id\":\"test-set-0/" + mode + "\"") - || !json.contains("smoke/Work")) { + // e.g. "smoke/Work" — not a source file like "Work.java". JaCoCo's id + // of each class the app ran is the id of the bytes the index read, + // so the record names no class in classMismatch: one named here + // would lose that class's coverage in Keploy. + if (!json.contains("\"id\":\"test-set-0/" + mode + "\"") + || !json.contains("smoke/Work") + || json.contains("\"classMismatch\"")) { throw new IllegalStateException("unexpected coverage payload for " + mode + ": " + json); } - String report = index.get(); if (report == null || !report.contains("\"source\":\"" + wantSource + "\"") || !report.contains("\"jacoco\":\"" + wantJacoco + "\"") @@ -173,6 +168,49 @@ public final class SmokeHarness { } System.out.println(mode + ": " + report); System.out.println(mode + ": " + json); + String otherWork = System.getProperty("smoke.otherWork", ""); + if (!otherWork.isEmpty()) { + runsAnotherCopy(otherWork, mode, records); + } + } + + // runsAnotherCopy runs a test in which only another smoke.Work runs: + // one built from other source, which the index never read, in a loader + // of its own (as a second web app or an agent's woven copy would be). + // JaCoCo measures it under the same name with another id, so the + // record must send its probes under smoke/Work and name smoke/Work in + // classMismatch, and no other class. + private static void runsAnotherCopy(String dir, String mode, BlockingQueue records) + throws Exception { + String id = "test-set-0/" + mode + "-another-copy"; + URLClassLoader loader = new URLClassLoader(new URL[] {new File(dir).toURI().toURL()}, null); + try { + Class other = Class.forName("smoke.Work", false, loader); + command("START " + id); + Object result = other.getMethod("exercise", int.class).invoke(null, 1); + command("END " + id); + if (!Integer.valueOf(3).equals(result)) { + throw new IllegalStateException("the copy in " + dir + " did not run in " + mode + ": " + result); + } + String json = next(records, mode); + if (!json.contains("\"id\":\"" + id + "\"") + || !json.contains("\"smoke/Work\":[") + || !json.contains(",\"classMismatch\":[\"smoke/Work\"]}")) { + throw new IllegalStateException("the record of a test that ran another copy of smoke/Work does" + + " not name it, alone, in classMismatch in " + mode + ": " + json); + } + System.out.println(mode + ": " + json); + } finally { + loader.close(); + } + } + + private static String next(BlockingQueue records, String mode) throws InterruptedException { + String record = records.poll(10, TimeUnit.SECONDS); + if (record == null) { + throw new IllegalStateException("timed out waiting for a record on the data socket in " + mode); + } + return record; } // loadsItsOwn loads ASM, Gson and JaCoCo's core from the app's own jars @@ -279,6 +317,23 @@ public final class PlainApp { } JAVA +# Another smoke.Work, from other source, compiled into a dir of its own that +# the index never reads: the copy runsAnotherCopy loads. +mkdir -p "${work_dir}/other-src/smoke" "${work_dir}/other-work" +cat > "${work_dir}/other-src/smoke/Work.java" <<'JAVA' +package smoke; + +public final class Work { + private Work() { + } + + public static int exercise(int input) { + return input * 3; + } +} +JAVA +javac -d "${work_dir}/other-work" "${work_dir}/other-src/smoke/Work.java" + javac -cp "${agent_jar}" -d "${work_dir}/classes" \ "${work_dir}/src/smoke/Work.java" \ "${work_dir}/src/smoke/SmokeHarness.java" \ @@ -336,16 +391,20 @@ run_smoke "classpath-fallback" \ # probes in place, in one pass, instead of taking JaCoCo's reset and dump. # This is the real (shaded) JaCoCo runtime, which the unit tests do not load: # its layout must be the one the pass reads. The END's diagnostics line names -# the step: `scan=` for the pass, `dump=` for JaCoCo's dump. +# the step: `scan=` for the pass, `dump=` for JaCoCo's dump. A second test +# runs only another copy of smoke.Work (smoke.otherWork): a class JaCoCo +# first sees there, which the pass must find, and name in classMismatch. scan_log="${work_dir}/in-place-scan.log" scan_rc=0 KEPLOY_JAVA_DEDUP_DIAGNOSTICS=1 run_smoke "in-place-scan" \ java_with_agents "in-place-scan" \ + -Dsmoke.otherWork="${work_dir}/other-work" \ -cp "${work_dir}/classes:${agent_jar}" \ smoke.SmokeHarness "in-place-scan" derived attached 2>"${scan_log}" || scan_rc=$? if [[ "${scan_rc}" -ne 0 ]] \ || ! grep -q 'diagnostics: START test-set-0/in-place-scan took .* scan=' "${scan_log}" \ || ! grep -q 'diagnostics: END test-set-0/in-place-scan took .* scan=' "${scan_log}" \ + || ! grep -q 'diagnostics: END test-set-0/in-place-scan-another-copy took .* scan=' "${scan_log}" \ || grep -q 'cannot be read in place\|the probe scan failed' "${scan_log}"; then cat "${scan_log}" >&2 echo "in-place-scan: the agent did not read JaCoCo's probes in place (exit ${scan_rc})" >&2 @@ -353,6 +412,26 @@ if [[ "${scan_rc}" -ne 0 ]] \ fi echo "in-place-scan: START and END read JaCoCo's probes in place" +# KEPLOY_JAVA_SCAN=off: each test takes JaCoCo's dump and reset instead, and +# its records must be the pass's: no class named for the app's own copy, and +# the other copy named. +dump_log="${work_dir}/jacoco-dump.log" +dump_rc=0 +KEPLOY_JAVA_SCAN=off KEPLOY_JAVA_DEDUP_DIAGNOSTICS=1 run_smoke "jacoco-dump" \ + java_with_agents "jacoco-dump" \ + -Dsmoke.otherWork="${work_dir}/other-work" \ + -cp "${work_dir}/classes:${agent_jar}" \ + smoke.SmokeHarness "jacoco-dump" derived attached 2>"${dump_log}" || dump_rc=$? +if [[ "${dump_rc}" -ne 0 ]] \ + || ! grep -q 'diagnostics: END test-set-0/jacoco-dump took .* dump=' "${dump_log}" \ + || ! grep -q 'diagnostics: END test-set-0/jacoco-dump-another-copy took .* dump=' "${dump_log}" \ + || grep -q 'diagnostics: END test-set-0/jacoco-dump.* scan=' "${dump_log}"; then + cat "${dump_log}" >&2 + echo "jacoco-dump: with KEPLOY_JAVA_SCAN=off the ENDs did not take JaCoCo's dump (exit ${dump_rc})" >&2 + exit 1 +fi +echo "jacoco-dump: with KEPLOY_JAVA_SCAN=off each END took JaCoCo's dump" + mkdir -p "${work_dir}/jar/lib" cp "${agent_jar}" "${work_dir}/jar/lib/keploy-sdk.jar" cat > "${work_dir}/jar/MANIFEST.MF" <&2 echo "sdk-only-in-place-scan: the agent did not read its own JaCoCo's probes in place" >&2 From fdb22d264b562992cc2bffce8beedb6d34d22b34 Mon Sep 17 00:00:00 2001 From: slayerjain Date: Fri, 20 Nov 2026 06:47:05 +0530 Subject: [PATCH 06/14] fix(sdk): carry the EPL-2.0 and Apache 2.0 texts in keploy-sdk.jar keploy-sdk.jar carries JaCoCo (EPL-2.0), the ASM it depends on (BSD 3-Clause), Gson and junixsocket (Apache 2.0). Its META-INF/THIRD-PARTY-NOTICES.txt named them, but the jar held neither license text the notices relied on: not the Apache License 2.0, which keploy-sdk, Gson and junixsocket are under and whose section 4(a) asks for a copy with each redistribution, nor the EPL-2.0, named only by its URL. junixsocket's NOTICE attribution was missing too. - META-INF/LICENSE is the repo's LICENSE (Apache 2.0), copied in at build time, so the jar's and the repo's cannot differ. - META-INF/LICENSE-EPL-2.0.txt is the EPL-2.0 as eclipse.org publishes it. - THIRD-PARTY-NOTICES.txt points to both. It now says that keploy-sdk's own code is under the Apache 2.0 and the third-party software stays under its own license, where keploy-sdk's source is, and the build that renames the packages; names org.jacoco.agent's AgentJar class, which the jar carries outside jacocoagent.jar and under its own name; adds org.jacoco.core's sources on Maven Central to JaCoCo's source pointer; gives Gson's and junixsocket's source tags, Gson's copyright line and junixsocket's NOTICE text; and says which about.html covers which JaCoCo part. - org.jacoco.core and org.jacoco.agent both hold an about.html at their root. The shade plugin kept the first it met, core's, and dropped the other with a warning. It now leaves out org.jacoco.agent's, the same file as the one inside jacocoagent.jar, which the jar carries whole, so which one lands no longer rests on the dependencies' order. Rows: ThirdPartyNoticesTest checks that the notices name the versions the build resolves (JaCoCo's core and agent, with AgentJar; ASM with asm-commons and asm-tree; Gson; junixsocket), so a dependency bumped without its notice fails CI's unit tests; that they quote the attributions the licenses ask for (the copyright lines, ASM's three conditions, the start and the damages sentence of its disclaimer, and the first and last lines of junixsocket's NOTICE); and that both license texts sit next to the notices. scripts/smoke-javaagent.sh checks that the built jar holds the repo's LICENSE, the EPL-2.0 text and the notices as the sources hold them, and org.jacoco.core's about.html; that org.jacoco.agent's about.html is the one inside jacocoagent.jar; and that each part of the jar maps onto a part the notices name, so a new package, or a dependency from another project, shaded in without its notice fails. Another artifact of ASM or junixsocket maps onto its project's part, whose license the notices already carry. The README gets a License section. Signed-off-by: slayerjain --- README.md | 9 + keploy-sdk/pom.xml | 28 ++ .../resources/META-INF/LICENSE-EPL-2.0.txt | 277 ++++++++++++++++++ .../META-INF/THIRD-PARTY-NOTICES.txt | 56 +++- .../keploy/dedup/ThirdPartyNoticesTest.java | 166 +++++++++++ scripts/smoke-javaagent.sh | 91 ++++++ 6 files changed, 617 insertions(+), 10 deletions(-) create mode 100644 keploy-sdk/src/main/resources/META-INF/LICENSE-EPL-2.0.txt create mode 100644 keploy-sdk/src/test/java/io/keploy/dedup/ThirdPartyNoticesTest.java diff --git a/README.md b/README.md index bda78c72..24eba05e 100644 --- a/README.md +++ b/README.md @@ -260,3 +260,12 @@ For a working reference, see the Java dedup sample in `keploy/samples-java`: That sample is used in CI to validate Java dynamic dedup for JDK 8, 17, and 21 across native, classpath, Docker, distroless, and restricted Docker runs. The CI matrix is narrower than the supported range: it pins the runtimes the sample is exercised on, not the ceiling — see [Supported Java versions](#supported-java-versions). + +## License + +keploy-sdk is under the Apache License 2.0 (`LICENSE`). `keploy-sdk.jar` also +carries JaCoCo (Eclipse Public License 2.0), the ASM it depends on (BSD +3-Clause), Gson and junixsocket (Apache License 2.0). The jar holds +`META-INF/THIRD-PARTY-NOTICES.txt`, which names each in the version the jar +carries and says where JaCoCo's source is, next to `META-INF/LICENSE` and +JaCoCo's `META-INF/LICENSE-EPL-2.0.txt`. diff --git a/keploy-sdk/pom.xml b/keploy-sdk/pom.xml index 9625560b..c9e35633 100644 --- a/keploy-sdk/pom.xml +++ b/keploy-sdk/pom.xml @@ -97,6 +97,21 @@ + + + src/main/resources + + + + ${project.basedir}/.. + + LICENSE + + META-INF + + org.apache.maven.plugins @@ -158,6 +173,19 @@ META-INF/versions/*/module-info.class + + + org.jacoco:org.jacoco.agent + + about.html + + diff --git a/keploy-sdk/src/main/resources/META-INF/LICENSE-EPL-2.0.txt b/keploy-sdk/src/main/resources/META-INF/LICENSE-EPL-2.0.txt new file mode 100644 index 00000000..e23ece2c --- /dev/null +++ b/keploy-sdk/src/main/resources/META-INF/LICENSE-EPL-2.0.txt @@ -0,0 +1,277 @@ +Eclipse Public License - v 2.0 + + THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE + PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION + OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + + a) in the case of the initial Contributor, the initial content + Distributed under this Agreement, and + + b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + where such changes and/or additions to the Program originate from + and are Distributed by that particular Contributor. A Contribution + "originates" from a Contributor if it was added to the Program by + such Contributor itself or anyone acting on such Contributor's behalf. + Contributions do not include changes or additions to the Program that + are not Modified Works. + +"Contributor" means any person or entity that Distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which +are necessarily infringed by the use or sale of its Contribution alone +or when combined with the Program. + +"Program" means the Contributions Distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement +or any Secondary License (as applicable), including Contributors. + +"Derivative Works" shall mean any work, whether in Source Code or other +form, that is based on (or derived from) the Program and for which the +editorial revisions, annotations, elaborations, or other modifications +represent, as a whole, an original work of authorship. + +"Modified Works" shall mean any work in Source Code or other form that +results from an addition to, deletion from, or modification of the +contents of the Program, including, for purposes of clarity any new file +in Source Code form that contains any contents of the Program. Modified +Works shall not include works that contain only declarations, +interfaces, types, classes, structures, or files of the Program solely +in each case in order to link to, bind by name, or subclass the Program +or Modified Works thereof. + +"Distribute" means the acts of a) distributing or b) making available +in any manner that enables the transfer of a copy. + +"Source Code" means the form of a Program preferred for making +modifications, including but not limited to software source code, +documentation source, and configuration files. + +"Secondary License" means either the GNU General Public License, +Version 2.0, or any later versions of that license, including any +exceptions or additional permissions as identified by the initial +Contributor. + +2. GRANT OF RIGHTS + + a) Subject to the terms of this Agreement, each Contributor hereby + grants Recipient a non-exclusive, worldwide, royalty-free copyright + license to reproduce, prepare Derivative Works of, publicly display, + publicly perform, Distribute and sublicense the Contribution of such + Contributor, if any, and such Derivative Works. + + b) Subject to the terms of this Agreement, each Contributor hereby + grants Recipient a non-exclusive, worldwide, royalty-free patent + license under Licensed Patents to make, use, sell, offer to sell, + import and otherwise transfer the Contribution of such Contributor, + if any, in Source Code or other form. This patent license shall + apply to the combination of the Contribution and the Program if, at + the time the Contribution is added by the Contributor, such addition + of the Contribution causes such combination to be covered by the + Licensed Patents. The patent license shall not apply to any other + combinations which include the Contribution. No hardware per se is + licensed hereunder. + + c) Recipient understands that although each Contributor grants the + licenses to its Contributions set forth herein, no assurances are + provided by any Contributor that the Program does not infringe the + patent or other intellectual property rights of any other entity. + Each Contributor disclaims any liability to Recipient for claims + brought by any other entity based on infringement of intellectual + property rights or otherwise. As a condition to exercising the + rights and licenses granted hereunder, each Recipient hereby + assumes sole responsibility to secure any other intellectual + property rights needed, if any. For example, if a third party + patent license is required to allow Recipient to Distribute the + Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + + d) Each Contributor represents that to its knowledge it has + sufficient copyright rights in its Contribution, if any, to grant + the copyright license set forth in this Agreement. + + e) Notwithstanding the terms of any Secondary License, no + Contributor makes additional grants to any Recipient (other than + those set forth in this Agreement) as a result of such Recipient's + receipt of the Program under the terms of a Secondary License + (if permitted under the terms of Section 3). + +3. REQUIREMENTS + +3.1 If a Contributor Distributes the Program in any form, then: + + a) the Program must also be made available as Source Code, in + accordance with section 3.2, and the Contributor must accompany + the Program with a statement that the Source Code for the Program + is available under this Agreement, and informs Recipients how to + obtain it in a reasonable manner on or through a medium customarily + used for software exchange; and + + b) the Contributor may Distribute the Program under a license + different than this Agreement, provided that such license: + i) effectively disclaims on behalf of all other Contributors all + warranties and conditions, express and implied, including + warranties or conditions of title and non-infringement, and + implied warranties or conditions of merchantability and fitness + for a particular purpose; + + ii) effectively excludes on behalf of all other Contributors all + liability for damages, including direct, indirect, special, + incidental and consequential damages, such as lost profits; + + iii) does not attempt to limit or alter the recipients' rights + in the Source Code under section 3.2; and + + iv) requires any subsequent distribution of the Program by any + party to be under a license that satisfies the requirements + of this section 3. + +3.2 When the Program is Distributed as Source Code: + + a) it must be made available under this Agreement, or if the + Program (i) is combined with other material in a separate file or + files made available under a Secondary License, and (ii) the initial + Contributor attached to the Source Code the notice described in + Exhibit A of this Agreement, then the Program may be made available + under the terms of such Secondary Licenses, and + + b) a copy of this Agreement must be included with each copy of + the Program. + +3.3 Contributors may not remove or alter any copyright, patent, +trademark, attribution notices, disclaimers of warranty, or limitations +of liability ("notices") contained within the Program from any copy of +the Program which they Distribute, provided that Contributors may add +their own appropriate notices. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities +with respect to end users, business partners and the like. While this +license is intended to facilitate the commercial use of the Program, +the Contributor who includes the Program in a commercial product +offering should do so in a manner which does not create potential +liability for other Contributors. Therefore, if a Contributor includes +the Program in a commercial product offering, such Contributor +("Commercial Contributor") hereby agrees to defend and indemnify every +other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits +and other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such +Commercial Contributor in connection with its distribution of the Program +in a commercial product offering. The obligations in this section do not +apply to any claims or Losses relating to any actual or alleged +intellectual property infringement. In order to qualify, an Indemnified +Contributor must: a) promptly notify the Commercial Contributor in +writing of such claim, and b) allow the Commercial Contributor to control, +and cooperate with the Commercial Contributor in, the defense and any +related settlement negotiations. The Indemnified Contributor may +participate in any such claim at its own expense. + +For example, a Contributor might include the Program in a commercial +product offering, Product X. That Contributor is then a Commercial +Contributor. If that Commercial Contributor then makes performance +claims, or offers warranties related to Product X, those performance +claims and warranties are such Commercial Contributor's responsibility +alone. Under this section, the Commercial Contributor would have to +defend claims against the other Contributors related to those performance +claims and warranties, and if a court requires any other Contributor to +pay any damages as a result, the Commercial Contributor must pay +those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT +PERMITTED BY APPLICABLE LAW, THE PROGRAM IS PROVIDED ON AN "AS IS" +BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR +IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF +TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR +PURPOSE. Each Recipient is solely responsible for determining the +appropriateness of using and distributing the Program and assumes all +risks associated with its exercise of rights under this Agreement, +including but not limited to the risks and costs of program errors, +compliance with applicable laws, damage to or loss of data, programs +or equipment, and unavailability or interruption of operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT +PERMITTED BY APPLICABLE LAW, NEITHER RECIPIENT NOR ANY CONTRIBUTORS +SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST +PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of +the remainder of the terms of this Agreement, and without further +action by the parties hereto, such provision shall be reformed to the +minimum extent necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity +(including a cross-claim or counterclaim in a lawsuit) alleging that the +Program itself (excluding combinations of the Program with other software +or hardware) infringes such Recipient's patent(s), then such Recipient's +rights granted under Section 2(b) shall terminate as of the date such +litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it +fails to comply with any of the material terms or conditions of this +Agreement and does not cure such failure in a reasonable period of +time after becoming aware of such noncompliance. If all Recipient's +rights under this Agreement terminate, Recipient agrees to cease use +and distribution of the Program as soon as reasonably practicable. +However, Recipient's obligations under this Agreement and any licenses +granted by Recipient relating to the Program shall continue and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, +but in order to avoid inconsistency the Agreement is copyrighted and +may only be modified in the following manner. The Agreement Steward +reserves the right to publish new versions (including revisions) of +this Agreement from time to time. No one other than the Agreement +Steward has the right to modify this Agreement. The Eclipse Foundation +is the initial Agreement Steward. The Eclipse Foundation may assign the +responsibility to serve as the Agreement Steward to a suitable separate +entity. Each new version of the Agreement will be given a distinguishing +version number. The Program (including Contributions) may always be +Distributed subject to the version of the Agreement under which it was +received. In addition, after a new version of the Agreement is published, +Contributor may elect to Distribute the Program (including its +Contributions) under the new version. + +Except as expressly stated in Sections 2(a) and 2(b) above, Recipient +receives no rights or licenses to the intellectual property of any +Contributor under this Agreement, whether expressly, by implication, +estoppel or otherwise. All rights in the Program not expressly granted +under this Agreement are reserved. Nothing in this Agreement is intended +to be enforceable by any entity that is not a Contributor or Recipient. +No third-party beneficiary rights are created under this Agreement. + +Exhibit A - Form of Secondary Licenses Notice + +"This Source Code may also be made available under the following +Secondary Licenses when the conditions for such availability set forth +in the Eclipse Public License, v. 2.0 are satisfied: {name license(s), +version(s), and exceptions or additional permissions here}." + + Simply including a copy of this Agreement, including this Exhibit A + is not sufficient to license the Source Code under Secondary Licenses. + + If it is not possible or desirable to put the notice in a particular + file, then You may include the notice in a location (such as a LICENSE + file in a relevant directory) where a recipient would be likely to + look for such a notice. + + You may add additional accurate notices of copyright ownership. \ No newline at end of file diff --git a/keploy-sdk/src/main/resources/META-INF/THIRD-PARTY-NOTICES.txt b/keploy-sdk/src/main/resources/META-INF/THIRD-PARTY-NOTICES.txt index 028b7242..7edd1074 100644 --- a/keploy-sdk/src/main/resources/META-INF/THIRD-PARTY-NOTICES.txt +++ b/keploy-sdk/src/main/resources/META-INF/THIRD-PARTY-NOTICES.txt @@ -1,22 +1,39 @@ -keploy-sdk.jar (Apache License 2.0) carries the following third-party software. +keploy-sdk's own code is under the Apache License 2.0 (META-INF/LICENSE in +the jar). Its source is at https://github.com/keploy/java-sdk, under +keploy-sdk/; the build that makes the jar, renaming the packages below, is +keploy-sdk/pom.xml. keploy-sdk.jar also carries the third-party software +below, which stays under its own license. -JaCoCo 0.8.15 - Eclipse Public License 2.0 (https://www.eclipse.org/legal/epl-2.0/) +JaCoCo 0.8.15 - Eclipse Public License 2.0 (META-INF/LICENSE-EPL-2.0.txt in +the jar; https://www.eclipse.org/legal/epl-2.0/) Copyright (c) 2009, 2026 Mountainminds GmbH & Co. KG and Contributors JaCoCo's source code is available under the EPL-2.0 at - https://github.com/jacoco/jacoco, tag v0.8.15. + https://github.com/jacoco/jacoco, tag v0.8.15 (jacocoagent.jar is built + from its org.jacoco.agent.rt module), and org.jacoco.core's also from + Maven Central as org.jacoco:org.jacoco.core:0.8.15:sources. - jacocoagent.jar, unchanged, from org.jacoco:org.jacoco.agent:0.8.15; keploy-sdk - starts it when the JVM attaches no JaCoCo agent of its own. + starts it when the JVM attaches no JaCoCo agent of its own. It holds its + own about.html. + - the class org.jacoco.agent.AgentJar and its package-info, unchanged and + not renamed, from org.jacoco:org.jacoco.agent:0.8.15, which keploy-sdk + uses to read jacocoagent.jar. - the classes of org.jacoco:org.jacoco.core:0.8.15, unchanged but for their package, renamed from org.jacoco.core to io.keploy.shaded.org.jacoco.core (maven-shade-plugin relocation, keploy-sdk/pom.xml), so that they never meet an app's own JaCoCo. + about.html at the root of the jar is org.jacoco.core's, unchanged. + org.jacoco.agent's, which covers AgentJar and jacocoagent.jar, is the same + file as the about.html inside jacocoagent.jar. Neither covers the rest of + the jar. ASM 9.10.1 - BSD 3-Clause License Source: https://gitlab.ow2.org/asm/asm - the classes of org.ow2.asm:asm, asm-commons and asm-tree 9.10.1, which org.jacoco.core depends on, unchanged but for their package, renamed from - org.objectweb.asm to io.keploy.shaded.org.objectweb.asm; and the copy - inside jacocoagent.jar. ASM is subject to the following license: + org.objectweb.asm to io.keploy.shaded.org.objectweb.asm. + - the copy of ASM inside jacocoagent.jar, whose version the about.html + inside jacocoagent.jar names. + ASM is subject to the following license: ASM: a very small and fast Java bytecode manipulation framework Copyright (c) 2000-2011 INRIA, France Telecom @@ -46,10 +63,29 @@ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -Gson 2.9.1 - Apache License 2.0 - Source: https://github.com/google/gson +Gson 2.9.1 - Apache License 2.0 (META-INF/LICENSE in the jar) + Copyright (C) 2008 Google Inc. + Source: https://github.com/google/gson, tag gson-parent-2.9.1 - its classes, unchanged but for their package, renamed from com.google.gson to io.keploy.shaded.com.google.gson. -junixsocket 2.10.1 - Apache License 2.0 - Source: https://github.com/kohlschutter/junixsocket +junixsocket 2.10.1 - Apache License 2.0 (META-INF/LICENSE in the jar) + Source: https://github.com/kohlschutter/junixsocket, tag junixsocket-2.10.1 + - the classes and native libraries of junixsocket-common and + junixsocket-native-common 2.10.1, unchanged. Its NOTICE file reads: + +junixsocket + +Copyright 2009-2024 Christian Kohlschütter + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/ThirdPartyNoticesTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/ThirdPartyNoticesTest.java new file mode 100644 index 00000000..5e6e6d4d --- /dev/null +++ b/keploy-sdk/src/test/java/io/keploy/dedup/ThirdPartyNoticesTest.java @@ -0,0 +1,166 @@ +package io.keploy.dedup; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Properties; + +import org.junit.Test; + +/** + * keploy-sdk.jar carries JaCoCo (EPL-2.0), ASM (BSD 3-Clause), Gson and + * junixsocket (Apache 2.0). META-INF/THIRD-PARTY-NOTICES.txt names each in + * the version the build carries and quotes the attributions their licenses + * ask for, and the license texts it points to sit next to it. A dependency + * bumped without its notice fails here: update + * keploy-sdk/src/main/resources/META-INF/THIRD-PARTY-NOTICES.txt. + * scripts/smoke-javaagent.sh checks the built jar holds them. + */ +public class ThirdPartyNoticesTest { + + private static final String NOTICES = "META-INF/THIRD-PARTY-NOTICES.txt"; + + @Test + public void noticesNameTheVersionsTheJarCarries() throws IOException { + String notices = flat(read(noticesUrl())); + + String jacoco = mavenVersion("org.jacoco", "org.jacoco.core"); + assertContains(notices, "JaCoCo " + jacoco + " - Eclipse Public License 2.0"); + assertContains(notices, "https://github.com/jacoco/jacoco, tag v" + jacoco); + assertContains(notices, "org.jacoco:org.jacoco.core:" + jacoco + ":sources"); + assertContains(notices, "the classes of org.jacoco:org.jacoco.core:" + jacoco + ","); + String agent = mavenVersion("org.jacoco", "org.jacoco.agent"); + assertContains(notices, "- jacocoagent.jar, unchanged, from org.jacoco:org.jacoco.agent:" + + agent + ";"); + // keploy-sdk.jar carries AgentJar from the same artifact, under its own + // name, outside jacocoagent.jar. + assertContains(notices, "- the class " + org.jacoco.agent.AgentJar.class.getName() + + " and its package-info, unchanged and not renamed, from" + + " org.jacoco:org.jacoco.agent:" + agent + ","); + + String asm = org.objectweb.asm.ClassReader.class.getPackage().getImplementationVersion(); + assertNotNull("ASM's jar names no Implementation-Version", asm); + assertEquals("asm-commons", asm, + org.objectweb.asm.commons.Remapper.class.getPackage().getImplementationVersion()); + assertEquals("asm-tree", asm, + org.objectweb.asm.tree.ClassNode.class.getPackage().getImplementationVersion()); + assertContains(notices, "ASM " + asm + " - BSD 3-Clause License"); + assertContains(notices, "asm-commons and asm-tree " + asm + ","); + + String gson = mavenVersion("com.google.code.gson", "gson"); + assertContains(notices, "Gson " + gson + " - Apache License 2.0"); + assertContains(notices, "tag gson-parent-" + gson); + + String junixsocket = mavenVersion("com.kohlschutter.junixsocket", "junixsocket-common"); + assertEquals("junixsocket-native-common", junixsocket, + mavenVersion("com.kohlschutter.junixsocket", "junixsocket-native-common")); + assertContains(notices, "junixsocket " + junixsocket + " - Apache License 2.0"); + assertContains(notices, "tag junixsocket-" + junixsocket); + assertContains(notices, "junixsocket-native-common " + junixsocket + ","); + } + + @Test + public void licenseTextsSitNextToTheNotices() throws IOException { + URL notices = noticesUrl(); + String text = read(notices); + assertContains(flat(text), "keploy-sdk's own code is under the Apache License 2.0" + + " (META-INF/LICENSE in the jar)."); + assertContains(text, "Apache License 2.0 (META-INF/LICENSE in the jar)"); + assertContains(text, "Eclipse Public License 2.0 (META-INF/LICENSE-EPL-2.0.txt in"); + + String apache = read(new URL(notices, "LICENSE")); + assertTrue("META-INF/LICENSE is not the Apache License 2.0", + apache.trim().startsWith("Apache License\n")); + assertContains(apache, "Version 2.0, January 2004"); + assertContains(apache, "4. Redistribution."); + + String epl = read(new URL(notices, "LICENSE-EPL-2.0.txt")); + assertTrue("META-INF/LICENSE-EPL-2.0.txt is not the EPL-2.0", + epl.startsWith("Eclipse Public License - v 2.0\n")); + assertContains(epl, "3.1 If a Contributor Distributes the Program in any form, then:"); + assertContains(epl, "Exhibit A - Form of Secondary Licenses Notice"); + } + + // The texts the licenses ask a copy to carry: JaCoCo's copyright line, + // ASM's copyright, conditions and disclaimer (BSD 3-Clause, clauses 1 and + // 2), Gson's copyright line (Apache 2.0, 4(c)) and junixsocket's NOTICE + // (4(d)). + @Test + public void noticesQuoteTheAttributionsTheLicensesAskFor() throws IOException { + String notices = flat(read(noticesUrl())); + assertContains(notices, "Copyright (c) 2009, 2026 Mountainminds GmbH & Co. KG and Contributors"); + + assertContains(notices, "ASM: a very small and fast Java bytecode manipulation framework" + + " Copyright (c) 2000-2011 INRIA, France Telecom All rights reserved."); + assertContains(notices, "1. Redistributions of source code must retain the above copyright" + + " notice, this list of conditions and the following disclaimer."); + assertContains(notices, "2. Redistributions in binary form must reproduce the above copyright" + + " notice, this list of conditions and the following disclaimer in the" + + " documentation and/or other materials provided with the distribution."); + assertContains(notices, "3. Neither the name of the copyright holders nor the names of its" + + " contributors may be used to endorse or promote products derived from" + + " this software without specific prior written permission."); + assertContains(notices, "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\""); + assertContains(notices, "IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE" + + " LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR" + + " CONSEQUENTIAL DAMAGES"); + assertContains(notices, "EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."); + + assertContains(notices, "Gson " + mavenVersion("com.google.code.gson", "gson") + + " - Apache License 2.0 (META-INF/LICENSE in the jar) Copyright (C) 2008 Google Inc."); + + assertContains(notices, "Its NOTICE file reads: junixsocket Copyright 2009-2024 Christian" + + " Kohlsch\u00fctter Licensed under the Apache License, Version 2.0 (the \"License\");"); + assertContains(notices, "See the License for the specific language governing permissions and" + + " limitations under the License."); + } + + private static URL noticesUrl() { + URL url = ThirdPartyNoticesTest.class.getClassLoader().getResource(NOTICES); + assertNotNull(NOTICES + " is not on the class path", url); + return url; + } + + // mavenVersion is the version in the pom.properties a dependency's jar + // holds: the version the build resolved, so the version the jar carries. + private static String mavenVersion(String group, String artifact) throws IOException { + String path = "META-INF/maven/" + group + "/" + artifact + "/pom.properties"; + URL url = ThirdPartyNoticesTest.class.getClassLoader().getResource(path); + assertNotNull(path + " is not on the class path", url); + Properties props = new Properties(); + try (InputStream in = url.openStream()) { + props.load(in); + } + String version = props.getProperty("version"); + assertNotNull(path + " names no version", version); + return version; + } + + private static String read(URL url) throws IOException { + try (InputStream in = url.openStream()) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buf = new byte[8192]; + for (int n; (n = in.read(buf)) > 0; ) { + out.write(buf, 0, n); + } + return new String(out.toByteArray(), StandardCharsets.UTF_8).replace("\r\n", "\n"); + } + } + + // flat folds each run of white space into one space, so that a phrase + // the notices wrap across lines matches. + private static String flat(String text) { + return text.replaceAll("\\s+", " "); + } + + private static void assertContains(String text, String want) { + assertTrue("THIRD-PARTY-NOTICES.txt or a license text lacks \"" + want + + "\": update keploy-sdk/src/main/resources/META-INF/", text.contains(want)); + } +} diff --git a/scripts/smoke-javaagent.sh b/scripts/smoke-javaagent.sh index be40fa3b..6effe558 100755 --- a/scripts/smoke-javaagent.sh +++ b/scripts/smoke-javaagent.sh @@ -32,6 +32,97 @@ cleanup() { } trap cleanup EXIT +# keploy-sdk.jar carries JaCoCo (EPL-2.0), ASM, Gson and junixsocket: it must +# hold keploy-sdk's own license, JaCoCo's EPL-2.0 text and about.html, and the +# notices that name each and say where JaCoCo's source is. A change to the +# shade plugin's filters or to the jar's resources that drops one fails here. +licenses_dir="${work_dir}/licenses" +jacoco_maven=META-INF/maven/org.jacoco +license_entries=(META-INF/LICENSE META-INF/LICENSE-EPL-2.0.txt META-INF/THIRD-PARTY-NOTICES.txt about.html + "${jacoco_maven}/org.jacoco.core/pom.properties" "${jacoco_maven}/org.jacoco.agent/pom.properties") +mkdir -p "${licenses_dir}" +(cd "${licenses_dir}" && jar xf "${agent_jar}" "${license_entries[@]}") +for entry in "${license_entries[@]}"; do + if [[ ! -s "${licenses_dir}/${entry}" ]]; then + echo "licenses: ${agent_jar} holds no ${entry}" >&2 + exit 1 + fi +done +if ! cmp -s "${repo_root}/LICENSE" "${licenses_dir}/META-INF/LICENSE"; then + echo "licenses: the jar's META-INF/LICENSE is not the repo's LICENSE" >&2 + exit 1 +fi +for entry in META-INF/LICENSE-EPL-2.0.txt META-INF/THIRD-PARTY-NOTICES.txt; do + if ! cmp -s "${module_dir}/src/main/resources/${entry}" "${licenses_dir}/${entry}"; then + echo "licenses: the jar's ${entry} is not keploy-sdk/src/main/resources/${entry}" >&2 + exit 1 + fi +done +# about.html at the jar's root is org.jacoco.core's, as the notices say, and +# org.jacoco.agent's, which the shade filter leaves out, is the same file as +# the about.html inside jacocoagent.jar. +for artifact in org.jacoco.core org.jacoco.agent; do + version="$(sed -n 's/^version=//p' "${licenses_dir}/${jacoco_maven}/${artifact}/pom.properties" | tr -d '\r')" + mvn -q -f "${repo_root}/pom.xml" org.apache.maven.plugins:maven-dependency-plugin:3.6.1:copy \ + -Dartifact="org.jacoco:${artifact}:${version}" -DoutputDirectory="${licenses_dir}" + mkdir -p "${licenses_dir}/${artifact}" + (cd "${licenses_dir}/${artifact}" && jar xf "../${artifact}-${version}.jar" about.html) +done +if ! cmp -s "${licenses_dir}/org.jacoco.core/about.html" "${licenses_dir}/about.html"; then + echo "licenses: the jar's about.html is not org.jacoco.core's" >&2 + exit 1 +fi +mkdir -p "${licenses_dir}/jacocoagent" +(cd "${licenses_dir}/jacocoagent" && jar xf "${agent_jar}" jacocoagent.jar && jar xf jacocoagent.jar about.html) +if ! cmp -s "${licenses_dir}/org.jacoco.agent/about.html" "${licenses_dir}/jacocoagent/about.html"; then + echo "licenses: org.jacoco.agent's about.html is not the one inside the jar's jacocoagent.jar" >&2 + exit 1 +fi +# The jar holds keploy-sdk's own packages and the parts the notices name, and +# nothing else: a dependency or a package that lands in the jar without its +# notice fails here. A third-party part goes in THIRD-PARTY-NOTICES.txt first, +# then in want_parts; a new package of keploy-sdk's own, in want_parts. +jar_parts="$(jar tf "${agent_jar}" | grep -v '/$' | awk -F/ ' + $1 == "io" && $2 == "keploy" && $3 == "shaded" { print $1 "/" $2 "/" $3 "/" $4 "/" $5 "/" $6; next } + $1 == "org" && $2 == "jacoco" && $3 == "agent" { print; next } + $1 == "META-INF" && $2 == "versions" && NF > 6 { print $1 "/" $2 "/*/" $4 "/" $5 "/" $6; next } + $1 == "META-INF" && ($2 == "maven" || $2 == "native-image") && NF > 3 { print $1 "/" $2 "/" $3; next } + $1 == "lib" && NF == 4 && $3 == "jni" && $4 ~ /^(lib)?junixsocket-native-/ { print "lib/*/jni/junixsocket-native"; next } + NF <= 3 { print; next } + { print $1 "/" $2 "/" $3 }' | LC_ALL=C sort -u)" +want_parts="$(LC_ALL=C sort <<'PARTS' +META-INF/LICENSE +META-INF/LICENSE-EPL-2.0.txt +META-INF/MANIFEST.MF +META-INF/THIRD-PARTY-NOTICES.txt +META-INF/maven/com.google.code.gson +META-INF/maven/com.kohlschutter.junixsocket +META-INF/maven/io.keploy +META-INF/maven/org.jacoco +META-INF/native-image/com.kohlschutter.junixsocket +META-INF/versions/*/org/newsclub/lib +META-INF/versions/*/org/newsclub/net +about.html +io/keploy/dedup +io/keploy/servlet +io/keploy/shaded/com/google/gson +io/keploy/shaded/org/jacoco/core +io/keploy/shaded/org/objectweb/asm +jacocoagent.jar +lib/*/jni/junixsocket-native +org/jacoco/agent/AgentJar.class +org/jacoco/agent/package-info.class +org/newsclub/lib +org/newsclub/net +PARTS +)" +if [[ "${jar_parts}" != "${want_parts}" ]]; then + echo "licenses: ${agent_jar} does not hold exactly the parts THIRD-PARTY-NOTICES.txt names (< the jar, > want_parts):" >&2 + diff <(printf '%s\n' "${jar_parts}") <(printf '%s\n' "${want_parts}") >&2 || true + exit 1 +fi +echo "licenses: keploy-sdk.jar holds its license, JaCoCo's EPL-2.0 text and about.html, and the notices of every part it holds" + mkdir -p "${work_dir}/src/smoke" "${work_dir}/classes" cat > "${work_dir}/src/smoke/Work.java" <<'JAVA' From 742ad1d0399d957d69c4bef6f725c62e177b20c2 Mon Sep 17 00:00:00 2001 From: slayerjain Date: Fri, 20 Nov 2026 08:13:38 +0530 Subject: [PATCH 07/14] fix(dedup): wait for a test's requests to finish before its END reads A server sends the response before it is done with the request: a filter's code after the chain (a request log, an authentication filter's cleanup) runs after Keploy has the response and has sent END. Under load END's pass ran first, that code landed between two tests, and the next START cleared it as background, so the same test got different coverage from run to run. The SDK now counts the requests of Tomcat (CoyoteAdapter.service and asyncDispatch), Jetty (HttpChannel.handle, and on Jetty 12 and 12.1 the servlet channel's handle, matched by name) and Undertow (Connectors.executeRootHandler). As each class loads, that method becomes one that counts the request in and out of RequestGate around the server's own code, which moves to a private keploy$request$. A class whose loader does not see the agent's own RequestGate is left unchanged, and so is any class loaded before the agent started. The JaCoCo keploy-sdk.jar starts adds its transformer first, so it ids the server's class as the index read it, and a server in a named module is counted too (the JVM lets a module an agent changed read the class path). END waits until every request that came in since the test's START is done, for at most KEPLOY_JAVA_END_WAIT_MS (200 ms by default, 10 s at most; 0 turns the wait and the class change off). A request already done costs END one read; one from before the START, or one that comes in once END has come (a probe, a scrape), is never waited for. Past the bound, a WARNING names the test, its record ends with "windowCut":true (after classMismatch, left out when false), and the next START opens no BACKGROUND window, since the cut test's late hits are in it. Limits: code on the app's own threads (an async task) is not counted, nor are Netty and WebFlux; the log says once when END does not wait, and why. A JaCoCo agent attached after keploy-sdk.jar (the agents in JAVA_TOOL_OPTIONS start before the command line's) sees the changed server class, so the README's examples list JaCoCo first. Rows: RequestGate's count and wait; the server entries on stand-ins of each server's class (arguments of every type, static, synchronized, a pre-Java 6 class, abstract/native/bridge/synthetic methods, interfaces, loaders that do not see the counter); END's wait, cut and no-window on the unix socket and on TCP; and real Tomcat 9, Jetty 9.4 and Undertow 2.2 in JVMs of their own with the agent, where a filter's code that runs after the response is in the test's record, missing with the wait off, and cut past the bound; Tomcat on the module path; and the embedded JaCoCo measuring Tomcat's own class with no classMismatch for it. Signed-off-by: slayerjain --- README.md | 15 +- keploy-sdk/pom.xml | 26 ++ .../io/keploy/dedup/KeployDedupAgent.java | 145 ++++++- .../java/io/keploy/dedup/RequestGate.java | 108 +++++ .../java/io/keploy/dedup/ServerEntries.java | 328 ++++++++++++++ .../keploy/dedup/CoverageTcpClientTest.java | 41 ++ .../io/keploy/dedup/DedupWireFormatTest.java | 13 + .../io/keploy/dedup/InProcessScanTest.java | 229 +++++++++- .../java/io/keploy/dedup/RequestGateTest.java | 185 ++++++++ .../io/keploy/dedup/ServerEntriesTest.java | 402 ++++++++++++++++++ .../java/io/keploy/dedup/ServerTailTest.java | 360 ++++++++++++++++ .../org/example/servers/HelloServlet.java | 22 + .../java/org/example/servers/ServerApp.java | 90 ++++ .../test/java/org/example/servers/Tail.java | 17 + .../java/org/example/servers/TailFilter.java | 41 ++ 15 files changed, 2012 insertions(+), 10 deletions(-) create mode 100644 keploy-sdk/src/main/java/io/keploy/dedup/RequestGate.java create mode 100644 keploy-sdk/src/main/java/io/keploy/dedup/ServerEntries.java create mode 100644 keploy-sdk/src/test/java/io/keploy/dedup/RequestGateTest.java create mode 100644 keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java create mode 100644 keploy-sdk/src/test/java/io/keploy/dedup/ServerTailTest.java create mode 100644 keploy-sdk/src/test/java/org/example/servers/HelloServlet.java create mode 100644 keploy-sdk/src/test/java/org/example/servers/ServerApp.java create mode 100644 keploy-sdk/src/test/java/org/example/servers/Tail.java create mode 100644 keploy-sdk/src/test/java/org/example/servers/TailFilter.java diff --git a/README.md b/README.md index 24eba05e..74b1a12b 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,13 @@ Keploy Enterprise drives dynamic dedup per testcase. 2. The Java agent clears what the application's classes ran since the last testcase. What it clears is background (a health check, a scheduled job): it is never counted in a testcase's coverage. 3. Enterprise replays the testcase. 4. Enterprise sends `END /` on `/tmp/coverage_control.sock`. -5. The Java agent reads, and clears, the probes of the application's classes that the testcase hit, and sends the executed probe indices per class (`{className -> [probeIdx]}`) as JSON on `/tmp/coverage_data.sock`. Probes capture branch-level coverage (which branch a test took), so they distinguish tests that run the same lines but take different branches. +5. The Java agent waits until the testcase's requests are done (below), then reads, and clears, the probes of the application's classes that the testcase hit, and sends the executed probe indices per class (`{className -> [probeIdx]}`) as JSON on `/tmp/coverage_data.sock`. Probes capture branch-level coverage (which branch a test took), so they distinguish tests that run the same lines but take different branches. 6. Enterprise writes the result to `dedupData.yaml` and uses it to identify duplicates. Coverage is collected at per-testcase granularity, not process granularity. +A server sends a response before it is done with the request: a filter's code after the chain (a request log, an authentication filter's cleanup) runs after Enterprise has the response and has sent `END`. Since 2.2.0 the agent counts the requests of Tomcat (8.5 to 11), Jetty (9.4 to 12.1) and Undertow, Spring Boot's three servers, from where the server starts running a request to where its response is finished, and each async dispatch of it alike, and `END` first waits until every request that came in since the testcase's `START` is done, for at most `KEPLOY_JAVA_END_WAIT_MS` (200 ms by default, 10 s at most). A request that comes in once `END` has come (a readiness probe, a metrics scrape, or a call the testcase's own code makes after its response) is not waited for. Before, `END` read the probes at once, so under load that code was missed, and the next `START` cleared it as background: the same testcase had different coverage from run to run. A request that is done costs `END` nothing, and a request that came in before the `START` (a stream, a request of the testcase before) is never waited for. When the wait reaches its bound, the log says so at WARNING and the testcase's record ends with `"windowCut":true` (after `classMismatch`; left out otherwise): it holds what was read then, a Keploy that reads the flag does not judge that testcase (an older one judges the hits the record holds), and what the next `START` clears is not sent as background. Limits: what the application runs on threads of its own (an async task, an executor's work between two dispatches of an async request, an Undertow completion listener that runs on the IO thread after an async write) is not counted; Netty, and so WebFlux, is not counted, nor is a server whose classes were loaded before the agent started (it is attached to a running JVM) or whose class loader does not see the agent's classes (a module system such as JBoss Modules); the log says once when a testcase's `END` does not wait, and why. A JaCoCo agent attached after keploy-sdk.jar instruments the server class as the agent changed it, so when the classes Keploy measures include the server's (an uber jar with the server inside, or every class on the class path), each testcase's `classMismatch` names that class: list `-javaagent:jacocoagent.jar` before keploy-sdk.jar. The agents in `JAVA_TOOL_OPTIONS` start before the command line's, so keploy-sdk.jar there with JaCoCo on the command line is attached before JaCoCo. The JaCoCo keploy-sdk.jar starts itself always sees the server's own class. To count them, the agent changes the one method of each server that runs a request (Tomcat's `CoyoteAdapter.service` and `asyncDispatch`, Jetty's `HttpChannel.handle` or, on Jetty 12, its servlet channel's `handle`, Undertow's `Connectors.executeRootHandler`) as the class loads: the server's own code moves to a private method `keploy$request$` beside it, which shows in a stack trace and keeps the method's annotations; the method of the old name, which counts the request and calls it, carries none, so reflection that reads the annotations of `service` itself finds none. + Since 2.2.0 a testcase's record also names, in `"classMismatch":[className, ...]` after its probes, each class whose probes come from bytes other than the ones the agent indexed: a class loader that weaves the bytes it defines (load-time weaving) or an agent listed before JaCoCo's that does not ask to retransform changed the class before JaCoCo saw it, or the JVM loaded a multi-release jar's versioned copy. JaCoCo's id for such a class is not the CRC64 of the indexed bytes, so its probe indices do not belong to the indexed class, and Keploy must not read them against it. The key is left out when there is no such class. Of two copies of one class (loaded twice, from different bytes) that a testcase ran, the record reports the copy the agent indexed, else the one the coverage manifest already names, else the lowest class id compared as an unsigned number; a BACKGROUND record chooses alike, and names such classes in a `classMismatch` of its own. Every class id the agent writes (the coverage manifest's `id`) is 16 lowercase hex digits since 2.2.0; older agents wrote it unpadded, so read it as a hex number. With the JaCoCo agent in the application's JVM (the usual case), steps 2 and 5 are one pass over the probe arrays of the application's classes alone, split across up to four threads (half the cores): a probe is cleared only once it was read as hit, so a hit made while the pass runs is reported by that pass or the next one, never lost. Library classes are neither read nor cleared. If JaCoCo's runtime data cannot be read that way, or the pass fails once, the agent uses JaCoCo's own reset and dump for the rest of the run, as it does when JaCoCo is reached over TCP. @@ -97,8 +99,10 @@ WARNING and starts JaCoCo on every class; set `KEPLOY_JAVA_CLASS_DIRS` to the app's class dirs and jars to fix that. If the command attaches a JaCoCo agent of its own (`-javaagent:jacocoagent.jar`, -in any order, or in `JAVA_TOOL_OPTIONS`), the SDK starts none and reads coverage -from that one, through JaCoCo's in-process runtime API +before or after keploy-sdk.jar, or in `JAVA_TOOL_OPTIONS`), the SDK starts none +and reads coverage from that one. List it before keploy-sdk.jar: one listed after +it instruments the server classes the SDK changes (see How It Works). The SDK +reads it through JaCoCo's in-process runtime API (`org.jacoco.agent.rt.RT.getAgent()`). That JaCoCo instruments every class its own options take in. If the in-process API is unavailable (for example because the JaCoCo agent is loaded into an isolated classloader), the SDK falls back to @@ -107,8 +111,8 @@ JaCoCo's TCP server mode. To use the fallback explicitly, start JaCoCo in ```bash java \ - -javaagent:/path/to/keploy-sdk.jar \ -javaagent:/path/to/jacocoagent.jar=address=127.0.0.1,port=36320,output=tcpserver \ + -javaagent:/path/to/keploy-sdk.jar \ -jar your-app.jar ``` @@ -205,8 +209,9 @@ ceiling does not quietly fall behind again. - `KEPLOY_JAVA_CLASS_DIRS`: optional comma-separated class, jar, war, ear, or zip locations of the app's own classes. You rarely need it: see "Which classes count as the app's" below - `KEPLOY_JAVA_CLASSPATH_FALLBACK`: scans the full classpath when neither the app's own classes nor the executable jar give any class. `false` leaves the index empty then, and no test's coverage is measured. Default: `true` - `KEPLOY_JAVA_DEDUP_DISABLED`: disables the Java dedup agent when set to `true`, `1`, or `yes` -- `KEPLOY_JAVA_DEDUP_DIAGNOSTICS`: when `true`, `1`, or `yes`, logs one line per START and END that says how long the command took and how long each step did: `warmup`, then `scan` (the pass that clears the background) for a START; `scan` (the pass that reads and clears the test's hits), `filter` (the hits made into the test's probes by class), `json` and `send` for an END, plus `classes` on TCP (the check for new app classes, and the class-zip frame when there are some). Where JaCoCo's own reset and dump are used instead, a START has `reset`, and an END has `dump` (JaCoCo's dump), `parse` and `filter` (to the app's classes) in place of `scan` and `filter`; a step that did not run is left out, so an empty dump has only `dump`. A START that cleared background hits also logs how many classes and probes they were. The app's class files are read once: before the app starts, when keploy-sdk.jar starts JaCoCo itself; otherwise on the first START, by the warmup's thread (timed under `warmup`, and, past the 10 seconds that START waits for the warmup, under its `scan`, which waits for the read, or, with JaCoCo's reset and dump, which never wait for it, on the first END whose dump is not empty, under `filter`), or, with the warmup off, under that START's `scan` (or, with JaCoCo's dump, on the first END whose dump is not empty, under `filter`). On the unix socket `took` ends before the answer is written; on TCP it includes the answer. Use it to see where a test's time goes on your app +- `KEPLOY_JAVA_DEDUP_DIAGNOSTICS`: when `true`, `1`, or `yes`, logs one line per START and END that says how long the command took and how long each step did: `warmup`, then `scan` (the pass that clears the background) for a START; `requests` (the wait for the test's requests to finish, when one still ran), `scan` (the pass that reads and clears the test's hits), `filter` (the hits made into the test's probes by class), `json` and `send` for an END, plus `classes` on TCP (the check for new app classes, and the class-zip frame when there are some). Where JaCoCo's own reset and dump are used instead, a START has `reset`, and an END has `dump` (JaCoCo's dump), `parse` and `filter` (to the app's classes) in place of `scan` and `filter`; a step that did not run is left out, so an empty dump has only `dump`. A START that cleared background hits also logs how many classes and probes they were. The app's class files are read once: before the app starts, when keploy-sdk.jar starts JaCoCo itself; otherwise on the first START, by the warmup's thread (timed under `warmup`, and, past the 10 seconds that START waits for the warmup, under its `scan`, which waits for the read, or, with JaCoCo's reset and dump, which never wait for it, on the first END whose dump is not empty, under `filter`), or, with the warmup off, under that START's `scan` (or, with JaCoCo's dump, on the first END whose dump is not empty, under `filter`). On the unix socket `took` ends before the answer is written; on TCP it includes the answer. Use it to see where a test's time goes on your app - `KEPLOY_JAVA_SCAN`: `off` (in any case) turns the one-pass read of the application's probes off: each testcase then takes JaCoCo's own reset and dump, as when JaCoCo is reached over TCP, a `START` no longer sees background hits, and `BACKGROUND` is answered `IGNORED`. Use it if the in-place read misbehaves on your JVM; at 50k application classes JaCoCo's reset and dump took about 20 ms per testcase in our profile, against under 2 ms with the in-place read. Default: on +- `KEPLOY_JAVA_END_WAIT_MS`: how long a testcase's `END` waits, at most, for the requests that came in since its `START` to finish (see "How It Works"). `0` turns the wait off, and leaves the servers' classes unchanged: `END` reads the probes at once, as before 2.2.0. At most `10000`: a larger value waits 10000 ms, said once at WARNING, so that Keploy has the record before it stops waiting for it. Default: `200` - `KEPLOY_JAVA_DEDUP_WARMUP_DISABLED`: when `true`, `1`, or `yes`, skips the warmup, which loads and initializes the app's own classes once, on the first START, so that no test is charged with their static initializers. It loads them through the class loader the app runs them with, such as a Spring Boot fat jar's own loader (since 2.2.0; before, a fat jar's classes were not warmed up), so every app class not loaded yet is loaded then, and JaCoCo instruments it: on an app with many classes it never uses, that is more class metadata (metaspace) than the app alone would load. It runs on a thread of its own: the first START waits for it for at most 10 seconds, then goes on, and the warmup stops after the class it is initializing. Its static initializer goes on while the tests run, and each line it runs from then on counts in the test running at the time (between two tests, in none: the next START clears it); classes it did not reach initialize when the app first uses them, as with the warmup off. The log says how long the warmup took, and, when the START stopped waiting, which class it was initializing ## Which classes count as the app's diff --git a/keploy-sdk/pom.xml b/keploy-sdk/pom.xml index c9e35633..26dd115a 100644 --- a/keploy-sdk/pom.xml +++ b/keploy-sdk/pom.xml @@ -94,6 +94,32 @@ 4.13.2 test + + + org.apache.tomcat.embed + tomcat-embed-core + 9.0.98 + test + + + org.eclipse.jetty + jetty-servlet + 9.4.57.v20241219 + test + + + io.undertow + undertow-servlet + 2.2.37.Final + test + + + org.jboss.spec.javax.servlet + jboss-servlet-api_4.0_spec + + + diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java index 19cdcbdb..8bfbf597 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java @@ -47,6 +47,7 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Supplier; import java.util.jar.JarEntry; @@ -78,7 +79,12 @@ static String toDedupJson(String id, Map> probesByClass) { } static String toDedupJson(String id, Map> probesByClass, List classMismatch) { - return GSON.toJson(new DedupPayload(id, new TestCoverage(probesByClass, classMismatch))); + return toDedupJson(id, probesByClass, classMismatch, false); + } + + static String toDedupJson(String id, Map> probesByClass, List classMismatch, + boolean windowCut) { + return GSON.toJson(new DedupPayload(id, new TestCoverage(probesByClass, classMismatch, windowCut))); } // encodeBytecodeFrame builds the bytecode frame the SDK writes to the @@ -106,6 +112,14 @@ static String encodeBytecodeFrame(String buildTag, String manifestJson, byte[] z private static final int DEFAULT_JACOCO_PORT = 36320; private static final int SOCKET_TIMEOUT_MILLIS = 3000; private static final int SOCKET_BACKLOG = 50; + // How long a test's END waits for its requests to finish by default. + static final long DEFAULT_END_WAIT_MILLIS = 200; + // Well inside the 30 s Keploy's k8s collector waits for an END's answer: + // past it, the record would be lost. + static final long MAX_END_WAIT_MILLIS = 10000; + private static final AtomicBoolean END_WAIT_INVALID_LOGGED = new AtomicBoolean(false); + private static final AtomicBoolean END_WAIT_CAPPED_LOGGED = new AtomicBoolean(false); + private static final AtomicBoolean NOT_COUNTED_LOGGED = new AtomicBoolean(false); // The answer to a command not taken (a START that could not clear the // counters, an END with no START for that test, as after a restart, an // END whose scan lost hits it had cleared): nothing is published @@ -270,7 +284,11 @@ private static boolean start(CoverageIndex index, Instrumentation instrumentatio } if (instrumentation != null) { + // JaCoCo's transformer first: transformers run in the order they + // were added, so JaCoCo instruments, and ids, a server's class as + // the index read it, and ServerEntries counts in JaCoCo's copy. startJacoco(instrumentation, index); + countRequests(instrumentation); } Thread thread = new Thread(worker, threadName); thread.setDaemon(true); @@ -280,6 +298,71 @@ private static boolean start(CoverageIndex index, Instrumentation instrumentatio return true; } + // countRequests makes the requests of the app's server count in + // RequestGate (ServerEntries), so that a test's END waits for them to + // finish, unless KEPLOY_JAVA_END_WAIT_MS is 0. From premain, before the + // app's main loads its server, and after startJacoco: a JaCoCo agent + // attached after keploy-sdk.jar adds its transformer after this one, so + // it ids the changed server class, which classMismatch then names when + // the index holds it. + private static void countRequests(Instrumentation instrumentation) { + if (endWaitMillis() == 0) { + return; + } + try { + instrumentation.addTransformer(new ServerEntries(), false); + } catch (RuntimeException e) { + log(Level.WARNING, "Keploy dedup does not count the app's requests: their server could not be" + + " changed to count them", e); + } + } + + // notCounted says, once, why a test's END does not wait for its + // requests: the wait is off, or no server counts them (uncounted names + // a server the app loaded whose requests are not counted, as Netty). + static String notCounted(long endWaitMillis, List uncounted) { + String why = endWaitMillis == 0 ? "KEPLOY_JAVA_END_WAIT_MS is 0" + : !uncounted.isEmpty() ? "the app's server is " + uncounted.get(0) + + ", whose requests keploy-sdk does not count" + : "no Tomcat, Jetty or Undertow request entry loaded after keploy-sdk.jar started" + + " (a Netty or WebFlux server, one started before the SDK, or the SDK not" + + " started as a -javaagent)"; + return "Keploy dedup: a test's END does not wait for its requests to finish: " + why + + ". Code a request runs after its response is sent can land after the test's END"; + } + + // endWaitMillis is how long a test's END waits for the requests that + // came in since its START to finish: KEPLOY_JAVA_END_WAIT_MS (or + // keploy.java.end.wait.ms), 200 by default, 10000 at most; 0 turns the + // wait and the count off. + static long endWaitMillis() { + String value = envOrProperty("KEPLOY_JAVA_END_WAIT_MS", "keploy.java.end.wait.ms", "").trim(); + if (value.isEmpty()) { + return DEFAULT_END_WAIT_MILLIS; + } + try { + long millis = Long.parseLong(value); + if (millis > MAX_END_WAIT_MILLIS) { + if (END_WAIT_CAPPED_LOGGED.compareAndSet(false, true)) { + log(Level.WARNING, "Keploy dedup: KEPLOY_JAVA_END_WAIT_MS is " + millis + ", so a test's END" + + " waits up to " + MAX_END_WAIT_MILLIS + " ms, its most, so that Keploy has the" + + " test's record before it stops waiting for it", null); + } + return MAX_END_WAIT_MILLIS; + } + if (millis >= 0) { + return millis; + } + } catch (NumberFormatException e) { + // Said below. + } + if (END_WAIT_INVALID_LOGGED.compareAndSet(false, true)) { + log(Level.WARNING, "Keploy dedup: KEPLOY_JAVA_END_WAIT_MS is not a number of milliseconds (" + value + + "), so a test's END waits up to " + DEFAULT_END_WAIT_MILLIS + " ms", null); + } + return DEFAULT_END_WAIT_MILLIS; + } + // startJacoco starts the JaCoCo agent keploy-sdk.jar carries, with // includes= the app's classes in index (EmbeddedJacoco), unless the JVM // has a JaCoCo agent of its own or the index holds no class. It runs in @@ -791,12 +874,19 @@ private boolean run(CoverageCommand command, Phases phases) { } try { + boolean cut = !collector.awaitRequests(command.testId, phases); TestCoverage coverage = collector.capture(phases); + if (cut) { + coverage = coverage.cut(); + } if (coverage.probesByClass.isEmpty()) { log(Level.FINE, "No Java coverage collected for " + command.testId, null); } publisher.publish(command.testId, coverage, phases); - lastEndedTestId = command.testId; + // After a cut END, what the next START clears holds this + // test's own late hits: not a window, as after an END not + // taken. + lastEndedTestId = cut ? null : command.testId; return true; } catch (Exception e) { // Not ACK for the same reason: nothing reached Keploy. @@ -1012,7 +1102,11 @@ private void dispatch(CoverageCommand command, OutputStream out, Phases phases) // exception would shift the collector onto the next test's frames. TestCoverage coverage; try { + boolean cut = !collector.awaitRequests(command.testId, phases); coverage = collector.capture(phases); + if (cut) { + coverage = coverage.cut(); + } } catch (Exception e) { // Not taken, as on the unix socket: an empty COV is // the frame of a test that ran no line of the app. @@ -1119,6 +1213,8 @@ private static final class CoverageCollector { // when the SDK was not started as a -javaagent. private final Instrumentation instrumentation; private final AtomicBoolean warmed = new AtomicBoolean(false); + // How long END waits for the test's requests; see awaitRequests. + private final long endWaitMillis; // The VM names of the app's classes, and the classes by those // names; see indexedClassNames. private volatile Set appClassNames; @@ -1158,6 +1254,7 @@ void resetBytecodeSentForNewConnection() { private CoverageCollector(JacocoClient jacocoClient, CoverageIndex coverageIndex, Instrumentation instrumentation) { + this.endWaitMillis = endWaitMillis(); this.jacocoClient = jacocoClient; this.coverageIndex = coverageIndex; this.instrumentation = instrumentation; @@ -1211,6 +1308,9 @@ private synchronized void dropProbeScan(Throwable cause) { // hits. Without it, or when the pass fails (in any way: a START only // has to clear), JaCoCo resets every class, and cleared stays empty. private boolean begin(Phases phases, List cleared) { + // This test's requests come in from now on; END waits for them + // alone. + RequestGate.open(); ProbeScan scan = probeScan(); if (scan != null) { try { @@ -1230,6 +1330,29 @@ private boolean begin(Phases phases, List cleared) { return reset; } + // awaitRequests waits, for at most endWaitMillis, until the requests + // that came in since this test's START are done, and says whether + // they are: a server writes a response before the request is done + // (a filter's code after the chain, a request log), so Keploy can + // send END while the test's own code still runs. False, the END is + // cut: what still runs lands after it. + private boolean awaitRequests(String testId, Phases phases) { + if (ServerEntries.countsNone() && NOT_COUNTED_LOGGED.compareAndSet(false, true)) { + log(Level.INFO, notCounted(endWaitMillis, ServerEntries.uncounted()), null); + } + if (endWaitMillis == 0 || RequestGate.running() <= 0) { + return true; + } + boolean done = RequestGate.await(TimeUnit.MILLISECONDS.toNanos(endWaitMillis)); + phases.mark("requests"); + if (!done) { + log(Level.WARNING, "Keploy dedup: " + testId + "'s requests still ran " + endWaitMillis + + " ms after its END, so its record says windowCut and a Keploy that reads it does not" + + " judge the test: what they ran after that is not in it (KEPLOY_JAVA_END_WAIT_MS sets the wait)", null); + } + return done; + } + // describe says how many classes and probes hits holds, and names a few classes. private String describe(List hits) { int probes = 0; @@ -2390,17 +2513,20 @@ private static final class DedupPayload { // Probes-by-class (see class note); serialized under the historical key. private final Map> executedLinesByFile; private final List classMismatch; + private final Boolean windowCut; private DedupPayload(String id, TestCoverage coverage) { this.id = id; this.executedLinesByFile = coverage.probesByClass; this.classMismatch = coverage.classMismatch.isEmpty() ? null : coverage.classMismatch; + this.windowCut = coverage.windowCut ? Boolean.TRUE : null; } } // TestCoverage is a test's coverage as its END read it: the probes hit - // by class, and the classes among them that are not the class the index - // read. + // by class, the classes among them that are not the class the index + // read, and whether the END was cut (its requests still ran at the end + // of its wait). private static final class TestCoverage { static final TestCoverage NONE = new TestCoverage(Collections.>emptyMap(), @@ -2408,10 +2534,21 @@ private static final class TestCoverage { final Map> probesByClass; final List classMismatch; + final boolean windowCut; TestCoverage(Map> probesByClass, List classMismatch) { + this(probesByClass, classMismatch, false); + } + + TestCoverage(Map> probesByClass, List classMismatch, boolean windowCut) { this.probesByClass = probesByClass; this.classMismatch = classMismatch; + this.windowCut = windowCut; + } + + // cut is this coverage, read at the end of a wait its requests outran. + TestCoverage cut() { + return new TestCoverage(probesByClass, classMismatch, true); } } diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/RequestGate.java b/keploy-sdk/src/main/java/io/keploy/dedup/RequestGate.java new file mode 100644 index 00000000..06eb52c0 --- /dev/null +++ b/keploy-sdk/src/main/java/io/keploy/dedup/RequestGate.java @@ -0,0 +1,108 @@ +package io.keploy.dedup; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.LockSupport; + +/** + * RequestGate counts the requests the app's server is running, so that a + * test's END can wait until the requests of that test are done before it + * reads the test's hits. + * + *

    A server writes a request's response before the request is done: a + * filter's code after the chain, a request log, runs after Keploy has the + * response and has sent the test's END. Read at once, the END missed those + * hits, and the next START cleared them as hits between two tests. + * ServerEntries makes each request of Tomcat, Jetty and Undertow call + * {@link #enter()} where the server starts running it and {@link #exit(Object)} + * where it is done with it, the response finished. + * + *

    Public only for the server classes ServerEntries changes, which call it + * from their own packages; not an API. + */ +public final class RequestGate { + + // The window of the test running, made anew at each START and when END + // waits: END waits only for the requests that came in since the START, + // never for one that runs from before (a request of the test before, cut + // at its bound, or a stream that never ends) or one that comes in after + // END. + private static volatile Window current = new Window(); + + private RequestGate() { + } + + /** + * Counts a request as running, in the window of the test running now. + * + * @return the token to pass to {@link #exit(Object)} when it is done + */ + public static Object enter() { + Window window = current; + window.running.incrementAndGet(); + return window; + } + + /** + * Counts the request {@link #enter()} returned the token of as done. + * + * @param token what {@link #enter()} returned + */ + public static void exit(Object token) { + if (token instanceof Window) { + Window window = (Window) token; + if (window.running.decrementAndGet() == 0) { + Thread waiter = window.waiter; + if (waiter != null) { + LockSupport.unpark(waiter); + } + } + } + } + + // open starts the window of a test: from now on, END waits for the + // requests that come in, and for no request that came in before. + static void open() { + current = new Window(); + } + + // running is how many requests of the current window are running. + static int running() { + return current.running.get(); + } + + // await closes the current window and waits until none of its requests + // runs, for at most timeoutNanos, and says whether none runs. A request + // that comes in from now on (a readiness probe, a metrics scrape, the + // next client) counts in a window of its own, so it does not draw the + // wait out. One thread waits at a time (the worker, under its test lock). + static boolean await(long timeoutNanos) { + Window window = current; + current = new Window(); + if (window.running.get() <= 0) { + return true; + } + // Set before the count is read again: an exit that takes the count + // to 0 after that read finds the waiter and wakes it, and one that + // took it to 0 before is seen by the read. + window.waiter = Thread.currentThread(); + try { + long deadline = System.nanoTime() + timeoutNanos; + while (window.running.get() > 0) { + long left = deadline - System.nanoTime(); + if (left <= 0 || Thread.currentThread().isInterrupted()) { + return false; + } + LockSupport.parkNanos(RequestGate.class, left); + } + return true; + } finally { + window.waiter = null; + } + } + + private static final class Window { + + private final AtomicInteger running = new AtomicInteger(); + private volatile Thread waiter; + } +} diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/ServerEntries.java b/keploy-sdk/src/main/java/io/keploy/dedup/ServerEntries.java new file mode 100644 index 00000000..99104408 --- /dev/null +++ b/keploy-sdk/src/main/java/io/keploy/dedup/ServerEntries.java @@ -0,0 +1,328 @@ +package io.keploy.dedup; + +import java.lang.instrument.ClassFileTransformer; +import java.security.ProtectionDomain; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; + +/** + * ServerEntries makes each request of Tomcat, Jetty and Undertow count in + * {@link RequestGate} while the server runs it: the method each server runs + * a request in (and each async dispatch of it) becomes a method of the same + * name and access that calls {@link RequestGate#enter()}, then the server's + * own code, renamed {@code keploy$request$} and private, then + * {@link RequestGate#exit(Object)}, however it returns or throws. Nothing + * else in the server's code changes. + * + *

    Only classes loaded from now on change (keploy-sdk.jar asks for no + * retransform), so a server started before the SDK is not counted, and + * neither is one whose class loader does not see keploy-sdk.jar's classes, + * as in a module system (both said in the log). A server in a named module + * (Tomcat's jar on the module path) is counted: the JVM makes a module whose + * class an agent changed read the class path's classes. Netty, and so + * WebFlux, is not counted: END then reads a test's hits as soon as it comes. + */ +final class ServerEntries implements ClassFileTransformer { + + private static final Logger LOGGER = Logger.getLogger(KeployDedupAgent.class.getName()); + private static final String GATE = Type.getInternalName(RequestGate.class); + static final String RENAMED = "keploy$request$"; + + // The servers' request entries: the class, and the methods a request + // runs in, the response finished when they return. + private static final Map ENTRIES = new HashMap<>(); + + static { + // Tomcat 8.5 to 11 (Spring Boot's default): service runs a request, + // filters, servlet and finished response; asyncDispatch each later + // dispatch of an async one. + add("Tomcat", "org/apache/catalina/connector/CoyoteAdapter", "service", "asyncDispatch"); + // Jetty 9.4 to 11: handle runs a request and each async dispatch of it. + add("Jetty", "org/eclipse/jetty/server/HttpChannel", "handle"); + // Jetty 12, servlets of each EE level (12.1's EE 11 too, as Spring + // Boot 4 runs it). Its core HttpChannel is an interface, so it is + // passed over as the 9.4-11 entry. + add("Jetty", "org/eclipse/jetty/ee11/servlet/ServletChannel", "handle"); + add("Jetty", "org/eclipse/jetty/ee10/servlet/ServletChannel", "handle"); + add("Jetty", "org/eclipse/jetty/ee9/nested/HttpChannel", "handle"); + add("Jetty", "org/eclipse/jetty/ee8/nested/HttpChannel", "handle"); + // Undertow: each dispatch of a request, on the IO thread and on the + // worker that runs its servlet chain. + add("Undertow", "io/undertow/server/Connectors", "executeRootHandler"); + } + + // Classes of servers that are not counted, so that the log can name them. + private static final Map UNCOUNTED = new HashMap<>(); + + static { + UNCOUNTED.put("io/netty/handler/codec/http/HttpServerCodec", "Netty"); + UNCOUNTED.put("reactor/netty/http/server/HttpServer", "Reactor Netty"); + } + + private static final Set COUNTED = new CopyOnWriteArraySet<>(); + private static final Set SEEN_UNCOUNTED = new CopyOnWriteArraySet<>(); + + private static void add(String server, String className, String... methods) { + ENTRIES.put(className, new Entry(server, new HashSet<>(Arrays.asList(methods)))); + } + + // counted names the servers whose requests count, in the order their + // classes loaded; empty when none did. + static List counted() { + return new ArrayList<>(COUNTED); + } + + // countsNone says whether no server's requests count. + static boolean countsNone() { + return COUNTED.isEmpty(); + } + + // uncounted names the servers whose requests do not count that the app + // loaded, as Netty. + static List uncounted() { + return new ArrayList<>(SEEN_UNCOUNTED); + } + + @Override + public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, + ProtectionDomain protectionDomain, byte[] classfileBuffer) { + if (classBeingRedefined != null) { + return null; + } + Entry entry = ENTRIES.get(className); + if (entry == null) { + String uncounted = UNCOUNTED.get(className); + if (uncounted != null) { + SEEN_UNCOUNTED.add(uncounted); + } + return null; + } + String name = className.replace('/', '.'); + try { + if (isInterface(classfileBuffer)) { + // Jetty 12's core HttpChannel: no code of its own to count, + // and its requests count in a ServletChannel. + LOGGER.log(Level.FINE, "Keploy dedup passes over " + name + ": an interface"); + return null; + } + if (!seesGate(loader)) { + LOGGER.log(Level.WARNING, "Keploy dedup does not count " + entry.server + "'s requests: the" + + " class loader of " + name + " does not see keploy-sdk.jar's classes, so a test's END" + + " reads its hits without waiting for its requests to finish"); + return null; + } + byte[] wrapped = wrap(classfileBuffer, entry.methods); + if (wrapped == null) { + LOGGER.log(Level.WARNING, "Keploy dedup does not count " + entry.server + "'s requests: " + + name + " has no method " + entry.methods + " to count them in"); + return null; + } + COUNTED.add(entry.server); + LOGGER.log(Level.INFO, "Keploy dedup counts " + entry.server + "'s requests (" + name + "): a" + + " test's END waits for them to finish"); + return wrapped; + } catch (Throwable t) { + // Never fail the server's class: it loads unchanged. + LOGGER.log(Level.WARNING, "Keploy dedup does not count " + entry.server + "'s requests: " + name + + " could not be changed", t); + return null; + } + } + + // seesGate says whether code of a class loader defines calls the + // RequestGate END reads: one it cannot see would fail each request, and + // a copy of its own would count where END never looks. + private static boolean seesGate(ClassLoader loader) { + // The bootstrap loader (null) does not see it either. + try { + return Class.forName(RequestGate.class.getName(), false, loader) == RequestGate.class; + } catch (Throwable t) { + return false; + } + } + + private static boolean isInterface(byte[] classfile) { + return (new ClassReader(classfile).getAccess() & Opcodes.ACC_INTERFACE) != 0; + } + + // wrap returns the class with each method of these names counted, or + // null when it has none to count (abstract and native methods only). + // transform passes over an interface before it. + static byte[] wrap(byte[] classfile, Set methods) { + ClassReader reader = new ClassReader(classfile); + ClassWriter writer = new ClassWriter(reader, 0); + Wrapper wrapper = new Wrapper(writer, methods); + reader.accept(wrapper, 0); + return wrapper.wrapped.isEmpty() ? null : writer.toByteArray(); + } + + private static final class Entry { + + private final String server; + private final Set methods; + + private Entry(String server, Set methods) { + this.server = server; + this.methods = Collections.unmodifiableSet(methods); + } + } + + // Wrapper renames each method to count and, at the class's end, adds + // the method that counts it under its old name and access. + private static final class Wrapper extends ClassVisitor { + + private static final int NOT_COUNTED = Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE | Opcodes.ACC_BRIDGE + | Opcodes.ACC_SYNTHETIC; + + private final Set methods; + private final List wrapped = new ArrayList<>(); + private String owner; + private int version; + + private Wrapper(ClassVisitor next, Set methods) { + super(Opcodes.ASM9, next); + this.methods = methods; + } + + @Override + public void visit(int version, int access, String name, String signature, String superName, + String[] interfaces) { + this.version = version & 0xFFFF; + this.owner = name; + super.visit(version, access, name, signature, superName, interfaces); + } + + @Override + public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, + String[] exceptions) { + if (!methods.contains(name) || (access & NOT_COUNTED) != 0) { + return super.visitMethod(access, name, descriptor, signature, exceptions); + } + wrapped.add(new Method(access, name, descriptor, signature, exceptions)); + int renamed = (access & ~(Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) | Opcodes.ACC_PRIVATE + | Opcodes.ACC_SYNTHETIC; + return super.visitMethod(renamed, RENAMED + name, descriptor, signature, exceptions); + } + + @Override + public void visitEnd() { + for (Method method : wrapped) { + counting(method); + } + super.visitEnd(); + } + + // counting adds, under the method's name and access, a method that + // counts the request in RequestGate around a call of the renamed one. + private void counting(Method method) { + int access = method.access & ~Opcodes.ACC_SYNCHRONIZED; + boolean isStatic = (access & Opcodes.ACC_STATIC) != 0; + Type[] args = Type.getArgumentTypes(method.descriptor); + Type returns = Type.getReturnType(method.descriptor); + MethodVisitor mv = super.visitMethod(access, method.name, method.descriptor, method.signature, + method.exceptions); + mv.visitCode(); + Label start = new Label(); + Label end = new Label(); + Label thrown = new Label(); + mv.visitTryCatchBlock(start, end, thrown, null); + + List locals = new ArrayList<>(); + int slot = 0; + if (!isStatic) { + locals.add(owner); + slot++; + } + for (Type arg : args) { + locals.add(frameType(arg)); + slot += arg.getSize(); + } + int token = slot; + locals.add("java/lang/Object"); + + mv.visitMethodInsn(Opcodes.INVOKESTATIC, GATE, "enter", "()Ljava/lang/Object;", false); + mv.visitVarInsn(Opcodes.ASTORE, token); + mv.visitLabel(start); + slot = 0; + if (!isStatic) { + mv.visitVarInsn(Opcodes.ALOAD, 0); + slot++; + } + for (Type arg : args) { + mv.visitVarInsn(arg.getOpcode(Opcodes.ILOAD), slot); + slot += arg.getSize(); + } + mv.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKESPECIAL, owner, + RENAMED + method.name, method.descriptor, false); + mv.visitLabel(end); + mv.visitVarInsn(Opcodes.ALOAD, token); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, GATE, "exit", "(Ljava/lang/Object;)V", false); + mv.visitInsn(returns.getOpcode(Opcodes.IRETURN)); + + mv.visitLabel(thrown); + if (version >= Opcodes.V1_6) { + mv.visitFrame(Opcodes.F_FULL, locals.size(), locals.toArray(), 1, + new Object[] {"java/lang/Throwable"}); + } + mv.visitVarInsn(Opcodes.ALOAD, token); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, GATE, "exit", "(Ljava/lang/Object;)V", false); + mv.visitInsn(Opcodes.ATHROW); + mv.visitMaxs(Math.max(Math.max(slot, returns.getSize() + 1), 2), token + 1); + mv.visitEnd(); + } + + // frameType is how a stack map frame names a local of this type. + private static Object frameType(Type type) { + switch (type.getSort()) { + case Type.BOOLEAN: + case Type.BYTE: + case Type.CHAR: + case Type.SHORT: + case Type.INT: + return Opcodes.INTEGER; + case Type.FLOAT: + return Opcodes.FLOAT; + case Type.LONG: + return Opcodes.LONG; + case Type.DOUBLE: + return Opcodes.DOUBLE; + default: + return type.getInternalName(); + } + } + } + + private static final class Method { + + private final int access; + private final String name; + private final String descriptor; + private final String signature; + private final String[] exceptions; + + private Method(int access, String name, String descriptor, String signature, String[] exceptions) { + this.access = access; + this.name = name; + this.descriptor = descriptor; + this.signature = signature; + this.exceptions = exceptions; + } + } +} diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/CoverageTcpClientTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/CoverageTcpClientTest.java index 6e9c3324..20571cb0 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/CoverageTcpClientTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/CoverageTcpClientTest.java @@ -34,6 +34,7 @@ public class CoverageTcpClientTest { private static final String WARMUP_DISABLED_PROPERTY = "keploy.java.dedup.warmup.disabled"; private static final String CLASS_DIRS_PROPERTY = "keploy.java.class.dirs"; private static final String BUILD_TAG_PROPERTY = "keploy.build.tag"; + private static final String END_WAIT_PROPERTY = "keploy.java.end.wait.ms"; @Rule public TemporaryFolder tmp = new TemporaryFolder(); @@ -58,6 +59,7 @@ public void tearDown() throws IOException { System.clearProperty(WARMUP_DISABLED_PROPERTY); System.clearProperty(CLASS_DIRS_PROPERTY); System.clearProperty(BUILD_TAG_PROPERTY); + System.clearProperty(END_WAIT_PROPERTY); collector.close(); if (jacoco != null) { jacoco.close(); @@ -210,6 +212,45 @@ public void theManifestWritesAClassIdAs16HexDigitsAndTheCovNamesAClassJacocoSawO } } + @Test + public void aCutEndSaysSoInItsCovAfterClassMismatch() throws Exception { + File shop = new File(tmp.newFolder("classes"), "com/acme/Shop.class"); + shop.getParentFile().mkdirs(); + Files.write(shop.toPath(), new byte[] {1}); + System.setProperty(CLASS_DIRS_PROPERTY, shop.getParentFile().getParentFile().getParent()); + System.setProperty(END_WAIT_PROPERTY, "50"); + jacoco = new FakeJacoco(); + System.setProperty(JACOCO_PORT_PROPERTY, String.valueOf(jacoco.server.getLocalPort())); + jacoco.hit = new ExecutionData(0x99L, "com/acme/Shop", new boolean[] {true, false}); + assertTrue(KeployDedupAgent.start()); + try (Socket sdk = collector.accept()) { + sdk.setSoTimeout(30000); + OutputStream out = sdk.getOutputStream(); + BufferedReader in = new BufferedReader( + new InputStreamReader(sdk.getInputStream(), StandardCharsets.UTF_8)); + + send(out, "START test-set-0/test-1"); + assertEquals("ACK", in.readLine()); + // The test's request is still running when its END comes, and + // past END's wait. + Object request = RequestGate.enter(); + try { + send(out, "END test-set-0/test-1"); + assertEquals("COV {\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"com/acme/Shop\":[0]}," + + "\"classMismatch\":[\"com/acme/Shop\"],\"windowCut\":true}", in.readLine()); + assertEquals("ACK", in.readLine()); + } finally { + RequestGate.exit(request); + } + // Its request done, the next test's END is not cut. + send(out, "START test-set-0/test-2"); + assertEquals("ACK", in.readLine()); + send(out, "END test-set-0/test-2"); + String cov = in.readLine(); + assertTrue(cov, cov.startsWith("COV ") && !cov.contains("windowCut")); + } + } + private static void send(OutputStream out, String command) throws IOException { out.write((command + "\n").getBytes(StandardCharsets.UTF_8)); out.flush(); diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/DedupWireFormatTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/DedupWireFormatTest.java index c44f1adc..6877a7b4 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/DedupWireFormatTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/DedupWireFormatTest.java @@ -57,6 +57,19 @@ public void covPayloadNamesTheMismatchedClassesAfterTheProbesAndOnlyWhenThereAre KeployDedupAgent.toDedupJson("t", probes, Collections.emptyList())); } + @Test + public void aCutEndsFlagSitsAfterClassMismatchAndIsLeftOutWhenFalse() { + Map> probes = new LinkedHashMap<>(); + probes.put("com/foo/Bar", Arrays.asList(0, 1)); + assertEquals("{\"id\":\"t\",\"executedLinesByFile\":{\"com/foo/Bar\":[0,1]}," + + "\"classMismatch\":[\"com/foo/Bar\"],\"windowCut\":true}", + KeployDedupAgent.toDedupJson("t", probes, Collections.singletonList("com/foo/Bar"), true)); + assertEquals("{\"id\":\"t\",\"executedLinesByFile\":{\"com/foo/Bar\":[0,1]},\"windowCut\":true}", + KeployDedupAgent.toDedupJson("t", probes, Collections.emptyList(), true)); + assertEquals("{\"id\":\"t\",\"executedLinesByFile\":{\"com/foo/Bar\":[0,1]}}", + KeployDedupAgent.toDedupJson("t", probes, Collections.emptyList(), false)); + } + @Test public void covPayloadWithNoCoverageSerializesEmptyMap() { String json = KeployDedupAgent.toDedupJson( diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java index 84f3c085..9d2f32e3 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java @@ -19,6 +19,7 @@ import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -53,7 +54,7 @@ public class InProcessScanTest { private static final String[] PROPERTIES = {"keploy.java.dedup.diagnostics", "keploy.java.class.dirs", "keploy.jacoco.port", "keploy.coverage.endpoint", "keploy.java.dedup.warmup.disabled", - "keploy.build.tag", "keploy.java.scan"}; + "keploy.build.tag", "keploy.java.scan", "keploy.java.end.wait.ms"}; private static final String TAKEN = "ACK proto=2\n"; private static final String IGNORED = "IGNORED proto=2\n"; private static final String SHOP = "com/acme/Shop"; @@ -178,6 +179,7 @@ public void eachTestGetsItsOwnHitsAndNoneOfTheBackground() throws Exception { data.next()); String end = logged(" END test-set-0/test-1 took "); assertTrue(end, end.contains(" scan=") && end.contains(" filter=") && !end.contains("dump=")); + assertFalse("no request ran: END did not wait", end.contains(" requests=")); assertTrue("a library class is left alone", lib[0]); // Nothing ran between the tests, or in the second one. @@ -906,6 +908,231 @@ protected Class findClass(String name) throws ClassNotFoundException { return Class.forName("test.BrokenAgent", true, loader).getConstructor().newInstance(); } + // Request is a request of the app's server, as ServerEntries counts it: + // in RequestGate from when it comes in until it is done, on a thread of + // its own. Its response reaches Keploy (responded) before it is done: + // tail runs after it, once release opens. + private static final class Request implements Closeable { + + private final CountDownLatch responded = new CountDownLatch(1); + private final CountDownLatch release = new CountDownLatch(1); + private final CountDownLatch done = new CountDownLatch(1); + private final Thread thread; + + Request(final Runnable before, final Runnable tail) { + thread = new Thread(new Runnable() { + @Override + public void run() { + Object token = RequestGate.enter(); + try { + before.run(); + responded.countDown(); + release.await(); + tail.run(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + RequestGate.exit(token); + done.countDown(); + } + } + }, "app-request"); + thread.setDaemon(true); + thread.start(); + } + + void awaitResponse() throws InterruptedException { + assertTrue(responded.await(10, TimeUnit.SECONDS)); + } + + // finishOnceEndWaits lets the tail run, from another thread, once a + // thread (the END the caller sends) waits in RequestGate, or after + // 10 s; so the END reads the hits only if it waited for the tail. + void finishOnceEndWaits() { + Thread later = new Thread(new Runnable() { + @Override + public void run() { + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10); + while (!endWaits() && System.nanoTime() < deadline) { + Thread.yield(); + } + release.countDown(); + } + }); + later.setDaemon(true); + later.start(); + } + + void finish() throws InterruptedException { + release.countDown(); + assertTrue(done.await(10, TimeUnit.SECONDS)); + } + + @Override + public void close() { + release.countDown(); + thread.interrupt(); + } + } + + private static Runnable hit(final boolean[] probes, final int... indices) { + return new Runnable() { + @Override + public void run() { + for (int index : indices) { + probes[index] = true; + } + } + }; + } + + @Test + public void anEndWaitsForItsTestsRequestToFinishAfterItsResponse() throws Exception { + // A filter's code after the chain runs after Keploy has the response + // and sent END: read at once, END missed it, and the next START + // cleared it as background, so the test's hits changed run to run. + System.setProperty("keploy.java.end.wait.ms", "10000"); + System.setProperty("keploy.java.dedup.diagnostics", "true"); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("BACKGROUND")); + try (Request request = new Request(hit(shop, 1), hit(shop, 2))) { + request.awaitResponse(); + request.finishOnceEndWaits(); + long began = System.nanoTime(); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertTrue("END ended when the request did, not at its bound", + System.nanoTime() - began < TimeUnit.SECONDS.toNanos(5)); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1,2]}}", + data.next()); + assertTrue(logged(" END test-set-0/test-1 took ").contains(" requests=")); + } + assertEquals(TAKEN, send("START test-set-0/test-2")); + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals("{\"id\":\"test-set-0/test-2\",\"executedLinesByFile\":{}}", data.next()); + assertNull("the tail is test-1's, not background", data.background.poll()); + } + assertEquals(0, count("windowCut")); + } + + @Test + public void anEndWhoseRequestOutrunsTheWaitIsCutAndOpensNoWindow() throws Exception { + System.setProperty("keploy.java.end.wait.ms", "50"); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); + boolean[] cart = agent.probes(CART_ID, CART, 3); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + assertEquals(TAKEN, send("BACKGROUND")); + try (Request request = new Request(hit(shop, 1), hit(shop, 2))) { + request.awaitResponse(); + long began = System.nanoTime(); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertTrue("END waited its bound", + System.nanoTime() - began >= TimeUnit.MILLISECONDS.toNanos(50)); + // What it read at the cut, and the flag, after classMismatch. + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1]}," + + "\"windowCut\":true}", data.next()); + assertNotNull(logged("test-set-0/test-1's requests still ran 50 ms after its END, so its" + + " record says windowCut")); + // The tail runs after the END: the cut test's own late hit. + request.finish(); + } + assertEquals(TAKEN, send("START test-set-0/test-2")); + assertFalse("cleared", shop[2]); + assertEquals(TAKEN, send("END test-set-0/test-2")); + assertEquals("{\"id\":\"test-set-0/test-2\",\"executedLinesByFile\":{}}", data.next()); + // The windows after it count as usual. + cart[1] = true; + assertEquals(TAKEN, send("START test-set-0/test-3")); + assertEquals(TAKEN, send("END test-set-0/test-3")); + assertEquals("{\"id\":\"test-set-0/test-3\",\"executedLinesByFile\":{}}", data.next()); + assertEquals("a START after a cut END is no window: test-1's late hit is not background", + background("test-set-0/test-2", "test-set-0/test-3", probes(CART, "1")), data.nextBackground()); + assertNull(data.background.poll()); + } + } + + @Test + public void anEndDoesNotWaitForARequestThatCameInBeforeItsStart() throws Exception { + // A stream opened before the test, or a request of the test before + // cut at its bound: never this test's to wait for. + System.setProperty("keploy.java.end.wait.ms", "20000"); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath); + Request stream = new Request(hit(shop, 0), hit(shop, 3))) { + stream.awaitResponse(); + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + shop[1] = true; + long began = System.nanoTime(); + assertEquals(TAKEN, send("END test-set-0/test-1")); + assertTrue(System.nanoTime() - began < TimeUnit.SECONDS.toNanos(10)); + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1]}}", + data.next()); + } + } + + @Test + public void withAWaitOfZeroAnEndReadsAtOnceAsBefore() throws Exception { + System.setProperty("keploy.java.end.wait.ms", "0"); + boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); + try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { + assertTrue(KeployDedupAgent.start()); + assertEquals(TAKEN, send("START test-set-0/test-1")); + try (Request request = new Request(hit(shop, 1), hit(shop, 2))) { + request.awaitResponse(); + assertEquals(TAKEN, send("END test-set-0/test-1")); + // The tail is lost to this test, and the record has no flag. + assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1]}}", + data.next()); + request.finish(); + } + } + assertEquals(0, count("windowCut")); + } + + @Test + public void theEndWaitIsReadFromItsSetting() throws ReflectiveOperationException { + ((java.util.concurrent.atomic.AtomicBoolean) read(KeployDedupAgent.class, null, "END_WAIT_INVALID_LOGGED")) + .set(false); + ((java.util.concurrent.atomic.AtomicBoolean) read(KeployDedupAgent.class, null, "END_WAIT_CAPPED_LOGGED")) + .set(false); + assertEquals(200, KeployDedupAgent.endWaitMillis()); + System.setProperty("keploy.java.end.wait.ms", " 0 "); + assertEquals(0, KeployDedupAgent.endWaitMillis()); + System.setProperty("keploy.java.end.wait.ms", "1500"); + assertEquals(1500, KeployDedupAgent.endWaitMillis()); + System.setProperty("keploy.java.end.wait.ms", "10000"); + assertEquals(10000, KeployDedupAgent.endWaitMillis()); + assertEquals(0, count("its most")); + System.setProperty("keploy.java.end.wait.ms", "10001"); + assertEquals(10000, KeployDedupAgent.endWaitMillis()); + System.setProperty("keploy.java.end.wait.ms", "60000"); + assertEquals(10000, KeployDedupAgent.endWaitMillis()); + assertEquals("said once in a JVM", 1, + count("KEPLOY_JAVA_END_WAIT_MS is 10001, so a test's END waits up to 10000 ms, its most")); + assertEquals(1, count("its most")); + System.setProperty("keploy.java.end.wait.ms", "-1"); + assertEquals(200, KeployDedupAgent.endWaitMillis()); + System.setProperty("keploy.java.end.wait.ms", "soon"); + assertEquals(200, KeployDedupAgent.endWaitMillis()); + assertEquals(200, KeployDedupAgent.endWaitMillis()); + assertEquals("said once in a JVM", 1, count("KEPLOY_JAVA_END_WAIT_MS is not a number")); + } + + // endWaits says whether a thread waits in RequestGate. + private static boolean endWaits() { + for (Thread thread : Thread.getAllStackTraces().keySet()) { + if (java.util.concurrent.locks.LockSupport.getBlocker(thread) == RequestGate.class) { + return true; + } + } + return false; + } + // scanOf is the ProbeScan the running agent's collector looked up. private static ProbeScan scanOf() throws ReflectiveOperationException { Object worker = read(KeployDedupAgent.class, null, "coverageWorker"); diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/RequestGateTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/RequestGateTest.java new file mode 100644 index 00000000..4e6f5fd3 --- /dev/null +++ b/keploy-sdk/src/test/java/io/keploy/dedup/RequestGateTest.java @@ -0,0 +1,185 @@ +package io.keploy.dedup; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collections; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.Before; +import org.junit.Test; + +/** + * RequestGate counts the requests of the test running: END waits for them + * until the last one is done, or its bound, and never for one that came in + * before the test's START. + */ +public class RequestGateTest { + + @Before + public void setUp() { + RequestGate.open(); + } + + @Test + public void withNoRequestRunningTheWaitEndsAtOnce() { + long began = System.nanoTime(); + assertTrue(RequestGate.await(TimeUnit.SECONDS.toNanos(20))); + assertTrue(System.nanoTime() - began < TimeUnit.SECONDS.toNanos(10)); + } + + // A wait that never ends fails the row, not the run. + @Test(timeout = 60000) + public void aRequestStillRunningAtTheBoundCutsTheWait() { + Object request = RequestGate.enter(); + try { + long began = System.nanoTime(); + assertFalse(RequestGate.await(TimeUnit.MILLISECONDS.toNanos(50))); + assertTrue(System.nanoTime() - began >= TimeUnit.MILLISECONDS.toNanos(50)); + // The END closed the test's window: what comes in now is not its. + assertEquals(0, RequestGate.running()); + } finally { + RequestGate.exit(request); + } + assertEquals(0, RequestGate.running()); + } + + @Test + public void aRequestThatComesInWhileEndWaitsIsNotWaitedFor() throws Exception { + // A readiness probe or a scrape that comes in during the wait is not + // the test's: the wait ends when the test's own request is done. + final Object tests = RequestGate.enter(); + final Object[] probe = new Object[1]; + final Thread waiter = Thread.currentThread(); + Thread server = new Thread(new Runnable() { + @Override + public void run() { + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30); + while (java.util.concurrent.locks.LockSupport.getBlocker(waiter) != RequestGate.class + && System.nanoTime() < deadline) { + Thread.yield(); + } + probe[0] = RequestGate.enter(); + RequestGate.exit(tests); + } + }); + server.start(); + long began = System.nanoTime(); + assertTrue(RequestGate.await(TimeUnit.SECONDS.toNanos(60))); + assertTrue(System.nanoTime() - began < TimeUnit.SECONDS.toNanos(30)); + server.join(); + assertEquals("the probe counts in the window after the END", 1, RequestGate.running()); + RequestGate.exit(probe[0]); + assertEquals(0, RequestGate.running()); + } + + @Test + public void theWaitEndsWhenTheLastRequestIsDoneNotAtItsBound() throws Exception { + final Object first = RequestGate.enter(); + final Object second = RequestGate.enter(); + Thread finishing = new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(50); + RequestGate.exit(first); + Thread.sleep(50); + RequestGate.exit(second); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + }); + finishing.start(); + long began = System.nanoTime(); + assertTrue(RequestGate.await(TimeUnit.SECONDS.toNanos(60))); + long took = System.nanoTime() - began; + assertTrue("woken when the count reached 0, not at the bound: " + took, + took < TimeUnit.SECONDS.toNanos(30)); + assertTrue("not before the second was done", took >= TimeUnit.MILLISECONDS.toNanos(90)); + finishing.join(); + } + + @Test + public void aRequestFromBeforeTheTestsStartIsNotWaitedFor() { + Object before = RequestGate.enter(); + try { + RequestGate.open(); + assertEquals(0, RequestGate.running()); + assertTrue(RequestGate.await(TimeUnit.SECONDS.toNanos(20))); + // Done after the START: it does not count in the new window. + Object during = RequestGate.enter(); + RequestGate.exit(before); + before = null; + assertEquals(1, RequestGate.running()); + RequestGate.exit(during); + assertEquals(0, RequestGate.running()); + } finally { + RequestGate.exit(before); + } + } + + @Test + public void anInterruptedWaiterStopsWaiting() { + Object request = RequestGate.enter(); + Thread.currentThread().interrupt(); + try { + long began = System.nanoTime(); + assertFalse(RequestGate.await(TimeUnit.SECONDS.toNanos(60))); + assertTrue(System.nanoTime() - began < TimeUnit.SECONDS.toNanos(30)); + } finally { + Thread.interrupted(); + RequestGate.exit(request); + } + } + + @Test + public void anExitWithNoTokenOfEnterCountsNothing() { + Object request = RequestGate.enter(); + RequestGate.exit(null); + RequestGate.exit("not a token"); + assertEquals(1, RequestGate.running()); + RequestGate.exit(request); + assertEquals(0, RequestGate.running()); + } + + @Test + public void manyThreadsCountExactly() throws Exception { + final AtomicBoolean failed = new AtomicBoolean(); + Thread[] threads = new Thread[8]; + for (int i = 0; i < threads.length; i++) { + threads[i] = new Thread(new Runnable() { + @Override + public void run() { + for (int n = 0; n < 20000; n++) { + Object token = RequestGate.enter(); + if (RequestGate.running() <= 0) { + failed.set(true); + } + RequestGate.exit(token); + } + } + }); + threads[i].start(); + } + for (Thread thread : threads) { + thread.join(); + } + assertFalse(failed.get()); + assertEquals(0, RequestGate.running()); + } + + @Test + public void theLogSaysWhyAnEndDoesNotWait() { + assertTrue(KeployDedupAgent.notCounted(0, Arrays.asList("Netty")).startsWith( + "Keploy dedup: a test's END does not wait for its requests to finish: KEPLOY_JAVA_END_WAIT_MS is 0.")); + assertTrue(KeployDedupAgent.notCounted(200, Arrays.asList("Netty")).startsWith( + "Keploy dedup: a test's END does not wait for its requests to finish: the app's server is Netty," + + " whose requests keploy-sdk does not count.")); + assertTrue(KeployDedupAgent.notCounted(200, Collections.emptyList()).startsWith( + "Keploy dedup: a test's END does not wait for its requests to finish: no Tomcat, Jetty or" + + " Undertow request entry loaded after keploy-sdk.jar started")); + } +} diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java new file mode 100644 index 00000000..6c9b96d3 --- /dev/null +++ b/keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java @@ -0,0 +1,402 @@ +package io.keploy.dedup; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; + +/** + * ServerEntries counts each request of Tomcat, Jetty and Undertow in + * RequestGate from the server's request entry, however it returns or throws, + * and changes no other method, and no class whose loader does not see the + * RequestGate END reads. Here on stand-ins of the servers' classes; the + * servers themselves run in ServerTailTest. + */ +public class ServerEntriesTest { + + private static final String BODY_DESCRIPTOR = "(Ljava/lang/Object;JDI[IZBCSF)J"; + private static final String[][] ENTRIES = { + {"org/apache/catalina/connector/CoyoteAdapter", "service", "Tomcat"}, + {"org/apache/catalina/connector/CoyoteAdapter", "asyncDispatch", "Tomcat"}, + {"org/eclipse/jetty/server/HttpChannel", "handle", "Jetty"}, + {"org/eclipse/jetty/ee10/servlet/ServletChannel", "handle", "Jetty"}, + {"org/eclipse/jetty/ee9/nested/HttpChannel", "handle", "Jetty"}, + {"org/eclipse/jetty/ee8/nested/HttpChannel", "handle", "Jetty"}, + {"io/undertow/server/Connectors", "executeRootHandler", "Undertow"}, + {"org/eclipse/jetty/ee11/servlet/ServletChannel", "handle", "Jetty"}, + }; + + // What body saw: the requests running while the server's code ran. + private static volatile int runningInBody = -1; + + private final List logged = new CopyOnWriteArrayList<>(); + private final Handler handler = new Handler() { + @Override + public void publish(LogRecord record) { + logged.add(record.getLevel() + " " + record.getMessage()); + } + + @Override + public void flush() { + } + + @Override + public void close() { + } + }; + + @Before + public void setUp() { + Logger.getLogger(KeployDedupAgent.class.getName()).addHandler(handler); + // A window of this test's own: no request of another test counts. + RequestGate.open(); + } + + @After + public void tearDown() { + Logger.getLogger(KeployDedupAgent.class.getName()).removeHandler(handler); + } + + // body is the server's own code in each stand-in: it says how many + // requests run, and throws when told to. + public static long body(Object first, long second, double third, int fourth, int[] fifth, boolean sixth, + byte seventh, char eighth, short ninth, float tenth) { + runningInBody = RequestGate.running(); + if (fourth < 0) { + throw new IllegalStateException("the server's code threw"); + } + return second + (long) third + fourth + fifth.length + (first == null ? 0 : 1) + (sixth ? 1 : 0) + + seventh + eighth + ninth + (long) tenth; + } + + // tick and count are the server's code of entries with no arguments. + public static void tick() { + runningInBody = RequestGate.running(); + } + + public static long count() { + runningInBody = RequestGate.running(); + return 7L; + } + + private static final Class[] BODY_TYPES = {Object.class, long.class, double.class, int.class, int[].class, + boolean.class, byte.class, char.class, short.class, float.class}; + + private static Object[] args(int fourth) { + return new Object[] {"x", 2L, 3.5d, fourth, new int[2], true, (byte) 1, (char) 1, (short) 1, 1.5f}; + } + + @Test + public void eachServersEntryCountsItsRequestWhileItRunsAndWhenItThrows() throws Exception { + for (String[] entry : ENTRIES) { + for (boolean isStatic : new boolean[] {false, true}) { + String label = entry[0] + "." + entry[1] + (isStatic ? " (static)" : ""); + Class type = load(entry[0], transformed(standIn(entry[0], entry[1], isStatic, Opcodes.V1_8, 0))); + Object owner = isStatic ? null : type.getDeclaredConstructor().newInstance(); + Method method = type.getMethod(entry[1], BODY_TYPES); + assertEquals(label, isStatic, Modifier.isStatic(method.getModifiers())); + + runningInBody = -1; + assertEquals(label, 0, RequestGate.running()); + assertEquals(label, 2L + 3L + 4L + 2L + 1L + 1L + 1L + 1L + 1L + 1L, method.invoke(owner, args(4))); + assertEquals(label + ": counted while the server ran it", 1, runningInBody); + assertEquals(label + ": done when it returned", 0, RequestGate.running()); + + try { + method.invoke(owner, args(-1)); + fail(label); + } catch (InvocationTargetException e) { + assertEquals(label, IllegalStateException.class, e.getCause().getClass()); + } + assertEquals(label + ": done when it threw", 0, RequestGate.running()); + + // The server's own code is still there, renamed and private. + Method renamed = type.getDeclaredMethod(ServerEntries.RENAMED + entry[1], BODY_TYPES); + assertTrue(label, Modifier.isPrivate(renamed.getModifiers()) && renamed.isSynthetic()); + assertTrue(label, ServerEntries.counted().contains(entry[2])); + } + } + } + + @Test + public void noOtherMethodIsCounted() throws Exception { + Class type = load(ENTRIES[0][0], transformed(standIn(ENTRIES[0][0], "service", false, Opcodes.V1_8, 0))); + Method other = type.getMethod("log", BODY_TYPES); + runningInBody = -1; + other.invoke(type.getDeclaredConstructor().newInstance(), args(0)); + assertEquals(0, runningInBody); + } + + @Test + public void theCountsHandlerHasItsFrameFromJava6On() throws Exception { + // A Java 6 class may go without frames only because HotSpot falls + // back to the old verifier for it: give it its frame as Java 7's. + for (int version : new int[] {Opcodes.V1_5, Opcodes.V1_6, Opcodes.V1_7}) { + final int[] frames = new int[1]; + byte[] out = transformed(standIn(ENTRIES[2][0], "handle", false, version, 0)); + new org.objectweb.asm.ClassReader(out).accept(new org.objectweb.asm.ClassVisitor(Opcodes.ASM9) { + @Override + public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, + String[] exceptions) { + if (!name.equals("handle")) { + return null; + } + return new MethodVisitor(Opcodes.ASM9) { + @Override + public void visitFrame(int type, int locals, Object[] local, int stack, Object[] stackItems) { + frames[0]++; + } + }; + } + }, 0); + assertEquals("frames of a class of version " + version, version >= Opcodes.V1_6 ? 1 : 0, frames[0]); + } + } + + @Test + public void aClassFromBeforeStackMapsIsCountedToo() throws Exception { + Class type = load(ENTRIES[2][0], transformed(standIn(ENTRIES[2][0], "handle", false, Opcodes.V1_5, 0))); + Method method = type.getMethod("handle", BODY_TYPES); + runningInBody = -1; + method.invoke(type.getDeclaredConstructor().newInstance(), args(0)); + assertEquals(1, runningInBody); + assertEquals(0, RequestGate.running()); + } + + @Test + public void aSynchronizedEntryKeepsItsLockOnTheServersOwnCode() throws Exception { + Class type = load(ENTRIES[0][0], transformed(standIn(ENTRIES[0][0], "service", false, Opcodes.V1_8, + Opcodes.ACC_SYNCHRONIZED))); + Method method = type.getMethod("service", BODY_TYPES); + Method renamed = type.getDeclaredMethod(ServerEntries.RENAMED + "service", BODY_TYPES); + assertFalse(Modifier.isSynchronized(method.getModifiers())); + assertTrue(Modifier.isSynchronized(renamed.getModifiers())); + runningInBody = -1; + method.invoke(type.getDeclaredConstructor().newInstance(), args(0)); + assertEquals(1, runningInBody); + } + + @Test + public void entriesWithNoArgumentsAreCountedToo() throws Exception { + // The least stack an entry's count takes: a static one with no + // argument (the throw's two slots), and one that returns a long. + ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); + writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, ENTRIES[6][0], null, "java/lang/Object", + null); + constructor(writer); + calls(writer, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "executeRootHandler", "()V", "tick", true); + writer.visitEnd(); + Class type = load(ENTRIES[6][0], transformed(writer.toByteArray())); + runningInBody = -1; + type.getMethod("executeRootHandler").invoke(null); + assertEquals(1, runningInBody); + + writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); + writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, ENTRIES[2][0], null, "java/lang/Object", + null); + constructor(writer); + calls(writer, Opcodes.ACC_PUBLIC, "handle", "()J", "count", false); + writer.visitEnd(); + type = load(ENTRIES[2][0], transformed(writer.toByteArray())); + runningInBody = -1; + assertEquals(7L, type.getMethod("handle").invoke(type.getDeclaredConstructor().newInstance())); + assertEquals(1, runningInBody); + assertEquals(0, RequestGate.running()); + } + + @Test + public void anEntryWithNoCodeOfItsOwnIsNotCounted() throws Exception { + // Abstract, native, a bridge, or made by the compiler: none is the + // server's request code. + int[] flags = {Opcodes.ACC_ABSTRACT, Opcodes.ACC_NATIVE, Opcodes.ACC_BRIDGE, Opcodes.ACC_SYNTHETIC}; + for (int flag : flags) { + ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); + writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_ABSTRACT, ENTRIES[0][0], + null, "java/lang/Object", null); + constructor(writer); + if (flag == Opcodes.ACC_ABSTRACT || flag == Opcodes.ACC_NATIVE) { + writer.visitMethod(Opcodes.ACC_PUBLIC | flag, "service", "()V", null, null).visitEnd(); + } else { + calls(writer, Opcodes.ACC_PUBLIC | flag, "service", "()V", "tick", false); + } + writer.visitEnd(); + assertNull("flag " + flag, transformedOrNull(ENTRIES[0][0], writer.toByteArray(), + getClass().getClassLoader())); + } + } + + @Test + public void anyOtherClassIsLeftAlone() throws Exception { + byte[] other = standIn("org/apache/catalina/core/StandardWrapperValve", "service", false, Opcodes.V1_8, 0); + assertNull(new ServerEntries().transform(getClass().getClassLoader(), + "org/apache/catalina/core/StandardWrapperValve", null, null, other)); + // An interface of an entry's name (Jetty 12's HttpChannel) has no + // code to count. + ClassWriter writer = new ClassWriter(0); + writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE, + "org/eclipse/jetty/server/HttpChannel", null, "java/lang/Object", null); + writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, "handle", "()V", null, null).visitEnd(); + writer.visitEnd(); + assertNull(transformedOrNull("org/eclipse/jetty/server/HttpChannel", writer.toByteArray(), + getClass().getClassLoader())); + // Nor one with a default method of the entry's name. + writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); + writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE, + "org/eclipse/jetty/server/HttpChannel", null, "java/lang/Object", null); + calls(writer, Opcodes.ACC_PUBLIC, "handle", "()V", "tick", false); + writer.visitEnd(); + assertNull(transformedOrNull("org/eclipse/jetty/server/HttpChannel", writer.toByteArray(), + getClass().getClassLoader())); + assertFalse("only a server's class is named", ServerEntries.uncounted().contains(null)); + // Passed over quietly: on Jetty 12 the requests count in its + // ServletChannel, so a WARNING would be wrong at each start. + assertNull(logged.toString(), logged("WARNING")); + // An entry's class with no method of its names. + assertNull(transformedOrNull(ENTRIES[0][0], standIn(ENTRIES[0][0], "other", false, Opcodes.V1_8, 0), + getClass().getClassLoader())); + assertNotNull(logged("WARNING Keploy dedup does not count Tomcat's requests:" + + " org.apache.catalina.connector.CoyoteAdapter has no method")); + // A class redefined later is never changed: the SDK asks for no retransform. + assertNull(new ServerEntries().transform(getClass().getClassLoader(), ENTRIES[0][0], Object.class, null, + standIn(ENTRIES[0][0], "service", false, Opcodes.V1_8, 0))); + } + + @Test + public void aServerWhoseLoaderDoesNotSeeTheGateIsNotChanged() throws Exception { + byte[] classfile = standIn(ENTRIES[6][0], "executeRootHandler", true, Opcodes.V1_8, 0); + // A module system's loader, that sees the JDK alone. + try (URLClassLoader alone = new URLClassLoader(new URL[0], null)) { + assertNull(transformedOrNull(ENTRIES[6][0], classfile, alone)); + } + assertNull(transformedOrNull(ENTRIES[6][0], classfile, null)); + // A loader with a copy of its own, which END never reads. + URL classes = RequestGate.class.getProtectionDomain().getCodeSource().getLocation(); + try (URLClassLoader own = new URLClassLoader(new URL[] {classes}, null)) { + assertTrue(Class.forName(RequestGate.class.getName(), false, own) != RequestGate.class); + assertNull(transformedOrNull(ENTRIES[6][0], classfile, own)); + } + assertNotNull(logged("WARNING Keploy dedup does not count Undertow's requests: the class loader of" + + " io.undertow.server.Connectors does not see keploy-sdk.jar's classes")); + } + + @Test + public void nettyIsNamedAsAServerNotCounted() throws Exception { + assertNull(new ServerEntries().transform(getClass().getClassLoader(), + "io/netty/handler/codec/http/HttpServerCodec", null, null, new byte[0])); + assertTrue(ServerEntries.uncounted().contains("Netty")); + } + + @Test + public void aClassThatCannotBeReadLoadsUnchanged() throws Exception { + assertNull(transformedOrNull(ENTRIES[0][0], new byte[] {1, 2, 3}, getClass().getClassLoader())); + assertNotNull(logged("WARNING Keploy dedup does not count Tomcat's requests:" + + " org.apache.catalina.connector.CoyoteAdapter could not be changed")); + } + + private String logged(String prefix) { + for (String line : logged) { + if (line.startsWith(prefix)) { + return line; + } + } + return null; + } + + private byte[] transformed(byte[] classfile) throws Exception { + String name = new org.objectweb.asm.ClassReader(classfile).getClassName(); + byte[] out = transformedOrNull(name, classfile, getClass().getClassLoader()); + assertNotNull(name, out); + return out; + } + + private static byte[] transformedOrNull(String name, byte[] classfile, ClassLoader loader) { + return new ServerEntries().transform(loader, name, null, null, classfile); + } + + // standIn is a class named as a server's, whose method `name` (and + // `log`, of no entry's name) runs body with its five arguments. + private static byte[] standIn(String internalName, String name, boolean isStatic, int version, int extra) { + ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); + writer.visit(version, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, internalName, null, "java/lang/Object", null); + MethodVisitor init = writer.visitMethod(Opcodes.ACC_PUBLIC, "", "()V", null, null); + init.visitCode(); + init.visitVarInsn(Opcodes.ALOAD, 0); + init.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V", false); + init.visitInsn(Opcodes.RETURN); + init.visitMaxs(0, 0); + init.visitEnd(); + callsBody(writer, Opcodes.ACC_PUBLIC | extra | (isStatic ? Opcodes.ACC_STATIC : 0), name, isStatic); + callsBody(writer, Opcodes.ACC_PUBLIC, "log", false); + writer.visitEnd(); + return writer.toByteArray(); + } + + private static void constructor(ClassWriter writer) { + MethodVisitor init = writer.visitMethod(Opcodes.ACC_PUBLIC, "", "()V", null, null); + init.visitCode(); + init.visitVarInsn(Opcodes.ALOAD, 0); + init.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V", false); + init.visitInsn(Opcodes.RETURN); + init.visitMaxs(0, 0); + init.visitEnd(); + } + + // calls adds a method that returns what the test's static method + // target (of the same descriptor, no arguments) returns. + private static void calls(ClassWriter writer, int access, String name, String descriptor, String target, + boolean isStatic) { + MethodVisitor mv = writer.visitMethod(access | (isStatic ? Opcodes.ACC_STATIC : 0), name, descriptor, null, + null); + mv.visitCode(); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(ServerEntriesTest.class), target, descriptor, + false); + mv.visitInsn(Type.getReturnType(descriptor).getOpcode(Opcodes.IRETURN)); + mv.visitMaxs(0, 0); + mv.visitEnd(); + } + + private static void callsBody(ClassWriter writer, int access, String name, boolean isStatic) { + MethodVisitor mv = writer.visitMethod(access, name, BODY_DESCRIPTOR, null, + new String[] {"java/lang/Exception"}); + mv.visitCode(); + int slot = isStatic ? 0 : 1; + for (Type arg : Type.getArgumentTypes(BODY_DESCRIPTOR)) { + mv.visitVarInsn(arg.getOpcode(Opcodes.ILOAD), slot); + slot += arg.getSize(); + } + mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(ServerEntriesTest.class), "body", + BODY_DESCRIPTOR, false); + mv.visitInsn(Opcodes.LRETURN); + mv.visitMaxs(0, 0); + mv.visitEnd(); + } + + private static Class load(String internalName, byte[] classfile) { + return new ClassLoader(ServerEntriesTest.class.getClassLoader()) { + Class define() { + return defineClass(internalName.replace('/', '.'), classfile, 0, classfile.length); + } + }.define(); + } +} diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/ServerTailTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/ServerTailTest.java new file mode 100644 index 00000000..01da8709 --- /dev/null +++ b/keploy-sdk/src/test/java/io/keploy/dedup/ServerTailTest.java @@ -0,0 +1,360 @@ +package io.keploy.dedup; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.jar.Attributes; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; +import java.util.zip.ZipEntry; +import org.example.servers.HelloServlet; +import org.example.servers.ServerApp; +import org.example.servers.Tail; +import org.example.servers.TailFilter; +import org.junit.Assume; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * A request's code that runs after its response is sent (a filter's code + * after the chain) is the test's: its END waits for the request to finish, + * on Tomcat, Jetty and Undertow, each in a JVM of its own with keploy-sdk.jar + * as its -javaagent. With the wait off, END misses that code (the next + * START clears it as hits between tests); past the wait, the record says + * windowCut. + */ +public class ServerTailTest { + + private static final String TAIL = Tail.class.getName().replace('.', '/'); + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + @Test + public void tomcatsEndWaitsForTheFiltersCodeAfterTheResponse() throws Exception { + Result result = run("tomcat", "10000", 1500); + assertTrue(result.log, endLine(result.log).contains(" requests=")); + assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); + assertFalse(result.cov, result.cov.contains("windowCut")); + assertTrue(result.log, result.log.contains("Keploy dedup counts Tomcat's requests" + + " (org.apache.catalina.connector.CoyoteAdapter)")); + assertFalse(result.log, result.log.contains("does not wait")); + } + + @Test + public void jettysEndWaitsForTheFiltersCodeAfterTheResponse() throws Exception { + Result result = run("jetty", "10000", 1500); + assertTrue(result.log, endLine(result.log).contains(" requests=")); + assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); + assertFalse(result.cov, result.cov.contains("windowCut")); + assertTrue(result.log, result.log.contains("Keploy dedup counts Jetty's requests" + + " (org.eclipse.jetty.server.HttpChannel)")); + } + + @Test + public void undertowsEndWaitsForTheFiltersCodeAfterTheResponse() throws Exception { + Result result = run("undertow", "10000", 1500); + assertTrue(result.log, endLine(result.log).contains(" requests=")); + assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); + assertFalse(result.cov, result.cov.contains("windowCut")); + assertTrue(result.log, result.log.contains("Keploy dedup counts Undertow's requests" + + " (io.undertow.server.Connectors)")); + } + + @Test + public void tomcatInANamedModuleCountsItsRequests() throws Exception { + // Tomcat's jars on the module path: CoyoteAdapter is in the named + // module org.apache.tomcat.embed.core, which reads no class path + // module: its changed code reaches RequestGate because the JVM adds + // that read when an agent changes a class of a named module. + Assume.assumeFalse("modules are Java 9's", System.getProperty("java.specification.version") + .startsWith("1.")); + Fork fork = new Fork("tomcat", "10000", 1500); + fork.modules = true; + Result result = run(fork); + assertTrue(result.log, result.log.contains("MODULE module org.apache.tomcat.embed.core")); + assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); + assertFalse(result.cov, result.cov.contains("windowCut")); + assertTrue(result.log, result.log.contains("Keploy dedup counts Tomcat's requests" + + " (org.apache.catalina.connector.CoyoteAdapter)")); + assertFalse(result.log, result.log.contains("IllegalAccessError")); + assertTrue(result.log, endLine(result.log).contains(" requests=")); + } + + @Test + public void jacocoMeasuresTheServersOwnClassAsTheIndexHasIt() throws Exception { + // The index (KEPLOY_JAVA_CLASS_DIRS) holds Tomcat's jar, so the + // embedded JaCoCo instruments CoyoteAdapter too: it must see the + // class's own bytes, the ones the index ids, not the counted ones, + // or every test's record names it in classMismatch. + Fork fork = new Fork("tomcat", "10000", 1500); + fork.indexTomcat = true; + Result result = run(fork); + assertTrue(result.cov, result.cov.contains("\"org/apache/catalina/connector/CoyoteAdapter\":[")); + assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); + // The class path has Servlet's API twice (Tomcat's jar and + // javax.servlet-api), so those classes differ from the index's copy. + assertFalse(result.cov, mismatched(result.cov).contains("org/apache/catalina/connector/CoyoteAdapter")); + assertTrue(result.log, endLine(result.log).contains(" requests=")); + } + + @Test + public void withTheWaitOffTheEndMissesTheFiltersCode() throws Exception { + // As before 2.2.0: the record has the servlet, not the filter's tail. + Fork fork = new Fork("tomcat", "0", 1500); + fork.tests = 2; + Result result = run(fork); + for (String cov : result.covs) { + assertTrue(cov, cov.contains("\"" + HelloServlet.class.getName().replace('.', '/') + "\"")); + assertFalse(cov, cov.contains("\"" + TAIL + "\"")); + assertFalse(cov, cov.contains("windowCut")); + } + assertFalse(result.log, result.log.contains("Keploy dedup counts")); + assertFalse(result.log, endLine(result.log).contains(" requests=")); + String why = "a test's END does not wait for its requests to finish: KEPLOY_JAVA_END_WAIT_MS is 0"; + assertTrue(result.log, result.log.contains(why)); + assertEquals("said once", result.log.indexOf(why), result.log.lastIndexOf(why)); + } + + @Test + public void aTailPastTheWaitCutsTheEnd() throws Exception { + Result result = run("tomcat", "100", 5000); + assertFalse(result.cov, result.cov.contains("\"" + TAIL + "\"")); + assertTrue(result.cov, result.cov.endsWith(",\"windowCut\":true}")); + assertTrue(result.log, result.log.contains("test-set-0/test-1's requests still ran 100 ms after its END")); + } + + private static final class Result { + String cov; + final List covs = new ArrayList<>(); + String log; + } + + // Fork is how ServerTailTest runs ServerApp: on server, with END's + // wait at endWait, tests tests whose request's filter runs tailMillis + // after the response; with modules, Tomcat's jars are on the module + // path instead of the class path; with indexTomcat, the index (and so + // the embedded JaCoCo) holds Tomcat's classes as well as the app's. + private static final class Fork { + final String server; + final String endWait; + final long tailMillis; + int tests = 1; + boolean modules; + boolean indexTomcat; + + Fork(String server, String endWait, long tailMillis) { + this.server = server; + this.endWait = endWait; + this.tailMillis = tailMillis; + } + } + + private Result run(String server, String endWait, long tailMillis) throws Exception { + return run(new Fork(server, endWait, tailMillis)); + } + + // run starts ServerApp as fork says in a JVM of its own and, as Keploy + // over its TCP channel, runs its tests, with the SDK's diagnostics on. + private Result run(Fork fork) throws Exception { + String server = fork.server; + long tailMillis = fork.tailMillis; + int tests = fork.tests; + boolean modules = fork.modules; + File classes = tmp.newFolder(); + for (Class app : new Class[] {ServerApp.class, HelloServlet.class, TailFilter.class, Tail.class}) { + String key = app.getName().replace('.', '/') + ".class"; + File copy = new File(classes, key); + copy.getParentFile().mkdirs(); + try (InputStream input = app.getClassLoader().getResourceAsStream(key)) { + Files.copy(input, copy.toPath()); + } + } + Result result = new Result(); + try (ServerSocket collector = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) { + collector.setSoTimeout(60000); + List command = new ArrayList<>(); + command.add(new File(System.getProperty("java.home"), "bin/java").getPath()); + command.add("-Djava.io.tmpdir=" + tmp.newFolder().getPath()); + command.add("-javaagent:" + sdkJar().getPath()); + List classPath = new ArrayList<>(); + List modulePath = new ArrayList<>(); + // Absolute: the app runs in a dir of its own. + for (String path : System.getProperty("java.class.path").split(File.pathSeparator)) { + String entry = new File(path).getAbsolutePath(); + String jar = new File(entry).getName(); + boolean tomcat = jar.startsWith("tomcat-embed-core-") || jar.startsWith("tomcat-annotations-api-"); + (modules && tomcat ? modulePath : classPath).add(entry); + } + if (modules) { + assertEquals(modulePath.toString(), 2, modulePath.size()); + command.add("--module-path"); + command.add(String.join(File.pathSeparator, modulePath)); + command.add("--add-modules"); + command.add("org.apache.tomcat.embed.core"); + } + command.add("-cp"); + command.add(String.join(File.pathSeparator, classPath)); + command.add(ServerApp.class.getName()); + command.add(server); + File log = tmp.newFile(); + ProcessBuilder builder = new ProcessBuilder(command).directory(tmp.newFolder()) + .redirectErrorStream(true).redirectOutput(log); + Map env = builder.environment(); + env.keySet().removeIf(name -> name.startsWith("KEPLOY_") || name.equals("JAVA_TOOL_OPTIONS") + || name.equals("JDK_JAVA_OPTIONS") || name.equals("_JAVA_OPTIONS")); + String classDirs = classes.getPath(); + if (fork.indexTomcat) { + for (String entry : classPath) { + if (new File(entry).getName().startsWith("tomcat-embed-core-")) { + classDirs += File.pathSeparator + entry; + } + } + } + env.put("KEPLOY_JAVA_CLASS_DIRS", classDirs); + env.put("KEPLOY_JAVA_DEDUP_DIAGNOSTICS", "true"); + env.put("KEPLOY_COVERAGE_ENDPOINT", "127.0.0.1:" + collector.getLocalPort()); + env.put("KEPLOY_JAVA_DEDUP_WARMUP_DISABLED", "true"); + env.put("KEPLOY_JAVA_END_WAIT_MS", fork.endWait); + Process app = builder.start(); + try { + int port = port(log, app); + try (Socket sdk = collector.accept()) { + sdk.setSoTimeout(60000); + OutputStream out = sdk.getOutputStream(); + BufferedReader in = new BufferedReader( + new InputStreamReader(sdk.getInputStream(), StandardCharsets.UTF_8)); + for (int test = 1; test <= tests; test++) { + send(out, "START test-set-0/test-" + test); + assertEquals("ACK", in.readLine()); + assertEquals("hello", get(port, "/hello?tail=" + tailMillis)); + send(out, "END test-set-0/test-" + test); + String cov = in.readLine(); + assertTrue(cov, cov != null && cov.startsWith("COV ")); + result.covs.add(cov.substring("COV ".length())); + String line; + while (!(line = in.readLine()).equals("ACK")) { + assertTrue(line, line.startsWith("CLASSES ") || line.startsWith("MANIFEST ")); + } + } + result.cov = result.covs.get(0); + } + } finally { + app.getOutputStream().close(); + if (!app.waitFor(30, TimeUnit.SECONDS)) { + app.destroyForcibly().waitFor(10, TimeUnit.SECONDS); + } + result.log = new String(Files.readAllBytes(log.toPath()), StandardCharsets.UTF_8); + } + } + return result; + } + + // mismatched is the classMismatch list of a record, "" when it has none. + private static String mismatched(String cov) { + int at = cov.indexOf("\"classMismatch\":["); + return at < 0 ? "" : cov.substring(at, cov.indexOf(']', at) + 1); + } + + // endLine is the diagnostics line of test-1's END: it names a + // requests step only when END waited for the test's requests. + private static String endLine(String log) { + for (String line : log.split("\n")) { + if (line.contains("END test-set-0/test-1 took ")) { + return line; + } + } + throw new AssertionError("no diagnostics line of test-1's END: " + log); + } + + // port is the port ServerApp prints once its server listens. + private static int port(File log, Process app) throws Exception { + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(60); + while (System.nanoTime() < deadline) { + for (String line : Files.readAllLines(log.toPath(), StandardCharsets.UTF_8)) { + if (line.startsWith("PORT ")) { + return Integer.parseInt(line.substring("PORT ".length()).trim()); + } + } + assertTrue(new String(Files.readAllBytes(log.toPath()), StandardCharsets.UTF_8), app.isAlive()); + Thread.sleep(50); + } + throw new AssertionError(new String(Files.readAllBytes(log.toPath()), StandardCharsets.UTF_8)); + } + + // get returns the body of a GET of path, read to its Content-Length, as + // Keploy reads a response: the request may still run on the server. + private static String get(int port, String path) throws IOException { + try (Socket socket = new Socket(InetAddress.getLoopbackAddress(), port)) { + socket.setSoTimeout(30000); + socket.getOutputStream().write(("GET " + path + " HTTP/1.1\r\nHost: localhost\r\n\r\n") + .getBytes(StandardCharsets.US_ASCII)); + InputStream in = socket.getInputStream(); + int length = -1; + String header; + while (!(header = readLine(in)).isEmpty()) { + if (header.toLowerCase(java.util.Locale.ROOT).startsWith("content-length:")) { + length = Integer.parseInt(header.substring("content-length:".length()).trim()); + } + } + assertTrue("a Content-Length", length >= 0); + byte[] body = new byte[length]; + int read = 0; + while (read < length) { + int n = in.read(body, read, length - read); + assertTrue(n > 0); + read += n; + } + return new String(body, StandardCharsets.UTF_8); + } + } + + private static String readLine(InputStream in) throws IOException { + ByteArrayOutputStream line = new ByteArrayOutputStream(); + int b; + while ((b = in.read()) >= 0 && b != '\n') { + if (b != '\r') { + line.write(b); + } + } + return new String(line.toByteArray(), StandardCharsets.US_ASCII); + } + + private static void send(OutputStream out, String command) throws IOException { + out.write((command + "\n").getBytes(StandardCharsets.UTF_8)); + out.flush(); + } + + // sdkJar is a jar whose Premain-Class is the SDK's, which is on the + // classpath it is run with. + private File sdkJar() throws IOException { + File sdk = tmp.newFile("keploy-sdk-" + System.nanoTime() + ".jar"); + Manifest manifest = new Manifest(); + manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); + manifest.getMainAttributes().putValue("Premain-Class", KeployDedupAgent.class.getName()); + try (JarOutputStream out = new JarOutputStream(new FileOutputStream(sdk), manifest)) { + out.putNextEntry(new ZipEntry("META-INF/keploy-test")); + out.closeEntry(); + } + return sdk; + } +} diff --git a/keploy-sdk/src/test/java/org/example/servers/HelloServlet.java b/keploy-sdk/src/test/java/org/example/servers/HelloServlet.java new file mode 100644 index 00000000..e526ffdb --- /dev/null +++ b/keploy-sdk/src/test/java/org/example/servers/HelloServlet.java @@ -0,0 +1,22 @@ +package org.example.servers; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** HelloServlet answers "hello", whole and flushed: the client has it all. */ +public final class HelloServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + byte[] body = "hello".getBytes(StandardCharsets.UTF_8); + response.setContentType("text/plain"); + response.setContentLength(body.length); + response.getOutputStream().write(body); + response.flushBuffer(); + } +} diff --git a/keploy-sdk/src/test/java/org/example/servers/ServerApp.java b/keploy-sdk/src/test/java/org/example/servers/ServerApp.java new file mode 100644 index 00000000..66954859 --- /dev/null +++ b/keploy-sdk/src/test/java/org/example/servers/ServerApp.java @@ -0,0 +1,90 @@ +package org.example.servers; + +import io.undertow.Undertow; +import io.undertow.servlet.Servlets; +import io.undertow.servlet.api.DeploymentManager; +import java.io.File; +import java.net.InetSocketAddress; +import java.nio.file.Files; +import java.util.EnumSet; +import javax.servlet.DispatcherType; +import org.apache.catalina.Context; +import org.apache.catalina.startup.Tomcat; +import org.apache.tomcat.util.descriptor.web.FilterDef; +import org.apache.tomcat.util.descriptor.web.FilterMap; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.servlet.FilterHolder; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; + +/** + * ServerApp is the app of ServerTailTest's own JVMs: HelloServlet behind + * TailFilter on the server args[0] names (tomcat, jetty or undertow), on a + * port it prints, until its stdin closes. + */ +public final class ServerApp { + + private ServerApp() { + } + + public static void main(String[] args) throws Exception { + AutoCloseable server; + int port; + if (args[0].equals("tomcat")) { + File base = Files.createTempDirectory("tomcat").toFile(); + Tomcat tomcat = new Tomcat(); + tomcat.setBaseDir(base.getPath()); + tomcat.setPort(0); + Context context = tomcat.addContext("", base.getPath()); + Tomcat.addServlet(context, "hello", new HelloServlet()); + context.addServletMappingDecoded("/hello", "hello"); + FilterDef filter = new FilterDef(); + filter.setFilterName("tail"); + filter.setFilter(new TailFilter()); + context.addFilterDef(filter); + FilterMap map = new FilterMap(); + map.setFilterName("tail"); + map.addURLPattern("/*"); + context.addFilterMap(map); + tomcat.start(); + port = tomcat.getConnector().getLocalPort(); + server = tomcat::stop; + try { + // The module CoyoteAdapter is in, on Java 9 and later. + System.out.println("MODULE " + Class.class.getMethod("getModule") + .invoke(Class.forName("org.apache.catalina.connector.CoyoteAdapter"))); + } catch (NoSuchMethodException e) { + // Java 8. + } + } else if (args[0].equals("jetty")) { + Server jetty = new Server(0); + ServletContextHandler context = new ServletContextHandler(); + context.addServlet(new ServletHolder(new HelloServlet()), "/hello"); + context.addFilter(new FilterHolder(new TailFilter()), "/*", EnumSet.of(DispatcherType.REQUEST)); + jetty.setHandler(context); + jetty.start(); + port = ((ServerConnector) jetty.getConnectors()[0]).getLocalPort(); + server = jetty::stop; + } else { + DeploymentManager deployment = Servlets.defaultContainer().addDeployment(Servlets.deployment() + .setClassLoader(ServerApp.class.getClassLoader()).setContextPath("/").setDeploymentName("app") + .addServlet(Servlets.servlet("hello", HelloServlet.class).addMapping("/hello")) + .addFilter(Servlets.filter("tail", TailFilter.class)) + .addFilterUrlMapping("tail", "/*", DispatcherType.REQUEST)); + deployment.deploy(); + Undertow undertow = Undertow.builder().addHttpListener(0, "127.0.0.1").setHandler(deployment.start()) + .build(); + undertow.start(); + port = ((InetSocketAddress) undertow.getListenerInfo().get(0).getAddress()).getPort(); + server = undertow::stop; + } + System.out.println("PORT " + port); + System.out.flush(); + while (System.in.read() >= 0) { + // Until the test closes stdin. + } + server.close(); + System.exit(0); + } +} diff --git a/keploy-sdk/src/test/java/org/example/servers/Tail.java b/keploy-sdk/src/test/java/org/example/servers/Tail.java new file mode 100644 index 00000000..40425106 --- /dev/null +++ b/keploy-sdk/src/test/java/org/example/servers/Tail.java @@ -0,0 +1,17 @@ +package org.example.servers; + +/** + * Tail is the app code TailFilter runs after the response is sent: the + * class a test's record must hold when END waits for the request. + */ +public final class Tail { + + private static volatile int runs; + + private Tail() { + } + + public static void ran() { + runs++; + } +} diff --git a/keploy-sdk/src/test/java/org/example/servers/TailFilter.java b/keploy-sdk/src/test/java/org/example/servers/TailFilter.java new file mode 100644 index 00000000..a6801010 --- /dev/null +++ b/keploy-sdk/src/test/java/org/example/servers/TailFilter.java @@ -0,0 +1,41 @@ +package org.example.servers; + +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +/** + * TailFilter runs app code after the chain, once the response is with the + * client (as a request log or an authentication filter does), after + * the request's ?tail= milliseconds: a thread descheduled under load. + */ +public final class TailFilter implements Filter { + + @Override + public void init(FilterConfig config) { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + chain.doFilter(request, response); + response.flushBuffer(); + String tail = request.getParameter("tail"); + if (tail != null) { + try { + Thread.sleep(Long.parseLong(tail)); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + Tail.ran(); + } + } + + @Override + public void destroy() { + } +} From 66c5c68aac8eff9e08124e6428d796b006827cc9 Mon Sep 17 00:00:00 2001 From: slayerjain Date: Fri, 20 Nov 2026 09:13:18 +0530 Subject: [PATCH 08/14] fix(dedup): let JaCoCo id a server's own class in any -javaagent order keploy-sdk.jar changes the method each server runs a request in (Tomcat's CoyoteAdapter, Jetty's HttpChannel or servlet channel, Undertow's Connectors) with a transformer that could not retransform, so the JVM ran it in the order the agents were attached. A JaCoCo agent attached after keploy-sdk.jar, or keploy-sdk.jar in JAVA_TOOL_OPTIONS with JaCoCo on the command line, then instrumented and ided the changed class, and every test's record named it in classMismatch whenever the classes Keploy measures include the server's (an uber jar, or every class on the class path). The requests were counted in every order. The transformer can now retransform (Can-Retransform-Classes: true in the jar's manifest). The JVM runs such transformers after every one that cannot, JaCoCo's among them, so JaCoCo instruments and ids the server's own class in any order. When another agent retransforms a class the SDK changed as it loaded, the JVM passes the class as it was before the SDK's change; on a redefine it passes the redefiner's bytes. The SDK changes either again, since the JVM refuses a class that drops a method, unless the bytes already carry the change (a copy taken from the class as the SDK changed it), which the JVM then takes as they are. A redefine with the server's own bytes, which the JVM refused before, now succeeds when no other agent changed the class as it loaded (JaCoCo does not redo its own change on a redefine). A class loaded before the SDK started is left as it is, since the JVM refuses a method added to a loaded class too. A jar that carries the SDK without the manifest entry still counts the requests, as before, and when the JVM has a JaCoCo agent of its own it says at WARNING that one attached after it is then named in classMismatch. KEPLOY_JAVA_END_WAIT_MS now caps at 2000 ms instead of 10000: a longer wait per test costs more than the SDK's overhead target. The JVM now calls the SDK's transformer in a second pass over each class it loads: one call into the agent with a copy of the class's bytes, then one map lookup. With KEPLOY_JAVA_END_WAIT_MS=0 no class gets that pass, but the JVM still holds the retransform capability the manifest asks for, as with any agent that can retransform. Rows, on a real Tomcat in a JVM of its own: JaCoCo's own agent attached before keploy-sdk.jar, after it, and after it with keploy-sdk.jar in JAVA_TOOL_OPTIONS, each measuring CoyoteAdapter with no classMismatch for it (the two later orders named it before this change); a jar without Can-Retransform-Classes counting, with its WARNING when JaCoCo is attached after it and none with the SDK's own JaCoCo; another agent's retransform of CoyoteAdapter succeeding and the requests still counted; a redefine of CoyoteAdapter with a dump of it as the SDK changed it succeeding, the requests still counted; and a retransform of a CoyoteAdapter loaded ahead of the SDK succeeding, the server left uncounted. Unit rows: the change made again on a retransform for the class changed as it loaded, and for no other; a retransform it cannot make again refused with a WARNING; bytes that already carry the change taken as they are on a redefine (changed twice, the JVM refuses the class) and counted as they are when a class loads with them; the 2000 ms cap. Signed-off-by: slayerjain --- README.md | 19 +- keploy-sdk/pom.xml | 2 +- .../io/keploy/dedup/KeployDedupAgent.java | 46 +++-- .../java/io/keploy/dedup/ServerEntries.java | 92 ++++++++- .../io/keploy/dedup/EmbeddedJacocoTest.java | 4 +- .../io/keploy/dedup/InProcessScanTest.java | 22 +- .../io/keploy/dedup/ServerEntriesTest.java | 76 ++++++- .../java/io/keploy/dedup/ServerTailTest.java | 193 ++++++++++++++++-- .../org/example/servers/Retransformer.java | 88 ++++++++ .../java/org/example/servers/ServerApp.java | 1 + 10 files changed, 485 insertions(+), 58 deletions(-) create mode 100644 keploy-sdk/src/test/java/org/example/servers/Retransformer.java diff --git a/README.md b/README.md index 74b1a12b..f6a48dda 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Keploy Enterprise drives dynamic dedup per testcase. Coverage is collected at per-testcase granularity, not process granularity. -A server sends a response before it is done with the request: a filter's code after the chain (a request log, an authentication filter's cleanup) runs after Enterprise has the response and has sent `END`. Since 2.2.0 the agent counts the requests of Tomcat (8.5 to 11), Jetty (9.4 to 12.1) and Undertow, Spring Boot's three servers, from where the server starts running a request to where its response is finished, and each async dispatch of it alike, and `END` first waits until every request that came in since the testcase's `START` is done, for at most `KEPLOY_JAVA_END_WAIT_MS` (200 ms by default, 10 s at most). A request that comes in once `END` has come (a readiness probe, a metrics scrape, or a call the testcase's own code makes after its response) is not waited for. Before, `END` read the probes at once, so under load that code was missed, and the next `START` cleared it as background: the same testcase had different coverage from run to run. A request that is done costs `END` nothing, and a request that came in before the `START` (a stream, a request of the testcase before) is never waited for. When the wait reaches its bound, the log says so at WARNING and the testcase's record ends with `"windowCut":true` (after `classMismatch`; left out otherwise): it holds what was read then, a Keploy that reads the flag does not judge that testcase (an older one judges the hits the record holds), and what the next `START` clears is not sent as background. Limits: what the application runs on threads of its own (an async task, an executor's work between two dispatches of an async request, an Undertow completion listener that runs on the IO thread after an async write) is not counted; Netty, and so WebFlux, is not counted, nor is a server whose classes were loaded before the agent started (it is attached to a running JVM) or whose class loader does not see the agent's classes (a module system such as JBoss Modules); the log says once when a testcase's `END` does not wait, and why. A JaCoCo agent attached after keploy-sdk.jar instruments the server class as the agent changed it, so when the classes Keploy measures include the server's (an uber jar with the server inside, or every class on the class path), each testcase's `classMismatch` names that class: list `-javaagent:jacocoagent.jar` before keploy-sdk.jar. The agents in `JAVA_TOOL_OPTIONS` start before the command line's, so keploy-sdk.jar there with JaCoCo on the command line is attached before JaCoCo. The JaCoCo keploy-sdk.jar starts itself always sees the server's own class. To count them, the agent changes the one method of each server that runs a request (Tomcat's `CoyoteAdapter.service` and `asyncDispatch`, Jetty's `HttpChannel.handle` or, on Jetty 12, its servlet channel's `handle`, Undertow's `Connectors.executeRootHandler`) as the class loads: the server's own code moves to a private method `keploy$request$` beside it, which shows in a stack trace and keeps the method's annotations; the method of the old name, which counts the request and calls it, carries none, so reflection that reads the annotations of `service` itself finds none. +A server sends a response before it is done with the request: a filter's code after the chain (a request log, an authentication filter's cleanup) runs after Enterprise has the response and has sent `END`. Since 2.2.0 the agent counts the requests of Tomcat (8.5 to 11), Jetty (9.4 to 12.1) and Undertow, Spring Boot's three servers, from where the server starts running a request to where its response is finished, and each async dispatch of it alike, and `END` first waits until every request that came in since the testcase's `START` is done, for at most `KEPLOY_JAVA_END_WAIT_MS` (200 ms by default, 2 s at most). A request that comes in once `END` has come (a readiness probe, a metrics scrape, or a call the testcase's own code makes after its response) is not waited for. Before, `END` read the probes at once, so under load that code was missed, and the next `START` cleared it as background: the same testcase had different coverage from run to run. A request that is done costs `END` nothing, and a request that came in before the `START` (a stream, a request of the testcase before) is never waited for. When the wait reaches its bound, the log says so at WARNING and the testcase's record ends with `"windowCut":true` (after `classMismatch`; left out otherwise): it holds what was read then, a Keploy that reads the flag does not judge that testcase (an older one judges the hits the record holds), and what the next `START` clears is not sent as background. Limits: what the application runs on threads of its own (an async task, an executor's work between two dispatches of an async request, an Undertow completion listener that runs on the IO thread after an async write) is not counted; Netty, and so WebFlux, is not counted, nor is a server whose classes were loaded before the agent started (it is attached to a running JVM) or whose class loader does not see the agent's classes (a module system such as JBoss Modules); the log says once when a testcase's `END` does not wait, and why. To count them, the agent changes the one method of each server that runs a request (Tomcat's `CoyoteAdapter.service` and `asyncDispatch`, Jetty's `HttpChannel.handle` or, on Jetty 12, its servlet channel's `handle`, Undertow's `Connectors.executeRootHandler`) as the class loads: the server's own code moves to a private method `keploy$request$` beside it, which shows in a stack trace and keeps the method's annotations; the method of the old name, which counts the request and calls it, carries none, so reflection that reads the annotations of `service` itself finds none. The agent makes its change after those of every agent that cannot retransform classes, JaCoCo's among them, whatever the order the agents are attached in: a JaCoCo agent attached before or after keploy-sdk.jar, or in `JAVA_TOOL_OPTIONS`, instruments the server's own class, so `classMismatch` never names it for that. When another agent retransforms or redefines the class later (an APM agent, a profiler, a hot-swap tool), the agent makes its change again, unless the new bytes already carry it (a copy taken from the class as the agent changed it). Since 2.2.0 a testcase's record also names, in `"classMismatch":[className, ...]` after its probes, each class whose probes come from bytes other than the ones the agent indexed: a class loader that weaves the bytes it defines (load-time weaving) or an agent listed before JaCoCo's that does not ask to retransform changed the class before JaCoCo saw it, or the JVM loaded a multi-release jar's versioned copy. JaCoCo's id for such a class is not the CRC64 of the indexed bytes, so its probe indices do not belong to the indexed class, and Keploy must not read them against it. The key is left out when there is no such class. Of two copies of one class (loaded twice, from different bytes) that a testcase ran, the record reports the copy the agent indexed, else the one the coverage manifest already names, else the lowest class id compared as an unsigned number; a BACKGROUND record chooses alike, and names such classes in a `classMismatch` of its own. Every class id the agent writes (the coverage manifest's `id`) is 16 lowercase hex digits since 2.2.0; older agents wrote it unpadded, so read it as a hex number. @@ -100,14 +100,13 @@ app's class dirs and jars to fix that. If the command attaches a JaCoCo agent of its own (`-javaagent:jacocoagent.jar`, before or after keploy-sdk.jar, or in `JAVA_TOOL_OPTIONS`), the SDK starts none -and reads coverage from that one. List it before keploy-sdk.jar: one listed after -it instruments the server classes the SDK changes (see How It Works). The SDK -reads it through JaCoCo's in-process runtime API -(`org.jacoco.agent.rt.RT.getAgent()`). That JaCoCo instruments every class its -own options take in. If the in-process API is unavailable (for example because -the JaCoCo agent is loaded into an isolated classloader), the SDK falls back to -JaCoCo's TCP server mode. To use the fallback explicitly, start JaCoCo in -`tcpserver` mode and set `KEPLOY_JACOCO_HOST` / `KEPLOY_JACOCO_PORT`: +and reads coverage from that one. The SDK reads it through JaCoCo's in-process +runtime API (`org.jacoco.agent.rt.RT.getAgent()`). That JaCoCo instruments +every class its own options take in. If the in-process API is unavailable (for +example because the JaCoCo agent is loaded into an isolated classloader), the +SDK falls back to JaCoCo's TCP server mode. To use the fallback explicitly, +start JaCoCo in `tcpserver` mode and set `KEPLOY_JACOCO_HOST` / +`KEPLOY_JACOCO_PORT`: ```bash java \ @@ -211,7 +210,7 @@ ceiling does not quietly fall behind again. - `KEPLOY_JAVA_DEDUP_DISABLED`: disables the Java dedup agent when set to `true`, `1`, or `yes` - `KEPLOY_JAVA_DEDUP_DIAGNOSTICS`: when `true`, `1`, or `yes`, logs one line per START and END that says how long the command took and how long each step did: `warmup`, then `scan` (the pass that clears the background) for a START; `requests` (the wait for the test's requests to finish, when one still ran), `scan` (the pass that reads and clears the test's hits), `filter` (the hits made into the test's probes by class), `json` and `send` for an END, plus `classes` on TCP (the check for new app classes, and the class-zip frame when there are some). Where JaCoCo's own reset and dump are used instead, a START has `reset`, and an END has `dump` (JaCoCo's dump), `parse` and `filter` (to the app's classes) in place of `scan` and `filter`; a step that did not run is left out, so an empty dump has only `dump`. A START that cleared background hits also logs how many classes and probes they were. The app's class files are read once: before the app starts, when keploy-sdk.jar starts JaCoCo itself; otherwise on the first START, by the warmup's thread (timed under `warmup`, and, past the 10 seconds that START waits for the warmup, under its `scan`, which waits for the read, or, with JaCoCo's reset and dump, which never wait for it, on the first END whose dump is not empty, under `filter`), or, with the warmup off, under that START's `scan` (or, with JaCoCo's dump, on the first END whose dump is not empty, under `filter`). On the unix socket `took` ends before the answer is written; on TCP it includes the answer. Use it to see where a test's time goes on your app - `KEPLOY_JAVA_SCAN`: `off` (in any case) turns the one-pass read of the application's probes off: each testcase then takes JaCoCo's own reset and dump, as when JaCoCo is reached over TCP, a `START` no longer sees background hits, and `BACKGROUND` is answered `IGNORED`. Use it if the in-place read misbehaves on your JVM; at 50k application classes JaCoCo's reset and dump took about 20 ms per testcase in our profile, against under 2 ms with the in-place read. Default: on -- `KEPLOY_JAVA_END_WAIT_MS`: how long a testcase's `END` waits, at most, for the requests that came in since its `START` to finish (see "How It Works"). `0` turns the wait off, and leaves the servers' classes unchanged: `END` reads the probes at once, as before 2.2.0. At most `10000`: a larger value waits 10000 ms, said once at WARNING, so that Keploy has the record before it stops waiting for it. Default: `200` +- `KEPLOY_JAVA_END_WAIT_MS`: how long a testcase's `END` waits, at most, for the requests that came in since its `START` to finish (see "How It Works"). `0` turns the wait off, and leaves the servers' classes unchanged: `END` reads the probes at once, as before 2.2.0. At most `2000`: a larger value waits 2000 ms, said once at WARNING, as a longer wait would cost each testcase more than the agent's overhead allows. Default: `200` - `KEPLOY_JAVA_DEDUP_WARMUP_DISABLED`: when `true`, `1`, or `yes`, skips the warmup, which loads and initializes the app's own classes once, on the first START, so that no test is charged with their static initializers. It loads them through the class loader the app runs them with, such as a Spring Boot fat jar's own loader (since 2.2.0; before, a fat jar's classes were not warmed up), so every app class not loaded yet is loaded then, and JaCoCo instruments it: on an app with many classes it never uses, that is more class metadata (metaspace) than the app alone would load. It runs on a thread of its own: the first START waits for it for at most 10 seconds, then goes on, and the warmup stops after the class it is initializing. Its static initializer goes on while the tests run, and each line it runs from then on counts in the test running at the time (between two tests, in none: the next START clears it); classes it did not reach initialize when the app first uses them, as with the warmup off. The log says how long the warmup took, and, when the START stopped waiting, which class it was initializing ## Which classes count as the app's diff --git a/keploy-sdk/pom.xml b/keploy-sdk/pom.xml index 26dd115a..3eaa521b 100644 --- a/keploy-sdk/pom.xml +++ b/keploy-sdk/pom.xml @@ -158,7 +158,7 @@ io.keploy.dedup.KeployDedupAgent io.keploy.dedup.KeployDedupAgent false - false + true ${project.name} ${project.version} io.keploy.sdk diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java index 8bfbf597..9fd5b6aa 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java @@ -114,9 +114,9 @@ static String encodeBytecodeFrame(String buildTag, String manifestJson, byte[] z private static final int SOCKET_BACKLOG = 50; // How long a test's END waits for its requests to finish by default. static final long DEFAULT_END_WAIT_MILLIS = 200; - // Well inside the 30 s Keploy's k8s collector waits for an END's answer: - // past it, the record would be lost. - static final long MAX_END_WAIT_MILLIS = 10000; + // A longer wait would cost each test more than the SDK's overhead + // target. Keploy must wait longer than this for an END's answer. + static final long MAX_END_WAIT_MILLIS = 2000; private static final AtomicBoolean END_WAIT_INVALID_LOGGED = new AtomicBoolean(false); private static final AtomicBoolean END_WAIT_CAPPED_LOGGED = new AtomicBoolean(false); private static final AtomicBoolean NOT_COUNTED_LOGGED = new AtomicBoolean(false); @@ -284,11 +284,13 @@ private static boolean start(CoverageIndex index, Instrumentation instrumentatio } if (instrumentation != null) { - // JaCoCo's transformer first: transformers run in the order they - // were added, so JaCoCo instruments, and ids, a server's class as - // the index read it, and ServerEntries counts in JaCoCo's copy. + // JaCoCo's transformer first: it cannot retransform, so the JVM + // runs it before ServerEntries, which can, and so in the order + // they were added where the jar may not retransform: JaCoCo + // instruments, and ids, a server's class as the index read it, + // and ServerEntries counts in JaCoCo's copy. startJacoco(instrumentation, index); - countRequests(instrumentation); + countRequests(instrumentation, IndexReport.JACOCO_ATTACHED.equals(index.jacoco)); } Thread thread = new Thread(worker, threadName); thread.setDaemon(true); @@ -301,16 +303,28 @@ private static boolean start(CoverageIndex index, Instrumentation instrumentatio // countRequests makes the requests of the app's server count in // RequestGate (ServerEntries), so that a test's END waits for them to // finish, unless KEPLOY_JAVA_END_WAIT_MS is 0. From premain, before the - // app's main loads its server, and after startJacoco: a JaCoCo agent - // attached after keploy-sdk.jar adds its transformer after this one, so - // it ids the changed server class, which classMismatch then names when - // the index holds it. - private static void countRequests(Instrumentation instrumentation) { + // app's main loads its server, and after startJacoco. ServerEntries can + // retransform when keploy-sdk.jar's manifest lets it (Can-Retransform- + // Classes), so the JVM runs it after JaCoCo's transformer even when a + // JaCoCo agent was attached after keploy-sdk.jar. In a jar that does not + // let it, it runs in the order it was added, before such a JaCoCo's, + // which then ids the changed server class: classMismatch names it when + // the index holds it, and the log says so when the JVM has a JaCoCo + // agent of its own (jacocoAttached). + private static void countRequests(Instrumentation instrumentation, boolean jacocoAttached) { if (endWaitMillis() == 0) { return; } try { - instrumentation.addTransformer(new ServerEntries(), false); + boolean retransformable = instrumentation.isRetransformClassesSupported(); + if (!retransformable && jacocoAttached) { + log(Level.WARNING, "Keploy dedup: the jar keploy-sdk.jar runs from has no Can-Retransform-Classes:" + + " true in its manifest, or this JVM cannot retransform, so a JaCoCo agent attached after" + + " it instruments the server classes it changes, and each test's classMismatch names them" + + " when Keploy measures them. Attach JaCoCo before it, or run keploy-sdk.jar as released", + null); + } + instrumentation.addTransformer(new ServerEntries(), retransformable); } catch (RuntimeException e) { log(Level.WARNING, "Keploy dedup does not count the app's requests: their server could not be" + " changed to count them", e); @@ -333,7 +347,7 @@ static String notCounted(long endWaitMillis, List uncounted) { // endWaitMillis is how long a test's END waits for the requests that // came in since its START to finish: KEPLOY_JAVA_END_WAIT_MS (or - // keploy.java.end.wait.ms), 200 by default, 10000 at most; 0 turns the + // keploy.java.end.wait.ms), 200 by default, 2000 at most; 0 turns the // wait and the count off. static long endWaitMillis() { String value = envOrProperty("KEPLOY_JAVA_END_WAIT_MS", "keploy.java.end.wait.ms", "").trim(); @@ -345,8 +359,8 @@ static long endWaitMillis() { if (millis > MAX_END_WAIT_MILLIS) { if (END_WAIT_CAPPED_LOGGED.compareAndSet(false, true)) { log(Level.WARNING, "Keploy dedup: KEPLOY_JAVA_END_WAIT_MS is " + millis + ", so a test's END" - + " waits up to " + MAX_END_WAIT_MILLIS + " ms, its most, so that Keploy has the" - + " test's record before it stops waiting for it", null); + + " waits up to " + MAX_END_WAIT_MILLIS + " ms, its most: a longer wait would cost" + + " each test more than the agent's overhead allows", null); } return MAX_END_WAIT_MILLIS; } diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/ServerEntries.java b/keploy-sdk/src/main/java/io/keploy/dedup/ServerEntries.java index 99104408..e7824a2b 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/ServerEntries.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/ServerEntries.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.WeakHashMap; import java.util.concurrent.CopyOnWriteArraySet; import java.util.logging.Level; import java.util.logging.Logger; @@ -31,8 +32,18 @@ * {@link RequestGate#exit(Object)}, however it returns or throws. Nothing * else in the server's code changes. * - *

    Only classes loaded from now on change (keploy-sdk.jar asks for no - * retransform), so a server started before the SDK is not counted, and + *

    It is added as a transformer that can retransform, so the JVM runs it + * after every transformer that cannot, as JaCoCo's, whatever the order the + * agents were attached in: JaCoCo instruments, and ids, the server's own + * class, and this wraps JaCoCo's copy. When another agent retransforms or + * redefines a class this changed as it loaded, the JVM passes the class as + * it was before this changed it (a retransform) or the redefiner's bytes (a + * redefine), and this changes them again, as the JVM refuses a class that + * has lost a method, unless they already carry the change, which the JVM + * then takes as they are. + * + *

    Only classes loaded from now on change (the JVM refuses a method added + * to a loaded class), so a server started before the SDK is not counted, and * neither is one whose class loader does not see keploy-sdk.jar's classes, * as in a module system (both said in the log). A server in a named module * (Tomcat's jar on the module path) is counted: the JVM makes a module whose @@ -79,6 +90,10 @@ final class ServerEntries implements ClassFileTransformer { private static final Set COUNTED = new CopyOnWriteArraySet<>(); private static final Set SEEN_UNCOUNTED = new CopyOnWriteArraySet<>(); + // The classes this changed as they loaded, by their loader: the ones it + // changes again when another agent retransforms them. + private final Map> changed = new WeakHashMap<>(); + private static void add(String server, String className, String... methods) { ENTRIES.put(className, new Entry(server, new HashSet<>(Arrays.asList(methods)))); } @@ -104,7 +119,7 @@ static List uncounted() { public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { if (classBeingRedefined != null) { - return null; + return changedAgain(loader, className, classfileBuffer); } Entry entry = ENTRIES.get(className); if (entry == null) { @@ -128,13 +143,24 @@ public byte[] transform(ClassLoader loader, String className, Class classBein + " reads its hits without waiting for its requests to finish"); return null; } - byte[] wrapped = wrap(classfileBuffer, entry.methods); - if (wrapped == null) { + // Bytes that already carry the change (another agent's copy of + // the class as this changed it) count as they are. + boolean carries = carriesChange(classfileBuffer); + byte[] wrapped = carries ? null : wrap(classfileBuffer, entry.methods); + if (!carries && wrapped == null) { LOGGER.log(Level.WARNING, "Keploy dedup does not count " + entry.server + "'s requests: " + name + " has no method " + entry.methods + " to count them in"); return null; } COUNTED.add(entry.server); + synchronized (changed) { + Set names = changed.get(loader); + if (names == null) { + names = new HashSet<>(); + changed.put(loader, names); + } + names.add(className); + } LOGGER.log(Level.INFO, "Keploy dedup counts " + entry.server + "'s requests (" + name + "): a" + " test's END waits for them to finish"); return wrapped; @@ -146,6 +172,45 @@ public byte[] transform(ClassLoader loader, String className, Class classBein } } + // changedAgain is a class another agent retransforms or redefines, when + // this changed it as it loaded. The JVM passes a retransform the class as + // it was before this changed it, and a redefine the redefiner's bytes, + // and refuses a class that has lost a method this added: so either is + // changed again, unless the bytes already carry the change (a redefiner's + // copy of the class as this changed it), which the JVM then takes as they + // are. Any other class is left as it is: it was loaded unchanged, and the + // JVM refuses a method added to it. + private byte[] changedAgain(ClassLoader loader, String className, byte[] classfile) { + Entry entry = ENTRIES.get(className); + if (entry == null) { + // Most classes an agent retransforms: no lock taken for them. + return null; + } + synchronized (changed) { + Set names = changed.get(loader); + if (names == null || !names.contains(className)) { + return null; + } + } + String name = className.replace('/', '.'); + try { + if (carriesChange(classfile)) { + return null; + } + byte[] wrapped = wrap(classfile, entry.methods); + if (wrapped == null) { + LOGGER.log(Level.WARNING, "Keploy dedup cannot count " + entry.server + "'s requests in " + name + + " as another agent changes it: it has no method " + entry.methods + " any more, so" + + " the JVM refuses that agent's change"); + } + return wrapped; + } catch (Throwable t) { + LOGGER.log(Level.WARNING, "Keploy dedup cannot count " + entry.server + "'s requests in " + name + + " as another agent changes it, so the JVM refuses that agent's change", t); + return null; + } + } + // seesGate says whether code of a class loader defines calls the // RequestGate END reads: one it cannot see would fail each request, and // a copy of its own would count where END never looks. @@ -162,6 +227,23 @@ private static boolean isInterface(byte[] classfile) { return (new ClassReader(classfile).getAccess() & Opcodes.ACC_INTERFACE) != 0; } + // carriesChange says whether the class already declares a method this + // renamed: only wrap names a method so. + static boolean carriesChange(byte[] classfile) { + final boolean[] carries = {false}; + new ClassReader(classfile).accept(new ClassVisitor(Opcodes.ASM9) { + @Override + public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, + String[] exceptions) { + if (name.startsWith(RENAMED)) { + carries[0] = true; + } + return null; + } + }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); + return carries[0]; + } + // wrap returns the class with each method of these names counted, or // null when it has none to count (abstract and native methods only). // transform passes over an interface before it. diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/EmbeddedJacocoTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/EmbeddedJacocoTest.java index 6155da22..6d8e0a89 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/EmbeddedJacocoTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/EmbeddedJacocoTest.java @@ -267,12 +267,14 @@ private static void assertLeftAlone(String output) { } // sdkJar is a jar whose Premain-Class is the SDK's, which is on the - // classpath it is run with. + // classpath it is run with, and that may retransform, as keploy-sdk.jar + // may. private File sdkJar() throws IOException { File sdk = tmp.newFile("keploy-sdk-" + System.nanoTime() + ".jar"); Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); manifest.getMainAttributes().putValue("Premain-Class", KeployDedupAgent.class.getName()); + manifest.getMainAttributes().putValue("Can-Retransform-Classes", "true"); try (JarOutputStream out = new JarOutputStream(new FileOutputStream(sdk), manifest)) { out.putNextEntry(new ZipEntry("META-INF/keploy-test")); out.closeEntry(); diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java index 9d2f32e3..e676358d 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/InProcessScanTest.java @@ -991,7 +991,7 @@ public void anEndWaitsForItsTestsRequestToFinishAfterItsResponse() throws Except // A filter's code after the chain runs after Keploy has the response // and sent END: read at once, END missed it, and the next START // cleared it as background, so the test's hits changed run to run. - System.setProperty("keploy.java.end.wait.ms", "10000"); + System.setProperty("keploy.java.end.wait.ms", "2000"); System.setProperty("keploy.java.dedup.diagnostics", "true"); boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath)) { @@ -1004,7 +1004,7 @@ public void anEndWaitsForItsTestsRequestToFinishAfterItsResponse() throws Except long began = System.nanoTime(); assertEquals(TAKEN, send("END test-set-0/test-1")); assertTrue("END ended when the request did, not at its bound", - System.nanoTime() - began < TimeUnit.SECONDS.toNanos(5)); + System.nanoTime() - began < TimeUnit.MILLISECONDS.toNanos(1500)); assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1,2]}}", data.next()); assertTrue(logged(" END test-set-0/test-1 took ").contains(" requests=")); @@ -1059,7 +1059,7 @@ public void anEndWhoseRequestOutrunsTheWaitIsCutAndOpensNoWindow() throws Except public void anEndDoesNotWaitForARequestThatCameInBeforeItsStart() throws Exception { // A stream opened before the test, or a request of the test before // cut at its bound: never this test's to wait for. - System.setProperty("keploy.java.end.wait.ms", "20000"); + System.setProperty("keploy.java.end.wait.ms", "2000"); boolean[] shop = agent.probes(SHOP_ID, SHOP, 4); try (DataReceiver data = new DataReceiver(KeployDedupAgent.dataSocketPath); Request stream = new Request(hit(shop, 0), hit(shop, 3))) { @@ -1069,7 +1069,7 @@ public void anEndDoesNotWaitForARequestThatCameInBeforeItsStart() throws Excepti shop[1] = true; long began = System.nanoTime(); assertEquals(TAKEN, send("END test-set-0/test-1")); - assertTrue(System.nanoTime() - began < TimeUnit.SECONDS.toNanos(10)); + assertTrue(System.nanoTime() - began < TimeUnit.MILLISECONDS.toNanos(1500)); assertEquals("{\"id\":\"test-set-0/test-1\",\"executedLinesByFile\":{\"" + SHOP + "\":[1]}}", data.next()); } @@ -1105,15 +1105,15 @@ public void theEndWaitIsReadFromItsSetting() throws ReflectiveOperationException assertEquals(0, KeployDedupAgent.endWaitMillis()); System.setProperty("keploy.java.end.wait.ms", "1500"); assertEquals(1500, KeployDedupAgent.endWaitMillis()); - System.setProperty("keploy.java.end.wait.ms", "10000"); - assertEquals(10000, KeployDedupAgent.endWaitMillis()); + System.setProperty("keploy.java.end.wait.ms", "2000"); + assertEquals(2000, KeployDedupAgent.endWaitMillis()); assertEquals(0, count("its most")); - System.setProperty("keploy.java.end.wait.ms", "10001"); - assertEquals(10000, KeployDedupAgent.endWaitMillis()); - System.setProperty("keploy.java.end.wait.ms", "60000"); - assertEquals(10000, KeployDedupAgent.endWaitMillis()); + System.setProperty("keploy.java.end.wait.ms", "2001"); + assertEquals(2000, KeployDedupAgent.endWaitMillis()); + System.setProperty("keploy.java.end.wait.ms", "10000"); + assertEquals(2000, KeployDedupAgent.endWaitMillis()); assertEquals("said once in a JVM", 1, - count("KEPLOY_JAVA_END_WAIT_MS is 10001, so a test's END waits up to 10000 ms, its most")); + count("KEPLOY_JAVA_END_WAIT_MS is 2001, so a test's END waits up to 2000 ms, its most")); assertEquals(1, count("its most")); System.setProperty("keploy.java.end.wait.ms", "-1"); assertEquals(200, KeployDedupAgent.endWaitMillis()); diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java index 6c9b96d3..c53c9911 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java @@ -1,5 +1,6 @@ package io.keploy.dedup; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -12,6 +13,7 @@ import java.lang.reflect.Modifier; import java.net.URL; import java.net.URLClassLoader; +import java.util.Collections; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.logging.Handler; @@ -277,11 +279,83 @@ public void anyOtherClassIsLeftAlone() throws Exception { getClass().getClassLoader())); assertNotNull(logged("WARNING Keploy dedup does not count Tomcat's requests:" + " org.apache.catalina.connector.CoyoteAdapter has no method")); - // A class redefined later is never changed: the SDK asks for no retransform. + // A class retransformed later that was not changed as it loaded (it + // loaded before the SDK started) is left as it is. assertNull(new ServerEntries().transform(getClass().getClassLoader(), ENTRIES[0][0], Object.class, null, standIn(ENTRIES[0][0], "service", false, Opcodes.V1_8, 0))); } + @Test + public void aClassChangedAsItLoadedIsChangedAgainWhenRetransformed() throws Exception { + ServerEntries entries = new ServerEntries(); + ClassLoader loader = getClass().getClassLoader(); + byte[] classfile = standIn(ENTRIES[0][0], "service", false, Opcodes.V1_8, 0); + assertNull("loaded before the SDK started", + entries.transform(loader, ENTRIES[0][0], Object.class, null, classfile)); + byte[] loaded = entries.transform(loader, ENTRIES[0][0], null, null, classfile); + assertNotNull(loaded); + // Another agent retransforms it: the JVM passes the class as it was + // before the SDK changed it, which changes it the same way again. + byte[] again = entries.transform(loader, ENTRIES[0][0], Object.class, null, classfile); + assertArrayEquals(loaded, again); + Class type = load(ENTRIES[0][0], again); + runningInBody = -1; + type.getMethod("service", BODY_TYPES).invoke(type.getDeclaredConstructor().newInstance(), args(4)); + assertEquals("counted while the server ran it", 1, runningInBody); + // Not another loader's class of that name, nor another class of that + // loader, nor a class another ServerEntries changed. + try (URLClassLoader other = new URLClassLoader(new URL[0], loader)) { + assertNull(entries.transform(other, ENTRIES[0][0], Object.class, null, classfile)); + } + assertNull(entries.transform(loader, ENTRIES[6][0], Object.class, null, + standIn(ENTRIES[6][0], "executeRootHandler", true, Opcodes.V1_8, 0))); + assertNull(new ServerEntries().transform(loader, ENTRIES[0][0], Object.class, null, classfile)); + assertNull(logged.toString(), logged("WARNING")); + // A class it cannot change again: the JVM refuses that retransform, + // and the log says why. + assertNull(entries.transform(loader, ENTRIES[0][0], Object.class, null, + standIn(ENTRIES[0][0], "other", false, Opcodes.V1_8, 0))); + assertNotNull(logged.toString(), logged("WARNING Keploy dedup cannot count Tomcat's requests in" + + " org.apache.catalina.connector.CoyoteAdapter as another agent changes it: it has no method")); + assertNull(entries.transform(loader, ENTRIES[0][0], Object.class, null, new byte[] {1, 2, 3})); + assertNotNull(logged.toString(), logged("WARNING Keploy dedup cannot count Tomcat's requests in" + + " org.apache.catalina.connector.CoyoteAdapter as another agent changes it, so the JVM")); + } + + @Test + public void bytesThatAlreadyCarryTheChangeAreTakenAsTheyAre() throws Exception { + ServerEntries entries = new ServerEntries(); + ClassLoader loader = getClass().getClassLoader(); + byte[] classfile = standIn(ENTRIES[0][0], "service", false, Opcodes.V1_8, 0); + byte[] loaded = entries.transform(loader, ENTRIES[0][0], null, null, classfile); + assertNotNull(loaded); + assertTrue(ServerEntries.carriesChange(loaded)); + assertFalse(ServerEntries.carriesChange(classfile)); + // Changed twice, the class would declare the renamed method twice, + // which the JVM refuses. + try { + load(ENTRIES[0][0], ServerEntries.wrap(loaded, Collections.singleton("service"))); + fail("a class changed twice loaded"); + } catch (ClassFormatError e) { + // As the JVM refuses it. + } + // Another agent redefines it with its copy of the class as the SDK + // changed it (as a tool's dump has it): the JVM takes it as it is. + assertNull(entries.transform(loader, ENTRIES[0][0], Object.class, null, loaded)); + // A class that loads with the change already in it counts as it is, + // and a later redefine with the server's own class changes it again. + ServerEntries fresh = new ServerEntries(); + logged.clear(); + assertNull(fresh.transform(loader, ENTRIES[0][0], null, null, loaded)); + assertNotNull(logged.toString(), logged("INFO Keploy dedup counts Tomcat's requests")); + Class type = load(ENTRIES[0][0], loaded); + runningInBody = -1; + type.getMethod("service", BODY_TYPES).invoke(type.getDeclaredConstructor().newInstance(), args(4)); + assertEquals("counted while the server ran it", 1, runningInBody); + assertArrayEquals(loaded, fresh.transform(loader, ENTRIES[0][0], Object.class, null, classfile)); + assertNull(logged.toString(), logged("WARNING")); + } + @Test public void aServerWhoseLoaderDoesNotSeeTheGateIsNotChanged() throws Exception { byte[] classfile = standIn(ENTRIES[6][0], "executeRootHandler", true, Opcodes.V1_8, 0); diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/ServerTailTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/ServerTailTest.java index 01da8709..c37a20e7 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/ServerTailTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/ServerTailTest.java @@ -26,9 +26,11 @@ import java.util.jar.Manifest; import java.util.zip.ZipEntry; import org.example.servers.HelloServlet; +import org.example.servers.Retransformer; import org.example.servers.ServerApp; import org.example.servers.Tail; import org.example.servers.TailFilter; +import org.jacoco.agent.AgentJar; import org.junit.Assume; import org.junit.Rule; import org.junit.Test; @@ -45,13 +47,14 @@ public class ServerTailTest { private static final String TAIL = Tail.class.getName().replace('.', '/'); + private static final String ADAPTER = "org.apache.catalina.connector.CoyoteAdapter"; @Rule public TemporaryFolder tmp = new TemporaryFolder(); @Test public void tomcatsEndWaitsForTheFiltersCodeAfterTheResponse() throws Exception { - Result result = run("tomcat", "10000", 1500); + Result result = run("tomcat", "2000", 500); assertTrue(result.log, endLine(result.log).contains(" requests=")); assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); assertFalse(result.cov, result.cov.contains("windowCut")); @@ -62,7 +65,7 @@ public void tomcatsEndWaitsForTheFiltersCodeAfterTheResponse() throws Exception @Test public void jettysEndWaitsForTheFiltersCodeAfterTheResponse() throws Exception { - Result result = run("jetty", "10000", 1500); + Result result = run("jetty", "2000", 500); assertTrue(result.log, endLine(result.log).contains(" requests=")); assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); assertFalse(result.cov, result.cov.contains("windowCut")); @@ -72,7 +75,7 @@ public void jettysEndWaitsForTheFiltersCodeAfterTheResponse() throws Exception { @Test public void undertowsEndWaitsForTheFiltersCodeAfterTheResponse() throws Exception { - Result result = run("undertow", "10000", 1500); + Result result = run("undertow", "2000", 500); assertTrue(result.log, endLine(result.log).contains(" requests=")); assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); assertFalse(result.cov, result.cov.contains("windowCut")); @@ -88,7 +91,7 @@ public void tomcatInANamedModuleCountsItsRequests() throws Exception { // that read when an agent changes a class of a named module. Assume.assumeFalse("modules are Java 9's", System.getProperty("java.specification.version") .startsWith("1.")); - Fork fork = new Fork("tomcat", "10000", 1500); + Fork fork = new Fork("tomcat", "2000", 500); fork.modules = true; Result result = run(fork); assertTrue(result.log, result.log.contains("MODULE module org.apache.tomcat.embed.core")); @@ -106,7 +109,7 @@ public void jacocoMeasuresTheServersOwnClassAsTheIndexHasIt() throws Exception { // embedded JaCoCo instruments CoyoteAdapter too: it must see the // class's own bytes, the ones the index ids, not the counted ones, // or every test's record names it in classMismatch. - Fork fork = new Fork("tomcat", "10000", 1500); + Fork fork = new Fork("tomcat", "2000", 500); fork.indexTomcat = true; Result result = run(fork); assertTrue(result.cov, result.cov.contains("\"org/apache/catalina/connector/CoyoteAdapter\":[")); @@ -135,6 +138,110 @@ public void withTheWaitOffTheEndMissesTheFiltersCode() throws Exception { assertEquals("said once", result.log.indexOf(why), result.log.lastIndexOf(why)); } + @Test + public void jacocoAttachedBeforeTheSdkMeasuresTheServersOwnClass() throws Exception { + assertJacocoMeasuresTheServersOwnClass(jacocoAttached("first", true)); + } + + @Test + public void jacocoAttachedAfterTheSdkMeasuresTheServersOwnClass() throws Exception { + // The SDK's transformer can retransform, so the JVM runs it after + // JaCoCo's, which cannot, whatever the -javaagent order: JaCoCo ids + // the server's own class, as the index does. + assertJacocoMeasuresTheServersOwnClass(jacocoAttached("last", true)); + } + + @Test + public void theSdkInJavaToolOptionsWithJacocoOnTheCommandLine() throws Exception { + // JAVA_TOOL_OPTIONS' agents start before the command line's. + assertJacocoMeasuresTheServersOwnClass(jacocoAttached("tool", true)); + } + + @Test + public void aJarThatCannotRetransformStillCountsTheRequests() throws Exception { + // A jar that carries the SDK without Can-Retransform-Classes: the + // requests count, the SDK's transformer runs in the order it was + // added, and the WARNING says what that costs. + Result result = jacocoAttached("last", false); + assertTrue(result.log, endLine(result.log).contains(" requests=")); + assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); + assertTrue(result.cov, mismatched(result.cov).contains("org/apache/catalina/connector/CoyoteAdapter")); + assertTrue(result.log, result.log.contains("WARNING: Keploy dedup: the jar keploy-sdk.jar runs from has no" + + " Can-Retransform-Classes: true in its manifest, or this JVM cannot retransform, so a JaCoCo agent" + + " attached after it instruments the server classes it changes")); + } + + @Test + public void aJarThatCannotRetransformWithTheSdksOwnJacocoSaysNothing() throws Exception { + // The SDK's own JaCoCo is added before its transformer, so it ids + // the server's own class in any case: nothing to warn of. + Fork fork = new Fork("tomcat", "2000", 500); + fork.indexTomcat = true; + fork.retransformable = false; + Result result = run(fork); + assertTrue(result.log, endLine(result.log).contains(" requests=")); + assertTrue(result.cov, result.cov.contains("\"org/apache/catalina/connector/CoyoteAdapter\":[")); + assertFalse(result.cov, mismatched(result.cov).contains("org/apache/catalina/connector/CoyoteAdapter")); + assertFalse(result.log, result.log.contains("Can-Retransform-Classes")); + } + + @Test + public void anotherAgentsRetransformKeepsTheServerCounted() throws Exception { + Fork fork = new Fork("tomcat", "2000", 500); + fork.retransformer = ""; + Result result = run(fork); + assertTrue(result.log, result.log.contains("RETRANSFORMED " + ADAPTER + " seen=1")); + assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); + assertTrue(result.log, endLine(result.log).contains(" requests=")); + } + + @Test + public void aRedefineWithTheClassAsTheSdkChangedItKeepsTheServerCounted() throws Exception { + // A hot-swap tool redefines CoyoteAdapter with its dump of the class + // as the SDK changed it: changed again, it would declare the renamed + // method twice, and the JVM would refuse it. + Fork fork = new Fork("tomcat", "2000", 500); + fork.retransformer = "redefine"; + Result result = run(fork); + assertTrue(result.log, result.log.contains("REDEFINED " + ADAPTER)); + assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); + assertTrue(result.log, endLine(result.log).contains(" requests=")); + } + + @Test + public void aServerLoadedBeforeTheSdkIsLeftAsItIsWhenRetransformed() throws Exception { + // The JVM refuses a method added to a loaded class: had the SDK + // changed it on another agent's retransform, that would fail. + Fork fork = new Fork("tomcat", "2000", 500); + fork.retransformer = "load"; + Result result = run(fork); + assertTrue(result.log, result.log.contains("RETRANSFORMED " + ADAPTER + " seen=1")); + assertFalse(result.log, endLine(result.log).contains(" requests=")); + assertTrue(result.log, result.log.contains("no Tomcat, Jetty or Undertow request entry loaded after" + + " keploy-sdk.jar started")); + } + + // jacocoAttached runs a test on Tomcat with JaCoCo's own agent attached + // in that order (first or last on the command line, or last with the SDK + // in JAVA_TOOL_OPTIONS), measuring the app and CoyoteAdapter, which the + // index holds too. + private Result jacocoAttached(String order, boolean retransformable) throws Exception { + Fork fork = new Fork("tomcat", "2000", 500); + fork.indexTomcat = true; + fork.jacoco = order; + fork.retransformable = retransformable; + return run(fork); + } + + private static void assertJacocoMeasuresTheServersOwnClass(Result result) { + assertTrue(result.log, result.log.contains("this JVM has a JaCoCo agent attached")); + assertTrue(result.cov, result.cov.contains("\"org/apache/catalina/connector/CoyoteAdapter\":[")); + assertTrue(result.cov, result.cov.contains("\"" + TAIL + "\"")); + assertFalse(result.cov, mismatched(result.cov).contains("org/apache/catalina/connector/CoyoteAdapter")); + assertTrue(result.log, endLine(result.log).contains(" requests=")); + assertFalse(result.log, result.log.contains("Can-Retransform-Classes")); + } + @Test public void aTailPastTheWaitCutsTheEnd() throws Exception { Result result = run("tomcat", "100", 5000); @@ -153,7 +260,12 @@ private static final class Result { // wait at endWait, tests tests whose request's filter runs tailMillis // after the response; with modules, Tomcat's jars are on the module // path instead of the class path; with indexTomcat, the index (and so - // the embedded JaCoCo) holds Tomcat's classes as well as the app's. + // the embedded JaCoCo) holds Tomcat's classes as well as the app's; + // jacoco attaches JaCoCo's own agent (first, last, or last with the SDK + // in JAVA_TOOL_OPTIONS); retransformer, when not null, attaches + // Retransformer with it as its args ("load": before the SDK; "" and + // "redefine": after it); without retransformable, the SDK's jar has no + // Can-Retransform-Classes. private static final class Fork { final String server; final String endWait; @@ -161,6 +273,9 @@ private static final class Fork { int tests = 1; boolean modules; boolean indexTomcat; + String jacoco; + String retransformer; + boolean retransformable = true; Fork(String server, String endWait, long tailMillis) { this.server = server; @@ -195,7 +310,27 @@ private Result run(Fork fork) throws Exception { List command = new ArrayList<>(); command.add(new File(System.getProperty("java.home"), "bin/java").getPath()); command.add("-Djava.io.tmpdir=" + tmp.newFolder().getPath()); - command.add("-javaagent:" + sdkJar().getPath()); + String sdkAgent = "-javaagent:" + sdkJar(fork.retransformable).getPath(); + String jacoco = fork.jacoco == null ? null : "-javaagent:" + jacocoJar().getPath() + + "=includes=org.example.*:" + ADAPTER + ",output=none"; + if ("load".equals(fork.retransformer)) { + command.add("-javaagent:" + retransformerJar().getPath() + "=load"); + } + if ("first".equals(fork.jacoco)) { + command.add(jacoco); + } + if (!"tool".equals(fork.jacoco)) { + command.add(sdkAgent); + } + if ("last".equals(fork.jacoco) || "tool".equals(fork.jacoco)) { + command.add(jacoco); + } + if ("".equals(fork.retransformer)) { + command.add("-javaagent:" + retransformerJar().getPath()); + } + if ("redefine".equals(fork.retransformer)) { + command.add("-javaagent:" + retransformerJar().getPath() + "=redefine"); + } List classPath = new ArrayList<>(); List modulePath = new ArrayList<>(); // Absolute: the app runs in a dir of its own. @@ -235,6 +370,9 @@ private Result run(Fork fork) throws Exception { env.put("KEPLOY_COVERAGE_ENDPOINT", "127.0.0.1:" + collector.getLocalPort()); env.put("KEPLOY_JAVA_DEDUP_WARMUP_DISABLED", "true"); env.put("KEPLOY_JAVA_END_WAIT_MS", fork.endWait); + if ("tool".equals(fork.jacoco)) { + env.put("JAVA_TOOL_OPTIONS", sdkAgent); + } Process app = builder.start(); try { int port = port(log, app); @@ -345,16 +483,45 @@ private static void send(OutputStream out, String command) throws IOException { } // sdkJar is a jar whose Premain-Class is the SDK's, which is on the - // classpath it is run with. - private File sdkJar() throws IOException { - File sdk = tmp.newFile("keploy-sdk-" + System.nanoTime() + ".jar"); + // classpath it is run with, and that may retransform, as keploy-sdk.jar + // may, when retransformable. + private File sdkJar(boolean retransformable) throws IOException { + return agentJar("keploy-sdk", KeployDedupAgent.class, retransformable); + } + + // retransformerJar is a jar whose Premain-Class is Retransformer, which + // may retransform and redefine classes. + private File retransformerJar() throws IOException { + return agentJar("retransformer", Retransformer.class, true, true); + } + + // jacocoJar is JaCoCo's own agent, jacocoagent.jar. + private File jacocoJar() throws IOException { + File jacoco = tmp.newFile("jacocoagent-" + System.nanoTime() + ".jar"); + AgentJar.extractTo(jacoco); + return jacoco; + } + + private File agentJar(String name, Class premain, boolean retransformable) throws IOException { + return agentJar(name, premain, retransformable, false); + } + + private File agentJar(String name, Class premain, boolean retransformable, boolean redefinable) + throws IOException { + File jar = tmp.newFile(name + "-" + System.nanoTime() + ".jar"); Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); - manifest.getMainAttributes().putValue("Premain-Class", KeployDedupAgent.class.getName()); - try (JarOutputStream out = new JarOutputStream(new FileOutputStream(sdk), manifest)) { + manifest.getMainAttributes().putValue("Premain-Class", premain.getName()); + if (retransformable) { + manifest.getMainAttributes().putValue("Can-Retransform-Classes", "true"); + } + if (redefinable) { + manifest.getMainAttributes().putValue("Can-Redefine-Classes", "true"); + } + try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar), manifest)) { out.putNextEntry(new ZipEntry("META-INF/keploy-test")); out.closeEntry(); } - return sdk; + return jar; } } diff --git a/keploy-sdk/src/test/java/org/example/servers/Retransformer.java b/keploy-sdk/src/test/java/org/example/servers/Retransformer.java new file mode 100644 index 00000000..76f09927 --- /dev/null +++ b/keploy-sdk/src/test/java/org/example/servers/Retransformer.java @@ -0,0 +1,88 @@ +package org.example.servers; + +import java.lang.instrument.ClassDefinition; +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.Instrumentation; +import java.security.ProtectionDomain; + +/** + * Retransformer is a -javaagent of ServerTailTest's JVMs that stands for + * another agent (an APM agent, a profiler) retransforming Tomcat's + * CoyoteAdapter once the server runs. With "load", its premain also loads + * that class, before the agents listed after it start. With "redefine", it + * keeps that class's bytes as the agents listed before it changed them as + * it loaded (as a tool's dump has them), and redefines the class with them + * instead (as a hot-swap tool does with a class it dumped). + */ +public final class Retransformer { + + static final String ADAPTER = "org.apache.catalina.connector.CoyoteAdapter"; + + private static volatile Instrumentation instrumentation; + private static volatile int seen; + private static volatile byte[] loaded; + + private Retransformer() { + } + + public static void premain(String args, Instrumentation inst) throws Exception { + instrumentation = inst; + if ("load".equals(args)) { + Class.forName(ADAPTER, false, ClassLoader.getSystemClassLoader()); + } + if ("redefine".equals(args)) { + inst.addTransformer(new ClassFileTransformer() { + @Override + public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, + ProtectionDomain protectionDomain, byte[] classfileBuffer) { + if (classBeingRedefined == null && className.equals(ADAPTER.replace('.', '/'))) { + loaded = classfileBuffer.clone(); + } + return null; + } + }, true); + } + } + + // afterStart retransforms CoyoteAdapter when this agent runs, and says + // how it went: RETRANSFORMED and how many times its own transformer saw + // the class, or RETRANSFORM FAILED and why; with "redefine", REDEFINED + // or REDEFINE FAILED. + static void afterStart() { + Instrumentation inst = instrumentation; + if (inst == null) { + return; + } + byte[] bytes = loaded; + if (bytes != null) { + try { + inst.redefineClasses(new ClassDefinition(Class.forName(ADAPTER), bytes)); + System.out.println("REDEFINED " + ADAPTER); + } catch (Throwable t) { + System.out.println("REDEFINE FAILED " + t); + } + System.out.flush(); + return; + } + ClassFileTransformer counter = new ClassFileTransformer() { + @Override + public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, + ProtectionDomain protectionDomain, byte[] classfileBuffer) { + if (classBeingRedefined != null && className.equals(ADAPTER.replace('.', '/'))) { + seen++; + } + return null; + } + }; + inst.addTransformer(counter, true); + try { + inst.retransformClasses(Class.forName(ADAPTER)); + System.out.println("RETRANSFORMED " + ADAPTER + " seen=" + seen); + } catch (Throwable t) { + System.out.println("RETRANSFORM FAILED " + t); + } finally { + inst.removeTransformer(counter); + } + System.out.flush(); + } +} diff --git a/keploy-sdk/src/test/java/org/example/servers/ServerApp.java b/keploy-sdk/src/test/java/org/example/servers/ServerApp.java index 66954859..0ef355b2 100644 --- a/keploy-sdk/src/test/java/org/example/servers/ServerApp.java +++ b/keploy-sdk/src/test/java/org/example/servers/ServerApp.java @@ -57,6 +57,7 @@ public static void main(String[] args) throws Exception { } catch (NoSuchMethodException e) { // Java 8. } + Retransformer.afterStart(); } else if (args[0].equals("jetty")) { Server jetty = new Server(0); ServletContextHandler context = new ServletContextHandler(); From 2751440dc7b92e1a978c3c9104980922f0fbd9aa Mon Sep 17 00:00:00 2001 From: slayerjain Date: Fri, 20 Nov 2026 11:51:03 +0530 Subject: [PATCH 09/14] test(sdk): run the built jar on Tomcat with JaCoCo attached after it The rows that attach JaCoCo's own agent after keploy-sdk.jar write an agent jar of their own, so none read the manifest the release ships: a keploy-sdk.jar built without Can-Retransform-Classes: true passed every row, while a JaCoCo agent attached after it would name the server's class in classMismatch. scripts/smoke-javaagent.sh now runs the built jar on a real embedded Tomcat 9.0.98, with JaCoCo's own agent attached after it and the index holding Tomcat's classes, as an uber jar's does. Its servlet runs more code after the response once the test's END waits for the request. The case fails unless the INDEX record says the attached JaCoCo measures and names Tomcat's jar among its roots, the test's record measures CoyoteAdapter and that later code and names no class in classMismatch, the log says the SDK counts Tomcat's requests, the END's diagnostics show the wait for them, and no WARNING names Can-Retransform-Classes. A failed check ends the harness's JVM with 1 (Tomcat's threads are not daemons), its sockets and HTTP reads time out, and the case is bounded at 180 s, so a regression fails the job instead of hanging it. With the manifest entry set to false, the case fails on JDK 21 and on JDK 8. ServerEntriesTest wraps JaCoCo's instrumented copy of a stand-in of CoyoteAdapter at class versions 52, 55, 61 and 65, as far as the JVM can define them: JaCoCo keeps its probes in a field at 52 and reads them through a dynamic constant from 55 on. The wrapped copy loads through the verifier, counts its request, and reports its probes under the CRC64 of the class's original bytes, the id the index holds, not the id of the wrapped bytes. Signed-off-by: slayerjain --- .../io/keploy/dedup/ServerEntriesTest.java | 71 +++++ scripts/smoke-javaagent.sh | 250 ++++++++++++++++++ 2 files changed, 321 insertions(+) diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java index c53c9911..61598497 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/ServerEntriesTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -20,6 +21,13 @@ import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; +import org.jacoco.core.data.ExecutionData; +import org.jacoco.core.data.ExecutionDataStore; +import org.jacoco.core.data.SessionInfoStore; +import org.jacoco.core.instr.Instrumenter; +import org.jacoco.core.internal.data.CRC64; +import org.jacoco.core.runtime.LoggerRuntime; +import org.jacoco.core.runtime.RuntimeData; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -186,6 +194,56 @@ public void aClassFromBeforeStackMapsIsCountedToo() throws Exception { assertEquals(0, RequestGate.running()); } + @Test + public void jacocosCopyOfTheServersClassIsCountedAndKeepsItsIdAtEachClassVersion() throws Exception { + // The JVM runs this transformer after JaCoCo's, so it wraps the copy + // JaCoCo instrumented: at Java 8's class version and at 11's, 17's + // and 21's, as far as this JVM can define them. The copy must pass the + // verifier (HotSpot verifies by default every class a loader other + // than the boot loader defines), count its requests, and report its + // probes under JaCoCo's id of the server's own bytes, the CRC64 of + // them: the id the index holds, not the id of the wrapped bytes, + // which JaCoCo would write had it run after this transformer. + String name = ENTRIES[0][0]; + int feature = Integer.parseInt(System.getProperty("java.specification.version").replaceFirst("^1\\.", "")); + int ran = 0; + for (int version : new int[] {Opcodes.V1_8, Opcodes.V11, Opcodes.V17, Opcodes.V21}) { + if (version > 44 + feature) { + continue; + } + byte[] original = standIn(name, "service", false, version, 0); + LoggerRuntime runtime = new LoggerRuntime(); + RuntimeData data = new RuntimeData(); + runtime.startup(data); + try { + byte[] instrumented = new Instrumenter(runtime).instrument(original, name); + // Up to Java 10's class version JaCoCo keeps its probes in a + // field; from 11's on it reads them through a dynamic constant. + assertEquals("class version " + version, version < Opcodes.V11, hasField(instrumented, "$jacocoData")); + Class type = load(name, transformed(instrumented)); + Method method = type.getMethod("service", BODY_TYPES); + runningInBody = -1; + assertEquals("class version " + version, 2L + 3L + 4L + 2L + 1L + 1L + 1L + 1L + 1L + 1L, + method.invoke(type.getDeclaredConstructor().newInstance(), args(4))); + assertEquals("class version " + version, 1, runningInBody); + assertEquals("class version " + version, 0, RequestGate.running()); + ExecutionDataStore store = new ExecutionDataStore(); + data.collect(store, new SessionInfoStore(), false); + assertEquals("class version " + version, 1, store.getContents().size()); + ExecutionData probes = store.getContents().iterator().next(); + assertEquals("class version " + version, name, probes.getName()); + assertEquals("class version " + version, CRC64.classId(original), probes.getId()); + assertNotEquals("class version " + version, CRC64.classId(transformed(original)), probes.getId()); + assertTrue("class version " + version, probes.hasHits()); + } finally { + runtime.shutdown(); + } + ran++; + } + assertEquals("the class versions this JVM can define", feature >= 21 ? 4 : feature >= 17 ? 3 + : feature >= 11 ? 2 : 1, ran); + } + @Test public void aSynchronizedEntryKeepsItsLockOnTheServersOwnCode() throws Exception { Class type = load(ENTRIES[0][0], transformed(standIn(ENTRIES[0][0], "service", false, Opcodes.V1_8, @@ -466,6 +524,19 @@ private static void callsBody(ClassWriter writer, int access, String name, boole mv.visitEnd(); } + private static boolean hasField(byte[] classfile, final String name) { + final boolean[] found = new boolean[1]; + new org.objectweb.asm.ClassReader(classfile).accept(new org.objectweb.asm.ClassVisitor(Opcodes.ASM9) { + @Override + public org.objectweb.asm.FieldVisitor visitField(int access, String field, String descriptor, + String signature, Object value) { + found[0] |= field.equals(name); + return null; + } + }, 0); + return found[0]; + } + private static Class load(String internalName, byte[] classfile) { return new ClassLoader(ServerEntriesTest.class.getClassLoader()) { Class define() { diff --git a/scripts/smoke-javaagent.sh b/scripts/smoke-javaagent.sh index 6effe558..3e3edb8c 100755 --- a/scripts/smoke-javaagent.sh +++ b/scripts/smoke-javaagent.sh @@ -523,6 +523,256 @@ if [[ "${dump_rc}" -ne 0 ]] \ fi echo "jacoco-dump: with KEPLOY_JAVA_SCAN=off each END took JaCoCo's dump" +# keploy-sdk.jar as built, on a real Tomcat, with JaCoCo's own agent attached +# after it and the index holding Tomcat's classes (as an uber jar's does). +# The jar's manifest lets the SDK's transformer retransform, so the JVM runs +# JaCoCo's before it, whatever the -javaagent order: JaCoCo ids Tomcat's own +# CoyoteAdapter, as the index read it, so the record measures it and names +# no class in classMismatch, and the SDK still counts Tomcat's requests: the +# servlet's code after the response (Tail) runs while the END waits for the +# request, and is in the record. The unit rows attach agent jars they write +# themselves; this case reads the manifest the release ships. +# The Tomcat keploy-sdk/pom.xml tests with (ServerTailTest's). +tomcat_version="$(sed -n '/tomcat-embed-core<\/artifactId>/{n;s/.*\([^<]*\)<\/version>.*/\1/p;}' \ + "${repo_root}/keploy-sdk/pom.xml")" +if [[ ! "${tomcat_version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "tomcat-jacoco-after: keploy-sdk/pom.xml names no tomcat-embed-core version on the line after its artifactId (read '${tomcat_version}')" >&2 + exit 1 +fi +for artifact in "org.apache.tomcat.embed:tomcat-embed-core:${tomcat_version}" \ + "org.apache.tomcat:tomcat-annotations-api:${tomcat_version}"; do + mvn -q -f "${repo_root}/pom.xml" org.apache.maven.plugins:maven-dependency-plugin:3.6.1:copy \ + -Dartifact="${artifact}" -DoutputDirectory="${work_dir}/tomcat-libs" +done +tomcat_core="${work_dir}/tomcat-libs/tomcat-embed-core-${tomcat_version}.jar" +tomcat_libs="$(find "${work_dir}/tomcat-libs" -name '*.jar' | sort | paste -sd: -)" + +mkdir -p "${work_dir}/tomcat-src/smoke" "${work_dir}/tomcat-classes" +cat > "${work_dir}/tomcat-src/smoke/TomcatHarness.java" <<'JAVA' +package smoke; + +import org.apache.catalina.Context; +import org.apache.catalina.startup.Tomcat; +import org.newsclub.net.unix.AFUNIXServerSocket; +import org.newsclub.net.unix.AFUNIXSocket; +import org.newsclub.net.unix.AFUNIXSocketAddress; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public final class TomcatHarness { + private static final File CONTROL_SOCKET = new File("/tmp/coverage_control.sock"); + private static final File DATA_SOCKET = new File("/tmp/coverage_data.sock"); + private static final String ADAPTER = "org/apache/catalina/connector/CoyoteAdapter"; + + // Hello answers, then runs Tail once the END waits for the request + // (a thread is in RequestGate.await), or after 1500 ms when none does: + // then the END did not wait, and Tail is not in the record. + public static final class Hello extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentLength(5); + response.getWriter().write("hello"); + response.flushBuffer(); + long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(1500L); + try { + while (!endWaits() && System.nanoTime() < deadline) { + Thread.sleep(10L); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + Tail.run(); + } + + private static boolean endWaits() { + for (StackTraceElement[] stack : Thread.getAllStackTraces().values()) { + for (StackTraceElement frame : stack) { + if ("io.keploy.dedup.RequestGate".equals(frame.getClassName()) + && "await".equals(frame.getMethodName())) { + return true; + } + } + } + return false; + } + } + + static final class Tail { + static volatile int runs; + + static void run() { + runs++; + } + } + + // main ends the JVM with 1 when a check fails: Tomcat's utility threads + // are not daemons, so a check that threw with Tomcat still running would + // leave the JVM up, and the case would hang instead of failing. + public static void main(String[] args) { + try { + run(args[0], new File(args[1])); + } catch (Throwable t) { + t.printStackTrace(); + System.exit(1); + } + } + + private static void run(String mode, File base) throws Exception { + if (!base.mkdirs()) { + throw new IllegalStateException("failed to create " + base); + } + Tomcat tomcat = new Tomcat(); + tomcat.setBaseDir(base.getPath()); + tomcat.setPort(0); + tomcat.getConnector().setProperty("address", "127.0.0.1"); + Context context = tomcat.addContext("", base.getPath()); + Tomcat.addServlet(context, "hello", new Hello()); + context.addServletMappingDecoded("/hello", "hello"); + tomcat.start(); + int port = tomcat.getConnector().getLocalPort(); + + if (DATA_SOCKET.exists() && !DATA_SOCKET.delete()) { + throw new IllegalStateException("failed to delete " + DATA_SOCKET); + } + final BlockingQueue records = new LinkedBlockingQueue(); + Thread receiver = new Thread(new Runnable() { + @Override + public void run() { + try (AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()) { + server.bind(AFUNIXSocketAddress.of(DATA_SOCKET), 8); + for (;;) { + try (AFUNIXSocket socket = server.accept()) { + records.add(readAll(socket.getInputStream())); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }, "coverage-data-receiver"); + receiver.setDaemon(true); + receiver.start(); + long deadline = System.currentTimeMillis() + 10000L; + while (!DATA_SOCKET.exists() && System.currentTimeMillis() < deadline) { + Thread.sleep(50L); + } + + String id = "test-set-0/" + mode; + command("START " + id); + String body = get(port); + command("END " + id); + String report = next(records, mode); + // The JaCoCo measuring is the one attached after keploy-sdk.jar (the + // SDK started none of its own), and the index read Tomcat's jar. + if (!report.startsWith("{\"kind\":\"index\",") + || !report.contains("\"source\":\"override\"") + || !report.contains("\"jacoco\":\"attached\"") + || !report.matches(".*\"roots\":\\[[^\\]]*tomcat-embed-core-[^\\]]*\\].*")) { + throw new IllegalStateException("the first record on the data socket was no INDEX report with source" + + " override, jacoco attached and Tomcat's jar among its roots in " + mode + ": " + report); + } + String json = next(records, mode); + if (!"hello".equals(body) + || !json.contains("\"id\":\"" + id + "\"") + || !json.contains("\"" + ADAPTER + "\":[") + || !json.contains("\"smoke/TomcatHarness$Tail\":[") + || json.contains("\"classMismatch\"")) { + throw new IllegalStateException("the record of a request to Tomcat, with JaCoCo attached after" + + " keploy-sdk.jar, does not measure " + ADAPTER + " and the servlet's code after the" + + " response with no class in classMismatch in " + mode + " (body " + body + "): " + json); + } + System.out.println(mode + ": " + json); + tomcat.stop(); + tomcat.destroy(); + } + + private static String get(int port) throws IOException { + HttpURLConnection connection = (HttpURLConnection) URI.create("http://127.0.0.1:" + port + "/hello") + .toURL().openConnection(); + connection.setConnectTimeout(10000); + connection.setReadTimeout(15000); + try (InputStream input = connection.getInputStream()) { + return readAll(input); + } finally { + connection.disconnect(); + } + } + + private static void command(String command) throws Exception { + try (AFUNIXSocket socket = AFUNIXSocket.newInstance()) { + socket.connect(AFUNIXSocketAddress.of(CONTROL_SOCKET), 3000); + // The END waits at most KEPLOY_JAVA_END_WAIT_MS (2000) before it ACKs. + socket.setSoTimeout(15000); + OutputStream output = socket.getOutputStream(); + output.write((command + "\n").getBytes(StandardCharsets.UTF_8)); + output.flush(); + String ack = readAll(socket.getInputStream()); + if (!"ACK proto=2\n".equals(ack)) { + throw new IllegalStateException("not taken: " + command + ": " + ack); + } + } + } + + private static String next(BlockingQueue records, String mode) throws InterruptedException { + String record = records.poll(10, TimeUnit.SECONDS); + if (record == null) { + throw new IllegalStateException("timed out waiting for a record on the data socket in " + mode); + } + return record; + } + + private static String readAll(InputStream input) throws IOException { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int read; + while ((read = input.read(buffer)) != -1) { + output.write(buffer, 0, read); + } + return new String(output.toByteArray(), StandardCharsets.UTF_8); + } +} +JAVA +javac -cp "${tomcat_libs}:${agent_jar}" -d "${work_dir}/tomcat-classes" \ + "${work_dir}/tomcat-src/smoke/TomcatHarness.java" + +tomcat_log="${work_dir}/tomcat-jacoco-after.log" +tomcat_rc=0 +KEPLOY_JAVA_DEDUP_DIAGNOSTICS=1 KEPLOY_JAVA_END_WAIT_MS=2000 \ +KEPLOY_JAVA_CLASS_DIRS="${work_dir}/tomcat-classes:${tomcat_core}" \ +run_smoke "tomcat-jacoco-after" \ + timeout -k 10 180 java \ + -javaagent:"${agent_jar}" \ + -javaagent:"${jacoco_jar}=includes=smoke.*:org.apache.catalina.connector.CoyoteAdapter,output=none" \ + -cp "${work_dir}/tomcat-classes:${tomcat_libs}:${agent_jar}" \ + smoke.TomcatHarness "tomcat-jacoco-after" "${work_dir}/tomcat-base" 2>"${tomcat_log}" || tomcat_rc=$? +if [[ "${tomcat_rc}" -eq 124 || "${tomcat_rc}" -eq 137 ]]; then + cat "${tomcat_log}" >&2 + echo "tomcat-jacoco-after: the Tomcat harness was killed or did not end within 180 s (exit ${tomcat_rc}; 137 can also be the OOM killer)" >&2 + exit 1 +fi +if [[ "${tomcat_rc}" -ne 0 ]] \ + || ! grep -q "Keploy dedup counts Tomcat's requests (org.apache.catalina.connector.CoyoteAdapter)" "${tomcat_log}" \ + || ! grep -q 'diagnostics: END test-set-0/tomcat-jacoco-after took .* requests=' "${tomcat_log}" \ + || grep -q 'Can-Retransform-Classes' "${tomcat_log}"; then + cat "${tomcat_log}" >&2 + echo "tomcat-jacoco-after: JaCoCo attached after keploy-sdk.jar did not measure Tomcat's own CoyoteAdapter, or the requests were not counted (exit ${tomcat_rc})" >&2 + exit 1 +fi +echo "tomcat-jacoco-after: JaCoCo attached after keploy-sdk.jar measured Tomcat's own CoyoteAdapter, and the requests were counted" + mkdir -p "${work_dir}/jar/lib" cp "${agent_jar}" "${work_dir}/jar/lib/keploy-sdk.jar" cat > "${work_dir}/jar/MANIFEST.MF" < Date: Fri, 20 Nov 2026 12:23:15 +0530 Subject: [PATCH 10/14] ci: stop a stuck Java agent job after 10 minutes The verify job had no timeout, so a hung build, unit test or smoke case held its runner for GitHub's default of 6 hours, up to three times over across the JDK 8, 17 and 21 legs. Timed on 4 CPUs (the size of a GitHub-hosted runner) with an empty Maven repository per JDK, the cache-miss worst case, the job's three steps as written take 116 to 121 seconds per JDK: build 25-29 s, unit tests 60-63 s, smoke 28-30 s. The smoke already stops its Tomcat case after 180 s. A job that reaches 10 minutes, about five times the slowest measured run, is stuck rather than slow, and now fails there. Signed-off-by: slayerjain --- .github/workflows/java-agent.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/java-agent.yml b/.github/workflows/java-agent.yml index dc3d220a..55f69340 100644 --- a/.github/workflows/java-agent.yml +++ b/.github/workflows/java-agent.yml @@ -19,6 +19,11 @@ jobs: verify: name: JDK ${{ matrix.java-version }} runs-on: ubuntu-latest + # A hung step would otherwise hold its runner for GitHub's default of + # 6 hours. The build, unit tests and smoke below take about 2 minutes on + # 4 CPUs with an empty Maven cache, and the smoke stops its own Tomcat + # case after 180 s, so a run that reaches 10 minutes is stuck, not slow. + timeout-minutes: 10 strategy: fail-fast: false matrix: From 7b6df567833f38ec56d5ee8ff8e37b067485e64b Mon Sep 17 00:00:00 2001 From: slayerjain Date: Fri, 20 Nov 2026 13:37:45 +0530 Subject: [PATCH 11/14] fix(dedup): keep an app's own JaCoCo agent apart from the SDK's keploy-sdk.jar carried org.jacoco.agent's AgentJar class under its own name and jacocoagent.jar at the jar's root, and EmbeddedJacoco read the agent jar through AgentJar. An app that ships its own org.jacoco.agent met the SDK's copy in either order: - the app's jar ahead of keploy-sdk.jar on the class path (a Jib launch): EmbeddedJacoco's AgentJar was the app's, and the SDK started the app's JaCoCo. With org.jacoco.agent 0.7.9 on JDK 21 that agent failed to start and every test's coverage was empty. - the app's jar in a loader that asks the system class loader first (Spring Boot's BOOT-INF/lib): the app's AgentJar was keploy-sdk.jar's, and read keploy-sdk.jar's jacocoagent.jar, not the app's. org.jacoco.agent is now a test dependency, not shaded in. The build unpacks only its jacocoagent.jar, byte for byte, to io/keploy/dedup/jacocoagent.jar, which EmbeddedJacoco reads from the loader that loaded keploy-sdk and names in an IllegalStateException when it is missing. keploy-sdk.jar holds none of that artifact's classes, and its about.html no longer needs a shade filter. The build unpacks it under target/jacocoagent and copies it into target/classes only when it is missing there or older, so a cleared target/classes gets it back and a build without clean leaves keploy-sdk.jar as it was. THIRD-PARTY-NOTICES.txt names the jar's new place and drops AgentJar. Rows: the carried jar is org.jacoco.agent's, byte for byte, and none sits at the root of keploy-sdk's classes; with an app's own jacocoagent.jar ahead of keploy-sdk, EmbeddedJacoco still reads the SDK's; a loader without it is the named error. The notices name the jar's place and no AgentJar. scripts/smoke-javaagent.sh checks the carried jar against org.jacoco.agent's, maps it as a part of its own, and adds org.jacoco.agent 0.7.9 to the app's own libraries: ahead of keploy-sdk.jar, the INDEX record must name JaCoCo 0.8.15; in the app's own parent-first loader, AgentJar must load from the app's jar and read the app's jacocoagent.jar. Both smoke cases fail on the jar before this change. Signed-off-by: slayerjain --- README.md | 10 ++- keploy-sdk/pom.xml | 87 +++++++++++++++---- .../java/io/keploy/dedup/EmbeddedJacoco.java | 25 ++++-- .../io/keploy/dedup/KeployDedupAgent.java | 14 +-- .../META-INF/THIRD-PARTY-NOTICES.txt | 16 ++-- .../io/keploy/dedup/EmbeddedJacocoTest.java | 62 +++++++++++++ .../keploy/dedup/ThirdPartyNoticesTest.java | 11 ++- scripts/smoke-javaagent.sh | 76 ++++++++++------ 8 files changed, 231 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index f6a48dda..4d13f61d 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,8 @@ test's coverage would be measured. Application code should not import `io.keploy java -javaagent:/path/to/keploy-sdk.jar -jar your-app.jar ``` -It carries the JaCoCo agent (`jacocoagent.jar`, JaCoCo 0.8.15, EPL-2.0; see +It carries the JaCoCo agent (`jacocoagent.jar`, JaCoCo 0.8.15, EPL-2.0, +unchanged, at `io/keploy/dedup/jacocoagent.jar`; see `META-INF/THIRD-PARTY-NOTICES.txt` in the jar) and starts it itself, before the app's `main`, with `includes=` set to the app's own classes, which it finds from how the JVM was launched (Spring Boot, Jib, shaded and plain jars, class dirs; @@ -98,6 +99,13 @@ When the SDK cannot tell the app's classes from its libraries, it says so at WARNING and starts JaCoCo on every class; set `KEPLOY_JAVA_CLASS_DIRS` to the app's class dirs and jars to fix that. +An app that ships its own `org.jacoco.agent` (any JaCoCo version) keeps it, and +the SDK keeps its own: `keploy-sdk.jar` holds none of that artifact's classes, +and its `jacocoagent.jar` is not where that artifact's `AgentJar` looks, so the +SDK starts JaCoCo 0.8.15 whether the app's jar comes before or after +`keploy-sdk.jar` on the class path, and the app's `AgentJar` reads the app's own +`jacocoagent.jar`, also in a Spring Boot fat jar. + If the command attaches a JaCoCo agent of its own (`-javaagent:jacocoagent.jar`, before or after keploy-sdk.jar, or in `JAVA_TOOL_OPTIONS`), the SDK starts none and reads coverage from that one. The SDK reads it through JaCoCo's in-process diff --git a/keploy-sdk/pom.xml b/keploy-sdk/pom.xml index 3eaa521b..85783e36 100644 --- a/keploy-sdk/pom.xml +++ b/keploy-sdk/pom.xml @@ -71,12 +71,13 @@ ${jacoco.version} - + org.jacoco org.jacoco.agent ${jacoco.version} + test com.kohlschutter.junixsocket @@ -139,6 +140,73 @@ + + + org.apache.maven.plugins + maven-dependency-plugin + 3.6.1 + + + + unpack-jacocoagent + generate-resources + + unpack + + + ${project.build.directory}/jacocoagent/markers + + + org.jacoco + org.jacoco.agent + ${jacoco.version} + jar + jacocoagent.jar + ${project.build.directory}/jacocoagent/classes/io/keploy/dedup + + + + + + + + + org.apache.maven.plugins + maven-resources-plugin + 3.3.1 + + + copy-jacocoagent + process-resources + + copy-resources + + + ${project.build.outputDirectory} + + + ${project.build.directory}/jacocoagent/classes + + + + + + org.apache.maven.plugins maven-shade-plugin @@ -199,19 +267,6 @@ META-INF/versions/*/module-info.class - - - org.jacoco:org.jacoco.agent - - about.html - - diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/EmbeddedJacoco.java b/keploy-sdk/src/main/java/io/keploy/dedup/EmbeddedJacoco.java index 72121b69..4ee9b3cb 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/EmbeddedJacoco.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/EmbeddedJacoco.java @@ -1,7 +1,5 @@ package io.keploy.dedup; -import org.jacoco.agent.AgentJar; - import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -32,13 +30,18 @@ *

    * JaCoCo is Eclipse Public License 2.0 software * (https://github.com/jacoco/jacoco), carried unchanged as the - * {@code jacocoagent.jar} of {@code org.jacoco:org.jacoco.agent}; see - * {@code META-INF/THIRD-PARTY-NOTICES.txt} in keploy-sdk.jar. + * {@code jacocoagent.jar} of {@code org.jacoco:org.jacoco.agent}, at + * {@link #AGENT_JAR} in keploy-sdk.jar; see + * {@code META-INF/THIRD-PARTY-NOTICES.txt} there. */ final class EmbeddedJacoco { // A class every JaCoCo agent jar holds. static final String RUNTIME_CLASS = "org/jacoco/agent/rt/RT.class"; + // The JaCoCo agent jar keploy-sdk.jar carries, under keploy-sdk's own + // package, so neither an app's own org.jacoco.agent nor this one is + // found in the other's place (keploy-sdk/pom.xml). + static final String AGENT_JAR = "io/keploy/dedup/jacocoagent.jar"; private static final String JAVAAGENT = "-javaagent:"; // A pattern takes in at most this many parts of a class's package, so // that an app over many packages needs only a few patterns; a package of @@ -188,6 +191,18 @@ static String options(String includes) { return includes.isEmpty() ? "output=none" : "output=none,includes=" + includes; } + /** + * agentJar opens the JaCoCo agent jar keploy-sdk.jar carries, at + * {@link #AGENT_JAR} of loader, the one that loaded keploy-sdk. + */ + static InputStream agentJar(ClassLoader loader) { + InputStream input = loader.getResourceAsStream(AGENT_JAR); + if (input == null) { + throw new IllegalStateException("keploy-sdk.jar holds no " + AGENT_JAR); + } + return input; + } + /** * start writes the JaCoCo agent jar into a new temp dir only this user * can read, puts it on the system classpath as the JVM does a @@ -202,7 +217,7 @@ static String start(Instrumentation instrumentation, String options) throws Exce dir.deleteOnExit(); File jar = new File(dir, "jacocoagent.jar"); jar.deleteOnExit(); - try (InputStream input = AgentJar.getResourceAsStream()) { + try (InputStream input = agentJar(EmbeddedJacoco.class.getClassLoader())) { Files.copy(input, jar.toPath()); } String premain; diff --git a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java index 9fd5b6aa..0e13ce83 100644 --- a/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java +++ b/keploy-sdk/src/main/java/io/keploy/dedup/KeployDedupAgent.java @@ -417,8 +417,11 @@ private static void startJacoco(Instrumentation instrumentation, CoverageIndex i + (includes.length() > 300 ? includes.substring(0, 300) + "..." : includes) + ")"), null); } catch (Throwable t) { // Never let premain fail the app's start: run it unmeasured. This - // must stay Throwable: an Error (NoClassDefFoundError for JaCoCo's - // AgentJar, a LinkageError from its premain) would abort the JVM. + // must stay Throwable: loading or initializing JaCoCo's PreMain + // can throw an Error (a LinkageError or an + // ExceptionInInitializerError), which would abort the JVM. A jar + // without the agent is agentJar's IllegalStateException, logged + // here with it. log(Level.WARNING, "Keploy dedup could not start the JaCoCo agent keploy-sdk.jar carries, so no" + " test's coverage is measured: attach jacocoagent.jar with -javaagent to measure it", t); } @@ -2355,9 +2358,10 @@ private String classKeyFromJarEntry(String entryName) { } // shouldSkipClass keeps keploy-sdk.jar's own classes out of the index: - // its code, the ASM, Gson and JaCoCo core it carries renamed under - // io/keploy/shaded, JaCoCo's agent (the AgentJar it starts, and the - // runtime of any JaCoCo agent), and junixsocket, which it carries under + // its code (with the jacocoagent.jar it starts, under io/keploy/dedup), + // the ASM, Gson and JaCoCo core it carries renamed under + // io/keploy/shaded, JaCoCo itself (the runtime of any JaCoCo agent, + // and an app's own copy), and junixsocket, which it carries under // its own names because its native library is bound to them. An app's // own ASM or Gson is one of the app's libraries, as Jackson is. The // shade plugin rewrites every string constant that starts with a diff --git a/keploy-sdk/src/main/resources/META-INF/THIRD-PARTY-NOTICES.txt b/keploy-sdk/src/main/resources/META-INF/THIRD-PARTY-NOTICES.txt index 7edd1074..f4c8e5ed 100644 --- a/keploy-sdk/src/main/resources/META-INF/THIRD-PARTY-NOTICES.txt +++ b/keploy-sdk/src/main/resources/META-INF/THIRD-PARTY-NOTICES.txt @@ -11,20 +11,16 @@ the jar; https://www.eclipse.org/legal/epl-2.0/) https://github.com/jacoco/jacoco, tag v0.8.15 (jacocoagent.jar is built from its org.jacoco.agent.rt module), and org.jacoco.core's also from Maven Central as org.jacoco:org.jacoco.core:0.8.15:sources. - - jacocoagent.jar, unchanged, from org.jacoco:org.jacoco.agent:0.8.15; keploy-sdk - starts it when the JVM attaches no JaCoCo agent of its own. It holds its - own about.html. - - the class org.jacoco.agent.AgentJar and its package-info, unchanged and - not renamed, from org.jacoco:org.jacoco.agent:0.8.15, which keploy-sdk - uses to read jacocoagent.jar. + - jacocoagent.jar, unchanged, from org.jacoco:org.jacoco.agent:0.8.15, at + io/keploy/dedup/jacocoagent.jar in the jar; keploy-sdk starts it when the + JVM attaches no JaCoCo agent of its own. It holds its own about.html, + which is also org.jacoco.agent's. - the classes of org.jacoco:org.jacoco.core:0.8.15, unchanged but for their package, renamed from org.jacoco.core to io.keploy.shaded.org.jacoco.core (maven-shade-plugin relocation, keploy-sdk/pom.xml), so that they never meet an app's own JaCoCo. - about.html at the root of the jar is org.jacoco.core's, unchanged. - org.jacoco.agent's, which covers AgentJar and jacocoagent.jar, is the same - file as the about.html inside jacocoagent.jar. Neither covers the rest of - the jar. + about.html at the root of the jar is org.jacoco.core's, unchanged. Neither + it nor the one inside jacocoagent.jar covers the rest of the jar. ASM 9.10.1 - BSD 3-Clause License Source: https://gitlab.ow2.org/asm/asm diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/EmbeddedJacocoTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/EmbeddedJacocoTest.java index 6d8e0a89..c6a29a70 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/EmbeddedJacocoTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/EmbeddedJacocoTest.java @@ -2,8 +2,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.File; import java.io.FileOutputStream; @@ -78,6 +80,66 @@ public void anEmptyIndexStartsNoJacoco() { assertNull(EmbeddedJacoco.includes(false, Collections.emptyList())); } + @Test + public void theAgentJarIsOrgJacocoAgentsUnchangedUnderKeploysOwnPackage() throws Exception { + // The build puts org.jacoco.agent's jacocoagent.jar, byte for byte, + // under io/keploy/dedup, and none at the root, where an app's own + // org.jacoco.agent (its AgentJar) looks for one. + assertEquals("io/keploy/dedup/jacocoagent.jar", EmbeddedJacoco.AGENT_JAR); + try (InputStream carried = EmbeddedJacoco.agentJar(EmbeddedJacoco.class.getClassLoader()); + InputStream jacocos = AgentJar.getResourceAsStream()) { + assertTrue(Arrays.equals(readAll(jacocos), readAll(carried))); + } + try (URLClassLoader sdkAlone = new URLClassLoader(new URL[] {sdkClasses()}, null)) { + assertNotNull(sdkAlone.getResource(EmbeddedJacoco.AGENT_JAR)); + assertNull("keploy-sdk's classes hold a jacocoagent.jar at the root, where an app's own" + + " AgentJar would read it", sdkAlone.getResource("jacocoagent.jar")); + } + } + + @Test + public void anAppsOwnJacocoAgentAheadOfTheSdkIsNotTheAgentStarted() throws Exception { + // An app's org.jacoco.agent ahead of keploy-sdk on the class path + // holds a jacocoagent.jar at its root: agentJar still reads the + // SDK's own. + File app = tmp.newFile("org.jacoco.agent-0.7.9.jar"); + try (JarOutputStream out = new JarOutputStream(new FileOutputStream(app))) { + out.putNextEntry(new ZipEntry("jacocoagent.jar")); + out.write("the app's own JaCoCo agent".getBytes(StandardCharsets.UTF_8)); + out.closeEntry(); + } + try (URLClassLoader appFirst = new URLClassLoader( + new URL[] {app.toURI().toURL(), sdkClasses()}, null); + InputStream carried = EmbeddedJacoco.agentJar(appFirst); + InputStream jacocos = AgentJar.getResourceAsStream()) { + assertTrue("agentJar read the app's jacocoagent.jar, not keploy-sdk's", + Arrays.equals(readAll(jacocos), readAll(carried))); + } + } + + private static URL sdkClasses() { + return EmbeddedJacoco.class.getProtectionDomain().getCodeSource().getLocation(); + } + + @Test + public void aLoaderWithoutTheAgentJarIsANamedError() throws Exception { + try (URLClassLoader empty = new URLClassLoader(new URL[0], null)) { + EmbeddedJacoco.agentJar(empty).close(); + fail("agentJar opened a jar an empty loader does not hold"); + } catch (IllegalStateException e) { + assertEquals("keploy-sdk.jar holds no io/keploy/dedup/jacocoagent.jar", e.getMessage()); + } + } + + private static byte[] readAll(InputStream input) throws IOException { + java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(); + byte[] buffer = new byte[8192]; + for (int n; (n = input.read(buffer)) != -1; ) { + out.write(buffer, 0, n); + } + return out.toByteArray(); + } + @Test public void optionsCarryIncludesOnlyWhenThereAreAny() { assertEquals("output=none", EmbeddedJacoco.options("")); diff --git a/keploy-sdk/src/test/java/io/keploy/dedup/ThirdPartyNoticesTest.java b/keploy-sdk/src/test/java/io/keploy/dedup/ThirdPartyNoticesTest.java index 5e6e6d4d..ae3d6fb4 100644 --- a/keploy-sdk/src/test/java/io/keploy/dedup/ThirdPartyNoticesTest.java +++ b/keploy-sdk/src/test/java/io/keploy/dedup/ThirdPartyNoticesTest.java @@ -1,6 +1,7 @@ package io.keploy.dedup; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -37,12 +38,10 @@ public void noticesNameTheVersionsTheJarCarries() throws IOException { assertContains(notices, "the classes of org.jacoco:org.jacoco.core:" + jacoco + ","); String agent = mavenVersion("org.jacoco", "org.jacoco.agent"); assertContains(notices, "- jacocoagent.jar, unchanged, from org.jacoco:org.jacoco.agent:" - + agent + ";"); - // keploy-sdk.jar carries AgentJar from the same artifact, under its own - // name, outside jacocoagent.jar. - assertContains(notices, "- the class " + org.jacoco.agent.AgentJar.class.getName() - + " and its package-info, unchanged and not renamed, from" - + " org.jacoco:org.jacoco.agent:" + agent + ","); + + agent + ", at " + EmbeddedJacoco.AGENT_JAR + " in the jar;"); + // keploy-sdk.jar carries only that jar from the artifact, not its AgentJar. + assertFalse("THIRD-PARTY-NOTICES.txt still names AgentJar, which keploy-sdk.jar no longer carries", + notices.contains("AgentJar")); String asm = org.objectweb.asm.ClassReader.class.getPackage().getImplementationVersion(); assertNotNull("ASM's jar names no Implementation-Version", asm); diff --git a/scripts/smoke-javaagent.sh b/scripts/smoke-javaagent.sh index 3e3edb8c..08d499f0 100755 --- a/scripts/smoke-javaagent.sh +++ b/scripts/smoke-javaagent.sh @@ -39,7 +39,7 @@ trap cleanup EXIT licenses_dir="${work_dir}/licenses" jacoco_maven=META-INF/maven/org.jacoco license_entries=(META-INF/LICENSE META-INF/LICENSE-EPL-2.0.txt META-INF/THIRD-PARTY-NOTICES.txt about.html - "${jacoco_maven}/org.jacoco.core/pom.properties" "${jacoco_maven}/org.jacoco.agent/pom.properties") + "${jacoco_maven}/org.jacoco.core/pom.properties" io/keploy/dedup/jacocoagent.jar) mkdir -p "${licenses_dir}" (cd "${licenses_dir}" && jar xf "${agent_jar}" "${license_entries[@]}") for entry in "${license_entries[@]}"; do @@ -58,22 +58,29 @@ for entry in META-INF/LICENSE-EPL-2.0.txt META-INF/THIRD-PARTY-NOTICES.txt; do exit 1 fi done -# about.html at the jar's root is org.jacoco.core's, as the notices say, and -# org.jacoco.agent's, which the shade filter leaves out, is the same file as -# the about.html inside jacocoagent.jar. +# about.html at the jar's root is org.jacoco.core's, as the notices say. Of +# org.jacoco.agent, built from the same JaCoCo release, the jar carries the +# jacocoagent.jar alone, unchanged, under io/keploy/dedup (not its AgentJar, +# nor its about.html, which is the same file as the about.html inside +# jacocoagent.jar). +jacoco_version="$(sed -n 's/^version=//p' "${licenses_dir}/${jacoco_maven}/org.jacoco.core/pom.properties" | tr -d '\r')" for artifact in org.jacoco.core org.jacoco.agent; do - version="$(sed -n 's/^version=//p' "${licenses_dir}/${jacoco_maven}/${artifact}/pom.properties" | tr -d '\r')" mvn -q -f "${repo_root}/pom.xml" org.apache.maven.plugins:maven-dependency-plugin:3.6.1:copy \ - -Dartifact="org.jacoco:${artifact}:${version}" -DoutputDirectory="${licenses_dir}" + -Dartifact="org.jacoco:${artifact}:${jacoco_version}" -DoutputDirectory="${licenses_dir}" mkdir -p "${licenses_dir}/${artifact}" - (cd "${licenses_dir}/${artifact}" && jar xf "../${artifact}-${version}.jar" about.html) + (cd "${licenses_dir}/${artifact}" && jar xf "../${artifact}-${jacoco_version}.jar" about.html) done if ! cmp -s "${licenses_dir}/org.jacoco.core/about.html" "${licenses_dir}/about.html"; then echo "licenses: the jar's about.html is not org.jacoco.core's" >&2 exit 1 fi +(cd "${licenses_dir}/org.jacoco.agent" && jar xf "../org.jacoco.agent-${jacoco_version}.jar" jacocoagent.jar) +if ! cmp -s "${licenses_dir}/org.jacoco.agent/jacocoagent.jar" "${licenses_dir}/io/keploy/dedup/jacocoagent.jar"; then + echo "licenses: the jar's io/keploy/dedup/jacocoagent.jar is not org.jacoco.agent ${jacoco_version}'s jacocoagent.jar" >&2 + exit 1 +fi mkdir -p "${licenses_dir}/jacocoagent" -(cd "${licenses_dir}/jacocoagent" && jar xf "${agent_jar}" jacocoagent.jar && jar xf jacocoagent.jar about.html) +(cd "${licenses_dir}/jacocoagent" && jar xf "${licenses_dir}/io/keploy/dedup/jacocoagent.jar" about.html) if ! cmp -s "${licenses_dir}/org.jacoco.agent/about.html" "${licenses_dir}/jacocoagent/about.html"; then echo "licenses: org.jacoco.agent's about.html is not the one inside the jar's jacocoagent.jar" >&2 exit 1 @@ -81,10 +88,11 @@ fi # The jar holds keploy-sdk's own packages and the parts the notices name, and # nothing else: a dependency or a package that lands in the jar without its # notice fails here. A third-party part goes in THIRD-PARTY-NOTICES.txt first, -# then in want_parts; a new package of keploy-sdk's own, in want_parts. +# then in want_parts; a new package of keploy-sdk's own, in want_parts. A file +# in keploy-sdk's own packages that is not a class is a part by its own name. jar_parts="$(jar tf "${agent_jar}" | grep -v '/$' | awk -F/ ' $1 == "io" && $2 == "keploy" && $3 == "shaded" { print $1 "/" $2 "/" $3 "/" $4 "/" $5 "/" $6; next } - $1 == "org" && $2 == "jacoco" && $3 == "agent" { print; next } + $1 == "io" && $2 == "keploy" && $3 != "shaded" && $NF !~ /\.class$/ { print; next } $1 == "META-INF" && $2 == "versions" && NF > 6 { print $1 "/" $2 "/*/" $4 "/" $5 "/" $6; next } $1 == "META-INF" && ($2 == "maven" || $2 == "native-image") && NF > 3 { print $1 "/" $2 "/" $3; next } $1 == "lib" && NF == 4 && $3 == "jni" && $4 ~ /^(lib)?junixsocket-native-/ { print "lib/*/jni/junixsocket-native"; next } @@ -104,14 +112,12 @@ META-INF/versions/*/org/newsclub/lib META-INF/versions/*/org/newsclub/net about.html io/keploy/dedup +io/keploy/dedup/jacocoagent.jar io/keploy/servlet io/keploy/shaded/com/google/gson io/keploy/shaded/org/jacoco/core io/keploy/shaded/org/objectweb/asm -jacocoagent.jar lib/*/jni/junixsocket-native -org/jacoco/agent/AgentJar.class -org/jacoco/agent/package-info.class org/newsclub/lib org/newsclub/net PARTS @@ -246,7 +252,7 @@ public final class SmokeHarness { String wantJacocoVersion = "attached".equals(wantJacoco) ? versionOf(new File(System.getProperty("smoke.jacocoJar")), null) : "embedded".equals(wantJacoco) - ? versionOf(new File(System.getProperty("smoke.sdkJar")), "jacocoagent.jar") : ""; + ? versionOf(new File(System.getProperty("smoke.sdkJar")), "io/keploy/dedup/jacocoagent.jar") : ""; if (wantSdkVersion.isEmpty() || wantJacocoVersion.isEmpty() != !wantJacoco.matches("attached|embedded") || !report.endsWith(",\"sdkVersion\":\"" + wantSdkVersion + "\",\"jacocoVersion\":\"" + wantJacocoVersion + "\"}")) { @@ -304,11 +310,13 @@ public final class SmokeHarness { return record; } - // loadsItsOwn loads ASM, Gson and JaCoCo's core from the app's own jars - // as Spring Boot's launcher loads BOOT-INF/lib: through a loader that asks - // its parent, the system class loader, first. keploy-sdk.jar is on the - // system class path, so a copy of these it carried under their own names - // would be what the app gets, in keploy-sdk's version, not the app's. + // loadsItsOwn loads ASM, Gson, JaCoCo's core and JaCoCo's AgentJar from + // the app's own jars as Spring Boot's launcher loads BOOT-INF/lib: through + // a loader that asks its parent, the system class loader, first. + // keploy-sdk.jar is on the system class path, so a copy of these it + // carried under their own names would be what the app gets, in + // keploy-sdk's version, not the app's; and so would a jacocoagent.jar it + // carried at its root, which is where the app's AgentJar looks for its own. private static void loadsItsOwn(String jars) throws Exception { String[] paths = jars.split(File.pathSeparator); URL[] urls = new URL[paths.length]; @@ -318,7 +326,8 @@ public final class SmokeHarness { URLClassLoader libraries = new URLClassLoader(urls, ClassLoader.getSystemClassLoader()); try { for (String name : new String[] { - "org.objectweb.asm.ClassReader", "com.google.gson.Gson", "org.jacoco.core.JaCoCo"}) { + "org.objectweb.asm.ClassReader", "com.google.gson.Gson", "org.jacoco.core.JaCoCo", + "org.jacoco.agent.AgentJar"}) { Class type = Class.forName(name, false, libraries); URL from = type.getProtectionDomain().getCodeSource() == null ? null : type.getProtectionDomain().getCodeSource().getLocation(); @@ -331,6 +340,16 @@ public final class SmokeHarness { + ", not from the app's own jars " + jars); } } + URL agent = (URL) Class.forName("org.jacoco.agent.AgentJar", false, libraries) + .getMethod("getResource").invoke(null); + boolean ownAgent = false; + for (URL url : urls) { + ownAgent |= agent != null && agent.toString().equals("jar:" + url + "!/jacocoagent.jar"); + } + if (!ownAgent) { + throw new IllegalStateException("the app's AgentJar read the JaCoCo agent jar " + agent + + ", not the one in the app's own jars " + jars); + } } finally { libraries.close(); } @@ -819,13 +838,16 @@ java_with_sdk_only() { -jar plain-app.jar "sdk-only-plain-jar" derived embedded ) -# The app's own ASM, Gson and JaCoCo core, in versions other than the ones -# keploy-sdk.jar carries. keploy-sdk.jar carries its copies renamed under -# io/keploy/shaded, so the app gets its own and keploy-sdk its own, whichever -# comes first: the app's on the class path before keploy-sdk.jar (Jib's -# launch), or the app's in a loader under the system class loader (Spring -# Boot's BOOT-INF/lib). -for artifact in org.ow2.asm:asm:5.2 com.google.code.gson:gson:2.11.0 org.jacoco:org.jacoco.core:0.7.9; do +# The app's own ASM, Gson, JaCoCo core and JaCoCo agent, in versions other +# than the ones keploy-sdk.jar carries. keploy-sdk.jar carries its copies +# renamed under io/keploy/shaded, and its jacocoagent.jar under +# io/keploy/dedup without JaCoCo's AgentJar, so the app gets its own and +# keploy-sdk its own, whichever comes first: the app's on the class path before +# keploy-sdk.jar (Jib's launch; the INDEX report names the JaCoCo keploy-sdk +# started, which must be its own), or the app's in a loader under the system +# class loader (Spring Boot's BOOT-INF/lib). +for artifact in org.ow2.asm:asm:5.2 com.google.code.gson:gson:2.11.0 org.jacoco:org.jacoco.core:0.7.9 \ + org.jacoco:org.jacoco.agent:0.7.9; do mvn -q -f "${repo_root}/pom.xml" org.apache.maven.plugins:maven-dependency-plugin:3.6.1:copy \ -Dartifact="${artifact}" -DoutputDirectory="${work_dir}/app-libs" done From 2edb3cf586452a4eb0fd1ec13340605f09cae20d Mon Sep 17 00:00:00 2001 From: slayerjain Date: Fri, 20 Nov 2026 14:49:57 +0530 Subject: [PATCH 12/14] build: check keploy-sdk and keploy-junit5 against Java 8's API keploy-sdk and keploy-junit5 run on Java 8, and both are released from JDK 17 with source and target 1.8. Those set the language level and the class file version, not the API the code is checked against: javac on JDK 17 compiles against JDK 17's own classes. A call to a method Java 8 lacks (java.util.List.of()) then fails on Java 8 only, in the released jar, with a NoSuchMethodError. CI's JDK 8 legs build on JDK 8 and catch that one, but not a Java 8 method that JDK 9 overrode with a narrower return type (ByteBuffer.position(int), flip()): that call compiles on every JDK, and on JDK 17 it refers to ByteBuffer's own position(int), which returns ByteBuffer and which Java 8 does not have. No CI leg runs a jar built on JDK 17 on Java 8. Each pom (the root's, for keploy-sdk, and keploy-junit5's, a project of its own) gets a profile, active on JDK 9 and later, that sets maven.compiler.release to 8: javac then checks main and test code against Java 8's API, and a call resolves as it would on JDK 8. The release profile does not turn it off (only activeByDefault profiles yield to -P). JDK 8's javac has no --release, and needs none: there the build is unchanged. Today's code has no such call: on JDK 17 both projects' main classes come out the same, instruction for instruction, with the profile and without it. Java8ApiTest, in keploy-sdk's rows and keploy-junit5's, checks that a call to ByteBuffer.position(int) refers to Buffer's method, as on Java 8. On JDK 17 and 21 it fails with the profile off, and on JDK 17 with release 9. Checked on JDK 8, 17 and 21, for both projects: the tree compiles to class file version 52; a planted java.util.List.of() fails to compile, with and without -P release; a planted ByteBuffer.position(0) compiles to ByteBuffer.position:(I)Ljava/nio/Buffer;, Buffer's method. On JDK 17 and 21 with the profile off, both compile, and the second to ByteBuffer.position:(I)Ljava/nio/ByteBuffer;, which Java 8 lacks. On JDK 17 the release builds of both projects (javadoc jars included, signing skipped) pass, and Java 8 runs scripts/smoke-javaagent.sh on the keploy-sdk jar built there. keploy-junit5's rows pass on JDK 8 with JUnit 5.10.5, JDK 17 with 5.14.4 and JDK 21 with JUnit 6.1.3. Signed-off-by: slayerjain --- README.md | 7 +++ keploy-junit5/pom.xml | 16 ++++++ .../java/io/keploy/junit5/Java8ApiTest.java | 57 +++++++++++++++++++ .../src/test/java/io/keploy/Java8ApiTest.java | 57 +++++++++++++++++++ pom.xml | 15 +++++ 5 files changed, 152 insertions(+) create mode 100644 keploy-junit5/src/test/java/io/keploy/junit5/Java8ApiTest.java create mode 100644 keploy-sdk/src/test/java/io/keploy/Java8ApiTest.java diff --git a/README.md b/README.md index 4d13f61d..b53a1a5c 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,13 @@ supported ceiling is the lower of two limits — not one: Both currently sit at **Java 26**, so that is the effective ceiling. +The floor is **Java 8**. keploy-sdk's and keploy-junit5's own code is compiled +for Java 8 (class file version 52), and, when built on JDK 9 or later (the +releases are built on JDK 17), against Java 8's API too (keploy-sdk since +2.2.0, keploy-junit5 from its first release): a call in that code to a method +Java 8 lacks fails the build, instead of failing on Java 8 at run time. The +check covers this repo's code, not the libraries shaded into `keploy-sdk.jar`. + ### Why this repo is part of the ceiling It is easy to assume the JaCoCo agent alone decides this. It does not. The dedup diff --git a/keploy-junit5/pom.xml b/keploy-junit5/pom.xml index 23db0dc4..185852da 100644 --- a/keploy-junit5/pom.xml +++ b/keploy-junit5/pom.xml @@ -181,5 +181,21 @@ + + + java8-api + + [9,) + + + 8 + + diff --git a/keploy-junit5/src/test/java/io/keploy/junit5/Java8ApiTest.java b/keploy-junit5/src/test/java/io/keploy/junit5/Java8ApiTest.java new file mode 100644 index 00000000..615f0a78 --- /dev/null +++ b/keploy-junit5/src/test/java/io/keploy/junit5/Java8ApiTest.java @@ -0,0 +1,57 @@ +package io.keploy.junit5; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; + +import org.junit.jupiter.api.Test; + +/** + * keploy-junit5 runs on Java 8, and is released from JDK 17: its pom's + * java8-api profile makes javac on JDK 9 and later check the code against + * Java 8's API (release 8), as it does for this test class. A call to + * ByteBuffer.position(int) then refers to the method Java 8 has, Buffer's, + * as when JDK 8 compiles it. With source and target 1.8 alone, JDK 9 and + * later compile it to ByteBuffer's own position(int), which JDK 9 added and + * Java 8 lacks (NoSuchMethodError on Java 8). It reads this test class as a + * stand-in for the main classes, which take release 8 from the same + * property: if tests ever get their own maven.compiler.testRelease, point + * this check at a main class instead. + */ +class Java8ApiTest { + /** Holds only the call, so nothing else puts a position descriptor in its class file. */ + static final class Call { + static void position(ByteBuffer buffer) { + buffer.position(0); + } + } + + @Test + void aCallResolvesAgainstJava8sApi() throws IOException { + String classFile = new String(read(Call.class), StandardCharsets.ISO_8859_1); + assertTrue(classFile.contains("(I)Ljava/nio/Buffer;"), + "the call to ByteBuffer.position(int) does not refer to Buffer's: (I)Ljava/nio/Buffer; is not in its class file"); + assertFalse(classFile.contains("(I)Ljava/nio/ByteBuffer;"), + "the call to ByteBuffer.position(int) refers to the one JDK 9 added, which Java 8 lacks: " + + "javac did not compile against Java 8's API (keploy-junit5's java8-api profile)"); + } + + static byte[] read(Class type) throws IOException { + String name = type.getName(); + try (InputStream in = type.getResourceAsStream(name.substring(name.lastIndexOf('.') + 1) + ".class")) { + assertNotNull(in, "no class file for " + name); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] chunk = new byte[4096]; + for (int n; (n = in.read(chunk)) != -1; ) { + out.write(chunk, 0, n); + } + return out.toByteArray(); + } + } +} diff --git a/keploy-sdk/src/test/java/io/keploy/Java8ApiTest.java b/keploy-sdk/src/test/java/io/keploy/Java8ApiTest.java new file mode 100644 index 00000000..ff26ccce --- /dev/null +++ b/keploy-sdk/src/test/java/io/keploy/Java8ApiTest.java @@ -0,0 +1,57 @@ +package io.keploy; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; + +import org.junit.Test; + +/** + * keploy-sdk runs on Java 8, and is released from JDK 17: the root pom's + * java8-api profile makes javac on JDK 9 and later check the code against + * Java 8's API (release 8), as it does for this test class. A call to + * ByteBuffer.position(int) then refers to the method Java 8 has, Buffer's, + * as when JDK 8 compiles it. With source and target 1.8 alone, JDK 9 and + * later compile it to ByteBuffer's own position(int), which JDK 9 added and + * Java 8 lacks (NoSuchMethodError on Java 8). It reads this test class as a + * stand-in for the main classes, which take release 8 from the same + * property: if tests ever get their own maven.compiler.testRelease, point + * this check at a main class instead. + */ +public class Java8ApiTest { + /** Holds only the call, so nothing else puts a position descriptor in its class file. */ + static final class Call { + static void position(ByteBuffer buffer) { + buffer.position(0); + } + } + + @Test + public void aCallResolvesAgainstJava8sApi() throws IOException { + String classFile = new String(read(Call.class), StandardCharsets.ISO_8859_1); + assertTrue("the call to ByteBuffer.position(int) does not refer to Buffer's: (I)Ljava/nio/Buffer; is not in its class file", + classFile.contains("(I)Ljava/nio/Buffer;")); + assertFalse("the call to ByteBuffer.position(int) refers to the one JDK 9 added, which Java 8 lacks: " + + "javac did not compile against Java 8's API (the root pom's java8-api profile)", + classFile.contains("(I)Ljava/nio/ByteBuffer;")); + } + + static byte[] read(Class type) throws IOException { + String name = type.getName(); + try (InputStream in = type.getResourceAsStream(name.substring(name.lastIndexOf('.') + 1) + ".class")) { + assertNotNull("no class file for " + name, in); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] chunk = new byte[4096]; + for (int n; (n = in.read(chunk)) != -1; ) { + out.write(chunk, 0, n); + } + return out.toByteArray(); + } + } +} diff --git a/pom.xml b/pom.xml index 5f95a3fb..990d5e99 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,21 @@ + + + java8-api + + [9,) + + + 8 + + release From e45279e513aaae0e01ebd3a51ea3d8e5bcf19701 Mon Sep 17 00:00:00 2001 From: slayerjain Date: Fri, 20 Nov 2026 16:17:02 +0530 Subject: [PATCH 13/14] build: pin the compiler plugin so Maven 3.8 keeps the Java 8 API check The java8-api profile sets maven.compiler.release to 8, which only maven-compiler-plugin 3.6 and later read. Neither the root pom nor keploy-sdk's named a version of that plugin, so keploy-sdk was compiled with the one the running Maven binds by default: 3.15.0 on the Maven of CI's runners today, 3.13.0 on Maven 3.9.9, and 3.1 on Maven 3.8 and older. 3.1 has no release parameter and ignores the property without a warning. A keploy-sdk jar built there on JDK 9 or later with -DskipTests could again carry a call Java 8 lacks, and nothing failed. With the tests on, Java8ApiTest fails, so CI would catch it; a package build would not. The root pom now pins 3.15.0 in its pluginManagement, which keploy-sdk inherits. That is the version CI's runners, which also build the release, bind today, so CI's and the release's compile do not change. keploy-junit5's pom keeps its own pin, 3.13.0. Other builds of keploy-sdk move to 3.15.0: from 3.1 on Maven 3.8, which is the fix checked below, and from 3.13.0 on Maven 3.9.9, where its main and test classes come out byte for byte the same from both on JDK 8, 17 and 21. A second row in each Java8ApiTest reads the pom (keploy-sdk's, then the root's; keploy-junit5's own) and fails when it names no compiler plugin version, or one before 3.6 such as 3.1 or 3.5.1. It also fails when a pom it reads is not the one it expects: run outside Maven from another directory, it would read another project's pom and could pass on that one's pin. On Maven 3.9 nothing else fails without the pin: the default binding hides it. Checked with Maven 3.8.8 on JDK 17 and 21. Without the pin, keploy-sdk compiles with compiler 3.1: a java.util.List.of() planted in its main code builds with -DskipTests, a planted ByteBuffer.position(0) refers to ByteBuffer's own position(int), which Java 8 lacks, and both Java8ApiTest rows fail. With the pin, compiler 3.15.0: the first fails to compile, the second refers to Buffer's method, and the rows pass. keploy-junit5 builds with 3.13.0 on Maven 3.8.8 as before. java-agent.yml's matrix (JDK 8, 17 and 21, KEPLOY_PROVEN unset and =1, the smoke), junit5-sdk.yml's three legs and release.yml's verify on JDK 17 pass. Signed-off-by: slayerjain --- .../java/io/keploy/junit5/Java8ApiTest.java | 60 +++++++++++++++++ .../src/test/java/io/keploy/Java8ApiTest.java | 65 +++++++++++++++++++ pom.xml | 21 ++++++ 3 files changed, 146 insertions(+) diff --git a/keploy-junit5/src/test/java/io/keploy/junit5/Java8ApiTest.java b/keploy-junit5/src/test/java/io/keploy/junit5/Java8ApiTest.java index 615f0a78..00c8f7d8 100644 --- a/keploy-junit5/src/test/java/io/keploy/junit5/Java8ApiTest.java +++ b/keploy-junit5/src/test/java/io/keploy/junit5/Java8ApiTest.java @@ -1,16 +1,22 @@ package io.keploy.junit5; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPathFactory; + import org.junit.jupiter.api.Test; +import org.w3c.dom.Document; /** * keploy-junit5 runs on Java 8, and is released from JDK 17: its pom's @@ -42,6 +48,60 @@ void aCallResolvesAgainstJava8sApi() throws IOException { + "javac did not compile against Java 8's API (keploy-junit5's java8-api profile)"); } + /** + * The java8-api profile works through maven-compiler-plugin's release + * parameter, which the plugin has since 3.6. A build that names no version + * gets the running Maven's default, 3.1 on Maven 3.8 and older, which + * ignores maven.compiler.release without a warning: a jar built there with + * -DskipTests, where the row above does not run, would carry calls Java 8 + * lacks. keploy-junit5's pom names the version itself. + */ + @Test + void theCompilerPluginVersionHasRelease() throws Exception { + File module = new File(System.getProperty("basedir", System.getProperty("user.dir"))).getAbsoluteFile(); + String version = compilerPluginVersion(pom(new File(module, "pom.xml"), "keploy-junit5")); + assertFalse(version.isEmpty(), "keploy-junit5's pom names no maven-compiler-plugin version: " + + "Maven 3.8 and older then use 3.1, which ignores release 8"); + assertTrue(hasRelease(version), "maven-compiler-plugin " + version + " has no release parameter " + + "(3.6 and later have it): it ignores the java8-api profile's release 8"); + } + + /** + * The pom at file, which must be artifactId's: run from another directory + * without Maven's basedir (an IDE's working directory, say), the row would + * read another module's pom and could pass on its pin. + */ + static Document pom(File file, String artifactId) throws Exception { + String where = ": run this row from keploy-junit5's directory, or through Maven, which sets basedir to it"; + assertTrue(file.isFile(), "no pom at " + file + where); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + Document doc = factory.newDocumentBuilder().parse(file); + assertEquals(artifactId, XPathFactory.newInstance().newXPath().evaluate("/project/artifactId", doc).trim(), + file + " is not " + artifactId + "'s pom" + where); + return doc; + } + + /** The version a pom gives maven-compiler-plugin in its build, or "" when it names none. */ + static String compilerPluginVersion(Document doc) throws Exception { + String plugin = "plugins/plugin[artifactId='maven-compiler-plugin']/version"; + String version = XPathFactory.newInstance().newXPath().evaluate("/project/build/" + plugin, doc).trim(); + if (version.isEmpty()) { + version = XPathFactory.newInstance().newXPath().evaluate("/project/build/pluginManagement/" + plugin, doc).trim(); + } + return version; + } + + /** True for 3.6 and later: a plain version such as 3.13.0. Anything else, a property included, is false. */ + static boolean hasRelease(String version) { + String[] parts = version.split("\\."); + if (parts.length < 2 || !parts[0].matches("[0-9]+") || !parts[1].matches("[0-9]+")) { + return false; + } + int major = Integer.parseInt(parts[0]); + return major > 3 || (major == 3 && Integer.parseInt(parts[1]) >= 6); + } + static byte[] read(Class type) throws IOException { String name = type.getName(); try (InputStream in = type.getResourceAsStream(name.substring(name.lastIndexOf('.') + 1) + ".class")) { diff --git a/keploy-sdk/src/test/java/io/keploy/Java8ApiTest.java b/keploy-sdk/src/test/java/io/keploy/Java8ApiTest.java index ff26ccce..23f8bda2 100644 --- a/keploy-sdk/src/test/java/io/keploy/Java8ApiTest.java +++ b/keploy-sdk/src/test/java/io/keploy/Java8ApiTest.java @@ -1,16 +1,22 @@ package io.keploy; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPathFactory; + import org.junit.Test; +import org.w3c.dom.Document; /** * keploy-sdk runs on Java 8, and is released from JDK 17: the root pom's @@ -42,6 +48,65 @@ public void aCallResolvesAgainstJava8sApi() throws IOException { classFile.contains("(I)Ljava/nio/ByteBuffer;")); } + /** + * The java8-api profile works through maven-compiler-plugin's release + * parameter, which the plugin has since 3.6. A build that names no version + * gets the running Maven's default, 3.1 on Maven 3.8 and older, which + * ignores maven.compiler.release without a warning: a jar built there with + * -DskipTests, where the row above does not run, would carry calls Java 8 + * lacks. keploy-sdk names no version itself and takes the root pom's. + */ + @Test + public void theCompilerPluginVersionHasRelease() throws Exception { + File module = new File(System.getProperty("basedir", System.getProperty("user.dir"))).getAbsoluteFile(); + Document modulePom = pom(new File(module, "pom.xml"), "keploy-sdk"); + Document rootPom = pom(new File(module.getParentFile(), "pom.xml"), "java-sdk"); + String version = compilerPluginVersion(modulePom); + if (version.isEmpty()) { + version = compilerPluginVersion(rootPom); + } + assertFalse("neither keploy-sdk's pom nor the root pom names a maven-compiler-plugin version: " + + "Maven 3.8 and older then use 3.1, which ignores release 8", version.isEmpty()); + assertTrue("maven-compiler-plugin " + version + " has no release parameter (3.6 and later have it): " + + "it ignores the java8-api profile's release 8", hasRelease(version)); + } + + /** + * The pom at file, which must be artifactId's: run from another directory + * without Maven's basedir (an IDE's working directory, say), the row would + * read another module's pom and could pass on its pin. + */ + static Document pom(File file, String artifactId) throws Exception { + String where = ": run this row from keploy-sdk's directory, or through Maven, which sets basedir to it"; + assertTrue("no pom at " + file + where, file.isFile()); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + Document doc = factory.newDocumentBuilder().parse(file); + assertEquals(file + " is not " + artifactId + "'s pom" + where, artifactId, + XPathFactory.newInstance().newXPath().evaluate("/project/artifactId", doc).trim()); + return doc; + } + + /** The version a pom gives maven-compiler-plugin in its build, or "" when it names none. */ + static String compilerPluginVersion(Document doc) throws Exception { + String plugin = "plugins/plugin[artifactId='maven-compiler-plugin']/version"; + String version = XPathFactory.newInstance().newXPath().evaluate("/project/build/" + plugin, doc).trim(); + if (version.isEmpty()) { + version = XPathFactory.newInstance().newXPath().evaluate("/project/build/pluginManagement/" + plugin, doc).trim(); + } + return version; + } + + /** True for 3.6 and later: a plain version such as 3.13.0. Anything else, a property included, is false. */ + static boolean hasRelease(String version) { + String[] parts = version.split("\\."); + if (parts.length < 2 || !parts[0].matches("[0-9]+") || !parts[1].matches("[0-9]+")) { + return false; + } + int major = Integer.parseInt(parts[0]); + return major > 3 || (major == 3 && Integer.parseInt(parts[1]) >= 6); + } + static byte[] read(Class type) throws IOException { String name = type.getName(); try (InputStream in = type.getResourceAsStream(name.substring(name.lastIndexOf('.') + 1) + ".class")) { diff --git a/pom.xml b/pom.xml index 990d5e99..ea7eb50d 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,27 @@ 1.8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.15.0 + + + + +