summaryrefslogtreecommitdiff
path: root/test/unique-fd.cpp
blob: eb3b591fec28f19fe108e0188b9b1f6521cf50ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2021, Google Inc.
 *
 * unique-fd.cpp - UniqueFD test
 */

#include <fcntl.h>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <libcamera/base/unique_fd.h>
#include <libcamera/base/utils.h>

#include "test.h"

using namespace libcamera;
using namespace std;

class UniqueFDTest : public Test
{
protected:
	int init() override
	{
		return createFd();
	}

	int run() override
	{
		/* Test creating empty UniqueFD. */
		UniqueFD fd;

		if (fd.isValid() || fd.get() != -1) {
			std::cout << "Failed fd check (default constructor)"
				  << std::endl;
			return TestFail;
		}

		/* Test creating UniqueFD from numerical file descriptor. */
		UniqueFD fd2(fd_);
		if (!fd2.isValid() || fd2.get() != fd_) {
			std::cout << "Failed fd check (fd constructor)"
				  << std::endl;
			return TestFail;
		}

		if (!isValidFd(fd_)) {
			std::cout << "Failed fd validity (fd constructor)"
				  << std::endl;
			return TestFail;
		}

		/* Test move constructor. */
		UniqueFD fd3(std::move(fd2));
		if (!fd3.isValid() || fd3.get() != fd_) {
			std::cout << "Failed fd check (move constructor)"
				  << std::endl;
			return TestFail;
		}

		if (fd2.isValid() || fd2.get() != -1) {
			std::cout << "Failed moved fd check (move constructor)"
				  << std::endl;
			return TestFail;
		}

		if (!isValidFd(fd_)) {
			std::cout << "Failed fd validity (move constructor)"
				  << std::endl;
			return TestFail;
		}

		/* Test move assignment operator. */
		fd = std::move(fd3);
		if (!fd.isValid() || fd.get() != fd_) {
			std::cout << "Failed fd check (move assignment)"
				  << std::endl;
			return TestFail;
		}

		if (fd3.isValid() || fd3.get() != -1) {
			std::cout << "Failed moved fd check (move assignment)"
				  << std::endl;
			return TestFail;
		}

		if (!isValidFd(fd_)) {
			std::cout << "Failed fd validity (move assignment)"
				  << std::endl;
			return TestFail;
		}

		/* Test swapping. */
		fd2.swap(fd);
		if (!fd2.isValid() || fd2.get() != fd_) {
			std::cout << "Failed fd check (swap)"
				  << std::endl;
			return TestFail;
		}

		if (fd.isValid() || fd.get() != -1) {
			std::cout << "Failed swapped fd check (swap)"
				  << std::endl;
			return TestFail;
		}

		if (!isValidFd(fd_)) {
			std::cout << "Failed fd validity (swap)"
				  << std::endl;
			return TestFail;
		}

		/* Test release. */
		int numFd = fd2.release();
		if (fd2.isValid() || fd2.get() != -1) {
			std::cout << "Failed fd check (release)"
				  << std::endl;
			return TestFail;
		}

		if (numFd != fd_) {
			std::cout << "Failed released fd check (release)"
				  << std::endl;
			return TestFail;
		}

		if (!isValidFd(fd_)) {
			std::cout << "Failed fd validity (release)"
				  << std::endl;
			return TestFail;
		}

		/* Test reset assignment. */
		fd.reset(numFd);
		if (!fd.isValid() || fd.get() != fd_) {
			std::cout << "Failed fd check (reset assignment)"
				  << std::endl;
			return TestFail;
		}

		if (!isValidFd(fd_)) {
			std::cout << "Failed fd validity (reset assignment)"
				  << std::endl;
			return TestFail;
		}

		/* Test reset destruction. */
		fd.reset();
		if (fd.isValid() || fd.get() != -1) {
			std::cout << "Failed fd check (reset destruction)"
				  << std::endl;
			return TestFail;
		}

		if (isValidFd(fd_)) {
			std::cout << "Failed fd validity (reset destruction)"
				  << std::endl;
			return TestFail;
		}

		/* Test destruction. */
		if (createFd() == TestFail) {
			std::cout << "Failed to recreate test fd"
				  << std::endl;
			return TestFail;
		}

		{
			UniqueFD fd4(fd_);
		}

		if (isValidFd(fd_)) {
			std::cout << "Failed fd validity (destruction)"
				  << std::endl;
			return TestFail;
		}

		return TestPass;
	}

