summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile76
-rw-r--r--stubs/.gitignore1
-rw-r--r--stubs/src/kotlin/proxy/KotlinProxyNoLog.java51
-rwxr-xr-xtools/gen_class.py123
5 files changed, 192 insertions, 60 deletions
diff --git a/.gitignore b/.gitignore
index e9c8436..9285c6c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,5 +2,6 @@ dist/
.stamp
vendor/
stubs/.compiled
+stubs/.compiled_nolog
stubs/gen/
tools/__pycache__/
diff --git a/Makefile b/Makefile
index 8c451df..cbe0c12 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,20 @@
.PHONY: all clean compileStubs jars
-# Stamp for the javac step (kept OUT of stubs/classes_min so it is not
-# packaged into the jar; gen_class.py wipes that tree anyway).
-STUBS_STAMP = stubs/.compiled
+# Two jar flavors, built from the same kotlin.proxy.* support sources and the
+# generated argument table; only the generated compiler trio differs. The
+# default flavor logs every stub method/ctor/class-load via KotlinProxyLog;
+# the --no-log flavor drops that logging and routes reference returns through
+# KotlinProxyNoLog instead.
+LOG_OUT = stubs/classes_min
+NOLOG_OUT = stubs/classes_nolog
+LOG_JAR = dist/kotlin-proxy-compiler-stubs.jar
+NOLOG_JAR = dist/kotlin-proxy-compiler-stubs-nolog.jar
+SEED_CLASSES = org/jetbrains/kotlin/cli/jvm/K2JVMCompiler,org/jetbrains/kotlin/cli/jvm/K2JVMCompiler$$Companion,org/jetbrains/kotlin/cli/jvm/K2JVMCompilerKt
+
+# Stamps for the javac step (kept OUT of the class trees so they are not
+# packaged into the jars; gen_class.py wipes those trees anyway).
+STUBS_STAMP = stubs/.compiled
+STUBS_NOLOG_STAMP = stubs/.compiled_nolog
# Versions the jar lists (jars/*.txt) are generated for — see
# tools/gen_jar_lists.py. Keep in sync with test_project/gradle/
@@ -18,7 +30,7 @@ ARGS_DESCRIPTION = vendor/kotlin-compiler-arguments-description-$(KOTLIN_VERSION
# tools/gen_arg_table.py / tools/ArgProbe.java).
GEN_ARGS_JAVA = stubs/gen/kotlin/proxy/GeneratedJvmArguments.java
-all: dist/kotlin-proxy-compiler-stubs.jar
+all: $(LOG_JAR) $(NOLOG_JAR)
# Plain curl: the artifact is not a build dependency of anything else, so
# there is no Gradle/dependency machinery involved.
@@ -49,14 +61,17 @@ jars/.stamp: tools/gen_jar_lists.py Makefile
--agp-version $(AGP_VERSION) --out-dir jars
clean:
- rm -f dist/kotlin-proxy-compiler-stubs.jar
- rm -f $(STUBS_STAMP)
- rm -rf stubs/classes_min/*
+ rm -f $(LOG_JAR) $(NOLOG_JAR)
+ rm -f $(STUBS_STAMP) $(STUBS_NOLOG_STAMP)
+ rm -rf $(LOG_OUT) $(NOLOG_OUT)
rm -rf stubs/gen
rm -rf vendor
rm -f jars/.stamp
-stubs/classes_min/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class: \
+# The trio (log flavor) — each generated .class is the real prerequisite for
+# its tree's javac step. gen_class.py wipes --out first, so the two trees are
+# independent.
+$(LOG_OUT)/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class: \
tools/gen_class.py jars/scan.txt jars/defs.txt jars/fallback.txt \
jars/.stamp
python3 tools/gen_class.py \
@@ -64,24 +79,51 @@ stubs/classes_min/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class: \
--def-jars jars/defs.txt \
--fallback-jars jars/fallback.txt \
--seed-only --no-closure \
- --seed 'org/jetbrains/kotlin/cli/jvm/K2JVMCompiler,org/jetbrains/kotlin/cli/jvm/K2JVMCompiler$$Companion,org/jetbrains/kotlin/cli/jvm/K2JVMCompilerKt' \
- --out stubs/classes_min
+ --seed '$(SEED_CLASSES)' \
+ --out $(LOG_OUT)
+
+# The trio (no-log flavor) — identical seeds, but the stubs skip
+# KotlinProxyLog (no loaded/call) and route reference returns through
+# KotlinProxyNoLog.
+$(NOLOG_OUT)/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class: \
+ tools/gen_class.py jars/scan.txt jars/defs.txt jars/fallback.txt \
+ jars/.stamp
+ python3 tools/gen_class.py \
+ --scan-jars jars/scan.txt \
+ --def-jars jars/defs.txt \
+ --fallback-jars jars/fallback.txt \
+ --seed-only --no-closure --no-log \
+ --seed '$(SEED_CLASSES)' \
+ --out $(NOLOG_OUT)
-# javac is driven by a stamp file (not .PHONY) so `make` is a no-op when
+# javac is driven by stamp files (not .PHONY) so `make` is a no-op when
# nothing changed - the stubs/src and stubs/gen sources, the generated stub
-# classes and defs.txt are the real inputs.
-$(STUBS_STAMP): stubs/classes_min/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class \
+# classes and defs.txt are the real inputs. Both trees compile the same
+# support sources (KotlinProxyLog AND KotlinProxyNoLog are in both jars; each
+# stub trio only references the one it was generated for).
+$(STUBS_STAMP): $(LOG_OUT)/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class \
stubs/src/kotlin/proxy/*.java $(GEN_ARGS_JAVA)
# DEFS is computed at recipe time: defs.txt may have been rewritten
# earlier in this same make run (via jars/.stamp), and a $(shell)
# variable would still hold the pre-rewrite contents.
DEFS=$$(grep -v '^[[:space:]]*#' jars/defs.txt | tr '\n' ':'); \
- javac -nowarn -proc:none -d stubs/classes_min \
+ javac -nowarn -proc:none -d $(LOG_OUT) \
-cp "$$DEFS" \
stubs/src/kotlin/proxy/*.java stubs/gen/kotlin/proxy/*.java; \
touch $@
-compileStubs: $(STUBS_STAMP)
+$(STUBS_NOLOG_STAMP): $(NOLOG_OUT)/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class \
+ stubs/src/kotlin/proxy/*.java $(GEN_ARGS_JAVA)
+ DEFS=$$(grep -v '^[[:space:]]*#' jars/defs.txt | tr '\n' ':'); \
+ javac -nowarn -proc:none -d $(NOLOG_OUT) \
+ -cp "$$DEFS" \
+ stubs/src/kotlin/proxy/*.java stubs/gen/kotlin/proxy/*.java; \
+ touch $@
+
+compileStubs: $(STUBS_STAMP) $(STUBS_NOLOG_STAMP)
+
+$(LOG_JAR): $(STUBS_STAMP)
+ jar cf $@ -C $(LOG_OUT) .
-dist/kotlin-proxy-compiler-stubs.jar: $(STUBS_STAMP)
- jar cf $@ -C stubs/classes_min .
+$(NOLOG_JAR): $(STUBS_NOLOG_STAMP)
+ jar cf $@ -C $(NOLOG_OUT) .
diff --git a/stubs/.gitignore b/stubs/.gitignore
index bb372d0..639c859 100644
--- a/stubs/.gitignore
+++ b/stubs/.gitignore
@@ -1 +1,2 @@
classes_min/
+classes_nolog/
diff --git a/stubs/src/kotlin/proxy/KotlinProxyNoLog.java b/stubs/src/kotlin/proxy/KotlinProxyNoLog.java
new file mode 100644
index 0000000..8b7e202
--- /dev/null
+++ b/stubs/src/kotlin/proxy/KotlinProxyNoLog.java
@@ -0,0 +1,51 @@
+package kotlin.proxy;
+
+public final class KotlinProxyNoLog {
+ private KotlinProxyNoLog() {
+ }
+
+ /**
+ * Return value for stub methods with a reference return type (other than
+ * String). Asks {@link KotlinProxyOverrides#invoke} first — per-method
+ * custom implementations, passed the full argument list and the receiver
+ * (the stub instance the method was called on, null for statics) — and
+ * falls back to materializing a real no-arg instance of the declared
+ * return type. The generated body checkcasts the result to the declared
+ * type.
+ *
+ * The generated bytecode pushes the args array FIRST (member, owner,
+ * return type and receiver last) so its stack stays shallow (peak 5) —
+ * keep this parameter order in sync with RETURN_VALUE_DESC in
+ * gen_class.py.
+ */
+ public static Object returnValue(Object[] args, String member,
+ String ownerDotName, String returnDotName,
+ Object instance) {
+ Object v = KotlinProxyOverrides.invoke(member, ownerDotName,
+ returnDotName, args, instance);
+ if (v == KotlinProxyOverrides.NO_VALUE) {
+ return newInstance(returnDotName);
+ }
+ return v;
+ }
+
+ /**
+ * Default return-value materialization (the fallback of
+ * {@link #returnValue}). Reflectively instantiates the real, un-stubbed
+ * class of the declared return type so the caller (the daemon) can keep
+ * going: e.g.
+ * K2JVMCompiler.createArguments() returns a genuine K2JVMCompilerArguments,
+ * which lets parseCommandLineArguments run and doExecute be reached with the
+ * fully-populated args (the boundary we want to log). Returns null when the
+ * type has no usable no-arg constructor (interfaces, enums, abstract, etc.).
+ */
+ private static Object newInstance(String dotName) {
+ try {
+ Class<?> c = Class.forName(dotName, false,
+ KotlinProxyNoLog.class.getClassLoader());
+ return c.getDeclaredConstructor().newInstance();
+ } catch (Throwable t) {
+ return null;
+ }
+ }
+}
diff --git a/tools/gen_class.py b/tools/gen_class.py
index 866576c..b52871e 100755
--- a/tools/gen_class.py
+++ b/tools/gen_class.py
@@ -16,6 +16,12 @@ custom implementations, passed the full argument list and the receiver —
the stub instance the method was called on, null for statics) before falling
back to a real no-arg instance of the declared return type.
+With --no-log the stubs skip KotlinProxyLog entirely: the loaded()/call()
+logging is dropped, and reference-return methods ask
+kotlin.proxy.KotlinProxyNoLog.returnValue (same signature, no logging)
+instead of KotlinProxyLog.returnValue. The emitted classes then reference
+kotlin.proxy.KotlinProxyNoLog only.
+
The optional Signature attribute is OMITTED entirely, so there are no
generics in the emitted class files and therefore no generics clashes.
The JVM links by name + erased descriptor, both of which are copied
@@ -26,7 +32,7 @@ needed.
Usage:
python3 gen_class.py --scan-jars <list> --def-jars <list> --out <dir>
- [--fallback-jars <list>] [--report <file>]
+ [--fallback-jars <list>] [--report <file>] [--no-log]
"""
import argparse
@@ -38,6 +44,7 @@ import zipfile
MAGIC = 0xCAFEBABE
MAJOR, MINOR = 52, 0 # Java 8
KPL = "kotlin/proxy/KotlinProxyLog"
+KNPL = "kotlin/proxy/KotlinProxyNoLog"
OBJ = "java/lang/Object"
CALL_DESC = "(Ljava/lang/String;Ljava/lang/String;)V"
CALL_ARG_DESC = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V"
@@ -435,30 +442,35 @@ def new_instance_target(desc):
return internal
-def log_body(cp, member, dotname, desc, is_static, call_ref):
- """Straight-line: log the call (with the first reference param, when
- there is one), produce the return value, return.
+def log_body(cp, member, dotname, desc, is_static, call_ref, no_log):
+ """Straight-line: (unless no_log) log the call (with the first reference
+ param, when there is one), produce the return value, return.
- Reference-class returns ask KotlinProxyLog.returnValue, which dispatches
- to KotlinProxyOverrides.invoke (per-method custom implementations, passed
- the full argument list and the receiver — aload_0, or null for statics)
- and falls back to materializing a real instance of the declared return
- type — so the daemon can proceed past e.g. createArguments() into
- doExecute() with a populated args object."""
- slot, has_arg = first_param_ref_slot(desc, is_static)
- if has_arg:
- call_arg_ref = cp.methodref(KPL, "call", CALL_ARG_DESC)
- b = ldc(cp.string(member)) + ldc(cp.string(dotname)) + aload(slot) \
- + bytes([0xb8]) + struct.pack(">H", call_arg_ref)
- max_stack = 3
- else:
- b = ldc(cp.string(member)) + ldc(cp.string(dotname)) \
- + bytes([0xb8]) + struct.pack(">H", call_ref)
- max_stack = 2
+ Reference-class returns ask KotlinProxyLog.returnValue — or
+ KotlinProxyNoLog.returnValue in --no-log mode, which skips the logging —
+ which dispatches to KotlinProxyOverrides.invoke (per-method custom
+ implementations, passed the full argument list and the receiver —
+ aload_0, or null for statics) and falls back to materializing a real
+ instance of the declared return type — so the daemon can proceed past
+ e.g. createArguments() into doExecute() with a populated args object."""
+ b = b""
+ max_stack = 0
+ if not no_log:
+ slot, has_arg = first_param_ref_slot(desc, is_static)
+ if has_arg:
+ call_arg_ref = cp.methodref(KPL, "call", CALL_ARG_DESC)
+ b = ldc(cp.string(member)) + ldc(cp.string(dotname)) + aload(slot) \
+ + bytes([0xb8]) + struct.pack(">H", call_arg_ref)
+ max_stack = 3
+ else:
+ b = ldc(cp.string(member)) + ldc(cp.string(dotname)) \
+ + bytes([0xb8]) + struct.pack(">H", call_ref)
+ max_stack = 2
ti = new_instance_target(desc)
if ti is not None:
# [argsArray, member, owner, returnType, instance] -> returnValue(...)
- rv = cp.methodref(KPL, "returnValue", RETURN_VALUE_DESC)
+ rv = cp.methodref(KNPL if no_log else KPL, "returnValue",
+ RETURN_VALUE_DESC)
b += args_array_body(cp, desc, is_static)
b += ldc(cp.string(member)) + ldc(cp.string(dotname)) \
+ ldc(cp.string(ti.replace("/", ".")))
@@ -468,17 +480,22 @@ def log_body(cp, member, dotname, desc, is_static, call_ref):
# the verifier is happy (null passes checkcast, so un-instantiable
# types still just return null).
b += bytes([0xc0]) + struct.pack(">H", cp.cls(ti))
- max_stack = 5
+ max_stack = max(max_stack, 5)
else:
push, _ = ret_bytes(desc)
b += push
+ r = desc[desc.rindex(")") + 1:]
+ # With no_log there is no call() on the stack, so the default push
+ # itself sets the peak: 2 for J/D, 1 for other non-void, 0 for void.
+ max_stack = max(max_stack, 2 if r in "JD" else (1 if r != "V" else 0))
_, ret = ret_bytes(desc)
b += ret
max_locals = count_locals(desc, is_static)
return code_attr(cp, max_stack, max_locals, b)
-def ctor_body(cp, super_name, member, dotname, desc, call_ref, is_enum):
+def ctor_body(cp, super_name, member, dotname, desc, call_ref, is_enum,
+ no_log):
# Enums: java.lang.Enum has no no-arg ctor, so the super call must be
# Enum.<init>(String name, int ordinal). The enum ctor's own name/ordinal
# params (last two) are ignored — we pass constants; unused params are
@@ -491,18 +508,27 @@ def ctor_body(cp, super_name, member, dotname, desc, call_ref, is_enum):
max_stack = 3
else:
sup = cp.methodref(super_name, "<init>", "()V")
- max_stack = 2
+ # with no_log there is no trailing call(), so only aload_0 is live
+ max_stack = 2 if not no_log else 1
b += bytes([0xb7]) + struct.pack(">H", sup) # invokespecial
- b += ldc(cp.string(member)) + ldc(cp.string(dotname))
- b += bytes([0xb8]) + struct.pack(">H", call_ref)
+ if not no_log:
+ b += ldc(cp.string(member)) + ldc(cp.string(dotname))
+ b += bytes([0xb8]) + struct.pack(">H", call_ref)
b += b"\xb1" # return
return code_attr(cp, max_stack, count_locals(desc, False), b)
-def clinit_body(cp, dotname, loaded_ref, internal, companion=None):
- b = ldc(cp.string(dotname))
- b += bytes([0xb8]) + struct.pack(">H", loaded_ref)
- max_stack = 2
+def clinit_body(cp, dotname, loaded_ref, internal, companion=None,
+ no_log=False):
+ b = b""
+ if no_log:
+ # no KotlinProxyLog.loaded() — the body is the companion init (or just
+ # return) only.
+ max_stack = 0
+ else:
+ b = ldc(cp.string(dotname))
+ b += bytes([0xb8]) + struct.pack(">H", loaded_ref)
+ max_stack = 2
if companion:
# new; dup; aconst_null -> 3 on the stack before invokespecial pops
max_stack = 3
@@ -583,7 +609,7 @@ def field_bytes(cp, name, desc, access):
# ----------------------------------------------------------------------
# class emission
# ----------------------------------------------------------------------
-def emit_class(cp, name, info, is_iface, dotname, companion=None):
+def emit_class(cp, name, info, is_iface, dotname, companion=None, no_log=False):
this_idx = cp.cls(name)
super_name = (info.get("super") or OBJ) if not is_iface else OBJ
super_idx = cp.cls(super_name)
@@ -596,8 +622,11 @@ def emit_class(cp, name, info, is_iface, dotname, companion=None):
if name == OBJ:
return None
- call_ref = cp.methodref(KPL, "call", CALL_DESC)
- loaded_ref = cp.methodref(KPL, "loaded", LOADED_DESC)
+ # With no_log the stubs never touch KotlinProxyLog, so keep those methodrefs
+ # out of the constant pool entirely (only KotlinProxyNoLog is referenced,
+ # lazily, from the reference-return bodies).
+ call_ref = None if no_log else cp.methodref(KPL, "call", CALL_DESC)
+ loaded_ref = None if no_log else cp.methodref(KPL, "loaded", LOADED_DESC)
fields = []
for f in info.get("fields", []):
@@ -627,7 +656,7 @@ def emit_class(cp, name, info, is_iface, dotname, companion=None):
if md == "()V":
has_noarg = True
body = ctor_body(cp, super_name, mn, dotname, md, call_ref,
- is_enum)
+ is_enum, no_log)
methods.append(method_bytes(cp, mn, md, ma, body))
elif (ma & A_BRIDGE) and not md[1:md.rindex(")")] \
and mn in concrete_noparam:
@@ -640,21 +669,22 @@ def emit_class(cp, name, info, is_iface, dotname, companion=None):
methods.append(method_bytes(cp, mn, md, ma, None))
else:
body = log_body(cp, mn, dotname, md,
- bool(ma & A_STATIC), call_ref)
+ bool(ma & A_STATIC), call_ref, no_log)
methods.append(method_bytes(cp, mn, md, ma, body))
if not is_iface:
# guarantee a no-arg ctor so child stubs can invokespecial super()
if not has_noarg:
body = ctor_body(cp, super_name, "<init>", dotname, "()V",
- call_ref, is_enum)
+ call_ref, is_enum, no_log)
methods.append(method_bytes(cp, "<init>", "()V",
A_PUBLIC, body))
- # synthesized static initializer -> KotlinProxyLog.loaded(name),
- # plus the companion-object init when the class has one
+ # synthesized static initializer -> KotlinProxyLog.loaded(name)
+ # (skipped with --no-log), plus the companion-object init when the
+ # class has one
methods.append(method_bytes(cp, "<clinit>", "()V",
A_STATIC | A_PRIVATE,
clinit_body(cp, dotname, loaded_ref,
- name, companion)))
+ name, companion, no_log)))
out = bytearray()
out += struct.pack(">IHH", MAGIC, MINOR, MAJOR)
@@ -675,14 +705,15 @@ def emit_class(cp, name, info, is_iface, dotname, companion=None):
return bytes(out)
-def emit_minimal(cp, name, is_iface):
+def emit_minimal(cp, name, is_iface, no_log=False):
"""A class/interface with no definition found: bare structure +
(for classes) a no-arg ctor and a loaded() clinit."""
info = {"access": A_PUBLIC, "super": OBJ, "interfaces": [],
"fields": [], "methods": []}
if is_iface:
info["access"] |= A_INTERFACE | A_ABSTRACT
- return emit_class(cp, name, info, is_iface, name.replace("/", "."))
+ return emit_class(cp, name, info, is_iface, name.replace("/", "."),
+ no_log=no_log)
# ----------------------------------------------------------------------
@@ -712,6 +743,11 @@ def main():
"the seed set only. Use with --seed to stub just the "
"compiler entry point; its super/interfaces are then "
"resolved from the real jars at load time")
+ ap.add_argument("--no-log", action="store_true",
+ help="do not call KotlinProxyLog: drop the loaded()/call() "
+ "logging and route reference returns through "
+ "KotlinProxyNoLog.returnValue instead (the stubs then "
+ "reference kotlin.proxy.KotlinProxyNoLog only)")
args = ap.parse_args()
exclude = [p for p in (args.exclude.split(",") if args.exclude else [])
@@ -826,14 +862,15 @@ def main():
cp = CP()
if e is None:
is_iface = kind_hint.get(name) == "interface"
- data = emit_minimal(cp, name, is_iface)
+ data = emit_minimal(cp, name, is_iface, args.no_log)
minimal.append(name)
n_minimal += 1
else:
_, info = e
is_iface = bool(info["access"] & A_INTERFACE)
companion = None if is_iface else companion_init(name, info, defs)
- data = emit_class(cp, name, info, is_iface, dot, companion)
+ data = emit_class(cp, name, info, is_iface, dot, companion,
+ args.no_log)
n_full += 1
if is_iface:
n_iface += 1