1 /* Test driver for malloc interposition tests.
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <http://www.gnu.org/licenses/>. */
28 static int do_test (void);
29 #define TEST_FUNCTION do_test ()
30 #include "../test-skeleton.c"
32 /* Fills BUFFER with a test string. */
34 line_string (int number
, char *buffer
, size_t length
)
36 for (size_t i
= 0; i
< length
- 2; ++i
)
37 buffer
[i
] = 'A' + ((number
+ i
) % 26);
38 buffer
[length
- 2] = '\n';
39 buffer
[length
- 1] = '\0';
42 /* Perform the tests. */
44 run_tests (void *closure
)
47 int fd
= create_temp_file ("tst-malloc-interpose", &temp_file_path
);
51 /* Line lengths excluding the line terminator. */
52 static const int line_lengths
[] = { 0, 45, 80, 2, 8201, 0, 17, -1 };
54 /* Fill the test file with data. */
56 FILE *fp
= fdopen (fd
, "w");
57 for (int lineno
= 0; line_lengths
[lineno
] >= 0; ++lineno
)
59 char buffer
[line_lengths
[lineno
] + 2];
60 line_string (lineno
, buffer
, sizeof (buffer
));
61 fprintf (fp
, "%s", buffer
);
66 printf ("error: fprintf: %m\n");
71 printf ("error: fclose: %m\n");
76 /* Read the test file. This tests libc-internal allocation with
79 FILE *fp
= fopen (temp_file_path
, "r");
82 size_t actual_size
= 0;
83 for (int lineno
= 0; ; ++lineno
)
86 ssize_t result
= getline (&actual
, &actual_size
, fp
);
89 printf ("error: invalid return value 0 from getline\n");
92 if (result
< 0 && errno
!= 0)
94 printf ("error: getline: %m\n");
97 if (result
< 0 && line_lengths
[lineno
] >= 0)
99 printf ("error: unexpected end of file after line %d\n", lineno
);
102 if (result
> 0 && line_lengths
[lineno
] < 0)
104 printf ("error: no end of file after line %d\n", lineno
);
107 if (result
== -1 && line_lengths
[lineno
] == -1)
108 /* End of file reached as expected. */
111 if (result
!= line_lengths
[lineno
] + 1)
113 printf ("error: line length mismatch: expected %d, got %zd\n",
114 line_lengths
[lineno
], result
);
118 char expected
[line_lengths
[lineno
] + 2];
119 line_string (lineno
, expected
, sizeof (expected
));
120 if (strcmp (actual
, expected
) != 0)
122 printf ("error: line mismatch\n");
123 printf ("error: expected: [[%s]]\n", expected
);
124 printf ("error: actual: [[%s]]\n", actual
);
129 if (fclose (fp
) != 0)
131 printf ("error: fclose (after reading): %m\n");
136 free (temp_file_path
);
138 /* Make sure that fork is working. */
142 printf ("error: fork: %m\n");
145 enum { exit_code
= 55 };
149 int ret
= waitpid (pid
, &status
, 0);
152 printf ("error: waitpid: %m\n");
155 if (!WIFEXITED (status
) || WEXITSTATUS (status
) != exit_code
)
157 printf ("error: unexpected exit status from child process: %d\n",
165 /* This is used to detect if malloc has not been successfully
166 interposed. The interposed malloc does not use brk/sbrk. */
167 static void *initial_brk
;
168 __attribute__ ((constructor
))
170 set_initial_brk (void)
172 initial_brk
= sbrk (0);
175 /* Terminate the process if the break value has been changed. */
176 __attribute__ ((destructor
))
180 void *current
= sbrk (0);
181 if (current
!= initial_brk
)
183 printf ("error: brk changed from %p to %p; no interposition?\n",
184 initial_brk
, current
);
194 #if INTERPOSE_THREADS
195 pthread_t thr
= xpthread_create (NULL
, run_tests
, NULL
);