[libcamera-devel,v2] ipa: raspberrypi: Fix crash under LTO
diff mbox series

Message ID 20230310113911.30842-1-naush@raspberrypi.com
State Accepted
Commit afc5ea57b496c633a1a16c67cf132df6c5ed9b46
Headers show
Series
  • [libcamera-devel,v2] ipa: raspberrypi: Fix crash under LTO
Related show

Commit Message

Naushir Patuck March 10, 2023, 11:39 a.m. UTC
From: Dave Jones <dave.jones@canonical.com>

When compiled with LTO (the default on Ubuntu), the global static
objects camHelpers and algorithms cause a crash in raspberrypi_ipa_proxy
at runtime as they're not allocated by the time the registration
routines execute.

This is a fairly crude fix which just converts the global static objects
into local static objects inside an equivalently named function.

Signed-off-by: Dave Jones <dave.jones@canonical.com>
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Tested-by: Naushir Patuck <naush@raspberrypi.com>
---
 src/ipa/raspberrypi/cam_helper.cpp           | 14 +++++++++++---
 src/ipa/raspberrypi/controller/algorithm.cpp | 15 ++++++++++++---
 2 files changed, 23 insertions(+), 6 deletions(-)

Comments

Laurent Pinchart March 10, 2023, 11:41 a.m. UTC | #1
Hi Naush and Dave,

Thank you for the patch.

