@@ -1975,12 +1975,48 @@ module BarrierGuard<guardChecksSig/3 guardChecks> {
19751975 * ```
19761976 * will block flow from `x = source()` to `sink(x)`.
19771977 *
1978- * NOTE: If an non-indirect expression is tracked, use `getABarrierNode` instead.
1978+ * NOTE: If a non-indirect expression is tracked, use `getABarrierNode` instead.
19791979 */
1980- IndirectExprNode getAnIndirectBarrierNode ( ) {
1980+ IndirectExprNode getAnIndirectBarrierNode ( ) { result = getAnIndirectBarrierNode ( _) }
1981+
1982+ /**
1983+ * Gets an indirect expression node with indirection index `indirectionIndex` that is
1984+ * safely guarded by the given guard check.
1985+ *
1986+ * For example, given the following code:
1987+ * ```cpp
1988+ * int* p;
1989+ * // ...
1990+ * *p = source();
1991+ * if(is_safe_pointer(p)) {
1992+ * sink(*p);
1993+ * }
1994+ * ```
1995+ * and the following barrier guard check:
1996+ * ```ql
1997+ * predicate myGuardChecks(IRGuardCondition g, Expr e, boolean branch) {
1998+ * exists(Call call |
1999+ * g.getUnconvertedResultExpression() = call and
2000+ * call.getTarget().hasName("is_safe_pointer") and
2001+ * e = call.getAnArgument() and
2002+ * branch = true
2003+ * )
2004+ * }
2005+ * ```
2006+ * implementing `isBarrier` as:
2007+ * ```ql
2008+ * predicate isBarrier(DataFlow::Node barrier) {
2009+ * barrier = DataFlow::BarrierGuard<myGuardChecks/3>::getAnIndirectBarrierNode(1)
2010+ * }
2011+ * ```
2012+ * will block flow from `x = source()` to `sink(x)`.
2013+ *
2014+ * NOTE: If a non-indirect expression is tracked, use `getABarrierNode` instead.
2015+ */
2016+ IndirectExprNode getAnIndirectBarrierNode ( int indirectionIndex ) {
19812017 exists ( IRGuardCondition g , Expr e , ValueNumber value , boolean edge |
19822018 e = value .getAnInstruction ( ) .getConvertedResultExpression ( ) and
1983- result .getConvertedExpr ( _ ) = e and
2019+ result .getConvertedExpr ( indirectionIndex ) = e and
19842020 guardChecks ( g , value .getAnInstruction ( ) .getConvertedResultExpression ( ) , edge ) and
19852021 g .controls ( result .getBasicBlock ( ) , edge )
19862022 )
0 commit comments