All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.wolf480pl.mias4j.core.rewrite.SandboxAdapter Maven / Gradle / Ivy

Go to download

Method Interception and Sandboxing for Java - an alternative Java sandbox based on bytecode rewrite

There is a newer version: 0.1.1
Show newest version
/*
 * Copyright (c) 2014 Wolf480pl 
 * This program is licensed under the GNU Lesser General Public License.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package com.github.wolf480pl.mias4j.core.rewrite;

import static org.objectweb.asm.Type.getType;

import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.AnalyzerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.wolf480pl.mias4j.core.InvocationType;
import com.github.wolf480pl.mias4j.core.runtime.ArgumentPack;
import com.github.wolf480pl.mias4j.core.runtime.Bootstraps;
import com.github.wolf480pl.mias4j.util.SequenceMethodVisitor;
import com.github.wolf480pl.mias4j.util.WrappedCheckedException;

public class SandboxAdapter extends ClassVisitor {
    public static final String INIT = "";
    protected static final Logger LOG = LoggerFactory.getLogger(SandboxAdapter.class);

    private final RewritePolicy policy;
    private Type clazz;

    public SandboxAdapter(ClassVisitor cv) {
        this(cv, BlindPolicy.ALWAYS_INTERCEPT);
    }

    public SandboxAdapter(ClassVisitor cv, RewritePolicy policy) {
        super(Opcodes.ASM5, cv);
        this.policy = policy;
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        this.clazz = Type.getObjectType(name);
        super.visit(version, access, name, signature, superName, interfaces);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        MethodAdapter ma = new MethodAdapter(cv.visitMethod(access, name, desc, signature, exceptions), policy, clazz, name.equals(INIT));
        AnalyzerAdapter analyzer = new AnalyzerAdapter(clazz.getInternalName(), access, name, desc, ma);
        ma.setAnalyzer(analyzer);
        return analyzer;
    }

    public static class MethodAdapter extends SequenceMethodVisitor {
        public static final String WRAPINVOKE_NAME = Bootstraps.WRAPINVOKE_NAME;
        public static final String WRAPINVOKE_DESC = Type.getMethodDescriptor(getType(CallSite.class), getType(MethodHandles.Lookup.class), getType(String.class), getType(MethodType.class),
                Type.INT_TYPE, getType(String.class), getType(MethodType.class));

        public static final String WRAPCONSTRUCTOR_NAME = Bootstraps.WRAPCONSTRUCTOR_NAME;
        public static final String WRAPCONSTRUCTOR_DESC = Type.getMethodDescriptor(getType(CallSite.class), getType(MethodHandles.Lookup.class), getType(String.class), getType(MethodType.class),
                getType(String.class), getType(MethodType.class));

        public static final String WRAPSUPERCONSTRUCTORARGS_NAME = Bootstraps.WRAPSUPERCONSTRUCTORARGS_NAME;
        public static final String WRAPSUPERCONSTRUCTORARGS_DESC = Type.getMethodDescriptor(getType(CallSite.class), getType(MethodHandles.Lookup.class), getType(String.class),
                getType(MethodType.class), getType(String.class), getType(MethodType.class));

        public static final String WRAPSUPERCONSTRUCTORRES_NAME = Bootstraps.WRAPSUPERCONSTRUCTORRES_NAME;
        public static final String WRAPSUPERCONSTRUCTORRES_DESC = Type.getMethodDescriptor(getType(CallSite.class), getType(MethodHandles.Lookup.class), getType(String.class),
                getType(MethodType.class), getType(String.class), getType(MethodType.class));

        public static final String WRAPHANDLE_NAME = Bootstraps.WRAPHANDLE_NAME;
        public static final String WRAPHANDLE_DESC = Type.getMethodDescriptor(getType(CallSite.class), getType(MethodHandles.Lookup.class), getType(String.class), getType(MethodType.class),
                Type.INT_TYPE, getType(String.class), getType(MethodType.class));

        public static final String WRAPDYNAMIC_NAME = Bootstraps.WRAPDYNAMIC_NAME;
        public static final String WRAPDYNAMIC_DESC = Type.getMethodDescriptor(getType(CallSite.class), getType(MethodHandles.Lookup.class), getType(String.class), getType(MethodType.class),
                Type.INT_TYPE, getType(String.class), getType(String.class), getType(MethodType.class), getType(Object[].class));

        public static final String[] ARGPACK_NAMES = makeArgPackNameTable();
        public static final String[] ARGPACK_DESCS;

        private final RewritePolicy policy;
        private final Type clazz;
        private final boolean constructor;
        private AnalyzerAdapter analyzer;
        private Set