1 /* Check if realpath does not consume extra stack space based on symlink
2 existance in the path (BZ #26341)
3 Copyright (C) 2021-2022 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
22 #include <sys/param.h>
25 #define __sysconf sysconf
26 #include <eloop-threshold.h>
27 #include <support/check.h>
28 #include <support/support.h>
29 #include <support/temp_file.h>
30 #include <support/xunistd.h>
31 #include <support/xthread.h>
33 static char *filename
;
34 static size_t filenamelen
;
35 static char *linkname
;
38 # define PATH_MAX 1024
44 int fd
= create_temp_file ("tst-canon-bz26341", &filename
);
45 TEST_VERIFY_EXIT (fd
!= -1);
48 /* Make filename a canonical path. */
49 char *saved_filename
= filename
;
50 filename
= realpath (filename
, NULL
);
51 free (saved_filename
);
52 TEST_VERIFY (filename
!= NULL
);
54 /* Create MAXLINKS symbolic links to the temporary filename.
55 On exit, linkname has the last link created. */
56 char *prevlink
= filename
;
57 int maxlinks
= __eloop_threshold ();
58 for (int i
= 0; i
< maxlinks
; i
++)
60 linkname
= xasprintf ("%s%d", filename
, i
);
61 xsymlink (prevlink
, linkname
);
62 add_temp_file (linkname
);
66 filenamelen
= strlen (filename
);
70 do_realpath (void *arg
)
72 /* Old implementation of realpath allocates a PATH_MAX using alloca
73 for each symlink in the path, leading to MAXSYMLINKS times PATH_MAX
75 This stack allocations tries fill the thread allocated stack minus
76 the resolved path (plus some slack), the realpath (plus some
77 slack), and the system call usage (plus some slack).
78 If realpath uses more than 2 * PATH_MAX plus some slack it will trigger
81 const size_t syscall_usage
= 1 * PATH_MAX
+ 1024;
82 const size_t realpath_usage
= 2 * PATH_MAX
+ 1024;
83 const size_t thread_usage
= 1 * PATH_MAX
+ 1024;
84 size_t stack_size
= support_small_thread_stack_size ()
85 - syscall_usage
- realpath_usage
- thread_usage
;
86 char stack
[stack_size
];
87 char *resolved
= stack
+ stack_size
- thread_usage
+ 1024;
89 char *p
= realpath (linkname
, resolved
);
90 TEST_VERIFY (p
!= NULL
);
91 TEST_COMPARE_BLOB (resolved
, filenamelen
, filename
, filenamelen
);
101 pthread_t th
= xpthread_create (support_small_stack_thread_attribute (),
108 #include <support/test-driver.c>