Skip to content

Commit 312bc52

Browse files
author
Dilan Bhalla
committed
adding msft open source tests
1 parent b0c11b5 commit 312bc52

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2312
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
| test.c:29:6:29:46 | ... && ... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.c:29:15:29:30 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.c:29:6:29:46 | ... && ... | as an operand in a binary logical operation |
2+
| test.c:34:6:34:38 | ! ... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.c:34:7:34:22 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.c:34:6:34:38 | ! ... | as an operand in an unary logical operation |
3+
| test.c:39:6:39:21 | call to RtlCompareMemory | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.c:39:6:39:21 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.c:39:6:39:21 | call to RtlCompareMemory | as the controlling expression in an If statement |
4+
| test.c:49:6:49:42 | ... == ... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.c:49:11:49:26 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.c:49:6:49:42 | ... == ... | as an operand in an equality operation where the other operand is likely a boolean value (lower precision result, needs to be reviewed) |
5+
| test.c:75:6:75:37 | (bool)... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.c:75:6:75:21 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.c:75:6:75:37 | (bool)... | as a boolean |
6+
| test.c:77:6:77:46 | ... == ... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.c:77:15:77:30 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.c:77:6:77:46 | ... == ... | as an operand in an equality operation where the other operand is a boolean value (high precision result) |
7+
| test.c:84:6:84:37 | (BOOLEAN)... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.c:84:6:84:21 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.c:84:6:84:37 | (BOOLEAN)... | as a boolean |
8+
| test.c:86:6:86:45 | ... == ... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.c:86:14:86:29 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.c:86:6:86:45 | ... == ... | as an operand in an equality operation where the other operand is a boolean value (high precision result) |
9+
| test.c:91:9:91:52 | ... && ... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.c:91:21:91:36 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.c:91:9:91:52 | ... && ... | as an operand in a binary logical operation |
10+
| test.cpp:18:6:18:46 | ... && ... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.cpp:18:15:18:30 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.cpp:18:6:18:46 | ... && ... | as an operand in a binary logical operation |
11+
| test.cpp:18:15:18:46 | (bool)... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.cpp:18:15:18:30 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.cpp:18:15:18:46 | (bool)... | as a boolean |
12+
| test.cpp:23:6:23:38 | ! ... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.cpp:23:7:23:22 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.cpp:23:6:23:38 | ! ... | as an operand in an unary logical operation |
13+
| test.cpp:23:7:23:38 | (bool)... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.cpp:23:7:23:22 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.cpp:23:7:23:38 | (bool)... | as a boolean |
14+
| test.cpp:28:9:28:52 | ... && ... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.cpp:28:21:28:36 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.cpp:28:9:28:52 | ... && ... | as an operand in a binary logical operation |
15+
| test.cpp:28:21:28:52 | (bool)... | This $@ is being handled $@ instead of the number of matching bytes. Please review the usage of this function and consider replacing it with `RtlEqualMemory`. | test.cpp:28:21:28:36 | call to RtlCompareMemory | call to `RtlCompareMemory` | test.cpp:28:21:28:52 | (bool)... | as a boolean |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Microsoft/Likely Bugs/Drivers/IncorrectUsageOfRtlCompareMemory.ql
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// semmle-extractor-options: --microsoft
2+
typedef unsigned __int64 size_t;
3+
4+
size_t RtlCompareMemory(
5+
const void* Source1,
6+
const void* Source2,
7+
size_t Length
8+
)
9+
{
10+
return Length;
11+
}
12+
13+
14+
#define bool _Bool
15+
#define false 0
16+
#define true 1
17+
18+
typedef unsigned char UCHAR;
19+
typedef UCHAR BOOLEAN; // winnt
20+
#define FALSE 0
21+
#define TRUE 1
22+
23+
int Test(const void* ptr)
24+
{
25+
size_t t = RtlCompareMemory("test", ptr, 5); //OK
26+
bool x;
27+
BOOLEAN y;
28+
29+
if (t > 0 && RtlCompareMemory("test", ptr, 5)) //bug
30+
{
31+
t++;
32+
}
33+
34+
if (!RtlCompareMemory("test", ptr, 4)) //bug
35+
{
36+
t--;
37+
}
38+
39+
if (RtlCompareMemory("test", ptr, 4)) //bug
40+
{
41+
t--;
42+
}
43+
44+
if (6 == RtlCompareMemory("test", ptr, 4)) //OK
45+
{
46+
t++;
47+
}
48+
49+
if (0 == RtlCompareMemory("test", ptr, 4)) // potentially a bug (lower precision)
50+
{
51+
t++;
52+
}
53+
54+
if (6 == RtlCompareMemory("test", ptr, 4) + 1) //OK
55+
{
56+
t++;
57+
}
58+
59+
if (0 == RtlCompareMemory("test", ptr, 4) + 1) // OK
60+
{
61+
t++;
62+
}
63+
64+
switch (RtlCompareMemory("test", ptr, 4))
65+
{
66+
case 1:
67+
t--;
68+
break;
69+
default:
70+
t++;
71+
}
72+
73+
/// _Bool
74+
75+
x = RtlCompareMemory("test", ptr, 4); // bug
76+
77+
if (false == RtlCompareMemory("test", ptr, 4)) // bug
78+
{
79+
t++;
80+
}
81+
82+
// BOOLEAN
83+
84+
y = RtlCompareMemory("test", ptr, 4); // bug
85+
86+
if (TRUE == RtlCompareMemory("test", ptr, 4)) // bug
87+
{
88+
t++;
89+
}
90+
91+
return (t == 5) && RtlCompareMemory("test", ptr, 5); //bug
92+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// semmle-extractor-options: --microsoft
2+
typedef unsigned __int64 size_t;
3+
4+
size_t RtlCompareMemory(
5+
const void* Source1,
6+
const void* Source2,
7+
size_t Length
8+
)
9+
{
10+
return Length;
11+
}
12+
13+
14+
bool Test(const void* ptr)
15+
{
16+
size_t t = RtlCompareMemory("test", ptr, 5); //OK
17+
18+
if (t > 0 && RtlCompareMemory("test", ptr, 5)) //bug
19+
{
20+
t++;
21+
}
22+
23+
if (!RtlCompareMemory("test", ptr, 4)) //bug
24+
{
25+
t--;
26+
}
27+
28+
return (t == 5) && RtlCompareMemory("test", ptr, 5); //bug
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
| test.c:86:6:86:29 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.c:86:6:86:29 | sizeof(<expr>) | binary operator | test.c:64:6:64:11 | Test01 | Usage | test.c:86:13:86:28 | ... / ... | binary operator |
2+
| test.c:93:6:93:30 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.c:93:6:93:30 | sizeof(<expr>) | binary operator | test.c:64:6:64:11 | Test01 | Usage | test.c:93:13:93:29 | ... * ... | binary operator |
3+
| test.c:95:6:95:35 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.c:95:6:95:35 | sizeof(<expr>) | binary operator | test.c:64:6:64:11 | Test01 | Usage | test.c:95:13:95:34 | ... * ... | binary operator |
4+
| test.c:98:6:98:31 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.c:98:6:98:31 | sizeof(<expr>) | sizeof | test.c:64:6:64:11 | Test01 | Usage | test.c:98:13:98:30 | sizeof(int) | sizeof |
5+
| test.c:116:6:116:24 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.c:116:6:116:24 | sizeof(<expr>) | sizeof | test.c:64:6:64:11 | Test01 | Usage | test.c:116:13:116:23 | sizeof(int) | sizeof |
6+
| test.c:117:6:117:18 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.c:117:6:117:18 | sizeof(<expr>) | binary operator | test.c:64:6:64:11 | Test01 | Usage | test.c:117:13:117:17 | ... + ... | binary operator |
7+
| test.cpp:89:6:89:29 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:89:6:89:29 | sizeof(<expr>) | binary operator | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:89:13:89:28 | ... / ... | binary operator |
8+
| test.cpp:96:6:96:30 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:96:6:96:30 | sizeof(<expr>) | binary operator | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:96:13:96:29 | ... * ... | binary operator |
9+
| test.cpp:98:6:98:35 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:98:6:98:35 | sizeof(<expr>) | binary operator | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:98:13:98:34 | ... * ... | binary operator |
10+
| test.cpp:101:6:101:31 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:101:6:101:31 | sizeof(<expr>) | sizeof | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:101:13:101:30 | sizeof(int) | sizeof |
11+
| test.cpp:120:6:120:24 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:120:6:120:24 | sizeof(<expr>) | sizeof | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:120:13:120:23 | sizeof(int) | sizeof |
12+
| test.cpp:121:6:121:18 | sizeof(<expr>) | $@: $@ of $@ inside sizeof. | test.cpp:121:6:121:18 | sizeof(<expr>) | binary operator | test.cpp:66:6:66:11 | Test01 | Usage | test.cpp:121:13:121:17 | ... + ... | binary operator |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Microsoft/Likely Bugs/SizeOfMisuse/ArgumentIsSizeofOrOperation.ql
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
| test2.c:72:6:72:42 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.c:72:6:72:42 | sizeof(<expr>) | Test01 | test2.c:46:1:46:48 | #define SOMESTRUCT_ERRNO_THAT_MATTERS 0x8000000d | SOMESTRUCT_ERRNO_THAT_MATTERS |
2+
| test2.c:80:10:80:32 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.c:80:10:80:32 | sizeof(<expr>) | Test01 | test2.c:2:1:2:26 | #define BAD_MACRO_CONST 5l | BAD_MACRO_CONST |
3+
| test2.c:81:6:81:29 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.c:81:6:81:29 | sizeof(<expr>) | Test01 | test2.c:3:1:3:35 | #define BAD_MACRO_CONST2 0x80005001 | BAD_MACRO_CONST2 |
4+
| test2.c:89:7:89:35 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.c:89:7:89:35 | sizeof(<expr>) | Test01 | test2.c:1:1:1:19 | #define PAGESIZE 64 | PAGESIZE |
5+
| test2.c:98:6:98:31 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.c:98:6:98:31 | sizeof(<expr>) | Test01 | test2.c:17:1:17:40 | #define SOME_SIZEOF_MACRO2 (sizeof(int)) | SOME_SIZEOF_MACRO2 |
6+
| test2.c:112:6:112:37 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.c:112:6:112:37 | sizeof(<expr>) | Test01 | test2.c:31:1:31:45 | #define ACE_CONDITION_SIGNATURE2 'xt' | ACE_CONDITION_SIGNATURE2 |
7+
| test2.cpp:75:6:75:42 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.cpp:75:6:75:42 | sizeof(<expr>) | Test01 | test2.cpp:48:1:48:48 | #define SOMESTRUCT_ERRNO_THAT_MATTERS 0x8000000d | SOMESTRUCT_ERRNO_THAT_MATTERS |
8+
| test2.cpp:83:10:83:32 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.cpp:83:10:83:32 | sizeof(<expr>) | Test01 | test2.cpp:2:1:2:26 | #define BAD_MACRO_CONST 5l | BAD_MACRO_CONST |
9+
| test2.cpp:84:6:84:29 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.cpp:84:6:84:29 | sizeof(<expr>) | Test01 | test2.cpp:3:1:3:35 | #define BAD_MACRO_CONST2 0x80005001 | BAD_MACRO_CONST2 |
10+
| test2.cpp:92:7:92:35 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.cpp:92:7:92:35 | sizeof(<expr>) | Test01 | test2.cpp:1:1:1:19 | #define PAGESIZE 64 | PAGESIZE |
11+
| test2.cpp:101:6:101:31 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.cpp:101:6:101:31 | sizeof(<expr>) | Test01 | test2.cpp:17:1:17:40 | #define SOME_SIZEOF_MACRO2 (sizeof(int)) | SOME_SIZEOF_MACRO2 |
12+
| test2.cpp:116:6:116:37 | sizeof(<expr>) | $@: sizeof of integer macro $@ will always return the size of the underlying integer type. | test2.cpp:116:6:116:37 | sizeof(<expr>) | Test01 | test2.cpp:32:1:32:45 | #define ACE_CONDITION_SIGNATURE2 'xt' | ACE_CONDITION_SIGNATURE2 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Microsoft/Likely Bugs/SizeOfMisuse/SizeOfConstIntMacro.ql
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#define PAGESIZE 64
2+
#define BAD_MACRO_CONST 5l
3+
#define BAD_MACRO_CONST2 0x80005001
4+
#define BAD_MACRO_OP1(VAR) strlen(#VAR)
5+
#define BAD_MACRO_OP2(VAR) sizeof(VAR)/sizeof(int)
6+
7+
long strlen(const char* x) { return 5; }
8+
9+
#define ALIGN_UP_BY(length, sizeofType) length * sizeofType
10+
#define ALIGN_UP(length, type) \
11+
ALIGN_UP_BY(length, sizeof(type))
12+
13+
#define SOME_SIZEOF_MACRO (sizeof(int) * 3)
14+
#define SOME_SIZEOF_MACRO_CAST ((char)(sizeof(int) * 3))
15+
16+
17+
#define SOME_SIZEOF_MACRO2 (sizeof(int))
18+
19+
typedef unsigned short WCHAR; // wc, 16-bit UNICODE character
20+
21+
#define UNICODE_NULL1 ((WCHAR)0)
22+
23+
#define ASCII_NULL ((char)0)
24+
25+
#define UNICODE_STRING_SIG L"xtra"
26+
#define ASCII_STRING_SIG "xtra"
27+
28+
#define UNICODE_SIG L'x'
29+
30+
#define ACE_CONDITION_SIGNATURE1 'xtra'
31+
#define ACE_CONDITION_SIGNATURE2 'xt'
32+
33+
#define ACE_CONDITION_SIGNATURE3(VAL) #VAL
34+
35+
#define NULL (void *)0
36+
37+
#define EFI_FILEPATH_SEPARATOR_UNICODE L'\\'
38+
39+
const char* Test()
40+
{
41+
return "foobar";
42+
}
43+
44+
#define FUNCTION_MACRO_OP1 Test()
45+
46+
#define SOMESTRUCT_ERRNO_THAT_MATTERS 0x8000000d
47+
48+
49+
char _RTL_CONSTANT_STRING_type_check(const void* s) {
50+
return ((char*)(s))[0];
51+
}
52+
53+
#define RTL_CONSTANT_STRING(s) \
54+
{ \
55+
sizeof( s ) - sizeof( (s)[0] ); \
56+
sizeof( s ) / sizeof(_RTL_CONSTANT_STRING_type_check(s)); \
57+
}
58+
59+
typedef struct {
60+
int a;
61+
char b;
62+
} SOMESTRUCT_THAT_MATTERS;
63+
64+
void Test01() {
65+
66+
RTL_CONSTANT_STRING("hello");
67+
68+
sizeof(NULL);
69+
sizeof(EFI_FILEPATH_SEPARATOR_UNICODE);
70+
71+
int y = sizeof(SOMESTRUCT_THAT_MATTERS);
72+
y = sizeof(SOMESTRUCT_ERRNO_THAT_MATTERS); // BUG: SizeOfConstIntMacro
73+
74+
const unsigned short* p = UNICODE_STRING_SIG;
75+
const char* p2 = ASCII_STRING_SIG;
76+
char p3 = 'xtra';
77+
unsigned short p4 = L'xtra';
78+
79+
int a[10];
80+
int x = sizeof(BAD_MACRO_CONST); //BUG: SizeOfConstIntMacro
81+
x = sizeof(BAD_MACRO_CONST2); //BUG: SizeOfConstIntMacro
82+
83+
x = sizeof(FUNCTION_MACRO_OP1); // GOOD
84+
85+
x = sizeof(BAD_MACRO_OP1(a)); //BUG: ArgumentIsFunctionCall (Low Prec)
86+
x = sizeof(BAD_MACRO_OP2(a)); //BUG: ArgumentIsSizeofOrOperation
87+
88+
x = 0;
89+
x += ALIGN_UP(sizeof(a), PAGESIZE); //BUG: SizeOfConstIntMacro
90+
x += ALIGN_UP_BY(sizeof(a), PAGESIZE); // GOOD
91+
92+
x = SOME_SIZEOF_MACRO * 3; // GOOD
93+
x = sizeof(SOME_SIZEOF_MACRO) * 3; //BUG: ArgumentIsSizeofOrOperation
94+
95+
x = sizeof(SOME_SIZEOF_MACRO_CAST) * 3; //BUG: ArgumentIsSizeofOrOperation
96+
97+
x = SOME_SIZEOF_MACRO2; // GOOD
98+
x = sizeof(SOME_SIZEOF_MACRO2); //BUG: SizeOfConstIntMacro, ArgumentIsSizeofOrOperation
99+
100+
x = sizeof(a) / sizeof(int); // GOOD
101+
102+
x = sizeof(UNICODE_NULL1);
103+
104+
x = sizeof(ASCII_NULL);
105+
106+
x = sizeof(UNICODE_STRING_SIG);
107+
x = sizeof(ASCII_STRING_SIG);
108+
109+
x = sizeof(UNICODE_SIG);
110+
111+
x = sizeof(ACE_CONDITION_SIGNATURE1); // GOOD (special case)
112+
x = sizeof(ACE_CONDITION_SIGNATURE2); // BUG: SizeOfConstIntMacro
113+
114+
x = sizeof(ACE_CONDITION_SIGNATURE3(xtra));
115+
116+
x = sizeof(sizeof(int)); // BUG: ArgumentIsSizeofOrOperation
117+
x = sizeof(3 + 2); // BUg: ArgumentIsSizeofOrOperation
118+
}

0 commit comments

Comments
 (0)