On Fri, Mar 10, 2023 at 11:39:11AM +0000, Naushir Patuck via libcamera-devel wrote:
> From: Dave Jones <dave.jones@canonical.com>
> 
> When compiled with LTO (the default on Ubuntu), the global static
> objects camHelpers and algorithms cause a crash in raspberrypi_ipa_proxy
> at runtime as they're not allocated by the time the registration
> routines execute.
> 
> This is a fairly crude fix which just converts the global static objects
> into local static objects inside an equivalently named function.
> 
> Signed-off-by: Dave Jones <dave.jones@canonical.com>
> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
> Tested-by: Naushir Patuck <naush@raspberrypi.com>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  src/ipa/raspberrypi/cam_helper.cpp           | 14 +++++++++++---
>  src/ipa/raspberrypi/controller/algorithm.cpp | 15 ++++++++++++---
>  2 files changed, 23 insertions(+), 6 deletions(-)
> 
> diff --git a/src/ipa/raspberrypi/cam_helper.cpp b/src/ipa/raspberrypi/cam_helper.cpp
> index d90ac1deda47..ddd5e9a4fef2 100644
> --- a/src/ipa/raspberrypi/cam_helper.cpp
> +++ b/src/ipa/raspberrypi/cam_helper.cpp
> @@ -25,7 +25,15 @@ namespace libcamera {
>  LOG_DECLARE_CATEGORY(IPARPI)
>  }
>  
> -static std::map<std::string, CamHelperCreateFunc> camHelpers;
> +namespace {
> +
> +std::map<std::string, CamHelperCreateFunc> &camHelpers()
> +{
> +	static std::map<std::string, CamHelperCreateFunc> helpers;
> +	return helpers;
> +}
> +
> +} /* namespace */
>  
>  CamHelper *CamHelper::create(std::string const &camName)
>  {
> @@ -33,7 +41,7 @@ CamHelper *CamHelper::create(std::string const &camName)
>  	 * CamHelpers get registered by static RegisterCamHelper
>  	 * initialisers.
>  	 */
> -	for (auto &p : camHelpers) {
> +	for (auto &p : camHelpers()) {
>  		if (camName.find(p.first) != std::string::npos)
>  			return p.second();
>  	}
> @@ -253,5 +261,5 @@ void CamHelper::populateMetadata([[maybe_unused]] const MdParser::RegisterMap &r
>  RegisterCamHelper::RegisterCamHelper(char const *camName,
>  				     CamHelperCreateFunc createFunc)
>  {
> -	camHelpers[std::string(camName)] = createFunc;
> +	camHelpers()[std::string(camName)] = createFunc;
>  }
> diff --git a/src/ipa/raspberrypi/controller/algorithm.cpp b/src/ipa/raspberrypi/controller/algorithm.cpp
> index 6d91ee292bd1..a957fde520c2 100644
> --- a/src/ipa/raspberrypi/controller/algorithm.cpp
> +++ b/src/ipa/raspberrypi/controller/algorithm.cpp
> @@ -34,14 +34,23 @@ void Algorithm::process([[maybe_unused]] StatisticsPtr &stats,
>  
>  /* For registering algorithms with the system: */
>  
> -static std::map<std::string, AlgoCreateFunc> algorithms;
> -std::map<std::string, AlgoCreateFunc> const &RPiController::getAlgorithms()
> +namespace {
> +
> +std::map<std::string, AlgoCreateFunc> &algorithms()
>  {
> +	static std::map<std::string, AlgoCreateFunc> algorithms;
>  	return algorithms;
>  }
>  
> +} /* namespace */
> +
> +std::map<std::string, AlgoCreateFunc> const &RPiController::getAlgorithms()
> +{
> +	return algorithms();
> +}
> +
>  RegisterAlgorithm::RegisterAlgorithm(char const *name,
>  				     AlgoCreateFunc createFunc)
>  {
> -	algorithms[std::string(name)] = createFunc;
> +	algorithms()[std::string(name)] = createFunc;
>  }

Patch
diff mbox series

diff --git a/src/ipa/raspberrypi/cam_helper.cpp b/src/ipa/raspberrypi/cam_helper.cpp
index d90ac1deda47..ddd5e9a4fef2 100644
--- a/src/ipa/raspberrypi/cam_helper.cpp
+++ b/src/ipa/raspberrypi/cam_helper.cpp
@@ -25,7 +25,15 @@  namespace libcamera {
 LOG_DECLARE_CATEGORY(IPARPI)
 }
 
-static std::map<std::string, CamHelperCreateFunc> camHelpers;
+namespace {
+
+std::map<std::string, CamHelperCreateFunc> &camHelpers()
+{
+	static std::map<std::string, CamHelperCreateFunc> helpers;
+	return helpers;
+}
+
+} /* namespace */
 
 CamHelper *CamHelper::create(std::string const &camName)
 {
@@ -33,7 +41,7 @@  CamHelper *CamHelper::create(std::string const &camName)
 	 * CamHelpers get registered by static RegisterCamHelper
 	 * initialisers.
 	 */
-	for (auto &p : camHelpers) {
+	for (auto &p : camHelpers()) {
 		if (camName.find(p.first) != std::string::npos)
 			return p.second();
 	}
@@ -253,5 +261,5 @@  void CamHelper::populateMetadata([[maybe_unused]] const MdParser::RegisterMap &r
 RegisterCamHelper::RegisterCamHelper(char const *camName,
 				     CamHelperCreateFunc createFunc)
 {
-	camHelpers[std::string(camName)] = createFunc;
+	camHelpers()[std::string(camName)] = createFunc;
 }
diff --git a/src/ipa/raspberrypi/controller/algorithm.cpp b/src/ipa/raspberrypi/controller/algorithm.cpp
index 6d91ee292bd1..a957fde520c2 100644
--- a/src/ipa/raspberrypi/controller/algorithm.cpp
+++ b/src/ipa/raspberrypi/controller/algorithm.cpp
@@ -34,14 +34,23 @@  void Algorithm::process([[maybe_unused]] StatisticsPtr &stats,
 
 /* For registering algorithms with the system: */
 
-static std::map<std::string, AlgoCreateFunc> algorithms;
-std::map<std::string, AlgoCreateFunc> const &RPiController::getAlgorithms()
+namespace {
+
+std::map<std::string, AlgoCreateFunc> &algorithms()
 {
+	static std::map<std::string, AlgoCreateFunc> algorithms;
 	return algorithms;
 }
 
+} /* namespace */
+
+std::map<std::string, AlgoCreateFunc> const &RPiController::getAlgorithms()
+{
+	return algorithms();
+}
+
 RegisterAlgorithm::RegisterAlgorithm(char const *name,
 				     AlgoCreateFunc createFunc)
 {
-	algorithms[std::string(name)] = createFunc;
+	algorithms()[std::string(name)] = createFunc;
 }