-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathLDEV6240.cfc
More file actions
269 lines (231 loc) · 10.7 KB
/
Copy pathLDEV6240.cfc
File metadata and controls
269 lines (231 loc) · 10.7 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
component extends="org.lucee.cfml.test.LuceeTestCase" labels="java,classloader" {
function beforeAll() {
variables.tempDir = getTempDirectory() & "LDEV6240/";
if ( directoryExists( variables.tempDir ) ) directoryDelete( variables.tempDir, true );
directoryCreate( variables.tempDir );
directoryCreate( variables.tempDir & "src/" );
directoryCreate( variables.tempDir & "classes1/" );
directoryCreate( variables.tempDir & "classes2/" );
// simple class with an interface, simulates an SPI provider
var interfaceSrc = 'package ldev6240;
public interface Greeter {
String greet();
}';
var implSrc = 'package ldev6240;
public class HelloGreeter implements Greeter {
public String greet() { return "hello"; }
}';
// a consumer that takes the interface as a method arg
var consumerSrc = 'package ldev6240;
public class GreeterConsumer {
public static String consume( Greeter g ) { return g.greet(); }
}';
directoryCreate( variables.tempDir & "src/ldev6240/" );
fileWrite( variables.tempDir & "src/ldev6240/Greeter.java", interfaceSrc );
fileWrite( variables.tempDir & "src/ldev6240/HelloGreeter.java", implSrc );
fileWrite( variables.tempDir & "src/ldev6240/GreeterConsumer.java", consumerSrc );
var javac = createObject( "java", "javax.tools.ToolProvider" ).getSystemJavaCompiler();
if ( isNull( javac ) )
throw( message="JDK required - javac not available", type="application" );
var result = javac.run(
javaCast( "null", "" ), javaCast( "null", "" ), javaCast( "null", "" ),
[
"-d", variables.tempDir & "classes1",
variables.tempDir & "src/ldev6240/Greeter.java",
variables.tempDir & "src/ldev6240/HelloGreeter.java",
variables.tempDir & "src/ldev6240/GreeterConsumer.java"
]
);
if ( result != 0 )
throw( message="javac compilation failed with exit code #result#", type="application" );
// copy to classes2 - same bytes, different classloader will make them different types
directoryCreate( variables.tempDir & "classes2/ldev6240/" );
for ( var f in [ "Greeter.class", "HelloGreeter.class", "GreeterConsumer.class" ] ) {
fileCopy(
variables.tempDir & "classes1/ldev6240/" & f,
variables.tempDir & "classes2/ldev6240/" & f
);
}
}
private function createIsolatedClassLoader( required string classDir ) {
var file = createObject( "java", "java.io.File" ).init( arguments.classDir );
var urlArray = createObject( "java", "java.lang.reflect.Array" )
.newInstance( createObject( "java", "java.net.URL" ).getClass(), 1 );
urlArray[ 1 ] = file.toURI().toURL();
// null parent forces independent loading - each CL gets its own copy of every class
return createObject( "java", "java.net.URLClassLoader" )
.init( urlArray, javaCast( "null", "" ) );
}
private function getEnvClassLoader() {
return createObject( "java", "lucee.runtime.osgi.EnvClassLoader" )
.getInstance( javaCast( "null", "" ) );
}
private function getCallerCache( required envCL ) {
var field = arguments.envCL.getClass().getDeclaredField( "callerCache" );
field.setAccessible( true );
return field.get( arguments.envCL );
}
function run( testResults, testBox ) {
describe( "LDEV-6240: EnvClassLoader stale class resolution", function() {
it( "findLoadedClass prevents EnvClassLoader from seeing classloader changes", function() {
// This is the core bug. EnvClassLoader.loadClass() calls findLoadedClass()
// which is a JVM-level cache that cannot be cleared. Once a class is loaded
// through EnvClassLoader, it's stuck there forever, even if the underlying
// classloader that provided it has been flushed and replaced.
//
// We can't test this directly on the live EnvClassLoader (it would pollute
// the running server's state), but we can demonstrate the JVM behaviour
// with a fresh URLClassLoader that mimics EnvClassLoader's pattern.
var cl1 = createIsolatedClassLoader( variables.tempDir & "classes1/" );
var cl2 = createIsolatedClassLoader( variables.tempDir & "classes2/" );
try {
// create a wrapper classloader that delegates to cl1 (simulates EnvClassLoader)
var wrapperClass = createObject( "java", "java.net.URLClassLoader" );
var emptyUrls = createObject( "java", "java.lang.reflect.Array" )
.newInstance( createObject( "java", "java.net.URL" ).getClass(), 0 );
var wrapper = wrapperClass.init( emptyUrls, cl1 );
// first load: goes through cl1
var class1 = wrapper.loadClass( "ldev6240.Greeter" );
expect( class1.getName() ).toBe( "ldev6240.Greeter" );
// load same class directly from cl2
var class2 = cl2.loadClass( "ldev6240.Greeter" );
// same name, different classloader, different identity
expect( class1.getName() ).toBe( class2.getName() );
expect( class1.isAssignableFrom( class2 ) ).toBeFalse(
"classes from different classloaders should not be assignable"
);
// wrapper.loadClass() will return the SAME class1 due to findLoadedClass
// even though we might want it to return class2 after a "flush"
var classAgain = wrapper.loadClass( "ldev6240.Greeter" );
expect( classAgain.equals( class1 ) ).toBeTrue(
"findLoadedClass returns the original - JVM cache can't be cleared"
);
}
finally {
cl1.close();
cl2.close();
}
});
it( "cross-classloader objects fail isAssignableFrom even with identical bytes", function() {
// Fundamental JVM behaviour: class identity = name + classloader.
// Same .class bytes from different classloaders are NOT the same type.
var cl1 = createIsolatedClassLoader( variables.tempDir & "classes1/" );
var cl2 = createIsolatedClassLoader( variables.tempDir & "classes1/" ); // SAME dir
try {
var iface1 = cl1.loadClass( "ldev6240.Greeter" );
var iface2 = cl2.loadClass( "ldev6240.Greeter" );
expect( iface1.getName() ).toBe( iface2.getName() );
expect( iface1.getClassLoader().equals( iface2.getClassLoader() ) ).toBeFalse();
expect( iface1.isAssignableFrom( iface2 ) ).toBeFalse(
"same bytes, different classloaders = different types"
);
// impl from cl2 does NOT implement interface from cl1
var impl2 = cl2.loadClass( "ldev6240.HelloGreeter" );
expect( iface1.isAssignableFrom( impl2 ) ).toBeFalse(
"impl from cl2 is not a subtype of interface from cl1"
);
// but Lucee's name-based matching handles this (LDEV-6226 fix)
var Reflector = createObject( "java", "lucee.runtime.reflection.Reflector" );
expect( Reflector.isInstaneOf( impl2, iface1, false ) ).toBeTrue(
"name-based isInstaneOf works across classloaders"
);
}
finally {
cl1.close();
cl2.close();
}
});
it( "EnvClassLoader callerCache is populated and queryable", function() {
// Verify callerCache exists and is used. This cache is the secondary
// staleness vector (after findLoadedClass). Unlike findLoadedClass,
// callerCache CAN be cleared - that's the fix for LDEV-6240.
var envCL = getEnvClassLoader();
var callerCache = getCallerCache( envCL );
// load something to ensure cache is populated
envCL.loadClass( "java.util.HashMap" );
// callerCache should be non-empty (many classes loaded by now)
expect( callerCache.size() ).toBeGT( 0,
"callerCache should have entries after class loading"
);
});
it( "EnvClassLoader has no cache invalidation API", function() {
// Documents the gap: there is no way to clear EnvClassLoader's caches.
// After LDEV-6240 fix, update this test to verify clearCallerCache() works.
var envCL = getEnvClassLoader();
var hasClearMethod = false;
try {
envCL.getClass().getMethod( "clearCallerCache", [] );
hasClearMethod = true;
}
catch ( java.lang.NoSuchMethodException e ) {
// expected pre-fix
}
if ( !hasClearMethod ) {
// pre-fix: document the gap
var callerCache = getCallerCache( envCL );
var sizeBefore = callerCache.size();
expect( sizeBefore ).toBeGT( 0,
"callerCache has entries but no API to clear them"
);
}
else {
// post-fix: verify it actually works
var callerCache = getCallerCache( envCL );
envCL.loadClass( "java.util.TreeMap" ); // ensure something is cached
expect( callerCache.size() ).toBeGT( 0 );
envCL.clearCallerCache();
expect( callerCache.size() ).toBe( 0,
"clearCallerCache() should empty the cache"
);
// verify classes still resolve after clearing (re-populates cache)
var cls = envCL.loadClass( "java.util.TreeMap" );
expect( cls ).notToBeNull();
expect( cls.getName() ).toBe( "java.util.TreeMap" );
}
});
it( "simulates stale class after classloader swap", function() {
// End-to-end simulation of the bug:
// 1. CL-A loads Greeter interface and HelloGreeter impl
// 2. CL-A is "flushed" (closed, replaced by CL-B with same classes)
// 3. CL-B loads Greeter - this is a DIFFERENT Class object
// 4. The old HelloGreeter (from CL-A) is NOT assignable to new Greeter (from CL-B)
// 5. This is what causes ServiceLoader's "not a subtype" error
var clA = createIsolatedClassLoader( variables.tempDir & "classes1/" );
try {
// step 1: load from CL-A (the "before flush" state)
var greeterA = clA.loadClass( "ldev6240.Greeter" );
var implA = clA.loadClass( "ldev6240.HelloGreeter" );
var instanceA = implA.getConstructor( [] ).newInstance( [] );
// works fine - same classloader
expect( greeterA.isAssignableFrom( implA ) ).toBeTrue();
expect( instanceA.greet() ).toBe( "hello" );
}
finally {
clA.close(); // step 2: "flush" CL-A
}
// step 3: CL-B takes over (simulates new PhysicalClassLoader after flush)
var clB = createIsolatedClassLoader( variables.tempDir & "classes1/" );
try {
var greeterB = clB.loadClass( "ldev6240.Greeter" );
// step 4: the OLD instance from CL-A is not compatible with CL-B's type
expect( greeterB.isAssignableFrom( instanceA.getClass() ) ).toBeFalse(
"old instance's class is not assignable to new classloader's interface"
);
// step 5: this is what ServiceLoader sees - "not a subtype"
// ServiceLoader.load(greeterB, envCL) → envCL returns old impl → boom
// Lucee's name-based matching (LDEV-6226) handles this for
// Reflector/Clazz code paths. The remaining gap is ServiceLoader
// and other JDK code that uses isAssignableFrom directly.
var Reflector = createObject( "java", "lucee.runtime.reflection.Reflector" );
expect( Reflector.isInstaneOf( instanceA.getClass(), greeterB, false ) ).toBeTrue(
"name-based matching works even after classloader swap"
);
// Reflector.like() cross-classloader support is tested in LDEV6226.cfc
}
finally {
clB.close();
}
});
});
}
}