<commit_before>/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2003, 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.daveho.ba.*;

import edu.umd.cs.findbugs.AnalysisException;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.CallGraph;
import edu.umd.cs.findbugs.CallGraphNode;
import edu.umd.cs.findbugs.CallGraphEdge;
import edu.umd.cs.findbugs.CallSite;
import edu.umd.cs.findbugs.Detector;
import edu.umd.cs.findbugs.SelfCalls;
import edu.umd.cs.findbugs.SourceLineAnnotation;

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

import java.util.*;

public class FindInconsistentSync2 implements Detector {
	private static final boolean DEBUG = Boolean.getBoolean("fis.debug");
	private static final boolean SYNC_ACCESS = Boolean.getBoolean("fis.syncAccess");

	/* ----------------------------------------------------------------------
	 * Helper classes
	 * ---------------------------------------------------------------------- */

	private static final int UNLOCKED = 0;
	private static final int LOCKED = 1;
	private static final int READ = 0;
	private static final int WRITE = 2;

	private static final int READ_UNLOCKED = READ | UNLOCKED;
	private static final int WRITE_UNLOCKED = WRITE | UNLOCKED;
	private static final int READ_LOCKED = READ | LOCKED;
	private static final int WRITE_LOCKED = WRITE | LOCKED;

	/**
	 * The access statistics for a field.
	 * Stores the number of locked and unlocked reads and writes,
	 * as well as the number of accesses made with a lock held.
	 */
	private static class FieldStats {
		private int[] countList = new int[4];
		private int numLocalLocks = 0;
		private LinkedList<SourceLineAnnotation> unsyncAccessList = new LinkedList<SourceLineAnnotation>();
		private LinkedList<SourceLineAnnotation> syncAccessList = new LinkedList<SourceLineAnnotation>();

		public void addAccess(int kind) {
			countList[kind]++;
		}

		public int getNumAccesses(int kind) {
			return countList[kind];
		}

		public void addLocalLock() {
			numLocalLocks++;
		}

		public int getNumLocalLocks() {
			return numLocalLocks;
		}

		public void addAccess(ClassContext classContext, Method method, InstructionHandle handle, boolean isLocked) {
			if (!SYNC_ACCESS && isLocked)
				return;

			JavaClass javaClass = classContext.getJavaClass();
			String sourceFile = javaClass.getSourceFileName();
			MethodGen methodGen = classContext.getMethodGen(method);
			SourceLineAnnotation accessSourceLine = SourceLineAnnotation.fromVisitedInstruction(methodGen, sourceFile, handle);
			if (accessSourceLine != null)
				(isLocked ? syncAccessList : unsyncAccessList).add(accessSourceLine);
		}

		public Iterator<SourceLineAnnotation> unsyncAccessIterator() {
			return unsyncAccessList.iterator();
		}

		public Iterator<SourceLineAnnotation> syncAccessIterator() {
			return syncAccessList.iterator();
		}
	}

	/* ----------------------------------------------------------------------
	 * Fields
	 * ---------------------------------------------------------------------- */

	private BugReporter bugReporter;
	private Map<XField, FieldStats> statMap = new HashMap<XField, FieldStats>();

	/* ----------------------------------------------------------------------
	 * Public methods
	 * ---------------------------------------------------------------------- */

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

	public void visitClassContext(ClassContext classContext) {
		try {
			JavaClass javaClass = classContext.getJavaClass();
			if (DEBUG) System.out.println("******** Analyzing class " + javaClass.getClassName());

			Set<Method> lockedMethodSet = findLockedMethods(classContext);

			Method[] methodList = javaClass.getMethods();
			for (int i = 0; i < methodList.length; ++i) {
				Method method = methodList[i];
				if (classContext.getMethodGen(method) == null)
					continue;
				if (isConstructor(method.getName()))
					continue;
				if (method.getName().startsWith("access$"))
					// Ignore inner class access methods;
					// we will treat calls to them as field accesses
					continue;
				analyzeMethod(classContext, method, lockedMethodSet);
			}

		} catch (CFGBuilderException e) {
			throw new AnalysisException("FindInconsistentSync2 caught exception: " + e.toString(), e);
		} catch (DataflowAnalysisException e) {
			throw new AnalysisException("FindInconsistentSync2 caught exception: " + e.toString(), e);
		}
	}

