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