about summary refs log tree commit diff
path: root/libio/tst-vtables-common.c
blob: bedd0305638fd0fb8ba736bdfc35f4db48286375 (plain) (blame)
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
/* Test for libio vtables and their validation.  Common code.
   Copyright (C) 2018-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* This test provides some coverage for how various stdio functions
   use the vtables in FILE * objects.  The focus is mostly on which
   functions call which methods, not so much on validating data
   processing.  An initial series of tests check that custom vtables
   do not work without activation through _IO_init.

   Note: libio vtables are deprecated feature.  Do not use this test
   as a documentation source for writing custom vtables.  See
   fopencookie for a different way of creating custom stdio
   streams.  */

#include <stdbool.h>
#include <string.h>
#include <support/capture_subprocess.h>
#include <support/check.h>
#include <support/namespace.h>
#include <support/support.h>
#include <support/test-driver.h>
#include <support/xunistd.h>

#include "libioP.h"

/* Data shared between the test subprocess and the test driver in the
   parent.  Note that *shared is reset at the start of the check_call
   function.  */
struct shared
{
  /* Expected file pointer for method calls.  */
  FILE *fp;

  /* If true, assume that a call to _IO_init is needed to enable
     custom vtables.  */
  bool initially_disabled;

  /* Requested return value for the methods which have one.  */
  int return_value;

  /* A value (usually a character) recorded by some of the methods
     below.  */
  int value;

  /* Likewise, for some data.  */
  char buffer[16];
  size_t buffer_length;

  /* Total number of method calls.  */
  unsigned int calls;

  /* Individual method call counts.  */
  unsigned int calls_finish;
  unsigned int calls_overflow;
  unsigned int calls_underflow;
  unsigned int calls_uflow;
  unsigned int calls_pbackfail;
  unsigned int calls_xsputn;
  unsigned int calls_xsgetn;
  unsigned int calls_seekoff;
  unsigned int calls_seekpos;
  unsigned int calls_setbuf;
  unsigned int calls_sync;
  unsigned int calls_doallocate;
  unsigned int calls_read;
  unsigned int calls_write;
  unsigned int calls_seek;
  unsigned int calls_close;
  unsigned int calls_stat;
  unsigned int calls_showmanyc;
  unsigned int calls_imbue;
} *shared;

/* Method implementations which increment the counters in *shared.  */

static void
log_method (FILE *fp, const char *name)
{
  if (test_verbose > 0)
    printf ("info: %s (%p) called\n", name, fp);
}

static void
method_finish (FILE *fp, int dummy)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_finish;
}

static int
method_overflow (FILE *fp, int ch)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_overflow;
  shared->value = ch;
  return shared->return_value;
}

static int
method_underflow (FILE *fp)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_underflow;
  return shared->return_value;
}

static int
method_uflow (FILE *fp)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_uflow;
  return shared->return_value;
}

static int
method_pbackfail (FILE *fp, int ch)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_pbackfail;
  shared->value = ch;
  return shared->return_value;
}

static size_t
method_xsputn (FILE *fp, const void *data, size_t n)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_xsputn;

  size_t to_copy = n;
  if (n > sizeof (shared->buffer))
    to_copy = sizeof (shared->buffer);
  memcpy (shared->buffer, data, to_copy);
  shared->buffer_length = to_copy;
  return to_copy;
}

static size_t
method_xsgetn (FILE *fp, void *data, size_t n)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_xsgetn;
  return 0;
}

static off64_t
method_seekoff (FILE *fp, off64_t offset, int dir, int mode)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_seekoff;
  return shared->return_value;
}

static off64_t
method_seekpos (FILE *fp, off64_t offset, int mode)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_seekpos;
  return shared->return_value;
}

static FILE *
method_setbuf (FILE *fp, char *buffer, ssize_t length)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_setbuf;
  return fp;
}

static int
method_sync (FILE *fp)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_sync;
  return shared->return_value;
}

static int
method_doallocate (FILE *fp)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_doallocate;
  return shared->return_value;
}

static ssize_t
method_read (FILE *fp, void *data, ssize_t length)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_read;
  return shared->return_value;
}

static ssize_t
method_write (FILE *fp, const void *data, ssize_t length)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_write;
  return shared->return_value;
}

static off64_t
method_seek (FILE *fp, off64_t offset, int mode)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_seek;
  return shared->return_value;
}

static int
method_close (FILE *fp)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_close;
  return shared->return_value;
}

static int
method_stat (FILE *fp, void *buffer)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_stat;
  return shared->return_value;
}

static int
method_showmanyc (FILE *fp)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_showmanyc;
  return shared->return_value;
}

static void
method_imbue (FILE *fp, void *locale)
{
  log_method (fp, __func__);
  TEST_VERIFY (fp == shared->fp);
  ++shared->calls;
  ++shared->calls_imbue;
}

/* Our custom vtable.  */

static const struct _IO_jump_t jumps =
{
  JUMP_INIT_DUMMY,
  JUMP_INIT (finish, method_finish),
  JUMP_INIT (overflow, method_overflow),
  JUMP_INIT (underflow, method_underflow),
  JUMP_INIT (uflow, method_uflow),
  JUMP_INIT (pbackfail, method_pbackfail),
  JUMP_INIT (xsputn, method_xsputn),
  JUMP_INIT (xsgetn, method_xsgetn),
  JUMP_INIT (seekoff, method_seekoff),
  JUMP_INIT (seekpos, method_seekpos),
  JUMP_INIT (setbuf, method_setbuf),
  JUMP_INIT (sync, method_sync),
  JUMP_INIT (doallocate, method_doallocate),
  JUMP_INIT (read, method_read),
  JUMP_INIT (write, method_write),
  JUMP_INIT (seek, method_seek),
  JUMP_INIT (close, method_close),
  JUMP_INIT (stat, method_stat),
  JUMP_INIT (showmanyc, method_showmanyc),
  JUMP_INIT (imbue, method_imbue)
};

