{"id":3395,"url":"https://patchwork.libcamera.org/api/1.1/patches/3395/?format=json","web_url":"https://patchwork.libcamera.org/patch/3395/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20200404015624.30440-6-laurent.pinchart@ideasonboard.com>","date":"2020-04-04T01:56:18","name":"[libcamera-devel,05/11] libcamera: ipa_module: Simplify error handling in loadIPAModuleInfo()","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"d4755a3a338f039f26559c33e98084c8507ee4af","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/3395/mbox/","series":[{"id":797,"url":"https://patchwork.libcamera.org/api/1.1/series/797/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=797","date":"2020-04-04T01:56:13","name":"Sign IPA modules instead of checking their advertised license","version":1,"mbox":"https://patchwork.libcamera.org/series/797/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/3395/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3395/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 0C14C62D90\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat,  4 Apr 2020 03:56:40 +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 93B5772E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat,  4 Apr 2020 03:56:39 +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=\"ukq98Cw6\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1585965399;\n\tbh=ifW0ACdZvc4+2qIx05JRtLgeNczXf7GhnLBEeopoaoI=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=ukq98Cw6vfmICoJe6j4W6uzVQeAYND26BgWzZFivvPNjuyZ3u8fC5zVg33Or2E5Kn\n\tuCh0KZjfG5qilah41y/COgW28HKXRhb7mEVhWmyFnbcvE2Q8Nom5qXrLX48xeTCbkD\n\t5U+yueJAi8oAQxtoGFhbNn6fc0j9swlTw4CIDtws=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sat,  4 Apr 2020 04:56:18 +0300","Message-Id":"<20200404015624.30440-6-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.24.1","In-Reply-To":"<20200404015624.30440-1-laurent.pinchart@ideasonboard.com>","References":"<20200404015624.30440-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 05/11] libcamera: ipa_module: Simplify\n\terror handling in loadIPAModuleInfo()","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":"Sat, 04 Apr 2020 01:56:40 -0000"},"content":"Create a helper class to handle cleanup of the mapped file to simplify\nerror handling in loadIPAModuleInfo().\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/ipa_module.cpp | 58 +++++++++++++-----------------------\n 1 file changed, 21 insertions(+), 37 deletions(-)","diff":"diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp\nindex a01d0757ff8f..d1c411e14ba9 100644\n--- a/src/libcamera/ipa_module.cpp\n+++ b/src/libcamera/ipa_module.cpp\n@@ -21,6 +21,7 @@\n #include <tuple>\n #include <unistd.h>\n \n+#include \"file.h\"\n #include \"log.h\"\n #include \"pipeline_handler.h\"\n #include \"utils.h\"\n@@ -283,55 +284,38 @@ IPAModule::~IPAModule()\n \n int IPAModule::loadIPAModuleInfo()\n {\n-\tint fd = open(libPath_.c_str(), O_RDONLY);\n-\tif (fd < 0) {\n-\t\tint ret = -errno;\n+\tFile file{ libPath_ };\n+\tif (!file.open(File::ReadOnly)) {\n \t\tLOG(IPAModule, Error) << \"Failed to open IPA library: \"\n-\t\t\t\t      << strerror(-ret);\n-\t\treturn ret;\n+\t\t\t\t      << strerror(-file.error());\n+\t\treturn file.error();\n \t}\n \n-\tvoid *data = nullptr;\n-\tsize_t dataSize;\n-\tvoid *map;\n-\tsize_t soSize;\n-\tstruct stat st;\n-\tint ret = fstat(fd, &st);\n-\tif (ret < 0)\n-\t\tgoto close;\n-\tsoSize = st.st_size;\n-\tmap = mmap(NULL, soSize, PROT_READ, MAP_PRIVATE, fd, 0);\n-\tif (map == MAP_FAILED) {\n-\t\tret = -errno;\n-\t\tgoto close;\n+\tSpan<uint8_t> data = file.map(0, -1, File::MapPrivate);\n+\tint ret = elfVerifyIdent(data.data(), data.size());\n+\tif (ret) {\n+\t\tLOG(IPAModule, Error) << \"IPA module is not an ELF file\";\n+\t\treturn ret;\n \t}\n \n-\tret = elfVerifyIdent(map, soSize);\n-\tif (ret)\n-\t\tgoto unmap;\n+\tvoid *info = nullptr;\n+\tsize_t infoSize;\n \n-\tstd::tie(data, dataSize) = elfLoadSymbol(map, soSize, \"ipaModuleInfo\");\n-\n-\tif (data && dataSize == sizeof(info_))\n-\t\tmemcpy(&info_, data, dataSize);\n+\tstd::tie(info, infoSize) = elfLoadSymbol(data.data(), data.size(),\n+\t\t\t\t\t\t \"ipaModuleInfo\");\n+\tif (!info || infoSize != sizeof(info_)) {\n+\t\tLOG(IPAModule, Error) << \"IPA module has no valid info\";\n+\t\treturn -EINVAL;\n+\t}\n \n-\tif (!data)\n-\t\tgoto unmap;\n+\tmemcpy(&info_, info, infoSize);\n \n \tif (info_.moduleAPIVersion != IPA_MODULE_API_VERSION) {\n \t\tLOG(IPAModule, Error) << \"IPA module API version mismatch\";\n-\t\tret = -EINVAL;\n+\t\treturn -EINVAL;\n \t}\n \n-unmap:\n-\tmunmap(map, soSize);\n-close:\n-\tif (ret || !data)\n-\t\tLOG(IPAModule, Error)\n-\t\t\t<< \"Error loading IPA module info for \" << libPath_;\n-\n-\tclose(fd);\n-\treturn ret;\n+\treturn 0;\n }\n \n /**\n","prefixes":["libcamera-devel","05/11"]}