[KMTESTS:MM]
authorPierre Schweitzer <pierre@reactos.org>
Thu, 8 Jun 2017 20:16:34 +0000 (20:16 +0000)
committerPierre Schweitzer <pierre@reactos.org>
Thu, 8 Jun 2017 20:16:34 +0000 (20:16 +0000)
When testing noncached mapping, use MmAllocateNonCachedMemory() as suggested by Thomas to avoid later potential failures

svn path=/trunk/; revision=74967

rostests/kmtests/ntos_mm/MmMapLockedPagesSpecifyCache_drv.c
rostests/kmtests/ntos_mm/MmMapLockedPagesSpecifyCache_user.c

index 3bb471e..95d1eec 100644 (file)
@@ -18,6 +18,7 @@ static KMT_MESSAGE_HANDLER TestMessageHandler;
 static PVOID CurrentBuffer;
 static PMDL CurrentMdl;
 static PVOID CurrentUser;
+static SIZE_T NonCachedLength;
 
 NTSTATUS
 TestEntry(
@@ -86,7 +87,14 @@ TestCleanEverything(VOID)
     _SEH2_END;
     ok_eq_hex(SehStatus, STATUS_SUCCESS);
     IoFreeMdl(CurrentMdl);
-    ExFreePoolWithTag(CurrentBuffer, 'MLPC');
+    if (NonCachedLength)
+    {
+        MmFreeNonCachedMemory(CurrentBuffer, NonCachedLength);
+    }
+    else
+    {
+        ExFreePoolWithTag(CurrentBuffer, 'MLPC');
+    }
     CurrentMdl = NULL;
 }
 
