Skip to content

Commit

Permalink
Revert "Darwin: Prohibit static initializers in Matter.framework" (#3…
Browse files Browse the repository at this point in the history
…4286)

This reverts commit f199ba4.
  • Loading branch information
woody-apple authored and pull[bot] committed Feb 4, 2025
1 parent 1328af8 commit 1402988
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 80 deletions.
8 changes: 3 additions & 5 deletions src/app/AttributeAccessInterfaceCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AttributeAccessInterfaceCache
kDefinitelyUsed
};

constexpr AttributeAccessInterfaceCache() = default;
AttributeAccessInterfaceCache() { Invalidate(); }

/**
* @brief Invalidate the whole cache. Must be called every time list of AAI registrations changes.
Expand Down Expand Up @@ -106,8 +106,6 @@ class AttributeAccessInterfaceCache
private:
struct AttributeAccessCacheEntry
{
constexpr AttributeAccessCacheEntry() = default;

EndpointId endpointId = kInvalidEndpointId;
ClusterId clusterId = kInvalidClusterId;
AttributeAccessInterface * accessor = nullptr;
Expand Down Expand Up @@ -139,8 +137,8 @@ class AttributeAccessInterfaceCache
return &mCacheSlots[0];
}

AttributeAccessCacheEntry mCacheSlots[1] = {};
AttributeAccessCacheEntry mLastUnusedEntry{};
AttributeAccessCacheEntry mCacheSlots[1];
AttributeAccessCacheEntry mLastUnusedEntry;
};

} // namespace app
Expand Down
2 changes: 1 addition & 1 deletion src/app/EventLoggingTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct Timestamp
kSystem = 0,
kEpoch
};
constexpr Timestamp() = default;
Timestamp() {}
Timestamp(Type aType, uint64_t aValue) : mType(aType), mValue(aValue) {}
Timestamp(System::Clock::Timestamp aValue) : mType(Type::kSystem), mValue(aValue.count()) {}
static Timestamp Epoch(System::Clock::Timestamp aValue)
Expand Down
2 changes: 1 addition & 1 deletion src/app/EventManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using namespace chip::TLV;

namespace chip {
namespace app {
EventManagement EventManagement::sInstance;
static EventManagement sInstance;

/**
* @brief
Expand Down
9 changes: 3 additions & 6 deletions src/app/EventManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace app {
inline constexpr const uint32_t kEventManagementProfile = 0x1;
inline constexpr const uint32_t kFabricIndexTag = 0x1;
inline constexpr size_t kMaxEventSizeReserve = 512;
inline constexpr uint16_t kRequiredEventField =
constexpr uint16_t kRequiredEventField =
(1 << to_underlying(EventDataIB::Tag::kPriority)) | (1 << to_underlying(EventDataIB::Tag::kPath));

/**
Expand Down Expand Up @@ -388,9 +388,6 @@ class EventManagement
void SetScheduledEventInfo(EventNumber & aEventNumber, uint32_t & aInitialWrittenEventBytes) const;

private:
constexpr EventManagement() = default;
static EventManagement sInstance;

/**
* @brief
* Internal structure for traversing events.
Expand Down Expand Up @@ -558,9 +555,9 @@ class EventManagement
MonotonicallyIncreasingCounter<EventNumber> * mpEventNumberCounter = nullptr;

EventNumber mLastEventNumber = 0; ///< Last event Number vended
Timestamp mLastEventTimestamp{}; ///< The timestamp of the last event in this buffer
Timestamp mLastEventTimestamp; ///< The timestamp of the last event in this buffer

System::Clock::Milliseconds64 mMonotonicStartupTime{};
System::Clock::Milliseconds64 mMonotonicStartupTime;
};
} // namespace app
} // namespace chip
7 changes: 2 additions & 5 deletions src/app/clusters/descriptor/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/util/attribute-storage.h>
#include <app/util/endpoint-config-api.h>
#include <lib/core/Global.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>

Expand Down Expand Up @@ -206,9 +205,7 @@ CHIP_ERROR DescriptorAttrAccess::ReadClusterRevision(EndpointId endpoint, Attrib
return aEncoder.Encode(kClusterRevision);
}

namespace {
Global<DescriptorAttrAccess> gAttrAccess;
}
DescriptorAttrAccess gAttrAccess;

CHIP_ERROR DescriptorAttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
Expand Down Expand Up @@ -247,5 +244,5 @@ CHIP_ERROR DescriptorAttrAccess::Read(const ConcreteReadAttributePath & aPath, A

void MatterDescriptorPluginServerInitCallback()
{
registerAttributeAccessOverride(&gAttrAccess.get());
registerAttributeAccessOverride(&gAttrAccess);
}
2 changes: 0 additions & 2 deletions src/darwin/Framework/Matter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2497,7 +2497,6 @@
"-Wl,-unexported_symbol,\"__Unwind_*\"",
"-Wl,-unexported_symbol,\"_unw_*\"",
"-Wl,-hidden-lCHIP",
"-Wl,-no_inits",
);
"OTHER_LDFLAGS[sdk=macosx*]" = (
"-framework",
Expand All @@ -2516,7 +2515,6 @@
"-Wl,-unexported_symbol,\"__Unwind_*\"",
"-Wl,-unexported_symbol,\"_unw_*\"",
"-Wl,-hidden-lCHIP",
"-Wl,-no_inits",
);
PRODUCT_BUNDLE_IDENTIFIER = com.csa.matter;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,14 @@
#include <platform/ThreadStackManager.h>
#endif

#include <optional>

// TODO : may be we can make it configurable
#define BLE_ADVERTISEMENT_VERSION 0

namespace chip {
namespace DeviceLayer {
namespace Internal {

namespace {
std::optional<System::Clock::Seconds32> gFirmwareBuildChipEpochTime;
}
static Optional<System::Clock::Seconds32> sFirmwareBuildChipEpochTime;

#if CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER

Expand Down Expand Up @@ -292,9 +288,9 @@ template <class ConfigClass>
CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetFirmwareBuildChipEpochTime(System::Clock::Seconds32 & chipEpochTime)
{
// If the setter was called and we have a value in memory, return this.
if (gFirmwareBuildChipEpochTime.has_value())
if (sFirmwareBuildChipEpochTime.HasValue())
{
chipEpochTime = gFirmwareBuildChipEpochTime.value();
chipEpochTime = sFirmwareBuildChipEpochTime.Value();
return CHIP_NO_ERROR;
}
#ifdef CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S
Expand Down Expand Up @@ -327,7 +323,7 @@ CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::SetFirmwareBuildChipEpo
//
// Implementations that can't use the hard-coded time for whatever reason
// should set this at each init.
gFirmwareBuildChipEpochTime = chipEpochTime;
sFirmwareBuildChipEpochTime.SetValue(chipEpochTime);
return CHIP_NO_ERROR;
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/support/IntrusiveList.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ enum class IntrusiveMode
class IntrusiveListNodePrivateBase
{
public:
constexpr IntrusiveListNodePrivateBase() : mPrev(nullptr), mNext(nullptr) {}
IntrusiveListNodePrivateBase() : mPrev(nullptr), mNext(nullptr) {}
~IntrusiveListNodePrivateBase() { VerifyOrDie(!IsInList()); }

// Note: The copy construct/assignment is not provided because the list node state is not copyable.
Expand All @@ -98,7 +98,7 @@ class IntrusiveListNodePrivateBase

private:
friend class IntrusiveListBase;
constexpr IntrusiveListNodePrivateBase(IntrusiveListNodePrivateBase * prev, IntrusiveListNodePrivateBase * next) :
IntrusiveListNodePrivateBase(IntrusiveListNodePrivateBase * prev, IntrusiveListNodePrivateBase * next) :
mPrev(prev), mNext(next)
{}

Expand Down Expand Up @@ -284,7 +284,7 @@ class IntrusiveListBase
// ^ |
// \------------------------------------------/
//
constexpr IntrusiveListBase() : mNode(&mNode, &mNode) {}
IntrusiveListBase() : mNode(&mNode, &mNode) {}
~IntrusiveListBase()
{
VerifyOrDie(Empty());
Expand Down Expand Up @@ -399,7 +399,7 @@ template <typename T, IntrusiveMode Mode = IntrusiveMode::Strict, typename Hook
class IntrusiveList : public IntrusiveListBase
{
public:
constexpr IntrusiveList() : IntrusiveListBase() {}
IntrusiveList() : IntrusiveListBase() {}

IntrusiveList(IntrusiveList &&) = default;
IntrusiveList & operator=(IntrusiveList &&) = default;
Expand Down
44 changes: 19 additions & 25 deletions src/messaging/ReliableMessageProtocolConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,28 @@
#include <app/icd/server/ICDConfigurationData.h> // nogncheck
#endif

#include <optional>

namespace chip {

using namespace System::Clock::Literals;

#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
namespace {
// Use std::optional for globals to avoid static initializers
std::optional<System::Clock::Timeout> gIdleRetransTimeoutOverride;
std::optional<System::Clock::Timeout> gActiveRetransTimeoutOverride;
std::optional<System::Clock::Timeout> gActiveThresholdTimeOverride;
} // namespace
static Optional<System::Clock::Timeout> idleRetransTimeoutOverride = NullOptional;
static Optional<System::Clock::Timeout> activeRetransTimeoutOverride = NullOptional;
static Optional<System::Clock::Timeout> activeThresholdTimeOverride = NullOptional;

void OverrideLocalMRPConfig(System::Clock::Timeout idleRetransTimeout, System::Clock::Timeout activeRetransTimeout,
System::Clock::Timeout activeThresholdTime)
{
gIdleRetransTimeoutOverride = idleRetransTimeout;
gActiveRetransTimeoutOverride = activeRetransTimeout;
gActiveThresholdTimeOverride = activeThresholdTime;
idleRetransTimeoutOverride.SetValue(idleRetransTimeout);
activeRetransTimeoutOverride.SetValue(activeRetransTimeout);
activeThresholdTimeOverride.SetValue(activeThresholdTime);
}

void ClearLocalMRPConfigOverride()
{
gActiveRetransTimeoutOverride = std::nullopt;
gIdleRetransTimeoutOverride = std::nullopt;
gActiveThresholdTimeOverride = std::nullopt;
activeRetransTimeoutOverride.ClearValue();
idleRetransTimeoutOverride.ClearValue();
activeThresholdTimeOverride.ClearValue();
}
#endif

Expand All @@ -67,15 +62,14 @@ namespace {

// This is not a static member of ReliableMessageProtocolConfig because the free
// function GetLocalMRPConfig() needs access to it.
// Use std::optional to avoid a static initializer
std::optional<ReliableMessageProtocolConfig> sDynamicLocalMPRConfig;
Optional<ReliableMessageProtocolConfig> sDynamicLocalMPRConfig;

} // anonymous namespace

bool ReliableMessageProtocolConfig::SetLocalMRPConfig(const Optional<ReliableMessageProtocolConfig> & localMRPConfig)
{
auto oldConfig = GetLocalMRPConfig();
sDynamicLocalMPRConfig = localMRPConfig.std_optional();
sDynamicLocalMPRConfig = localMRPConfig;
return oldConfig != GetLocalMRPConfig();
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_DYNAMIC_MRP_CONFIG
Expand All @@ -95,9 +89,9 @@ Optional<ReliableMessageProtocolConfig> GetLocalMRPConfig()
ReliableMessageProtocolConfig config(CHIP_CONFIG_MRP_LOCAL_IDLE_RETRY_INTERVAL, CHIP_CONFIG_MRP_LOCAL_ACTIVE_RETRY_INTERVAL);

#if CHIP_DEVICE_CONFIG_ENABLE_DYNAMIC_MRP_CONFIG
if (sDynamicLocalMPRConfig.has_value())
if (sDynamicLocalMPRConfig.HasValue())
{
config = sDynamicLocalMPRConfig.value();
config = sDynamicLocalMPRConfig.Value();
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_DYNAMIC_MRP_CONFIG

Expand All @@ -111,19 +105,19 @@ Optional<ReliableMessageProtocolConfig> GetLocalMRPConfig()
#endif

#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
if (gIdleRetransTimeoutOverride.has_value())
if (idleRetransTimeoutOverride.HasValue())
{
config.mIdleRetransTimeout = gIdleRetransTimeoutOverride.value();
config.mIdleRetransTimeout = idleRetransTimeoutOverride.Value();
}

if (gActiveRetransTimeoutOverride.has_value())
if (activeRetransTimeoutOverride.HasValue())
{
config.mActiveRetransTimeout = gActiveRetransTimeoutOverride.value();
config.mActiveRetransTimeout = activeRetransTimeoutOverride.Value();
}

if (gActiveThresholdTimeOverride.has_value())
if (activeThresholdTimeOverride.HasValue())
{
config.mActiveThresholdTime = gActiveThresholdTimeOverride.value();
config.mActiveThresholdTime = activeRetransTimeoutOverride.Value();
}
#endif

Expand Down
5 changes: 2 additions & 3 deletions src/messaging/ReliableMessageProtocolConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,8 @@ inline constexpr System::Clock::Milliseconds32 kDefaultActiveTime = System::Cloc
*/
struct ReliableMessageProtocolConfig
{
constexpr ReliableMessageProtocolConfig(System::Clock::Milliseconds32 idleInterval,
System::Clock::Milliseconds32 activeInterval,
System::Clock::Milliseconds16 activeThreshold = kDefaultActiveTime) :
ReliableMessageProtocolConfig(System::Clock::Milliseconds32 idleInterval, System::Clock::Milliseconds32 activeInterval,
System::Clock::Milliseconds16 activeThreshold = kDefaultActiveTime) :
mIdleRetransTimeout(idleInterval),
mActiveRetransTimeout(activeInterval), mActiveThresholdTime(activeThreshold)
{}
Expand Down
8 changes: 2 additions & 6 deletions src/platform/Darwin/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <platform/Darwin/ConfigurationManagerImpl.h>

#include <lib/core/CHIPVendorIdentifiers.hpp>
#include <lib/core/Global.h>
#include <platform/CHIPDeviceConfig.h>
#include <platform/ConfigurationManager.h>
#include <platform/Darwin/DiagnosticDataProviderImpl.h>
Expand Down Expand Up @@ -139,13 +138,10 @@ CHIP_ERROR GetMACAddressFromInterfaces(io_iterator_t primaryInterfaceIterator, u
}
#endif // TARGET_OS_OSX

namespace {
AtomicGlobal<ConfigurationManagerImpl> gInstance;
}

ConfigurationManagerImpl & ConfigurationManagerImpl::GetDefaultInstance()
{
return gInstance.get();
static ConfigurationManagerImpl sInstance;
return sInstance;
}

CHIP_ERROR ConfigurationManagerImpl::Init()
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Darwin/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
namespace chip {
namespace DeviceLayer {

inline constexpr int kCountryCodeLength = 2;
static constexpr int kCountryCodeLength = 2;

/**
* Concrete implementation of the ConfigurationManager singleton object for the Darwin platform.
Expand Down
10 changes: 0 additions & 10 deletions src/platform/Darwin/DeviceInstanceInfoProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "DeviceInstanceInfoProviderImpl.h"

#include <lib/core/Global.h>
#include <platform/Darwin/PosixConfig.h>

namespace chip {
Expand All @@ -34,14 +33,5 @@ CHIP_ERROR DeviceInstanceInfoProviderImpl::GetProductId(uint16_t & productId)
return Internal::PosixConfig::ReadConfigValue(Internal::PosixConfig::kConfigKey_ProductId, productId);
}

namespace {
AtomicGlobal<DeviceInstanceInfoProviderImpl> gInstance;
} // namespace

DeviceInstanceInfoProviderImpl & DeviceInstanceInfoProviderMgrImpl()
{
return gInstance.get();
}

} // namespace DeviceLayer
} // namespace chip
8 changes: 5 additions & 3 deletions src/platform/Darwin/DeviceInstanceInfoProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ class DeviceInstanceInfoProviderImpl : public Internal::GenericDeviceInstanceInf
CHIP_ERROR GetVendorId(uint16_t & vendorId) override;
CHIP_ERROR GetProductId(uint16_t & productId) override;

DeviceInstanceInfoProviderImpl() : DeviceInstanceInfoProviderImpl(ConfigurationManagerImpl::GetDefaultInstance()) {}
DeviceInstanceInfoProviderImpl(ConfigurationManagerImpl & configManager) :
Internal::GenericDeviceInstanceInfoProvider<Internal::PosixConfig>(configManager)
{}
};

DeviceInstanceInfoProviderImpl & DeviceInstanceInfoProviderMgrImpl();

inline DeviceInstanceInfoProviderImpl & DeviceInstanceInfoProviderMgrImpl()
{
static DeviceInstanceInfoProviderImpl sInstance(ConfigurationManagerImpl::GetDefaultInstance());
return sInstance;
}
} // namespace DeviceLayer
} // namespace chip

0 comments on commit 1402988

Please sign in to comment.