/
GroovyCallSiteSelector.java
248 lines (232 loc) · 10 KB
/
GroovyCallSiteSelector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* The MIT License
*
* Copyright 2014 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.scriptsecurity.sandbox.groovy;
import com.google.common.primitives.Primitives;
import groovy.lang.GString;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.apache.commons.lang.ClassUtils;
/**
* Assists in determination of which method or other JVM element is actually about to be called by Groovy.
* Most of this just duplicates what {@link java.lang.invoke.MethodHandles.Lookup} and {@link java.lang.invoke.MethodHandle#asType} do,
* but {@link org.codehaus.groovy.vmplugin.v7.TypeTransformers} (Groovy 2) shows that there are Groovy-specific complications.
* Comments in https://github.com/kohsuke/groovy-sandbox/issues/7 note that it would be great for the sandbox itself to just tell us what the call site is so we would not have to guess.
*/
class GroovyCallSiteSelector {
private static boolean matches(@Nonnull Class<?>[] parameterTypes, @Nonnull Object[] parameters, boolean varargs) {
if (varargs) {
parameters = parametersForVarargs(parameterTypes, parameters);
}
if (parameters.length != parameterTypes.length) {
return false;
}
for (int i = 0; i < parameterTypes.length; i++) {
if (parameters[i] == null) {
if (parameterTypes[i].isPrimitive()) {
return false;
} else {
// A null argument is assignable to any reference-typed parameter.
continue;
}
}
if (parameterTypes[i].isInstance(parameters[i])) {
// OK, this parameter matches.
continue;
}
if (
parameterTypes[i].isPrimitive()
&& parameters[i] != null
&& isInstancePrimitive(ClassUtils.primitiveToWrapper(parameterTypes[i]), parameters[i])
) {
// Groovy passes primitive values as objects (for example, passes 0 as Integer(0))
// The prior test fails as int.class.isInstance(new Integer(0)) returns false.
continue;
}
// TODO what about a primitive parameter type and a wrapped parameter?
if (parameterTypes[i] == String.class && parameters[i] instanceof GString) {
// Cf. SandboxInterceptorTest and class Javadoc.
continue;
}
// Mismatch.
return false;
}
return true;
}
/**
* Translates a method parameter list with varargs possibly spliced into the end into the actual parameters to be passed to the JVM call.
*/
private static Object[] parametersForVarargs(Class<?>[] parameterTypes, Object[] parameters) {
int fixedLen = parameterTypes.length - 1;
Class<?> componentType = parameterTypes[fixedLen].getComponentType();
assert componentType != null;
int arrayLength = parameters.length - fixedLen;
if (arrayLength >= 0) {
if (arrayLength == 1 && parameterTypes[fixedLen].isInstance(parameters[fixedLen])) {
// not a varargs call
return parameters;
} else {
Object array = Array.newInstance(componentType, arrayLength);
for (int i = 0; i < arrayLength; i++) {
Array.set(array, i, parameters[fixedLen + i]);
}
Object[] parameters2 = new Object[fixedLen + 1];
System.arraycopy(parameters, 0, parameters2, 0, fixedLen);
parameters2[fixedLen] = array;
return parameters2;
}
} else {
return parameters;
}
}
/**
* {@link Class#isInstance} extended to handle some important cases of primitive types.
*/
private static boolean isInstancePrimitive(@Nonnull Class<?> type, @Nonnull Object instance) {
if (type.isInstance(instance)) {
return true;
}
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.2
if (instance instanceof Number) {
if (type == Long.class && instance instanceof Integer) {
return true; // widening
}
if (type == Integer.class && instance instanceof Long) {
Long n = (Long) instance;
if (n >= Integer.MIN_VALUE && n <= Integer.MAX_VALUE) {
return true; // safe narrowing
}
}
// TODO etc. for other conversions if they ever come up
}
return false;
}
/**
* Looks up the most general possible definition of a given method call.
* Preferentially searches for compatible definitions in supertypes.
* @param receiver an actual receiver object
* @param method the method name
* @param args a set of actual arguments
*/
public static @CheckForNull Method method(@Nonnull Object receiver, @Nonnull String method, @Nonnull Object[] args) {
for (Class<?> c : types(receiver)) {
Method candidate = findMatchingMethod(c, method, args);
if (candidate != null) {
return candidate;
}
}
if (receiver instanceof GString) { // cf. GString.invokeMethod
Method candidate = findMatchingMethod(String.class, method, args);
if (candidate != null) {
return candidate;
}
}
return null;
}
public static @CheckForNull Constructor<?> constructor(@Nonnull Class<?> receiver, @Nonnull Object[] args) {
for (Constructor<?> c : receiver.getDeclaredConstructors()) {
if (matches(c.getParameterTypes(), args, c.isVarArgs())) {
return c;
}
}
return null;
}
public static @CheckForNull Method staticMethod(@Nonnull Class<?> receiver, @Nonnull String method, @Nonnull Object[] args) {
// TODO should we check for inherited static calls?
return findMatchingMethod(receiver, method, args);
}
private static Method findMatchingMethod(Class<?> receiver, String method, Object[] args) {
Method candidate = null;
for (Method m : receiver.getDeclaredMethods()) {
if (m.getName().equals(method) && (matches(m.getParameterTypes(), args, m.isVarArgs()))) {
if (candidate == null || isMoreSpecific(m, candidate)) {
candidate = m;
}
}
}
return candidate;
}
public static @CheckForNull Field field(@Nonnull Object receiver, @Nonnull String field) {
for (Class<?> c : types(receiver)) {
for (Field f : c.getDeclaredFields()) {
if (f.getName().equals(field)) {
return f;
}
}
}
return null;
}
public static @CheckForNull Field staticField(@Nonnull Class<?> receiver, @Nonnull String field) {
for (Field f : receiver.getDeclaredFields()) {
if (f.getName().equals(field)) {
return f;
}
}
return null;
}
private static Iterable<Class<?>> types(@Nonnull Object o) {
Set<Class<?>> types = new LinkedHashSet<Class<?>>();
visitTypes(types, o.getClass());
return types;
}
private static void visitTypes(@Nonnull Set<Class<?>> types, @Nonnull Class<?> c) {
Class<?> s = c.getSuperclass();
if (s != null) {
visitTypes(types, s);
}
for (Class<?> i : c.getInterfaces()) {
visitTypes(types, i);
}
// Visit supertypes first.
types.add(c);
}
// TODO nowhere close to implementing http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2.5
private static boolean isMoreSpecific(Method more, Method less) {
Class<?>[] moreParams = more.getParameterTypes();
Class<?>[] lessParams = less.getParameterTypes();
assert moreParams.length == lessParams.length;
for (int i = 0; i < moreParams.length; i++) {
Class<?> moreParam = Primitives.wrap(moreParams[i]);
Class<?> lessParam = Primitives.wrap(lessParams[i]);
if (moreParam.isAssignableFrom(lessParam)) {
return false;
} else if (lessParam.isAssignableFrom(moreParam)) {
return true;
}
if (moreParam == Long.class && lessParam == Integer.class) {
return false;
} else if (moreParam == Integer.class && lessParam == Long.class) {
return true;
}
}
// Incomparable. Arbitrarily pick one of them.
return more.toString().compareTo(less.toString()) > 0;
}
private GroovyCallSiteSelector() {}
}