File: InstrClassLoader.java

package info (click to toggle)
emma-coverage 2.0.5312%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,000 kB
  • ctags: 3,667
  • sloc: java: 23,109; xml: 414; makefile: 22
file content (462 lines) | stat: -rw-r--r-- 17,561 bytes parent folder | download
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
 * 
 * This program and the accompanying materials are made available under
 * the terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * $Id: InstrClassLoader.java,v 1.1.1.1.2.2 2004/07/16 23:32:03 vlad_r Exp $
 */
package com.vladium.emma.rt;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.CodeSource;
import java.util.Map;

import com.vladium.logging.Logger;
import com.vladium.util.ByteArrayOStream;
import com.vladium.util.asserts.$assert;
import com.vladium.emma.IAppConstants;
import com.vladium.emma.filter.IInclExclFilter;

// ----------------------------------------------------------------------------
/**
 * @author Vlad Roubtsov, (C) 2003
 */
public
final class InstrClassLoader extends URLClassLoader
{
    // public: ................................................................
        
    // TODO: proper security [use PrivilegedAction as needed]
    // TODO: [see above as well] need to keep track of res URLs to support [path] exclusion patterns
    // TODO: improve error handling so it is clear when errors come from buggy instrumentation
    
    
    public static final String PROPERTY_FORCED_DELEGATION_FILTER  = "clsload.forced_delegation_filter";
    public static final String PROPERTY_THROUGH_DELEGATION_FILTER  = "clsload.through_delegation_filter";
        

    public InstrClassLoader (final ClassLoader parent, final File [] classpath,
                             final IInclExclFilter forcedDelegationFilter,
                             final IInclExclFilter throughDelegationFilter,
                             final IClassLoadHook hook, final Map cache)
        throws MalformedURLException
    {
        // setting ClassLoader.parent to null disables the standard delegation
        // behavior in a few places, including URLClassLoader.getResource():
        
        super (filesToURLs (classpath), null);
        
        // TODO: arg validation
        
        m_hook = hook;
        m_cache = cache; // can be null
        
        m_forcedDelegationFilter = forcedDelegationFilter;
        m_throughDelegationFilter = throughDelegationFilter;
         
        m_parent = parent;        
        m_bufPool = new PoolEntry [BAOS_POOL_SIZE];
        
        m_log = Logger.getLogger ();
    }
    
    /**
     * Overrides java.lang.ClassLoader.loadClass() to change the usual parent-child
     * delegation rules just enough to be able to 'replace' the parent loader. This
     * also has the effect of detecting 'system' classes without doing any class
     * name-based matching.
     */
    public synchronized final Class loadClass (final String name, final boolean resolve)
        throws ClassNotFoundException
    {
        final boolean trace1 = m_log.atTRACE1 ();
        
        if (trace1) m_log.trace1 ("loadClass",  "(" + name + ", " + resolve + "): nest level " + m_nestLevel);
        
        Class c = null;
        
        // first, check if this class has already been defined by this classloader
        // instance:
        c = findLoadedClass (name);
        
        if (c == null)
        {
            Class parentsVersion = null;
            if (m_parent != null)
            {
                try
                {
                    parentsVersion = m_parent.loadClass (name); // note: it is important that this does not init the class
                    
                    if ((parentsVersion.getClassLoader () != m_parent) ||
                        ((m_forcedDelegationFilter == null) || m_forcedDelegationFilter.included (name)))
                    {
                        // (a) m_parent itself decided to delegate: use parent's version
                        // (b) the class was on the forced delegation list: use parent's version
                        c = parentsVersion;
                        if (trace1) m_log.trace1 ("loadClass", "using parent's version for [" + name + "]");
                    }
                }
                catch (ClassNotFoundException cnfe)
                {
                    // if the class was on the forced delegation list, error out: 
                    if ((m_forcedDelegationFilter == null) || m_forcedDelegationFilter.included (name))
                        throw cnfe;
                }
            }
            
            if (c == null)
            {
                try
                {
                    // either (a) m_parent was null or (b) it could not load 'c'
                    // or (c) it will define 'c' itself if allowed to. In any
                    // of these cases I attempt to define my own version:
                    c = findClass (name);
                }
                catch (ClassNotFoundException cnfe)
                {
                    // this is a difficult design point unless I resurrect the -lx option
                    // and document how to use it [which will confuse most users anyway]
                    
                    // another alternative would be to see if parent's version is included by
                    // the filter and print a warning; still, it does not help with JAXP etc 
                    
                    if (parentsVersion != null)
                    {
                        final boolean delegate = (m_throughDelegationFilter == null) || m_throughDelegationFilter.included (name); 
                        
                        if (delegate)
                        {
                            c = parentsVersion;
                            if (trace1) m_log.trace1 ("loadClass", "[delegation filter] using parent's version for [" + name + "]");
                        }
                        else
                            throw cnfe;
                    }
                    else
                      throw cnfe;
                }
            }
        }
        
        if (c == null) throw new ClassNotFoundException (name);
        
        if (resolve) resolveClass (c); // this never happens in J2SE JVMs
        return c;
    }
    
