private Function createCheckcast() { Function fn = FunctionBuilder.checkcast(sootClass); Value info = getInfoStruct(fn); Value result = call(fn, BC_CHECKCAST, fn.getParameterRef(0), info, fn.getParameterRef(1)); fn.add(new Ret(result)); return fn; }
private Function createLdcClassWrapper() { Function fn = FunctionBuilder.ldcExternal(sootClass); Value info = getInfoStruct(fn); Value result = call(fn, LDC_CLASS_WRAPPER, fn.getParameterRef(0), info); fn.add(new Ret(result)); return fn; }
private Function createAllocator() { Function fn = FunctionBuilder.allocator(sootClass); Value info = getInfoStruct(fn); Value result = call(fn, BC_ALLOCATE, fn.getParameterRef(0), info); fn.add(new Ret(result)); return fn; }
private Function createInstanceof() { Function fn = FunctionBuilder.instanceOf(sootClass); Value info = getInfoStruct(fn); Value result = call(fn, BC_INSTANCEOF, fn.getParameterRef(0), info, fn.getParameterRef(1)); fn.add(new Ret(result)); return fn; }
private void createLookupFunction(SootMethod m) { // TODO: This should use a virtual method table or interface method table. Function function = FunctionBuilder.lookup(m); mb.addFunction(function); Variable reserved0 = function.newVariable(I8_PTR_PTR); function.add(new Getelementptr(reserved0, function.getParameterRef(0), 0, 4)); Variable reserved1 = function.newVariable(I8_PTR_PTR); function.add(new Getelementptr(reserved1, function.getParameterRef(0), 0, 5)); function.add(new Store(getString(m.getName()), reserved0.ref())); function.add(new Store(getString(getDescriptor(m)), reserved1.ref())); Value lookupFn = sootClass.isInterface() ? BC_LOOKUP_INTERFACE_METHOD : BC_LOOKUP_VIRTUAL_METHOD; List<Value> args = new ArrayList<Value>(); args.add(function.getParameterRef(0)); if (sootClass.isInterface()) { Value info = getInfoStruct(function); args.add(info); } args.add(function.getParameterRef(1)); args.add(getString(m.getName())); args.add(getString(getDescriptor(m))); Value fptr = call(function, lookupFn, args); Variable f = function.newVariable(function.getType()); function.add(new Bitcast(f, fptr, f.getType())); Value result = call(function, f.ref(), function.getParameterRefs()); function.add(new Ret(result)); }
private Function createClassInitWrapperFunction(FunctionRef targetFn) { Function fn = FunctionBuilder.clinitWrapper(targetFn); Value info = getInfoStruct(fn); Variable infoHeader = fn.newVariable(new PointerType(new StructureType(I8_PTR, I32))); fn.add(new Bitcast(infoHeader, info, infoHeader.getType())); Variable infoHeaderFlags = fn.newVariable(new PointerType(I32)); fn.add(new Getelementptr(infoHeaderFlags, infoHeader.ref(), 0, 1)); Variable flags = fn.newVariable(I32); fn.add(new Load(flags, infoHeaderFlags.ref())); Variable initializedFlag = fn.newVariable(I32); fn.add(new And(initializedFlag, flags.ref(), new IntegerConstant(CI_INITIALIZED))); Variable initialized = fn.newVariable(I1); fn.add( new Icmp( initialized, Icmp.Condition.eq, initializedFlag.ref(), new IntegerConstant(CI_INITIALIZED))); Label trueLabel = new Label(); Label falseLabel = new Label(); fn.add( new Br(initialized.ref(), fn.newBasicBlockRef(trueLabel), fn.newBasicBlockRef(falseLabel))); fn.newBasicBlock(trueLabel); Value result = call(fn, targetFn, fn.getParameterRefs()); fn.add(new Ret(result)); fn.newBasicBlock(falseLabel); call(fn, BC_INITIALIZE_CLASS, fn.getParameterRef(0), info); fn.add(new Br(fn.newBasicBlockRef(trueLabel))); return fn; }
private Function createFieldSetter(SootField field) { Function fn = FunctionBuilder.setter(field); Value fieldPtr = null; Value value = null; if (field.isStatic()) { fieldPtr = getClassFieldPtr(fn, field); value = fn.getParameterRef(1); } else { fieldPtr = getInstanceFieldPtr(fn, fn.getParameterRef(1), field); value = fn.getParameterRef(2); } if (Modifier.isVolatile(field.getModifiers()) || !field.isStatic() && Modifier.isFinal(field.getModifiers())) { if (LongType.v().equals(field.getType())) { fn.add(new Store(value, fieldPtr, false, Ordering.unordered, 8)); } else { fn.add(new Store(value, fieldPtr)); } fn.add(new Fence(Ordering.seq_cst)); } else { fn.add(new Store(value, fieldPtr)); } fn.add(new Ret()); return fn; }
private Function createFieldGetter(SootField field) { Function fn = FunctionBuilder.getter(field); Value fieldPtr = null; if (field.isStatic()) { fieldPtr = getClassFieldPtr(fn, field); } else { fieldPtr = getInstanceFieldPtr(fn, fn.getParameterRef(1), field); } Variable result = fn.newVariable(getType(field.getType())); if (Modifier.isVolatile(field.getModifiers())) { fn.add(new Fence(Ordering.seq_cst)); if (LongType.v().equals(field.getType())) { fn.add(new Load(result, fieldPtr, false, Ordering.unordered, 8)); } else { fn.add(new Load(result, fieldPtr)); } } else { fn.add(new Load(result, fieldPtr)); } fn.add(new Ret(new VariableRef(result))); return fn; }
private void compile(Clazz clazz, OutputStream out) throws IOException { sootClass = clazz.getSootClass(); mb = new ModuleBuilder(); trampolines = new HashSet<Trampoline>(); catches = new HashSet<String>(); classFields = getClassFields(sootClass); instanceFields = getInstanceFields(sootClass); classType = getClassType(sootClass); instanceType = getInstanceType(sootClass); attributesEncoder.encode(mb, sootClass); // Add a <clinit> method if the class has ConstantValueTags but no <clinit>. // This has to be done before createInfoStruct() is called otherwise the // ClassInfoHeader->initializer value will become NULL and constant static fields // will never be initialized. if (!sootClass.declaresMethodByName("<clinit>") && hasConstantValueTags(classFields)) { SootMethod clinit = new SootMethod("<clinit>", Collections.EMPTY_LIST, VoidType.v(), Modifier.STATIC); JimpleBody body = Jimple.v().newBody(clinit); clinit.setActiveBody(body); body.getUnits().add(new JReturnVoidStmt()); this.sootClass.addMethod(clinit); } if (isStruct(sootClass)) { if (!Modifier.isFinal(sootClass.getModifiers())) { throw new IllegalArgumentException("Struct class must be final"); } SootMethod _sizeOf = new SootMethod("_sizeOf", Collections.EMPTY_LIST, IntType.v(), Modifier.PROTECTED); sootClass.addMethod(_sizeOf); SootMethod sizeOf = new SootMethod( "sizeOf", Collections.EMPTY_LIST, IntType.v(), Modifier.PUBLIC | Modifier.STATIC); sootClass.addMethod(sizeOf); } mb.addInclude( getClass() .getClassLoader() .getResource( String.format("header-%s-%s.ll", config.getOs().getFamily(), config.getArch()))); mb.addInclude(getClass().getClassLoader().getResource("header.ll")); mb.addFunction(createInstanceof()); mb.addFunction(createCheckcast()); mb.addFunction(createLdcClass()); mb.addFunction(createLdcClassWrapper()); Function allocator = createAllocator(); mb.addFunction(allocator); mb.addFunction(createClassInitWrapperFunction(allocator.ref())); for (SootField f : sootClass.getFields()) { Function getter = createFieldGetter(f); Function setter = createFieldSetter(f); mb.addFunction(getter); mb.addFunction(setter); if (f.isStatic() && !f.isPrivate()) { mb.addFunction(createClassInitWrapperFunction(getter.ref())); if (!f.isFinal()) { mb.addFunction(createClassInitWrapperFunction(setter.ref())); } } } for (SootMethod method : sootClass.getMethods()) { String name = method.getName(); if (isBridge(method)) { bridgeMethod(method); } else if (isStruct(sootClass) && ("_sizeOf".equals(name) || "sizeOf".equals(name) || isStructMember(method))) { structMember(method); } else if (method.isNative()) { nativeMethod(method); } else if (!method.isAbstract()) { method(method); } if (!name.equals("<clinit>") && !name.equals("<init>") && !method.isPrivate() && !method.isStatic() && !Modifier.isFinal(method.getModifiers()) && !Modifier.isFinal(sootClass.getModifiers())) { createLookupFunction(method); } if (method.isStatic()) { String fnName = mangleMethod(method); if (method.isSynchronized()) { fnName += "_synchronized"; } FunctionRef fn = new FunctionRef(fnName, getFunctionType(method)); mb.addFunction(createClassInitWrapperFunction(fn)); } } Set<String> trampolineDependencies = new HashSet<String>(); for (Trampoline trampoline : trampolines) { trampolineResolver.compile(mb, trampoline); trampolineDependencies.addAll(trampolineResolver.getDependencies()); } Global classInfoStruct = null; StructureConstant classInfoErrorStruct = createClassInfoErrorStruct(); if (classInfoErrorStruct != null) { // The class cannot be loaded at runtime. Replace the ClassInfo struct // with a ClassInfoError struct with details of why. classInfoStruct = new Global(mangleClass(sootClass) + "_info_struct", classInfoErrorStruct); } else { classInfoStruct = new Global(mangleClass(sootClass) + "_info_struct", createClassInfoStruct()); } mb.addGlobal(classInfoStruct); Function infoFn = FunctionBuilder.infoStruct(sootClass); infoFn.add(new Ret(new ConstantBitcast(classInfoStruct.ref(), I8_PTR_PTR))); mb.addFunction(infoFn); out.write(mb.build().toString().getBytes("UTF-8")); clazz.clearDependencies(); clazz.addDependency( "java/lang/Object"); // Make sure no class or interface has zero dependencies if (sootClass.hasSuperclass() && !sootClass.isInterface()) { clazz.addDependency(getInternalName(sootClass.getSuperclass())); } for (SootClass iface : sootClass.getInterfaces()) { clazz.addDependency(getInternalName(iface)); } for (SootField f : sootClass.getFields()) { addDependencyIfNeeded(clazz, f.getType()); } for (SootMethod m : sootClass.getMethods()) { addDependencyIfNeeded(clazz, m.getReturnType()); @SuppressWarnings("unchecked") List<soot.Type> paramTypes = (List<soot.Type>) m.getParameterTypes(); for (soot.Type type : paramTypes) { addDependencyIfNeeded(clazz, type); } } clazz.addDependencies(attributesEncoder.getDependencies()); clazz.addDependencies(trampolineDependencies); clazz.addDependencies(catches); for (Trampoline t : trampolines) { if (!(t instanceof LdcString)) { String desc = t.getTarget(); if (desc.charAt(0) == 'L' || desc.charAt(0) == '[') { // Target is a descriptor addDependencyIfNeeded(clazz, desc); } else { clazz.addDependency(t.getTarget()); } } if (t instanceof FieldAccessor) { addDependencyIfNeeded(clazz, ((FieldAccessor) t).getFieldDesc()); } else if (t instanceof Invoke) { String methodDesc = ((Invoke) t).getMethodDesc(); addDependencyIfNeeded(clazz, getReturnTypeDescriptor(methodDesc)); for (String desc : getParameterDescriptors(methodDesc)) { addDependencyIfNeeded(clazz, desc); } } } clazz.saveDependencies(); }
private Value getInfoStruct(Function f) { return call(f, FunctionBuilder.infoStruct(sootClass).ref()); }