[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [XENBUS PATCH 03/13] Refine function annotations
* Replace __analysis_assume with _Analysis_assume_ or assertions * Add more assertions when appropriate * Adjust annotations of functions where annotations don't match usage Signed-off-by: Tu Dinh <ngoc-tu.dinh@xxxxxxxxxx> --- src/common/assert.h | 4 +- src/common/util.h | 11 +++-- src/monitor/monitor.c | 4 +- src/xen/driver.c | 42 +++++++++--------- src/xenbus/balloon.c | 2 +- src/xenbus/bus.c | 8 ++++ src/xenbus/debug.c | 18 ++++---- src/xenbus/evtchn.c | 10 ++--- src/xenbus/fdo.c | 95 ++++++++++++++++++++++++++++------------ src/xenbus/fdo.h | 22 +++++----- src/xenbus/pdo.c | 6 ++- src/xenbus/pdo.h | 2 +- src/xenbus/shared_info.c | 2 +- src/xenbus/store.c | 1 + src/xenfilt/fdo.c | 2 + 15 files changed, 144 insertions(+), 85 deletions(-) diff --git a/src/common/assert.h b/src/common/assert.h index 1c1104d..90a839f 100644 --- a/src/common/assert.h +++ b/src/common/assert.h @@ -89,7 +89,7 @@ __Bug( #define ASSERT(_EXP) \ do { \ __ASSERT(_EXP); \ - __analysis_assume(_EXP); \ + _Analysis_assume_(_EXP); \ } while (FALSE) #define ASSERT3U(_X, _OP, _Y) \ @@ -132,7 +132,7 @@ __Bug( #define ASSERT(_EXP) \ do { \ - __analysis_assume(_EXP); \ + _Analysis_assume_(_EXP); \ } while (FALSE) #define ASSERT3U(_X, _OP, _Y) \ diff --git a/src/common/util.h b/src/common/util.h index 9398daa..dbf64fd 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -150,13 +150,14 @@ __AllocatePoolWithTag( { PUCHAR Buffer; - __analysis_assume(PoolType == NonPagedPool || + _Analysis_assume_(PoolType == NonPagedPool || PoolType == PagedPool); if (NumberOfBytes == 0) return NULL; #if (_MSC_VER >= 1928) // VS 16.9 (EWDK 20344 or later) +#pragma warning(suppress:28160) // annotation error Buffer = ExAllocatePoolUninitialized(PoolType, NumberOfBytes, Tag); #else #pragma warning(suppress:28160) // annotation error @@ -278,7 +279,9 @@ static FORCEINLINE PSTR __strtok_r( _In_opt_ PSTR Buffer, _In_ PSTR Delimiter, - _Inout_ PSTR *Context + _When_(Buffer != NULL, _Outptr_) + _When_(Buffer == NULL, _Inout_) + PSTR *Context ) { PSTR Token; @@ -316,7 +319,9 @@ static FORCEINLINE PWSTR __wcstok_r( _In_opt_ PWSTR Buffer, _In_ PWSTR Delimiter, - _Inout_ PWSTR *Context + _When_(Buffer != NULL, _Outptr_) + _When_(Buffer == NULL, _Inout_) + PWSTR *Context ) { PWSTR Token; diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c index f6e39f8..ae20ac7 100644 --- a/src/monitor/monitor.c +++ b/src/monitor/monitor.c @@ -119,8 +119,8 @@ __Log( Length = __min(MAXIMUM_BUFFER_SIZE - 1, Length + 2); - __analysis_assume(Length < MAXIMUM_BUFFER_SIZE); - __analysis_assume(Length >= 2); + _Analysis_assume_(Length < MAXIMUM_BUFFER_SIZE); + _Analysis_assume_(Length >= 2); Buffer[Length] = '\0'; Buffer[Length - 1] = '\n'; Buffer[Length - 2] = '\r'; diff --git a/src/xen/driver.c b/src/xen/driver.c index 48fe169..b44753c 100644 --- a/src/xen/driver.c +++ b/src/xen/driver.c @@ -164,14 +164,14 @@ __DriverGetMemoryKey( static FORCEINLINE NTSTATUS __DriverSetPfnArray( - _In_ PSTR Name, - _In_ ULONG Count, - _In_ PFN_NUMBER PfnArray[] + _In_ PSTR Name, + _In_ ULONG Count, + _In_reads_(Count) PFN_NUMBER PfnArray[] ) { - HANDLE Key = __DriverGetMemoryKey(); - LONG Index; - NTSTATUS status; + HANDLE Key = __DriverGetMemoryKey(); + LONG Index; + NTSTATUS status; Index = 0; while (Index < (LONG)Count) { @@ -224,17 +224,17 @@ fail1: static FORCEINLINE NTSTATUS __DriverAllocatePfnArray( - _In_ PSTR Name, - _In_ ULONG Count, - _Out_ PFN_NUMBER PfnArray[] + _In_ PSTR Name, + _In_ ULONG Count, + _Out_writes_all_(Count) PFN_NUMBER PfnArray[] ) { - PHYSICAL_ADDRESS LowAddress; - PHYSICAL_ADDRESS HighAddress; - LARGE_INTEGER SkipBytes; - SIZE_T TotalBytes; - PMDL Mdl; - NTSTATUS status; + PHYSICAL_ADDRESS LowAddress; + PHYSICAL_ADDRESS HighAddress; + LARGE_INTEGER SkipBytes; + SIZE_T TotalBytes; + PMDL Mdl; + NTSTATUS status; LowAddress.QuadPart = 0ull; HighAddress.QuadPart = ~0ull; @@ -289,14 +289,14 @@ fail1: static FORCEINLINE NTSTATUS __DriverGetPfnArray( - _In_ PSTR Name, - _In_ ULONG Count, - _Out_writes_(Count) PFN_NUMBER PfnArray[] + _In_ PSTR Name, + _In_ ULONG Count, + _Out_writes_all_(Count) PFN_NUMBER PfnArray[] ) { - HANDLE Key = __DriverGetMemoryKey(); - ULONG Index; - NTSTATUS status; + HANDLE Key = __DriverGetMemoryKey(); + ULONG Index; + NTSTATUS status; for (Index = 0; Index < Count; Index++) { CHAR ValueName[MAXNAMELEN]; diff --git a/src/xenbus/balloon.c b/src/xenbus/balloon.c index 7afd387..544628a 100644 --- a/src/xenbus/balloon.c +++ b/src/xenbus/balloon.c @@ -323,7 +323,7 @@ static ULONG BalloonAllocatePfnArray( _In_ PXENBUS_BALLOON_CONTEXT Context, _In_ ULONG Requested, - _Inout_ PBOOLEAN Slow + _Out_ PBOOLEAN Slow ) { LARGE_INTEGER Start; diff --git a/src/xenbus/bus.c b/src/xenbus/bus.c index c97759c..f9c8021 100644 --- a/src/xenbus/bus.c +++ b/src/xenbus/bus.c @@ -103,6 +103,8 @@ BusTranslateAddress( { PXENBUS_BUS_CONTEXT Context = _Context; + ASSERT(Context != NULL); + return PdoTranslateBusAddress(Context->Pdo, BusAddress, Length, @@ -123,6 +125,8 @@ BusGetDmaAdapter( PXENBUS_BUS_CONTEXT Context = _Context; XENBUS_DMA_ADAPTER_TYPE Type; + ASSERT(Context != NULL); + if (Context->InterceptDmaAdapter != 0) { RTL_OSVERSIONINFOEXW VersionInformation; NTSTATUS status; @@ -161,6 +165,8 @@ BusSetData( { PXENBUS_BUS_CONTEXT Context = _Context; + ASSERT(Context != NULL); + return PdoSetBusData(Context->Pdo, DataType, Buffer, @@ -182,6 +188,8 @@ BusGetData( { PXENBUS_BUS_CONTEXT Context = _Context; + ASSERT(Context != NULL); + return PdoGetBusData(Context->Pdo, DataType, Buffer, diff --git a/src/xenbus/debug.c b/src/xenbus/debug.c index a0bb27d..7fc1430 100644 --- a/src/xenbus/debug.c +++ b/src/xenbus/debug.c @@ -91,17 +91,17 @@ RtlCaptureStackBackTrace( static NTSTATUS DebugRegister( - _In_ PINTERFACE Interface, - _In_ PSTR Prefix, - _In_ XENBUS_DEBUG_FUNCTION Function, - _In_opt_ PVOID Argument, - _Out_opt_ PXENBUS_DEBUG_CALLBACK *Callback + _In_ PINTERFACE Interface, + _In_ PSTR Prefix, + _In_ XENBUS_DEBUG_FUNCTION Function, + _In_opt_ PVOID Argument, + _Out_ PXENBUS_DEBUG_CALLBACK *Callback ) { - PXENBUS_DEBUG_CONTEXT Context = Interface->Context; - ULONG Length; - KIRQL Irql; - NTSTATUS status; + PXENBUS_DEBUG_CONTEXT Context = Interface->Context; + ULONG Length; + KIRQL Irql; + NTSTATUS status; *Callback = __DebugAllocate(sizeof (XENBUS_DEBUG_CALLBACK)); diff --git a/src/xenbus/evtchn.c b/src/xenbus/evtchn.c index 8927938..4344bae 100644 --- a/src/xenbus/evtchn.c +++ b/src/xenbus/evtchn.c @@ -1084,13 +1084,13 @@ EvtchnWaitVersion5( Timeout); } -static KSERVICE_ROUTINE EvtchnInterruptCallback; - -_Use_decl_annotations_ +_Function_class_(KSERVICE_ROUTINE) +_IRQL_requires_(HIGH_LEVEL) +_IRQL_requires_same_ static BOOLEAN EvtchnInterruptCallback( - PKINTERRUPT InterruptObject, - PVOID Argument + _In_opt_ PKINTERRUPT InterruptObject, + _In_ PVOID Argument ) { PXENBUS_EVTCHN_PROCESSOR Processor = Argument; diff --git a/src/xenbus/fdo.c b/src/xenbus/fdo.c index c673ae0..89595ed 100644 --- a/src/xenbus/fdo.c +++ b/src/xenbus/fdo.c @@ -435,7 +435,7 @@ FdoTranslateBusAddress( _In_ PXENBUS_FDO Fdo, _In_ PHYSICAL_ADDRESS BusAddress, _In_ ULONG Length, - _Inout_ PULONG AddressSpace, + _Out_ PULONG AddressSpace, _Out_ PPHYSICAL_ADDRESS TranslatedAddress ) { @@ -453,11 +453,11 @@ FdoTranslateBusAddress( ULONG FdoSetBusData( - _In_ PXENBUS_FDO Fdo, - _In_ ULONG DataType, - _In_ PVOID Buffer, - _In_ ULONG Offset, - _In_ ULONG Length + _In_ PXENBUS_FDO Fdo, + _In_ ULONG DataType, + _In_reads_bytes_(Length) PVOID Buffer, + _In_ ULONG Offset, + _In_range_(!=, 0) ULONG Length ) { PBUS_INTERFACE_STANDARD BusInterface; @@ -474,18 +474,19 @@ FdoSetBusData( ULONG FdoGetBusData( - _In_ PXENBUS_FDO Fdo, - _In_ ULONG DataType, - _In_ PVOID Buffer, - _In_ ULONG Offset, - _In_ ULONG Length + _In_ PXENBUS_FDO Fdo, + _In_ ULONG DataType, + _Out_writes_bytes_(Length) PVOID Buffer, + _In_ ULONG Offset, + _In_range_(!=, 0) ULONG Length ) { - PBUS_INTERFACE_STANDARD BusInterface; + PBUS_INTERFACE_STANDARD BusInterface; BusInterface = Fdo->LowerBusInterface; ASSERT(BusInterface != NULL); +#pragma prefast(suppress:6001) // imprecise GetBusData annotations return BusInterface->GetBusData(BusInterface->Context, DataType, Buffer, @@ -863,6 +864,8 @@ FdoDelegateIrpCompletion( UNREFERENCED_PARAMETER(DeviceObject); UNREFERENCED_PARAMETER(Irp); + ASSERT(Event != NULL); + KeSetEvent(Event, IO_NO_INCREMENT, FALSE); return STATUS_MORE_PROCESSING_REQUIRED; @@ -949,6 +952,8 @@ FdoForwardIrpSynchronouslyCompletion( UNREFERENCED_PARAMETER(DeviceObject); UNREFERENCED_PARAMETER(Irp); + ASSERT(Event != NULL); + KeSetEvent(Event, IO_NO_INCREMENT, FALSE); return STATUS_MORE_PROCESSING_REQUIRED; @@ -4721,8 +4726,8 @@ static IO_WORKITEM_ROUTINE FdoSetDevcePowerUpWorker; _Use_decl_annotations_ static VOID FdoSetDevcePowerUpWorker( - _In_ PDEVICE_OBJECT DeviceObject, - _In_opt_ PVOID Context + PDEVICE_OBJECT DeviceObject, + PVOID Context ) { PXENBUS_FDO Fdo = (PXENBUS_FDO) Context; @@ -4730,6 +4735,8 @@ FdoSetDevcePowerUpWorker( UNREFERENCED_PARAMETER(DeviceObject); + ASSERT(Fdo != NULL); + Irp = InterlockedExchangePointer(&Fdo->DevicePowerIrp, NULL); ASSERT(Irp != NULL); @@ -4755,6 +4762,8 @@ FdoSetDevicePowerUpComplete( UNREFERENCED_PARAMETER(DeviceObject); + ASSERT(Fdo != NULL); + StackLocation = IoGetCurrentIrpStackLocation(Irp); DeviceState = StackLocation->Parameters.Power.State.DeviceState; @@ -4806,6 +4815,8 @@ FdoSetDevicePowerDownWorker( UNREFERENCED_PARAMETER(DeviceObject); + ASSERT(Fdo != NULL); + Irp = InterlockedExchangePointer(&Fdo->DevicePowerIrp, NULL); ASSERT(Irp != NULL); @@ -4915,6 +4926,8 @@ FdoRequestDevicePowerUpComplete( UNREFERENCED_PARAMETER(PowerState); UNREFERENCED_PARAMETER(IoStatus); + ASSERT(Irp != NULL); + IoCompleteRequest(Irp, IO_NO_INCREMENT); } @@ -4936,6 +4949,8 @@ FdoSetSystemPowerUpWorker( UNREFERENCED_PARAMETER(DeviceObject); + ASSERT(Fdo != NULL); + Irp = InterlockedExchangePointer(&Fdo->SystemPowerIrp, NULL); ASSERT(Irp != NULL); @@ -4988,9 +5003,14 @@ FdoSetSystemPowerUpComplete( UNREFERENCED_PARAMETER(DeviceObject); + ASSERT(Fdo != NULL); + StackLocation = IoGetCurrentIrpStackLocation(Irp); SystemState = StackLocation->Parameters.Power.State.SystemState; + ASSERT(SystemState >= PowerSystemUnspecified && + SystemState < PowerSystemMaximum); + if (SystemState < PowerSystemHibernate && __FdoGetSystemPowerState(Fdo) >= PowerSystemHibernate) { @@ -5061,6 +5081,8 @@ FdoSetSystemPowerDownWorker( UNREFERENCED_PARAMETER(DeviceObject); + ASSERT(Fdo != NULL); + Irp = InterlockedExchangePointer(&Fdo->SystemPowerIrp, NULL); ASSERT(Irp != NULL); @@ -5088,17 +5110,24 @@ FdoRequestDevicePowerDownComplete( ) { PIRP Irp = (PIRP) Context; - PIO_STACK_LOCATION StackLocation = IoGetCurrentIrpStackLocation(Irp); - PDEVICE_OBJECT UpperDeviceObject = StackLocation->DeviceObject; - PXENBUS_DX Dx = (PXENBUS_DX)UpperDeviceObject->DeviceExtension; - PXENBUS_FDO Fdo = Dx->Fdo; - SYSTEM_POWER_STATE SystemState = StackLocation->Parameters.Power.State.SystemState; + PIO_STACK_LOCATION StackLocation; + PDEVICE_OBJECT UpperDeviceObject; + PXENBUS_DX Dx; + PXENBUS_FDO Fdo; + SYSTEM_POWER_STATE SystemState; NTSTATUS status = IoStatus->Status; UNREFERENCED_PARAMETER(DeviceObject); UNREFERENCED_PARAMETER(MinorFunction); UNREFERENCED_PARAMETER(PowerState); + ASSERT(Irp != NULL); + StackLocation = IoGetCurrentIrpStackLocation(Irp); + UpperDeviceObject = StackLocation->DeviceObject; + Dx = (PXENBUS_DX)UpperDeviceObject->DeviceExtension; + Fdo = Dx->Fdo; + SystemState = StackLocation->Parameters.Power.State.SystemState; + if (!NT_SUCCESS(status)) goto fail1; @@ -5232,6 +5261,8 @@ FdoRequestQuerySystemPowerUpComplete( UNREFERENCED_PARAMETER(MinorFunction); UNREFERENCED_PARAMETER(PowerState); + ASSERT(Irp != NULL); + if (!NT_SUCCESS(IoStatus->Status)) Irp->IoStatus.Status = IoStatus->Status; IoCompleteRequest(Irp, IO_NO_INCREMENT); @@ -5255,6 +5286,8 @@ FdoQuerySystemPowerUpComplete( UNREFERENCED_PARAMETER(DeviceObject); + ASSERT(Fdo != NULL); + StackLocation = IoGetCurrentIrpStackLocation(Irp); SystemState = StackLocation->Parameters.Power.State.SystemState; PowerState.DeviceState = Fdo->LowerDeviceCapabilities.DeviceState[SystemState]; @@ -5299,23 +5332,29 @@ static REQUEST_POWER_COMPLETE FdoRequestQuerySystemPowerDownComplete; _Use_decl_annotations_ static VOID FdoRequestQuerySystemPowerDownComplete( - _In_ PDEVICE_OBJECT DeviceObject, - _In_ UCHAR MinorFunction, - _In_ POWER_STATE PowerState, - _In_opt_ PVOID Context, - _In_ PIO_STATUS_BLOCK IoStatus + PDEVICE_OBJECT DeviceObject, + UCHAR MinorFunction, + POWER_STATE PowerState, + PVOID Context, + PIO_STATUS_BLOCK IoStatus ) { PIRP Irp = (PIRP) Context; - PIO_STACK_LOCATION StackLocation = IoGetCurrentIrpStackLocation(Irp); - PDEVICE_OBJECT UpperDeviceObject = StackLocation->DeviceObject; - PXENBUS_DX Dx = (PXENBUS_DX)UpperDeviceObject->DeviceExtension; - PXENBUS_FDO Fdo = Dx->Fdo; + PIO_STACK_LOCATION StackLocation; + PDEVICE_OBJECT UpperDeviceObject; + PXENBUS_DX Dx; + PXENBUS_FDO Fdo; UNREFERENCED_PARAMETER(DeviceObject); UNREFERENCED_PARAMETER(MinorFunction); UNREFERENCED_PARAMETER(PowerState); + ASSERT(Irp != NULL); + StackLocation = IoGetCurrentIrpStackLocation(Irp); + UpperDeviceObject = StackLocation->DeviceObject; + Dx = (PXENBUS_DX)UpperDeviceObject->DeviceExtension; + Fdo = Dx->Fdo; + if (!NT_SUCCESS(IoStatus->Status)) goto fail1; diff --git a/src/xenbus/fdo.h b/src/xenbus/fdo.h index e506787..e104d99 100644 --- a/src/xenbus/fdo.h +++ b/src/xenbus/fdo.h @@ -100,26 +100,26 @@ FdoTranslateBusAddress( _In_ PXENBUS_FDO Fdo, _In_ PHYSICAL_ADDRESS BusAddress, _In_ ULONG Length, - _Inout_ PULONG AddressSpace, + _Out_ PULONG AddressSpace, _Out_ PPHYSICAL_ADDRESS TranslatedAddress ); extern ULONG FdoSetBusData( - _In_ PXENBUS_FDO Fdo, - _In_ ULONG DataType, - _In_ PVOID Buffer, - _In_ ULONG Offset, - _In_ ULONG Length + _In_ PXENBUS_FDO Fdo, + _In_ ULONG DataType, + _In_reads_bytes_(Length) PVOID Buffer, + _In_ ULONG Offset, + _In_range_(!=, 0) ULONG Length ); extern ULONG FdoGetBusData( - _In_ PXENBUS_FDO Fdo, - _In_ ULONG DataType, - _In_ PVOID Buffer, - _In_ ULONG Offset, - _In_ ULONG Length + _In_ PXENBUS_FDO Fdo, + _In_ ULONG DataType, + _Out_writes_bytes_(Length) PVOID Buffer, + _In_ ULONG Offset, + _In_range_(!=, 0) ULONG Length ); extern PSTR diff --git a/src/xenbus/pdo.c b/src/xenbus/pdo.c index df07973..ec2fb5e 100644 --- a/src/xenbus/pdo.c +++ b/src/xenbus/pdo.c @@ -518,7 +518,7 @@ PdoTranslateBusAddress( _In_ PXENBUS_PDO Pdo, _In_ PHYSICAL_ADDRESS BusAddress, _In_ ULONG Length, - _Inout_ PULONG AddressSpace, + _Out_ PULONG AddressSpace, _Out_ PPHYSICAL_ADDRESS TranslatedAddress ) { @@ -1713,6 +1713,8 @@ PdoSetDevicePowerWorker( UNREFERENCED_PARAMETER(DeviceObject); + ASSERT(Pdo != NULL); + Irp = InterlockedExchangePointer(&Pdo->DevicePowerIrp, NULL); ASSERT(Irp != NULL); @@ -1803,6 +1805,8 @@ PdoSetSystemPowerWorker( UNREFERENCED_PARAMETER(DeviceObject); + ASSERT(Pdo != NULL); + Irp = InterlockedExchangePointer(&Pdo->SystemPowerIrp, NULL); ASSERT(Irp != NULL); diff --git a/src/xenbus/pdo.h b/src/xenbus/pdo.h index af59955..d930919 100644 --- a/src/xenbus/pdo.h +++ b/src/xenbus/pdo.h @@ -87,7 +87,7 @@ PdoTranslateBusAddress( _In_ PXENBUS_PDO Pdo, _In_ PHYSICAL_ADDRESS BusAddress, _In_ ULONG Length, - _Inout_ PULONG AddressSpace, + _Out_ PULONG AddressSpace, _Out_ PPHYSICAL_ADDRESS TranslatedAddress ); diff --git a/src/xenbus/shared_info.c b/src/xenbus/shared_info.c index 84056b7..e85f4c1 100644 --- a/src/xenbus/shared_info.c +++ b/src/xenbus/shared_info.c @@ -205,7 +205,7 @@ SharedInfoEvtchnPoll( _In_ PINTERFACE Interface, _In_ ULONG Index, _In_ XENBUS_SHARED_INFO_EVENT Event, - _In_opt_ PVOID Argument + _In_ PVOID Argument ) { PXENBUS_SHARED_INFO_CONTEXT Context = Interface->Context; diff --git a/src/xenbus/store.c b/src/xenbus/store.c index 9bdd590..688e22f 100644 --- a/src/xenbus/store.c +++ b/src/xenbus/store.c @@ -186,6 +186,7 @@ StorePrepareRequest( va_list Arguments; NTSTATUS status; +#pragma prefast(suppress:6001) // for ASSERT on Request only ASSERT(IsZeroMemory(Request, sizeof (XENBUS_STORE_REQUEST))); if (Transaction != NULL) { diff --git a/src/xenfilt/fdo.c b/src/xenfilt/fdo.c index de70c6d..594da4a 100644 --- a/src/xenfilt/fdo.c +++ b/src/xenfilt/fdo.c @@ -1091,6 +1091,8 @@ FdoSetDevicePowerUpComplete( UNREFERENCED_PARAMETER(DeviceObject); + ASSERT(Fdo != NULL); + StackLocation = IoGetCurrentIrpStackLocation(Irp); PowerState = StackLocation->Parameters.Power.State; -- 2.50.0.windows.1 Ngoc Tu Dinh | Vates XCP-ng Developer XCP-ng & Xen Orchestra - Vates solutions web: https://vates.tech
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |