1 /*
2 * Copyright (C) 2021 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 <gtest/gtest.h>
30
31 #include <link.h>
32
33 #include "ldso_trampoline_phdr.h"
34
35 struct PhdrTrimSegmentTestParameters {
36 std::string name;
37 size_t in_vaddr, in_filesz, in_memsz;
38 size_t out_vaddr, out_filesz, out_memsz;
39 size_t trim_start, trim_end;
40 };
41
42 class PhdrTrimSegmentTests : public ::testing::TestWithParam<PhdrTrimSegmentTestParameters> {
43 protected:
44 };
45
TEST_P(PhdrTrimSegmentTests,Tests)46 TEST_P(PhdrTrimSegmentTests, Tests) {
47 auto params = GetParam();
48 ElfW(Phdr) phdr{};
49 phdr.p_vaddr = params.in_vaddr;
50 phdr.p_paddr = params.in_vaddr;
51 phdr.p_filesz = params.in_filesz;
52 phdr.p_memsz = params.in_memsz;
53 phdr_trim_segment(&phdr, params.trim_start, params.trim_end);
54 ASSERT_EQ(phdr.p_vaddr, params.out_vaddr);
55 ASSERT_EQ(phdr.p_paddr, params.out_vaddr);
56 ASSERT_EQ(phdr.p_filesz, params.out_filesz);
57 ASSERT_EQ(phdr.p_memsz, params.out_memsz);
58 }
59
60 INSTANTIATE_TEST_CASE_P(ldso_trampoline, PhdrTrimSegmentTests,
61 ::testing::Values(
62 PhdrTrimSegmentTestParameters{
63 .name = "noop",
64 .in_vaddr = 1,
65 .in_filesz = 2,
66 .in_memsz = 2,
67 .trim_start = 1,
68 .trim_end = 3,
69 .out_vaddr = 1,
70 .out_filesz = 2,
71 .out_memsz = 2,
72 },
73 PhdrTrimSegmentTestParameters{
74 .name = "trim_beginning",
75 .in_vaddr = 1,
76 .in_filesz = 2,
77 .in_memsz = 2,
78 .trim_start = 2,
79 .trim_end = 3,
80 .out_vaddr = 2,
81 .out_filesz = 1,
82 .out_memsz = 1,
83 },
84 PhdrTrimSegmentTestParameters{
85 .name = "trim_end",
86 .in_vaddr = 1,
87 .in_filesz = 2,
88 .in_memsz = 2,
89 .trim_start = 1,
90 .trim_end = 2,
91 .out_vaddr = 1,
92 .out_filesz = 1,
93 .out_memsz = 1,
94 },
95 PhdrTrimSegmentTestParameters{
96 .name = "trim_data_bss",
97 .in_vaddr = 1,
98 .in_filesz = 2,
99 .in_memsz = 3,
100 .trim_start = 2,
101 .trim_end = 4,
102 .out_vaddr = 2,
103 .out_filesz = 1,
104 .out_memsz = 2,
105 }),
__anona3e001920102(const testing::TestParamInfo<PhdrTrimSegmentTestParameters>& info) 106 [](const testing::TestParamInfo<PhdrTrimSegmentTestParameters>& info) {
107 return info.param.name;
108 });
109