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 <errno.h>
13#include <sys/prctl.h>
14
15using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
16using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
17
18TEST(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
34TEST(LlvmLibcSysPrctlTest, GetTHPDisable) {
35 // Manually check errno since the return value logic here is not
36 // covered in ErrnoSetterMatcher.
37 LIBC_NAMESPACE::libc_errno = 0;
38 int ret = LIBC_NAMESPACE::prctl(PR_GET_THP_DISABLE, arg2: 0, arg3: 0, arg4: 0, arg5: 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

source code of libc/test/src/sys/prctl/linux/prctl_test.cpp