<commit_before>import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessControlException;
import java.security.Permission;
import java.util.PropertyPermission;
import java.util.Scanner;

public class JavaSafeExecutor {
    private static ThreadDeath TLE = new ThreadDeath();
    private static int INVOCATION_ERROR_CODE = -1000;
    private static int ACCESS_ERROR_CODE = -1001;
    private static int NO_ENTRY_POINT_ERROR_CODE = -1002;
    private static int PROGRAM_ERROR_CODE = -1;
    private static ShockerThread shockerThread;
    private static SubmissionThread submissionThread;
    private static boolean _safeBlock = false;

    static {
        new Scanner(new ByteArrayInputStream(new byte[128])).close(); // Load locale
    }

    public static void main(String[] argv) throws MalformedURLException, ClassNotFoundException, UnsupportedEncodingException {
        String path = argv[0];
        String classname = argv[1];
        int TL = Integer.parseInt(argv[2]);

        System.setOut(new UnsafePrintStream(new FileOutputStream(FileDescriptor.out)));

        URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{new File(path).toURI().toURL()});
        Class program = classLoader.loadClass(classname);
        submissionThread = new SubmissionThread(program);

        // Count runtime loading as part of time used
        // Note that time here might be negative if RT loading time was greater than TL
        // Oh well.
        TL -= ManagementFactory.getRuntimeMXBean().getUptime();

        shockerThread = new ShockerThread(TL, submissionThread);
        System.setSecurityManager(new SubmissionSecurityManager());
        shockerThread.start();
        submissionThread.start();

        try {
            submissionThread.join();
        } catch (InterruptedException ignored) {
        }
        _safeBlock = true;
        shockerThread.stop();

        // UnsafePrintStream buffers
        System.out.flush();

        long totalProgramTime = ManagementFactory.getRuntimeMXBean().getUptime();
        boolean tle = submissionThread.tle;