@@ -128,8 +136,22 @@ TestMessageHandler(
 
                 if (!skip(Length > 0, "Null size!\n"))
                 {
-                    CurrentBuffer = ExAllocatePoolWithTag(NonPagedPool, Length, 'MLPC');
-                    ok(CurrentBuffer != NULL, "ExAllocatePool failed!\n");
+                    if (QueryBuffer->Cached)
+                    {
+                        CurrentBuffer = ExAllocatePoolWithTag(NonPagedPool, Length, 'MLPC');
+                        ok(CurrentBuffer != NULL, "ExAllocatePool failed!\n");
+                        NonCachedLength = 0;
+                    }
+                    else
+                    {
+                        CurrentBuffer = MmAllocateNonCachedMemory(Length);
+                        ok(CurrentBuffer != NULL, "MmAllocateNonCachedMemory failed!\n");
+                        if (CurrentBuffer)
+                        {
+                            RtlZeroMemory(CurrentBuffer, Length);
+                            NonCachedLength = Length;
+                        }
+                    }
                     if (!skip(CurrentBuffer != NULL, "ExAllocatePool failed!\n"))
                     {
                         CurrentMdl = IoAllocateMdl(CurrentBuffer, Length, FALSE, FALSE, NULL);
@@ -163,17 +185,8 @@ TestMessageHandler(
                                 SehStatus = _SEH2_GetExceptionCode();
                             }
                             _SEH2_END;
-
-                            if (QueryBuffer->Cached)
-                            {
-                                ok_eq_hex(SehStatus, STATUS_SUCCESS);
-                                ok(CurrentUser != NULL, "MmMapLockedPagesSpecifyCache failed!\n");
-                            }
-                            else
-                            {
-                                ok_eq_hex(SehStatus, STATUS_INVALID_ADDRESS);
-                                ok_eq_pointer(CurrentUser, NULL);
-                            }
+                            ok_eq_hex(SehStatus, STATUS_SUCCESS);
+                            ok(CurrentUser != NULL, "MmMapLockedPagesSpecifyCache failed!\n");
                         }
                         else
                         {
index e935ccb..23b2772 100644 (file)
     Var = (Length);                            \
 }
 
+#define FILL_QUERY_BUFFER(QueryBuffer, BufferLength, UseCache) \
+{                                                              \
+    QueryBuffer.Length = BufferLength;                         \
+    QueryBuffer.Buffer = NULL;                                 \
+    QueryBuffer.Cached = UseCache;                             \
+}
+
+#define FILL_READ_BUFFER(QueryBuffer, ReadBuffer)               \
+{                                                               \
+    PULONG Buffer;                                              \
+    ReadBuffer.Buffer = QueryBuffer.Buffer;                     \
+    if (!skip(QueryBuffer.Buffer != NULL, "Buffer is NULL\n"))  \
+    {                                                           \
+        ReadBuffer.Pattern = WRITE_PATTERN;                     \
+        ReadBuffer.Length = QueryBuffer.Length;                 \
+        Buffer = QueryBuffer.Buffer;                            \
+        for (i = 0; i < ReadBuffer.Length / sizeof(ULONG); ++i) \
+        {                                                       \
+            Buffer[i] = ReadBuffer.Pattern;                     \
+        }                                                       \
+    }                                                           \
+}
+
 START_TEST(MmMapLockedPagesSpecifyCache)
 {
     QUERY_BUFFER QueryBuffer;
     READ_BUFFER ReadBuffer;
     DWORD Length;
     USHORT i;
-    PULONG Buffer;
     USHORT BufferLength;
 
     KmtLoadDriver(L"MmMapLockedPagesSpecifyCache", FALSE);
@@ -29,150 +51,90 @@ START_TEST(MmMapLockedPagesSpecifyCache)
 
     // Less than a page
     SET_BUFFER_LENGTH(BufferLength, 2048);
-    QueryBuffer.Length = BufferLength;
-    QueryBuffer.Buffer = NULL;
-    QueryBuffer.Cached = FALSE;
     Length = sizeof(QUERY_BUFFER);
+    FILL_QUERY_BUFFER(QueryBuffer, BufferLength, FALSE);
     ok(KmtSendBufferToDriver(IOCTL_QUERY_BUFFER, &QueryBuffer, sizeof(QUERY_BUFFER), &Length) == ERROR_SUCCESS, "\n");
     ok_eq_int(QueryBuffer.Length, BufferLength);
-    ok_eq_pointer(QueryBuffer.Buffer, NULL);
+    ok(QueryBuffer.Buffer != NULL, "Buffer is NULL\n");
 
-    ReadBuffer.Buffer = QueryBuffer.Buffer;
     Length = 0;
+    FILL_READ_BUFFER(QueryBuffer, ReadBuffer);
     ok(KmtSendBufferToDriver(IOCTL_READ_BUFFER, &ReadBuffer, sizeof(READ_BUFFER), &Length) == ERROR_SUCCESS, "\n");
 
-    QueryBuffer.Length = BufferLength;
-    QueryBuffer.Buffer = NULL;
-    QueryBuffer.Cached = TRUE;
     Length = sizeof(QUERY_BUFFER);
+    FILL_QUERY_BUFFER(QueryBuffer, BufferLength, TRUE);
     ok(KmtSendBufferToDriver(IOCTL_QUERY_BUFFER, &QueryBuffer, sizeof(QUERY_BUFFER), &Length) == ERROR_SUCCESS, "\n");
     ok_eq_int(QueryBuffer.Length, BufferLength);
     ok(QueryBuffer.Buffer != NULL, "Buffer is NULL\n");
 
-    ReadBuffer.Buffer = QueryBuffer.Buffer;
-    if (!skip(QueryBuffer.Buffer != NULL, "Buffer is NULL\n"))
-    {
-        ReadBuffer.Pattern = WRITE_PATTERN;
-        ReadBuffer.Length = QueryBuffer.Length;
-        Buffer = QueryBuffer.Buffer;
-        for (i = 0; i < ReadBuffer.Length / sizeof(ULONG); ++i)
-        {
-            Buffer[i] = ReadBuffer.Pattern;
-        }
-    }
-
     Length = 0;
+    FILL_READ_BUFFER(QueryBuffer, ReadBuffer);
     ok(KmtSendBufferToDriver(IOCTL_READ_BUFFER, &ReadBuffer, sizeof(READ_BUFFER), &Length) == ERROR_SUCCESS, "\n");
 
     // 1 page
     SET_BUFFER_LENGTH(BufferLength, 4096);
-    QueryBuffer.Length = BufferLength;
-    QueryBuffer.Buffer = NULL;
-    QueryBuffer.Cached = FALSE;
     Length = sizeof(QUERY_BUFFER);
+    FILL_QUERY_BUFFER(QueryBuffer, BufferLength, FALSE);
     ok(KmtSendBufferToDriver(IOCTL_QUERY_BUFFER, &QueryBuffer, sizeof(QUERY_BUFFER), &Length) == ERROR_SUCCESS, "\n");
     ok_eq_int(QueryBuffer.Length, BufferLength);
-    ok_eq_pointer(QueryBuffer.Buffer, NULL);
+    ok(QueryBuffer.Buffer != NULL, "Buffer is NULL\n");
 
-    ReadBuffer.Buffer = QueryBuffer.Buffer;
     Length = 0;
+    FILL_READ_BUFFER(QueryBuffer, ReadBuffer);
     ok(KmtSendBufferToDriver(IOCTL_READ_BUFFER, &ReadBuffer, sizeof(READ_BUFFER), &Length) == ERROR_SUCCESS, "\n");
 
-    QueryBuffer.Length = BufferLength;
-    QueryBuffer.Buffer = NULL;
-    QueryBuffer.Cached = TRUE;
     Length = sizeof(QUERY_BUFFER);
+    FILL_QUERY_BUFFER(QueryBuffer, BufferLength, TRUE);
     ok(KmtSendBufferToDriver(IOCTL_QUERY_BUFFER, &QueryBuffer, sizeof(QUERY_BUFFER), &Length) == ERROR_SUCCESS, "\n");
     ok_eq_int(QueryBuffer.Length, BufferLength);
     ok(QueryBuffer.Buffer != NULL, "Buffer is NULL\n");
 
-    ReadBuffer.Buffer = QueryBuffer.Buffer;
-    if (!skip(QueryBuffer.Buffer != NULL, "Buffer is NULL\n"))
-    {
-        ReadBuffer.Pattern = WRITE_PATTERN;
-        ReadBuffer.Length = QueryBuffer.Length;
-        Buffer = QueryBuffer.Buffer;
-        for (i = 0; i < ReadBuffer.Length / sizeof(ULONG); ++i)
-        {
-            Buffer[i] = ReadBuffer.Pattern;
-        }
-    }
-
     Length = 0;
+    FILL_READ_BUFFER(QueryBuffer, ReadBuffer);
     ok(KmtSendBufferToDriver(IOCTL_READ_BUFFER, &ReadBuffer, sizeof(READ_BUFFER), &Length) == ERROR_SUCCESS, "\n");
 
     // more than 1 page
     SET_BUFFER_LENGTH(BufferLength, 4096 + 2048);
-    QueryBuffer.Length = BufferLength;
-    QueryBuffer.Buffer = NULL;
-    QueryBuffer.Cached = FALSE;
     Length = sizeof(QUERY_BUFFER);
+    FILL_QUERY_BUFFER(QueryBuffer, BufferLength, FALSE);
     ok(KmtSendBufferToDriver(IOCTL_QUERY_BUFFER, &QueryBuffer, sizeof(QUERY_BUFFER), &Length) == ERROR_SUCCESS, "\n");
     ok_eq_int(QueryBuffer.Length, BufferLength);
-    ok_eq_pointer(QueryBuffer.Buffer, NULL);
+    ok(QueryBuffer.Buffer != NULL, "Buffer is NULL\n");
 
-    ReadBuffer.Buffer = QueryBuffer.Buffer;
     Length = 0;
+    FILL_READ_BUFFER(QueryBuffer, ReadBuffer);
     ok(KmtSendBufferToDriver(IOCTL_READ_BUFFER, &ReadBuffer, sizeof(READ_BUFFER), &Length) == ERROR_SUCCESS, "\n");
 
-    QueryBuffer.Length = BufferLength;
-    QueryBuffer.Buffer = NULL;
-    QueryBuffer.Cached = TRUE;
     Length = sizeof(QUERY_BUFFER);
+    FILL_QUERY_BUFFER(QueryBuffer, BufferLength, TRUE);
     ok(KmtSendBufferToDriver(IOCTL_QUERY_BUFFER, &QueryBuffer, sizeof(QUERY_BUFFER), &Length) == ERROR_SUCCESS, "\n");
     ok_eq_int(QueryBuffer.Length, BufferLength);
     ok(QueryBuffer.Buffer != NULL, "Buffer is NULL\n");
 
-    ReadBuffer.Buffer = QueryBuffer.Buffer;
-    if (!skip(QueryBuffer.Buffer != NULL, "Buffer is NULL\n"))
-    {
-        ReadBuffer.Pattern = WRITE_PATTERN;
-        ReadBuffer.Length = QueryBuffer.Length;
-        Buffer = QueryBuffer.Buffer;
-        for (i = 0; i < ReadBuffer.Length / sizeof(ULONG); ++i)
-        {
-            Buffer[i] = ReadBuffer.Pattern;
-        }
-    }
-
     Length = 0;
+    FILL_READ_BUFFER(QueryBuffer, ReadBuffer);
     ok(KmtSendBufferToDriver(IOCTL_READ_BUFFER, &ReadBuffer, sizeof(READ_BUFFER), &Length) == ERROR_SUCCESS, "\n");
 
     // 2 pages
     SET_BUFFER_LENGTH(BufferLength, 2 * 4096);
-    QueryBuffer.Length = BufferLength;
-    QueryBuffer.Buffer = NULL;
-    QueryBuffer.Cached = FALSE;
     Length = sizeof(QUERY_BUFFER);
+    FILL_QUERY_BUFFER(QueryBuffer, BufferLength, FALSE);
     ok(KmtSendBufferToDriver(IOCTL_QUERY_BUFFER, &QueryBuffer, sizeof(QUERY_BUFFER), &Length) == ERROR_SUCCESS, "\n");
     ok_eq_int(QueryBuffer.Length, BufferLength);
-    ok_eq_pointer(QueryBuffer.Buffer, NULL);
+    ok(QueryBuffer.Buffer != NULL, "Buffer is NULL\n");
 
-    ReadBuffer.Buffer = QueryBuffer.Buffer;
     Length = 0;
+    FILL_READ_BUFFER(QueryBuffer, ReadBuffer);
     ok(KmtSendBufferToDriver(IOCTL_READ_BUFFER, &ReadBuffer, sizeof(READ_BUFFER), &Length) == ERROR_SUCCESS, "\n");
 
-    QueryBuffer.Length = BufferLength;
-    QueryBuffer.Buffer = NULL;
-    QueryBuffer.Cached = TRUE;
     Length = sizeof(QUERY_BUFFER);
+    FILL_QUERY_BUFFER(QueryBuffer, BufferLength, TRUE);
     ok(KmtSendBufferToDriver(IOCTL_QUERY_BUFFER, &QueryBuffer, sizeof(QUERY_BUFFER), &Length) == ERROR_SUCCESS, "\n");
     ok_eq_int(QueryBuffer.Length, BufferLength);
     ok(QueryBuffer.Buffer != NULL, "Buffer is NULL\n");
 
-    ReadBuffer.Buffer = QueryBuffer.Buffer;
-    if (!skip(QueryBuffer.Buffer != NULL, "Buffer is NULL\n"))
-    {
-        ReadBuffer.Pattern = WRITE_PATTERN;
-        ReadBuffer.Length = QueryBuffer.Length;
-        Buffer = QueryBuffer.Buffer;
-        for (i = 0; i < ReadBuffer.Length / sizeof(ULONG); ++i)
-        {
-            Buffer[i] = ReadBuffer.Pattern;
-        }
-    }
-
     Length = 0;
+    FILL_READ_BUFFER(QueryBuffer, ReadBuffer);
     ok(KmtSendBufferToDriver(IOCTL_READ_BUFFER, &ReadBuffer, sizeof(READ_BUFFER), &Length) == ERROR_SUCCESS, "\n");
 
     // more than 2 pages
@@ -183,10 +145,10 @@ START_TEST(MmMapLockedPagesSpecifyCache)
     Length = sizeof(QUERY_BUFFER);
     ok(KmtSendBufferToDriver(IOCTL_QUERY_BUFFER, &QueryBuffer, sizeof(QUERY_BUFFER), &Length) == ERROR_SUCCESS, "\n");
     ok_eq_int(QueryBuffer.Length, BufferLength);
-    ok_eq_pointer(QueryBuffer.Buffer, NULL);
+    ok(QueryBuffer.Buffer != NULL, "Buffer is NULL\n");
 
-    ReadBuffer.Buffer = QueryBuffer.Buffer;
     Length = 0;
+    FILL_READ_BUFFER(QueryBuffer, ReadBuffer);
     ok(KmtSendBufferToDriver(IOCTL_READ_BUFFER, &ReadBuffer, sizeof(READ_BUFFER), &Length) == ERROR_SUCCESS, "\n");
 
     QueryBuffer.Length = BufferLength;
@@ -197,19 +159,8 @@ START_TEST(MmMapLockedPagesSpecifyCache)
     ok_eq_int(QueryBuffer.Length, BufferLength);
     ok(QueryBuffer.Buffer != NULL, "Buffer is NULL\n");
 
-    ReadBuffer.Buffer = QueryBuffer.Buffer;
-    if (!skip(QueryBuffer.Buffer != NULL, "Buffer is NULL\n"))
-    {
-        ReadBuffer.Pattern = WRITE_PATTERN;
-        ReadBuffer.Length = QueryBuffer.Length;
-        Buffer = QueryBuffer.Buffer;
-        for (i = 0; i < ReadBuffer.Length / sizeof(ULONG); ++i)
-        {
-            Buffer[i] = ReadBuffer.Pattern;
-        }
-    }
-
     Length = 0;
+    FILL_READ_BUFFER(QueryBuffer, ReadBuffer);
     ok(KmtSendBufferToDriver(IOCTL_READ_BUFFER, &ReadBuffer, sizeof(READ_BUFFER), &Length) == ERROR_SUCCESS, "\n");
 
     KmtCloseDriver();