	public void report() {
		for (Iterator<Map.Entry<XField, FieldStats>> i = statMap.entrySet().iterator(); i.hasNext(); ) {
			Map.Entry<XField, FieldStats> entry = i.next();
			XField xfield = entry.getKey();
			FieldStats stats = entry.getValue();

			int numReadUnlocked = stats.getNumAccesses(READ_UNLOCKED);
			int numWriteUnlocked = stats.getNumAccesses(WRITE_UNLOCKED);
			int numReadLocked = stats.getNumAccesses(READ_LOCKED);
			int numWriteLocked = stats.getNumAccesses(WRITE_LOCKED);

			int locked =  numReadLocked + numWriteLocked;
			int biasedLocked =  numReadLocked + 2 * numWriteLocked;
			int unlocked =  numReadUnlocked + numWriteUnlocked;
			int biasedUnlocked =  numReadUnlocked + 2 * numWriteUnlocked;
			int writes =  numWriteLocked + numWriteUnlocked;

			if (locked == 0)
				continue;

			if (unlocked == 0) 
				continue;

			if (numReadUnlocked > 0 && 2 * biasedUnlocked > biasedLocked)
				continue;

			// NOTE: we ignore access to public, volatile, and final fields

			if (numWriteUnlocked + numWriteLocked == 0)
				// No writes outside of constructor
				continue;

			if (stats.getNumLocalLocks() == 0)
				continue;

			int freq = (100 * locked) / (locked + unlocked);
			if (freq < 50) continue;

			// At this point, we report the field as being inconsistently synchronized
			BugInstance bugInstance = new BugInstance("IS2_INCONSISTENT_SYNC", freq > 65 ? NORMAL_PRIORITY : LOW_PRIORITY)
				.addClass(xfield.getClassName())
				.addField(xfield)
				.addInt(freq).describe("INT_SYNC_PERCENT");

			// Add source lines for unsynchronized accesses
			for (Iterator<SourceLineAnnotation> j = stats.unsyncAccessIterator(); j.hasNext(); ) {
				SourceLineAnnotation accessSourceLine = j.next();
				bugInstance.addSourceLine(accessSourceLine).describe("SOURCE_LINE_UNSYNC_ACCESS");
			}

			if (SYNC_ACCESS) {
				// Add source line for synchronized accesses;
				// useful for figuring out what the detector is doing
				for (Iterator<SourceLineAnnotation> j = stats.syncAccessIterator(); j.hasNext(); ) {
					SourceLineAnnotation accessSourceLine = j.next();
					bugInstance.addSourceLine(accessSourceLine).describe("SOURCE_LINE_SYNC_ACCESS");
				}
			}

			bugReporter.reportBug(bugInstance);
		}
	}<commit_msg>Changed % for L priority<commit_after>/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2003, 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.daveho.ba.*;

import edu.umd.cs.findbugs.AnalysisException;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.CallGraph;
import edu.umd.cs.findbugs.CallGraphNode;
import edu.umd.cs.findbugs.CallGraphEdge;
import edu.umd.cs.findbugs.CallSite;
import edu.umd.cs.findbugs.Detector;
import edu.umd.cs.findbugs.SelfCalls;
import edu.umd.cs.findbugs.SourceLineAnnotation;

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

import java.util.*;

public class FindInconsistentSync2 implements Detector {
	private static final boolean DEBUG = Boolean.getBoolean("fis.debug");
	private static final boolean SYNC_ACCESS = Boolean.getBoolean("fis.syncAccess");

	/* ----------------------------------------------------------------------
	 * Helper classes
	 * ---------------------------------------------------------------------- */

	private static final int UNLOCKED = 0;
	private static final int LOCKED = 1;
	private static final int READ = 0;
	private static final int WRITE = 2;

	private static final int READ_UNLOCKED = READ | UNLOCKED;
	private static final int WRITE_UNLOCKED = WRITE | UNLOCKED;
	private static final int READ_LOCKED = READ | LOCKED;
	private static final int WRITE_LOCKED = WRITE | LOCKED;

	/**
	 * The access statistics for a field.
	 * Stores the number of locked and unlocked reads and writes,
	 * as well as the number of accesses made with a lock held.
	 */
	private static class FieldStats {
		private int[] countList = new int[4];
		private int numLocalLocks = 0;
		private LinkedList<SourceLineAnnotation> unsyncAccessList = new LinkedList<SourceLineAnnotation>();
		private LinkedList<SourceLineAnnotation> syncAccessList = new LinkedList<SourceLineAnnotation>();

		public void addAccess(int kind) {
			countList[kind]++;
		}

		public int getNumAccesses(int kind) {
			return countList[kind];
		}

		public void addLocalLock() {
			numLocalLocks++;
		}

		public int getNumLocalLocks() {
			return numLocalLocks;
		}

		public void addAccess(ClassContext classContext, Method method, InstructionHandle handle, boolean isLocked) {
			if (!SYNC_ACCESS && isLocked)
				return;

			JavaClass javaClass = classContext.getJavaClass();
			String sourceFile = javaClass.getSourceFileName();
			MethodGen methodGen = classContext.getMethodGen(method);
			SourceLineAnnotation accessSourceLine = SourceLineAnnotation.fromVisitedInstruction(methodGen, sourceFile, handle);
			if (accessSourceLine != null)
				(isLocked ? syncAccessList : unsyncAccessList).add(accessSourceLine);
		}

