Mike Frysinger | 3159ddc | 2022-09-07 12:24:21 | [diff] [blame] | 1 | /* Copyright 2018 Google LLC |
Mike Frysinger | 7c83058 | 2013-02-03 00:49:00 | [diff] [blame] | 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. |
Mike Frysinger | 3159ddc | 2022-09-07 12:24:21 | [diff] [blame] | 13 | * * Neither the name of Google LLC nor the names of its |
Mike Frysinger | 7c83058 | 2013-02-03 00:49:00 | [diff] [blame] | 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> |
Chris Palmer | 29f7c7e | 2020-08-13 00:10:59 | [diff] [blame] | 47 | #include <stdbool.h> |
Mike Frysinger | 7c83058 | 2013-02-03 00:49:00 | [diff] [blame] | 48 | #include <stdint.h> |
| 49 | #include <stdio.h> |
| 50 | #include <stdlib.h> |
Chris Palmer | 29f7c7e | 2020-08-13 00:10:59 | [diff] [blame] | 51 | #include <string.h> |
Mike Frysinger | 7c83058 | 2013-02-03 00:49:00 | [diff] [blame] | 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> |
Doug Kwan | 32a80cd | 2022-07-01 17:52:39 | [diff] [blame] | 57 | #include <unistd.h> |
Mike Frysinger | 7c83058 | 2013-02-03 00:49:00 | [diff] [blame] | 58 | |
| 59 | #include <linux/capability.h> |
| 60 | |
| 61 | #include "linux_syscall_support.h" |
| 62 | |
Matthew Denton | 92a65a8 | 2021-04-01 20:00:07 | [diff] [blame] | 63 | #define SKIP_TEST_EXIT_STATUS 77 |
| 64 | |
Mike Frysinger | 7c83058 | 2013-02-03 00:49:00 | [diff] [blame] | 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)) |
Doug Kwan | 32a80cd | 2022-07-01 17:52:39 | [diff] [blame] | 76 | |
| 77 | // Returns true iff pointed timevals are equal. |
| 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. |
| 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 | |