git » libfiu » master » tree

[master] / preload / posix / modules / posix.custom.c

  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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
 * Custom-made wrappers for some special POSIX functions.
 */

#include "codegen.h"
#include "hash.h"

#include <inttypes.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>


/* Wrapper for open(), we can't generate it because it has a variable number
 * of arguments */
mkwrap_init(int, open, (const char *pathname, int flags, ...),
		(const char *, int, ...))

int open(const char *pathname, int flags, ...)
{
	int r;
	int fstatus;

	/* Differences from the generated code begin here */

	mode_t mode;
	va_list l;

	if (flags & O_CREAT) {
		va_start(l, flags);

		/* va_arg() can only take fully promoted types, and mode_t
		 * sometimes is smaller than an int, so we should always pass
		 * int to it, and not mode_t. Not doing so would may result in
		 * a compile-time warning and run-time error. We asume that it
		 * is never bigger than an int, which holds in practice. */
		mode = va_arg(l, int);

		va_end(l);
	} else {
		/* set it to 0, it's ignored anyway */
		mode = 0;
	}

	/* Differences from the generated code end here */

/* Use the normal macros to complete the function, now that we have
 * set mode to something */
mkwrap_body_called(open, (pathname, flags, mode), -1)

	static const int valid_errnos[] = {
	  #ifdef EACCESS
		EACCES,
	  #endif
	  #ifdef EFAULT
		EFAULT,
	  #endif
	  #ifdef EFBIG
		EFBIG,
	  #endif
	  #ifdef EOVERFLOW
		EOVERFLOW,
	  #endif
	  #ifdef ELOOP
		ELOOP,
	  #endif
	  #ifdef EMFILE
		EMFILE,
	  #endif
	  #ifdef ENAMETOOLONG
		ENAMETOOLONG,
	  #endif
	  #ifdef ENFILE
		ENFILE,
	  #endif
	  #ifdef ENOENT
		ENOENT,
	  #endif
	  #ifdef ENOMEM
		ENOMEM,
	  #endif
	  #ifdef ENOSPC
		ENOSPC,
	  #endif
	  #ifdef ENOTDIR
		ENOTDIR,
	  #endif
	  #ifdef EROFS
		EROFS
	  #endif
	};
mkwrap_body_errno("posix/io/oc/open", -1)
mkwrap_bottom(open, (pathname, flags, mode))


/* The 64-bit variant for glibc.
 * The code is identical to open(), just using open64() where appropriate. */
#ifdef __GLIBC__

mkwrap_init(int, open64, (const char *pathname, int flags, ...),
		(const char *, int, ...))

int open64(const char *pathname, int flags, ...)
{
	int r;
	int fstatus;

	/* Differences from the generated code begin here */

	mode_t mode;
	va_list l;

	if (flags & O_CREAT) {
		va_start(l, flags);

		/* va_arg() can only take fully promoted types, and mode_t
		 * sometimes is smaller than an int, so we should always pass
		 * int to it, and not mode_t. Not doing so would may result in
		 * a compile-time warning and run-time error. We asume that it
		 * is never bigger than an int, which holds in practise. */
		mode = va_arg(l, int);

		va_end(l);
	} else {
		/* set it to 0, it's ignored anyway */
		mode = 0;
	}

	/* Differences from the generated code end here */

/* Use the normal macros to complete the function, now that we have
 * set mode to something */
mkwrap_body_called(open, (pathname, flags, mode), -1)

	static const int valid_errnos[] = {
	  #ifdef EACCESS
		EACCES,
	  #endif
	  #ifdef EFAULT
		EFAULT,
	  #endif
	  #ifdef EFBIG
		EFBIG,
	  #endif
	  #ifdef EOVERFLOW
		EOVERFLOW,
	  #endif
	  #ifdef ELOOP
		ELOOP,
	  #endif
	  #ifdef EMFILE
		EMFILE,
	  #endif
	  #ifdef ENAMETOOLONG
		ENAMETOOLONG,
	  #endif
	  #ifdef ENFILE
		ENFILE,
	  #endif
	  #ifdef ENOENT
		ENOENT,
	  #endif
	  #ifdef ENOMEM
		ENOMEM,
	  #endif
	  #ifdef ENOSPC
		ENOSPC,
	  #endif
	  #ifdef ENOTDIR
		ENOTDIR,
	  #endif
	  #ifdef EROFS
		EROFS
	  #endif
	};
mkwrap_body_errno("posix/io/oc/open", -1)
mkwrap_bottom(open64, (pathname, flags, mode))

#endif

/*
 * To simulate ferror() properly, we need to keep track of which FILE * have
 * we returned a failure for.
 *
 * To do that, we keep a set of FILE *. This set is implemented using a hash
 * table, for convenience.
 *
 * An alternative would be to have a static array, but then lookups would
 * scale linearly with the number of files we're expected to fail, and (most
 * importantly) require a new data type just for this.
 */
static hash_t *ferror_hash_table;
static pthread_mutex_t ferror_hash_table_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_once_t ferror_hash_table_is_initialized = PTHREAD_ONCE_INIT;

static void _fiu_init_ferror_hash_table(void)
{
	rec_inc();
	pthread_mutex_lock(&ferror_hash_table_mutex);

	// Create the hash table. No need for a value destructor, as our values
	// are not pointers but static values.
	ferror_hash_table = hash_create(NULL);

	pthread_mutex_unlock(&ferror_hash_table_mutex);
	rec_dec();
}

/* Our hash table uses character keys, to convert stream to keys we just get
 * the hexadecimal representation via PRIxPTR (%p is implementation
 * dependent). */
#define STREAM_KEY_SIZE (sizeof(uintptr_t) * 2 + 1)

static void stream_to_key(void *stream, char key[STREAM_KEY_SIZE])
{
	snprintf(key, STREAM_KEY_SIZE, "%" PRIxPTR, (uintptr_t) stream);
}

void set_ferror(void * stream)
{
	char key[STREAM_KEY_SIZE];
	stream_to_key(stream, key);

	pthread_once(&ferror_hash_table_is_initialized,
			_fiu_init_ferror_hash_table);

	pthread_mutex_lock(&ferror_hash_table_mutex);

	// Use a dummy the value; we don't care about it, we only need to be able to
	// distinguish it from NULL.
	hash_set(ferror_hash_table, key, (void *) 0xDEAD);

	pthread_mutex_unlock(&ferror_hash_table_mutex);
}

static int get_ferror(void * stream)
{
	char key[STREAM_KEY_SIZE];
	stream_to_key(stream, key);

	pthread_once(&ferror_hash_table_is_initialized,
			_fiu_init_ferror_hash_table);

	pthread_mutex_lock(&ferror_hash_table_mutex);

	void *value = hash_get(ferror_hash_table, key);

	pthread_mutex_unlock(&ferror_hash_table_mutex);

	return value != NULL;
}

static void clear_ferror(void * stream)
{
	char key[STREAM_KEY_SIZE];
	stream_to_key(stream, key);

	pthread_once(&ferror_hash_table_is_initialized,
			_fiu_init_ferror_hash_table);

	pthread_mutex_lock(&ferror_hash_table_mutex);

	hash_del(ferror_hash_table, key);

	pthread_mutex_unlock(&ferror_hash_table_mutex);
}


/* Wrapper for ferror() */
mkwrap_top(int, ferror, (FILE *stream), (stream), (FILE *), 1)
mkwrap_body_hardcoded("posix/stdio/error/ferror", 1)

	/* The following is like mkwrap_bottom(), but has an additional check to
	 * return 1 if we previously returned an error for this file, so the
	 * semantics are consistent. */
	if (_fiu_orig_ferror == NULL)
		_fiu_init_ferror();

	printd("calling orig\n");
	r = (*_fiu_orig_ferror) (stream);

	if (r == 0 && get_ferror(stream)) {
		printd("ferror fixed\n");
		r = 1;
	}

exit:
	rec_dec();
	return r;
}


/* Custom wrapper for clearerr().
 * This function does not fail as such, but we override it so we can provide a
 * consistent view. */
mkwrap_init(void, clearerr, (FILE *stream), (FILE *))

void clearerr (FILE *stream)
{
	rec_inc();

	if (_fiu_orig_clearerr == NULL)
			_fiu_init_clearerr();

	printd("calling orig\n");
	(*_fiu_orig_clearerr) (stream);

	printd("fixing internal state\n");
	clear_ferror(stream);

	rec_dec();
}


/* Custom wrapper for fclose() that clears ferror_hash_table. */
mkwrap_top(int , fclose, (FILE *stream), (stream), (FILE *), (EOF))
	static const int valid_errnos[] = {
	  #ifdef EAGAIN
		EAGAIN,
	  #endif
	  #ifdef EBADF
		EBADF,
	  #endif
	  #ifdef EFBIG
		EFBIG,
	  #endif
	  #ifdef EFBIG
		EFBIG,
	  #endif
	  #ifdef EINTR
		EINTR,
	  #endif
	  #ifdef EIO
		EIO,
	  #endif
	  #ifdef ENOMEM
		ENOMEM,
	  #endif
	  #ifdef ENOSPC
		ENOSPC,
	  #endif
	  #ifdef EPIPE
		EPIPE,
	  #endif
	  #ifdef ENXIO
		ENXIO,
	  #endif
	};
mkwrap_body_errno("posix/stdio/oc/fclose", EOF)

/* We need a custom injection of clear_ferror here to prevent stale entries in
 * ferror_hash_table. */
	clear_ferror(stream);

mkwrap_bottom(fclose, (stream))


/*
 * Variadic functions
 *
 * For some variadic functions, like printf(), we use a non-variadic
 * counterpart (vprintf()) to implement the wrapping.
 *
 */

/* Generate the body for a variadic function, which relies on a non-variadic
 * function for expansion.
 *
 * We expect mkwrap_init to be called on the non-variadic one, see the calls
 * below for examples.
 */
#define mkwrap_variadic_def_and_body_called( \
		RTYPE, /* return type */ \
		NAME, /* variadic name */ \
		NVNAME, /* non-variadic name */ \
		PARAMS, /* parameter definition, with types */ \
		PARAMSN, /* parameter names only, with "arguments" for va part */ \
		LASTPN, /* last parameter name */ \
		ON_ERR /* what to return on error */ \
		) \
								\
	RTYPE NAME PARAMS					\
	{ 							\
		RTYPE r;					\
		int fstatus;					\
		va_list arguments;				\
								\
		if (_fiu_called) {				\
			if (_fiu_orig_##NVNAME == NULL) {	\
				if (_fiu_in_init_##NVNAME) {	\
					printd("fail on init\n"); \
					return ON_ERR;		\
				} else {			\
					printd("get orig\n");	\
					_fiu_init_##NVNAME();	\
				}				\
			}					\
			printd("orig\n");			\
			va_start(arguments, LASTPN);		\
			r = (*_fiu_orig_##NVNAME) PARAMSN;	\
			va_end(arguments);			\
								\
			return r;				\
		}						\
								\
		printd("fiu\n");				\
								\
		/* fiu_fail() may call anything */		\
		rec_inc();


#define mkwrap_variadic_bottom( \
		NAME /* variadic name */, \
		NVNAME /* non-variadic name */, \
		PARAMSN /* parameter names only, with "arguments" for va part */, \
		LASTPN /* last parameter name */ \
		) \
								\
		if (_fiu_orig_##NVNAME == NULL)			\
			_fiu_init_##NVNAME();			\
								\
		printd("calling orig\n");			\
		va_start(arguments, LASTPN);			\
		r = (*_fiu_orig_##NVNAME) PARAMSN;		\
		va_end(arguments);				\
								\
	exit:							\
		rec_dec();					\
		return r;					\
	}


/* fprintf() -> vfprintf() */
mkwrap_init(int, vfprintf,
	(FILE *restrict stream, const char *restrict format, va_list ap),
	(FILE *restrict, const char *restrict, va_list))

mkwrap_variadic_def_and_body_called(int, fprintf, vfprintf,
	(FILE *restrict stream, const char *restrict format, ...),
	(stream, format, arguments),
	format, -1)

	static const int valid_errnos[] = {
	  #ifdef EAGAIN
		EAGAIN,
	  #endif
	  #ifdef EBADF
		EBADF,
	  #endif
	  #ifdef EFBIG
		EFBIG,
	  #endif
	  #ifdef EINTR
		EINTR,
	  #endif
	  #ifdef EIO
		EIO,
	  #endif
	  #ifdef ENOMEM
		ENOMEM,
	  #endif
	  #ifdef ENOSPC
		ENOSPC,
	  #endif
	  #ifdef ENXIO
		ENXIO,
	  #endif
	  #ifdef EPIPE
		EPIPE,
	  #endif
	  #ifdef EILSEQ
		EILSEQ,
	  #endif
	  #ifdef EOVERFLOW
		EOVERFLOW,
	  #endif
	};

mkwrap_body_errno_ferror("posix/stdio/sp/fprintf", -1, stream)
mkwrap_variadic_bottom(fprintf, vfprintf, (stream, format, arguments), format)


/* printf() -> vprintf() */
mkwrap_init(int, vprintf,
	(const char *restrict format, va_list ap),
	(const char *restrict, va_list))

mkwrap_variadic_def_and_body_called(int, printf, vprintf,
	(const char *restrict format, ...),
	(format, arguments),
	format, -1)

	static const int valid_errnos[] = {
	  #ifdef EAGAIN
		EAGAIN,
	  #endif
	  #ifdef EBADF
		EBADF,
	  #endif
	  #ifdef EFBIG
		EFBIG,
	  #endif
	  #ifdef EINTR
		EINTR,
	  #endif
	  #ifdef EIO
		EIO,
	  #endif
	  #ifdef ENOMEM
		ENOMEM,
	  #endif
	  #ifdef ENOSPC
		ENOSPC,
	  #endif
	  #ifdef ENXIO
		ENXIO,
	  #endif
	  #ifdef EPIPE
		EPIPE,
	  #endif
	  #ifdef EILSEQ
		EILSEQ,
	  #endif
	  #ifdef EOVERFLOW
		EOVERFLOW,
	  #endif
	};

mkwrap_body_errno_ferror("posix/stdio/sp/printf", -1, stdout)
mkwrap_variadic_bottom(printf, vprintf, (format, arguments), format)


/* dprintf() -> vdprintf() */
mkwrap_init(int, vdprintf,
	(int fildes, const char *restrict format, va_list ap),
	(int, const char *restrict, va_list))

mkwrap_variadic_def_and_body_called(int, dprintf, vdprintf,
	(int fildes, const char *restrict format, ...),
	(fildes, format, arguments),
	format, -1)

	static const int valid_errnos[] = {
	  #ifdef EAGAIN
		EAGAIN,
	  #endif
	  #ifdef EBADF
		EBADF,
	  #endif
	  #ifdef EFBIG
		EFBIG,
	  #endif
	  #ifdef EINTR
		EINTR,
	  #endif
	  #ifdef EIO
		EIO,
	  #endif
	  #ifdef ENOMEM
		ENOMEM,
	  #endif
	  #ifdef ENOSPC
		ENOSPC,
	  #endif
	  #ifdef ENXIO
		ENXIO,
	  #endif
	  #ifdef EPIPE
		EPIPE,
	  #endif
	  #ifdef EILSEQ
		EILSEQ,
	  #endif
	  #ifdef EOVERFLOW
		EOVERFLOW,
	  #endif
	};

mkwrap_body_errno("posix/stdio/sp/dprintf", -1)
mkwrap_variadic_bottom(dprintf, vdprintf, (fildes, format, arguments), format)


/* fscanf() -> vfscanf() */
mkwrap_init(int, vfscanf,
	(FILE *restrict stream, const char *restrict format, va_list ap),
	(FILE *restrict, const char *restrict, va_list))

mkwrap_variadic_def_and_body_called(int, fscanf, vfscanf,
	(FILE *restrict stream, const char *restrict format, ...),
	(stream, format, arguments),
	format, EOF)

	static const int valid_errnos[] = {
	  #ifdef EAGAIN
		EAGAIN,
	  #endif
	  #ifdef EBADF
		EBADF,
	  #endif
	  #ifdef EINTR
		EINTR,
	  #endif
	  #ifdef EIO
		EIO,
	  #endif
	  #ifdef ENOMEM
		ENOMEM,
	  #endif
	  #ifdef ENXIO
		ENXIO,
	  #endif
	  #ifdef EOVERFLOW
		EOVERFLOW,
	  #endif
	  #ifdef EILSEQ
		EILSEQ,
	  #endif
	  #ifdef EINVAL
		EINVAL,
	  #endif
	};

mkwrap_body_errno_ferror("posix/stdio/sp/fscanf", EOF, stream)
mkwrap_variadic_bottom(fscanf, vfscanf, (stream, format, arguments), format)


/* scanf() -> vscanf() */
mkwrap_init(int, vscanf,
	(const char *restrict format, va_list ap),
	(const char *restrict, va_list))

mkwrap_variadic_def_and_body_called(int, scanf, vscanf,
	(const char *restrict format, ...),
	(format, arguments),
	format, EOF)

	static const int valid_errnos[] = {
	  #ifdef EAGAIN
		EAGAIN,
	  #endif
	  #ifdef EBADF
		EBADF,
	  #endif
	  #ifdef EINTR
		EINTR,
	  #endif
	  #ifdef EIO
		EIO,
	  #endif
	  #ifdef ENOMEM
		ENOMEM,
	  #endif
	  #ifdef ENXIO
		ENXIO,
	  #endif
	  #ifdef EOVERFLOW
		EOVERFLOW,
	  #endif
	  #ifdef EILSEQ
		EILSEQ,
	  #endif
	};

mkwrap_body_errno_ferror("posix/stdio/sp/scanf", EOF, stdin)
mkwrap_variadic_bottom(scanf, vscanf, (format, arguments), format)