Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2015 The Pennsylvania State University and the University of Wisconsin
   * Systems and Internet Infrastructure Security Laboratory
   *
   * Author: Damien Octeau
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package edu.psu.cse.siis.coal;
 
 import java.io.File;
 import java.util.Map;
 import java.util.Set;
 
 
 import soot.Scene;
 import soot.SootClass;
 import soot.Value;
Singleton that represents the COAL model for the analysis.
 
 public class Model implements Serializable {
   private static final long serialVersionUID = 1L;
 
   private static Model instance;
 
   private final Logger logger = LoggerFactory.getLogger(getClass());
 
   private Set<StringmodeledTypes = new HashSet<>();
   private Map<StringArgument[]> copyConstructors = new HashMap<>();
   private Map<StringArgument[]> staticFieldToArgumentsMap = new HashMap<>();
   private Set<StringexcludedClasses = new HashSet<>();

  
Returns the singleton instance of this class. This class should be first initialized using loadModelFromCompiledFile(java.lang.String), loadModelFromDirectory(java.lang.String) or loadModelFromFile(java.lang.String).

Returns:
The singleton instance.
 
   public static Model v() {
     if ( == null) {
       throw new RuntimeException("Model was not initialized. Please make sure you call "
           + "loadModelFromDirectory(), loadModelFromFile() or loadCompiledModelFromFile()");
     }
     return ;
   }

  
Returns the set of all fully-qualified types modeled using COAL.

Returns:
The set of all types modeled using COAL.
 
   public Set<StringgetModeledTypes() {
     return ;
   }

  
Loads the model from a sequence of paths, which can either be directories or files. They should be separated by the platform's path separator (':' in most platforms).

Parameters:
modelPaths A sequence of paths to model files or directories.
Throws:
java.io.FileNotFoundException if something goes wrong with the file operations.
edu.psu.cse.siis.coal.lang.ParseException if something goes wrong with the parsing process.
  public static void loadModel(String modelPathsthrows FileNotFoundExceptionParseException {
    String[] modelPathsParts = modelPaths.split(.);
    for (String modelPath : modelPathsParts) {
      File file = new File(modelPath);
      if (file.isDirectory()) {
        loadModelFromDirectory(modelPath);
      } else {
        loadModelFromFile(modelPath);
      }
    }
  }

  
Loads the model from a directory containing all COAL specifications for the analysis. The COAL files should all be in that same directory (no subdirectories). This should only be called once, as it calls endInitialization(), which prevents any further modification to the model.

Parameters:
modelDir The path to a directory containing COAL model files.
Throws:
java.io.FileNotFoundException if something goes wrong with the file operations.
edu.psu.cse.siis.coal.lang.ParseException if something goes wrong with the parsing process.
  public static void loadModelFromDirectory(String modelDirthrows FileNotFoundException,
      ParseException {
    if ( == null) {
       = new Model();
    }
    PropagationParser.parseModelFromDirectory(modelDir);
  }

  
Loads the COAL model from a single file. This can be called multiple times. After all COAL files have been loaded, method endInitialization() should be called.

Parameters:
modelFilePath The path to a COAL specification file.
Throws:
java.io.FileNotFoundException if something goes wrong with the file operations.
edu.psu.cse.siis.coal.lang.ParseException if something goes wrong with the parsing process.
  public static void loadModelFromFile(String modelFilePaththrows FileNotFoundException,
      ParseException {
    if ( == null) {
       = new Model();
    }
    PropagationParser.parseModelFromFile(new File(modelFilePath));
  }

  
Loads the COAL model from a compiled COAL file. For the moment, this is simply a serialized Model object. There is no need to call endInitialization() after this. This method can read a serialized file from inside a jar archive, on the condition that the path to the file starts with /res/. Compiling the model can be done using the ModelCompiler class.

Parameters:
compiledModelFilePath The path to a compiled model file.
Throws:
java.io.IOException if something goes wrong with the file operations.
java.lang.ClassNotFoundException if something goes wrong with the deserialization.
  public static void loadModelFromCompiledFile(String compiledModelFilePaththrows IOException,
    InputStream inputStream;
    if (compiledModelFilePath.startsWith("/res/")) {
      inputStream = Model.class.getResourceAsStream(compiledModelFilePath);
    } else {
      inputStream = new FileInputStream(compiledModelFilePath);
    }
    ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
     = (ModelobjectInputStream.readObject();
  }

  
Determines if a type is modeled using COAL.

Parameters:
type A fully-qualified class type.
Returns:
True if the type is modeled using COAL.
  public boolean isModeledType(String type) {
    return .contains(type);
  }

  
Returns the arguments for a potential modifier method.

Parameters:
invokeExpr An invoke expression.
Returns:
An array of arguments if the invoke expression is for a modifier, null otherwise.
  public Argument[] getArgumentsForMethod(InvokeExpr invokeExpr) {
  }

  
Returns the arguments for a potential generating modifier method.

Parameters:
invokeExpr An invoke expression.
Returns:
An array of arguments if the invoke expression is for a generating modifier, null otherwise.
  public Argument[] getArgumentsForGenMethod(InvokeExpr invokeExpr) {
  }

  
Returns the arguments for a potential method that copies a COAL value. In this case the source COAL value is the base of the method call. The value is copied to the return value of the method.

Parameters:
invokeExpr An invoke expression.
Returns:
An array of arguments if the invoke expression is for a copy modifier, null otherwise.
  public Argument[] getArgumentsForCopyMethod(InvokeExpr invokeExpr) {
  }

  
Returns the arguments for a potential copy constructor.

Parameters:
signature A method signature.
Returns:
An array of arguments if the method signature is for a copy constructor of interest, null otherwise.
  public Argument[] getArgumentsForCopyConstructor(String signature) {
    return .get(signature);
  }

  
Returns the arguments for a potential copy constructor.

Parameters:
methodRef A Soot method reference.
Returns:
An array of arguments if the method reference is for a copy constructor of interest, null otherwise.
    return getArgumentsForCopyConstructor(methodRef.getSignature());
  }

  
Returns the arguments for a potential COAL source.

Parameters:
invokeExpr An invoke expression.
Returns:
An array of arguments if the invoke expression is for a COAL source, null otherwise.
  public Argument[] getArgumentsForSource(InvokeExpr invokeExpr) {
  }

  
Returns the arguments for a potential COAL query.

Parameters:
stmt A program statement.
Returns:
An array of arguments if the statement is for a COAL query, null otherwise.
  public Argument[] getArgumentsForQuery(Stmt stmt) {
    if (stmt.containsInvokeExpr()) {
      InvokeExpr invokeExpr = stmt.getInvokeExpr();
      SootMethod method = invokeExpr.getMethod();
      if (AnalysisParameters.v().isAnalysisClass(method.getDeclaringClass().getName())
          && method.isConcrete() && method.hasActiveBody()) {
        MethodDescription description = .get(method.getSignature());
        if (description == null) {
          return null;
        } else {
          return description.getArguments();
        }
      }
    }
    return null;
  }

  
Returns the arguments for a static final field. This is useful when COAL values are constants that should not be propagated. They can be modeled using the COAL language and retrieved using this method.

Parameters:
signature A field signature.
Returns:
An array of arguments if the statement is for a COAL constant, null otherwise.
  public Argument[] getArgumentsForStaticField(String signature) {
    return .get(signature);
  }

  
Returns the arguments associated with a method descriptor.

Parameters:
signatureToMethodDescriptionMap A map from signatures to method descriptors.
invokeExpr An invoke expression.
Returns:
An array of arguments if arguments are found for the method descriptor, null otherwise.
      Map<StringMethodDescriptionsignatureToMethodDescriptionMapInvokeExpr invokeExpr) {
    SootMethod method = invokeExpr.getMethod();
    String signature = method.getSignature();
    MethodDescription methodDescription = signatureToMethodDescriptionMap.get(signature);
    if (methodDescription != null) {
      return methodDescription.getArguments();
    }
    signature = method.getSubSignature();
    methodDescription = signatureToMethodDescriptionMap.get(signature);
    if (methodDescription == null) {
      return null;
    }
    String superclassName = methodDescription.getBaseClass();
    if (superclassName == null || !Scene.v().containsClass(superclassName)
        || invokeExpr instanceof InterfaceInvokeExpr) {
      return null;
    }
    SootClass superclass = Scene.v().getSootClass(superclassName);
    String baseType;
    if (invokeExpr instanceof InstanceInvokeExpr) {
      Value baseValue = ((InstanceInvokeExprinvokeExpr).getBase();
      baseType = baseValue.getType().toString();
    } else {
      baseType = invokeExpr.getMethod().getDeclaringClass().getName();
    }
    if (Scene.v().containsClass(baseType)
        && Scene.v().getActiveHierarchy()
            .isClassSubclassOfIncluding(Scene.v().getSootClass(baseType), superclass)) {
      return methodDescription.getArguments();
    } else {
      return null;
    }
  }

  
Determines if the class is excluded from the analysis results. This can be used when processing the results results to avoid reporting results for a certain class. This does not prevent the class from being traversed during the constant propagation, which is decided by whether the class is included using the -input or -classpath flag on the command line.

Parameters:
name A fully-qualified class name.
Returns:
True if the class should be excluded from the result.
  public boolean isExcludedClass(String name) {
    return .contains(name);
  }

  
Adds a type to the set of modeled types.

Parameters:
type A fully-qualified class type.
  public void addModeledType(String type) {
    .add(type);
  }

  
Adds a COAL modifier.

Parameters:
extendedSignature An ExtendedSignature.
arguments An array of arguments.
modifierModifier A modifier modifier.
  public void addModifier(ExtendedSignature extendedSignatureArgument[] arguments,
      String modifierModifier) {
    MethodDescription methodDescription =
        new MethodDescription(extendedSignature.getSuperclass(), arguments);
    String signature = extendedSignature.getSignature();
    if (modifierModifier == null) {
      .put(signaturemethodDescription);
    } else if (modifierModifier.equals("gen")) {
      .put(signaturemethodDescription);
    } else if (modifierModifier.equals("copy")) {
      .put(signaturemethodDescription);
    } else {
      throw new RuntimeException("Unknown modifier modifier: " + modifierModifier);
    }
  }

  
Adds a COAL source.

Parameters:
extendedSignature An ExtendedSignature.
arguments An array of arguments.
  public void addSource(ExtendedSignature extendedSignatureArgument[] arguments) {
    MethodDescription methodDescription =
        new MethodDescription(extendedSignature.getSuperclass(), arguments);
    .put(extendedSignature.getSignature(), methodDescription);
  }

  
Adds a copy constructor.

Parameters:
signature A method signature.
arguments An array of arguments.
  public void addCopyConstructor(String signatureArgument[] arguments) {
    .put(signaturearguments);
  }

  
Adds a COAL query.

Parameters:
extendedSignature An ExtendedSignature.
arguments An array of arguments.
  public void addQuery(ExtendedSignature extendedSignatureArgument[] arguments) {
    MethodDescription methodDescription =
        new MethodDescription(extendedSignature.getSuperclass(), arguments);
    .put(extendedSignature.getSignature(), methodDescription);
  }

  
Adds a COAL constant.

Parameters:
signature A field signature. This should be for a static final field (i.e., a constant).
arguments An array of arguments.
  public void addConstant(String signatureArgument[] arguments) {
    .put(signaturearguments);
  }

  
Adds a type for which results should not be reported.

Parameters:
name A fully-qualified class type.
  public void addExcludedClass(String name) {
    .add(name);
  }

  
Ends the initialization of this model. This should be called after the model is populated. No other modification to the model should be performed after calling this method.
  public void endInitialization() {
     = Collections.unmodifiableSet();
  }
  public String toString() {
    StringBuilder stringBuilder = new StringBuilder("*****Model*****\n");
    stringBuilder.append("**Modeled Types**\n");
    stringBuilder.append().append("\n");
    stringBuilder.append("\n**Modifiers**\n");
      stringBuilder.append(entry.getKey()).append("\n");
      stringBuilder.append(entry.getValue()).append("\n");
    }
    stringBuilder.append("\n**Generating Modifiers**\n");
      stringBuilder.append(entry.getKey()).append("\n");
      stringBuilder.append(entry.getValue()).append("\n");
    }
    stringBuilder.append("\n**Copy Modifiers**\n");
      stringBuilder.append(entry.getKey()).append("\n");
      stringBuilder.append(entry.getValue()).append("\n");
    }
    stringBuilder.append("\n**Sources**\n");
      stringBuilder.append(entry.getKey()).append("\n");
      stringBuilder.append(entry.getValue()).append("\n");
    }
    stringBuilder.append("\n**Copy Constructors**\n");
    for (Map.Entry<StringArgument[]> entry : .entrySet()) {
      stringBuilder.append(entry.getKey()).append("\n");
      for (Argument argument : entry.getValue()) {
        stringBuilder.append("    " + argument.toString()).append("\n");
      }
    }
    stringBuilder.append("\n**Static Fields**\n");
    for (Map.Entry<StringArgument[]> entry : .entrySet()) {
      stringBuilder.append(entry.getKey()).append("\n");
      for (Argument argument : entry.getValue()) {
        stringBuilder.append("    " + argument.toString()).append("\n");
      }
    }
    stringBuilder.append("\n**Queries**\n");
      stringBuilder.append(entry.getKey()).append("\n");
      stringBuilder.append(entry.getValue().toString()).append("\n");
    }
    stringBuilder.append("\n**Excluded Classes**\n");
    stringBuilder.append().append("\n");
    return stringBuilder.toString();
  }
  public void dump() {
  }
  private Model() {
  }
New to GrepCode? Check out our FAQ X