- tests: add TestSupport with a single RESULT: PASS/FAIL verdict protocol - tests: assert on native return values and captured output, not runner markers - tests: replace the brittle "Bytes: 399" marker with a class-file magic check - tests: read the shared case list from tests/cases.txt - tests: add run-tests.sh and copy-bin.sh, run the tests on Linux too - tests: exclude the Loop.java demo from the compiled test output - CI: set up JDK 25 and run the integration tests in the build-linux job - Docs: refresh the AGENTS.md test/CI sections and rule 5
8.9 KiB
AGENTS.md
Guidance for AI coding agents (and humans) working in this repository.
Project at a glance
JuiceAgent is a JVMTI-based runtime injection framework for Windows. It injects
into a running JVM (even with DisableAttachMechanism=true, no -javaagent),
loads a JAR and calls an entry point, and exposes native APIs to
redefine/retransform classes, query loaded classes and read bytecode.
Four binaries:
| Target | Output | Role |
|---|---|---|
injector |
injector.exe |
CLI driver: loads libinject.dll and injects libloader.dll into a target PID |
libinject |
libinject.dll |
Reflective DLL injector (exports inject, plus JNI bindings) |
libloader |
libloader.dll |
Reflectively loaded into the target JVM. Attaches to the JVM, reads config.toml, registers the embedded JuiceAgent-API jar, bootstraps the agent |
libagent |
libagent.dll |
The JVMTI agent. Registers ClassFileLoadHook and implements the native side of the JuiceAgent-API |
Toolchain
- Windows x64, MinGW-w64 GCC (the maintainer uses w64devkit), Ninja, CMake 3.15+
- C++20
- JDK 17+ for the Java side.
--enable-native-access=ALL-UNNAMEDis required from JDK 22 on to keepSystem.loadwarning-free (a future JDK will deny native access without it).
Build
cmake --preset release # or: --preset debug
cmake --build build
Binaries land in build/bin.
Alternatively, without presets:
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build
Test
Integration tests live in tests/. They are plain main() programs (not JUnit)
that load the native agent (resources/libagent.dll on Windows,
resources/libagent.so on Linux) and call JuiceAgent.init(""). The shared
tests/src/TestSupport.java performs that bootstrap and prints the single verdict
line each test is judged on (RESULT: PASS <name> / RESULT: FAIL <name>: ...).
Some tests also need the injection module compiled into
out/production/injection.
Build first, then:
# Windows
pwsh tests/run-tests.ps1
# or point it at a specific JDK:
pwsh tests/run-tests.ps1 -JdkHome "C:\Program Files\Zulu\zulu-25"
# Linux
bash tests/run-tests.sh
# or: JAVA_HOME=/path/to/jdk bash tests/run-tests.sh
Both runners compile the modules, refresh tests/resources from build/bin
(tests/copy-bin.bat / tests/copy-bin.sh), compile the tests, then run every
case listed in tests/cases.txt and assert on its verdict output. Loop.java
is a manual demo (infinite loop) and is not compiled into the test output.
Running the tests rebuilds the tracked file
tests/injection.jar. After a local run, restore it withgit checkout -- tests/injection.jarbefore committing.
Release
-
Bump
Version.txt(Major/Minor/Patch). -
Add
docs/update/vX.Y.Z.md(bilingual release notes) and an entry indocs/update/CHANGELOG.md. -
Commit the docs, then commit
Version X.Y.Z, then tagvX.Y.Z. -
Build the package and attach it to a GitHub Release:
cmake -P script/package.cmake # -> dist/JuiceAgent_vX.Y.Z_x64.zip(
script/package.batwraps this but ends withpause.)On Linux,
script/package.shpacksinjector,libloader.soandlibagent.sointodist/JuiceAgent_vX.Y.Z_x64_linux.tar.gz:./script/package.sh
tests/, build/, dist/ and *.log are gitignored; dist/*.zip and
dist/*.tar.gz are not committed.
Hard rules (each of these was learned by breaking the build or a target JVM)
1. libloader must never throw a C++ exception
libloader.dll is loaded by a reflective loader (manual mapping), so its
unwind tables are never registered with the OS. A C++ exception thrown inside it
cannot be unwound or caught - it crashes the target JVM with
EXCEPTION_ILLEGAL_INSTRUCTION (a try/catch there is useless, not a fix).
- The
loadertarget is compiled with-fno-exceptions(andSPDLOG_NO_EXCEPTIONS) on purpose: any exception-capable code fails the build. - Report problems with
spdlog+ an earlyreturn, never withthrow. - This also means library code that can throw (
toml::parse_file,std::filesystem, allocations) must be used in a non-throwing way. toml++ switches to the non-throwingtoml::parse_resultAPI automatically when exceptions are disabled.
2. Config reads are non-throwing
includes/JuiceAgent/Config.hpp intentionally has no throwing getters. Use:
if (!cfg.contains("JarLoader.Mode")) { spdlog::error("Missing key: JarLoader.Mode"); return; }
const std::string mode = cfg.get<std::string>("JarLoader.Mode", "");
For required values, check contains() first and then get(path, default).
3. Config schema changes are a coordinated edit
The current schema is v2 (CONFIG_VERSION in includes/JuiceAgent/JuiceAgent.hpp).
It is validated at load time, so a mismatch makes the loader refuse to start.
Changing the schema requires updating, together:
CONFIG_VERSIONdocs/config.md- the three
examples/*/config.toml - the config block in
README.mdandREADME_ZH.md
4. Keep the MinGW runtime out of the shipped DLLs
Do not introduce a dependency on libstdc++-6.dll / libgcc_s_seh-1.dll /
libwinpthread-1.dll. A DLL that needs them can pick up an incompatible copy
from elsewhere on the machine and fail to load (UnsatisfiedLinkError: The specified procedure could not be found). w64devkit links the runtime statically;
CI passes -static-libgcc -static-libstdc++ to match.
5. Tests assert on output, not exit codes
The test programs print diagnostics and still exit 0 when a step fails. Each
one ends by printing a single verdict line - RESULT: PASS <name> or
RESULT: FAIL <name>: <reason> - and the runners match RESULT: PASS <name>
(and reject any RESULT: FAIL) instead of trusting the exit code, which would
silently pass broken builds.
Notes when adding tests:
GetClassBytesByNameonly resolves already loaded classes; load the target first (Class.forName("Target")).- Tests that print to stderr make Windows PowerShell 5.1 unhappy; the runner sets
$ErrorActionPreference = "Continue"and relies on explicit checks.
Layout
src/injector injector.exe
src/libinject libinject.dll (reflective injector)
src/libloader libloader.dll (runs in the target; exception-free)
src/libagent libagent.dll (JVMTI agent)
services/ IService implementations, self-registered via JUICEAGENT_REGISTER_SERVICE
includes/eventbus.hpp typed EventBus
includes/JuiceAgent Config.hpp / Logger.hpp / Utils.hpp / JuiceAgent.hpp (CONFIG_VERSION)
includes/toml++ vendored
includes/spdlog vendored
includes/ptrace-inject-cpp vendored Linux injector library (libptrace_inject.a)
tests/ integration tests + run-tests.{ps1,sh} + copy-bin.{bat,sh} + cases.txt
docs/ config.md, advanced.md, Inject.md, update/ (release notes + changelog)
examples/ runnable end-to-end examples (each has config.toml + HowToUse)
script/ package.cmake / package.bat
Conventions
Services and the event bus
A service derives from IService (src/libagent/includes/services/base.hpp) and
self-registers at file scope:
JUICEAGENT_REGISTER_SERVICE(::JuiceAgent::Services::MyService)
Subscribe with the typed bus - there is no EventId and no void* casting:
listen<MyService, EventLoaded>(&MyService::onLoaded); // append
listenFront<MyService, EventLoaded>(&MyService::onLoaded); // run before others
Publish with EventBus::getInstance().post(event). Call unlistenAll() in
onShutdown().
Commit messages
Type: subject, then - component: detail bullets. Types used here:
Feat, BugFix, Refactor, Docs, CI, and Version X.Y.Z for the release
commit.
Docs
README.md and README_ZH.md are kept in sync; keep both updated. Prose docs
live in docs/ (English). Release notes and the changelog live in docs/update/.
Line endings
Tracked text files are LF in the index, CRLF in the working tree (no
.gitattributes). When creating files, write CRLF to avoid noisy diffs.
CI
.github/workflows/ci.yml has two jobs:
build-and-testonwindows-latest: JDK 25 (Temurin) + MinGW-w64 (MSYS2) + Ninja, thenconfigure -> build -> tests -> package, uploadingdist/JuiceAgent_*.zipas an artifact (14-day retention).build-linuxonubuntu-latest: JDK 25 (Temurin) + GCC + Ninja, thenconfigure -> build -> tests -> package, uploadingdist/JuiceAgent_*.tar.gz.
CI-specific notes:
- It builds with
-static-libgcc -static-libstdc++and also copies any remaining non-system imported DLLs next to the binaries (see rule 4). - The workflow prints the DLL import list - useful when a load error appears.
- GitHub's unauthenticated REST API is limited to 60 requests/hour, so avoid
polling workflow status in a loop; install
ghor use a token instead.