Skip to content

Commit 8ce22c1

Browse files
AnnotationSyntax: CodeQL port of c28266 (#164)
* WIP c28266 * CodeQL port of C28266 * Remove commented-out code in AnnotationSyntax.ql * updates from review
1 parent fa6d266 commit 8ce22c1

File tree

5 files changed

+638
-0
lines changed

5 files changed

+638
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
2+
<qhelp>
3+
<overview>
4+
<p>
5+
A syntax error in the annotations was found for the property in the function.
6+
</p>
7+
</overview>
8+
<recommendation>
9+
<p>
10+
This warning indicates an error in the annotations, not in the code that is being analyzed.
11+
</p>
12+
</recommendation>
13+
<example>
14+
<p>
15+
_IRQL_saves_global_ not applied to entire function
16+
</p>
17+
<sample language="c"> <![CDATA[
18+
// FAIL
19+
VOID test1(
20+
_IRQL_saves_global_(OldIrql, *Irql) PKIRQL Irql)
21+
{
22+
// ...
23+
;
24+
}
25+
}]]>
26+
</sample>
27+
<p>
28+
_Kernel_clear_do_init_ not used with either "yes" or "no"
29+
</p>
30+
<sample language="c"> <![CDATA[
31+
// FAIL
32+
_Function_class_(DRIVER_ADD_DEVICE)
33+
_IRQL_requires_(PASSIVE_LEVEL)
34+
_IRQL_requires_same_
35+
_Kernel_clear_do_init_(IRP_MJ_CREATE)
36+
NTSTATUS
37+
test4(
38+
_In_ PDRIVER_OBJECT DriverObject,
39+
_In_ PDEVICE_OBJECT PhysicalDeviceObject)
40+
41+
{
42+
; // do nothing
43+
}
44+
}]]>
45+
</sample>
46+
</example>
47+
<semmleNotes>
48+
<p>
49+
</p>
50+
</semmleNotes>
51+
<references>
52+
<li>
53+
<a href="https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/28266-function-property-syntax-error">
54+
C28266
55+
</a>
56+
</li>
57+
</references>
58+
</qhelp>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
/**
4+
* @id cpp/drivers/annotation-syntax
5+
* @kind problem
6+
* @name Annotation syntax error
7+
* @description A syntax error in the annotations was found for the property in the function.
8+
* @platform Desktop
9+
* @feature.area Multiple
10+
* @impact Annotations
11+
* @repro.text
12+
* @owner.email: [email protected]
13+
* @opaqueid CQLD-C28266
14+
* @problem.severity warning
15+
* @precision medium
16+
* @tags correctness
17+
* @scope domainspecific
18+
* @query-version v1
19+
*/
20+
21+
import cpp
22+
import drivers.libraries.SAL
23+
24+
from SALAnnotation sa
25+
where
26+
// restoreIRQLGlobal was not on the whole function
27+
// saveIRQLGlobal was not on the whole function
28+
(
29+
sa.toString().matches("%restoresIRQLGlobal%") or //restoreIRQLGlobal //__drv_restoresIRQLGlobal //_IRQL_restores_global_
30+
sa.toString().matches("%_IRQL_saves_global_%") or //restoreIRQLGlobal //__drv_restoresIRQLGlobal //_IRQL_restores_global_
31+
sa.toString().matches("%savesIRQLGlobal%") or //saveIRQLGlobal //__drv_savesIRQLGlobal //_IRQL_saves_global_
32+
sa.toString().matches("%_IRQL_restores_global_%")
33+
) and
34+
exists(SALParameter sp | sp.getAnnotation() = sa)
35+
or
36+
(
37+
sa.toString().matches("%_When_%") or
38+
sa.toString().matches("%drv_when%")
39+
) and
40+
(
41+
//_Kernel_clear_do_init_ was not \"yes\" or \"no\"")
42+
exists(int i |
43+
sa.getUnexpandedArgument(i).toString().matches("%_Kernel_clear_do_init_%") and
44+
not sa.getUnexpandedArgument(i).toString().matches("_Kernel_clear_do_init_(%yes%)") and
45+
not sa.getUnexpandedArgument(i).toString().matches("_Kernel_clear_do_init_(%no%)")
46+
)
47+
or
48+
//__drv_dispatchType cannot be used with __drv_when
49+
exists(int i | sa.getUnexpandedArgument(i).toString().matches("%__drv_dispatchType%"))
50+
)
51+
or
52+
sa.toString().matches("%_Kernel_clear_do_init_%") and
53+
not sa.getUnexpandedArgument(0).toString().toLowerCase().matches("\"yes\"") and
54+
not sa.getUnexpandedArgument(0).toString().toLowerCase().matches("\"no\"")
55+
or
56+
//__drv_dispatch value out of range val > 63 || val < -1
57+
sa.toString().matches("%drv_dispatch%") and
58+
(
59+
sa.getUnexpandedArgument(0).toInt() > 63 or
60+
sa.getUnexpandedArgument(0).toInt() < -1
61+
)
62+
select sa, "Possible annotation syntax error"

0 commit comments

Comments
 (0)