1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <android-base/file.h>
30 #include <android-base/unique_fd.h>
31 
32 #include "linker_phdr.h"
33 
34 #include <gtest/gtest.h>
35 
36 #include <fcntl.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 
42 #include <string>
43 
44 using ::android::base::GetExecutableDirectory;
45 using ::android::base::unique_fd;
46 
47 namespace {
48 
GetTestElfPath(const std::string & filename)49 static std::string GetTestElfPath(const std::string& filename) {
50   static std::string test_data_dir = GetExecutableDirectory();
51 
52   return test_data_dir + "/" + filename;
53 }
54 
GetPadSegment(const std::string & elf_path)55 bool GetPadSegment(const std::string& elf_path) {
56   std::string path = GetTestElfPath(elf_path);
57 
58   unique_fd fd{TEMP_FAILURE_RETRY(open(path.c_str(), O_CLOEXEC | O_RDWR))};
59   EXPECT_GE(fd.get(), 0) << "Failed to open " << path << ": " << strerror(errno);
60 
61   struct stat file_stat;
62   EXPECT_NE(TEMP_FAILURE_RETRY(fstat(fd.get(), &file_stat)), -1)
63         << "Failed to stat " << path << ": " << strerror(errno);
64 
65   ElfReader elf_reader;
66   EXPECT_TRUE(elf_reader.Read(path.c_str(), fd.get(), 0, file_stat.st_size))
67         << "Failed to read ELF file";
68 
69   return elf_reader.should_pad_segments();
70 }
71 
72 };  // anonymous namespace
73 
TEST(crt_pad_segment,note_absent)74 TEST(crt_pad_segment, note_absent) {
75   if (!page_size_migration_supported()) {
76     GTEST_SKIP() << "Kernel does not support page size migration";
77   }
78   ASSERT_FALSE(GetPadSegment("no_crt_pad_segment.so"));
79 }
80 
TEST(crt_pad_segment,note_present_and_enabled)81 TEST(crt_pad_segment, note_present_and_enabled) {
82   if (!page_size_migration_supported()) {
83     GTEST_SKIP() << "Kernel does not support page size migration";
84   }
85   ASSERT_TRUE(GetPadSegment("crt_pad_segment_enabled.so"));
86 }
87 
TEST(crt_pad_segment,note_present_and_disabled)88 TEST(crt_pad_segment, note_present_and_disabled) {
89   if (!page_size_migration_supported()) {
90     GTEST_SKIP() << "Kernel does not support page size migration";
91   }
92   ASSERT_FALSE(GetPadSegment("crt_pad_segment_disabled.so"));
93 }
94