1 //===- unittest/Support/ProcessTest.cpp -----------------------------------===//
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 "llvm/ADT/Triple.h"
10 #include "llvm/Support/Error.h"
11 #include "llvm/Support/Host.h"
12 #include "llvm/Support/Process.h"
13 #include "gtest/gtest.h"
14 
15 #ifdef _WIN32
16 #include <windows.h>
17 #endif
18 
19 namespace {
20 
21 using namespace llvm;
22 using namespace sys;
23 
TEST(ProcessTest,GetProcessIdTest)24 TEST(ProcessTest, GetProcessIdTest) {
25   const Process::Pid pid = Process::getProcessId();
26 
27 #ifdef _WIN32
28   EXPECT_EQ((DWORD)pid, ::GetCurrentProcessId());
29 #else
30   EXPECT_EQ(pid, ::getpid());
31 #endif
32 }
33 
TEST(ProcessTest,GetRandomNumberTest)34 TEST(ProcessTest, GetRandomNumberTest) {
35   const unsigned r1 = Process::GetRandomNumber();
36   const unsigned r2 = Process::GetRandomNumber();
37   // It should be extremely unlikely that both r1 and r2 are 0.
38   EXPECT_NE((r1 | r2), 0u);
39 }
40 
41 #ifdef _MSC_VER
42 #define setenv(name, var, ignore) _putenv_s(name, var)
43 #endif
44 
45 #if HAVE_SETENV || _MSC_VER
TEST(ProcessTest,Basic)46 TEST(ProcessTest, Basic) {
47   setenv("__LLVM_TEST_ENVIRON_VAR__", "abc", true);
48   Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
49   EXPECT_TRUE(val.hasValue());
50   EXPECT_STREQ("abc", val->c_str());
51 }
52 
TEST(ProcessTest,None)53 TEST(ProcessTest, None) {
54   Optional<std::string> val(
55       Process::GetEnv("__LLVM_TEST_ENVIRON_NO_SUCH_VAR__"));
56   EXPECT_FALSE(val.hasValue());
57 }
58 #endif
59 
60 #ifdef _WIN32
61 
TEST(ProcessTest,EmptyVal)62 TEST(ProcessTest, EmptyVal) {
63   SetEnvironmentVariableA("__LLVM_TEST_ENVIRON_VAR__", "");
64   Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
65   EXPECT_TRUE(val.hasValue());
66   EXPECT_STREQ("", val->c_str());
67 }
68 
TEST(ProcessTest,Wchar)69 TEST(ProcessTest, Wchar) {
70   SetEnvironmentVariableW(L"__LLVM_TEST_ENVIRON_VAR__", L"abcdefghijklmnopqrs");
71   Optional<std::string> val(Process::GetEnv("__LLVM_TEST_ENVIRON_VAR__"));
72   EXPECT_TRUE(val.hasValue());
73   EXPECT_STREQ("abcdefghijklmnopqrs", val->c_str());
74 }
75 #endif
76 
77 class PageSizeTest : public testing::Test {
78   Triple Host;
79 
80 protected:
PageSizeTest()81   PageSizeTest() : Host(Triple::normalize(sys::getProcessTriple())) {}
82 
isSupported() const83   bool isSupported() const {
84     // For now just on X86-64 and Aarch64. This can be expanded in the future.
85     return (Host.getArch() == Triple::x86_64 ||
86             Host.getArch() == Triple::aarch64) &&
87            Host.getOS() == Triple::Linux;
88   }
89 
pageSizeAsExpected(unsigned PageSize) const90   bool pageSizeAsExpected(unsigned PageSize) const {
91     switch (Host.getArch()) {
92     case Triple::x86_64:
93       return PageSize == 4096;
94     case Triple::aarch64:
95       // supported granule sizes are 4k, 16k and 64k
96       return PageSize == 4096 || PageSize == 16384 || PageSize == 65536;
97     default:
98       llvm_unreachable("unexpected arch!");
99     }
100   }
101 };
102 
TEST_F(PageSizeTest,PageSize)103 TEST_F(PageSizeTest, PageSize) {
104   if (!isSupported())
105     return;
106 
107   llvm::Expected<unsigned> Result = llvm::sys::Process::getPageSize();
108   ASSERT_FALSE(!Result);
109   ASSERT_TRUE(pageSizeAsExpected(*Result));
110 }
111 
112 } // end anonymous namespace
113