Skip to content

Commit 77fb9a1

Browse files
MultipleFunctionClassAnnotations: Port of C28177 (#168)
* Port of C28177 * Update src/drivers/general/queries/MultipleFunctionClassAnnotations/driver_snippet.c Co-authored-by: NateD-MSFT <[email protected]> Signed-off-by: Jacob Ronstadt <[email protected]> * Update example description in qhelp file --------- Signed-off-by: Jacob Ronstadt <[email protected]> Co-authored-by: NateD-MSFT <[email protected]>
1 parent eeaf255 commit 77fb9a1

File tree

4 files changed

+471
-0
lines changed

4 files changed

+471
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
2+
<qhelp>
3+
<overview>
4+
<p>
5+
Function is annotated with more than one function class. All but one will be ignored.
6+
</p>
7+
</overview>
8+
<recommendation>
9+
<p>
10+
This warning can be generated when there is a chain of typedefs. Only use one function class annotation.
11+
</p>
12+
</recommendation>
13+
<example>
14+
<p>
15+
Example function with multiple __drv_functionClass annotations
16+
</p>
17+
<sample language="c"> <![CDATA[
18+
__drv_functionClass(FAKE_DRIVER_ADD_DEVICE)
19+
__drv_functionClass(FAKE_DRIVER_ADD_DEVICE2)
20+
__drv_maxFunctionIRQL(PASSIVE_LEVEL)
21+
__drv_requiresIRQL(PASSIVE_LEVEL)
22+
__drv_sameIRQL
23+
__drv_when(return >= 0, __drv_clearDoInit(yes)) typedef NTSTATUS
24+
FAKE_DRIVER_ADD_DEVICE(
25+
__in struct _DRIVER_OBJECT *DriverObject,
26+
__in struct _DEVICE_OBJECT *PhysicalDeviceObject);
27+
28+
typedef FAKE_DRIVER_ADD_DEVICE *PDRIVER_ADD_DEVICE;
29+
30+
FAKE_DRIVER_ADD_DEVICE FakeDriverAddDevice;
31+
32+
_Use_decl_annotations_
33+
NTSTATUS
34+
FakeDriverAddDevice(
35+
__in struct _DRIVER_OBJECT *DriverObject,
36+
__in struct _DEVICE_OBJECT *PhysicalDeviceObject)
37+
{
38+
return STATUS_SUCCESS;
39+
}
40+
}]]>
41+
</sample>
42+
43+
</example>
44+
<semmleNotes>
45+
<p>
46+
47+
</p>
48+
</semmleNotes>
49+
<references>
50+
<li>
51+
<a href="https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/28177-function-annotated-with-more-than-one-class">
52+
C28177
53+
</a>
54+
</li>
55+
</references>
56+
</qhelp>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
/**
4+
* @id cpp/drivers/multiple-function-class-annotations
5+
* @kind problem
6+
* @name Multiple Function Class Annotations
7+
* @description Function is annotated with more than one function class. All but one will be ignored.
8+
* @platform Desktop
9+
* @feature.area Multiple
10+
* @impact Insecure Coding Practice
11+
* @repro.text This warning can be generated when there is a chain of typedefs.
12+
* @owner.email: [email protected]
13+
* @opaqueid CQLD-c28177
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+
class FunctionClassAnnotatedTypedef extends TypedefType {
25+
FunctionClassAnnotation funcAnnotation;
26+
27+
FunctionClassAnnotatedTypedef() { funcAnnotation.getTypedefDeclarations() = this }
28+
29+
FunctionClassAnnotation getFuncClassAnnotation() { result = funcAnnotation }
30+
}
31+
32+
class FunctionClassAnnotation extends SALAnnotation {
33+
string annotationName;
34+
35+
FunctionClassAnnotation() {
36+
this.getMacroName() = ["__drv_functionClass", "_Function_class_"] and
37+
annotationName = this.getMacroName()
38+
}
39+
}
40+
41+
class AnnotatedFunction extends Function {
42+
FunctionClassAnnotation funcClassAnnotation;
43+
44+
AnnotatedFunction() {
45+
funcClassAnnotation.getMacroName() = ["__drv_functionClass", "_Function_class_"] and
46+
exists(FunctionDeclarationEntry fde |
47+
fde = this.getADeclarationEntry() and
48+
funcClassAnnotation.getDeclarationEntry() = fde
49+
)
50+
or
51+
exists(FunctionDeclarationEntry fde |
52+
fde.getFunction() = this and
53+
fde.getTypedefType().(FunctionClassAnnotatedTypedef).getFuncClassAnnotation() =
54+
funcClassAnnotation
55+
)
56+
}
57+
58+
FunctionClassAnnotation getFuncClassAnnotation() { result = funcClassAnnotation }
59+
}
60+
61+
from AnnotatedFunction f
62+
where
63+
count(f.getFuncClassAnnotation() ) > 1
64+
select f, "Function is annotated with more than one function class. All but one will be ignored."

0 commit comments

Comments
 (0)