Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f43dbea
feat(dedup): v3 honest TCP protocol, async executor tails, and listen…
slayerjain Nov 26, 2026
3b9e07d
fix(dedup): address r1 review — app-only rewrites, dynamic HELLO, opt…
slayerjain Nov 26, 2026
65da5b7
fix(dedup): address r2 review — non-latching JaCoCo lookup, live HELL…
slayerjain Nov 26, 2026
a8c6177
fix(dedup): address r3 review — bounded O(1) self-removing tracking, …
slayerjain Nov 26, 2026
b7a53fa
fix(dedup): address r4 review — weak HandRef tokens with ReferenceQue…
slayerjain Nov 26, 2026
0271fed
fix(dedup): address plg-87 audit — v3 identity HELLO, fail-closed poo…
slayerjain Nov 26, 2026
2e43c97
fix(dedup): close untracked async tail, B2 async forms, unarmed in-fl…
slayerjain Nov 27, 2026
95b625e
fix(dedup): guard MinimalStage isDone, track MinimalFuture & GC permi…
slayerjain Nov 27, 2026
db2174c
fix(dedup): track executing work past lingering cap, live Thread/Sche…
slayerjain Nov 27, 2026
a6cc514
fix(dedup): close R5 review gaps — late-start current-window contamin…
slayerjain Nov 27, 2026
e24bf0a
test(dedup): consume one-time untracked tail report before asserting …
slayerjain Nov 27, 2026
5650f4f
test(dedup): fix supplier countdown race before RequestGate.exit and …
slayerjain Nov 27, 2026
b582be1
fix(dedup): address R1 review — unverified no-server-entry, Executor …
slayerjain Nov 27, 2026
cf6e0c9
fix(dedup): close R9 review findings — TimerTask running-body & cance…
slayerjain Nov 27, 2026
53304de
fix(dedup): suppress BACKGROUND after no-server-entry, add Timer.canc…
slayerjain Nov 27, 2026
6576c18
test(dedup): add TCP no-server-entry BACKGROUND suppression test and …
slayerjain Nov 27, 2026
88567de
fix(dedup): runtime-check executor method refs, latch windowCut=false…
slayerjain Nov 27, 2026
56f289a
fix(dedup): latch asyncExecutors=false on pre-captured executor metho…
slayerjain Nov 27, 2026
e8eba30
test(smoke): accept protocol v3 ACK caps and trailing unverified fiel…
slayerjain Nov 27, 2026
90cbdfb
fix(dedup): latch untracked method refs and library async dispatchers…
slayerjain Nov 27, 2026
b789b31
fix(dedup): stop Tomcat NIO2 false latch, detect 5 async hand-offs, a…
slayerjain Nov 28, 2026
4f3a70c
fix(dedup): fail closed on unallowlisted dispatch classes and externa…
slayerjain Nov 28, 2026
d00e09b
fix(dedup): fail closed on off-window executor hand-offs and JDK pool…
slayerjain Nov 28, 2026
4c609a7
feat(dedup): enforce allowlist at JDK concurrency sinks via bootstrap…
slayerjain Nov 28, 2026
5a3c5da
fix(dedup): hook Servlet AsyncContext.start by interface, tighten sin…
slayerjain Nov 28, 2026
1a62bef
test(dedup): add AsyncContext fail-closed loader, Jakarta Tomcat 10, …
slayerjain Nov 28, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/java-agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
strategy:
fail-fast: false
matrix:
java-version: ["8", "17", "21"]
java-version: ["8", "11", "17", "21"]