/* Our file implementation.  */

struct my_file
{
  FILE f;
  const struct _IO_jump_t *vtable;
};

struct my_file
my_file_create (void)
{
  return (struct my_file)
    {
      /* Disable locking, so that we do not have to set up a lock
         pointer.  */
      .f._flags =  _IO_USER_LOCK,

      /* Copy the offset from the an initialized handle, instead of
         figuring it out from scratch.  */
      .f._vtable_offset = stdin->_vtable_offset,

      .vtable = &jumps,
    };
}

/* Initial tests which do not enable vtable compatibility.  */

/* Inhibit GCC optimization of fprintf.  */
typedef int (*fprintf_type) (FILE *, const char *, ...);
static const volatile fprintf_type fprintf_ptr = &fprintf;

static void
without_compatibility_fprintf (void *closure)
{
  /* This call should abort.  */
  fprintf_ptr (shared->fp, " ");
  _exit (1);
}

static void
without_compatibility_fputc (void *closure)
{
  /* This call should abort.  */
  fputc (' ', shared->fp);
  _exit (1);
}

static void
without_compatibility_fgetc (void *closure)
{
  /* This call should abort.  */
  fgetc (shared->fp);
  _exit (1);
}

static void
without_compatibility_fflush (void *closure)
{
  /* This call should abort.  */
  fflush (shared->fp);
  _exit (1);
}

static void
check_for_termination (const char *name, void (*callback) (void *))
{
  struct my_file file = my_file_create ();
  shared->fp = &file.f;
  shared->return_value = -1;
  shared->calls = 0;
  struct support_capture_subprocess proc
    = support_capture_subprocess (callback, NULL);
  support_capture_subprocess_check (&proc, name, -SIGABRT,
                                    sc_allow_stderr);
  const char *message
    = "Fatal error: glibc detected an invalid stdio handle\n";
  TEST_COMPARE_BLOB (proc.err.buffer, proc.err.length,
                     message, strlen (message));
  TEST_COMPARE (shared->calls, 0);
  support_capture_subprocess_free (&proc);
}

/* The test with vtable validation disabled.  */

/* This function does not have a prototype in libioP.h to prevent
   accidental use from within the library (which would disable vtable
   verification).  */
void _IO_init (FILE *fp, int flags);

static void
with_compatibility_fprintf (void *closure)
{
  TEST_COMPARE (fprintf_ptr (shared->fp, "A%sCD", "B"), 4);
  TEST_COMPARE (shared->calls, 3);
  TEST_COMPARE (shared->calls_xsputn, 3);
  TEST_COMPARE_BLOB (shared->buffer, shared->buffer_length,
                     "CD", 2);
}

static void
with_compatibility_fputc (void *closure)
{
  shared->return_value = '@';
  TEST_COMPARE (fputc ('@', shared->fp), '@');
  TEST_COMPARE (shared->calls, 1);
  TEST_COMPARE (shared->calls_overflow, 1);
  TEST_COMPARE (shared->value, '@');
}

static void
with_compatibility_fgetc (void *closure)
{
  shared->return_value = 'X';
  TEST_COMPARE (fgetc (shared->fp), 'X');
  TEST_COMPARE (shared->calls, 1);
  TEST_COMPARE (shared->calls_uflow, 1);
}

static void
with_compatibility_fflush (void *closure)
{
  TEST_COMPARE (fflush (shared->fp), 0);
  TEST_COMPARE (shared->calls, 1);
  TEST_COMPARE (shared->calls_sync, 1);
}

/* Call CALLBACK in a subprocess, after setting up a custom file
   object and updating shared->fp.  */
static void
check_call (const char *name, void (*callback) (void *),
            bool initially_disabled)
{
  *shared = (struct shared)
    {
     .initially_disabled = initially_disabled,
    };

  /* Set up a custom file object.  */
  struct my_file file = my_file_create ();
  shared->fp = &file.f;
  if (shared->initially_disabled)
    _IO_init (shared->fp, file.f._flags);

  if (test_verbose > 0)
    printf ("info: calling test %s\n", name);
  support_isolate_in_subprocess (callback, NULL);
}

/* Run the tests.  INITIALLY_DISABLED indicates whether custom vtables
   are disabled when the test starts.  */
static int
run_tests (bool initially_disabled)
{
  /* The test relies on fatal error messages being printed to standard
     error.  */
  setenv ("LIBC_FATAL_STDERR_", "1", 1);

  shared = support_shared_allocate (sizeof (*shared));
  shared->initially_disabled = initially_disabled;

  if (initially_disabled)
    {
      check_for_termination ("fprintf", without_compatibility_fprintf);
      check_for_termination ("fputc", without_compatibility_fputc);
      check_for_termination ("fgetc", without_compatibility_fgetc);
      check_for_termination ("fflush", without_compatibility_fflush);
    }

  check_call ("fprintf", with_compatibility_fprintf, initially_disabled);
  check_call ("fputc", with_compatibility_fputc, initially_disabled);
  check_call ("fgetc", with_compatibility_fgetc, initially_disabled);
  check_call ("fflush", with_compatibility_fflush, initially_disabled);

  support_shared_free (shared);
  shared = NULL;

  return 0;
}