Skip to content

Commit 263b19b

Browse files
JakobL-MSFTisayrdglFredrik Orderudcgallredtristanb-ntdev
authored
RI develop to main (#1189)
* Memory leakage in message "sizeof(SCANNER_MESSAGE) * threadCount * requestCount" bytes long memory is allocated but only "sizeof(SCANNER_MESSAGE) * threadCount" bytes long of it is freed. * Handles malloc function fail case * Refactors comment line * Solves the miscalculation of the message index * simbatt: Fix broken registry read-back The GetSimBattStateFromRegistry function is currently using default settings if GetSimBattStateFromRegistry succeeds, whereas settings from registry are only applied if GetSimBattStateFromRegistry fails. This does not make sense to me. Therefore proposing to remove the `!` negation from `if (!NT_SUCCESS(Status)) {` on the line after `Status = GetSimBattStateFromRegistry(Device, RegState);` so that default settings are loaded when registry read-back fails. * Issue of freeing memory without waiting completion of threads accessing it is fixed * Fixes information leakage warning Fixes the issue of possible information leakage from uninitialized padding bytes * Avoids possible multiplication overflow warning * Avoids possible multiplication overflow warning * CI Pipelines build with WDK Nuget Packages (#1179) Integrate nuget into the workflow pipelines * FI from main to develop (#1188) Fix text and minor issues in Winget configuration files Co-authored-by: Adonais Romero Gonzalez <adonais.neoadonis@gmail.com> * Improve version info, vsix installation, and update building locally readme * Fix printing vsix version * Refactored vsix install and cleaned up build sampleset --------- Co-authored-by: İsa Yurdagül <38290414+isayrdgl@users.noreply.github.com> Co-authored-by: Fredrik Orderud <fredrik.orderud@ge.com> Co-authored-by: Christian Allred <13487734+cgallred@users.noreply.github.com> Co-authored-by: tristanb-ntdev <60945150+tristanb-ntdev@users.noreply.github.com> Co-authored-by: Matt <138825652+middlemose@users.noreply.github.com> Co-authored-by: Adonais Romero Gonzalez <adonais.neoadonis@gmail.com>
1 parent 41b5bca commit 263b19b

8 files changed

Lines changed: 151 additions & 77 deletions

File tree

.github/scripts/Install-Vsix.ps1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<#
2+
3+
.SYNOPSIS
4+
Download and install the latest WDK VSIX.
5+
6+
#>
7+
8+
# set uri by resolving amd64 vsix
9+
$uri = "https://marketplace.visualstudio.com$((Invoke-WebRequest -Uri "https://marketplace.visualstudio.com/items?itemName=DriverDeveloperKits-WDK.WDKVsix").Links | Where-Object outerHTML -like '*(amd64)*' | select -expand href)"
10+
11+
# set download version
12+
$uri_version = ([regex]'(\d+\.)(\d+\.)(\d+\.)(\d+)').Matches($uri).Value
13+
14+
# set msbuild path
15+
$msbuild_path = (Resolve-Path "$env:ProgramFiles\Microsoft Visual Studio\2022\*\MSBuild\")
16+
17+
# download vsix, expand, and store the downloaded version extracted from the extension manifest
18+
"Downloading WDK VSIX version: $uri_version..."
19+
Invoke-WebRequest -Uri "$uri" -OutFile wdk.zip
20+
"Expanding WDK VSIX archive..."
21+
Expand-Archive ".\wdk.zip" .\
22+
"Extracting version from manifest..."
23+
$downloaded_version = ([xml](Get-Content .\extension.vsixmanifest)).PackageManifest.Metadata.Identity.Version
24+
"Downloaded WDK VSIX version: $downloaded_version"
25+
26+
# copy msbuild files, extension manifest, and check installed version from the extension manifest
27+
"Copying WDK extension files to build path..."
28+
cp (".\`$MSBuild\*", ".\extension.vsixmanifest") "$msbuild_path" -Recurse -Force
29+
"Extracting version from copied manifest..."
30+
$installed_version = ([xml](Get-Content ${msbuild_path}\extension.vsixmanifest)).PackageManifest.Metadata.Identity.Version
31+
"Installed WDK VSIX Version: $installed_version"
32+
if (-not ("$downloaded_version" -eq "$installed_version")) {
33+
"WDK VSIX installation failed due to version mismatch"
34+
exit 1
35+
}
36+
37+
# set github environment variable for vsix version
38+
"SAMPLES_VSIX_VERSION=$installed_version" | Out-File -FilePath "$env:GITHUB_ENV" -Append

.github/workflows/Code-Scanning.yml

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,31 @@ jobs:
3232
language: [ 'cpp' ]
3333

3434
steps:
35-
- name: Checkout repository
36-
uses: actions/checkout@v4
37-
with:
38-
submodules: 'recursive'
39-
40-
- name: Initialize CodeQL
41-
uses: github/codeql-action/init@v3
42-
with:
43-
languages: ${{ matrix.language }}
44-
packs: microsoft/windows-drivers
45-
- name: Add MSBuild to PATH
46-
uses: microsoft/setup-msbuild@v2
47-
48-
- name: Retrieve and build all available solutions
49-
run: |
50-
.\Build-AllSamples.ps1 -Verbose -ThrottleLimit 1
51-
env:
52-
WDS_Configuration: Debug
53-
WDS_Platform: x64
54-
WDS_WipeOutputs: ${{ true }}
55-
56-
- name: Perform CodeQL analysis
57-
uses: github/codeql-action/analyze@v3
58-
with:
59-
category: "/language:${{matrix.language}}"
60-
61-
62-
35+
- name: Checkout repository
36+
uses: actions/checkout@v4
37+
with:
38+
submodules: 'recursive'
39+
40+
- name: Install WDK VSIX
41+
run: .\.github\scripts\Install-Vsix.ps1
42+
43+
- name: Install Nuget Packages
44+
run: nuget restore .\packages.config -PackagesDirectory .\packages\
45+
46+
- name: Initialize CodeQL
47+
uses: github/codeql-action/init@v3
48+
with:
49+
languages: ${{ matrix.language }}
50+
packs: microsoft/windows-drivers
51+
52+
- name: Retrieve and build all available solutions
53+
run: .\Build-AllSamples.ps1 -Verbose -ThrottleLimit 1
54+
env:
55+
WDS_Configuration: Debug
56+
WDS_Platform: x64
57+
WDS_WipeOutputs: ${{ true }}
58+
59+
- name: Perform CodeQL analysis
60+
uses: github/codeql-action/analyze@v3
61+
with:
62+
category: "/language:${{matrix.language}}"

.github/workflows/ci-pr.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ jobs:
2222
with:
2323
submodules: 'recursive'
2424

25-
- name: Add MSBuild to PATH
26-
uses: microsoft/setup-msbuild@v2
25+
- name: Install WDK VSIX
26+
run: .\.github\scripts\Install-Vsix.ps1
27+
28+
- name: Install Nuget Packages
29+
run: nuget restore .\packages.config -PackagesDirectory .\packages\
2730

2831
- name: Get changed files
2932
id: get-changed-files
@@ -63,8 +66,7 @@ jobs:
6366
path: _logs
6467

6568
- name: Join and generate global reports
66-
run: |
67-
.\.github\scripts\Join-CsvReports.ps1
69+
run: .\.github\scripts\Join-CsvReports.ps1
6870

6971
- name: Archive global overview build reports
7072
uses: actions/upload-artifact@v3

.github/workflows/ci.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ jobs:
2222
with:
2323
submodules: 'recursive'
2424

25-
- name: Add MSBuild to PATH
26-
uses: microsoft/setup-msbuild@v2
25+
- name: Install WDK VSIX
26+
run: .\.github\scripts\Install-Vsix.ps1
27+
28+
- name: Install Nuget Packages
29+
run: nuget restore .\packages.config -PackagesDirectory .\packages\
2730

2831
- name: Retrieve and build all available solutions
29-
run: |
30-
.\Build-AllSamples.ps1 -Verbose
32+
run: .\Build-AllSamples.ps1 -Verbose
3133
env:
3234
WDS_Configuration: ${{ matrix.configuration }}
3335
WDS_Platform: ${{ matrix.platform }}
@@ -56,8 +58,7 @@ jobs:
5658
path: _logs
5759

5860
- name: Join and generate global reports
59-
run: |
60-
.\.github\scripts\Join-CsvReports.ps1
61+
run: .\.github\scripts\Join-CsvReports.ps1
6162

6263
- name: Archive global overview build reports
6364
uses: actions/upload-artifact@v3

Build-SampleSet.ps1

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ param(
99
)
1010

1111
$root = Get-Location
12+
13+
# launch developer powershell (if necessary to prevent multiple developer sessions)
14+
if (-not $env:VSCMD_VER) {
15+
Import-Module (Resolve-Path "$env:ProgramFiles\Microsoft Visual Studio\2022\*\Common7\Tools\Microsoft.VisualStudio.DevShell.dll")
16+
Enter-VsDevShell -VsInstallPath (Resolve-Path "$env:ProgramFiles\Microsoft Visual Studio\2022\*")
17+
cd $root
18+
}
19+
1220
$ThrottleFactor = 5
1321
$LogicalProcessors = (Get-CIMInstance -Class 'CIM_Processor' -Verbose:$false).NumberOfLogicalProcessors
1422

@@ -45,27 +53,34 @@ finally {
4553
}
4654

4755
#
48-
# Determine build environment: 'GitHub', 'NuGet', 'EWDK', or 'WDK'. Only used to determine build number.
56+
# Determine build environment: 'GitHub', 'NuGet', 'EWDK', or 'WDK'.
4957
# Determine build number (used for exclusions based on build number). Five digits. Say, '22621'.
58+
# Determine NuGet package version (if applicable).
59+
# Determine WDK vsix version.
5060
#
5161
$build_environment=""
5262
$build_number=0
63+
$nuget_package_version=0
64+
$vsix_version=""
5365
#
54-
# WDK NuGet will require presence of a folder 'packages'
66+
# In Github we build using NuGet and get the version from packages and vsix version from env var set from the install vsix step.
5567
#
56-
#
57-
# Hack: In GitHub we do not have an environment variable where we can see WDK build number, so we have it hard coded.
58-
#
59-
if (-not $env:GITHUB_REPOSITORY -eq '') {
68+
if ($env:GITHUB_REPOSITORY) {
6069
$build_environment="GitHub"
61-
$build_number=22621
70+
$nuget_package_version=([regex]'(?<=x64\.)(\d+\.)(\d+\.)(\d+\.)(\d+)').Matches((Get-Childitem .\packages\*WDK.x64* -Name)).Value
71+
$build_number=$nuget_package_version.split('.')[2]
72+
$vsix_version = $env:SAMPLES_VSIX_VERSION
6273
}
6374
#
64-
# Hack: If user has hydrated nuget packages, then use those. That will be indicated by presence of a folder named .\packages.
75+
# WDK NuGet will require presence of a folder 'packages'. The version is sourced from repo .\Env-Vars.ps1.
76+
#
77+
# Hack: If user has hydrated nuget packages, then use those. That will be indicated by presence of a folder named '.\packages'.
78+
# Further, we need to test that the directory has been hydrated using '.\packages\*'.
6579
#
66-
elseif(Test-Path(".\packages")) {
80+
elseif(Test-Path(".\packages\*")) {
6781
$build_environment=("NuGet")
68-
$build_number=26100
82+
$nuget_package_version=([regex]'(?<=x64\.)(\d+\.)(\d+\.)(\d+\.)(\d+)').Matches((Get-Childitem .\packages\*WDK.x64* -Name)).Value
83+
$build_number=$nuget_package_version.split('.')[2]
6984
}
7085
#
7186
# EWDK sets environment variable BuildLab. For example 'ni_release_svc_prod1.22621.2428'.
@@ -91,7 +106,19 @@ else {
91106
Write-Error "Could not determine build environment."
92107
exit 1
93108
}
94-
109+
#
110+
# Get the vsix version from packages if not set
111+
if (-not $vsix_version) {
112+
$vsix_version = ls "${env:ProgramData}\Microsoft\VisualStudio\Packages\Microsoft.Windows.DriverKit,version=*" | Select -ExpandProperty Name
113+
if ($vsix_version) {
114+
$vsix_version = $vsix_version.split('=')[1]
115+
}
116+
else {
117+
Write-Error "No version of the WDK VSIX could be found. The WDK VSIX is not installed."
118+
exit 1
119+
}
120+
}
121+
#
95122
#
96123
# InfVerif_AdditionalOptions
97124
#
@@ -155,6 +182,8 @@ $SolutionsTotal = $sampleSet.Count * $Configurations.Count * $Platforms.Count
155182

156183
Write-Output ("Build Environment: " + $build_environment)
157184
Write-Output ("Build Number: " + $build_number)
185+
if (($build_environment -eq "GitHub") -or ($build_environment -eq "NuGet")) { Write-Output ("Nuget Package Version: " + $nuget_package_version) }
186+
Write-Output ("WDK VSIX Version: " + $vsix_version)
158187
Write-Output ("Samples: " + $sampleSet.Count)
159188
Write-Output ("Configurations: " + $Configurations.Count + " (" + $Configurations + ")")
160189
Write-Output ("Platforms: " + $Platforms.Count + " (" + $Platforms + ")")

Building-Locally.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ To build the Windows Driver Samples you need a "driver build environment". In e
7979

8080
### Option A: Use WDK NuGet Packages
8181
* See [Download the Windows Driver Kit (WDK)](https://learn.microsoft.com/en-us/windows-hardware/drivers/download-the-wdk) for instructions on how to install Visual Studio, but only complete "Step 1". You do not need to install the SDK or the WDK.
82-
* Install the Visual Studio Windows Driver Kit Extension (WDK.vsix). Open Visual Studio -> Extensions -> Manage Extensions... -> Online -> Visual Studio Market Place -> Windows Driver Kit -> 10.0.26100.0 -> Download
82+
* Install the Visual Studio Windows Driver Kit Extension (WDK.vsix).
83+
* Open Visual Studio -> Extensions -> Manage Extensions... -> Browse.
84+
* In the search bar type: `Windows Driver Kit`.
85+
* Find the `Microsoft` signed extension.
86+
* Click the Install button.
8387
* Launch a "Developer Command Prompt for VS 2022".
8488
* Restore WDK packages from feed :
8589

filesys/miniFilter/scanner/user/scanUser.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,6 @@ Return Value
254254
}
255255
}
256256

257-
free( message );
258-
259257
return hr;
260258
}
261259

@@ -268,13 +266,12 @@ main (
268266
{
269267
DWORD requestCount = SCANNER_DEFAULT_REQUEST_COUNT;
270268
DWORD threadCount = SCANNER_DEFAULT_THREAD_COUNT;
271-
HANDLE threads[SCANNER_MAX_THREAD_COUNT];
269+
HANDLE threads[SCANNER_MAX_THREAD_COUNT] = { NULL };
272270
SCANNER_THREAD_CONTEXT context;
273271
HANDLE port, completion;
274-
PSCANNER_MESSAGE msg;
272+
PSCANNER_MESSAGE messages;
275273
DWORD threadId;
276274
HRESULT hr;
277-
DWORD i, j;
278275

279276
//
280277
// Check how many threads and per thread requests are desired.
@@ -343,11 +340,23 @@ main (
343340
context.Completion = completion;
344341

345342
//
346-
// Create specified number of threads.
343+
// Allocate messages.
347344
//
348345

349-
for (i = 0; i < threadCount; i++) {
346+
messages = calloc(((size_t) threadCount) * requestCount, sizeof(SCANNER_MESSAGE));
347+
348+
if (messages == NULL) {
350349

350+
hr = ERROR_NOT_ENOUGH_MEMORY;
351+
goto main_cleanup;
352+
}
353+
354+
//
355+
// Create specified number of threads.
356+
//
357+
358+
for (DWORD i = 0; i < threadCount; i++) {
359+
351360
threads[i] = CreateThread( NULL,
352361
0,
353362
(LPTHREAD_START_ROUTINE) ScannerWorker,
@@ -366,20 +375,9 @@ main (
366375
goto main_cleanup;
367376
}
368377

369-
for (j = 0; j < requestCount; j++) {
370-
371-
//
372-
// Allocate the message.
373-
//
374-
375-
#pragma prefast(suppress:__WARNING_MEMORY_LEAK, "msg will not be leaked because it is freed in ScannerWorker")
376-
msg = malloc( sizeof( SCANNER_MESSAGE ) );
377-
378-
if (msg == NULL) {
379-
380-
hr = ERROR_NOT_ENOUGH_MEMORY;
381-
goto main_cleanup;
382-
}
378+
for (DWORD j = 0; j < requestCount; j++) {
379+
380+
PSCANNER_MESSAGE msg = &(messages[i * requestCount + j]);
383381

384382
memset( &msg->Ovlp, 0, sizeof( OVERLAPPED ) );
385383

@@ -393,24 +391,26 @@ main (
393391
&msg->Ovlp );
394392

395393
if (hr != HRESULT_FROM_WIN32( ERROR_IO_PENDING )) {
396-
397-
free( msg );
398394
goto main_cleanup;
399395
}
400396
}
401397
}
402398

403399
hr = S_OK;
404-
405-
WaitForMultipleObjectsEx( i, threads, TRUE, INFINITE, FALSE );
406-
400+
407401
main_cleanup:
408402

403+
for (INT i = 0; threads[i] != NULL; ++i) {
404+
WaitForSingleObjectEx(threads[i], INFINITE, FALSE);
405+
}
406+
409407
printf( "Scanner: All done. Result = 0x%08x\n", hr );
410408

411409
CloseHandle( port );
412410
CloseHandle( completion );
413411

412+
free(messages);
413+
414414
return hr;
415415
}
416416

simbatt/func/miniclass.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ Return Value:
202202
}
203203

204204
Status = GetSimBattStateFromRegistry(Device, RegState);
205-
if (!NT_SUCCESS(Status)) {
205+
if (NT_SUCCESS(Status)) {
206206

207207
RtlZeroMemory(RegState, sizeof(SIMBATT_STATE));
208208
WdfWaitLockAcquire(DevExt->StateLock, NULL);

0 commit comments

Comments
 (0)