<commit_before>/*
 * Bytecode Analysis Framework
 * Copyright (C) 2005 University of Maryland
 * 
 * This library 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 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.detect;

import java.util.HashSet;
import java.util.Iterator;

import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.MethodGen;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.Detector;
import edu.umd.cs.findbugs.ba.AnalysisContext;
import edu.umd.cs.findbugs.ba.CFG;
import edu.umd.cs.findbugs.ba.CFGBuilderException;
import edu.umd.cs.findbugs.ba.ClassContext;
import edu.umd.cs.findbugs.ba.Dataflow;
import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
import edu.umd.cs.findbugs.ba.DataflowCFGPrinter;
import edu.umd.cs.findbugs.ba.DepthFirstSearch;
import edu.umd.cs.findbugs.ba.SignatureConverter;
import edu.umd.cs.findbugs.ba.obl.Obligation;
import edu.umd.cs.findbugs.ba.obl.ObligationAnalysis;
import edu.umd.cs.findbugs.ba.obl.ObligationFactory;
import edu.umd.cs.findbugs.ba.obl.PolicyDatabase;
import edu.umd.cs.findbugs.ba.obl.State;
import edu.umd.cs.findbugs.ba.obl.StateSet;

/**
 * Find unsatisfied obligations in Java methods.
 * Examples: open streams, open database connections, etc.
 *
 * <p>See Weimer and Necula,
 * <a href="http://doi.acm.org/10.1145/1028976.1029011"
 * >Finding and preventing run-time error handling mistakes</a>,
 * OOPSLA 2004.</p>
 * 
 * @author <NAME>
 */
public class FindUnsatisfiedObligation implements Detector {
	
	private static final boolean DEBUG = Boolean.getBoolean("oa.debug");
	private static final boolean DEBUG_PRINTCFG = Boolean.getBoolean("oa.printcfg");
	private static final String DEBUG_METHOD = System.getProperty("oa.method");
	
	private BugReporter bugReporter;
	private ObligationFactory factory;
	private PolicyDatabase database;
	
	public FindUnsatisfiedObligation(BugReporter bugReporter) {
		this.bugReporter = bugReporter;
		this.factory = new ObligationFactory();
		this.database = buildDatabase();
	}

	/* (non-Javadoc)
	 * @see edu.umd.cs.findbugs.Detector#setAnalysisContext(edu.umd.cs.findbugs.ba.AnalysisContext)
	 */
	public void setAnalysisContext(AnalysisContext analysisContext) {
	}

	/* (non-Javadoc)
	 * @see edu.umd.cs.findbugs.Detector#visitClassContext(edu.umd.cs.findbugs.ba.ClassContext)
	 */
	public void visitClassContext(ClassContext classContext) {
		// FIXME: prescreen class
		
		Method[] methodList = classContext.getJavaClass().getMethods();
		for (int i = 0; i < methodList.length; ++i) {
			Method method = methodList[i];
			
			if (DEBUG_METHOD != null && !method.getName().equals(DEBUG_METHOD))
				continue;
			
			MethodGen methodGen = classContext.getMethodGen(method);
			
			if (methodGen != null) {
				// FIXME: prescreen method
				analyzeMethod(classContext, method);
			}
		}
	}

