Skip to content

Commit 3285eae

Browse files
CodeQL port of C28266
1 parent 47f6665 commit 3285eae

File tree

8 files changed

+623
-274
lines changed

8 files changed

+623
-274
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
TODO notes
50+
</p>
51+
</semmleNotes>
52+
<references>
53+
<li>
54+
<a href="https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/28266-function-property-syntax-error">
55+
C28266
56+
</a>
57+
</li>
58+
</references>
59+
</qhelp>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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, SALParameter sp
25+
where
26+
// restoreIRQLGlobal was not on the whole function
27+
// saveIRQLGlobalL 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+
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+
// f.toString().matches("%drv_dispatchType%") or
57+
//__drv_dispatch value out of range val > 63 || val < -1
58+
sa.toString().matches("%drv_dispatch%") and
59+
(
60+
sa.getUnexpandedArgument(0).toInt() > 63 or
61+
sa.getUnexpandedArgument(0).toInt() < -1
62+
)
63+
select sa, "Possible annotation syntax error"

0 commit comments

Comments
 (0)