| 1 | //===-- Unittests for prctl -----------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/sys/prctl/prctl.h" |
| 10 | #include "test/UnitTest/ErrnoCheckingTest.h" |
| 11 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
| 12 | #include <sys/prctl.h> |
| 13 | |
| 14 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; |
| 15 | using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; |
| 16 | using LlvmLibcSysPrctlTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; |
| 17 | |
| 18 | TEST_F(LlvmLibcSysPrctlTest, GetSetName) { |
| 19 | char name[17]; |
| 20 | unsigned long name_addr = 0; |
| 21 | ASSERT_THAT(LIBC_NAMESPACE::prctl(PR_GET_NAME, name_addr, 0, 0, 0), |
| 22 | Fails(EFAULT, -1)); |
| 23 | |
| 24 | name_addr = reinterpret_cast<unsigned long>("libc-test" ); |
| 25 | ASSERT_THAT(LIBC_NAMESPACE::prctl(PR_SET_NAME, name_addr, 0, 0, 0), |
| 26 | Succeeds()); |
| 27 | |
| 28 | name_addr = reinterpret_cast<unsigned long>(name); |
| 29 | ASSERT_THAT(LIBC_NAMESPACE::prctl(PR_GET_NAME, name_addr, 0, 0, 0), |
| 30 | Succeeds()); |
| 31 | ASSERT_STREQ(name, "libc-test" ); |
| 32 | } |
| 33 | |
| 34 | TEST_F(LlvmLibcSysPrctlTest, GetTHPDisable) { |
| 35 | // Manually check errno since the return value logic here is not |
| 36 | // covered in ErrnoSetterMatcher. |
| 37 | // Note that PR_GET_THP_DISABLE is not supported by QEMU. |
| 38 | int ret = LIBC_NAMESPACE::prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0); |
| 39 | ASSERT_ERRNO_SUCCESS(); |
| 40 | // PR_GET_THP_DISABLE return (as the function result) the current |
| 41 | // setting of the "THP disable" flag for the calling thread, which |
| 42 | // is either 1, if the flag is set; or 0, if it is not. |
| 43 | ASSERT_TRUE(ret == 0 || ret == 1); |
| 44 | } |
| 45 | |