<commit_before>/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2003-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 edu.umd.cs.findbugs.*;
import edu.umd.cs.findbugs.ba.*;
import edu.umd.cs.findbugs.ba.vna.*;

import java.util.*;
import org.apache.bcel.Constants;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;

public class FindMismatchedWaitOrNotify implements Detector, StatelessDetector {
	private BugReporter bugReporter;

	public FindMismatchedWaitOrNotify(BugReporter bugReporter) {
		this.bugReporter = bugReporter;
	}

	@Override
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			throw new RuntimeException(e);
		}
	}

	public void visitClassContext(ClassContext classContext) {
		JavaClass jclass = classContext.getJavaClass();

		Method[] methodList = jclass.getMethods();
		for (Method method : methodList) {
			MethodGen methodGen = classContext.getMethodGen(method);
			if (methodGen == null)
				continue;

			// Don't bother analyzing the method unless there is both locking
			// and a method call.
			BitSet bytecodeSet = classContext.getBytecodeSet(method);
			if (bytecodeSet == null) continue;
			if (!(bytecodeSet.get(Constants.MONITORENTER) && bytecodeSet.get(Constants.INVOKEVIRTUAL)))
				continue;

			try {
				analyzeMethod(classContext, method);
			} catch (DataflowAnalysisException e) {
				bugReporter.logError("FindMismatchedWaitOrNotify: caught exception", e);
			} catch (CFGBuilderException e) {
				bugReporter.logError("FindMismatchedWaitOrNotify: caught exception", e);
			}
		}
	}

	private void analyzeMethod(ClassContext classContext, Method method)
	throws CFGBuilderException, DataflowAnalysisException {

		MethodGen methodGen = classContext.getMethodGen(method);
		if (methodGen == null) return;
		ConstantPoolGen cpg = methodGen.getConstantPool();
		CFG cfg = classContext.getCFG(method);
		ValueNumberDataflow vnaDataflow = classContext.getValueNumberDataflow(method);
		LockDataflow dataflow = classContext.getLockDataflow(method);

		for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
			Location location = i.next();

			InstructionHandle handle = location.getHandle();

			Instruction ins = handle.getInstruction();
			if (!(ins instanceof INVOKEVIRTUAL))
				continue;
			INVOKEVIRTUAL inv = (INVOKEVIRTUAL) ins;

			String methodName = inv.getName(cpg);
			String methodSig = inv.getSignature(cpg);

			if (Hierarchy.isMonitorWait(methodName, methodSig)
					|| Hierarchy.isMonitorNotify(methodName, methodSig)) {
				int numConsumed = inv.consumeStack(cpg);
				if (numConsumed == Constants.UNPREDICTABLE)
					throw new DataflowAnalysisException("Unpredictable stack consumption", methodGen, handle);

				ValueNumberFrame frame = vnaDataflow.getFactAtLocation(location);
				if (!frame.isValid())
					// Probably dead code
					continue;
				if (frame.getStackDepth() - numConsumed < 0)
					throw new DataflowAnalysisException("Stack underflow", methodGen, handle);
				ValueNumber ref = frame.getValue(frame.getNumSlots() - numConsumed);
				LockSet lockSet = dataflow.getFactAtLocation(location);
				int lockCount = lockSet.getLockCount(ref.getNumber());

				if (lockCount == 0) {
					Collection<ValueNumber> lockedValueNumbers = lockSet.getLockedValueNumbers(frame);
					boolean foundMatch = false;
					for(ValueNumber v : lockedValueNumbers) 
						if (frame.fromMatchingLoads(ref, v)){
							foundMatch = true;
							break;
						}
<commit_msg>handle fuzzy matches coming from method calls<commit_after>/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2003-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 edu.umd.cs.findbugs.*;
import edu.umd.cs.findbugs.ba.*;
import edu.umd.cs.findbugs.ba.vna.*;

import java.util.*;
import org.apache.bcel.Constants;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;

public class FindMismatchedWaitOrNotify implements Detector, StatelessDetector {
	private BugReporter bugReporter;

	public FindMismatchedWaitOrNotify(BugReporter bugReporter) {
		this.bugReporter = bugReporter;
	}

	@Override
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			throw new RuntimeException(e);
		}
	}

	public void visitClassContext(ClassContext classContext) {
		JavaClass jclass = classContext.getJavaClass();

		Method[] methodList = jclass.getMethods();
		for (Method method : methodList) {
			MethodGen methodGen = classContext.getMethodGen(method);
			if (methodGen == null)
				continue;

			// Don't bother analyzing the method unless there is both locking
			// and a method call.
			BitSet bytecodeSet = classContext.getBytecodeSet(method);
			if (bytecodeSet == null) continue;
			if (!(bytecodeSet.get(Constants.MONITORENTER) && bytecodeSet.get(Constants.INVOKEVIRTUAL)))
				continue;

			try {
				analyzeMethod(classContext, method);
			} catch (DataflowAnalysisException e) {
				bugReporter.logError("FindMismatchedWaitOrNotify: caught exception", e);
			} catch (CFGBuilderException e) {
				bugReporter.logError("FindMismatchedWaitOrNotify: caught exception", e);
			}
		}
	}

	private void analyzeMethod(ClassContext classContext, Method method)
	throws CFGBuilderException, DataflowAnalysisException {

		MethodGen methodGen = classContext.getMethodGen(method);
		if (methodGen == null) return;
		ConstantPoolGen cpg = methodGen.getConstantPool();
		CFG cfg = classContext.getCFG(method);
		ValueNumberDataflow vnaDataflow = classContext.getValueNumberDataflow(method);
		LockDataflow dataflow = classContext.getLockDataflow(method);

		for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) {
			Location location = i.next();

			InstructionHandle handle = location.getHandle();

			Instruction ins = handle.getInstruction();
			if (!(ins instanceof INVOKEVIRTUAL))
				continue;
			INVOKEVIRTUAL inv = (INVOKEVIRTUAL) ins;

			String methodName = inv.getName(cpg);
			String methodSig = inv.getSignature(cpg);

			if (Hierarchy.isMonitorWait(methodName, methodSig)
					|| Hierarchy.isMonitorNotify(methodName, methodSig)) {
				int numConsumed = inv.consumeStack(cpg);
				if (numConsumed == Constants.UNPREDICTABLE)
					throw new DataflowAnalysisException("Unpredictable stack consumption", methodGen, handle);

				ValueNumberFrame frame = vnaDataflow.getFactAtLocation(location);
				if (!frame.isValid())
					// Probably dead code
					continue;
				if (frame.getStackDepth() - numConsumed < 0)
					throw new DataflowAnalysisException("Stack underflow", methodGen, handle);
				ValueNumber ref = frame.getValue(frame.getNumSlots() - numConsumed);
				LockSet lockSet = dataflow.getFactAtLocation(location);
				int lockCount = lockSet.getLockCount(ref.getNumber());

				if (lockCount == 0) {
					Collection<ValueNumber> lockedValueNumbers = lockSet.getLockedValueNumbers(frame);
					boolean foundMatch = false;
					for(ValueNumber v : lockedValueNumbers) 
						if (frame.fuzzyMatch(ref, v)){
							foundMatch = true;
							break;
						}

{'fraction_non_alphanumeric': 0.09450830140485313, 'fraction_numerical': 0.0060373853477301755, 'mean_word_length': 4.346989447548107, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '7662742', 'n_tokens_mistral': 2876, 'n_tokens_neox': 2466, 'n_words': 720}