Coverage Report

Created: 2026-03-11 11:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/common/elf.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/Elf.cpp
19
// and modified by Doris
20
21
#if defined(__ELF__) && !defined(__FreeBSD__)
22
23
#include "common/elf.h"
24
25
#include <fcntl.h>
26
#include <fmt/format.h>
27
#include <sys/mman.h>
28
#include <unistd.h>
29
30
#include <cstring>
31
#include <system_error>
32
33
#include "common/logging.h"
34
#include "common/macros.h"
35
#include "util/unaligned.h"
36
37
namespace doris {
38
39
14
Elf::Elf(const std::string& path) {
40
14
    _file = path;
41
14
    std::error_code ec;
42
14
    elf_size = std::filesystem::file_size(_file, ec);
43
14
    if (ec) {
44
0
        LOG(FATAL) << fmt::format("failed to get file size {}: ({}), {}", _file.native(),
45
0
                                  ec.value(), ec.message());
46
0
    }
47
    /// Check if it's an elf.
48
14
    if (elf_size < sizeof(ElfEhdr)) {
49
0
        LOG(FATAL) << fmt::format("The size of supposedly ELF file '{}' is too small", path);
50
0
    }
51
14
    RETRY_ON_EINTR(_fd, open(_file.c_str(), O_RDONLY));
52
14
    if (_fd < 0) {
53
0
        LOG(FATAL) << fmt::format("failed to open {}", _file.native());
54
0
    }
55
14
    mapped = static_cast<char*>(mmap(nullptr, elf_size, PROT_READ, MAP_SHARED, _fd, 0));
56
14
    if (MAP_FAILED == mapped) {
57
0
        LOG(FATAL) << fmt::format("MMappedFileDescriptor: Cannot mmap {}, read from {}.", elf_size,
58
0
                                  path);
59
0
    }
60
61
14
    header = reinterpret_cast<const ElfEhdr*>(mapped);
62
63
14
    if (memcmp(header->e_ident,
64
14
               "\x7F"
65
14
               "ELF",
66
14
               4) != 0) {
67
0
        LOG(FATAL) << fmt::format("The file '{}' is not ELF according to magic", path);
68
0
    }
69
70
    /// Get section header.
71
14
    ElfOff section_header_offset = header->e_shoff;
72
14
    uint16_t section_header_num_entries = header->e_shnum;
73
74
14
    if (!section_header_offset || !section_header_num_entries ||
75
14
        section_header_offset + section_header_num_entries * sizeof(ElfShdr) > elf_size) {
76
0
        LOG(FATAL) << fmt::format(
77
0
                "The ELF '{}' is truncated (section header points after end of file)", path);
78
0
    }
79
80
14
    section_headers = reinterpret_cast<const ElfShdr*>(mapped + section_header_offset);
81
82
    /// The string table with section names.
83
606
    auto section_names_strtab = findSection([&](const Section& section, size_t idx) {
84
606
        return section.header.sh_type == SHT_STRTAB && header->e_shstrndx == idx;
85
606
    });
86
87
14
    if (!section_names_strtab) {
88
0
        LOG(FATAL) << fmt::format("The ELF '{}' doesn't have string table with section names",
89
0
                                  path);
90
0
    }
91
92
14
    ElfOff section_names_offset = section_names_strtab->header.sh_offset;
93
14
    if (section_names_offset >= elf_size) {
94
0
        LOG(FATAL) << fmt::format(
95
0
                "The ELF '{}' is truncated (section names string table points after end of file)",
96
0
                path);
97
0
    }
98
14
    section_names = reinterpret_cast<const char*>(mapped + section_names_offset);
99
100
    /// Get program headers
101
102
14
    ElfOff program_header_offset = header->e_phoff;
103
14
    uint16_t program_header_num_entries = header->e_phnum;
104
105
14
    if (!program_header_offset || !program_header_num_entries ||
106
14
        program_header_offset + program_header_num_entries * sizeof(ElfPhdr) > elf_size) {
107
0
        LOG(FATAL) << fmt::format(
108
0
                "The ELF '{}' is truncated (program header points after end of file)", path);
109
0
    }
110
14
    program_headers = reinterpret_cast<const ElfPhdr*>(mapped + program_header_offset);
111
14
}
112
113
14
Elf::~Elf() {
114
14
    if (mapped) {
115
14
        munmap(static_cast<void*>(mapped), elf_size);
116
14
    }
117
14
    if (_fd > 0) {
118
14
        int res = ::close(_fd);
119
14
        if (-1 == res) {
120
0
            LOG(WARNING) << fmt::format("failed to close {}", _file.native());
121
0
        }
122
14
        _fd = -1;
123
14
    }
124
14
}
125
126
360k
Elf::Section::Section(const ElfShdr& header_, const Elf& elf_) : header(header_), elf(elf_) {}
127
128
5.90k
bool Elf::iterateSections(std::function<bool(const Section& section, size_t idx)>&& pred) const {
129
363k
    for (size_t idx = 0; idx < header->e_shnum; ++idx) {
130
360k
        Section section(section_headers[idx], *this);
131
132
        /// Sections spans after end of file.
133
360k
        if (section.header.sh_offset + section.header.sh_size > elf_size) {
134
2
            continue;
135
2
        }
136
137
360k
        if (pred(section, idx)) {
138
2.96k
            return true;
139
2.96k
        }
140
360k
    }
141
2.94k
    return false;
142
5.90k
}
143
144
std::optional<Elf::Section> Elf::findSection(
145
5.87k
        std::function<bool(const Section& section, size_t idx)>&& pred) const {
146
5.87k
    std::optional<Elf::Section> result;
147
148
359k
    iterateSections([&](const Section& section, size_t idx) {
149
359k
        if (pred(section, idx)) {
150
2.94k
            result.emplace(section);
151
2.94k
            return true;
152
2.94k
        }
153
356k
        return false;
154
359k
    });
155
156
5.87k
    return result;
157
5.87k
}
158
159
5.86k
std::optional<Elf::Section> Elf::findSectionByName(const char* name) const {
160
5.86k
    return findSection(
161
359k
            [&](const Section& section, size_t) { return 0 == strcmp(name, section.name()); });
162
5.86k
}
163
164
14
std::string Elf::getBuildID() const {
165
    /// Section headers are the first choice for a debuginfo file
166
282
    if (std::string build_id; iterateSections([&build_id](const Section& section, size_t) {
167
282
            if (section.header.sh_type == SHT_NOTE) {
168
14
                build_id = Elf::getBuildID(section.begin(), section.size());
169
14
                if (!build_id.empty()) {
170
10
                    return true;
171
10
                }
172
14
            }
173
272
            return false;
174
282
        })) {
175
10
        return build_id;
176
10
    }
177
178
    /// fallback to PHDR
179
48
    for (size_t idx = 0; idx < header->e_phnum; ++idx) {
180
48
        const ElfPhdr& phdr = program_headers[idx];
181
182
48
        if (phdr.p_type == PT_NOTE) {
183
4
            return getBuildID(mapped + phdr.p_offset, phdr.p_filesz);
184
4
        }
185
48
    }
186
187
0
    return {};
188
4
}
189
190
#if defined(OS_SUNOS)
191
std::string Elf::getBuildID(const char* nhdr_pos, size_t size) {
192
    return {};
193
}
194
#else
195
29
std::string Elf::getBuildID(const char* nhdr_pos, size_t size) {
196
29
    const char* nhdr_end = nhdr_pos + size;
197
198
40
    while (nhdr_pos < nhdr_end) {
199
30
        ElfNhdr nhdr = unaligned_load<ElfNhdr>(nhdr_pos);
200
201
30
        nhdr_pos += sizeof(ElfNhdr) + nhdr.n_namesz;
202
30
        if (nhdr.n_type == NT_GNU_BUILD_ID) {
203
19
            const char* build_id = nhdr_pos;
204
19
            return {build_id, nhdr.n_descsz};
205
19
        }
206
11
        nhdr_pos += nhdr.n_descsz;
207
11
    }
208
209
10
    return {};
210
29
}
211
#endif // OS_SUNOS
212
213
0
std::string Elf::getStoredBinaryHash() const {
214
0
    if (auto section = findSectionByName(".clickhouse.hash")) {
215
0
        return {section->begin(), section->end()};
216
0
    } else {
217
0
        return {};
218
0
    }
219
0
}
220
221
359k
const char* Elf::Section::name() const {
222
359k
    if (!elf.section_names) {
223
0
        LOG(FATAL) << fmt::format("Section names are not initialized");
224
0
    }
225
226
    /// TODO buffer overflow is possible, we may need to check strlen.
227
359k
    return elf.section_names + header.sh_name;
228
359k
}
229
230
2.97k
const char* Elf::Section::begin() const {
231
2.97k
    return elf.mapped + header.sh_offset;
232
2.97k
}
233
234
10
const char* Elf::Section::end() const {
235
10
    return begin() + size();
236
10
}
237
238
2.95k
size_t Elf::Section::size() const {
239
2.95k
    return header.sh_size;
240
2.95k
}
241
242
} // namespace doris
243
244
#endif