        long mem = 0;
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/self/status")));
            for (String line; (line = in.readLine()) != null; ) {
                if (line.startsWith("VmHWM:")) {
                    String[] data = line.split("\\s+");
                    mem = Integer.parseInt(data[1]);
                }
            }
        } catch (Exception ignored) {
        }
        boolean mle = submissionThread.mle;
        int error = submissionThread.error;

        System.err.println();
        System.err.printf("%d %d %d %d %d\n", totalProgramTime, tle ? 1 : 0, mem, mle ? 1 : 0, error);
    }

    public static class SubmissionSecurityManager extends SecurityManager {
        @Override
        public void checkPermission(Permission perm) {
            if (perm instanceof RuntimePermission) {
                if (perm.getName().equals("writeFileDescriptor") || perm.getName().equals("readFileDescriptor"))
                    return;
            }
            if (perm instanceof PropertyPermission) {
                if (perm.getActions().contains("write"))
                    throw new AccessControlException(perm.getClass() + " - " + perm.getName() + ": " + perm.getActions(), perm);
                return;
            }
            if (!_safeBlock) {
                throw new AccessControlException(perm.getClass() + " - " + perm.getName() + ": " + perm.getActions(), perm);
            }
        }
    }

    public static class ShockerThread extends Thread {
        private final long timelimit;
        private final Thread target;

        public ShockerThread(long timelimit, Thread target) {
            super("Grader-TL-Thread");
            this.timelimit = timelimit;
            this.target = target;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(timelimit);
                _safeBlock = true;
                target.stop(TLE);
            } catch (InterruptedException ignored) {
            }
        }
    }

    public static class SubmissionThread extends Thread {
        private final Class submission;
        private boolean tle = false;
        private boolean mle = false;
        private int error = 0;

        public SubmissionThread(Class process) {
            super(null, null, "Submission-Grading-Thread(" + process.getSimpleName() + ")", 8000000);
            this.submission = process;
        }

        @Override
        public void run() {
            Method handle;
            try {
                handle = submission.getMethod("main", String[].class);
                if (!Modifier.isStatic(handle.getModifiers())) System.exit(-10);
                try {
                    handle.invoke(null, new Object[]{new String[0]});
                } catch (InvocationTargetException e) {
                    if (e.getCause() == TLE) {
                        tle = true;
                        return;
                    } else if (e.getCause() instanceof OutOfMemoryError) {
                        mle = true;
                        return;
                    } else {
                        e.getCause().printStackTrace();
                        error = INVOCATION_ERROR_CODE;
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    error = ACCESS_ERROR_CODE;
                } catch (Throwable throwable) {
                    error = PROGRAM_ERROR_CODE;
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                error = NO_ENTRY_POINT_ERROR_CODE;
            }
            _safeBlock = true;
            shockerThread.stop();
        }
    }<commit_msg>Prevent throw new OutOfMemoryError from being counted as MLE<commit_after>import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessControlException;
import java.security.Permission;
import java.util.PropertyPermission;
import java.util.Scanner;

public class JavaSafeExecutor {
    private static ThreadDeath TLE = new ThreadDeath();
    private static int INVOCATION_ERROR_CODE = -1000;
    private static int ACCESS_ERROR_CODE = -1001;
    private static int NO_ENTRY_POINT_ERROR_CODE = -1002;
    private static int PROGRAM_ERROR_CODE = -1;
    private static ShockerThread shockerThread;
    private static SubmissionThread submissionThread;
    private static boolean _safeBlock = false;

    static {
        new Scanner(new ByteArrayInputStream(new byte[128])).close(); // Load locale
    }

    public static void main(String[] argv) throws MalformedURLException, ClassNotFoundException, UnsupportedEncodingException {
        String path = argv[0];
        String classname = argv[1];
        int TL = Integer.parseInt(argv[2]);

        System.setOut(new UnsafePrintStream(new FileOutputStream(FileDescriptor.out)));

        URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{new File(path).toURI().toURL()});
        Class program = classLoader.loadClass(classname);
        submissionThread = new SubmissionThread(program);

        // Count runtime loading as part of time used
        // Note that time here might be negative if RT loading time was greater than TL
        // Oh well.
        TL -= ManagementFactory.getRuntimeMXBean().getUptime();

        shockerThread = new ShockerThread(TL, submissionThread);
        System.setSecurityManager(new SubmissionSecurityManager());
        shockerThread.start();
        submissionThread.start();

        try {
            submissionThread.join();
        } catch (InterruptedException ignored) {
        }
        _safeBlock = true;
        shockerThread.stop();

        // UnsafePrintStream buffers
        System.out.flush();

        long totalProgramTime = ManagementFactory.getRuntimeMXBean().getUptime();
        boolean tle = submissionThread.tle;

        long mem = 0;
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/self/status")));
            for (String line; (line = in.readLine()) != null; ) {
                if (line.startsWith("VmHWM:")) {
                    String[] data = line.split("\\s+");
                    mem = Integer.parseInt(data[1]);
                }
            }
        } catch (Exception ignored) {
        }
        boolean mle = submissionThread.mle;
        int error = submissionThread.error;

        System.err.println();
        System.err.printf("%d %d %d %d %d\n", totalProgramTime, tle ? 1 : 0, mem, mle ? 1 : 0, error);
    }

    public static class SubmissionSecurityManager extends SecurityManager {
        @Override
        public void checkPermission(Permission perm) {
            if (perm instanceof RuntimePermission) {
                if (perm.getName().equals("writeFileDescriptor") || perm.getName().equals("readFileDescriptor"))
                    return;
            }
            if (perm instanceof PropertyPermission) {
                if (perm.getActions().contains("write"))
                    throw new AccessControlException(perm.getClass() + " - " + perm.getName() + ": " + perm.getActions(), perm);
                return;
            }
            if (!_safeBlock) {
                throw new AccessControlException(perm.getClass() + " - " + perm.getName() + ": " + perm.getActions(), perm);
            }
        }
    }

    public static class ShockerThread extends Thread {
        private final long timelimit;
        private final Thread target;

        public ShockerThread(long timelimit, Thread target) {
            super("Grader-TL-Thread");
            this.timelimit = timelimit;
            this.target = target;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(timelimit);
                _safeBlock = true;
                target.stop(TLE);
            } catch (InterruptedException ignored) {
            }
        }
    }

    public static class SubmissionThread extends Thread {
        private final Class submission;
        private boolean tle = false;
        private boolean mle = false;
        private int error = 0;

        public SubmissionThread(Class process) {
            super(null, null, "Submission-Grading-Thread(" + process.getSimpleName() + ")", 8000000);
            this.submission = process;
        }

        @Override
        public void run() {
            Method handle;
            try {
                handle = submission.getMethod("main", String[].class);
                if (!Modifier.isStatic(handle.getModifiers())) System.exit(-10);
                try {
                    handle.invoke(null, new Object[]{new String[0]});
                } catch (InvocationTargetException e) {
                    if (e.getCause() == TLE) {
                        tle = true;
                        return;
                    } else if (e.getCause() instanceof OutOfMemoryError) {
                        // Prevent throw new OutOfMemoryError from being counted as MLE
                        if(e.getCause().getStackTrace()[0].getClassName().equals(submission.getSimpleName())) {
                            error = PROGRAM_ERROR_CODE;
                            return;
                        }
                        mle = true;
                        return;
                    } else {
                        e.getCause().printStackTrace();
                        error = INVOCATION_ERROR_CODE;
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    error = ACCESS_ERROR_CODE;
                } catch (Throwable throwable) {
                    error = PROGRAM_ERROR_CODE;
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                error = NO_ENTRY_POINT_ERROR_CODE;
            }
            _safeBlock = true;
            shockerThread.stop();
        }
    }
{'fraction_non_alphanumeric': 0.09624990089590106, 'fraction_numerical': 0.005787679378419092, 'mean_word_length': 1.9071214565568104, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1669021', 'n_tokens_mistral': 3218, 'n_tokens_neox': 3071, 'n_words': 763}