{"id":3982,"url":"https://patchwork.libcamera.org/api/patches/3982/?format=json","web_url":"https://patchwork.libcamera.org/patch/3982/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200608005442.1771-1-laurent.pinchart@ideasonboard.com>","date":"2020-06-08T00:54:42","name":"[libcamera-devel] libcamera: ipa_module: Fix valgrind assertion failure","commit_ref":"96fab38e02792a109c0d35ca2154e95a7b4c8fcb","pull_url":null,"state":"accepted","archived":false,"hash":"2eb48dc559045057ed33775447fbe9090dfb5dc4","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/3982/mbox/","series":[{"id":969,"url":"https://patchwork.libcamera.org/api/series/969/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=969","date":"2020-06-08T00:54:42","name":"[libcamera-devel] libcamera: ipa_module: Fix valgrind assertion failure","version":1,"mbox":"https://patchwork.libcamera.org/series/969/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/3982/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3982/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A55C56002E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  8 Jun 2020 02:55:06 +0200 (CEST)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 1EE772C9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  8 Jun 2020 02:55:06 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"jTvCwPL1\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1591577706;\n\tbh=Hdanr5Uk8mo+xaM/e+YgKFzsLPbznNo94NY1WTD375w=;\n\th=From:To:Subject:Date:From;\n\tb=jTvCwPL1phaOjA+y5C10iDDb8vyG8gQyxWBPzQ5Tx8Qn5ZP0na9ZwTvpEgsxD3cu9\n\tKWvEvXroFmH1VAXtBbT8OADXKzbTCpK6e9QaLmd6w0NFw/7gCJ5ZWtaPBidk4hPT0n\n\tilpSpAdsMSfQQ78u9QnjVSV1zgv63DdUqayofY1g=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon,  8 Jun 2020 03:54:42 +0300","Message-Id":"<20200608005442.1771-1-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH] libcamera: ipa_module: Fix valgrind\n\tassertion failure","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Mon, 08 Jun 2020 00:55:07 -0000"},"content":"As the ELF parsing code uses non-const pointers to the ELF mapping, we\nhave to map the module in private read-write mode. This causes issues\nwith valgrind, due to the IPA manager mapping the module in shared\nread-only mode and valgrind having trouble loading debugging symbols\nlater at dlopen time due to conflicting mappings.\n\nThis is likely a bug in valgrind (reported as [1]), but we can easily\nwork around it by using shared read-only mappings only. As such a\nmapping shouldn't be less efficient than private read-write mappings,\nswitch the mapping type. This requires modifying the ELF parsing\nfunctions to operate on const memory, which is a good idea anyway as\nthey're not supposed to modify the ELF file.\n\n[1] https://bugs.kde.org/show_bug.cgi?id=422601\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/ipa_module.cpp | 42 ++++++++++++++++++++----------------\n 1 file changed, 23 insertions(+), 19 deletions(-)","diff":"diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp\nindex 72e357ec97ca..de512a7f3a0d 100644\n--- a/src/libcamera/ipa_module.cpp\n+++ b/src/libcamera/ipa_module.cpp\n@@ -45,26 +45,27 @@ LOG_DEFINE_CATEGORY(IPAModule)\n namespace {\n \n template<typename T>\n-typename std::remove_extent_t<T> *elfPointer(Span<uint8_t> elf, off_t offset,\n-\t\t\t\t\t     size_t objSize)\n+typename std::remove_extent_t<T> *elfPointer(Span<const uint8_t> elf,\n+\t\t\t\t\t     off_t offset, size_t objSize)\n {\n \tsize_t size = offset + objSize;\n \tif (size > elf.size() || size < objSize)\n \t\treturn nullptr;\n \n \treturn reinterpret_cast<typename std::remove_extent_t<T> *>\n-\t\t(reinterpret_cast<char *>(elf.data()) + offset);\n+\t\t(reinterpret_cast<const char *>(elf.data()) + offset);\n }\n \n template<typename T>\n-typename std::remove_extent_t<T> *elfPointer(Span<uint8_t> elf, off_t offset)\n+typename std::remove_extent_t<T> *elfPointer(Span<const uint8_t> elf,\n+\t\t\t\t\t     off_t offset)\n {\n \treturn elfPointer<T>(elf, offset, sizeof(T));\n }\n \n-int elfVerifyIdent(Span<uint8_t> elf)\n+int elfVerifyIdent(Span<const uint8_t> elf)\n {\n-\tchar *e_ident = elfPointer<char[EI_NIDENT]>(elf, 0);\n+\tconst char *e_ident = elfPointer<const char[EI_NIDENT]>(elf, 0);\n \tif (!e_ident)\n \t\treturn -ENOEXEC;\n \n@@ -88,14 +89,15 @@ int elfVerifyIdent(Span<uint8_t> elf)\n \treturn 0;\n }\n \n-ElfW(Shdr) *elfSection(Span<uint8_t> elf, ElfW(Ehdr) *eHdr, ElfW(Half) idx)\n+const ElfW(Shdr) *elfSection(Span<const uint8_t> elf, const ElfW(Ehdr) *eHdr,\n+\t\t\t     ElfW(Half) idx)\n {\n \tif (idx >= eHdr->e_shnum)\n \t\treturn nullptr;\n \n \toff_t offset = eHdr->e_shoff + idx *\n \t\t\t\t       static_cast<uint32_t>(eHdr->e_shentsize);\n-\treturn elfPointer<ElfW(Shdr)>(elf, offset);\n+\treturn elfPointer<const ElfW(Shdr)>(elf, offset);\n }\n \n /**\n@@ -106,26 +108,26 @@ ElfW(Shdr) *elfSection(Span<uint8_t> elf, ElfW(Ehdr) *eHdr, ElfW(Half) idx)\n  * \\return The memory region storing the symbol on success, or an empty span\n  * otherwise\n  */\n-Span<uint8_t> elfLoadSymbol(Span<uint8_t> elf, const char *symbol)\n+Span<const uint8_t> elfLoadSymbol(Span<const uint8_t> elf, const char *symbol)\n {\n-\tElfW(Ehdr) *eHdr = elfPointer<ElfW(Ehdr)>(elf, 0);\n+\tconst ElfW(Ehdr) *eHdr = elfPointer<const ElfW(Ehdr)>(elf, 0);\n \tif (!eHdr)\n \t\treturn {};\n \n-\tElfW(Shdr) *sHdr = elfSection(elf, eHdr, eHdr->e_shstrndx);\n+\tconst ElfW(Shdr) *sHdr = elfSection(elf, eHdr, eHdr->e_shstrndx);\n \tif (!sHdr)\n \t\treturn {};\n \toff_t shnameoff = sHdr->sh_offset;\n \n \t/* Locate .dynsym section header. */\n-\tElfW(Shdr) *dynsym = nullptr;\n+\tconst ElfW(Shdr) *dynsym = nullptr;\n \tfor (unsigned int i = 0; i < eHdr->e_shnum; i++) {\n \t\tsHdr = elfSection(elf, eHdr, i);\n \t\tif (!sHdr)\n \t\t\treturn {};\n \n \t\toff_t offset = shnameoff + sHdr->sh_name;\n-\t\tchar *name = elfPointer<char[8]>(elf, offset);\n+\t\tconst char *name = elfPointer<const char[8]>(elf, offset);\n \t\tif (!name)\n \t\t\treturn {};\n \n@@ -146,16 +148,17 @@ Span<uint8_t> elfLoadSymbol(Span<uint8_t> elf, const char *symbol)\n \toff_t dynsym_nameoff = sHdr->sh_offset;\n \n \t/* Locate symbol in the .dynsym section. */\n-\tElfW(Sym) *targetSymbol = nullptr;\n+\tconst ElfW(Sym) *targetSymbol = nullptr;\n \tunsigned int dynsym_num = dynsym->sh_size / dynsym->sh_entsize;\n \tfor (unsigned int i = 0; i < dynsym_num; i++) {\n \t\toff_t offset = dynsym->sh_offset + dynsym->sh_entsize * i;\n-\t\tElfW(Sym) *sym = elfPointer<ElfW(Sym)>(elf, offset);\n+\t\tconst ElfW(Sym) *sym = elfPointer<const ElfW(Sym)>(elf, offset);\n \t\tif (!sym)\n \t\t\treturn {};\n \n \t\toffset = dynsym_nameoff + sym->st_name;\n-\t\tchar *name = elfPointer<char>(elf, offset, strlen(symbol) + 1);\n+\t\tconst char *name = elfPointer<const char>(elf, offset,\n+\t\t\t\t\t\t\t  strlen(symbol) + 1);\n \t\tif (!name)\n \t\t\treturn {};\n \n@@ -176,7 +179,8 @@ Span<uint8_t> elfLoadSymbol(Span<uint8_t> elf, const char *symbol)\n \tif (!sHdr)\n \t\treturn {};\n \toff_t offset = sHdr->sh_offset + (targetSymbol->st_value - sHdr->sh_addr);\n-\tuint8_t *data = elfPointer<uint8_t>(elf, offset, targetSymbol->st_size);\n+\tconst uint8_t *data = elfPointer<const uint8_t>(elf, offset,\n+\t\t\t\t\t\t\ttargetSymbol->st_size);\n \tif (!data)\n \t\treturn {};\n \n@@ -277,14 +281,14 @@ int IPAModule::loadIPAModuleInfo()\n \t\treturn file.error();\n \t}\n \n-\tSpan<uint8_t> data = file.map(0, -1, File::MapPrivate);\n+\tSpan<const uint8_t> data = file.map();\n \tint ret = elfVerifyIdent(data);\n \tif (ret) {\n \t\tLOG(IPAModule, Error) << \"IPA module is not an ELF file\";\n \t\treturn ret;\n \t}\n \n-\tSpan<uint8_t> info = elfLoadSymbol(data, \"ipaModuleInfo\");\n+\tSpan<const uint8_t> info = elfLoadSymbol(data, \"ipaModuleInfo\");\n \tif (info.size() != sizeof(info_)) {\n \t\tLOG(IPAModule, Error) << \"IPA module has no valid info\";\n \t\treturn -EINVAL;\n","prefixes":["libcamera-devel"]}