tst-vtables-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* Test for libio vtables and their validation. Common code.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This test provides some coverage for how various stdio functions
  16. use the vtables in FILE * objects. The focus is mostly on which
  17. functions call which methods, not so much on validating data
  18. processing. An initial series of tests check that custom vtables
  19. do not work without activation through _IO_init.
  20. Note: libio vtables are deprecated feature. Do not use this test
  21. as a documentation source for writing custom vtables. See
  22. fopencookie for a different way of creating custom stdio
  23. streams. */
  24. #include <stdbool.h>
  25. #include <string.h>
  26. #include <support/capture_subprocess.h>
  27. #include <support/check.h>
  28. #include <support/namespace.h>
  29. #include <support/support.h>
  30. #include <support/test-driver.h>
  31. #include <support/xunistd.h>
  32. #include "libioP.h"
  33. /* Data shared between the test subprocess and the test driver in the
  34. parent. Note that *shared is reset at the start of the check_call
  35. function. */
  36. struct shared
  37. {
  38. /* Expected file pointer for method calls. */
  39. FILE *fp;
  40. /* If true, assume that a call to _IO_init is needed to enable
  41. custom vtables. */
  42. bool initially_disabled;
  43. /* Requested return value for the methods which have one. */
  44. int return_value;
  45. /* A value (usually a character) recorded by some of the methods
  46. below. */
  47. int value;
  48. /* Likewise, for some data. */
  49. char buffer[16];
  50. size_t buffer_length;
  51. /* Total number of method calls. */
  52. unsigned int calls;
  53. /* Individual method call counts. */
  54. unsigned int calls_finish;
  55. unsigned int calls_overflow;
  56. unsigned int calls_underflow;
  57. unsigned int calls_uflow;
  58. unsigned int calls_pbackfail;
  59. unsigned int calls_xsputn;
  60. unsigned int calls_xsgetn;
  61. unsigned int calls_seekoff;
  62. unsigned int calls_seekpos;
  63. unsigned int calls_setbuf;
  64. unsigned int calls_sync;
  65. unsigned int calls_doallocate;
  66. unsigned int calls_read;
  67. unsigned int calls_write;
  68. unsigned int calls_seek;
  69. unsigned int calls_close;
  70. unsigned int calls_stat;
  71. unsigned int calls_showmanyc;
  72. unsigned int calls_imbue;
  73. } *shared;
  74. /* Method implementations which increment the counters in *shared. */
  75. static void
  76. log_method (FILE *fp, const char *name)
  77. {
  78. if (test_verbose > 0)
  79. printf ("info: %s (%p) called\n", name, fp);
  80. }
  81. static void
  82. method_finish (FILE *fp, int dummy)
  83. {
  84. log_method (fp, __func__);
  85. TEST_VERIFY (fp == shared->fp);
  86. ++shared->calls;
  87. ++shared->calls_finish;
  88. }
  89. static int
  90. method_overflow (FILE *fp, int ch)
  91. {
  92. log_method (fp, __func__);
  93. TEST_VERIFY (fp == shared->fp);
  94. ++shared->calls;
  95. ++shared->calls_overflow;
  96. shared->value = ch;
  97. return shared->return_value;
  98. }
  99. static int
  100. method_underflow (FILE *fp)
  101. {
  102. log_method (fp, __func__);
  103. TEST_VERIFY (fp == shared->fp);
  104. ++shared->calls;
  105. ++shared->calls_underflow;
  106. return shared->return_value;
  107. }
  108. static int
  109. method_uflow (FILE *fp)
  110. {
  111. log_method (fp, __func__);
  112. TEST_VERIFY (fp == shared->fp);
  113. ++shared->calls;
  114. ++shared->calls_uflow;
  115. return shared->return_value;
  116. }
  117. static int
  118. method_pbackfail (FILE *fp, int ch)
  119. {
  120. log_method (fp, __func__);
  121. TEST_VERIFY (fp == shared->fp);
  122. ++shared->calls;
  123. ++shared->calls_pbackfail;
  124. shared->value = ch;
  125. return shared->return_value;
  126. }
  127. static size_t
  128. method_xsputn (FILE *fp, const void *data, size_t n)
  129. {
  130. log_method (fp, __func__);
  131. TEST_VERIFY (fp == shared->fp);
  132. ++shared->calls;
  133. ++shared->calls_xsputn;
  134. size_t to_copy = n;
  135. if (n > sizeof (shared->buffer))
  136. to_copy = sizeof (shared->buffer);
  137. memcpy (shared->buffer, data, to_copy);
  138. shared->buffer_length = to_copy;
  139. return to_copy;
  140. }
  141. static size_t
  142. method_xsgetn (FILE *fp, void *data, size_t n)
  143. {
  144. log_method (fp, __func__);
  145. TEST_VERIFY (fp == shared->fp);
  146. ++shared->calls;
  147. ++shared->calls_xsgetn;
  148. return 0;
  149. }
  150. static off64_t
  151. method_seekoff (FILE *fp, off64_t offset, int dir, int mode)
  152. {
  153. log_method (fp, __func__);
  154. TEST_VERIFY (fp == shared->fp);
  155. ++shared->calls;
  156. ++shared->calls_seekoff;
  157. return shared->return_value;
  158. }
  159. static off64_t
  160. method_seekpos (FILE *fp, off64_t offset, int mode)
  161. {
  162. log_method (fp, __func__);
  163. TEST_VERIFY (fp == shared->fp);
  164. ++shared->calls;
  165. ++shared->calls_seekpos;
  166. return shared->return_value;
  167. }
  168. static FILE *
  169. method_setbuf (FILE *fp, char *buffer, ssize_t length)
  170. {
  171. log_method (fp, __func__);
  172. TEST_VERIFY (fp == shared->fp);
  173. ++shared->calls;
  174. ++shared->calls_setbuf;
  175. return fp;
  176. }
  177. static int
  178. method_sync (FILE *fp)
  179. {
  180. log_method (fp, __func__);
  181. TEST_VERIFY (fp == shared->fp);
  182. ++shared->calls;
  183. ++shared->calls_sync;
  184. return shared->return_value;
  185. }
  186. static int
  187. method_doallocate (FILE *fp)
  188. {
  189. log_method (fp, __func__);
  190. TEST_VERIFY (fp == shared->fp);
  191. ++shared->calls;
  192. ++shared->calls_doallocate;
  193. return shared->return_value;
  194. }
  195. static ssize_t
  196. method_read (FILE *fp, void *data, ssize_t length)
  197. {
  198. log_method (fp, __func__);
  199. TEST_VERIFY (fp == shared->fp);
  200. ++shared->calls;
  201. ++shared->calls_read;
  202. return shared->return_value;
  203. }
  204. static ssize_t
  205. method_write (FILE *fp, const void *data, ssize_t length)
  206. {
  207. log_method (fp, __func__);
  208. TEST_VERIFY (fp == shared->fp);
  209. ++shared->calls;
  210. ++shared->calls_write;
  211. return shared->return_value;
  212. }
  213. static off64_t
  214. method_seek (FILE *fp, off64_t offset, int mode)
  215. {
  216. log_method (fp, __func__);
  217. TEST_VERIFY (fp == shared->fp);
  218. ++shared->calls;
  219. ++shared->calls_seek;
  220. return shared->return_value;
  221. }
  222. static int
  223. method_close (FILE *fp)
  224. {
  225. log_method (fp, __func__);
  226. TEST_VERIFY (fp == shared->fp);
  227. ++shared->calls;
  228. ++shared->calls_close;
  229. return shared->return_value;
  230. }
  231. static int
  232. method_stat (FILE *fp, void *buffer)
  233. {
  234. log_method (fp, __func__);
  235. TEST_VERIFY (fp == shared->fp);
  236. ++shared->calls;
  237. ++shared->calls_stat;
  238. return shared->return_value;
  239. }
  240. static int
  241. method_showmanyc (FILE *fp)
  242. {
  243. log_method (fp, __func__);
  244. TEST_VERIFY (fp == shared->fp);
  245. ++shared->calls;
  246. ++shared->calls_showmanyc;
  247. return shared->return_value;
  248. }
  249. static void
  250. method_imbue (FILE *fp, void *locale)
  251. {
  252. log_method (fp, __func__);
  253. TEST_VERIFY (fp == shared->fp);
  254. ++shared->calls;
  255. ++shared->calls_imbue;
  256. }
  257. /* Our custom vtable. */
  258. static const struct _IO_jump_t jumps =
  259. {
  260. JUMP_INIT_DUMMY,
  261. JUMP_INIT (finish, method_finish),
  262. JUMP_INIT (overflow, method_overflow),
  263. JUMP_INIT (underflow, method_underflow),
  264. JUMP_INIT (uflow, method_uflow),
  265. JUMP_INIT (pbackfail, method_pbackfail),
  266. JUMP_INIT (xsputn, method_xsputn),
  267. JUMP_INIT (xsgetn, method_xsgetn),
  268. JUMP_INIT (seekoff, method_seekoff),
  269. JUMP_INIT (seekpos, method_seekpos),
  270. JUMP_INIT (setbuf, method_setbuf),
  271. JUMP_INIT (sync, method_sync),
  272. JUMP_INIT (doallocate, method_doallocate),
  273. JUMP_INIT (read, method_read),
  274. JUMP_INIT (write, method_write),
  275. JUMP_INIT (seek, method_seek),
  276. JUMP_INIT (close, method_close),
  277. JUMP_INIT (stat, method_stat),
  278. JUMP_INIT (showmanyc, method_showmanyc),
  279. JUMP_INIT (imbue, method_imbue)
  280. };
  281. /* Our file implementation. */
  282. struct my_file
  283. {
  284. FILE f;
  285. const struct _IO_jump_t *vtable;
  286. };
  287. struct my_file
  288. my_file_create (void)
  289. {
  290. return (struct my_file)
  291. {
  292. /* Disable locking, so that we do not have to set up a lock
  293. pointer. */
  294. .f._flags = _IO_USER_LOCK,
  295. /* Copy the offset from the an initialized handle, instead of
  296. figuring it out from scratch. */
  297. .f._vtable_offset = stdin->_vtable_offset,
  298. .vtable = &jumps,
  299. };
  300. }
  301. /* Initial tests which do not enable vtable compatibility. */
  302. /* Inhibit GCC optimization of fprintf. */
  303. typedef int (*fprintf_type) (FILE *, const char *, ...);
  304. static const volatile fprintf_type fprintf_ptr = &fprintf;
  305. static void
  306. without_compatibility_fprintf (void *closure)
  307. {
  308. /* This call should abort. */
  309. fprintf_ptr (shared->fp, " ");
  310. _exit (1);
  311. }
  312. static void
  313. without_compatibility_fputc (void *closure)
  314. {
  315. /* This call should abort. */
  316. fputc (' ', shared->fp);
  317. _exit (1);
  318. }
  319. static void
  320. without_compatibility_fgetc (void *closure)
  321. {
  322. /* This call should abort. */
  323. fgetc (shared->fp);
  324. _exit (1);
  325. }
  326. static void
  327. without_compatibility_fflush (void *closure)
  328. {
  329. /* This call should abort. */
  330. fflush (shared->fp);
  331. _exit (1);
  332. }
  333. static void
  334. check_for_termination (const char *name, void (*callback) (void *))
  335. {
  336. struct my_file file = my_file_create ();
  337. shared->fp = &file.f;
  338. shared->return_value = -1;
  339. shared->calls = 0;
  340. struct support_capture_subprocess proc
  341. = support_capture_subprocess (callback, NULL);
  342. support_capture_subprocess_check (&proc, name, -SIGABRT,
  343. sc_allow_stderr);
  344. const char *message
  345. = "Fatal error: glibc detected an invalid stdio handle\n";
  346. TEST_COMPARE_BLOB (proc.err.buffer, proc.err.length,
  347. message, strlen (message));
  348. TEST_COMPARE (shared->calls, 0);
  349. support_capture_subprocess_free (&proc);
  350. }
  351. /* The test with vtable validation disabled. */
  352. /* This function does not have a prototype in libioP.h to prevent
  353. accidental use from within the library (which would disable vtable
  354. verification). */
  355. void _IO_init (FILE *fp, int flags);
  356. static void
  357. with_compatibility_fprintf (void *closure)
  358. {
  359. TEST_COMPARE (fprintf_ptr (shared->fp, "A%sCD", "B"), 4);
  360. TEST_COMPARE (shared->calls, 3);
  361. TEST_COMPARE (shared->calls_xsputn, 3);
  362. TEST_COMPARE_BLOB (shared->buffer, shared->buffer_length,
  363. "CD", 2);
  364. }
  365. static void
  366. with_compatibility_fputc (void *closure)
  367. {
  368. shared->return_value = '@';
  369. TEST_COMPARE (fputc ('@', shared->fp), '@');
  370. TEST_COMPARE (shared->calls, 1);
  371. TEST_COMPARE (shared->calls_overflow, 1);
  372. TEST_COMPARE (shared->value, '@');
  373. }
  374. static void
  375. with_compatibility_fgetc (void *closure)
  376. {
  377. shared->return_value = 'X';
  378. TEST_COMPARE (fgetc (shared->fp), 'X');
  379. TEST_COMPARE (shared->calls, 1);
  380. TEST_COMPARE (shared->calls_uflow, 1);
  381. }
  382. static void
  383. with_compatibility_fflush (void *closure)
  384. {
  385. TEST_COMPARE (fflush (shared->fp), 0);
  386. TEST_COMPARE (shared->calls, 1);
  387. TEST_COMPARE (shared->calls_sync, 1);
  388. }
  389. /* Call CALLBACK in a subprocess, after setting up a custom file
  390. object and updating shared->fp. */
  391. static void
  392. check_call (const char *name, void (*callback) (void *),
  393. bool initially_disabled)
  394. {
  395. *shared = (struct shared)
  396. {
  397. .initially_disabled = initially_disabled,
  398. };
  399. /* Set up a custom file object. */
  400. struct my_file file = my_file_create ();
  401. shared->fp = &file.f;
  402. if (shared->initially_disabled)
  403. _IO_init (shared->fp, file.f._flags);
  404. if (test_verbose > 0)
  405. printf ("info: calling test %s\n", name);
  406. support_isolate_in_subprocess (callback, NULL);
  407. }
  408. /* Run the tests. INITIALLY_DISABLED indicates whether custom vtables
  409. are disabled when the test starts. */
  410. static int
  411. run_tests (bool initially_disabled)
  412. {
  413. /* The test relies on fatal error messages being printed to standard
  414. error. */
  415. setenv ("LIBC_FATAL_STDERR_", "1", 1);
  416. shared = support_shared_allocate (sizeof (*shared));
  417. shared->initially_disabled = initially_disabled;
  418. if (initially_disabled)
  419. {
  420. check_for_termination ("fprintf", without_compatibility_fprintf);
  421. check_for_termination ("fputc", without_compatibility_fputc);
  422. check_for_termination ("fgetc", without_compatibility_fgetc);
  423. check_for_termination ("fflush", without_compatibility_fflush);
  424. }
  425. check_call ("fprintf", with_compatibility_fprintf, initially_disabled);
  426. check_call ("fputc", with_compatibility_fputc, initially_disabled);
  427. check_call ("fgetc", with_compatibility_fgetc, initially_disabled);
  428. check_call ("fflush", with_compatibility_fflush, initially_disabled);
  429. support_shared_free (shared);
  430. shared = NULL;
  431. return 0;
  432. }