1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include "BionicDeathTest.h"
20 #include "ScopedSignalHandler.h"
21 #include "TemporaryFile.h"
22 #include "utils.h"
23 
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <libgen.h>
27 #include <limits.h>
28 #include <stdint.h>
29 #include <sys/capability.h>
30 #include <sys/param.h>
31 #include <sys/syscall.h>
32 #include <sys/types.h>
33 #include <sys/utsname.h>
34 #include <sys/wait.h>
35 #include <unistd.h>
36 
37 #include <android-base/file.h>
38 #include <android-base/strings.h>
39 
40 #include "private/get_cpu_count_from_string.h"
41 
42 #if defined(NOFORTIFY)
43 #define UNISTD_TEST unistd_nofortify
44 #define UNISTD_DEATHTEST unistd_nofortify_DeathTest
45 #else
46 #define UNISTD_TEST unistd
47 #define UNISTD_DEATHTEST unistd_DeathTest
48 #endif
49 
get_brk()50 static void* get_brk() {
51   return sbrk(0);
52 }
53 
page_align(uintptr_t addr)54 static void* page_align(uintptr_t addr) {
55   uintptr_t mask = sysconf(_SC_PAGE_SIZE) - 1;
56   return reinterpret_cast<void*>((addr + mask) & ~mask);
57 }
58 
TEST(UNISTD_TEST,brk)59 TEST(UNISTD_TEST, brk) {
60   void* initial_break = get_brk();
61 
62   void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 1);
63   int ret = brk(new_break);
64   if (ret == -1) {
65     ASSERT_EQ(errno, ENOMEM);
66   } else {
67     ASSERT_EQ(0, ret);
68     ASSERT_GE(get_brk(), new_break);
69   }
70 
71   // Expand by a full page to force the mapping to expand
72   new_break = page_align(reinterpret_cast<uintptr_t>(initial_break) + sysconf(_SC_PAGE_SIZE));
73   ret = brk(new_break);
74   if (ret == -1) {
75     ASSERT_EQ(errno, ENOMEM);
76   } else {
77     ASSERT_EQ(0, ret);
78     ASSERT_EQ(get_brk(), new_break);
79   }
80 }
81 
TEST(UNISTD_TEST,brk_ENOMEM)82 TEST(UNISTD_TEST, brk_ENOMEM) {
83   ASSERT_EQ(-1, brk(reinterpret_cast<void*>(-1)));
84   ASSERT_EQ(ENOMEM, errno);
85 }
86 
87 #if defined(__GLIBC__)
88 #define SBRK_MIN INTPTR_MIN
89 #define SBRK_MAX INTPTR_MAX
90 #else
91 #define SBRK_MIN PTRDIFF_MIN
92 #define SBRK_MAX PTRDIFF_MAX
93 #endif
94 
TEST(UNISTD_TEST,sbrk_ENOMEM)95 TEST(UNISTD_TEST, sbrk_ENOMEM) {
96 #if defined(__BIONIC__) && !defined(__LP64__)
97   // There is no way to guarantee that all overflow conditions can be tested
98   // without manipulating the underlying values of the current break.
99   extern void* __bionic_brk;
100 
101   class ScopedBrk {
102   public:
103     ScopedBrk() : saved_brk_(__bionic_brk) {}
104     virtual ~ScopedBrk() { __bionic_brk = saved_brk_; }
105 
106   private:
107     void* saved_brk_;
108   };
109 
110   ScopedBrk scope_brk;
111 
112   // Set the current break to a point that will cause an overflow.
113   __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) + 2);
114 
115   // Can't increase by so much that we'd overflow.
116   ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MAX));
117   ASSERT_EQ(ENOMEM, errno);
118 
119   // Set the current break to a point that will cause an overflow.
120   __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX));
121 
122   ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN));
123   ASSERT_EQ(ENOMEM, errno);
124 
125   __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) - 1);
126 
127   ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN + 1));
128   ASSERT_EQ(ENOMEM, errno);
129 #else
130   class ScopedBrk {
131   public:
132     ScopedBrk() : saved_brk_(get_brk()) {}
133     virtual ~ScopedBrk() { brk(saved_brk_); }
134 
135   private:
136     void* saved_brk_;
137   };
138 
139   ScopedBrk scope_brk;
140 
141   uintptr_t cur_brk = reinterpret_cast<uintptr_t>(get_brk());
142   if (cur_brk < static_cast<uintptr_t>(-(SBRK_MIN+1))) {
143     // Do the overflow test for a max negative increment.
144     ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MIN));
145 #if defined(__BIONIC__)
146     // GLIBC does not set errno in overflow case.
147     ASSERT_EQ(ENOMEM, errno);
148 #endif
149   }
150 
151   uintptr_t overflow_brk = static_cast<uintptr_t>(SBRK_MAX) + 2;
152   if (cur_brk < overflow_brk) {
153     // Try and move the value to PTRDIFF_MAX + 2.
154     cur_brk = reinterpret_cast<uintptr_t>(sbrk(overflow_brk));
155   }
156   if (cur_brk >= overflow_brk) {
157     ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MAX));
158 #if defined(__BIONIC__)
159     // GLIBC does not set errno in overflow case.
160     ASSERT_EQ(ENOMEM, errno);
161 #endif
162   }
163 #endif
164 }
165 
TEST(UNISTD_TEST,truncate)166 TEST(UNISTD_TEST, truncate) {
167   TemporaryFile tf;
168   ASSERT_EQ(0, close(tf.fd));
169   ASSERT_EQ(0, truncate(tf.filename, 123));
170 
171   struct stat sb;
172   ASSERT_EQ(0, stat(tf.filename, &sb));
173   ASSERT_EQ(123, sb.st_size);
174 }
175 
TEST(UNISTD_TEST,truncate64)176 TEST(UNISTD_TEST, truncate64) {
177   TemporaryFile tf;
178   ASSERT_EQ(0, close(tf.fd));
179   ASSERT_EQ(0, truncate64(tf.filename, 123));
180 
181   struct stat sb;
182   ASSERT_EQ(0, stat(tf.filename, &sb));
183   ASSERT_EQ(123, sb.st_size);
184 }
185 
TEST(UNISTD_TEST,ftruncate)186 TEST(UNISTD_TEST, ftruncate) {
187   TemporaryFile tf;
188   ASSERT_EQ(0, ftruncate(tf.fd, 123));
189   ASSERT_EQ(0, close(tf.fd));
190 
191   struct stat sb;
192   ASSERT_EQ(0, stat(tf.filename, &sb));
193   ASSERT_EQ(123, sb.st_size);
194 }
195 
TEST(UNISTD_TEST,ftruncate64)196 TEST(UNISTD_TEST, ftruncate64) {
197   TemporaryFile tf;
198   ASSERT_EQ(0, ftruncate64(tf.fd, 123));
199   ASSERT_EQ(0, close(tf.fd));
200 
201   struct stat sb;
202   ASSERT_EQ(0, stat(tf.filename, &sb));
203   ASSERT_EQ(123, sb.st_size);
204 }
205 
TEST(UNISTD_TEST,ftruncate_negative)206 TEST(UNISTD_TEST, ftruncate_negative) {
207   TemporaryFile tf;
208   errno = 0;
209   ASSERT_EQ(-1, ftruncate(tf.fd, -123));
210   ASSERT_EQ(EINVAL, errno);
211 }
212 
213 static bool g_pause_test_flag = false;
PauseTestSignalHandler(int)214 static void PauseTestSignalHandler(int) {
215   g_pause_test_flag = true;
216 }
217 
TEST(UNISTD_TEST,pause)218 TEST(UNISTD_TEST, pause) {
219   ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
220 
221   alarm(1);
222   ASSERT_FALSE(g_pause_test_flag);
223   ASSERT_EQ(-1, pause());
224   ASSERT_TRUE(g_pause_test_flag);
225 }
226 
TEST(UNISTD_TEST,read)227 TEST(UNISTD_TEST, read) {
228   int fd = open("/proc/version", O_RDONLY);
229   ASSERT_TRUE(fd != -1);
230 
231   char buf[5];
232   ASSERT_EQ(5, read(fd, buf, 5));
233   ASSERT_EQ(buf[0], 'L');
234   ASSERT_EQ(buf[1], 'i');
235   ASSERT_EQ(buf[2], 'n');
236   ASSERT_EQ(buf[3], 'u');
237   ASSERT_EQ(buf[4], 'x');
238   close(fd);
239 }
240 
TEST(UNISTD_TEST,read_EBADF)241 TEST(UNISTD_TEST, read_EBADF) {
242   // read returns ssize_t which is 64-bits on LP64, so it's worth explicitly checking that
243   // our syscall stubs correctly return a 64-bit -1.
244   char buf[1];
245   ASSERT_EQ(-1, read(-1, buf, sizeof(buf)));
246   ASSERT_EQ(EBADF, errno);
247 }
248 
TEST(UNISTD_TEST,syscall_long)249 TEST(UNISTD_TEST, syscall_long) {
250   // Check that syscall(3) correctly returns long results.
251   // https://code.google.com/p/android/issues/detail?id=73952
252   // We assume that the break is > 4GiB, but this is potentially flaky.
253   uintptr_t p = reinterpret_cast<uintptr_t>(sbrk(0));
254   ASSERT_EQ(p, static_cast<uintptr_t>(syscall(__NR_brk, 0)));
255 }
256 
TEST(UNISTD_TEST,alarm)257 TEST(UNISTD_TEST, alarm) {
258   ASSERT_EQ(0U, alarm(0));
259 }
260 
TEST(UNISTD_TEST,_exit)261 TEST(UNISTD_TEST, _exit) {
262   pid_t pid = fork();
263   ASSERT_NE(-1, pid) << strerror(errno);
264 
265   if (pid == 0) {
266     _exit(99);
267   }
268 
269   AssertChildExited(pid, 99);
270 }
271 
TEST(UNISTD_TEST,getenv_unsetenv)272 TEST(UNISTD_TEST, getenv_unsetenv) {
273   ASSERT_EQ(0, setenv("test-variable", "hello", 1));
274   ASSERT_STREQ("hello", getenv("test-variable"));
275   ASSERT_EQ(0, unsetenv("test-variable"));
276   ASSERT_TRUE(getenv("test-variable") == NULL);
277 }
278 
TEST(UNISTD_TEST,unsetenv_EINVAL)279 TEST(UNISTD_TEST, unsetenv_EINVAL) {
280   EXPECT_EQ(-1, unsetenv(""));
281   EXPECT_EQ(EINVAL, errno);
282   EXPECT_EQ(-1, unsetenv("a=b"));
283   EXPECT_EQ(EINVAL, errno);
284 }
285 
TEST(UNISTD_TEST,setenv_EINVAL)286 TEST(UNISTD_TEST, setenv_EINVAL) {
287   EXPECT_EQ(-1, setenv(NULL, "value", 0));
288   EXPECT_EQ(EINVAL, errno);
289   EXPECT_EQ(-1, setenv(NULL, "value", 1));
290   EXPECT_EQ(EINVAL, errno);
291   EXPECT_EQ(-1, setenv("", "value", 0));
292   EXPECT_EQ(EINVAL, errno);
293   EXPECT_EQ(-1, setenv("", "value", 1));
294   EXPECT_EQ(EINVAL, errno);
295   EXPECT_EQ(-1, setenv("a=b", "value", 0));
296   EXPECT_EQ(EINVAL, errno);
297   EXPECT_EQ(-1, setenv("a=b", "value", 1));
298   EXPECT_EQ(EINVAL, errno);
299 }
300 
TEST(UNISTD_TEST,setenv)301 TEST(UNISTD_TEST, setenv) {
302   ASSERT_EQ(0, unsetenv("test-variable"));
303 
304   char a[] = "a";
305   char b[] = "b";
306   char c[] = "c";
307 
308   // New value.
309   EXPECT_EQ(0, setenv("test-variable", a, 0));
310   EXPECT_STREQ(a, getenv("test-variable"));
311 
312   // Existing value, no overwrite.
313   EXPECT_EQ(0, setenv("test-variable", b, 0));
314   EXPECT_STREQ(a, getenv("test-variable"));
315 
316   // Existing value, overwrite.
317   EXPECT_EQ(0, setenv("test-variable", c, 1));
318   EXPECT_STREQ(c, getenv("test-variable"));
319   // But the arrays backing the values are unchanged.
320   EXPECT_EQ('a', a[0]);
321   EXPECT_EQ('b', b[0]);
322   EXPECT_EQ('c', c[0]);
323 
324   ASSERT_EQ(0, unsetenv("test-variable"));
325 }
326 
TEST(UNISTD_TEST,putenv)327 TEST(UNISTD_TEST, putenv) {
328   ASSERT_EQ(0, unsetenv("a"));
329 
330   char* s1 = strdup("a=b");
331   ASSERT_EQ(0, putenv(s1));
332 
333   ASSERT_STREQ("b", getenv("a"));
334   s1[2] = 'c';
335   ASSERT_STREQ("c", getenv("a"));
336 
337   char* s2 = strdup("a=b");
338   ASSERT_EQ(0, putenv(s2));
339 
340   ASSERT_STREQ("b", getenv("a"));
341   ASSERT_EQ('c', s1[2]);
342 
343   ASSERT_EQ(0, unsetenv("a"));
344   free(s1);
345   free(s2);
346 }
347 
TEST(UNISTD_TEST,clearenv)348 TEST(UNISTD_TEST, clearenv) {
349   extern char** environ;
350 
351   // Guarantee that environ is not initially empty...
352   ASSERT_EQ(0, setenv("test-variable", "a", 1));
353 
354   // Stash a copy.
355   std::vector<char*> old_environ;
356   for (size_t i = 0; environ[i] != NULL; ++i) {
357     old_environ.push_back(strdup(environ[i]));
358   }
359 
360   ASSERT_EQ(0, clearenv());
361 
362   EXPECT_TRUE(environ == NULL || environ[0] == NULL);
363   EXPECT_EQ(NULL, getenv("test-variable"));
364   EXPECT_EQ(0, setenv("test-variable", "post-clear", 1));
365   EXPECT_STREQ("post-clear", getenv("test-variable"));
366 
367   // Put the old environment back.
368   for (size_t i = 0; i < old_environ.size(); ++i) {
369     EXPECT_EQ(0, putenv(old_environ[i]));
370   }
371 
372   // Check it wasn't overwritten.
373   EXPECT_STREQ("a", getenv("test-variable"));
374 
375   EXPECT_EQ(0, unsetenv("test-variable"));
376 }
377 
TestFsyncFunction(int (* fn)(int))378 static void TestFsyncFunction(int (*fn)(int)) {
379   int fd;
380 
381   // Can't sync an invalid fd.
382   errno = 0;
383   EXPECT_EQ(-1, fn(-1));
384   EXPECT_EQ(EBADF, errno);
385 
386   // It doesn't matter whether you've opened a file for write or not.
387   TemporaryFile tf;
388   ASSERT_NE(-1, tf.fd);
389 
390   EXPECT_EQ(0, fn(tf.fd));
391 
392   ASSERT_NE(-1, fd = open(tf.filename, O_RDONLY));
393   EXPECT_EQ(0, fn(fd));
394   close(fd);
395 
396   ASSERT_NE(-1, fd = open(tf.filename, O_RDWR));
397   EXPECT_EQ(0, fn(fd));
398   close(fd);
399 
400   // The fd can even be a directory.
401   ASSERT_NE(-1, fd = open("/data/local/tmp", O_RDONLY));
402   EXPECT_EQ(0, fn(fd));
403   close(fd);
404 
405   // But some file systems may choose to be fussy...
406   errno = 0;
407   ASSERT_NE(-1, fd = open("/proc/version", O_RDONLY));
408   EXPECT_EQ(-1, fn(fd));
409   EXPECT_EQ(EINVAL, errno);
410   close(fd);
411 }
412 
TEST(UNISTD_TEST,fdatasync)413 TEST(UNISTD_TEST, fdatasync) {
414   TestFsyncFunction(fdatasync);
415 }
416 
TEST(UNISTD_TEST,fsync)417 TEST(UNISTD_TEST, fsync) {
418   TestFsyncFunction(fsync);
419 }
420 
AssertGetPidCorrect()421 static void AssertGetPidCorrect() {
422   // The loop is just to make manual testing/debugging with strace easier.
423   pid_t getpid_syscall_result = syscall(__NR_getpid);
424   for (size_t i = 0; i < 128; ++i) {
425     ASSERT_EQ(getpid_syscall_result, getpid());
426   }
427 }
428 
TestGetPidCachingWithFork(int (* fork_fn)(),void (* exit_fn)(int))429 static void TestGetPidCachingWithFork(int (*fork_fn)(), void (*exit_fn)(int)) {
430   pid_t parent_pid = getpid();
431   ASSERT_EQ(syscall(__NR_getpid), parent_pid);
432 
433   pid_t fork_result = fork_fn();
434   ASSERT_NE(fork_result, -1);
435   if (fork_result == 0) {
436     // We're the child.
437     ASSERT_NO_FATAL_FAILURE(AssertGetPidCorrect());
438     ASSERT_EQ(parent_pid, getppid());
439     exit_fn(123);
440   } else {
441     // We're the parent.
442     ASSERT_EQ(parent_pid, getpid());
443     AssertChildExited(fork_result, 123);
444   }
445 }
446 
447 // gettid() is marked as __attribute_const__, which will have the compiler
448 // optimize out multiple calls to gettid in the same function. This wrapper
449 // defeats that optimization.
GetTidForTest()450 static __attribute__((__noinline__)) pid_t GetTidForTest() {
451   __asm__("");
452   return gettid();
453 }
454 
AssertGetTidCorrect()455 static void AssertGetTidCorrect() {
456   // The loop is just to make manual testing/debugging with strace easier.
457   pid_t gettid_syscall_result = syscall(__NR_gettid);
458   for (size_t i = 0; i < 128; ++i) {
459     ASSERT_EQ(gettid_syscall_result, GetTidForTest());
460   }
461 }
462 
TestGetTidCachingWithFork(int (* fork_fn)(),void (* exit_fn)(int))463 static void TestGetTidCachingWithFork(int (*fork_fn)(), void (*exit_fn)(int)) {
464   pid_t parent_tid = GetTidForTest();
465   ASSERT_EQ(syscall(__NR_gettid), parent_tid);
466 
467   pid_t fork_result = fork_fn();
468   ASSERT_NE(fork_result, -1);
469   if (fork_result == 0) {
470     // We're the child.
471     EXPECT_EQ(syscall(__NR_getpid), syscall(__NR_gettid));
472     EXPECT_EQ(getpid(), GetTidForTest()) << "real tid is " << syscall(__NR_gettid)
473                                          << ", pid is " << syscall(__NR_getpid);
474     ASSERT_NO_FATAL_FAILURE(AssertGetTidCorrect());
475     exit_fn(123);
476   } else {
477     // We're the parent.
478     ASSERT_EQ(parent_tid, GetTidForTest());
479     AssertChildExited(fork_result, 123);
480   }
481 }
482 
TEST(UNISTD_TEST,getpid_caching_and_fork)483 TEST(UNISTD_TEST, getpid_caching_and_fork) {
484   TestGetPidCachingWithFork(fork, exit);
485 }
486 
TEST(UNISTD_TEST,gettid_caching_and_fork)487 TEST(UNISTD_TEST, gettid_caching_and_fork) {
488   TestGetTidCachingWithFork(fork, exit);
489 }
490 
TEST(UNISTD_TEST,getpid_caching_and_vfork)491 TEST(UNISTD_TEST, getpid_caching_and_vfork) {
492   TestGetPidCachingWithFork(vfork, _exit);
493 }
494 
CloneLikeFork()495 static int CloneLikeFork() {
496   return clone(nullptr, nullptr, SIGCHLD, nullptr);
497 }
498 
TEST(UNISTD_TEST,getpid_caching_and_clone_process)499 TEST(UNISTD_TEST, getpid_caching_and_clone_process) {
500   TestGetPidCachingWithFork(CloneLikeFork, exit);
501 }
502 
TEST(UNISTD_TEST,gettid_caching_and_clone_process)503 TEST(UNISTD_TEST, gettid_caching_and_clone_process) {
504   TestGetTidCachingWithFork(CloneLikeFork, exit);
505 }
506 
CloneAndSetTid()507 static int CloneAndSetTid() {
508   pid_t child_tid = 0;
509   pid_t parent_tid = GetTidForTest();
510 
511   int rv = clone(nullptr, nullptr, CLONE_CHILD_SETTID | SIGCHLD, nullptr, nullptr, nullptr, &child_tid);
512   EXPECT_NE(-1, rv);
513 
514   if (rv == 0) {
515     // Child.
516     EXPECT_EQ(child_tid, GetTidForTest());
517     EXPECT_NE(child_tid, parent_tid);
518   } else {
519     EXPECT_NE(child_tid, GetTidForTest());
520     EXPECT_NE(child_tid, parent_tid);
521     EXPECT_EQ(GetTidForTest(), parent_tid);
522   }
523 
524   return rv;
525 }
526 
TEST(UNISTD_TEST,gettid_caching_and_clone_process_settid)527 TEST(UNISTD_TEST, gettid_caching_and_clone_process_settid) {
528   TestGetTidCachingWithFork(CloneAndSetTid, exit);
529 }
530 
CloneStartRoutine(int (* start_routine)(void *))531 static int CloneStartRoutine(int (*start_routine)(void*)) {
532   void* child_stack[1024];
533   return clone(start_routine, &child_stack[1024], SIGCHLD, NULL);
534 }
535 
GetPidCachingCloneStartRoutine(void *)536 static int GetPidCachingCloneStartRoutine(void*) {
537   AssertGetPidCorrect();
538   return 123;
539 }
540 
TEST(UNISTD_TEST,getpid_caching_and_clone)541 TEST(UNISTD_TEST, getpid_caching_and_clone) {
542   pid_t parent_pid = getpid();
543   ASSERT_EQ(syscall(__NR_getpid), parent_pid);
544 
545   int clone_result = CloneStartRoutine(GetPidCachingCloneStartRoutine);
546   ASSERT_NE(clone_result, -1);
547 
548   ASSERT_EQ(parent_pid, getpid());
549 
550   AssertChildExited(clone_result, 123);
551 }
552 
GetTidCachingCloneStartRoutine(void *)553 static int GetTidCachingCloneStartRoutine(void*) {
554   AssertGetTidCorrect();
555   return 123;
556 }
557 
TEST(UNISTD_TEST,gettid_caching_and_clone)558 TEST(UNISTD_TEST, gettid_caching_and_clone) {
559   pid_t parent_tid = GetTidForTest();
560   ASSERT_EQ(syscall(__NR_gettid), parent_tid);
561 
562   int clone_result = CloneStartRoutine(GetTidCachingCloneStartRoutine);
563   ASSERT_NE(clone_result, -1);
564 
565   ASSERT_EQ(parent_tid, GetTidForTest());
566 
567   AssertChildExited(clone_result, 123);
568 }
569 
CloneChildExit(void *)570 static int CloneChildExit(void*) {
571   AssertGetPidCorrect();
572   AssertGetTidCorrect();
573   exit(33);
574 }
575 
TEST(UNISTD_TEST,clone_fn_and_exit)576 TEST(UNISTD_TEST, clone_fn_and_exit) {
577   int clone_result = CloneStartRoutine(CloneChildExit);
578   ASSERT_NE(-1, clone_result);
579 
580   AssertGetPidCorrect();
581   AssertGetTidCorrect();
582 
583   AssertChildExited(clone_result, 33);
584 }
585 
GetPidCachingPthreadStartRoutine(void *)586 static void* GetPidCachingPthreadStartRoutine(void*) {
587   AssertGetPidCorrect();
588   return NULL;
589 }
590 
TEST(UNISTD_TEST,getpid_caching_and_pthread_create)591 TEST(UNISTD_TEST, getpid_caching_and_pthread_create) {
592   pid_t parent_pid = getpid();
593 
594   pthread_t t;
595   ASSERT_EQ(0, pthread_create(&t, NULL, GetPidCachingPthreadStartRoutine, NULL));
596 
597   ASSERT_EQ(parent_pid, getpid());
598 
599   void* result;
600   ASSERT_EQ(0, pthread_join(t, &result));
601   ASSERT_EQ(NULL, result);
602 }
603 
GetTidCachingPthreadStartRoutine(void *)604 static void* GetTidCachingPthreadStartRoutine(void*) {
605   AssertGetTidCorrect();
606   uint64_t tid = GetTidForTest();
607   return reinterpret_cast<void*>(tid);
608 }
609 
TEST(UNISTD_TEST,gettid_caching_and_pthread_create)610 TEST(UNISTD_TEST, gettid_caching_and_pthread_create) {
611   pid_t parent_tid = GetTidForTest();
612 
613   pthread_t t;
614   ASSERT_EQ(0, pthread_create(&t, NULL, GetTidCachingPthreadStartRoutine, &parent_tid));
615 
616   ASSERT_EQ(parent_tid, GetTidForTest());
617 
618   void* result;
619   ASSERT_EQ(0, pthread_join(t, &result));
620   ASSERT_NE(static_cast<uint64_t>(parent_tid), reinterpret_cast<uint64_t>(result));
621 }
622 
623 class UNISTD_DEATHTEST : public BionicDeathTest {};
624 
TEST_F(UNISTD_DEATHTEST,abort)625 TEST_F(UNISTD_DEATHTEST, abort) {
626   ASSERT_EXIT(abort(), testing::KilledBySignal(SIGABRT), "");
627 }
628 
TEST(UNISTD_TEST,sethostname)629 TEST(UNISTD_TEST, sethostname) {
630   // The permissions check happens before the argument check, so this will
631   // fail for a different reason if you're running as root than if you're
632   // not, but it'll fail either way. Checking that we have the symbol is about
633   // all we can do for sethostname(2).
634   ASSERT_EQ(-1, sethostname("", -1));
635 }
636 
TEST(UNISTD_TEST,gethostname)637 TEST(UNISTD_TEST, gethostname) {
638   char hostname[HOST_NAME_MAX + 1];
639   memset(hostname, 0, sizeof(hostname));
640 
641   // Can we get the hostname with a big buffer?
642   ASSERT_EQ(0, gethostname(hostname, HOST_NAME_MAX));
643 
644   // Can we get the hostname with a right-sized buffer?
645   errno = 0;
646   ASSERT_EQ(0, gethostname(hostname, strlen(hostname) + 1));
647 
648   // Does uname(2) agree?
649   utsname buf;
650   ASSERT_EQ(0, uname(&buf));
651   ASSERT_EQ(0, strncmp(hostname, buf.nodename, SYS_NMLN));
652   ASSERT_GT(strlen(hostname), 0U);
653 
654   // Do we correctly detect truncation?
655   errno = 0;
656   ASSERT_EQ(-1, gethostname(hostname, strlen(hostname)));
657   ASSERT_EQ(ENAMETOOLONG, errno);
658 }
659 
TEST(UNISTD_TEST,pathconf_fpathconf)660 TEST(UNISTD_TEST, pathconf_fpathconf) {
661   TemporaryFile tf;
662   long rc = 0L;
663   // As a file system's block size is always power of 2, the configure values
664   // for ALLOC and XFER should be power of 2 as well.
665   rc = pathconf(tf.filename, _PC_ALLOC_SIZE_MIN);
666   ASSERT_TRUE(rc > 0 && powerof2(rc));
667   rc = pathconf(tf.filename, _PC_REC_MIN_XFER_SIZE);
668   ASSERT_TRUE(rc > 0 && powerof2(rc));
669   rc = pathconf(tf.filename, _PC_REC_XFER_ALIGN);
670   ASSERT_TRUE(rc > 0 && powerof2(rc));
671 
672   rc = fpathconf(tf.fd, _PC_ALLOC_SIZE_MIN);
673   ASSERT_TRUE(rc > 0 && powerof2(rc));
674   rc = fpathconf(tf.fd, _PC_REC_MIN_XFER_SIZE);
675   ASSERT_TRUE(rc > 0 && powerof2(rc));
676   rc = fpathconf(tf.fd, _PC_REC_XFER_ALIGN);
677   ASSERT_TRUE(rc > 0 && powerof2(rc));
678 }
679 
680 
TEST(UNISTD_TEST,_POSIX_macros_smoke)681 TEST(UNISTD_TEST, _POSIX_macros_smoke) {
682   // Make a tight verification of _POSIX_* / _POSIX2_* / _XOPEN_* macros, to prevent change by mistake.
683   // Verify according to POSIX.1-2008.
684   EXPECT_EQ(200809L, _POSIX_VERSION);
685 
686   EXPECT_EQ(_POSIX_VERSION, _POSIX_ADVISORY_INFO);
687   EXPECT_GT(_POSIX_AIO_LISTIO_MAX, 0);
688   EXPECT_GT(_POSIX_AIO_MAX, 0);
689   EXPECT_GT(_POSIX_ARG_MAX, 0);
690   EXPECT_GT(_POSIX_BARRIERS, 0);
691   EXPECT_GT(_POSIX_SPIN_LOCKS, 0);
692   EXPECT_GT(_POSIX_CHILD_MAX, 0);
693   EXPECT_NE(_POSIX_CHOWN_RESTRICTED, -1);
694   EXPECT_EQ(_POSIX_VERSION, _POSIX_CLOCK_SELECTION);
695   EXPECT_EQ(0, _POSIX_CPUTIME);             // Use sysconf to detect support at runtime.
696   EXPECT_GT(_POSIX_DELAYTIMER_MAX, 0);
697   EXPECT_EQ(_POSIX_VERSION, _POSIX_FSYNC);
698   EXPECT_GT(_POSIX_HOST_NAME_MAX, 0);
699   EXPECT_EQ(_POSIX_VERSION, _POSIX_IPV6);
700   EXPECT_GT(_POSIX_JOB_CONTROL, 0);
701   EXPECT_GT(_POSIX_LINK_MAX, 0);
702   EXPECT_GT(_POSIX_LOGIN_NAME_MAX, 0);
703   EXPECT_EQ(_POSIX_VERSION, _POSIX_MAPPED_FILES);
704   EXPECT_GT(_POSIX_MAX_CANON, 0);
705   EXPECT_GT(_POSIX_MAX_INPUT, 0);
706   EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMLOCK);
707   EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMLOCK_RANGE);
708   EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMORY_PROTECTION);
709   EXPECT_EQ(0, _POSIX_MONOTONIC_CLOCK);
710   EXPECT_GT(_POSIX_MQ_OPEN_MAX, 0);
711   EXPECT_GT(_POSIX_MQ_PRIO_MAX, 0);
712   EXPECT_GT(_POSIX_NAME_MAX, 0);
713   EXPECT_GT(_POSIX_NGROUPS_MAX, 0);
714   EXPECT_GT(_POSIX_NO_TRUNC, 0);
715   EXPECT_GT(_POSIX_OPEN_MAX, 0);
716   EXPECT_GT(_POSIX_PATH_MAX, 0);
717   EXPECT_GT(_POSIX_PIPE_BUF, 0);
718   EXPECT_EQ(_POSIX_VERSION, _POSIX_PRIORITY_SCHEDULING);
719   EXPECT_EQ(_POSIX_VERSION, _POSIX_RAW_SOCKETS);
720   EXPECT_EQ(_POSIX_VERSION, _POSIX_READER_WRITER_LOCKS);
721   EXPECT_EQ(_POSIX_VERSION, _POSIX_REALTIME_SIGNALS);
722   EXPECT_GT(_POSIX_REGEXP, 0);
723   EXPECT_GT(_POSIX_RE_DUP_MAX, 0);
724   EXPECT_GT(_POSIX_SAVED_IDS, 0);
725   EXPECT_EQ(_POSIX_VERSION, _POSIX_SEMAPHORES);
726   EXPECT_GT(_POSIX_SEM_NSEMS_MAX, 0);
727   EXPECT_GT(_POSIX_SEM_VALUE_MAX, 0);
728   EXPECT_GT(_POSIX_SHELL, 0);
729   EXPECT_GT(_POSIX_SIGQUEUE_MAX, 0);
730   EXPECT_EQ(-1, _POSIX_SPORADIC_SERVER);
731   EXPECT_GT(_POSIX_SSIZE_MAX, 0);
732   EXPECT_GT(_POSIX_STREAM_MAX, 0);
733   EXPECT_GT(_POSIX_SYMLINK_MAX, 0);
734   EXPECT_GT(_POSIX_SYMLOOP_MAX, 0);
735   EXPECT_EQ(_POSIX_VERSION, _POSIX_SYNCHRONIZED_IO);
736   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREADS);
737   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_ATTR_STACKADDR);
738   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_ATTR_STACKSIZE);
739   EXPECT_EQ(0, _POSIX_THREAD_CPUTIME);       // Use sysconf to detect support at runtime.
740   EXPECT_GT(_POSIX_THREAD_DESTRUCTOR_ITERATIONS, 0);
741   EXPECT_EQ(_POSIX_THREAD_KEYS_MAX, 128);
742   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_PRIORITY_SCHEDULING);
743   EXPECT_EQ(-1, _POSIX_THREAD_PRIO_INHERIT);
744   EXPECT_EQ(-1, _POSIX_THREAD_PRIO_PROTECT);
745   EXPECT_EQ(-1, _POSIX_THREAD_ROBUST_PRIO_PROTECT);
746   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_SAFE_FUNCTIONS);
747   EXPECT_EQ(-1, _POSIX_THREAD_SPORADIC_SERVER);
748   EXPECT_GT(_POSIX_THREAD_THREADS_MAX, 0);
749   EXPECT_EQ(_POSIX_VERSION, _POSIX_TIMEOUTS);
750   EXPECT_EQ(_POSIX_VERSION, _POSIX_TIMERS);
751   EXPECT_GT(_POSIX_TIMER_MAX, 0);
752   EXPECT_EQ(-1, _POSIX_TRACE);
753   EXPECT_EQ(-1, _POSIX_TRACE_EVENT_FILTER);
754   EXPECT_EQ(-1, _POSIX_TRACE_INHERIT);
755   EXPECT_EQ(-1, _POSIX_TRACE_LOG);
756   EXPECT_GT(_POSIX_TTY_NAME_MAX, 0);
757   EXPECT_EQ(-1, _POSIX_TYPED_MEMORY_OBJECTS);
758   EXPECT_GT(_POSIX_TZNAME_MAX, 0);
759   EXPECT_NE(-1, _POSIX_VDISABLE);
760 
761   EXPECT_GT(_POSIX2_BC_BASE_MAX, 0);
762   EXPECT_GT(_POSIX2_BC_DIM_MAX, 0);
763   EXPECT_GT(_POSIX2_BC_SCALE_MAX, 0);
764   EXPECT_GT(_POSIX2_BC_STRING_MAX, 0);
765   EXPECT_GT(_POSIX2_CHARCLASS_NAME_MAX, 0);
766   EXPECT_GT(_POSIX2_COLL_WEIGHTS_MAX, 0);
767   EXPECT_EQ(_POSIX_VERSION, _POSIX2_C_BIND);
768   EXPECT_GT(_POSIX2_EXPR_NEST_MAX, 0);
769   EXPECT_GT(_POSIX2_LINE_MAX, 0);
770   EXPECT_GT(_POSIX2_RE_DUP_MAX, 0);
771 
772   EXPECT_EQ(700, _XOPEN_VERSION);
773   EXPECT_GT(_XOPEN_IOV_MAX, 0);
774   EXPECT_GT(_XOPEN_UNIX, 0);
775 
776 #if defined(__BIONIC__)
777   // These tests only pass on bionic, as bionic and glibc has different support on these macros.
778   // Macros like _POSIX_ASYNCHRONOUS_IO are not supported on bionic yet.
779   EXPECT_EQ(-1, _POSIX_ASYNCHRONOUS_IO);
780   EXPECT_EQ(-1, _POSIX_MESSAGE_PASSING);
781   EXPECT_EQ(-1, _POSIX_PRIORITIZED_IO);
782   EXPECT_EQ(-1, _POSIX_SHARED_MEMORY_OBJECTS);
783   EXPECT_EQ(-1, _POSIX_SPAWN);
784   EXPECT_EQ(-1, _POSIX_THREAD_PROCESS_SHARED);
785   EXPECT_EQ(-1, _POSIX_THREAD_ROBUST_PRIO_INHERIT);
786 
787   EXPECT_EQ(-1, _POSIX2_VERSION);
788   EXPECT_EQ(-1, _POSIX2_CHAR_TERM);
789   EXPECT_EQ(-1, _POSIX2_C_DEV);
790   EXPECT_EQ(-1, _POSIX2_LOCALEDEF);
791   EXPECT_EQ(-1, _POSIX2_SW_DEV);
792   EXPECT_EQ(-1, _POSIX2_UPE);
793 
794   EXPECT_EQ(-1, _XOPEN_ENH_I18N);
795   EXPECT_EQ(-1, _XOPEN_CRYPT);
796   EXPECT_EQ(-1, _XOPEN_LEGACY);
797   EXPECT_EQ(-1, _XOPEN_REALTIME);
798   EXPECT_EQ(-1, _XOPEN_REALTIME_THREADS);
799   EXPECT_EQ(-1, _XOPEN_SHM);
800 
801 #endif // defined(__BIONIC__)
802 }
803 
804 #define VERIFY_SYSCONF_UNSUPPORTED(name) VerifySysconf(name, #name, [](long v){return v == -1;})
805 
806 // sysconf() means unlimited when it returns -1 with errno unchanged.
807 #define VERIFY_SYSCONF_POSITIVE(name) \
808   VerifySysconf(name, #name, [](long v){return (v > 0 || v == -1);})
809 
810 #define VERIFY_SYSCONF_POSIX_VERSION(name) \
811   VerifySysconf(name, #name, [](long v){return v == _POSIX_VERSION;})
812 
VerifySysconf(int option,const char * option_name,bool (* verify)(long))813 static void VerifySysconf(int option, const char *option_name, bool (*verify)(long)) {
814   errno = 0;
815   long ret = sysconf(option);
816   EXPECT_TRUE(0 == errno && verify(ret)) << "name = " << option_name << ", ret = "
817       << ret <<", Error Message: " << strerror(errno);
818 }
819 
TEST(UNISTD_TEST,sysconf)820 TEST(UNISTD_TEST, sysconf) {
821   VERIFY_SYSCONF_POSIX_VERSION(_SC_ADVISORY_INFO);
822   VERIFY_SYSCONF_POSITIVE(_SC_ARG_MAX);
823   VERIFY_SYSCONF_POSIX_VERSION(_SC_BARRIERS);
824   VERIFY_SYSCONF_POSITIVE(_SC_BC_BASE_MAX);
825   VERIFY_SYSCONF_POSITIVE(_SC_BC_DIM_MAX);
826   VERIFY_SYSCONF_POSITIVE(_SC_BC_SCALE_MAX);
827   VERIFY_SYSCONF_POSITIVE(_SC_CHILD_MAX);
828   VERIFY_SYSCONF_POSITIVE(_SC_CLK_TCK);
829   VERIFY_SYSCONF_POSITIVE(_SC_COLL_WEIGHTS_MAX);
830   VERIFY_SYSCONF_POSIX_VERSION(_SC_CPUTIME);
831   VERIFY_SYSCONF_POSITIVE(_SC_EXPR_NEST_MAX);
832   VERIFY_SYSCONF_POSITIVE(_SC_LINE_MAX);
833   VERIFY_SYSCONF_POSITIVE(_SC_NGROUPS_MAX);
834   VERIFY_SYSCONF_POSITIVE(_SC_OPEN_MAX);
835   VERIFY_SYSCONF_POSITIVE(_SC_PASS_MAX);
836   VERIFY_SYSCONF_POSIX_VERSION(_SC_2_C_BIND);
837   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_FORT_DEV);
838   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_FORT_RUN);
839   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_UPE);
840   VERIFY_SYSCONF_POSITIVE(_SC_JOB_CONTROL);
841   VERIFY_SYSCONF_POSITIVE(_SC_SAVED_IDS);
842   VERIFY_SYSCONF_POSIX_VERSION(_SC_VERSION);
843   VERIFY_SYSCONF_POSITIVE(_SC_RE_DUP_MAX);
844   VERIFY_SYSCONF_POSITIVE(_SC_STREAM_MAX);
845   VERIFY_SYSCONF_POSITIVE(_SC_TZNAME_MAX);
846   VerifySysconf(_SC_XOPEN_VERSION, "_SC_XOPEN_VERSION", [](long v){return v == _XOPEN_VERSION;});
847   VERIFY_SYSCONF_POSITIVE(_SC_ATEXIT_MAX);
848   VERIFY_SYSCONF_POSITIVE(_SC_IOV_MAX);
849   VERIFY_SYSCONF_POSITIVE(_SC_PAGESIZE);
850   VERIFY_SYSCONF_POSITIVE(_SC_PAGE_SIZE);
851   VerifySysconf(_SC_PAGE_SIZE, "_SC_PAGE_SIZE",
852                 [](long v){return v == sysconf(_SC_PAGESIZE) && v == getpagesize();});
853   VERIFY_SYSCONF_POSITIVE(_SC_XOPEN_UNIX);
854   VERIFY_SYSCONF_POSITIVE(_SC_AIO_LISTIO_MAX);
855   VERIFY_SYSCONF_POSITIVE(_SC_AIO_MAX);
856   VerifySysconf(_SC_AIO_PRIO_DELTA_MAX, "_SC_AIO_PRIO_DELTA_MAX", [](long v){return v >= 0;});
857   VERIFY_SYSCONF_POSITIVE(_SC_DELAYTIMER_MAX);
858   VERIFY_SYSCONF_POSITIVE(_SC_MQ_OPEN_MAX);
859   VERIFY_SYSCONF_POSITIVE(_SC_MQ_PRIO_MAX);
860   VERIFY_SYSCONF_POSITIVE(_SC_RTSIG_MAX);
861   VERIFY_SYSCONF_POSITIVE(_SC_SEM_NSEMS_MAX);
862   VERIFY_SYSCONF_POSITIVE(_SC_SEM_VALUE_MAX);
863   VERIFY_SYSCONF_POSIX_VERSION(_SC_SPIN_LOCKS);
864   VERIFY_SYSCONF_POSITIVE(_SC_TIMER_MAX);
865   VERIFY_SYSCONF_POSIX_VERSION(_SC_FSYNC);
866   VERIFY_SYSCONF_POSIX_VERSION(_SC_MAPPED_FILES);
867   VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMLOCK);
868   VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMLOCK_RANGE);
869   VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMORY_PROTECTION);
870   VERIFY_SYSCONF_POSIX_VERSION(_SC_PRIORITY_SCHEDULING);
871   VERIFY_SYSCONF_POSIX_VERSION(_SC_REALTIME_SIGNALS);
872   VERIFY_SYSCONF_POSIX_VERSION(_SC_SEMAPHORES);
873   VERIFY_SYSCONF_POSIX_VERSION(_SC_SYNCHRONIZED_IO);
874   VERIFY_SYSCONF_POSIX_VERSION(_SC_TIMERS);
875   VERIFY_SYSCONF_POSITIVE(_SC_GETGR_R_SIZE_MAX);
876   VERIFY_SYSCONF_POSITIVE(_SC_GETPW_R_SIZE_MAX);
877   VERIFY_SYSCONF_POSITIVE(_SC_LOGIN_NAME_MAX);
878   VERIFY_SYSCONF_POSITIVE(_SC_THREAD_DESTRUCTOR_ITERATIONS);
879   VERIFY_SYSCONF_POSITIVE(_SC_THREAD_KEYS_MAX);
880   VERIFY_SYSCONF_POSITIVE(_SC_THREAD_STACK_MIN);
881   VERIFY_SYSCONF_POSITIVE(_SC_THREAD_THREADS_MAX);
882   VERIFY_SYSCONF_POSITIVE(_SC_TTY_NAME_MAX);
883   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREADS);
884   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_ATTR_STACKADDR);
885   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_ATTR_STACKSIZE);
886   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_PRIORITY_SCHEDULING);
887   VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_PRIO_INHERIT);
888   VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_PRIO_PROTECT);
889   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_SAFE_FUNCTIONS);
890   VERIFY_SYSCONF_POSITIVE(_SC_NPROCESSORS_CONF);
891   VERIFY_SYSCONF_POSITIVE(_SC_NPROCESSORS_ONLN);
892   VERIFY_SYSCONF_POSITIVE(_SC_PHYS_PAGES);
893   VERIFY_SYSCONF_POSITIVE(_SC_AVPHYS_PAGES);
894   VERIFY_SYSCONF_POSIX_VERSION(_SC_MONOTONIC_CLOCK);
895   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS);
896   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_ACCOUNTING);
897   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_CHECKPOINT);
898   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_LOCATE);
899   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_MESSAGE);
900   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_TRACK);
901   VERIFY_SYSCONF_POSIX_VERSION(_SC_CLOCK_SELECTION);
902   VERIFY_SYSCONF_POSITIVE(_SC_HOST_NAME_MAX);
903   VERIFY_SYSCONF_POSIX_VERSION(_SC_IPV6);
904   VERIFY_SYSCONF_POSIX_VERSION(_SC_RAW_SOCKETS);
905   VERIFY_SYSCONF_POSIX_VERSION(_SC_READER_WRITER_LOCKS);
906   VERIFY_SYSCONF_POSITIVE(_SC_REGEXP);
907   VERIFY_SYSCONF_POSITIVE(_SC_SHELL);
908   VERIFY_SYSCONF_UNSUPPORTED(_SC_SPORADIC_SERVER);
909   VERIFY_SYSCONF_POSITIVE(_SC_SYMLOOP_MAX);
910   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_CPUTIME);
911   VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_SPORADIC_SERVER);
912   VERIFY_SYSCONF_POSIX_VERSION(_SC_TIMEOUTS);
913   VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE);
914   VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_EVENT_FILTER);
915   VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_EVENT_NAME_MAX);
916   VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_INHERIT);
917   VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_LOG);
918   VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_NAME_MAX);
919   VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_SYS_MAX);
920   VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_USER_EVENT_MAX);
921   VERIFY_SYSCONF_UNSUPPORTED(_SC_TYPED_MEMORY_OBJECTS);
922   VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_STREAMS);
923 
924 #if defined(__LP64__)
925   VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_ILP32_OFF32);
926   VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_ILP32_OFFBIG);
927   VERIFY_SYSCONF_POSITIVE(_SC_V7_LP64_OFF64);
928   VERIFY_SYSCONF_POSITIVE(_SC_V7_LPBIG_OFFBIG);
929 #else
930   VERIFY_SYSCONF_POSITIVE(_SC_V7_ILP32_OFF32);
931 #if defined(__BIONIC__)
932   // bionic does not support 64 bits off_t type on 32bit machine.
933   VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_ILP32_OFFBIG);
934 #endif
935   VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_LP64_OFF64);
936   VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_LPBIG_OFFBIG);
937 #endif
938 
939 #if defined(__BIONIC__)
940   // Tests can only run on bionic, as bionic and glibc have different support for these options.
941   // Below options are not supported on bionic yet.
942   VERIFY_SYSCONF_UNSUPPORTED(_SC_ASYNCHRONOUS_IO);
943   VERIFY_SYSCONF_UNSUPPORTED(_SC_MESSAGE_PASSING);
944   VERIFY_SYSCONF_UNSUPPORTED(_SC_PRIORITIZED_IO);
945   VERIFY_SYSCONF_UNSUPPORTED(_SC_SHARED_MEMORY_OBJECTS);
946   VERIFY_SYSCONF_UNSUPPORTED(_SC_SPAWN);
947   VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_PROCESS_SHARED);
948   VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_ROBUST_PRIO_INHERIT);
949   VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_ROBUST_PRIO_PROTECT);
950 
951   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_C_DEV);
952   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_CHAR_TERM);
953   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_LOCALEDEF);
954   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_SW_DEV);
955   VERIFY_SYSCONF_UNSUPPORTED(_SC_2_VERSION);
956 
957   VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_CRYPT);
958   VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_ENH_I18N);
959   VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_LEGACY);
960   VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_REALTIME);
961   VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_REALTIME_THREADS);
962   VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_SHM);
963   VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_UUCP);
964 #endif // defined(__BIONIC__)
965 }
966 
TEST(UNISTD_TEST,get_cpu_count_from_string)967 TEST(UNISTD_TEST, get_cpu_count_from_string) {
968   ASSERT_EQ(0, GetCpuCountFromString(" "));
969   ASSERT_EQ(1, GetCpuCountFromString("0"));
970   ASSERT_EQ(40, GetCpuCountFromString("0-39"));
971   ASSERT_EQ(4, GetCpuCountFromString("0, 1-2, 4\n"));
972 }
973 
TEST(UNISTD_TEST,sysconf_SC_NPROCESSORS_ONLN)974 TEST(UNISTD_TEST, sysconf_SC_NPROCESSORS_ONLN) {
975   std::string line;
976   ASSERT_TRUE(android::base::ReadFileToString("/sys/devices/system/cpu/online", &line));
977   long online_cpus = 0;
978   for (const std::string& s : android::base::Split(line, ",")) {
979     std::vector<std::string> numbers = android::base::Split(s, "-");
980     if (numbers.size() == 1u) {
981       online_cpus++;
982     } else {
983       online_cpus += atoi(numbers[1].c_str()) - atoi(numbers[0].c_str()) + 1;
984     }
985   }
986   ASSERT_EQ(online_cpus, sysconf(_SC_NPROCESSORS_ONLN));
987 }
988 
TEST(UNISTD_TEST,sysconf_SC_ARG_MAX)989 TEST(UNISTD_TEST, sysconf_SC_ARG_MAX) {
990   // Since Linux 2.6.23, ARG_MAX isn't a constant and depends on RLIMIT_STACK.
991 
992   // Get our current limit, and set things up so we restore the limit.
993   rlimit rl;
994   ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
995   uint64_t original_rlim_cur = rl.rlim_cur;
996   if (rl.rlim_cur == RLIM_INFINITY) {
997     rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
998   }
999   auto guard = make_scope_guard([&rl, original_rlim_cur]() {
1000     rl.rlim_cur = original_rlim_cur;
1001     ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1002   });
1003 
1004   // _SC_ARG_MAX should be 1/4 the stack size.
1005   EXPECT_EQ(static_cast<long>(rl.rlim_cur / 4), sysconf(_SC_ARG_MAX));
1006 
1007   // If you have a really small stack, the kernel still guarantees "32 pages" (fs/exec.c).
1008   rl.rlim_cur = 1024;
1009   rl.rlim_max = RLIM_INFINITY;
1010   ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1011 
1012   EXPECT_EQ(static_cast<long>(32 * sysconf(_SC_PAGE_SIZE)), sysconf(_SC_ARG_MAX));
1013 
1014   // With a 128-page stack limit, we know exactly what _SC_ARG_MAX should be...
1015   rl.rlim_cur = 128 * sysconf(_SC_PAGE_SIZE);
1016   rl.rlim_max = RLIM_INFINITY;
1017   ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1018 
1019   EXPECT_EQ(static_cast<long>((128 * sysconf(_SC_PAGE_SIZE)) / 4), sysconf(_SC_ARG_MAX));
1020 }
1021 
TEST(UNISTD_TEST,dup2_same)1022 TEST(UNISTD_TEST, dup2_same) {
1023   // POSIX says of dup2:
1024   // If fildes2 is already a valid open file descriptor ...
1025   // [and] fildes is equal to fildes2 ... dup2() shall return
1026   // fildes2 without closing it.
1027   // This isn't true of dup3(2), so we need to manually implement that.
1028 
1029   // Equal and valid.
1030   int fd = open("/proc/version", O_RDONLY);
1031   ASSERT_TRUE(fd != -1);
1032   ASSERT_EQ(fd, dup2(fd, fd));
1033   ASSERT_EQ(0, close(fd)); // Check that dup2 didn't close fd.
1034 
1035   // Equal, but invalid.
1036   errno = 0;
1037   ASSERT_EQ(-1, dup2(fd, fd));
1038   ASSERT_EQ(EBADF, errno);
1039 }
1040 
TEST(UNISTD_TEST,lockf_smoke)1041 TEST(UNISTD_TEST, lockf_smoke) {
1042   constexpr off64_t file_size = 32*1024LL;
1043 
1044   TemporaryFile tf;
1045   ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1046 
1047   // Lock everything.
1048   ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1049   ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, file_size));
1050 
1051   // Try-lock everything, this should succeed too.
1052   ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1053   ASSERT_EQ(0, lockf64(tf.fd, F_TLOCK, file_size));
1054 
1055   // Check status.
1056   ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1057   ASSERT_EQ(0, lockf64(tf.fd, F_TEST, file_size));
1058 
1059   // Unlock file.
1060   ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1061   ASSERT_EQ(0, lockf64(tf.fd, F_ULOCK, file_size));
1062 }
1063 
TEST(UNISTD_TEST,lockf_zero)1064 TEST(UNISTD_TEST, lockf_zero) {
1065   constexpr off64_t file_size = 32*1024LL;
1066 
1067   TemporaryFile tf;
1068   ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1069 
1070   // Lock everything by specifying a size of 0 (meaning "to the end, even if it changes").
1071   ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1072   ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, 0));
1073 
1074   // Check that it's locked.
1075   ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1076   ASSERT_EQ(0, lockf64(tf.fd, F_TEST, file_size));
1077 
1078   // Move the end.
1079   ASSERT_EQ(0, ftruncate(tf.fd, 2*file_size));
1080 
1081   // Check that the new section is locked too.
1082   ASSERT_EQ(file_size, lseek64(tf.fd, file_size, SEEK_SET));
1083   ASSERT_EQ(0, lockf64(tf.fd, F_TEST, 2*file_size));
1084 }
1085 
TEST(UNISTD_TEST,lockf_negative)1086 TEST(UNISTD_TEST, lockf_negative) {
1087   constexpr off64_t file_size = 32*1024LL;
1088 
1089   TemporaryFile tf;
1090   ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1091 
1092   // Lock everything, but specifying the range in reverse.
1093   ASSERT_EQ(file_size, lseek64(tf.fd, file_size, SEEK_SET));
1094   ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, -file_size));
1095 
1096   // Check that it's locked.
1097   ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1098   ASSERT_EQ(0, lockf64(tf.fd, F_TEST, file_size));
1099 }
1100 
TEST(UNISTD_TEST,lockf_with_child)1101 TEST(UNISTD_TEST, lockf_with_child) {
1102   constexpr off64_t file_size = 32*1024LL;
1103 
1104   TemporaryFile tf;
1105   ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1106 
1107   // Lock everything.
1108   ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1109   ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, file_size));
1110 
1111   // Fork a child process
1112   pid_t pid = fork();
1113   ASSERT_NE(-1, pid);
1114   if (pid == 0) {
1115     // Check that the child cannot lock the file.
1116     ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1117     ASSERT_EQ(-1, lockf64(tf.fd, F_TLOCK, file_size));
1118     ASSERT_EQ(EAGAIN, errno);
1119     // Check also that it reports itself as locked.
1120     ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1121     ASSERT_EQ(-1, lockf64(tf.fd, F_TEST, file_size));
1122     ASSERT_EQ(EACCES, errno);
1123     _exit(0);
1124   }
1125   AssertChildExited(pid, 0);
1126 }
1127 
TEST(UNISTD_TEST,lockf_partial_with_child)1128 TEST(UNISTD_TEST, lockf_partial_with_child) {
1129   constexpr off64_t file_size = 32*1024LL;
1130 
1131   TemporaryFile tf;
1132   ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1133 
1134   // Lock the first half of the file.
1135   ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1136   ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, file_size/2));
1137 
1138   // Fork a child process.
1139   pid_t pid = fork();
1140   ASSERT_NE(-1, pid);
1141   if (pid == 0) {
1142     // Check that the child can lock the other half.
1143     ASSERT_EQ(file_size/2, lseek64(tf.fd, file_size/2, SEEK_SET));
1144     ASSERT_EQ(0, lockf64(tf.fd, F_TLOCK, file_size/2));
1145     // Check that the child cannot lock the first half.
1146     ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1147     ASSERT_EQ(-1, lockf64(tf.fd, F_TEST, file_size/2));
1148     ASSERT_EQ(EACCES, errno);
1149     // Check also that it reports itself as locked.
1150     ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1151     ASSERT_EQ(-1, lockf64(tf.fd, F_TEST, file_size/2));
1152     ASSERT_EQ(EACCES, errno);
1153     _exit(0);
1154   }
1155   AssertChildExited(pid, 0);
1156 
1157   // The second half was locked by the child, but the lock disappeared
1158   // when the process exited, so check it can be locked now.
1159   ASSERT_EQ(file_size/2, lseek64(tf.fd, file_size/2, SEEK_SET));
1160   ASSERT_EQ(0, lockf64(tf.fd, F_TLOCK, file_size/2));
1161 }
1162 
TEST(UNISTD_TEST,getdomainname)1163 TEST(UNISTD_TEST, getdomainname) {
1164   struct utsname u;
1165   ASSERT_EQ(0, uname(&u));
1166 
1167   char buf[sizeof(u.domainname)];
1168   ASSERT_EQ(0, getdomainname(buf, sizeof(buf)));
1169   EXPECT_STREQ(u.domainname, buf);
1170 
1171 #if defined(__BIONIC__)
1172   // bionic and glibc have different behaviors when len is too small
1173   ASSERT_EQ(-1, getdomainname(buf, strlen(u.domainname)));
1174   EXPECT_EQ(EINVAL, errno);
1175 #endif
1176 }
1177 
TEST(UNISTD_TEST,setdomainname)1178 TEST(UNISTD_TEST, setdomainname) {
1179   __user_cap_header_struct header;
1180   memset(&header, 0, sizeof(header));
1181   header.version = _LINUX_CAPABILITY_VERSION_3;
1182 
1183   __user_cap_data_struct old_caps[_LINUX_CAPABILITY_U32S_3];
1184   ASSERT_EQ(0, capget(&header, &old_caps[0]));
1185 
1186   auto admin_idx = CAP_TO_INDEX(CAP_SYS_ADMIN);
1187   auto admin_mask = CAP_TO_MASK(CAP_SYS_ADMIN);
1188   bool has_admin = old_caps[admin_idx].effective & admin_mask;
1189   if (has_admin) {
1190     __user_cap_data_struct new_caps[_LINUX_CAPABILITY_U32S_3];
1191     memcpy(new_caps, old_caps, sizeof(new_caps));
1192     new_caps[admin_idx].effective &= ~admin_mask;
1193 
1194     ASSERT_EQ(0, capset(&header, &new_caps[0])) << "failed to drop admin privileges";
1195   }
1196 
1197   const char* name = "newdomainname";
1198   ASSERT_EQ(-1, setdomainname(name, strlen(name)));
1199   ASSERT_EQ(EPERM, errno);
1200 
1201   if (has_admin) {
1202     ASSERT_EQ(0, capset(&header, &old_caps[0])) << "failed to restore admin privileges";
1203   }
1204 }
1205 
1206 #if defined(__GLIBC__)
1207 #define BIN_DIR "/bin/"
1208 #else
1209 #define BIN_DIR "/system/bin/"
1210 #endif
1211 
TEST(UNISTD_TEST,execve_failure)1212 TEST(UNISTD_TEST, execve_failure) {
1213   ExecTestHelper eth;
1214   errno = 0;
1215   ASSERT_EQ(-1, execve("/", eth.GetArgs(), eth.GetEnv()));
1216   ASSERT_EQ(EACCES, errno);
1217 }
1218 
TEST(UNISTD_TEST,execve_args)1219 TEST(UNISTD_TEST, execve_args) {
1220   // int execve(const char* path, char* argv[], char* envp[]);
1221 
1222   // Test basic argument passing.
1223   ExecTestHelper eth;
1224   eth.SetArgs({"echo", "hello", "world", nullptr});
1225   eth.Run([&]() { execve(BIN_DIR "echo", eth.GetArgs(), eth.GetEnv()); }, 0, "hello world\n");
1226 
1227   // Test environment variable setting too.
1228   eth.SetArgs({"printenv", nullptr});
1229   eth.SetEnv({"A=B", nullptr});
1230   eth.Run([&]() { execve(BIN_DIR "printenv", eth.GetArgs(), eth.GetEnv()); }, 0, "A=B\n");
1231 }
1232 
TEST(UNISTD_TEST,execl_failure)1233 TEST(UNISTD_TEST, execl_failure) {
1234   errno = 0;
1235   ASSERT_EQ(-1, execl("/", "/", nullptr));
1236   ASSERT_EQ(EACCES, errno);
1237 }
1238 
TEST(UNISTD_TEST,execl)1239 TEST(UNISTD_TEST, execl) {
1240   ExecTestHelper eth;
1241   // int execl(const char* path, const char* arg, ...);
1242   eth.Run([&]() { execl(BIN_DIR "echo", "echo", "hello", "world", nullptr); }, 0, "hello world\n");
1243 }
1244 
TEST(UNISTD_TEST,execle_failure)1245 TEST(UNISTD_TEST, execle_failure) {
1246   ExecTestHelper eth;
1247   errno = 0;
1248   ASSERT_EQ(-1, execle("/", "/", nullptr, eth.GetEnv()));
1249   ASSERT_EQ(EACCES, errno);
1250 }
1251 
TEST(UNISTD_TEST,execle)1252 TEST(UNISTD_TEST, execle) {
1253   ExecTestHelper eth;
1254   eth.SetEnv({"A=B", nullptr});
1255   // int execle(const char* path, const char* arg, ..., char* envp[]);
1256   eth.Run([&]() { execle(BIN_DIR "printenv", "printenv", nullptr, eth.GetEnv()); }, 0, "A=B\n");
1257 }
1258 
TEST(UNISTD_TEST,execv_failure)1259 TEST(UNISTD_TEST, execv_failure) {
1260   ExecTestHelper eth;
1261   errno = 0;
1262   ASSERT_EQ(-1, execv("/", eth.GetArgs()));
1263   ASSERT_EQ(EACCES, errno);
1264 }
1265 
TEST(UNISTD_TEST,execv)1266 TEST(UNISTD_TEST, execv) {
1267   ExecTestHelper eth;
1268   eth.SetArgs({"echo", "hello", "world", nullptr});
1269   // int execv(const char* path, char* argv[]);
1270   eth.Run([&]() { execv(BIN_DIR "echo", eth.GetArgs()); }, 0, "hello world\n");
1271 }
1272 
TEST(UNISTD_TEST,execlp_failure)1273 TEST(UNISTD_TEST, execlp_failure) {
1274   errno = 0;
1275   ASSERT_EQ(-1, execlp("/", "/", nullptr));
1276   ASSERT_EQ(EACCES, errno);
1277 }
1278 
TEST(UNISTD_TEST,execlp)1279 TEST(UNISTD_TEST, execlp) {
1280   ExecTestHelper eth;
1281   // int execlp(const char* file, const char* arg, ...);
1282   eth.Run([&]() { execlp("echo", "echo", "hello", "world", nullptr); }, 0, "hello world\n");
1283 }
1284 
TEST(UNISTD_TEST,execvp_failure)1285 TEST(UNISTD_TEST, execvp_failure) {
1286   ExecTestHelper eth;
1287   eth.SetArgs({nullptr});
1288   errno = 0;
1289   ASSERT_EQ(-1, execvp("/", eth.GetArgs()));
1290   ASSERT_EQ(EACCES, errno);
1291 }
1292 
TEST(UNISTD_TEST,execvp)1293 TEST(UNISTD_TEST, execvp) {
1294   ExecTestHelper eth;
1295   eth.SetArgs({"echo", "hello", "world", nullptr});
1296   // int execvp(const char* file, char* argv[]);
1297   eth.Run([&]() { execvp("echo", eth.GetArgs()); }, 0, "hello world\n");
1298 }
1299 
TEST(UNISTD_TEST,execvpe_failure)1300 TEST(UNISTD_TEST, execvpe_failure) {
1301   ExecTestHelper eth;
1302   errno = 0;
1303   ASSERT_EQ(-1, execvpe("this-does-not-exist", eth.GetArgs(), eth.GetEnv()));
1304   // Running in CTS we might not even be able to search all directories in $PATH.
1305   ASSERT_TRUE(errno == ENOENT || errno == EACCES);
1306 }
1307 
TEST(UNISTD_TEST,execvpe)1308 TEST(UNISTD_TEST, execvpe) {
1309   // int execvpe(const char* file, char* argv[], char* envp[]);
1310 
1311   // Test basic argument passing.
1312   ExecTestHelper eth;
1313   eth.SetArgs({"echo", "hello", "world", nullptr});
1314   eth.Run([&]() { execvpe("echo", eth.GetArgs(), eth.GetEnv()); }, 0, "hello world\n");
1315 
1316   // Test environment variable setting too.
1317   eth.SetArgs({"printenv", nullptr});
1318   eth.SetEnv({"A=B", nullptr});
1319   eth.Run([&]() { execvpe("printenv", eth.GetArgs(), eth.GetEnv()); }, 0, "A=B\n");
1320 }
1321 
TEST(UNISTD_TEST,execvpe_ENOEXEC)1322 TEST(UNISTD_TEST, execvpe_ENOEXEC) {
1323   // Create a shell script with #!.
1324   TemporaryFile tf;
1325   ASSERT_TRUE(android::base::WriteStringToFile("#!" BIN_DIR "sh\necho script\n", tf.filename));
1326 
1327   // Set $PATH so we can find it.
1328   setenv("PATH", dirname(tf.filename), 1);
1329 
1330   ExecTestHelper eth;
1331   eth.SetArgs({basename(tf.filename), nullptr});
1332 
1333   // It's not inherently executable.
1334   errno = 0;
1335   ASSERT_EQ(-1, execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()));
1336   ASSERT_EQ(EACCES, errno);
1337 
1338   // Make it executable (and keep it writable because we're going to rewrite it below).
1339   ASSERT_EQ(0, chmod(tf.filename, 0777));
1340 
1341   // TemporaryFile will have a writable fd, so we can test ETXTBSY while we're here...
1342   errno = 0;
1343   ASSERT_EQ(-1, execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()));
1344   ASSERT_EQ(ETXTBSY, errno);
1345 
1346   // 1. The simplest test: the kernel should handle this.
1347   ASSERT_EQ(0, close(tf.fd));
1348   eth.Run([&]() { execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()); }, 0, "script\n");
1349 
1350   // 2. Try again without a #!. We should have to handle this ourselves.
1351   ASSERT_TRUE(android::base::WriteStringToFile("echo script\n", tf.filename));
1352   eth.Run([&]() { execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()); }, 0, "script\n");
1353 
1354   // 3. Again without a #!, but also with a leading '/', since that's a special case in the
1355   // implementation.
1356   eth.Run([&]() { execvpe(tf.filename, eth.GetArgs(), eth.GetEnv()); }, 0, "script\n");
1357 }
1358 
TEST(UNISTD_TEST,execvp_libcore_test_55017)1359 TEST(UNISTD_TEST, execvp_libcore_test_55017) {
1360   ExecTestHelper eth;
1361   eth.SetArgs({"/system/bin/does-not-exist", nullptr});
1362 
1363   errno = 0;
1364   ASSERT_EQ(-1, execvp("/system/bin/does-not-exist", eth.GetArgs()));
1365   ASSERT_EQ(ENOENT, errno);
1366 }
1367 
TEST(UNISTD_TEST,exec_argv0_null)1368 TEST(UNISTD_TEST, exec_argv0_null) {
1369   // http://b/33276926
1370   char* args[] = {nullptr};
1371   char* envs[] = {nullptr};
1372   ASSERT_EXIT(execve("/system/bin/run-as", args, envs), testing::ExitedWithCode(1),
1373               "<unknown>: usage: run-as");
1374 }
1375