ReactOS  0.4.13-dev-455-g28ed234
NtContinue.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPL - See COPYING in the top level directory
4  * PURPOSE: Test for NtContinue
5  * PROGRAMMER:
6  */
7 
8 #include "precomp.h"
9 
10 #include <setjmp.h>
11 #include <time.h>
12 
13 #ifdef _M_IX86
14 #define NTC_SEGMENT_BITS (0xFFFF)
15 #define NTC_EFLAGS_BITS (0x3C0CD5)
16 #endif
17 
18 void continuePoint(void);
19 
20 static jmp_buf jmpbuf;
22 static unsigned int nRandBytes;
23 
24 static int initrand(void)
25 {
26  unsigned int nRandMax;
27  unsigned int nRandMaxBits;
28  time_t tLoc;
29 
30  nRandMax = RAND_MAX;
31  for(nRandMaxBits = 0; nRandMax != 0; nRandMax >>= 1, ++ nRandMaxBits);
32  nRandBytes = nRandMaxBits / CHAR_BIT;
33  //assert(nRandBytes != 0);
34  srand((unsigned)(time(&tLoc) & UINT_MAX));
35  return 1;
36 }
37 
38 static void randbytes(void * p, size_t n)
39 {
40  unsigned char * b;
41  size_t i;
42  int r = rand();
43 
44  b = (unsigned char *)p;
45  for(i = 0; i < n; ++ i)
46  {
47  if(i % nRandBytes == 0)
48  r = rand();
49  b[i] = (unsigned char)(r & UCHAR_MAX);
50  r >>= CHAR_BIT;
51  }
52 }
53 
54 static ULONG randULONG(void)
55 {
56  ULONG n;
57  randbytes(&n, sizeof(n));
58  return n;
59 }
60 
61 void check(CONTEXT * pContext)
62 {
63 #ifdef _M_IX86
64  ok(pContext->ContextFlags == CONTEXT_FULL,
65  "ContextFlags=0x%lx\n", pContext->ContextFlags);
66 
67  /* Random data segments */
68  ok((pContext->SegGs & NTC_SEGMENT_BITS) ==
69  (continueContext.SegGs & NTC_SEGMENT_BITS),
70  "SegGs=0x%lx / 0x%lx\n", pContext->SegGs, continueContext.SegGs);
71 
72  ok((pContext->SegFs & NTC_SEGMENT_BITS) ==
73  (continueContext.SegFs & NTC_SEGMENT_BITS),
74  "SegFs=0x%lx / 0x%lx\n", pContext->SegFs, continueContext.SegFs);
75 
76  ok((pContext->SegEs & NTC_SEGMENT_BITS) ==
77  (continueContext.SegEs & NTC_SEGMENT_BITS),
78  "SegEs=0x%lx / 0x%lx\n", pContext->SegEs, continueContext.SegEs);
79 
80  ok((pContext->SegDs & NTC_SEGMENT_BITS) ==
81  (continueContext.SegDs & NTC_SEGMENT_BITS),
82  "SegDs=0x%lx / 0x%lx\n", pContext->SegDs, continueContext.SegDs);
83 
84  /* Integer registers */
85  ok(pContext->Edi == continueContext.Edi,
86  "Edi: 0x%lx != 0x%lx\n", pContext->Edi, continueContext.Edi);
87  ok(pContext->Esi == continueContext.Esi,
88  "Esi: 0x%lx != 0x%lx\n", pContext->Esi, continueContext.Esi);
89  ok(pContext->Ebx == continueContext.Ebx,
90  "Ebx: 0x%lx != 0x%lx\n", pContext->Ebx, continueContext.Ebx);
91  ok(pContext->Edx == continueContext.Edx,
92  "Edx: 0x%lx != 0x%lx\n", pContext->Edx, continueContext.Edx);
93  ok(pContext->Ecx == continueContext.Ecx,
94  "Ecx: 0x%lx != 0x%lx\n", pContext->Ecx, continueContext.Ecx);
95  ok(pContext->Eax == continueContext.Eax,
96  "Eax: 0x%lx != 0x%lx\n", pContext->Eax, continueContext.Eax);
97 
98  /* Control registers and segments */
99  ok(pContext->Ebp == continueContext.Ebp,
100  "Ebp: 0x%lx != 0x%lx\n", pContext->Ebp, continueContext.Ebp);
101  ok(pContext->Eip == continueContext.Eip,
102  "Eip: 0x%lx != 0x%lx\n", pContext->Eip, continueContext.Eip);
103  ok(pContext->Esp == continueContext.Esp,
104  "Esp: 0x%lx != 0x%lx\n", pContext->Esp, continueContext.Esp);
105 
106  ok((pContext->SegCs & NTC_SEGMENT_BITS) ==
107  (continueContext.SegCs & NTC_SEGMENT_BITS),
108  "SegCs: 0x%lx != 0x%lx\n", pContext->SegCs, continueContext.SegCs);
109 
110  ok((pContext->EFlags & NTC_EFLAGS_BITS) ==
111  (continueContext.EFlags & NTC_EFLAGS_BITS),
112  "EFlags: 0x%lx != 0x%lx\n", pContext->EFlags, continueContext.EFlags);
113 
114  ok((pContext->SegSs & NTC_SEGMENT_BITS) ==
115  (continueContext.SegSs & NTC_SEGMENT_BITS),
116  "SegSs: 0x%lx != 0x%lx\n", pContext->SegSs, continueContext.SegSs);
117 #endif
118 
119  /* Return where we came from */
120  longjmp(jmpbuf, 1);
121 }
122 
124 {
125 #ifdef __RUNTIME_CHECKS__
126  skip("This test breaks MSVC runtime checks!\n");
127  return;
128 #endif /* __RUNTIME_CHECKS__ */
129  initrand();
130 
131  /* First time */
132  if(setjmp(jmpbuf) == 0)
133  {
134  CONTEXT bogus;
135 
138 
139 #ifdef _M_IX86
141 
142  /* Fill the integer registers with random values */
150 
151  /* Randomize all the allowed flags (determined experimentally with WinDbg) */
152  continueContext.EFlags = randULONG() & 0x3C0CD5;
153 
154  /* Randomize the stack pointer as much as possible */
155  continueContext.Esp = (ULONG)(((ULONG_PTR)&bogus) & 0xFFFFFFFF) +
156  sizeof(bogus) - (randULONG() & 0xF) * 4;
157 
158  /* continuePoint() is implemented in assembler */
159  continueContext.Eip = (ULONG)((ULONG_PTR)continuePoint & 0xFFFFFFF);
160 
161  /* Can't do a lot about segments */
162 #endif
163 
165  ok(0, "should never get here\n");
166  }
167 
168  /* Second time */
169  return;
170 }
ULONG Esp
Definition: nt_native.h:1479
static unsigned int nRandBytes
Definition: NtContinue.c:22
ULONG Eip
Definition: nt_native.h:1476
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
ULONG Ecx
Definition: nt_native.h:1467
START_TEST(NtContinue)
Definition: NtContinue.c:123
void __cdecl srand(_In_ unsigned int _Seed)
GLdouble n
Definition: glext.h:7729
#define CHAR_BIT
Definition: urlcache.c:57
ULONG SegGs
Definition: nt_native.h:1453
#define CONTEXT_FULL
Definition: compat.h:270
ULONG SegFs
Definition: nt_native.h:1454
__u16 time
Definition: mkdosfs.c:366
ULONG SegDs
Definition: nt_native.h:1456
uint32_t ULONG_PTR
Definition: typedefs.h:63
BOOL WINAPI GetThreadContext(IN HANDLE hThread, OUT LPCONTEXT lpContext)
Definition: thread.c:462
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
HANDLE WINAPI GetCurrentThread(VOID)
Definition: proc.c:1178
NTSTATUS NTAPI NtContinue(IN PCONTEXT Context, IN BOOLEAN TestAlert)
Definition: except.c:212
_Check_return_ int __cdecl rand(void)
Definition: rand.c:10
ULONG SegEs
Definition: nt_native.h:1455
ULONG Esi
Definition: nt_native.h:1464
#define ok(value,...)
ULONG SegCs
Definition: nt_native.h:1477
ULONG Edx
Definition: nt_native.h:1466
unsigned char
Definition: typeof.h:29
ULONG EFlags
Definition: nt_native.h:1478
ULONG Ebx
Definition: nt_native.h:1465
int longjmp(jmp_buf buf, int retval)
#define b
Definition: ke_i.h:79
ULONG ContextFlags
Definition: compat.h:331
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
static CONTEXT continueContext
Definition: NtContinue.c:21
static ULONG randULONG(void)
Definition: NtContinue.c:54
void continuePoint(void)
void check(CONTEXT *pContext)
Definition: NtContinue.c:61
ULONG Eax
Definition: nt_native.h:1468
#define RAND_MAX
Definition: stdlib.h:99
#define UINT_MAX
Definition: limits.h:41
static int initrand(void)
Definition: NtContinue.c:24
ULONG SegSs
Definition: nt_native.h:1480
__kernel_time_t time_t
Definition: linux.h:252
#define UCHAR_MAX
Definition: limits.h:25
#define setjmp
Definition: setjmp.h:183
#define skip(...)
unsigned int ULONG
Definition: retypes.h:1
static void randbytes(void *p, size_t n)
Definition: NtContinue.c:38
long jmp_buf[100]
Definition: of.h:11
GLfloat GLfloat p
Definition: glext.h:8902
static jmp_buf jmpbuf
Definition: NtContinue.c:20
ULONG Ebp
Definition: nt_native.h:1475
ULONG Edi
Definition: nt_native.h:1463