4
4
#include <sys/task.h>
5
5
6
6
#include "private/stdio.h"
7
+ #include "private/utils.h"
7
8
8
9
/* syscall wrappers */
9
10
#define _ (name , num , rettype , arglist ) static rettype _##name arglist;
@@ -18,8 +19,13 @@ static const void *syscall_table[SYS_COUNT] = {SYSCALL_TABLE};
18
19
/* Weak, generic dispatcher */
19
20
int syscall (int num , void * a1 , void * a2 , void * a3 )
20
21
{
21
- if (num <= 0 || num >= SYS_COUNT )
22
+ if (unlikely ( num <= 0 || num >= SYS_COUNT ) )
22
23
return - ENOSYS ;
24
+
25
+ /* safety check for valid function pointer */
26
+ if (unlikely (!syscall_table [num ]))
27
+ return - ENOSYS ;
28
+
23
29
return ((int (* )(void * , void * , void * )) syscall_table [num ])(a1 , a2 , a3 );
24
30
}
25
31
@@ -84,40 +90,71 @@ static int _sbrk(int incr)
84
90
extern uint32_t _end , _stack ;
85
91
static char * brk = (char * ) & _end ;
86
92
char * prev = brk ;
87
- if (brk + incr >= (char * ) & _stack ) {
93
+
94
+ if (unlikely (incr < 0 )) {
95
+ errno = EINVAL ;
96
+ return -1 ;
97
+ }
98
+
99
+ if (unlikely (brk + incr >= (char * ) & _stack )) {
88
100
errno = ENOMEM ;
89
101
return -1 ;
90
102
}
103
+
91
104
brk += incr ;
92
105
return (int ) prev ;
93
106
}
94
107
95
108
static int _usleep (int usec )
96
109
{
110
+ if (unlikely (usec < 0 )) {
111
+ errno = EINVAL ;
112
+ return -1 ;
113
+ }
114
+
97
115
errno = EINTR ;
98
116
return 0 ;
99
117
}
100
118
101
119
static int _stat (char * file , struct stat * st )
102
120
{
121
+ if (unlikely (!st )) {
122
+ errno = EFAULT ;
123
+ return -1 ;
124
+ }
125
+
103
126
st -> st_mode = S_IFCHR ;
104
127
return 0 ;
105
128
}
106
129
107
130
static int _open (char * path , int flags )
108
131
{
132
+ errno = ENOENT ;
109
133
return -1 ;
110
134
}
111
135
112
136
static int _close (int file )
113
137
{
138
+ if (unlikely (file < 0 )) {
139
+ errno = EBADF ;
140
+ return -1 ;
141
+ }
114
142
return -1 ;
115
143
}
116
144
117
145
static int _read (int file , char * ptr , int len )
118
146
{
119
- int idx ;
147
+ if (unlikely (!ptr || len < 0 )) {
148
+ errno = EFAULT ;
149
+ return -1 ;
150
+ }
151
+
152
+ if (unlikely (file < 0 )) {
153
+ errno = EBADF ;
154
+ return -1 ;
155
+ }
120
156
157
+ int idx ;
121
158
for (idx = 0 ; idx < len ; idx ++ )
122
159
* ptr ++ = _getchar ();
123
160
@@ -126,8 +163,17 @@ static int _read(int file, char *ptr, int len)
126
163
127
164
static int _write (int file , char * ptr , int len )
128
165
{
129
- int idx ;
166
+ if (unlikely (!ptr || len < 0 )) {
167
+ errno = EFAULT ;
168
+ return -1 ;
169
+ }
170
+
171
+ if (unlikely (file < 0 )) {
172
+ errno = EBADF ;
173
+ return -1 ;
174
+ }
130
175
176
+ int idx ;
131
177
for (idx = 0 ; idx < len ; idx ++ )
132
178
_putchar (* ptr ++ );
133
179
@@ -136,37 +182,60 @@ static int _write(int file, char *ptr, int len)
136
182
137
183
static int _lseek (int file , int ptr , int dir )
138
184
{
185
+ if (unlikely (file < 0 )) {
186
+ errno = EBADF ;
187
+ return -1 ;
188
+ }
139
189
return 0 ;
140
190
}
141
191
142
192
static int _chdir (const char * path )
143
193
{
144
- errno = EFAULT ;
194
+ if (unlikely (!path )) {
195
+ errno = EFAULT ;
196
+ return -1 ;
197
+ }
198
+ errno = ENOENT ;
145
199
return -1 ;
146
200
}
147
201
148
202
static int _mknod (const char * path , int mode , int dev )
149
203
{
150
- errno = EFAULT ;
204
+ if (unlikely (!path )) {
205
+ errno = EFAULT ;
206
+ return -1 ;
207
+ }
208
+ errno = EPERM ;
151
209
return -1 ;
152
210
}
153
211
154
212
static int _unlink (char * name )
155
213
{
214
+ if (unlikely (!name )) {
215
+ errno = EFAULT ;
216
+ return -1 ;
217
+ }
156
218
errno = ENOENT ;
157
219
return -1 ;
158
220
}
159
221
160
222
static int _link (char * old , char * new )
161
223
{
224
+ if (unlikely (!old || !new )) {
225
+ errno = EFAULT ;
226
+ return -1 ;
227
+ }
162
228
errno = EMLINK ;
163
229
return -1 ;
164
230
}
165
231
166
232
/* Linmo syscalls (wrapper implementation) */
167
233
168
- int _tadd (void * task , int stack_size )
234
+ static int _tadd (void * task , int stack_size )
169
235
{
236
+ if (unlikely (!task || stack_size <= 0 ))
237
+ return - EINVAL ;
238
+
170
239
return mo_task_spawn (task , stack_size );
171
240
}
172
241
@@ -175,8 +244,11 @@ int sys_task_add(void *task, int stack_size)
175
244
return syscall (SYS_tadd , task , (void * ) stack_size , 0 );
176
245
}
177
246
178
- int _tcancel (int id )
247
+ static int _tcancel (int id )
179
248
{
249
+ if (unlikely (id <= 0 ))
250
+ return - EINVAL ;
251
+
180
252
return mo_task_cancel (id );
181
253
}
182
254
@@ -185,36 +257,36 @@ int sys_task_cancel(int id)
185
257
return syscall (SYS_tcancel , (void * ) id , 0 , 0 );
186
258
}
187
259
188
- int _tyield (void )
260
+ static int _tyield (void )
189
261
{
190
262
mo_task_yield ();
191
-
192
263
return 0 ;
193
264
}
194
265
195
266
int sys_task_yield (void )
196
267
{
197
- syscall (SYS_tyield , 0 , 0 , 0 );
198
-
199
- return 0 ;
268
+ return syscall (SYS_tyield , 0 , 0 , 0 );
200
269
}
201
270
202
- int _tdelay (int ticks )
271
+ static int _tdelay (int ticks )
203
272
{
204
- mo_task_delay (ticks );
273
+ if (unlikely (ticks < 0 ))
274
+ return - EINVAL ;
205
275
276
+ mo_task_delay (ticks );
206
277
return 0 ;
207
278
}
208
279
209
280
int sys_task_delay (int ticks )
210
281
{
211
- syscall (SYS_tdelay , (void * ) ticks , 0 , 0 );
212
-
213
- return 0 ;
282
+ return syscall (SYS_tdelay , (void * ) ticks , 0 , 0 );
214
283
}
215
284
216
- int _tsuspend (int id )
285
+ static int _tsuspend (int id )
217
286
{
287
+ if (unlikely (id <= 0 ))
288
+ return - EINVAL ;
289
+
218
290
return mo_task_suspend (id );
219
291
}
220
292
@@ -223,8 +295,11 @@ int sys_task_suspend(int id)
223
295
return syscall (SYS_tsuspend , (void * ) id , 0 , 0 );
224
296
}
225
297
226
- int _tresume (int id )
298
+ static int _tresume (int id )
227
299
{
300
+ if (unlikely (id <= 0 ))
301
+ return - EINVAL ;
302
+
228
303
return mo_task_resume (id );
229
304
}
230
305
@@ -233,9 +308,11 @@ int sys_task_resume(int id)
233
308
return syscall (SYS_tresume , (void * ) id , 0 , 0 );
234
309
}
235
310
236
-
237
- int _tpriority (int id , int priority )
311
+ static int _tpriority (int id , int priority )
238
312
{
313
+ if (unlikely (id <= 0 ))
314
+ return - EINVAL ;
315
+
239
316
return mo_task_priority (id , priority );
240
317
}
241
318
@@ -244,7 +321,7 @@ int sys_task_priority(int id, int priority)
244
321
return syscall (SYS_tpriority , (void * ) id , (void * ) priority , 0 );
245
322
}
246
323
247
- int _tid (void )
324
+ static int _tid (void )
248
325
{
249
326
return mo_task_id ();
250
327
}
@@ -254,21 +331,18 @@ int sys_task_id(void)
254
331
return syscall (SYS_tid , 0 , 0 , 0 );
255
332
}
256
333
257
- int _twfi (void )
334
+ static int _twfi (void )
258
335
{
259
336
mo_task_wfi ();
260
-
261
337
return 0 ;
262
338
}
263
339
264
340
int sys_task_wfi (void )
265
341
{
266
- syscall (SYS_twfi , 0 , 0 , 0 );
267
-
268
- return 0 ;
342
+ return syscall (SYS_twfi , 0 , 0 , 0 );
269
343
}
270
344
271
- int _tcount (void )
345
+ static int _tcount (void )
272
346
{
273
347
return mo_task_count ();
274
348
}
@@ -278,7 +352,7 @@ int sys_task_count(void)
278
352
return syscall (SYS_tcount , 0 , 0 , 0 );
279
353
}
280
354
281
- int _ticks (void )
355
+ static int _ticks (void )
282
356
{
283
357
return mo_ticks ();
284
358
}
@@ -288,7 +362,7 @@ int sys_ticks(void)
288
362
return syscall (SYS_ticks , 0 , 0 , 0 );
289
363
}
290
364
291
- int _uptime (void )
365
+ static int _uptime (void )
292
366
{
293
367
return mo_uptime ();
294
368
}
0 commit comments