[NTOS:KD64] Some code formatting + add SAL2 annotations to the functions that are going to be involved in the next commit.

+ s/ReturnStatus/Status/ , and get rid of Win32 LPSTR and replace by
  PCHAR (since the concerned code deals with counted strings).
This commit is contained in:
Hermès Bélusca-Maïto 2019-11-17 22:28:42 +01:00
parent 6c1aac6948
commit 54c03f6965
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
5 changed files with 115 additions and 128 deletions

View file

@ -12,9 +12,9 @@
// Default size of the DbgPrint log buffer // Default size of the DbgPrint log buffer
// //
#if DBG #if DBG
#define KD_DEFAULT_LOG_BUFFER_SIZE 0x8000 #define KD_DEFAULT_LOG_BUFFER_SIZE 0x8000
#else #else
#define KD_DEFAULT_LOG_BUFFER_SIZE 0x1000 #define KD_DEFAULT_LOG_BUFFER_SIZE 0x1000
#endif #endif
// //
@ -211,26 +211,26 @@ KdDisableDebuggerWithLock(
NTSTATUS NTSTATUS
NTAPI NTAPI
KdpPrint( KdpPrint(
IN ULONG ComponentId, _In_ ULONG ComponentId,
IN ULONG Level, _In_ ULONG Level,
IN LPSTR String, _In_reads_bytes_(Length) PCHAR String,
IN USHORT Length, _In_ USHORT Length,
IN KPROCESSOR_MODE PreviousMode, _In_ KPROCESSOR_MODE PreviousMode,
IN PKTRAP_FRAME TrapFrame, _In_ PKTRAP_FRAME TrapFrame,
IN PKEXCEPTION_FRAME ExceptionFrame, _In_ PKEXCEPTION_FRAME ExceptionFrame,
OUT PBOOLEAN Handled _Out_ PBOOLEAN Handled
); );
USHORT USHORT
NTAPI NTAPI
KdpPrompt( KdpPrompt(
IN LPSTR PromptString, _In_reads_bytes_(PromptLength) PCHAR PromptString,
IN USHORT PromptLength, _In_ USHORT PromptLength,
OUT LPSTR ResponseString, _Out_writes_bytes_(MaximumResponseLength) PCHAR ResponseString,
IN USHORT MaximumResponseLength, _In_ USHORT MaximumResponseLength,
IN KPROCESSOR_MODE PreviousMode, _In_ KPROCESSOR_MODE PreviousMode,
IN PKTRAP_FRAME TrapFrame, _In_ PKTRAP_FRAME TrapFrame,
IN PKEXCEPTION_FRAME ExceptionFrame _In_ PKEXCEPTION_FRAME ExceptionFrame
); );
VOID VOID
@ -345,12 +345,12 @@ KdpAllowDisable(
NTSTATUS NTSTATUS
NTAPI NTAPI
KdpCopyMemoryChunks( KdpCopyMemoryChunks(
IN ULONG64 Address, _In_ ULONG64 Address,
IN PVOID Buffer, _In_ PVOID Buffer,
IN ULONG TotalSize, _In_ ULONG TotalSize,
IN ULONG ChunkSize, _In_ ULONG ChunkSize,
IN ULONG Flags, _In_ ULONG Flags,
OUT PULONG ActualSize OPTIONAL _Out_opt_ PULONG ActualSize
); );
// //
@ -359,16 +359,16 @@ KdpCopyMemoryChunks(
VOID VOID
NTAPI NTAPI
KdpMoveMemory( KdpMoveMemory(
IN PVOID Destination, _In_ PVOID Destination,
IN PVOID Source, _In_ PVOID Source,
IN SIZE_T Length _In_ SIZE_T Length
); );
VOID VOID
NTAPI NTAPI
KdpZeroMemory( KdpZeroMemory(
IN PVOID Destination, _In_ PVOID Destination,
IN SIZE_T Length _In_ SIZE_T Length
); );
// //
@ -510,7 +510,7 @@ KdpSysCheckLowMemory(
VOID VOID
__cdecl __cdecl
KdpDprintf( KdpDprintf(
IN PCHAR Format, _In_ PCHAR Format,
... ...
); );

View file

@ -17,9 +17,10 @@
VOID VOID
NTAPI NTAPI
KdpMoveMemory(IN PVOID Destination, KdpMoveMemory(
IN PVOID Source, _In_ PVOID Destination,
IN SIZE_T Length) _In_ PVOID Source,
_In_ SIZE_T Length)
{ {
PCHAR DestinationBytes, SourceBytes; PCHAR DestinationBytes, SourceBytes;
@ -31,8 +32,9 @@ KdpMoveMemory(IN PVOID Destination,
VOID VOID
NTAPI NTAPI
KdpZeroMemory(IN PVOID Destination, KdpZeroMemory(
IN SIZE_T Length) _In_ PVOID Destination,
_In_ SIZE_T Length)
{ {
PCHAR DestinationBytes; PCHAR DestinationBytes;
@ -43,12 +45,13 @@ KdpZeroMemory(IN PVOID Destination,
NTSTATUS NTSTATUS
NTAPI NTAPI
KdpCopyMemoryChunks(IN ULONG64 Address, KdpCopyMemoryChunks(
IN PVOID Buffer, _In_ ULONG64 Address,
IN ULONG TotalSize, _In_ PVOID Buffer,
IN ULONG ChunkSize, _In_ ULONG TotalSize,
IN ULONG Flags, _In_ ULONG ChunkSize,
OUT PULONG ActualSize OPTIONAL) _In_ ULONG Flags,
_Out_opt_ PULONG ActualSize)
{ {
NTSTATUS Status; NTSTATUS Status;
ULONG RemainingLength, CopyChunk; ULONG RemainingLength, CopyChunk;
@ -94,10 +97,7 @@ KdpCopyMemoryChunks(IN ULONG64 Address,
} }
/* Do the copy */ /* Do the copy */
Status = MmDbgCopyMemory(Address, Status = MmDbgCopyMemory(Address, Buffer, CopyChunk, Flags);
Buffer,
CopyChunk,
Flags);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
/* Copy failed, break out */ /* Copy failed, break out */
@ -2147,15 +2147,16 @@ KdDisableDebugger(VOID)
*/ */
NTSTATUS NTSTATUS
NTAPI NTAPI
KdSystemDebugControl(IN SYSDBG_COMMAND Command, KdSystemDebugControl(
IN PVOID InputBuffer, _In_ SYSDBG_COMMAND Command,
IN ULONG InputBufferLength, _In_ PVOID InputBuffer,
OUT PVOID OutputBuffer, _In_ ULONG InputBufferLength,
IN ULONG OutputBufferLength, _Out_ PVOID OutputBuffer,
IN OUT PULONG ReturnLength, _In_ ULONG OutputBufferLength,
IN KPROCESSOR_MODE PreviousMode) _Inout_ PULONG ReturnLength,
_In_ KPROCESSOR_MODE PreviousMode)
{ {
/* handle sime internal commands */ /* Handle some internal commands */
if (Command == ' soR') if (Command == ' soR')
{ {
switch ((ULONG_PTR)InputBuffer) switch ((ULONG_PTR)InputBuffer)
@ -2269,7 +2270,7 @@ KdRefreshDebuggerNotPresent(VOID)
/* Check if the debugger is completely disabled */ /* Check if the debugger is completely disabled */
if (KdPitchDebugger) if (KdPitchDebugger)
{ {
/* Don't try to refresh then -- fail early */ /* Don't try to refresh then, fail early */
return TRUE; return TRUE;
} }

View file

@ -17,7 +17,8 @@
BOOLEAN BOOLEAN
NTAPI NTAPI
KdpPrintString(IN PSTRING Output) KdpPrintString(
_In_ PSTRING Output)
{ {
STRING Data, Header; STRING Data, Header;
DBGKD_DEBUG_IO DebugIo; DBGKD_DEBUG_IO DebugIo;
@ -57,8 +58,9 @@ KdpPrintString(IN PSTRING Output)
BOOLEAN BOOLEAN
NTAPI NTAPI
KdpPromptString(IN PSTRING PromptString, KdpPromptString(
IN PSTRING ResponseString) _In_ PSTRING PromptString,
_In_ PSTRING ResponseString)
{ {
STRING Data, Header; STRING Data, Header;
DBGKD_DEBUG_IO DebugIo; DBGKD_DEBUG_IO DebugIo;
@ -210,19 +212,20 @@ KdpSymbol(IN PSTRING DllPath,
USHORT USHORT
NTAPI NTAPI
KdpPrompt(IN LPSTR PromptString, KdpPrompt(
IN USHORT PromptLength, _In_reads_bytes_(PromptLength) PCHAR PromptString,
OUT PCHAR ResponseString, _In_ USHORT PromptLength,
IN USHORT MaximumResponseLength, _Out_writes_bytes_(MaximumResponseLength) PCHAR ResponseString,
IN KPROCESSOR_MODE PreviousMode, _In_ USHORT MaximumResponseLength,
IN PKTRAP_FRAME TrapFrame, _In_ KPROCESSOR_MODE PreviousMode,
IN PKEXCEPTION_FRAME ExceptionFrame) _In_ PKTRAP_FRAME TrapFrame,
_In_ PKEXCEPTION_FRAME ExceptionFrame)
{ {
STRING PromptBuffer, ResponseBuffer; STRING PromptBuffer, ResponseBuffer;
BOOLEAN Enable, Resend; BOOLEAN Enable, Resend;
PCHAR SafeResponseString;
CHAR CapturedPrompt[512]; CHAR CapturedPrompt[512];
CHAR SafeResponseBuffer[512]; CHAR SafeResponseBuffer[512];
PCHAR SafeResponseString;
/* Normalize the lengths */ /* Normalize the lengths */
PromptLength = min(PromptLength, PromptLength = min(PromptLength,
@ -236,26 +239,18 @@ KdpPrompt(IN LPSTR PromptString,
/* Handle user-mode buffers safely */ /* Handle user-mode buffers safely */
_SEH2_TRY _SEH2_TRY
{ {
/* Probe the prompt */ /* Probe and capture the prompt */
ProbeForRead(PromptString, ProbeForRead(PromptString, PromptLength, 1);
PromptLength, KdpMoveMemory(CapturedPrompt, PromptString, PromptLength);
1);
/* Capture prompt */
KdpMoveMemory(CapturedPrompt,
PromptString,
PromptLength);
PromptString = CapturedPrompt; PromptString = CapturedPrompt;
/* Probe and make room for response */ /* Probe and make room for the response */
ProbeForWrite(ResponseString, ProbeForWrite(ResponseString, MaximumResponseLength, 1);
MaximumResponseLength,
1);
SafeResponseString = SafeResponseBuffer; SafeResponseString = SafeResponseBuffer;
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{ {
/* Bad string pointer, bail out */ /* Bad string pointer, bail out */
_SEH2_YIELD(return 0); _SEH2_YIELD(return 0);
} }
_SEH2_END; _SEH2_END;
@ -265,7 +260,7 @@ KdpPrompt(IN LPSTR PromptString,
SafeResponseString = ResponseString; SafeResponseString = ResponseString;
} }
/* Setup the prompt and response buffers */ /* Setup the prompt and response buffers */
PromptBuffer.Buffer = PromptString; PromptBuffer.Buffer = PromptString;
PromptBuffer.Length = PromptLength; PromptBuffer.Length = PromptLength;
ResponseBuffer.Buffer = SafeResponseString; ResponseBuffer.Buffer = SafeResponseString;
@ -290,19 +285,19 @@ KdpPrompt(IN LPSTR PromptString,
/* Exit the debugger */ /* Exit the debugger */
KdExitDebugger(Enable); KdExitDebugger(Enable);
/* Copy back response if required */ /* Copy back the response if required */
if (PreviousMode != KernelMode) if (PreviousMode != KernelMode)
{ {
_SEH2_TRY _SEH2_TRY
{ {
/* Safely copy back response to user mode */ /* Safely copy back the response to user mode */
KdpMoveMemory(ResponseString, KdpMoveMemory(ResponseString,
ResponseBuffer.Buffer, ResponseBuffer.Buffer,
ResponseBuffer.Length); ResponseBuffer.Length);
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{ {
/* String became invalid after we exited, fail */ /* String became invalid after we exited, fail */
_SEH2_YIELD(return 0); _SEH2_YIELD(return 0);
} }
_SEH2_END; _SEH2_END;
@ -314,16 +309,17 @@ KdpPrompt(IN LPSTR PromptString,
NTSTATUS NTSTATUS
NTAPI NTAPI
KdpPrint(IN ULONG ComponentId, KdpPrint(
IN ULONG Level, _In_ ULONG ComponentId,
IN LPSTR String, _In_ ULONG Level,
IN USHORT Length, _In_reads_bytes_(Length) PCHAR String,
IN KPROCESSOR_MODE PreviousMode, _In_ USHORT Length,
IN PKTRAP_FRAME TrapFrame, _In_ KPROCESSOR_MODE PreviousMode,
IN PKEXCEPTION_FRAME ExceptionFrame, _In_ PKTRAP_FRAME TrapFrame,
OUT PBOOLEAN Handled) _In_ PKEXCEPTION_FRAME ExceptionFrame,
_Out_ PBOOLEAN Handled)
{ {
NTSTATUS ReturnStatus; NTSTATUS Status;
BOOLEAN Enable; BOOLEAN Enable;
STRING OutputString; STRING OutputString;
PVOID CapturedString; PVOID CapturedString;
@ -356,27 +352,21 @@ KdpPrint(IN ULONG ComponentId,
/* Normalize the length */ /* Normalize the length */
Length = min(Length, 512); Length = min(Length, 512);
/* Check if we need to verify the buffer */ /* Check if we need to verify the string */
if (PreviousMode != KernelMode) if (PreviousMode != KernelMode)
{ {
/* Capture user-mode buffers */ /* Capture user-mode buffers */
_SEH2_TRY _SEH2_TRY
{ {
/* Probe the string */ /* Probe and capture the string */
ProbeForRead(String, ProbeForRead(String, Length, 1);
Length,
1);
/* Capture it */
CapturedString = alloca(Length); CapturedString = alloca(Length);
KdpMoveMemory(CapturedString, KdpMoveMemory(CapturedString, String, Length);
String,
Length);
String = CapturedString; String = CapturedString;
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{ {
/* Bad pointer, fail the print */ /* Bad string pointer, bail out */
_SEH2_YIELD(return STATUS_ACCESS_VIOLATION); _SEH2_YIELD(return STATUS_ACCESS_VIOLATION);
} }
_SEH2_END; _SEH2_END;
@ -404,29 +394,30 @@ KdpPrint(IN ULONG ComponentId,
if (KdpPrintString(&OutputString)) if (KdpPrintString(&OutputString))
{ {
/* User pressed CTRL-C, breakpoint on return */ /* User pressed CTRL-C, breakpoint on return */
ReturnStatus = STATUS_BREAKPOINT; Status = STATUS_BREAKPOINT;
} }
else else
{ {
/* String was printed */ /* String was printed */
ReturnStatus = STATUS_SUCCESS; Status = STATUS_SUCCESS;
} }
/* Exit the debugger and return */ /* Exit the debugger and return */
KdExitDebugger(Enable); KdExitDebugger(Enable);
*Handled = TRUE; *Handled = TRUE;
return ReturnStatus; return Status;
} }
VOID VOID
__cdecl __cdecl
KdpDprintf(IN PCHAR Format, KdpDprintf(
...) _In_ PCHAR Format,
...)
{ {
STRING String; STRING String;
CHAR Buffer[100];
USHORT Length; USHORT Length;
va_list ap; va_list ap;
CHAR Buffer[100];
/* Format the string */ /* Format the string */
va_start(ap, Format); va_start(ap, Format);

View file

@ -166,7 +166,7 @@ KdpTrap(IN PKTRAP_FRAME TrapFrame,
/* Call the worker routine */ /* Call the worker routine */
ReturnStatus = KdpPrint((ULONG)KdpGetParameterThree(ContextRecord), ReturnStatus = KdpPrint((ULONG)KdpGetParameterThree(ContextRecord),
(ULONG)KdpGetParameterFour(ContextRecord), (ULONG)KdpGetParameterFour(ContextRecord),
(LPSTR)ExceptionRecord->ExceptionInformation[1], (PCHAR)ExceptionRecord->ExceptionInformation[1],
(USHORT)ExceptionRecord->ExceptionInformation[2], (USHORT)ExceptionRecord->ExceptionInformation[2],
PreviousMode, PreviousMode,
TrapFrame, TrapFrame,
@ -174,17 +174,16 @@ KdpTrap(IN PKTRAP_FRAME TrapFrame,
&Handled); &Handled);
/* Update the return value for the caller */ /* Update the return value for the caller */
KeSetContextReturnRegister(ContextRecord, KeSetContextReturnRegister(ContextRecord, ReturnStatus);
ReturnStatus);
break; break;
/* DbgPrompt */ /* DbgPrompt */
case BREAKPOINT_PROMPT: case BREAKPOINT_PROMPT:
/* Call the worker routine */ /* Call the worker routine */
ReturnLength = KdpPrompt((LPSTR)ExceptionRecord->ExceptionInformation[1], ReturnLength = KdpPrompt((PCHAR)ExceptionRecord->ExceptionInformation[1],
(USHORT)ExceptionRecord->ExceptionInformation[2], (USHORT)ExceptionRecord->ExceptionInformation[2],
(LPSTR)KdpGetParameterThree(ContextRecord), (PCHAR)KdpGetParameterThree(ContextRecord),
(USHORT)KdpGetParameterFour(ContextRecord), (USHORT)KdpGetParameterFour(ContextRecord),
PreviousMode, PreviousMode,
TrapFrame, TrapFrame,
@ -205,10 +204,8 @@ KdpTrap(IN PKTRAP_FRAME TrapFrame,
case BREAKPOINT_LOAD_SYMBOLS: case BREAKPOINT_LOAD_SYMBOLS:
/* Call the worker routine */ /* Call the worker routine */
KdpSymbol((PSTRING)ExceptionRecord-> KdpSymbol((PSTRING)ExceptionRecord->ExceptionInformation[1],
ExceptionInformation[1], (PKD_SYMBOLS_INFO)ExceptionRecord->ExceptionInformation[2],
(PKD_SYMBOLS_INFO)ExceptionRecord->
ExceptionInformation[2],
Unload, Unload,
PreviousMode, PreviousMode,
ContextRecord, ContextRecord,
@ -221,10 +218,8 @@ KdpTrap(IN PKTRAP_FRAME TrapFrame,
case BREAKPOINT_COMMAND_STRING: case BREAKPOINT_COMMAND_STRING:
/* Call the worker routine */ /* Call the worker routine */
KdpCommandString((PSTRING)ExceptionRecord-> KdpCommandString((PSTRING)ExceptionRecord->ExceptionInformation[1],
ExceptionInformation[1], (PSTRING)ExceptionRecord->ExceptionInformation[2],
(PSTRING)ExceptionRecord->
ExceptionInformation[2],
PreviousMode, PreviousMode,
ContextRecord, ContextRecord,
TrapFrame, TrapFrame,

View file

@ -33,13 +33,13 @@ Author:
NTSTATUS NTSTATUS
NTAPI NTAPI
KdSystemDebugControl( KdSystemDebugControl(
SYSDBG_COMMAND Command, _In_ SYSDBG_COMMAND Command,
PVOID InputBuffer, _In_ PVOID InputBuffer,
ULONG InputBufferLength, _In_ ULONG InputBufferLength,
PVOID OutputBuffer, _Out_ PVOID OutputBuffer,
ULONG OutputBufferLength, _In_ ULONG OutputBufferLength,
PULONG ReturnLength, _Inout_ PULONG ReturnLength,
KPROCESSOR_MODE PreviousMode _In_ KPROCESSOR_MODE PreviousMode
); );
BOOLEAN BOOLEAN