    // TODO: remove this in the release build
    
    public final URL getResource (final String name)
    {
        final boolean trace1 = m_log.atTRACE1 ();
        
        if (trace1) m_log.trace1 ("getResource",  "(" + name + "): nest level " + m_nestLevel);
        
        final URL result = super.getResource (name);
        if (trace1 && (result != null)) m_log.trace1 ("loadClass",  "[" + name + "] found in " + result);
        
        return result;
    }
    
    // protected: .............................................................
    
    
    protected final Class findClass (final String name)
        throws ClassNotFoundException
    {
        final boolean trace1 = m_log.atTRACE1 ();
        
        if (trace1) m_log.trace1 ("findClass",  "(" + name + "): nest level " + m_nestLevel);
        
        final boolean useClassCache = (m_cache != null);
        final ClassPathCacheEntry entry = useClassCache ? (ClassPathCacheEntry) m_cache.remove (name) : null;
        
        byte [] bytes;
        int length;
        URL classURL = null;
            
        if (entry != null) // cache hit
        {
            ++ m_cacheHits;
            
            // used cached class def bytes, no need to repeat disk I/O:
            
            try
            {
                classURL = new URL (entry.m_srcURL);
            }
            catch (MalformedURLException murle) // this should never happen
            {
                if ($assert.ENABLED)
                {
                    murle.printStackTrace (System.out);
                }
            }
            
            PoolEntry buf = null;
            try
            {
                buf = acquirePoolEntry ();
                final ByteArrayOStream baos = buf.m_baos; // reset() has been called on this
                
                // the original class definition:
                bytes = entry.m_bytes;
                length = bytes.length;
                
                if ((m_hook != null) && m_hook.processClassDef (name, bytes, length, baos)) // note: this can overwrite 'bytes'
                {
                    // the instrumented class definition:
                    bytes = baos.getByteArray ();
                    length = baos.size ();
                    
                    if (trace1) m_log.trace1 ("findClass",  "defining [cached] instrumented [" + name + "] {" + length + " bytes }");
                }
                else
                {
                    if (trace1) m_log.trace1 ("findClass",  "defining [cached] [" + name + "] {" + length + " bytes }");
                }
                
                return defineClass (name, bytes, length, classURL);
            }
            catch (IOException ioe)
            {
                throw new ClassNotFoundException (name);
            }
            finally
            {
                if (buf != null) releasePoolEntry (buf);
            }
        }
        else // cache miss
        {
            if (useClassCache) ++ m_cacheMisses;
            
            // .class files are not guaranteed to be loadable as resources;
            // but if Sun's code does it...
            final String classResource = name.replace ('.', '/') + ".class";
            
            // even thought normal delegation is disabled, this will find bootstrap classes:
            classURL = getResource (classResource); // important to hook into URLClassLoader's overload of this so that Class-Path manifest attributes are processed etc
            
            if (trace1 && (classURL != null)) m_log.trace1 ("findClass",  "[" + name + "] found in " + classURL);
            
            if (classURL == null)
                throw new ClassNotFoundException (name);
            else
            {
                InputStream in = null;
                PoolEntry buf = null;
                try
                {
                    in = classURL.openStream ();
                    
                    buf = acquirePoolEntry ();
                    final ByteArrayOStream baos = buf.m_baos; // reset() has been called on this
                    
                    readFully (in, baos, buf.m_buf);
                    in.close (); // don't keep the file handle across reentrant calls
                    in = null;
                    
                    // the original class definition:
                    bytes = baos.getByteArray ();
                    length = baos.size ();
                    
                    baos.reset (); // reuse this for processClassDef below
                    
                    if ((m_hook != null) && m_hook.processClassDef (name, bytes, length, baos)) // note: this can overwrite 'bytes'
                    {
                        // the instrumented class definition:
                        bytes = baos.getByteArray ();
                        length = baos.size ();
                        
                        if (trace1) m_log.trace1 ("findClass",  "defining instrumented [" + name + "] {" + length + " bytes }");
                    }
                    else
                    {
                        if (trace1) m_log.trace1 ("findClass",  "defining [" + name + "] {" + length + " bytes }");
                    }
                    
                    return defineClass (name, bytes, length, classURL);
                }
                catch (IOException ioe)
                {
                    throw new ClassNotFoundException (name);
                }
                finally
                {
                    if (buf != null) releasePoolEntry (buf);
                    if (in != null) try { in.close (); } catch (Exception ignore) {}
                }
            }
        }
    }
    