	/**
	 * Analyze given method for unsatisfied obligations.
	 * 
	 * @param classContext the ClassContext of the class containing the method
	 * @param method       the method
	 */
	private void analyzeMethod(ClassContext classContext, Method method) {
		MethodGen methodGen = classContext.getMethodGen(method);
		if (DEBUG) {
			System.out.println("*** Analyzing method " +
					SignatureConverter.convertMethodSignature(methodGen));
		}

		try {
			CFG cfg = classContext.getCFG(method);
			DepthFirstSearch dfs = classContext.getDepthFirstSearch(method);
			
			ObligationAnalysis analysis =
				new ObligationAnalysis(dfs, methodGen, factory, database, bugReporter);
			Dataflow<StateSet, ObligationAnalysis> dataflow =
				new Dataflow<StateSet, ObligationAnalysis>(cfg, analysis);
			
			dataflow.execute();
			
			if (DEBUG_PRINTCFG) {
				System.out.println("Dataflow CFG:");
				DataflowCFGPrinter<StateSet, ObligationAnalysis> cfgPrinter =
					new DataflowCFGPrinter<StateSet, ObligationAnalysis>(cfg, dataflow, analysis);
				cfgPrinter.print(System.out);
			}
			
			// See if there are any states with nonempty obligation sets
			StateSet factAtExit = dataflow.getStartFact(cfg.getExit());
			HashSet<Obligation> leakedObligationSet = new HashSet<Obligation>();
			for (Iterator<State> i = factAtExit.stateIterator(); i.hasNext();) {
				State state = i.next();
				for (int id = 0; id < factory.getMaxObligationTypes(); ++id) {
					if (state.getObligationSet().getCount(id) > 0) {
						leakedObligationSet.add(factory.getObligationById(id));<commit_msg>ObligationAnalysis now requires a TypeDataflow object.<commit_after>/*
 * Bytecode Analysis Framework
 * Copyright (C) 2005 University of Maryland
 * 
 * This library 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 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.detect;

import java.util.HashSet;
import java.util.Iterator;

import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.MethodGen;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.Detector;
import edu.umd.cs.findbugs.ba.AnalysisContext;
import edu.umd.cs.findbugs.ba.CFG;
import edu.umd.cs.findbugs.ba.CFGBuilderException;
import edu.umd.cs.findbugs.ba.ClassContext;
import edu.umd.cs.findbugs.ba.Dataflow;
import edu.umd.cs.findbugs.ba.DataflowAnalysisException;
import edu.umd.cs.findbugs.ba.DataflowCFGPrinter;
import edu.umd.cs.findbugs.ba.DepthFirstSearch;
import edu.umd.cs.findbugs.ba.SignatureConverter;
import edu.umd.cs.findbugs.ba.TypeDataflow;
import edu.umd.cs.findbugs.ba.obl.Obligation;
import edu.umd.cs.findbugs.ba.obl.ObligationAnalysis;
import edu.umd.cs.findbugs.ba.obl.ObligationFactory;
import edu.umd.cs.findbugs.ba.obl.PolicyDatabase;
import edu.umd.cs.findbugs.ba.obl.State;
import edu.umd.cs.findbugs.ba.obl.StateSet;

/**
 * Find unsatisfied obligations in Java methods.
 * Examples: open streams, open database connections, etc.
 *
 * <p>See Weimer and Necula,
 * <a href="http://doi.acm.org/10.1145/1028976.1029011"
 * >Finding and preventing run-time error handling mistakes</a>,
 * OOPSLA 2004.</p>
 * 
 * @author <NAME>
 */
public class FindUnsatisfiedObligation implements Detector {
	
	private static final boolean DEBUG = Boolean.getBoolean("oa.debug");
	private static final boolean DEBUG_PRINTCFG = Boolean.getBoolean("oa.printcfg");
	private static final String DEBUG_METHOD = System.getProperty("oa.method");
	
	private BugReporter bugReporter;
	private ObligationFactory factory;
	private PolicyDatabase database;
	
	public FindUnsatisfiedObligation(BugReporter bugReporter) {
		this.bugReporter = bugReporter;
		this.factory = new ObligationFactory();
		this.database = buildDatabase();
	}

	/* (non-Javadoc)
	 * @see edu.umd.cs.findbugs.Detector#setAnalysisContext(edu.umd.cs.findbugs.ba.AnalysisContext)
	 */
	public void setAnalysisContext(AnalysisContext analysisContext) {
	}

	/* (non-Javadoc)
	 * @see edu.umd.cs.findbugs.Detector#visitClassContext(edu.umd.cs.findbugs.ba.ClassContext)
	 */
	public void visitClassContext(ClassContext classContext) {
		// FIXME: prescreen class
		
		Method[] methodList = classContext.getJavaClass().getMethods();
		for (int i = 0; i < methodList.length; ++i) {
			Method method = methodList[i];
			
			if (DEBUG_METHOD != null && !method.getName().equals(DEBUG_METHOD))
				continue;
			
			MethodGen methodGen = classContext.getMethodGen(method);
			
			if (methodGen != null) {
				// FIXME: prescreen method
				analyzeMethod(classContext, method);
			}
		}
	}

	/**
	 * Analyze given method for unsatisfied obligations.
	 * 
	 * @param classContext the ClassContext of the class containing the method
	 * @param method       the method
	 */
	private void analyzeMethod(ClassContext classContext, Method method) {
		MethodGen methodGen = classContext.getMethodGen(method);
		if (DEBUG) {
			System.out.println("*** Analyzing method " +
					SignatureConverter.convertMethodSignature(methodGen));
		}

		try {
			CFG cfg = classContext.getCFG(method);
			DepthFirstSearch dfs = classContext.getDepthFirstSearch(method);
			TypeDataflow typeDataflow = classContext.getTypeDataflow(method);
			
			ObligationAnalysis analysis =
				new ObligationAnalysis(dfs, typeDataflow, methodGen, factory, database, bugReporter);
			Dataflow<StateSet, ObligationAnalysis> dataflow =
				new Dataflow<StateSet, ObligationAnalysis>(cfg, analysis);
			
			dataflow.execute();
			
			if (DEBUG_PRINTCFG) {
				System.out.println("Dataflow CFG:");
				DataflowCFGPrinter<StateSet, ObligationAnalysis> cfgPrinter =
					new DataflowCFGPrinter<StateSet, ObligationAnalysis>(cfg, dataflow, analysis);
				cfgPrinter.print(System.out);
			}
			
			// See if there are any states with nonempty obligation sets
			StateSet factAtExit = dataflow.getStartFact(cfg.getExit());
			HashSet<Obligation> leakedObligationSet = new HashSet<Obligation>();
			for (Iterator<State> i = factAtExit.stateIterator(); i.hasNext();) {
				State state = i.next();
				for (int id = 0; id < factory.getMaxObligationTypes(); ++id) {
					if (state.getObligationSet().getCount(id) > 0) {
						leakedObligationSet.add(factory.getObligationById(id));
{'fraction_non_alphanumeric': 0.10682692307692308, 'fraction_numerical': 0.009038461538461539, 'mean_word_length': 4.431331592689295, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, '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': '2895152', 'n_tokens_mistral': 3657, 'n_tokens_neox': 3230, 'n_words': 804}