[libcamera-devel] [PATCH] ipa: raspberrypi: Fix crash under LTO

Naushir Patuck naush at raspberrypi.com
Fri Mar 10 10:51:26 CET 2023


From: Dave Jones <dave.jones at 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 at canonical.com>
Signed-off-by: Naushir Patuck <naush at raspberrypi.com>
Tested-by: Naushir Patuck <naush at raspberrypi.com>
---
 src/ipa/raspberrypi/cam_helper.cpp           | 16 +++++++++++++---
 src/ipa/raspberrypi/controller/algorithm.cpp | 19 +++++++++++++++----
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/src/ipa/raspberrypi/cam_helper.cpp b/src/ipa/raspberrypi/cam_helper.cpp
index d90ac1deda47..efe453dc6135 100644
--- a/src/ipa/raspberrypi/cam_helper.cpp
+++ b/src/ipa/raspberrypi/cam_helper.cpp
@@ -9,6 +9,7 @@
 
 #include <limits>
 #include <map>
+#include <memory>
 #include <string.h>
 
 #include "libcamera/internal/v4l2_videodevice.h"
@@ -25,7 +26,16 @@ namespace libcamera {
 LOG_DECLARE_CATEGORY(IPARPI)
 }
 
-static std::map<std::string, CamHelperCreateFunc> camHelpers;
+namespace {
+
+std::map<std::string, CamHelperCreateFunc> &camHelpers()
+{
+	static std::unique_ptr<std::map<std::string, CamHelperCreateFunc>> obj =
+		std::make_unique<std::map<std::string, CamHelperCreateFunc>>();
+	return *obj;
+}
+
+} /* namespace */
 
 CamHelper *CamHelper::create(std::string const &camName)
 {
@@ -33,7 +43,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 +263,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..2650f98bee5b 100644
--- a/src/ipa/raspberrypi/controller/algorithm.cpp
+++ b/src/ipa/raspberrypi/controller/algorithm.cpp
@@ -4,9 +4,10 @@
  *
  * algorithm.cpp - ISP control algorithms
  */
-
 #include "algorithm.h"
 
+#include <memory>
+
 using namespace RPiController;
 
 int Algorithm::read([[maybe_unused]] const libcamera::YamlObject &params)
@@ -34,14 +35,24 @@ void Algorithm::process([[maybe_unused]] StatisticsPtr &stats,
 
 /* For registering algorithms with the system: */
 
-static std::map<std::string, AlgoCreateFunc> algorithms;
+namespace {
+
+static std::map<std::string, AlgoCreateFunc> &algorithms()
+{
+	static std::unique_ptr<std::map<std::string, AlgoCreateFunc>> obj =
+		std::make_unique<std::map<std::string, AlgoCreateFunc>>();
+	return *obj;
+}
+
+} /* namespace */
+
 std::map<std::string, AlgoCreateFunc> const &RPiController::getAlgorithms()
 {
-	return algorithms;
+	return algorithms();
 }
 
 RegisterAlgorithm::RegisterAlgorithm(char const *name,
 				     AlgoCreateFunc createFunc)
 {
-	algorithms[std::string(name)] = createFunc;
+	algorithms()[std::string(name)] = createFunc;
 }
-- 
2.34.1



More information about the libcamera-devel mailing list