steps:
- name: Checkout
Expand Down
18 changes: 12 additions & 6 deletions README.md

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions keploy-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@
</artifactItems>
</configuration>
</execution>
<execution>
<id>copy-tomcat10</id>
<phase>generate-test-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<skip>${skipTests}</skip>
<outputDirectory>${project.build.directory}/tomcat10</outputDirectory>
<artifactItems>
<artifactItem><groupId>org.apache.tomcat</groupId><artifactId>tomcat-annotations-api</artifactId><version>10.1.34</version></artifactItem>
<artifactItem><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-core</artifactId><version>10.1.34</version></artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
5 changes: 4 additions & 1 deletion keploy-sdk/src/main/java/io/keploy/dedup/BuildTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ static <T> List<ClassFile> files(List<T> entries, Function<T, String> name, Func

/**
* answer is the control socket's reply to Keploy's TABLE command. When the heap has less free than the
* table needs ({@link #lowHeap}) the analysis is skipped and the reply is {@code IGNORED proto=2
* table needs ({@link #lowHeap}) the analysis is skipped and the reply is {@code IGNORED proto=3
* reason=low_heap heap_free_mb=F heap_needed_mb=N}, with a WARNING in the app's log; else it is
* {@link #request}'s ACK or IGNORED. The heap is read once, here, when Keploy asks: a send that runs after
* another one was checked when it was asked. The bound takes loaded, the class count of the index when it
Expand Down Expand Up @@ -456,6 +456,7 @@ boolean request(Runnable send) {
* the WARNING then says that ask is not sent, as it was ACKed and logged as sent later.
*/
private boolean start(Runnable send, boolean handover) {
JdkSinkBootstrap.enterInternal();
try {
threads.newThread(() -> loop(send)).start();
return true;
Expand All @@ -468,6 +469,8 @@ private boolean start(Runnable send, boolean handover) {
? ": the ask for the build table that waited for the send before it is not sent (Keploy"
+ " asks again on its next run)" : ""), t);
return false;
} finally {
JdkSinkBootstrap.exitInternal();
}
}

Expand Down
279 changes: 279 additions & 0 deletions keploy-sdk/src/main/java/io/keploy/dedup/JdkSinkBootstrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
package io.keploy.dedup;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Timer;

