EsotericSoftware / reflectasm

High performance Java reflection
BSD 3-Clause "New" or "Revised" License
1.53k stars 222 forks source link

Threads may overwrite defineClassMethod in getDefineClassMethod method of AccessClassLoader #62

Closed detective0922 closed 6 years ago

detective0922 commented 6 years ago
    private static Method getDefineClassMethod() throws Exception {
        // DCL on volatile
        if (defineClassMethod==null) {
            synchronized(accessClassLoaders) {
                defineClassMethod = ClassLoader.class.getDeclaredMethod("defineClass", new Class[] {String.class, byte[].class, int.class,
                int.class, ProtectionDomain.class});
                try {
                    defineClassMethod.setAccessible(true);
                }
                catch (Exception ignored) {
                }
            }
        }
        return defineClassMethod;
    }

When Thread 1 assign a value to defineClassMethod and leave the critical section, then Thread 2 may assign a new value to defineClassMethod. Maybe should check defineClassMethod==null inside synchronized(accessClassLoaders) code block too to avoid this issue and complete the DCL.