		public Iterator<SourceLineAnnotation> unsyncAccessIterator() {
			return unsyncAccessList.iterator();
		}

		public Iterator<SourceLineAnnotation> syncAccessIterator() {
			return syncAccessList.iterator();
		}
	}

	/* ----------------------------------------------------------------------
	 * Fields
	 * ---------------------------------------------------------------------- */

	private BugReporter bugReporter;
	private Map<XField, FieldStats> statMap = new HashMap<XField, FieldStats>();

	/* ----------------------------------------------------------------------
	 * Public methods
	 * ---------------------------------------------------------------------- */

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

	public void visitClassContext(ClassContext classContext) {
		try {
			JavaClass javaClass = classContext.getJavaClass();
			if (DEBUG) System.out.println("******** Analyzing class " + javaClass.getClassName());

			Set<Method> lockedMethodSet = findLockedMethods(classContext);

			Method[] methodList = javaClass.getMethods();
			for (int i = 0; i < methodList.length; ++i) {
				Method method = methodList[i];
				if (classContext.getMethodGen(method) == null)
					continue;
				if (isConstructor(method.getName()))
					continue;
				if (method.getName().startsWith("access$"))
					// Ignore inner class access methods;
					// we will treat calls to them as field accesses
					continue;
				analyzeMethod(classContext, method, lockedMethodSet);
			}

		} catch (CFGBuilderException e) {
			throw new AnalysisException("FindInconsistentSync2 caught exception: " + e.toString(), e);
		} catch (DataflowAnalysisException e) {
			throw new AnalysisException("FindInconsistentSync2 caught exception: " + e.toString(), e);
		}
	}

	public void report() {
		for (Iterator<Map.Entry<XField, FieldStats>> i = statMap.entrySet().iterator(); i.hasNext(); ) {
			Map.Entry<XField, FieldStats> entry = i.next();
			XField xfield = entry.getKey();
			FieldStats stats = entry.getValue();

			int numReadUnlocked = stats.getNumAccesses(READ_UNLOCKED);
			int numWriteUnlocked = stats.getNumAccesses(WRITE_UNLOCKED);
			int numReadLocked = stats.getNumAccesses(READ_LOCKED);
			int numWriteLocked = stats.getNumAccesses(WRITE_LOCKED);

			int locked =  numReadLocked + numWriteLocked;
			int biasedLocked =  numReadLocked + 2 * numWriteLocked;
			int unlocked =  numReadUnlocked + numWriteUnlocked;
			int biasedUnlocked =  numReadUnlocked + 2 * numWriteUnlocked;
			int writes =  numWriteLocked + numWriteUnlocked;

			if (locked == 0)
				continue;

			if (unlocked == 0) 
				continue;

			if (numReadUnlocked > 0 && 2 * biasedUnlocked > biasedLocked)
				continue;

			// NOTE: we ignore access to public, volatile, and final fields

			if (numWriteUnlocked + numWriteLocked == 0)
				// No writes outside of constructor
				continue;

			if (stats.getNumLocalLocks() == 0)
				continue;

			int freq = (100 * locked) / (locked + unlocked);
			if (freq < 50) continue;

			// At this point, we report the field as being inconsistently synchronized
			BugInstance bugInstance = new BugInstance("IS2_INCONSISTENT_SYNC", freq >= 75 ? NORMAL_PRIORITY : LOW_PRIORITY)
				.addClass(xfield.getClassName())
				.addField(xfield)
				.addInt(freq).describe("INT_SYNC_PERCENT");

			// Add source lines for unsynchronized accesses
			for (Iterator<SourceLineAnnotation> j = stats.unsyncAccessIterator(); j.hasNext(); ) {
				SourceLineAnnotation accessSourceLine = j.next();
				bugInstance.addSourceLine(accessSourceLine).describe("SOURCE_LINE_UNSYNC_ACCESS");
			}

			if (SYNC_ACCESS) {
				// Add source line for synchronized accesses;
				// useful for figuring out what the detector is doing
				for (Iterator<SourceLineAnnotation> j = stats.syncAccessIterator(); j.hasNext(); ) {
					SourceLineAnnotation accessSourceLine = j.next();
					bugInstance.addSourceLine(accessSourceLine).describe("SOURCE_LINE_SYNC_ACCESS");
				}
			}

			bugReporter.reportBug(bugInstance);
		}
	}
{'fraction_non_alphanumeric': 0.1480063374702931, 'fraction_numerical': 0.006205439662001585, 'mean_word_length': 3.2422290674880987, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, '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': '5833111', 'n_tokens_mistral': 4835, 'n_tokens_neox': 4219, 'n_words': 1219}