/**
* Installs {@code io.keploy.dedup.bootstrap.KeploySinkBridge} onto the
* bootstrap class loader (before it is ever loaded by any other class loader),
* opens {@code java.base} read-access to the bootstrap unnamed module on JDK 9+
* via {@code Instrumentation.redefineModule}, registers the runtime sink
* handler, and retransforms the 5+1 JDK concurrency sink classes.
*
* <p>NOTE: This class intentionally avoids static imports or direct bytecode
* references to {@code KeploySinkBridge} inside {@link #install(Instrumentation)}
* until AFTER {@code appendToBootstrapClassLoaderSearch} completes, guaranteeing
* parent-first resolution resolves the single bootstrap-loaded class everywhere.
*/
final class JdkSinkBootstrap {

private static final Logger LOGGER = Logger.getLogger(KeployDedupAgent.class.getName());
private static final String BRIDGE_CLASS_NAME = "io.keploy.dedup.bootstrap.KeploySinkBridge";
private static final String BRIDGE_CLASS_RESOURCE = "io/keploy/dedup/bootstrap/KeploySinkBridge.class";
private static final String BRIDGE_HANDLER_RESOURCE = "io/keploy/dedup/bootstrap/KeploySinkBridge$Handler.class";

private static final AtomicBoolean INSTALLED = new AtomicBoolean(false);
private static volatile boolean active;
private static volatile boolean failed;

private JdkSinkBootstrap() {
}

static boolean isActive() {
return active;
}

static boolean hasFailed() {
return failed || (active && BridgeAccess.hasCallbackError());
}

static void setActiveForTest(boolean value) {
active = value;
if (value) {
failed = false;
}
}

static boolean install(Instrumentation instrumentation) {
if (instrumentation == null || !instrumentation.isRetransformClassesSupported()) {
failed = true;
return false;
}
if (!INSTALLED.compareAndSet(false, true)) {
return active;
}
try {
File jarFile = buildBootstrapBridgeJar();
instrumentation.appendToBootstrapClassLoaderSearch(new JarFile(jarFile));

// Load KeploySinkBridge AND KeploySinkBridge$Handler explicitly via
// the bootstrap class loader (null) so both java.base and
// io.keploy.dedup.* share the exact same bootstrap classes with zero
// loader constraint conflicts.
Class<?> bridgeClass = Class.forName(BRIDGE_CLASS_NAME, true, null);
Class<?> handlerClass = Class.forName(BRIDGE_CLASS_NAME + "$Handler", true, null);
if (bridgeClass.getClassLoader() != null || handlerClass.getClassLoader() != null) {
LOGGER.log(Level.WARNING,
"Keploy dedup: KeploySinkBridge was loaded by non-bootstrap classloader "
+ bridgeClass.getClassLoader() + "/" + handlerClass.getClassLoader()
+ "; JDK sink hooks disabled (fail-closed)");
failed = true;
return false;
}

// On JDK 9+, java.base is a named module and does not read the bootstrap
// class loader's unnamed module unless granted via Instrumentation.redefineModule.
grantJavaBaseReadAccess(instrumentation, bridgeClass);

// Register RequestGate's handler BEFORE retransforming JDK classes,
// with internal guard active during setup.
BridgeAccess.installHandler();

JdkSinkTransformer transformer = new JdkSinkTransformer();
instrumentation.addTransformer(transformer, true);

List<Class<?>> targets = new ArrayList<>();
targets.add(Thread.class);
targets.add(ThreadPoolExecutor.class);
targets.add(ScheduledThreadPoolExecutor.class);
targets.add(ForkJoinPool.class);
targets.add(Timer.class);
try {
Class<?> vt = Class.forName("java.lang.VirtualThread", false, null);
if (instrumentation.isModifiableClass(vt)) {
targets.add(vt);
}
} catch (ClassNotFoundException ignored) {
// JDK < 21: VirtualThread does not exist.
}

List<Class<?>> modifiable = new ArrayList<>();
for (Class<?> c : targets) {
if (instrumentation.isModifiableClass(c)) {
modifiable.add(c);
} else {
LOGGER.log(Level.WARNING, "Keploy dedup: JDK sink class is not modifiable: " + c.getName());
failed = true;
return false;
}
}

BridgeAccess.enterInternal();
try {
instrumentation.retransformClasses(modifiable.toArray(new Class<?>[0]));
} finally {
BridgeAccess.exitInternal();
}

boolean allCoreHooked = JdkSinkTransformer.hookedThreadStart
&& JdkSinkTransformer.hookedTpeExecute
&& JdkSinkTransformer.hookedStpeDelayedExecute
&& JdkSinkTransformer.hookedFjpSubmit
&& JdkSinkTransformer.hookedTimerSched;
if (!allCoreHooked) {
LOGGER.log(Level.WARNING,
"Keploy dedup: incomplete JDK sink instrumentation (thread="
+ JdkSinkTransformer.hookedThreadStart
+ ", tpe=" + JdkSinkTransformer.hookedTpeExecute
+ ", stpe=" + JdkSinkTransformer.hookedStpeDelayedExecute
+ ", fjp=" + JdkSinkTransformer.hookedFjpSubmit
+ ", timer=" + JdkSinkTransformer.hookedTimerSched + ")");
failed = true;
return false;
}
active = true;
if (SinkAllowlist.AUDIT) {
System.err.println("KEPLOY_SINK_BOOTSTRAP_INSTALLED bridgeCL=" + bridgeClass.getClassLoader()
+ " thread=" + JdkSinkTransformer.hookedThreadStart
+ " vthread=" + JdkSinkTransformer.hookedVirtualThreadStart
+ " tpe=" + JdkSinkTransformer.hookedTpeExecute
+ " stpe=" + JdkSinkTransformer.hookedStpeDelayedExecute
+ " fjp=" + JdkSinkTransformer.hookedFjpSubmit
+ " timer=" + JdkSinkTransformer.hookedTimerSched
+ " handler=" + (BridgeAccess.hasHandler()));
}
return true;
} catch (Throwable t) {
failed = true;
LOGGER.log(Level.WARNING,
"Keploy dedup: could not install bootstrap JDK sink bridge; asyncExecutors will fail closed", t);
return false;
}
}

static void enterInternal() {
if (active) {
BridgeAccess.enterInternal();
}
}

static void exitInternal() {
if (active) {
BridgeAccess.exitInternal();
}
}

static boolean isInternal() {
return active && BridgeAccess.isInternal();
}

private static void grantJavaBaseReadAccess(Instrumentation instrumentation, Class<?> bridgeClass)
throws Exception {
Method getModule;
try {
getModule = Class.class.getMethod("getModule");
} catch (NoSuchMethodException jdk8) {
// JDK 8 has no JPMS modules.
return;
}
Object javaBaseModule = getModule.invoke(Object.class);
Object bridgeModule = getModule.invoke(bridgeClass);
if (javaBaseModule == null || bridgeModule == null) {
return;
}
Class<?> moduleClass = Class.forName("java.lang.Module");
Method redefineModule = Instrumentation.class.getMethod("redefineModule",
moduleClass, Set.class, Map.class, Map.class, Set.class, Map.class);
redefineModule.invoke(instrumentation,
javaBaseModule,
Collections.singleton(bridgeModule),
Collections.emptyMap(),
Collections.emptyMap(),
Collections.emptySet(),
Collections.emptyMap());
}

private static File buildBootstrapBridgeJar() throws IOException {
ClassLoader cl = JdkSinkBootstrap.class.getClassLoader();
byte[] bridgeBytes = readResource(cl, BRIDGE_CLASS_RESOURCE);
byte[] handlerBytes = readResource(cl, BRIDGE_HANDLER_RESOURCE);
File tempJar = Files.createTempFile("keploy-sink-bridge-", ".jar").toFile();
tempJar.deleteOnExit();
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(tempJar))) {
writeJarEntry(jos, BRIDGE_CLASS_RESOURCE, bridgeBytes);
writeJarEntry(jos, BRIDGE_HANDLER_RESOURCE, handlerBytes);
}
return tempJar;
}

