<commit_before>package edu.columbia.cs.psl.phosphor.runtime;

import edu.columbia.cs.psl.phosphor.struct.*;

/**
 * This class handles dynamically doing source-based tainting.
 * 
 * If you want to replace the *value* dynamically, then: 1. extend this class 2.
 * Set Configuration.autoTainter to be an instance of your new
 * TaintSourceWrapper 3. Override autoTaint..., change the value, then call
 * super.autoTaint... in order to set the taint correctly
 * 
 * Example:
 * 
 * 
 * public TaintedIntWithObjTag autoTaint(TaintedIntWithObjTag ret, String
 * source, int argIdx) { ret.val = 100; //Change value to be 100 instead of
 * whatever it was normally ret = super.autoTaint(ret, source, argIdx); //will
 * set the taint return ret; }
 * 
 * @author jon
 *
 */
public class TaintSourceWrapper<T extends AutoTaintLabel> {

	public void combineTaintsOnArray(Object inputArray, Taint<T> tag){
		if(tag == null) {
			return;
		}
		if(inputArray instanceof LazyArrayObjTags)
		{
			LazyArrayObjTags array = ((LazyArrayObjTags) inputArray);
			if(array.taints == null)
				array.taints = new Taint[array.getLength()];
			for(int i=0; i < array.getLength();i++)
			{
				if(array.taints[i] == null)
					array.taints[i] = tag.copy();
				else
					array.taints[i].addDependency(tag);
			}

		}else if (inputArray instanceof Object[])
		{
			//Object[]
			for(int i = 0; i < ((Object[]) inputArray).length; i++){
				Object o = ((Object[])inputArray)[i];
				if(o instanceof TaintedWithObjTag)
				{

					Taint existing = (Taint) ((TaintedWithObjTag) o).getPHOSPHOR_TAG();
					if(existing != null)
						existing.addDependency(tag);
					else
						((TaintedWithObjTag) o).setPHOSPHOR_TAG(tag.copy());
				}
			}
		}
	}

	public Taint<? extends AutoTaintLabel> generateTaint(String source) {
		StackTraceElement[] st = Thread.currentThread().getStackTrace();
		StackTraceElement[] s = new StackTraceElement[st.length - 3];
		System.arraycopy(st, 3, s, 0, s.length);
		return new Taint<AutoTaintLabel>(new AutoTaintLabel(source, s));
	}


	public Object autoTaint(Object obj, String source, int argIdx) {
	    if(obj instanceof LazyArrayObjTags) {
	        return autoTaint((LazyArrayObjTags) obj, source, argIdx);
        } else if(obj instanceof TaintedWithObjTag) {
            return autoTaint((TaintedWithObjTag) obj, source, argIdx);
        } else if(obj instanceof TaintedPrimitiveWithObjTag) {
            return autoTaint((TaintedPrimitiveWithObjTag) obj, source, argIdx);
        }
		return obj;
	}

    @SuppressWarnings("unchecked")
	public TaintedWithObjTag autoTaint(TaintedWithObjTag ret, String source, int argIdx) {
        Taint prevTag = (Taint)ret.getPHOSPHOR_TAG();
        if(prevTag != null) {
            prevTag.addDependency(generateTaint(source));
        } else {
            ret.setPHOSPHOR_TAG(generateTaint(source));
        }
        return ret;
    }

	public LazyArrayObjTags autoTaint(LazyArrayObjTags ret, String source, int argIdx) {
		setTaints(ret, source);
		return ret;
	}

