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/errno/libc_errno.h" 10 #include "src/sys/prctl/prctl.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 17 TEST(LlvmLibcSysPrctlTest, GetSetName) { 18 char name[17]; 19 unsigned long name_addr = 0; 20 ASSERT_THAT(LIBC_NAMESPACE::prctl(PR_GET_NAME, name_addr, 0, 0, 0), 21 Fails(EFAULT, -1)); 22 23 name_addr = reinterpret_cast<unsigned long>("libc-test"); 24 ASSERT_THAT(LIBC_NAMESPACE::prctl(PR_SET_NAME, name_addr, 0, 0, 0), 25 Succeeds()); 26 27 name_addr = reinterpret_cast<unsigned long>(name); 28 ASSERT_THAT(LIBC_NAMESPACE::prctl(PR_GET_NAME, name_addr, 0, 0, 0), 29 Succeeds()); 30 ASSERT_STREQ(name, "libc-test"); 31 } 32 33 TEST(LlvmLibcSysPrctlTest, GetTHPDisable) { 34 // Manually check errno since the return value logic here is not 35 // covered in ErrnoSetterMatcher. 36 LIBC_NAMESPACE::libc_errno = 0; 37 int ret = LIBC_NAMESPACE::prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0); 38 ASSERT_ERRNO_SUCCESS(); 39 // PR_GET_THP_DISABLE return (as the function result) the current 40 // setting of the "THP disable" flag for the calling thread, which 41 // is either 1, if the flag is set; or 0, if it is not. 42 ASSERT_TRUE(ret == 0 || ret == 1); 43 } 44