Skip to content

Commit 9e10258

Browse files
committed
wip2
1 parent ac63637 commit 9e10258

File tree

3 files changed

+155
-153
lines changed

3 files changed

+155
-153
lines changed

rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,5 +303,6 @@ class Vec extends Struct {
303303
pragma[nomagic]
304304
Vec() { this.getCanonicalPath() = "alloc::vec::Vec" }
305305

306+
/** Gets the type parameter representing the element type. */
306307
TypeParam getElementTypeParam() { result = this.getGenericParamList().getTypeParam(0) }
307308
}

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 0 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -847,159 +847,6 @@ private predicate functionInfoBlanket(
847847
)
848848
}
849849

850-
/**
851-
* A wrapper around `IsInstantiationOf` which ensures to substitute in lookup
852-
* traits when checking whether argument types are instantiations of function
853-
* types.
854-
*/
855-
private module ArgIsInstantiationOf<
856-
HasTypeTreeSig Arg, IsInstantiationOfInputSig<Arg, FunctionType> Input>
857-
{
858-
final private class ArgFinal = Arg;
859-
860-
private class ArgSubst extends ArgFinal {
861-
Type getTypeAt(TypePath path) { result = substituteLookupTraits(super.getTypeAt(path)) }
862-
}
863-
864-
private module IsInstantiationOfInput implements IsInstantiationOfInputSig<ArgSubst, FunctionType>
865-
{
866-
pragma[nomagic]
867-
predicate potentialInstantiationOf(ArgSubst arg, TypeAbstraction abs, FunctionType constraint) {
868-
Input::potentialInstantiationOf(arg, abs, constraint)
869-
}
870-
871-
predicate relevantTypeMention(FunctionType constraint) {
872-
Input::relevantTypeMention(constraint)
873-
}
874-
}
875-
876-
predicate argIsInstantiationOf(Arg arg, ImplOrTraitItemNode i, FunctionType constraint) {
877-
IsInstantiationOf<ArgSubst, FunctionType, IsInstantiationOfInput>::isInstantiationOf(arg, i,
878-
constraint)
879-
}
880-
881-
predicate argIsNotInstantiationOf(Arg arg, ImplOrTraitItemNode i, FunctionType constraint) {
882-
IsInstantiationOf<ArgSubst, FunctionType, IsInstantiationOfInput>::isNotInstantiationOf(arg, i,
883-
constraint)
884-
}
885-
}
886-
887-
signature module ArgsAreInstantiationsOfInputSig {
888-
predicate toCheck(ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, FunctionType t);
889-
890-
class Call {
891-
string toString();
892-
893-
Location getLocation();
894-
895-
Type getArgType(FunctionTypePosition pos, TypePath path);
896-
897-
predicate hasTargetCand(ImplOrTraitItemNode i, Function f);
898-
}
899-
}
900-
901-
/**
902-
* A wrapper around `IsInstantiationOf` which ensures to substitute in lookup
903-
* traits when checking whether argument types are instantiations of function
904-
* types.
905-
*/
906-
private module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
907-
pragma[nomagic]
908-
private predicate toCheckRanked(
909-
ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, int rnk
910-
) {
911-
Input::toCheck(i, f, pos, _) and
912-
pos =
913-
rank[rnk + 1](FunctionTypePosition pos0, int j |
914-
Input::toCheck(i, f, pos0, _) and
915-
(
916-
j = pos0.asPositional()
917-
or
918-
pos0.isSelf() and j = -1
919-
or
920-
pos0.isReturn() and j = -2
921-
)
922-
|
923-
pos0 order by j
924-
)
925-
}
926-
927-
private newtype TCallAndPos =
928-
MkCallAndPos(Input::Call call, FunctionTypePosition pos) {
929-
exists(call.getArgType(pos, _))
930-
// exists(ImplOrTraitItemNode i, Function f |
931-
// Input::toCheck(i, f, pos, _) and
932-
// call.hasTargetCand(i, f)
933-
// )
934-
}
935-
936-
/** A call tagged with a position. */
937-
private class CallAndPos extends MkCallAndPos {
938-
Input::Call call;
939-
FunctionTypePosition pos;
940-
941-
CallAndPos() { this = MkCallAndPos(call, pos) }
942-
943-
Input::Call getCall() { result = call }
944-
945-
FunctionTypePosition getPos() { result = pos }
946-
947-
Location getLocation() { result = call.getLocation() }
948-
949-
Type getTypeAt(TypePath path) { result = call.getArgType(pos, path) }
950-
951-
string toString() { result = call.toString() + " [arg " + pos + "]" }
952-
}
953-
954-
private module ArgIsIsInstantiationOfInput implements
955-
IsInstantiationOfInputSig<CallAndPos, FunctionType>
956-
{
957-
pragma[nomagic]
958-
private predicate potentialInstantiationOf0(
959-
CallAndPos cp, Input::Call call, FunctionTypePosition pos, int rnk, Function f,
960-
TypeAbstraction abs, FunctionType constraint
961-
) {
962-
cp = MkCallAndPos(call, pos) and
963-
call.hasTargetCand(abs, f) and
964-
toCheckRanked(abs, f, pos, rnk) and
965-
Input::toCheck(abs, f, pos, constraint)
966-
}
967-
968-
pragma[nomagic]
969-
predicate potentialInstantiationOf(CallAndPos cp, TypeAbstraction abs, FunctionType constraint) {
970-
exists(Input::Call call, FunctionTypePosition pos, int rnk, Function f |
971-
potentialInstantiationOf0(cp, call, pos, rnk, f, abs, constraint)
972-
|
973-
rnk = 0
974-
or
975-
argsAreInstantiationsOfFromIndex(call, abs, f, rnk - 1)
976-
)
977-
}
978-
979-
predicate relevantTypeMention(FunctionType constraint) { Input::toCheck(_, _, _, constraint) }
980-
}
981-
982-
pragma[nomagic]
983-
private predicate argsAreInstantiationsOfFromIndex(
984-
Input::Call call, ImplOrTraitItemNode i, Function f, int rnk
985-
) {
986-
exists(FunctionTypePosition pos |
987-
ArgIsInstantiationOf<CallAndPos, ArgIsIsInstantiationOfInput>::argIsInstantiationOf(MkCallAndPos(call,
988-
pos), i, _) and
989-
call.hasTargetCand(i, f) and
990-
toCheckRanked(i, f, pos, rnk)
991-
)
992-
}
993-
994-
pragma[nomagic]
995-
predicate argsAreInstantiationsOf(Input::Call call, ImplOrTraitItemNode i, Function f) {
996-
exists(int rnk |
997-
argsAreInstantiationsOfFromIndex(call, i, f, rnk) and
998-
rnk = max(int r | toCheckRanked(i, f, _, r))
999-
)
1000-
}
1001-
}
1002-
1003850
/**
1004851
* Holds if the type path `path` pointing to `type` is stripped of any leading
1005852
* complex root type allowed for `self` parameters, such as `&`, `Box`, `Rc`,

rust/ql/lib/codeql/rust/internal/typeinference/FunctionType.qll

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,157 @@ Type substituteLookupTraits(Type t) {
241241
or
242242
result = TTrait(getALookupTrait(t))
243243
}
244+
245+
/**
246+
* A wrapper around `IsInstantiationOf` which ensures to substitute in lookup
247+
* traits when checking whether argument types are instantiations of function
248+
* types.
249+
*/
250+
module ArgIsInstantiationOf<HasTypeTreeSig Arg, IsInstantiationOfInputSig<Arg, FunctionType> Input> {
251+
final private class ArgFinal = Arg;
252+
253+
private class ArgSubst extends ArgFinal {
254+
Type getTypeAt(TypePath path) { result = substituteLookupTraits(super.getTypeAt(path)) }
255+
}
256+
257+
private module IsInstantiationOfInput implements IsInstantiationOfInputSig<ArgSubst, FunctionType>
258+
{
259+
pragma[nomagic]
260+
predicate potentialInstantiationOf(ArgSubst arg, TypeAbstraction abs, FunctionType constraint) {
261+
Input::potentialInstantiationOf(arg, abs, constraint)
262+
}
263+
264+
predicate relevantTypeMention(FunctionType constraint) {
265+
Input::relevantTypeMention(constraint)
266+
}
267+
}
268+
269+
predicate argIsInstantiationOf(Arg arg, ImplOrTraitItemNode i, FunctionType constraint) {
270+
IsInstantiationOf<ArgSubst, FunctionType, IsInstantiationOfInput>::isInstantiationOf(arg, i,
271+
constraint)
272+
}
273+
274+
predicate argIsNotInstantiationOf(Arg arg, ImplOrTraitItemNode i, FunctionType constraint) {
275+
IsInstantiationOf<ArgSubst, FunctionType, IsInstantiationOfInput>::isNotInstantiationOf(arg, i,
276+
constraint)
277+
}
278+
}
279+
280+
/**
281+
* Provides the input for `ArgsAreInstantiationsOf`.
282+
*/
283+
signature module ArgsAreInstantiationsOfInputSig {
284+
predicate toCheck(ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, FunctionType t);
285+
286+
class Call {
287+
string toString();
288+
289+
Location getLocation();
290+
291+
Type getArgType(FunctionTypePosition pos, TypePath path);
292+
293+
predicate hasTargetCand(ImplOrTraitItemNode i, Function f);
294+
}
295+
}
296+
297+
/**
298+
* A wrapper around `IsInstantiationOf` which ensures to substitute in lookup
299+
* traits when checking whether argument types are instantiations of function
300+
* types.
301+
*/
302+
module ArgsAreInstantiationsOf<ArgsAreInstantiationsOfInputSig Input> {
303+
pragma[nomagic]
304+
private predicate toCheckRanked(
305+
ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, int rnk
306+
) {
307+
Input::toCheck(i, f, pos, _) and
308+
pos =
309+
rank[rnk + 1](FunctionTypePosition pos0, int j |
310+
Input::toCheck(i, f, pos0, _) and
311+
(
312+
j = pos0.asPositional()
313+
or
314+
pos0.isSelf() and j = -1
315+
or
316+
pos0.isReturn() and j = -2
317+
)
318+
|
319+
pos0 order by j
320+
)
321+
}
322+
323+
private newtype TCallAndPos =
324+
MkCallAndPos(Input::Call call, FunctionTypePosition pos) {
325+
exists(call.getArgType(pos, _))
326+
// exists(ImplOrTraitItemNode i, Function f |
327+
// Input::toCheck(i, f, pos, _) and
328+
// call.hasTargetCand(i, f)
329+
// )
330+
}
331+
332+
/** A call tagged with a position. */
333+
private class CallAndPos extends MkCallAndPos {
334+
Input::Call call;
335+
FunctionTypePosition pos;
336+
337+
CallAndPos() { this = MkCallAndPos(call, pos) }
338+
339+
Input::Call getCall() { result = call }
340+
341+
FunctionTypePosition getPos() { result = pos }
342+
343+
Location getLocation() { result = call.getLocation() }
344+
345+
Type getTypeAt(TypePath path) { result = call.getArgType(pos, path) }
346+
347+
string toString() { result = call.toString() + " [arg " + pos + "]" }
348+
}
349+
350+
private module ArgIsIsInstantiationOfInput implements
351+
IsInstantiationOfInputSig<CallAndPos, FunctionType>
352+
{
353+
pragma[nomagic]
354+
private predicate potentialInstantiationOf0(
355+
CallAndPos cp, Input::Call call, FunctionTypePosition pos, int rnk, Function f,
356+
TypeAbstraction abs, FunctionType constraint
357+
) {
358+
cp = MkCallAndPos(call, pos) and
359+
call.hasTargetCand(abs, f) and
360+
toCheckRanked(abs, f, pos, rnk) and
361+
Input::toCheck(abs, f, pos, constraint)
362+
}
363+
364+
pragma[nomagic]
365+
predicate potentialInstantiationOf(CallAndPos cp, TypeAbstraction abs, FunctionType constraint) {
366+
exists(Input::Call call, FunctionTypePosition pos, int rnk, Function f |
367+
potentialInstantiationOf0(cp, call, pos, rnk, f, abs, constraint)
368+
|
369+
rnk = 0
370+
or
371+
argsAreInstantiationsOfFromIndex(call, abs, f, rnk - 1)
372+
)
373+
}
374+
375+
predicate relevantTypeMention(FunctionType constraint) { Input::toCheck(_, _, _, constraint) }
376+
}
377+
378+
pragma[nomagic]
379+
private predicate argsAreInstantiationsOfFromIndex(
380+
Input::Call call, ImplOrTraitItemNode i, Function f, int rnk
381+
) {
382+
exists(FunctionTypePosition pos |
383+
ArgIsInstantiationOf<CallAndPos, ArgIsIsInstantiationOfInput>::argIsInstantiationOf(MkCallAndPos(call,
384+
pos), i, _) and
385+
call.hasTargetCand(i, f) and
386+
toCheckRanked(i, f, pos, rnk)
387+
)
388+
}
389+
390+
pragma[nomagic]
391+
predicate argsAreInstantiationsOf(Input::Call call, ImplOrTraitItemNode i, Function f) {
392+
exists(int rnk |
393+
argsAreInstantiationsOfFromIndex(call, i, f, rnk) and
394+
rnk = max(int r | toCheckRanked(i, f, _, r))
395+
)
396+
}
397+
}

0 commit comments

Comments
 (0)