From patchwork Fri Aug 26 11:39:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 17204 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 66678C0DA4 for ; Fri, 26 Aug 2022 11:39:46 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4E47B61FBB; Fri, 26 Aug 2022 13:39:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1661513985; bh=DQfpMcbNtUoBdFmgnTbLvJ38yNa23qiAAEaj7/AVmwo=; 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=HjJIP1nPhcDpA9tc09mKlgJLc33MRQ3hAaxl//4Mf/o+cbx/q4Gw9b4AUtMdpBXsH ISOhkxxyMejlq7ee7xpSYs2boVuIxC6Dj2iI+IkWs7tB3/322GO+P++dAhOw/HMHJy N6a60LCJwkZlGxtr0EiM0LCw+gStvFo1EnmPmES4DSMwc8WJF1cgzWqG1Mec4vyOzE gRy/jFNh6rDDGZhsDWrFQghIA6R5eZvPdb7P4uHEJokmO97ct8Pdpgv02HKWocGwGk UEOv7efXIWt3IY8dE7vfCUipSPSgL29xTQQurP/xsGQ2otLN4rKR3Fqc08qCnmXgk1 KuXuU2SVtNO/w== 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 4A74E61FBB for ; Fri, 26 Aug 2022 13:39:43 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="hEQQoZj0"; 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 B70363F1; Fri, 26 Aug 2022 13:39:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1661513982; bh=DQfpMcbNtUoBdFmgnTbLvJ38yNa23qiAAEaj7/AVmwo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hEQQoZj05JY1/Us71QKqwwGONBQNkVdZDt2XUbKk9GX9UIVrHCpbIKhwH2kc8I5Ua pwTmLYziouNfD/MaJFAKZdbPZ8jDZ6SDFEvwcC/LtYF7Bpd+qe9a8J0QH+ITaCe0mf NlntZPO7lRzFuZyEsIABMT6vWD3uN813KS9v1OcA= To: libcamera-devel Date: Fri, 26 Aug 2022 14:39:30 +0300 Message-Id: <20220826113931.59323-2-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220826113931.59323-1-tomi.valkeinen@ideasonboard.com> References: <20220826113931.59323-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 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 --- 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 Fri Aug 26 11:39:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 17205 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 87F7AC0DA4 for ; Fri, 26 Aug 2022 11:39:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4293361FC6; Fri, 26 Aug 2022 13:39:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1661513987; bh=TO/Vx+RzC7G8vuR5uPw3FpEv0EXcr+Qx4fZmWen4Z0k=; 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=h6R8FdXR7Vi2AylcGABaS5f9OlYzKOVZFT+6mGAZ5rgpyCHPQ1HBmhvjrQYcG6A/x fL/GgIyZyZEp/r0dFl0+DcpZ+phIUsGfGNmEBTRVNNlCg4TqOZTNwKUgkh0y0X+0mo RgYvoK8RXx90ErDYJERTEUosEetvjk+zaMe4n+khtysKvcqaDPsSgdPv0L6BAHNfmg YZhbvyDjFz7WjNLkU2zMjowRCf0CDWoecpfol5CuTD+1/pDRw5iLkY2bXh/m6nl5VU lJ//UI+uFdxPVwog1QQXbaNuJHy8CbkD6XBWn8rTvF8r75D+x8UXffq099IkAn65l2 +6OZG90LalMeg== 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 8727561FA2 for ; Fri, 26 Aug 2022 13:39:43 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Ig4f7onR"; 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 1DB592B3; Fri, 26 Aug 2022 13:39:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1661513983; bh=TO/Vx+RzC7G8vuR5uPw3FpEv0EXcr+Qx4fZmWen4Z0k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ig4f7onRZOvOvy+JgHVpU/b+mvfzgiJ/qfjDpTkHKt/K3qtNk23XukANyCR/msGdZ QQhoOG/EAuPRdAFC25eOg9tPd+CReFTGsXeN/CkyoG0buxphZK76eyAVzhngaXQ7Jz HTBXhTWkGQ864leVj7GKVWbtJCSfslBj2Nmvr8p4= To: libcamera-devel Date: Fri, 26 Aug 2022 14:39:31 +0300 Message-Id: <20220826113931.59323-3-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220826113931.59323-1-tomi.valkeinen@ideasonboard.com> References: <20220826113931.59323-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 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 --- include/libcamera/base/log.h | 6 +++-- src/libcamera/base/log.cpp | 51 +++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h index 8b462767..76e01118 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 std::string &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 std::string &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..c6500e96 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 std::string &name) const; static bool destroyed_; - std::unordered_set categories_; + std::vector categories_; std::list> levels_; std::shared_ptr output_; @@ -707,12 +708,14 @@ 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 illegal to call this function + * if a log category with the same name already exists. */ void Logger::registerCategory(LogCategory *category) { - categories_.insert(category); + ASSERT(!findCategory(category->name())); + + categories_.push_back(category); const std::string &name = category->name(); for (const std::pair &level : levels_) { @@ -736,6 +739,21 @@ void Logger::registerCategory(LogCategory *category) } } +/** + * \brief Find an existing log category with the given name + * \param[in] name The name of the log category + */ +LogCategory *Logger::findCategory(const std::string &name) const +{ + if (auto it = std::find_if(categories_.begin(), categories_.end(), + [&name](auto c) { return name == c->name(); }); + it != categories_.end()) { + return *it; + } + + return nullptr; +} + /** * \enum LogSeverity * Log message severity @@ -760,14 +778,33 @@ 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 + * \return The pointer to the LogCategory + * + * Create and return a new LogCategory with the given name if such a category + * does not yet exist, or return the existing one. + */ +LogCategory *LogCategory::create(const std::string &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 */ -LogCategory::LogCategory(const char *name) +LogCategory::LogCategory(const std::string &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; }