    public void debugDump (final PrintWriter out)
    {
        if (out != null)
        {
            out.println (this + ": " + m_cacheHits + " class cache hits, " + m_cacheMisses + " misses");
        }
    }

    // package: ...............................................................
    
    // private: ...............................................................
    
    
    private static final class PoolEntry
    {
        PoolEntry (final int baosCapacity, final int bufSize)
        {
            m_baos = new ByteArrayOStream (baosCapacity);
            m_buf = new byte [bufSize];
        }
        
        void trim (final int baosCapacity, final int baosMaxCapacity)
        {
            if (m_baos.capacity () > baosMaxCapacity)
            {
                m_baos = new ByteArrayOStream (baosCapacity);
            }
        }
        
        ByteArrayOStream m_baos;
        final byte [] m_buf;
        
    } // end of nested class
    
    
    /*
     * 'srcURL' may be null
     */
    private Class defineClass (final String className, final byte [] bytes, final int length, final URL srcURL)
    {
        // support ProtectionDomains with non-null class source URLs:
        // [however, disable anything related to sealing or signing]
        
        final CodeSource csrc = new CodeSource (srcURL, (java.security.CodeSigner[])null);
        
        // allow getPackage() to return non-null on the class we are about to
        // define (however, don't bother emulating the original manifest info since
        // we may be altering manifest content anyway):
        
        final int lastDot = className.lastIndexOf ('.');
        if (lastDot >= 0)
        {
            final String packageName = className.substring (0, lastDot);
            
            final Package pkg = getPackage (packageName);
            if (pkg == null)
            {
                definePackage (packageName,
                               IAppConstants.APP_NAME, IAppConstants.APP_VERSION, IAppConstants.APP_COPYRIGHT,
                               IAppConstants.APP_NAME, IAppConstants.APP_VERSION, IAppConstants.APP_COPYRIGHT,
                               srcURL);
            }
        }
        
        return defineClass (className, bytes, 0, length, csrc);
    }
    
    
    private static URL [] filesToURLs (final File [] classpath)
        throws MalformedURLException
    {
        if ((classpath == null) || (classpath.length == 0))
            return EMPTY_URL_ARRAY;
            
        final URL [] result = new URL [classpath.length];
        
        for (int f = 0; f < result.length ; ++ f)
        {
            result [f] = classpath [f].toURL (); // note: this does proper dir encoding
        }
        
        return result;
    }
    
    /**
     * Reads the entire contents of a given stream into a flat byte array.
     */
    private static void readFully (final InputStream in, final ByteArrayOStream out, final byte [] buf)
        throws IOException
    {
        for (int read; (read = in.read (buf)) >= 0; )
        {
            out.write (buf, 0, read);
        }
    }
    
    /*
     * not MT-safe; must be called from loadClass() only
     */
    private PoolEntry acquirePoolEntry ()
    {
        PoolEntry result;
        
        if (m_nestLevel >= BAOS_POOL_SIZE)
        {
            result = new PoolEntry (BAOS_INIT_SIZE, BAOS_INIT_SIZE);
        }
        else
        {
            result = m_bufPool [m_nestLevel];
            if (result == null)
            {
                result = new PoolEntry (BAOS_INIT_SIZE, BAOS_INIT_SIZE);
                m_bufPool [m_nestLevel] = result;
            }
            else
            {
                result.m_baos.reset ();
            }
        }
        
        ++ m_nestLevel;
            
        return result;
    }
    
    /*
     * not MT-safe; must be called from loadClass() only
     */
    private void releasePoolEntry (final PoolEntry buf)
    {
        if (-- m_nestLevel < BAOS_POOL_SIZE)
        {
            buf.trim (BAOS_INIT_SIZE, BAOS_MAX_SIZE);
        }
    }
    
    
    private final ClassLoader m_parent;    
    
    private final IInclExclFilter m_forcedDelegationFilter;
    private final IInclExclFilter m_throughDelegationFilter;
    
    private final Map /* classJavaName:String -> ClassPathCacheEntry */ m_cache; // can be null
    private final IClassLoadHook m_hook;
    private final PoolEntry [] m_bufPool;
    
    private final Logger m_log; // a loader instance is used concurrently but cached its log config at construction time
    
    private int m_nestLevel;
    
    private int m_cacheHits, m_cacheMisses;
    
    private static final int BAOS_INIT_SIZE = 32 * 1024;
    private static final int BAOS_MAX_SIZE = 1024 * 1024;
    private static final int BAOS_POOL_SIZE = 8;
    private static final URL [] EMPTY_URL_ARRAY = new URL [0];
    
} // end of class
// ----------------------------------------------------------------------------