|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.entitlement.tools; |
| 11 | + |
| 12 | +import org.elasticsearch.core.Tuple; |
| 13 | +import org.objectweb.asm.ClassReader; |
| 14 | +import org.objectweb.asm.ClassVisitor; |
| 15 | +import org.objectweb.asm.MethodVisitor; |
| 16 | +import org.objectweb.asm.Opcodes; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.lang.constant.ClassDesc; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Comparator; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.TreeMap; |
| 25 | +import java.util.TreeSet; |
| 26 | +import java.util.function.Predicate; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +import static java.util.Collections.emptySet; |
| 30 | + |
| 31 | +public class AccessibleJdkMethods { |
| 32 | + |
| 33 | + private static final Set<AccessibleMethod.Descriptor> EXCLUDES = Set.of( |
| 34 | + new AccessibleMethod.Descriptor("toString", "()Ljava/lang/String;", true, false), |
| 35 | + new AccessibleMethod.Descriptor("hashCode", "()I", true, false), |
| 36 | + new AccessibleMethod.Descriptor("equals", "(Ljava/lang/Object;)Z", true, false), |
| 37 | + new AccessibleMethod.Descriptor("close", "()V", true, false) |
| 38 | + ); |
| 39 | + |
| 40 | + public record AccessibleMethod(Descriptor descriptor, boolean isFinal, boolean isDeprecated) { |
| 41 | + public record Descriptor(String method, String descriptor, boolean isPublic, boolean isStatic) { |
| 42 | + public static final Comparator<Descriptor> COMPARATOR = Comparator.comparing(Descriptor::method) |
| 43 | + .thenComparing(Descriptor::descriptor) |
| 44 | + .thenComparing(Descriptor::isStatic); |
| 45 | + } |
| 46 | + |
| 47 | + public static final Comparator<AccessibleMethod> COMPARATOR = Comparator.comparing( |
| 48 | + AccessibleMethod::descriptor, |
| 49 | + Descriptor.COMPARATOR |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public record ModuleClass(String module, String clazz) { |
| 54 | + public static final Comparator<ModuleClass> COMPARATOR = Comparator.comparing(ModuleClass::module) |
| 55 | + .thenComparing(ModuleClass::clazz); |
| 56 | + } |
| 57 | + |
| 58 | + public static Stream<Tuple<ModuleClass, AccessibleMethod>> loadAccessibleMethods(Predicate<String> modulePredicate) throws IOException { |
| 59 | + // 1st: map class names to module names (including later excluded modules) for lookup in 2nd step |
| 60 | + final Map<String, String> moduleNameByClass = Utils.loadClassToModuleMapping(); |
| 61 | + final Map<String, Set<String>> exportsByModule = Utils.loadExportsByModule(); |
| 62 | + final AccessibleMethodsVisitor visitor = new AccessibleMethodsVisitor(modulePredicate, moduleNameByClass, exportsByModule); |
| 63 | + // 2nd: calculate accessible implementations of classes in included modules |
| 64 | + Utils.walkJdkModules(modulePredicate, exportsByModule, (moduleName, moduleClasses, moduleExports) -> { |
| 65 | + for (var classFile : moduleClasses) { |
| 66 | + // visit class once (skips if class was already visited earlier due to a dependency on it) |
| 67 | + visitor.visitOnce(new ModuleClass(moduleName, Utils.internalClassName(classFile, moduleName))); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + return visitor.getAccessibleMethods().entrySet().stream().flatMap(e -> e.getValue().stream().map(m -> Tuple.tuple(e.getKey(), m))); |
| 72 | + } |
| 73 | + |
| 74 | + private static class AccessibleMethodsVisitor extends ClassVisitor { |
| 75 | + private final Map<ModuleClass, Set<AccessibleMethod>> inheritableAccessByClass = new TreeMap<>(ModuleClass.COMPARATOR); |
| 76 | + private final Map<ModuleClass, Set<AccessibleMethod>> accessibleImplementationsByClass = new TreeMap<>(ModuleClass.COMPARATOR); |
| 77 | + |
| 78 | + private final Predicate<String> modulePredicate; |
| 79 | + private final Map<String, String> moduleNameByClass; |
| 80 | + private final Map<String, Set<String>> exportsByModule; |
| 81 | + |
| 82 | + private Set<AccessibleMethod> accessibleImplementations; |
| 83 | + private Set<AccessibleMethod> inheritableAccess; |
| 84 | + |
| 85 | + private ModuleClass moduleClass; |
| 86 | + private boolean isPublicClass; |
| 87 | + private boolean isFinalClass; |
| 88 | + private boolean isDeprecatedClass; |
| 89 | + private boolean isExported; |
| 90 | + |
| 91 | + AccessibleMethodsVisitor( |
| 92 | + Predicate<String> modulePredicate, |
| 93 | + Map<String, String> moduleNameByClass, |
| 94 | + Map<String, Set<String>> exportsByModule |
| 95 | + ) { |
| 96 | + super(Opcodes.ASM9); |
| 97 | + this.modulePredicate = modulePredicate; |
| 98 | + this.moduleNameByClass = moduleNameByClass; |
| 99 | + this.exportsByModule = exportsByModule; |
| 100 | + } |
| 101 | + |
| 102 | + private static Set<AccessibleMethod> newSortedSet() { |
| 103 | + return new TreeSet<>(AccessibleMethod.COMPARATOR); |
| 104 | + } |
| 105 | + |
| 106 | + Map<ModuleClass, Set<AccessibleMethod>> getAccessibleMethods() { |
| 107 | + return Collections.unmodifiableMap(accessibleImplementationsByClass); |
| 108 | + } |
| 109 | + |
| 110 | + void visitOnce(ModuleClass moduleClass) { |
| 111 | + if (accessibleImplementationsByClass.containsKey(moduleClass)) { |
| 112 | + return; |
| 113 | + } |
| 114 | + if (moduleClass.clazz.startsWith("com/sun/") && moduleClass.clazz.contains("/internal/")) { |
| 115 | + // skip com.sun.*.internal classes as they are not part of the supported JDK API |
| 116 | + // even if methods override some publicly visible API |
| 117 | + return; |
| 118 | + } |
| 119 | + try { |
| 120 | + ClassReader cr = new ClassReader(moduleClass.clazz); |
| 121 | + cr.accept(this, 0); |
| 122 | + } catch (IOException e) { |
| 123 | + throw new RuntimeException(e); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { |
| 129 | + final Set<AccessibleMethod> currentInheritedAccess = newSortedSet(); |
| 130 | + if (superName != null) { |
| 131 | + var superModuleClass = getModuleClassFromName(superName); |
| 132 | + visitOnce(superModuleClass); |
| 133 | + currentInheritedAccess.addAll(inheritableAccessByClass.getOrDefault(superModuleClass, emptySet())); |
| 134 | + } |
| 135 | + if (interfaces != null && interfaces.length > 0) { |
| 136 | + for (var interfaceName : interfaces) { |
| 137 | + var interfaceModuleClass = getModuleClassFromName(interfaceName); |
| 138 | + visitOnce(interfaceModuleClass); |
| 139 | + currentInheritedAccess.addAll(inheritableAccessByClass.getOrDefault(interfaceModuleClass, emptySet())); |
| 140 | + } |
| 141 | + } |
| 142 | + // only initialize local state AFTER visiting all dependencies above! |
| 143 | + super.visit(version, access, name, signature, superName, interfaces); |
| 144 | + this.moduleClass = getModuleClassFromName(name); |
| 145 | + this.isExported = getModuleExports(moduleClass.module()).contains(getPackageName(name)); |
| 146 | + this.isPublicClass = (access & Opcodes.ACC_PUBLIC) != 0; |
| 147 | + this.isFinalClass = (access & Opcodes.ACC_FINAL) != 0; |
| 148 | + this.isDeprecatedClass = (access & Opcodes.ACC_DEPRECATED) != 0; |
| 149 | + this.inheritableAccess = currentInheritedAccess; |
| 150 | + this.accessibleImplementations = newSortedSet(); |
| 151 | + } |
| 152 | + |
| 153 | + private ModuleClass getModuleClassFromName(String name) { |
| 154 | + String module = moduleNameByClass.get(name); |
| 155 | + if (module == null) { |
| 156 | + throw new IllegalStateException("Unknown module for class: " + name); |
| 157 | + } |
| 158 | + return new ModuleClass(module, name); |
| 159 | + } |
| 160 | + |
| 161 | + private Set<String> getModuleExports(String module) { |
| 162 | + Set<String> exports = exportsByModule.get(module); |
| 163 | + if (exports == null) { |
| 164 | + throw new IllegalStateException("Unknown exports for module: " + module); |
| 165 | + } |
| 166 | + return exports; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void visitEnd() { |
| 171 | + super.visitEnd(); |
| 172 | + if (accessibleImplementationsByClass.put(moduleClass, unmodifiableSet(accessibleImplementations)) != null |
| 173 | + || inheritableAccessByClass.put(moduleClass, unmodifiableSet(inheritableAccess)) != null) { |
| 174 | + throw new IllegalStateException("Class " + moduleClass.clazz() + " was already visited!"); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private static Set<AccessibleMethod> unmodifiableSet(Set<AccessibleMethod> set) { |
| 179 | + return set.isEmpty() ? emptySet() : Collections.unmodifiableSet(set); |
| 180 | + } |
| 181 | + |
| 182 | + private static String getPackageName(String className) { |
| 183 | + return ClassDesc.ofInternalName(className).packageName(); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public final MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { |
| 188 | + MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions); |
| 189 | + boolean isPublic = (access & Opcodes.ACC_PUBLIC) != 0; |
| 190 | + boolean isProtected = (access & Opcodes.ACC_PROTECTED) != 0; |
| 191 | + boolean isFinal = (access & Opcodes.ACC_FINAL) != 0; |
| 192 | + boolean isStatic = (access & Opcodes.ACC_STATIC) != 0; |
| 193 | + boolean isDeprecated = (access & Opcodes.ACC_DEPRECATED) != 0; |
| 194 | + if ((isPublic || isProtected) == false) { |
| 195 | + return mv; |
| 196 | + } |
| 197 | + |
| 198 | + var methodDescriptor = new AccessibleMethod.Descriptor(name, descriptor, isPublic, isStatic); |
| 199 | + var method = new AccessibleMethod(methodDescriptor, isFinal, isDeprecatedClass || isDeprecated); |
| 200 | + if (isPublicClass && isExported && EXCLUDES.contains(methodDescriptor) == false) { |
| 201 | + // class is public and exported, to be accessible outside the JDK the method must be either: |
| 202 | + // - public or |
| 203 | + // - protected if not a final class |
| 204 | + if (isPublic || isFinalClass == false) { |
| 205 | + if (modulePredicate.test(moduleClass.module)) { |
| 206 | + accessibleImplementations.add(method); |
| 207 | + } |
| 208 | + // if public and not static, the method can be accessible on non-public and non-exported subclasses, |
| 209 | + // but skip constructors |
| 210 | + if (isPublic && isStatic == false && name.equals("<init>") == false) { |
| 211 | + inheritableAccess.add(method); |
| 212 | + } |
| 213 | + } |
| 214 | + } else if (inheritableAccess.contains(method)) { |
| 215 | + if (modulePredicate.test(moduleClass.module)) { |
| 216 | + accessibleImplementations.add(method); |
| 217 | + } |
| 218 | + } |
| 219 | + return mv; |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments