[{"id":26624,"web_url":"https://patchwork.libcamera.org/comment/26624/","msgid":"<20230310114159.GG5342@pendragon.ideasonboard.com>","date":"2023-03-10T11:41:59","subject":"Re: [libcamera-devel] [PATCH v2] ipa: raspberrypi: Fix crash under\n\tLTO","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Naush and Dave,\n\nThank you for the patch.\n\nOn Fri, Mar 10, 2023 at 11:39:11AM +0000, Naushir Patuck via libcamera-devel wrote:\n> From: Dave Jones <dave.jones@canonical.com>\n> \n> When compiled with LTO (the default on Ubuntu), the global static\n> objects camHelpers and algorithms cause a crash in raspberrypi_ipa_proxy\n> at runtime as they're not allocated by the time the registration\n> routines execute.\n> \n> This is a fairly crude fix which just converts the global static objects\n> into local static objects inside an equivalently named function.\n> \n> Signed-off-by: Dave Jones <dave.jones@canonical.com>\n> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>\n> Tested-by: Naushir Patuck <naush@raspberrypi.com>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  src/ipa/raspberrypi/cam_helper.cpp           | 14 +++++++++++---\n>  src/ipa/raspberrypi/controller/algorithm.cpp | 15 ++++++++++++---\n>  2 files changed, 23 insertions(+), 6 deletions(-)\n> \n> diff --git a/src/ipa/raspberrypi/cam_helper.cpp b/src/ipa/raspberrypi/cam_helper.cpp\n> index d90ac1deda47..ddd5e9a4fef2 100644\n> --- a/src/ipa/raspberrypi/cam_helper.cpp\n> +++ b/src/ipa/raspberrypi/cam_helper.cpp\n> @@ -25,7 +25,15 @@ namespace libcamera {\n>  LOG_DECLARE_CATEGORY(IPARPI)\n>  }\n>  \n> -static std::map<std::string, CamHelperCreateFunc> camHelpers;\n> +namespace {\n> +\n> +std::map<std::string, CamHelperCreateFunc> &camHelpers()\n> +{\n> +\tstatic std::map<std::string, CamHelperCreateFunc> helpers;\n> +\treturn helpers;\n> +}\n> +\n> +} /* namespace */\n>  \n>  CamHelper *CamHelper::create(std::string const &camName)\n>  {\n> @@ -33,7 +41,7 @@ CamHelper *CamHelper::create(std::string const &camName)\n>  \t * CamHelpers get registered by static RegisterCamHelper\n>  \t * initialisers.\n>  \t */\n> -\tfor (auto &p : camHelpers) {\n> +\tfor (auto &p : camHelpers()) {\n>  \t\tif (camName.find(p.first) != std::string::npos)\n>  \t\t\treturn p.second();\n>  \t}\n> @@ -253,5 +261,5 @@ void CamHelper::populateMetadata([[maybe_unused]] const MdParser::RegisterMap &r\n>  RegisterCamHelper::RegisterCamHelper(char const *camName,\n>  \t\t\t\t     CamHelperCreateFunc createFunc)\n>  {\n> -\tcamHelpers[std::string(camName)] = createFunc;\n> +\tcamHelpers()[std::string(camName)] = createFunc;\n>  }\n> diff --git a/src/ipa/raspberrypi/controller/algorithm.cpp b/src/ipa/raspberrypi/controller/algorithm.cpp\n> index 6d91ee292bd1..a957fde520c2 100644\n> --- a/src/ipa/raspberrypi/controller/algorithm.cpp\n> +++ b/src/ipa/raspberrypi/controller/algorithm.cpp\n> @@ -34,14 +34,23 @@ void Algorithm::process([[maybe_unused]] StatisticsPtr &stats,\n>  \n>  /* For registering algorithms with the system: */\n>  \n> -static std::map<std::string, AlgoCreateFunc> algorithms;\n> -std::map<std::string, AlgoCreateFunc> const &RPiController::getAlgorithms()\n> +namespace {\n> +\n> +std::map<std::string, AlgoCreateFunc> &algorithms()\n>  {\n> +\tstatic std::map<std::string, AlgoCreateFunc> algorithms;\n>  \treturn algorithms;\n>  }\n>  \n> +} /* namespace */\n> +\n> +std::map<std::string, AlgoCreateFunc> const &RPiController::getAlgorithms()\n> +{\n> +\treturn algorithms();\n> +}\n> +\n>  RegisterAlgorithm::RegisterAlgorithm(char const *name,\n>  \t\t\t\t     AlgoCreateFunc createFunc)\n>  {\n> -\talgorithms[std::string(name)] = createFunc;\n> +\talgorithms()[std::string(name)] = createFunc;\n>  }","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 1906CBD80A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 10 Mar 2023 11:41:58 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 719CA626CA;\n\tFri, 10 Mar 2023 12:41:57 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 445B0603B1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 10 Mar 2023 12:41:56 +0100 (CET)","from pendragon.ideasonboard.com\n\t(117.145-247-81.adsl-dyn.isp.belgacom.be [81.247.145.117])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D0B792E5;\n\tFri, 10 Mar 2023 12:41:55 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1678448517;\n\tbh=r2nucDl1tNcW1roPcvl+F6Lm/PRExn7kuseEIUuv6wI=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=H1IgIFGHnpxbz9J5IY54yvXPw+lhIGjD0jds69AjnZAIHdTmIExIoj6462GiPzYrd\n\tf5y38P1ZPjiHvIoNuSH3qOgiGsYaDggcmARbXdPTOXm7hlWMgRpal3q5kNXb2JYez8\n\tBXmBh4IBYTlZNcH8FqMXyDvPJDq9VrmWgO4PtRgtTtggtvIVzlXxAVxGkmvLZwfJFk\n\tlK5vs/FxS4Fcfj6cqfKevvMtQQAWw6FAtXlPTuj7c6q3V2M/nv2ODAmtZCIn0nmT7e\n\tipRqotH+1hb1Hzn6k3c/ZGamuE5AjvHh9+FpjRRSeEoOkxkXfFbxtAYXHWlTIdZGVF\n\tmn6pZ79cuJeQg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1678448515;\n\tbh=r2nucDl1tNcW1roPcvl+F6Lm/PRExn7kuseEIUuv6wI=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=XXmHaDDUqVcjCSYQ1m12orOYzWQUIf/zaVo6h6+xpxm+J91amKXx5y8znfsLZ1QLi\n\tFRRYRDIgDh4JnHOIxMC0l39MKD/nrrHL6n9VXFJIuEqU0GQK9oLGGvxfiemImvkwLi\n\t6eTw0HyQ+VpnHiKJN+iFVfsgGNFWjVS7Ltq2kWww="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"XXmHaDDU\"; dkim-atps=neutral","Date":"Fri, 10 Mar 2023 13:41:59 +0200","To":"Naushir Patuck <naush@raspberrypi.com>","Message-ID":"<20230310114159.GG5342@pendragon.ideasonboard.com>","References":"<20230310113911.30842-1-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20230310113911.30842-1-naush@raspberrypi.com>","Subject":"Re: [libcamera-devel] [PATCH v2] ipa: raspberrypi: Fix crash under\n\tLTO","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>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"Dave Jones <dave.jones@canonical.com>,\n\tlibcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]