-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsysbasic.c
More file actions
177 lines (141 loc) · 3.96 KB
/
Copy pathsysbasic.c
File metadata and controls
177 lines (141 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (c) 2011 The Native Client Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
* NaCl tests for simple syscalls not using newlib
*/
#define CHECK_ERRNO 1
#include "native_client/src/trusted/service_runtime/include/sys/nacl_syscalls.h"
/*
* These must come after <sys/nacl_syscalls.h> when using nacl-newlib
* otherwise we get errors about conflicting types. This is a bug in
* nacl-newlib.
*/
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
/* NOTE: defining CHECK_ERRNO pulls in some newlib magic (reent.h, etc.) */
#if defined(CHECK_ERRNO)
#include <errno.h>
#endif
int mystrlen(const char* s) {
int count = 0;
while(*s++) ++count;
return count;
}
#define myprint(s) write(1, s, mystrlen(s))
void hextochar(int n, char buffer[9]) {
int i;
buffer[8] = 0;
for (i=0; i < 8; ++i) {
int nibble = 0xf & (n >> (4 * (7 - i)));
if (nibble <= 9) {
buffer[i] = nibble + '0';
} else {
buffer[i] = nibble - 10 + 'A';
}
}
}
void PrintInt(int i) {
char buffer[16];
hextochar(i, buffer);
myprint(buffer);
myprint("\n");
}
void Error(const char* message) {
myprint("ERROR: ");
myprint(message);
_exit(-1);
}
#if defined(CHECK_ERRNO)
void CheckErrno(int expected) {
char buffer[16];
myprint("\nerrno\n");
hextochar(errno, buffer);
myprint(buffer);
myprint("\n");
if (expected != errno) Error("bad errno value\n");
}
#else
void CheckErrno(int expected) {
expected = expected;
}
#endif
const int kExitOk = 69;
const int kMmapBase = 0xa00000;
const int kMmapSize = 0x10000;
const int kInvalidFileDescriptor = 100;
/*
* This is defined by the linker as the address of the end of our data segment.
* That's where the break starts out by default.
*/
extern char end;
int main(void) {
/*
* Round up to the end of the page that's our last initial data page.
* Then add 10MB for good measure to be out of the way of any allocations
* that might have been done before we got here.
*/
const int sysbrkBase = ((((intptr_t) &end) + 0xffff) & -0x10000) + (10 << 20);
void *map_result;
int i;
CheckErrno(0);
myprint("\nnull_syscall()\n");
null_syscall();
myprint("\nsysbreak()\n");
i = (int) sysbrk(0);
PrintInt(i);
if (0 == i) Error("bad sysbrk() value\n");
if (sysbrkBase < i) {
Error("INTERNAL ERROR: sysbrkBase too small\n");
}
myprint("\nsysbrk()\n");
i = (int) sysbrk((void *) sysbrkBase);
PrintInt(i);
if (sysbrkBase != i) Error("bad sysbrk() value\n");
myprint("\nmmap()\n");
i = (int) mmap(0, kMmapSize,
PROT_READ, MAP_ANONYMOUS | MAP_SHARED,
-1, 0);
PrintInt(i);
if (-1 == i) Error("bad mmap() value\n");
myprint("\nmunmap()\n");
i = (int) munmap((void *)i, kMmapSize);
PrintInt(i);
if (0 != i) Error("bad munmap() value\n");
myprint("\nmmap()\n");
map_result = mmap((void *)kMmapBase, kMmapSize,
PROT_READ, MAP_ANONYMOUS | MAP_SHARED,
-1, 0);
i = (int) map_result;
PrintInt(i);
/*
* It is an ERROR to expect i to be kMapBase. Since the mmap
* syscall were not made with MAP_FIXED, the kernel is free to use
* the provided address as a hint -- or to ignore it altogether.
* Exactly what behavior we implement may vary from release to
* release -- thus, the only reasonable value to compare this with
* is MAP_FAILED.
*/
if (MAP_FAILED == map_result) Error("bad: mmap() failed\n");
myprint("\nmunmap()\n");
i = (int) munmap(map_result, kMmapSize);
PrintInt(i);
if (0 != i) Error("bad munmap() value\n");
CheckErrno(0);
/* Some expect failures */
myprint("\nclose()\n");
i = (int) close(kInvalidFileDescriptor);
PrintInt(i);
if (~0 != i) Error("bad close value\n");
CheckErrno(EBADF);
myprint("\nclock()\n");
i = clock();
PrintInt(i);
if (0 > i) Error("bad clock value\n");
myprint("before _exit()\n");
_exit(kExitOk);
return 0;
}