	@SuppressWarnings("unchecked")
	public TaintedPrimitiveWithObjTag autoTaint(TaintedPrimitiveWithObjTag ret, String source, int argIdx) {
		if (ret.taint != null)
			ret.taint.addDependency(generateTaint(source));
		else
			ret.taint = generateTaint(source);
		return ret;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void setTaints(LazyArrayObjTags ret, String source) {
		Taint retTaint = generateTaint(source);
		Taint[] taintArray = ret.taints;
		if (taintArray != null) {
			for (int i = 0; i < taintArray.length; i++) {
				if(taintArray[i] == null)
					taintArray[i] = retTaint.copy();
				else
					taintArray[i].addDependency(retTaint);
			}
		} else {
			ret.setTaints(retTaint);
		}
	}


	public void checkTaint(int tag, Object obj, String baseSink, String actualSink) {
		if (tag != 0)
			throw new IllegalAccessError("Argument carries taint " + tag + " at " + actualSink);
	}

	public static void checkTaint(int tag) {
		if (tag != 0)
			throw new IllegalAccessError("Argument carries taint " + tag);
	}

	public Taint[] getStringValueTaints(String str) {
		if(str == null) {
			return null;
		} else {
			return((String)str).valuePHOSPHOR_TAG.taints;

		}
	}

	@SuppressWarnings("unchecked")
	public void checkTaint(Object obj, String baseSink, String actualSink) {
		if (obj == null)<commit_msg>Changed autoTaint to create a tag right after it is called from a source so that relevant StackTraceElementa are recorded if the phosphor.stacktrace property is set.<commit_after>package edu.columbia.cs.psl.phosphor.runtime;

import edu.columbia.cs.psl.phosphor.struct.*;

/**
 * This class handles dynamically doing source-based tainting.
 * 
 * If you want to replace the *value* dynamically, then: 1. extend this class 2.
 * Set Configuration.autoTainter to be an instance of your new
 * TaintSourceWrapper 3. Override autoTaint..., change the value, then call
 * super.autoTaint... in order to set the taint correctly
 * 
 * Example:
 * 
 * 
 * public TaintedIntWithObjTag autoTaint(TaintedIntWithObjTag ret, String
 * source, int argIdx) { ret.val = 100; //Change value to be 100 instead of
 * whatever it was normally ret = super.autoTaint(ret, source, argIdx); //will
 * set the taint return ret; }
 * 
 * @author jon
 *
 */
public class TaintSourceWrapper<T extends AutoTaintLabel> {

	public void combineTaintsOnArray(Object inputArray, Taint<T> tag){
		if(tag == null) {
			return;
		}
		if(inputArray instanceof LazyArrayObjTags)
		{
			LazyArrayObjTags array = ((LazyArrayObjTags) inputArray);
			if(array.taints == null)
				array.taints = new Taint[array.getLength()];
			for(int i=0; i < array.getLength();i++)
			{
				if(array.taints[i] == null)
					array.taints[i] = tag.copy();
				else
					array.taints[i].addDependency(tag);
			}

		}else if (inputArray instanceof Object[])
		{
			//Object[]
			for(int i = 0; i < ((Object[]) inputArray).length; i++){
				Object o = ((Object[])inputArray)[i];
				if(o instanceof TaintedWithObjTag)
				{

					Taint existing = (Taint) ((TaintedWithObjTag) o).getPHOSPHOR_TAG();
					if(existing != null)
						existing.addDependency(tag);
					else
						((TaintedWithObjTag) o).setPHOSPHOR_TAG(tag.copy());
				}
			}
		}
	}

	public Taint<AutoTaintLabel> generateTaint(String source) {
		StackTraceElement[] st = Thread.currentThread().getStackTrace();
		StackTraceElement[] s = new StackTraceElement[st.length - 3];
		System.arraycopy(st, 3, s, 0, s.length);
		return new Taint<>(new AutoTaintLabel(source, s));
	}

	/* Called by sources for the arguments and return value. Adds a new taint tag to the specified object. */
	public Object autoTaint(Object obj, String source, int argIdx) {
		return autoTaint(obj, source, argIdx, generateTaint(source));
	}

	/* Called by sources for the arguments and return value. Adds the specified tag to the specified object. */
	public Object autoTaint(Object obj, String source, int argIdx, Taint<? extends AutoTaintLabel> tag) {
		if(obj instanceof LazyArrayObjTags) {
			return autoTaint((LazyArrayObjTags) obj, source, argIdx, tag);
		} else if(obj instanceof TaintedWithObjTag) {
			return autoTaint((TaintedWithObjTag) obj, source, argIdx, tag);
		} else if(obj instanceof TaintedPrimitiveWithObjTag) {
			return autoTaint((TaintedPrimitiveWithObjTag) obj, source, argIdx, tag);
		}
		return obj;
	}

    @SuppressWarnings("unchecked")
	public TaintedWithObjTag autoTaint(TaintedWithObjTag ret, String source, int argIdx, Taint<? extends AutoTaintLabel> tag) {
        Taint prevTag = (Taint)ret.getPHOSPHOR_TAG();
        if(prevTag != null) {
            prevTag.addDependency(tag);
        } else {
            ret.setPHOSPHOR_TAG(tag);
        }
        return ret;
    }

	@SuppressWarnings("unchecked")
	public LazyArrayObjTags autoTaint(LazyArrayObjTags ret, String source, int argIdx, Taint<? extends AutoTaintLabel> tag) {
		Taint[] taintArray = ret.taints;
		if (taintArray != null) {
			for (int i = 0; i < taintArray.length; i++) {
				if(taintArray[i] == null)
					taintArray[i] = tag.copy();
				else
					taintArray[i].addDependency(tag);
			}
		} else {
			ret.setTaints(tag);
		}
		return ret;
	}

	@SuppressWarnings("unchecked")
	public TaintedPrimitiveWithObjTag autoTaint(TaintedPrimitiveWithObjTag ret, String source, int argIdx, Taint<? extends AutoTaintLabel> tag) {
		if (ret.taint != null)
			ret.taint.addDependency(tag);
		else
			ret.taint = tag;
		return ret;
	}

	public void checkTaint(int tag, Object obj, String baseSink, String actualSink) {
		if (tag != 0)
			throw new IllegalAccessError("Argument carries taint " + tag + " at " + actualSink);
	}

	public static void checkTaint(int tag) {
		if (tag != 0)
			throw new IllegalAccessError("Argument carries taint " + tag);
	}

	public Taint[] getStringValueTaints(String str) {
		if(str == null) {
			return null;
		} else {
			return((String)str).valuePHOSPHOR_TAG.taints;

		}
	}

	@SuppressWarnings("unchecked")
	public void checkTaint(Object obj, String baseSink, String actualSink) {
		if (obj == null)
{'fraction_non_alphanumeric': 0.11833665118336652, 'fraction_numerical': 0.0037602300376023004, 'mean_word_length': 3.797347480106101, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 15, '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': '680868', 'n_tokens_mistral': 3287, 'n_tokens_neox': 2979, 'n_words': 776}