ReactOS  0.4.14-dev-614-gbfd8a84
ntoskrnl.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPLv2+ - See COPYING in the top level directory
4  * PURPOSE: Test to validate section flags in ntoskrnl
5  * PROGRAMMER: Mark Jansen
6  */
7 
8 #include <apitest.h>
9 #include <strsafe.h>
10 
11 typedef struct KnownSections
12 {
13  const char* Name;
17 
18 static struct KnownSections g_Sections[] = {
19  {
20  ".text",
23  },
24  {
25  ".data",
28  },
29  {
30  ".rsrc",
33  },
34  {
35  ".rdata",
38  },
39  {
40  ".reloc",
42  0
43  },
44  {
45  "INIT",
47  0
48  },
49  { NULL, 0 },
50 };
51 
52 static char* Chr2Str(DWORD value)
53 {
54  static char buf[512];
55  buf[0] = '\0';
56 #define IFX(x) if( value & IMAGE_SCN_##x )\
57  {\
58  if(buf[0]) { StringCchCatA(buf, _countof(buf), "|" ); }\
59  StringCchCatA(buf, _countof(buf), #x );\
60  value &= ~(IMAGE_SCN_##x);\
61  }
62  IFX(TYPE_NO_PAD);
63  IFX(CNT_CODE);
64  IFX(CNT_INITIALIZED_DATA);
65  IFX(CNT_UNINITIALIZED_DATA);
66  IFX(LNK_OTHER);
67  IFX(LNK_INFO);
68  IFX(LNK_REMOVE);
69  IFX(LNK_COMDAT);
70  //IFX(NO_DEFER_SPEC_EXC);
71  //IFX(GPREL);
72  IFX(MEM_FARDATA);
73  IFX(MEM_PURGEABLE);
74  IFX(MEM_16BIT);
75  IFX(MEM_LOCKED);
76  IFX(MEM_PRELOAD);
77  IFX(ALIGN_1BYTES);
78  IFX(ALIGN_2BYTES);
79  IFX(ALIGN_4BYTES);
80  IFX(ALIGN_8BYTES);
81  IFX(ALIGN_16BYTES);
82  IFX(ALIGN_32BYTES);
83  IFX(ALIGN_64BYTES);
84  //IFX(ALIGN_128BYTES);
85  //IFX(ALIGN_256BYTES);
86  //IFX(ALIGN_512BYTES);
87  //IFX(ALIGN_1024BYTES);
88  //IFX(ALIGN_2048BYTES);
89  //IFX(ALIGN_4096BYTES);
90  //IFX(ALIGN_8192BYTES);
91  IFX(LNK_NRELOC_OVFL);
92  IFX(MEM_DISCARDABLE);
93  IFX(MEM_NOT_CACHED);
94  IFX(MEM_NOT_PAGED);
95  IFX(MEM_SHARED);
96  IFX(MEM_EXECUTE);
97  IFX(MEM_READ);
98  IFX(MEM_WRITE);
99  if( value )
100  {
102  }
103  return buf;
104 }
105 
106 
107 START_TEST(ntoskrnl_SectionFlags)
108 {
109  char buf[MAX_PATH];
110  HMODULE mod;
112  StringCchCatA(buf, _countof(buf), "\\ntoskrnl.exe");
113 
115  if( mod != NULL )
116  {
117  // we have to take into account that a datafile is not returned at the exact address it's loaded at.
118  PIMAGE_DOS_HEADER dos = (PIMAGE_DOS_HEADER)(((size_t)mod) & (~0xf));
120  PIMAGE_SECTION_HEADER firstSection;
121  WORD numSections, n;
122 
124  {
125  skip("Couldn't find ntoskrnl.exe dos header\n");
126  FreeLibrary(mod);
127  return;
128  }
129  nt = (PIMAGE_NT_HEADERS)( ((PBYTE)dos) + dos->e_lfanew );
131  {
132  skip("Couldn't find ntoskrnl.exe nt header\n");
133  FreeLibrary(mod);
134  return;
135  }
136  firstSection = IMAGE_FIRST_SECTION(nt);
137  numSections = nt->FileHeader.NumberOfSections;
138  for( n = 0; n < numSections; ++n )
139  {
140  PIMAGE_SECTION_HEADER section = firstSection + n;
141  char name[9] = {0};
142  size_t i;
144 
145  for( i = 0; g_Sections[i].Name; ++i )
146  {
147  if( !_strnicmp(name, g_Sections[i].Name, 8) )
148  {
149  if( g_Sections[i].Required )
150  {
151  DWORD Flags = g_Sections[i].Required & section->Characteristics;
153  "Missing required Characteristics on %s: %s\n",
155  }
156  if( g_Sections[i].Disallowed )
157  {
158  DWORD Flags = g_Sections[i].Disallowed & section->Characteristics;
159  ok(!Flags, "Disallowed section Characteristics on %s: %s\n",
160  name, Chr2Str(section->Characteristics));
161  }
162  break;
163  }
164  }
165  }
166  FreeLibrary(mod);
167  }
168  else
169  {
170  skip("Couldn't load ntoskrnl.exe as datafile\n");
171  }
172 }
173 
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define _countof(array)
Definition: fontsub.cpp:30
GLdouble n
Definition: glext.h:7729
const char * Name
Definition: ntoskrnl.c:13
UINT WINAPI GetSystemDirectoryA(OUT LPSTR lpBuffer, IN UINT uSize)
Definition: path.c:2282
#define IMAGE_SCN_MEM_READ
Definition: ntimage.h:240
DWORD Required
Definition: ntoskrnl.c:14
Definition: parser.c:55
static char * Chr2Str(DWORD value)
Definition: ntoskrnl.c:52
IMAGE_NT_HEADERS nt
Definition: module.c:50
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
STRSAFEAPI StringCchCopyNA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc, size_t cchToCopy)
Definition: strsafe.h:230
#define IMAGE_FIRST_SECTION(NtHeader)
Definition: ntimage.h:427
struct KnownSections KnownSections
STRSAFEAPI StringCchCatA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc)
Definition: strsafe.h:320
_Check_return_ _CRTIMP int __cdecl _strnicmp(_In_reads_or_z_(_MaxCount) const char *_Str1, _In_reads_or_z_(_MaxCount) const char *_Str2, _In_ size_t _MaxCount)
STRSAFEAPI StringCchPrintfA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszFormat,...)
Definition: strsafe.h:520
#define IFX(x)
smooth NULL
Definition: ftsmooth.c:416
struct _IMAGE_DOS_HEADER * PIMAGE_DOS_HEADER
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
#define IMAGE_NT_SIGNATURE
Definition: pedump.c:93
#define FreeLibrary(x)
Definition: compat.h:413
#define IMAGE_SCN_CNT_INITIALIZED_DATA
Definition: ntimage.h:231
#define MAX_PATH
Definition: compat.h:26
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned long DWORD
Definition: ntddk_ex.h:95
#define MEM_READ
Definition: ws2san.h:35
#define IMAGE_SCN_MEM_WRITE
Definition: ntimage.h:241
IMAGE_DOS_HEADER dos
Definition: module.c:49
#define IMAGE_SCN_CNT_CODE
Definition: ntimage.h:230
DWORD Disallowed
Definition: ntoskrnl.c:15
#define LOAD_LIBRARY_AS_DATAFILE
Definition: winbase.h:338
static struct KnownSections g_Sections[]
Definition: ntoskrnl.c:18
#define MEM_WRITE
Definition: ws2san.h:36
#define ok(value,...)
Definition: atltest.h:57
#define IMAGE_SCN_MEM_NOT_PAGED
Definition: ntimage.h:237
#define skip(...)
Definition: atltest.h:64
Definition: name.c:38
const char * PCSTR
Definition: typedefs.h:51
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
Definition: loader.c:159
struct _MEMORY_AREA struct _MM_REQUIRED_RESOURCES * Required
Definition: newmm.h:66
BYTE * PBYTE
Definition: pedump.c:66
START_TEST(ntoskrnl_SectionFlags)
Definition: ntoskrnl.c:107
#define IMAGE_SCN_MEM_EXECUTE
Definition: ntimage.h:239
static int mod
Definition: i386-dis.c:1273
PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS
Definition: ntddk_ex.h:187
#define IMAGE_SCN_MEM_DISCARDABLE
Definition: ntimage.h:235