From patchwork Mon Aug 29 08:44:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 17218 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 0D3D0C0DA4 for ; Mon, 29 Aug 2022 08:45:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7B4C561FC6; Mon, 29 Aug 2022 10:45:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1661762701; bh=JOn2Yz0SLXrrVjZ/Ulr08TOR1wHDzS96CPzzwSCVj4g=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=hlAm95Ub+nnwsCeaqhhtprTfPj6Qrn6pMEcXuX8G+UgIai9OxbHEMj32RV0CSe3ZY VjeABGk9xZg5OHlec8QG7VQnPyW22YXbluW+ysblETEq5FOKY/NcpGgvAml/5bC6h1 cwiTGlCi9BmJ53JR1Zbm4tpP3Ji0VR0etRquTYkiKKUkcS4s2HCiGa+pxPb50+9myN fOOxYaCn2qeersx3s7O9TOoPX96V2w+g8eXoHuJ2AALRN/t3LdiTce4zu21OS3+sNx 80IJpJH89ET8NOi5E4eomOBKM9SKgNpMSqZ/urh8gYrDAwXblRaH6b+gmJnQHpn9/f 2MFP4B1sTEDTQ== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9240761FBF for ; Mon, 29 Aug 2022 10:44:57 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="jwXIZjv5"; dkim-atps=neutral Received: from deskari.lan (91-158-154-79.elisa-laajakaista.fi [91.158.154.79]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id EBD8A8BD; Mon, 29 Aug 2022 10:44:56 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1661762697; bh=JOn2Yz0SLXrrVjZ/Ulr08TOR1wHDzS96CPzzwSCVj4g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jwXIZjv5WQ3tATotsDuINWt02IKRq0NHBq6w/devM38N8R4z+U+6K+JhZmx1bTXet vONwDFqP++2c0dfGAU1pNrj/ftZGstdNFdjsBf21MPcuBSUDQ2TB10H4IZrX5jTS39 odysSQUkBBE/4v2vlhXHda7OXnkRd1qq+vVmRqos= To: libcamera-devel , Laurent Pinchart Date: Mon, 29 Aug 2022 11:44:44 +0300 Message-Id: <20220829084445.61585-2-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220829084445.61585-1-tomi.valkeinen@ideasonboard.com> References: <20220829084445.61585-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/2] libcamera: base: log: Fix use of freed name X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tomi Valkeinen via libcamera-devel From: Tomi Valkeinen Reply-To: Tomi Valkeinen Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" LogCategory just stores the char * that was given to it in the constructor, i.e. it refers to memory "outside" LogCategory. If the LogCategory is defined in a .so that is unloaded, then it leads to the LogCategory pointing to freed memory, causing a crash. Fix this by taking a copy of the name by using a std::string instead of just storing the pointer. Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- include/libcamera/base/log.h | 4 ++-- src/libcamera/base/log.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 3fc5ced3..8b462767 100644 --- a/include/libcamera/base/log.h +++ b/include/libcamera/base/log.h @@ -31,14 +31,14 @@ class LogCategory public: explicit LogCategory(const char *name); - const char *name() const { return name_; } + const std::string &name() const { return name_; } LogSeverity severity() const { return severity_; } void setSeverity(LogSeverity severity); static const LogCategory &defaultCategory(); private: - const char *name_; + const std::string name_; LogSeverity severity_; }; diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index 5c359a22..a4a5b452 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -568,7 +568,7 @@ void Logger::logSetLevel(const char *category, const char *level) return; for (LogCategory *c : categories_) { - if (!strcmp(c->name(), category)) { + if (c->name() == category) { c->setSeverity(severity); break; } From patchwork Mon Aug 29 08:44:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 17219 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 79555C327D for ; Mon, 29 Aug 2022 08:45:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 07E2E61FC7; Mon, 29 Aug 2022 10:45:02 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1661762702; bh=I057OMLs4150pBj6/72SFrnIhQGH3+aIBAlQ9u4drzU=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=vL/kHv7CUgLpW+S62uH6IyDq/AMIYcPCKCyuDGpygj7I057p/R6VWAr4XQS1USg2h NSCi9eLbz5eIcTioLnIwRNLBr1HH1my+OePkbhTuSU2S9Q23W5SRPdKrgl/OBpA5QY ZT/bd55wKesps2mtMdEzP2jlt3en9nY+xc3QxGUE9nMSZMHRFjzPsDxE7AoYYQW23b 4CAKK7hZsM9bXSG2kQPd6aUIoonVtoVHL5vahr/7ZkNWPKQFpGYfQHOdOV1ZqGQYcb wUwQ5FDtCY3b1XqjCzQDa8rnE3n5/Dgg8FZccOWjSpn0SAze48PUZ8xPw9PaVDPDZ9 Z4lVAPhQhOWUg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D044F61F9F for ; Mon, 29 Aug 2022 10:44:57 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Hz6VAkYB"; dkim-atps=neutral Received: from deskari.lan (91-158-154-79.elisa-laajakaista.fi [91.158.154.79]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5A53B481; Mon, 29 Aug 2022 10:44:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1661762697; bh=I057OMLs4150pBj6/72SFrnIhQGH3+aIBAlQ9u4drzU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hz6VAkYBxiDPTlnkhaq/iySoSsUHSMV2CxegqcYXdnhFqEEWHY3xJpLbCmL7FRrIo CfEKygvQickLBho7V9fjvZ1XEdtkzM1X44pA4tglktkrBjAy9uxEragpqlcZih4U0R lkWRdWpQ27eCshuyDgyXKwNB+LQDGwGJSxaEna0I= To: libcamera-devel , Laurent Pinchart Date: Mon, 29 Aug 2022 11:44:45 +0300 Message-Id: <20220829084445.61585-3-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220829084445.61585-1-tomi.valkeinen@ideasonboard.com> References: <20220829084445.61585-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 2/2] libcamera: base: log: Fix LogCategory creation issues X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tomi Valkeinen via libcamera-devel From: Tomi Valkeinen Reply-To: Tomi Valkeinen Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Each declaration of a LogCategory will create a new LogCategory, and will be stored in an unordered_set Logger::categories_. This means that when a plugin .so is unloaded and loaded, as happens when destructing and creating a CamereManager, we'll get duplicate categories. The Logger::registerCategory docs say "Log categories must have unique names. If a category with the same name already exists this function performs no operation.". The code does not comply with this. We solve the issue with two changes: Change the unordered_set to a vector for simplicity, as there's no need for an unordered_set. Instead of using the LogCategory constructor to create new categories in _LOG_CATEGORY() macro, use a factory method. The factory method will return either an existing LogCategory if one exists with the given name, or a newly created one. Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- include/libcamera/base/log.h | 6 +++-- src/libcamera/base/log.cpp | 49 +++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 8b462767..dcaacbe0 100644 --- a/include/libcamera/base/log.h +++ b/include/libcamera/base/log.h @@ -29,7 +29,7 @@ enum LogSeverity { class LogCategory { public: - explicit LogCategory(const char *name); + static LogCategory *create(const char *name); const std::string &name() const { return name_; } LogSeverity severity() const { return severity_; } @@ -38,6 +38,8 @@ public: static const LogCategory &defaultCategory(); private: + explicit LogCategory(const char *name); + const std::string name_; LogSeverity severity_; }; @@ -49,7 +51,7 @@ extern const LogCategory &_LOG_CATEGORY(name)(); const LogCategory &_LOG_CATEGORY(name)() \ { \ /* The instance will be deleted by the Logger destructor. */ \ - static LogCategory *category = new LogCategory(#name); \ + static LogCategory *category = LogCategory::create(#name); \ return *category; \ } diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp index a4a5b452..8bf1beb9 100644 --- a/src/libcamera/base/log.cpp +++ b/src/libcamera/base/log.cpp @@ -314,10 +314,11 @@ private: friend LogCategory; void registerCategory(LogCategory *category); + LogCategory *findCategory(const char *name) const; static bool destroyed_; - std::unordered_set categories_; + std::vector categories_; std::list> levels_; std::shared_ptr output_; @@ -707,12 +708,12 @@ LogSeverity Logger::parseLogLevel(const std::string &level) * \brief Register a log category with the logger * \param[in] category The log category * - * Log categories must have unique names. If a category with the same name - * already exists this function performs no operation. + * Log categories must have unique names. It is invalid to call this function + * if a log category with the same name already exists. */ void Logger::registerCategory(LogCategory *category) { - categories_.insert(category); + categories_.push_back(category); const std::string &name = category->name(); for (const std::pair &level : levels_) { @@ -736,6 +737,22 @@ void Logger::registerCategory(LogCategory *category) } } +/** + * \brief Find an existing log category with the given name + * \param[in] name Name of the log category + * \return The pointer to the found log category or nullptr if not found. + */ +LogCategory *Logger::findCategory(const char *name) const +{ + if (auto it = std::find_if(categories_.begin(), categories_.end(), + [name](auto c) { return c->name() == name; }); + it != categories_.end()) { + return *it; + } + + return nullptr; +} + /** * \enum LogSeverity * Log message severity @@ -760,6 +777,27 @@ void Logger::registerCategory(LogCategory *category) * and is used to control the log level per group. */ +/** + * \brief Create a new LogCategory or return an existing one + * \param[in] name Name of the log category + * + * Create and return a new LogCategory with the given name if such a category + * does not yet exist, or return the existing one. + * + * \return The pointer to the LogCategory + */ +LogCategory *LogCategory::create(const char *name) +{ + LogCategory *category = Logger::instance()->findCategory(name); + + if (!category) { + category = new LogCategory(name); + Logger::instance()->registerCategory(category); + } + + return category; +} + /** * \brief Construct a log category * \param[in] name The category name @@ -767,7 +805,6 @@ void Logger::registerCategory(LogCategory *category) LogCategory::LogCategory(const char *name) : name_(name), severity_(LogSeverity::LogInfo) { - Logger::instance()->registerCategory(this); } /** @@ -804,7 +841,7 @@ void LogCategory::setSeverity(LogSeverity severity) */ const LogCategory &LogCategory::defaultCategory() { - static const LogCategory *category = new LogCategory("default"); + static const LogCategory *category = LogCategory::create("default"); return *category; }