• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018, Google Inc.  All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above
10  * copyright notice, this list of conditions and the following disclaimer
11  * in the documentation and/or other materials provided with the
12  * distribution.
13  *     * Neither the name of Google Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * Make sure it's defined before including anything else.  A number of syscalls
32  * are GNU extensions and rely on being exported by glibc.
33  */
34 #ifndef _GNU_SOURCE
35 #define _GNU_SOURCE
36 #endif
37 
38 /*
39  * Make sure the assert checks aren't removed as all the unittests are based
40  * on them.
41  */
42 #undef NDEBUG
43 
44 #include <assert.h>
45 #include <fcntl.h>
46 #include <sched.h>
47 #include <stdbool.h>
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <sys/mman.h>
53 #include <sys/prctl.h>
54 #include <sys/stat.h>
55 #include <sys/vfs.h>
56 #include <sys/wait.h>
57 #include <unistd.h>
58 
59 #include <linux/capability.h>
60 
61 #include "linux_syscall_support.h"
62 
63 #define SKIP_TEST_EXIT_STATUS 77
64 
assert_buffers_eq_len(const void * buf1,const void * buf2,size_t len)65 void assert_buffers_eq_len(const void *buf1, const void *buf2, size_t len) {
66   const uint8_t *u8_1 = (const uint8_t *)buf1;
67   const uint8_t *u8_2 = (const uint8_t *)buf2;
68   size_t i;
69 
70   for (i = 0; i < len; ++i) {
71     if (u8_1[i] != u8_2[i])
72       printf("offset %zu: %02x != %02x\n", i, u8_1[i], u8_2[i]);
73   }
74 }
75 #define assert_buffers_eq(obj1, obj2) assert_buffers_eq_len(obj1, obj2, sizeof(*obj1))
76 
77 // Returns true iff pointed timevals are equal.
kernel_timeval_eq(const struct kernel_timeval * lhs,const struct kernel_timeval * rhs)78 static inline bool kernel_timeval_eq(const struct kernel_timeval* lhs,
79                                      const struct kernel_timeval* rhs) {
80   return (lhs->tv_sec == rhs->tv_sec) && (lhs->tv_usec == rhs->tv_usec);
81 }
82 
83 // Returns true iff pointed itimervals are equal.
kernel_itimerval_eq(const struct kernel_itimerval * lhs,const struct kernel_itimerval * rhs)84 static inline bool kernel_itimerval_eq(const struct kernel_itimerval* lhs,
85                                        const struct kernel_itimerval* rhs) {
86   return kernel_timeval_eq(&lhs->it_interval, &rhs->it_interval) &&
87          kernel_timeval_eq(&lhs->it_value, &rhs->it_value);
88 }
89 
90