private static byte[] readResource(ClassLoader cl, String resource) throws IOException {
try (InputStream is = cl != null ? cl.getResourceAsStream(resource)
: ClassLoader.getSystemResourceAsStream(resource)) {
if (is == null) {
throw new IOException("Missing bootstrap bridge resource: " + resource);
}
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
byte[] buf = new byte[4096];
int n;
while ((n = is.read(buf)) != -1) {
out.write(buf, 0, n);
}
return out.toByteArray();
}
}

private static void writeJarEntry(JarOutputStream jos, String name, byte[] bytes) throws IOException {
jos.putNextEntry(new JarEntry(name));
jos.write(bytes);
jos.closeEntry();
}

// Separate nested holder so KeploySinkBridge is NEVER referenced by
// JdkSinkBootstrap's constant pool until AFTER appendToBootstrapClassLoaderSearch.
private static final class BridgeAccess {
static void installHandler() {
io.keploy.dedup.bootstrap.KeploySinkBridge.handler = new JdkSinkHandler();
}

static void enterInternal() {
io.keploy.dedup.bootstrap.KeploySinkBridge.enterInternal();
}

static void exitInternal() {
io.keploy.dedup.bootstrap.KeploySinkBridge.exitInternal();
}

static boolean isInternal() {
return io.keploy.dedup.bootstrap.KeploySinkBridge.isInternal();
}

static boolean hasHandler() {
return io.keploy.dedup.bootstrap.KeploySinkBridge.handler != null;
}

static boolean hasCallbackError() {
return io.keploy.dedup.bootstrap.KeploySinkBridge.lastCallbackError != null;
}
}
}
Loading
Loading