	void cleanup() override
	{
		if (fd_ > 0)
			close(fd_);
	}

private:
	int createFd()
	{
		fd_ = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
		if (fd_ < 0)
			return TestFail;

		/* Cache inode number of temp file. */
		struct stat s;
		if (fstat(fd_, &s))
			return TestFail;

		inodeNr_ = s.st_ino;

		return 0;
	}

	bool isValidFd(int fd)
	{
		struct stat s;
		if (fstat(fd, &s))
			return false;

		/* Check that inode number matches cached temp file. */
		return s.st_ino == inodeNr_;
	}

	int fd_;
	ino_t inodeNr_;
};

TEST_REGISTER(UniqueFDTest)
an>map, offset, soSize); if (!sHdr) return std::make_tuple(nullptr, 0); off_t shnameoff = sHdr->sh_offset; /* Locate .dynsym section header. */ SecHeader *dynsym = nullptr; for (unsigned int i = 0; i < eHdr->e_shnum; i++) { offset = eHdr->e_shoff + eHdr->e_shentsize * i; sHdr = elfPointer<SecHeader>(map, offset, soSize); if (!sHdr) return std::make_tuple(nullptr, 0); offset = shnameoff + sHdr->sh_name; char *name = elfPointer<char[8]>(map, offset, soSize); if (!name) return std::make_tuple(nullptr, 0); if (sHdr->sh_type == SHT_DYNSYM && !strcmp(name, ".dynsym")) { dynsym = sHdr; break; } } if (dynsym == nullptr) { LOG(IPAModule, Error) << "ELF has no .dynsym section"; return std::make_tuple(nullptr, 0); } offset = eHdr->e_shoff + eHdr->e_shentsize * dynsym->sh_link; sHdr = elfPointer<SecHeader>(map, offset, soSize); if (!sHdr) return std::make_tuple(nullptr, 0); off_t dynsym_nameoff = sHdr->sh_offset; /* Locate symbol in the .dynsym section. */ SymHeader *targetSymbol = nullptr; unsigned int dynsym_num = dynsym->sh_size / dynsym->sh_entsize; for (unsigned int i = 0; i < dynsym_num; i++) { offset = dynsym->sh_offset + dynsym->sh_entsize * i; SymHeader *sym = elfPointer<SymHeader>(map, offset, soSize); if (!sym) return std::make_tuple(nullptr, 0); offset = dynsym_nameoff + sym->st_name; char *name = elfPointer<char>(map, offset, soSize, strlen(symbol) + 1); if (!name) return std::make_tuple(nullptr, 0); if (!strcmp(name, symbol) && sym->st_info & STB_GLOBAL) { targetSymbol = sym; break; } } if (targetSymbol == nullptr) { LOG(IPAModule, Error) << "Symbol " << symbol << " not found"; return std::make_tuple(nullptr, 0); } /* Locate and return data of symbol. */ if (targetSymbol->st_shndx >= eHdr->e_shnum) return std::make_tuple(nullptr, 0); offset = eHdr->e_shoff + targetSymbol->st_shndx * eHdr->e_shentsize; sHdr = elfPointer<SecHeader>(map, offset, soSize); if (!sHdr) return std::make_tuple(nullptr, 0); offset = sHdr->sh_offset + (targetSymbol->st_value - sHdr->sh_addr); char *data = elfPointer<char>(map, offset, soSize, targetSymbol->st_size); if (!data) return std::make_tuple(nullptr, 0); return std::make_tuple(data, targetSymbol->st_size); } } /* namespace */ /** * \def IPA_MODULE_API_VERSION * \brief The IPA module API version * * This version number specifies the version for the layout of * struct IPAModuleInfo. The IPA module shall use this macro to * set its moduleAPIVersion field. * * \sa IPAModuleInfo::moduleAPIVersion */ /** * \struct IPAModuleInfo * \brief Information of an IPA module * * This structure contains the information of an IPA module. It is loaded, * read, and validated before anything else is loaded from the shared object. * * \var IPAModuleInfo::moduleAPIVersion * \brief The IPA module API version that the IPA module implements * * This version number specifies the version for the layout of * struct IPAModuleInfo. The IPA module shall report here the version that * it was built for, using the macro IPA_MODULE_API_VERSION. * * \var IPAModuleInfo::pipelineVersion * \brief The pipeline handler version that the IPA module is for * * \var IPAModuleInfo::pipelineName * \brief The name of the pipeline handler that the IPA module is for * * This name is used to match a pipeline handler with the module. * * \var IPAModuleInfo::name * \brief The name of the IPA module * * \var IPAModuleInfo::license * \brief License of the IPA module * * This license is used to determine whether to force isolation of the IPA in * a separate process. If the license is "Proprietary", then the IPA will * be isolated. If the license is open-source, then the IPA will be allowed to * run without isolation if the user enables it. The license should be an * SPDX license string. The following licenses are currently available to * allow the IPA to run unisolated: * * - GPL-2.0-only * - GPL-2.0-or-later * - GPL-3.0-only * - GPL-3.0-or-later * - LGPL-2.1-only * - LGPL-2.1-or-later * - LGPL-3.0-only * - LGPL-3.0-or-later * * Any other license will cause the IPA to be run isolated. * * \todo Allow user to choose to isolate open source IPAs */ /** * \var ipaModuleInfo * \brief Information of an IPA module * * An IPA module must export a struct IPAModuleInfo of this name. */ /** * \class IPAModule * \brief Wrapper around IPA module shared object */ /** * \brief Construct an IPAModule instance * \param[in] libPath path to IPA module shared object * * Loads the IPAModuleInfo from the IPA module shared object at libPath. * The IPA module shared object file must be of the same endianness and * bitness as libcamera. * * The caller shall call the isValid() method after constructing an * IPAModule instance to verify the validity of the IPAModule. */ IPAModule::IPAModule(const std::string &libPath) : libPath_(libPath), valid_(false), loaded_(false), dlHandle_(nullptr), ipaCreate_(nullptr) { if (loadIPAModuleInfo() < 0) return; valid_ = true; } IPAModule::~IPAModule() { if (dlHandle_) dlclose(dlHandle_); } int IPAModule::loadIPAModuleInfo() { int fd = open(libPath_.c_str(), O_RDONLY); if (fd < 0) { int ret = -errno; LOG(IPAModule, Error) << "Failed to open IPA library: " << strerror(-ret); return ret; } void *data = nullptr; size_t dataSize; void *map; size_t soSize; struct stat st; int ret = fstat(fd, &st); if (ret < 0) goto close; soSize = st.st_size; map = mmap(NULL, soSize, PROT_READ, MAP_PRIVATE, fd, 0); if (map == MAP_FAILED) { ret = -errno; goto close; } ret = elfVerifyIdent(map, soSize); if (ret) goto unmap; if (sizeof(unsigned long) == 4) std::tie(data, dataSize) = elfLoadSymbol<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym> (map, soSize, "ipaModuleInfo"); else std::tie(data, dataSize) = elfLoadSymbol<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym> (map, soSize, "ipaModuleInfo"); if (data && dataSize == sizeof(info_)) memcpy(&info_, data, dataSize); if (!data) goto unmap; if (info_.moduleAPIVersion != IPA_MODULE_API_VERSION) { LOG(IPAModule, Error) << "IPA module API version mismatch"; ret = -EINVAL; } unmap: munmap(map, soSize); close: if (ret || !data) LOG(IPAModule, Error) << "Error loading IPA module info for " << libPath_; close(fd); return ret; } /** * \brief Check if the IPAModule instance is valid * * An IPAModule instance is valid if the IPA module shared object exists and * the IPA module information it contains was successfully retrieved and * validated. * * \return True if the IPAModule is valid, false otherwise */ bool IPAModule::isValid() const { return valid_; } /** * \brief Retrieve the IPA module information * * The content of the IPA module information is loaded from the module, * and is valid only if the module is valid (as returned by isValid()). * Calling this function on an invalid module is an error. * * \return the IPA module information */ const struct IPAModuleInfo &IPAModule::info() const { return info_; } /** * \brief Retrieve the IPA module path * * The IPA module path is the file name and path of the IPA module shared * object from which the IPA module was created. * * \return The IPA module path */ const std::string &IPAModule::path() const { return libPath_; } /** * \brief Load the IPA implementation factory from the shared object * * The IPA module shared object implements an IPAInterface class to be used * by pipeline handlers. This method loads the factory function from the * shared object. Later, createInstance() can be called to instantiate the * IPAInterface. * * This method only needs to be called successfully once, after which * createInstance() can be called as many times as IPAInterface instances are * needed. * * Calling this function on an invalid module (as returned by isValid()) is * an error. * * \return True if load was successful, or already loaded, and false otherwise */ bool IPAModule::load() { if (!valid_) return false; if (loaded_) return true; dlHandle_ = dlopen(libPath_.c_str(), RTLD_LAZY); if (!dlHandle_) { LOG(IPAModule, Error) << "Failed to open IPA module shared object: " << dlerror(); return false; } void *symbol = dlsym(dlHandle_, "ipaCreate"); if (!symbol) { LOG(IPAModule, Error) << "Failed to load ipaCreate() from IPA module shared object: " << dlerror(); dlclose(dlHandle_); dlHandle_ = nullptr; return false; } ipaCreate_ = reinterpret_cast<IPAIntfFactory>(symbol); loaded_ = true; return true; } /** * \brief Instantiate an IPAInterface * * After loading the IPA module with load(), this method creates an * instance of the IPA module interface. * * Calling this function on a module that has not yet been loaded, or an * invalid module (as returned by load() and isValid(), respectively) is * an error. * * \return The IPA implementation as a new IPAInterface instance on success, * or nullptr on error */ std::unique_ptr<IPAInterface> IPAModule::createInstance() { if (!valid_ || !loaded_) return nullptr; return std::unique_ptr<IPAInterface>(ipaCreate_()); } /** * \brief Verify if the IPA module maches a given pipeline handler * \param[in] pipe Pipeline handler to match with * \param[in] minVersion Minimum acceptable version of IPA module * \param[in] maxVersion Maximum acceptable version of IPA module * * This method checks if this IPA module matches the \a pipe pipeline handler, * and the input version range. * * \return True if the pipeline handler matches the IPA module, or false otherwise */ bool IPAModule::match(PipelineHandler *pipe, uint32_t minVersion, uint32_t maxVersion) const { return info_.pipelineVersion >= minVersion && info_.pipelineVersion <= maxVersion && !strcmp(info_.pipelineName, pipe->name()); } /** * \brief Verify if the IPA module is open source * * \sa IPAModuleInfo::license */ bool IPAModule::isOpenSource() const { static const char *osLicenses[] = { "GPL-2.0-only", "GPL-2.0-or-later", "GPL-3.0-only", "GPL-3.0-or-later", "LGPL-2.1-only", "LGPL-2.1-or-later", "LGPL-3.0-only", "LGPL-3.0-or-later", }; for (unsigned int i = 0; i < ARRAY_SIZE(osLicenses); i++) if (!strcmp(osLicenses[i], info_.license)) return true; return false; } } /* namespace libcamera */