Skip to content

Commit c0a3640

Browse files
Add qtil.go
1 parent 68e245c commit c0a3640

19 files changed

Lines changed: 229 additions & 0 deletions

go/src/codeql-pack.lock.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
lockVersion: 1.0.0
3+
dependencies:
4+
codeql/dataflow:
5+
version: 1.1.6
6+
codeql/go-all:
7+
version: 2.1.3
8+
codeql/mad:
9+
version: 1.0.12
10+
codeql/ssa:
11+
version: 1.0.12
12+
codeql/threat-models:
13+
version: 1.0.12
14+
codeql/tutorial:
15+
version: 1.0.12
16+
codeql/typetracking:
17+
version: 1.0.12
18+
codeql/util:
19+
version: 1.0.12
20+
compiled: false

go/src/qlpack.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: mfairhurst/qtil-go
2+
library: true
3+
warnOnImplicitThis: false
4+
version: 0.0.1
5+
license: MIT
6+
dependencies:
7+
codeql/go-all: '>=0.0.1 <5.0.0'
8+
mfairhurst/qtil: 0.0.1

go/src/qtil/Go.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Qtil {
2+
private import qtil.Qtil as Common
3+
// Importing qtil.Go should import all of Qtil.
4+
import Common::Qtil
5+
import qtil.go.ast.TwoOperands
6+
import qtil.go.format.QlFormat
7+
import qtil.go.graph.CustomPathProblem
8+
}

go/src/qtil/go/ast/TwoOperands.qll

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import go as go
2+
import qtil.parameterization.SignatureTypes
3+
4+
/**
5+
* A module for dealing with pairs of exclusive operands in C++ ASTs.
6+
*
7+
* For instance, to find cases where one operand is an integer and the other is a constant, you
8+
* will want to to perform checks on each operand separately and consistently without worrying about
9+
* order. This module makes this common pattern easy to implement.
10+
*
11+
* This module takes two type parameters:
12+
* - `Operand`: the type of the operands (e.g. `Expr`)
13+
* - `HasOperands`: a type that has operands of type `Operand` (e.g. `BinaryExpr`)
14+
*
15+
* ```ql
16+
* // Using this module:
17+
* predicate myBinaryTestNew(BinaryExpr e) {
18+
* exists(TwoOperands<BinaryExpr>::Set set |
19+
* set.getOperation() = e and
20+
* set.someOperand().isInteger() and
21+
* set.otherOperand().isConstant()
22+
* )
23+
* }
24+
*
25+
* // Is roughly equivalent to:
26+
* predicate myBinaryTestOld(BinaryExpr e) {
27+
* exists(Expr a, Expr b |
28+
* e.getAnOperand() = a and
29+
* e.getAnOperand() = b and
30+
* a != b and
31+
* a.isInteger() and
32+
* b.isConstant()
33+
* )
34+
* }
35+
* ```
36+
*
37+
* Some caution about using this module: for each use, two `Set` objects exst. If you do not
38+
* properly constrain the usage of `someOperand()` and `otherOperand()`, then these members could
39+
* hold for different `Set`s. Therefore, `someOperand()` and `otherOperand()` may be the same
40+
* operand. This will not happen if the `Set` is properly constrained across the two member
41+
* invocations.
42+
*
43+
* ```ql
44+
* predicate bug(BinaryExpr e) {
45+
* // Bad: the two sets are not constrained to the same instance, therefore the operands not
46+
* // guaranteed to be different.
47+
* TwoOperands<BinaryExpr>::getASet(e).someOperand().isInteger() and
48+
* TwoOperands<BinaryExpr>::getASet(e).otherOperand().isConstant()
49+
* }
50+
* ```
51+
*/
52+
module TwoOperands<Signature<go::BinaryExpr>::Type BinOp> {
53+
private import qtil.ast.TwoOperands as Make
54+
import Make::TwoOperands<go::Expr, BinOp>
55+
}

go/src/qtil/go/format/QlFormat.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
private import qtil.format.QLFormat
2+
private import go
3+
private import qtil.go.locations.Locatable
4+
5+
import QlFormat<DbLocation, GoLocatableConfig>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
private import qtil.locations.CustomPathProblem
2+
private import qtil.go.locations.Locatable
3+
private import go
4+
5+
// Import the Go specific configuration for making custom path problems.
6+
import PathProblem<DbLocation, GoLocatableConfig>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
private import qtil.locations.Locatable
2+
private import go as go
3+
4+
/**
5+
* A module to declare `Locatable`s specific to Go for use in other qtil modules.
6+
*/
7+
module GoLocatableConfig implements LocatableConfig<go::DbLocation> {
8+
class Locatable = go::Locatable;
9+
}

go/test/codeql-pack.lock.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
lockVersion: 1.0.0
3+
dependencies:
4+
codeql/dataflow:
5+
version: 1.1.6
6+
codeql/go-all:
7+
version: 2.1.3
8+
codeql/mad:
9+
version: 1.0.12
10+
codeql/ssa:
11+
version: 1.0.12
12+
codeql/threat-models:
13+
version: 1.0.12
14+
codeql/tutorial:
15+
version: 1.0.12
16+
codeql/typetracking:
17+
version: 1.0.12
18+
codeql/util:
19+
version: 1.0.12
20+
compiled: false

go/test/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module codeql.qtil.go.test
2+
3+
go 1.2.1

go/test/qlpack.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: mfairhurst/qtil-go-test
2+
library: true
3+
warnOnImplicitThis: false
4+
version: 0.0.1
5+
license: MIT
6+
dependencies:
7+
mfairhurst/qtil-go: "*"
8+
extractor: go

0 commit comments

Comments
 (0)