Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/cm/app/aoscore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,28 @@ void AosCore::Init(const std::string& configFile)

// Initialize crypto provider

err = mCryptoProvider.Init();
err = mCryptoProvider.Init(mAllocator);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize crypto provider");

// Initialize PKCS11 manager

err = mPKCS11Manager.Init(mAllocator);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize PKCS11 manager");

// Initialize cert loader

err = mCertLoader.Init(mCryptoProvider, mPKCS11Manager);
err = mCertLoader.Init(mAllocator, mCryptoProvider, mPKCS11Manager);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize cert loader");

// Initialize crypto helper

err = mCryptoHelper.Init(
mIAMClient, mCryptoProvider, mCertLoader, mConfig.mServiceDiscoveryURL.c_str(), mConfig.mCACert.c_str());
err = mCryptoHelper.Init(mAllocator, mIAMClient, mCryptoProvider, mCertLoader, mConfig.mServiceDiscoveryURL.c_str(),
mConfig.mCACert.c_str());
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize crypto helper");

// Initialize file info provider

err = mFileInfoProvider.Init(mCryptoProvider);
err = mFileInfoProvider.Init(mAllocator, mCryptoProvider);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize file info provider");

// Initialize TLS credentials
Expand All @@ -72,13 +77,13 @@ void AosCore::Init(const std::string& configFile)
InitDatabase();
InitStorageState();

err = mAlerts.Init(mConfig.mAlerts, mCommunication, mCommunication);
err = mAlerts.Init(mAllocator, mConfig.mAlerts, mCommunication, mCommunication);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize alerts");

err = mDownloadSpaceAllocator.Init(mConfig.mImageManager.mInstallPath, mPlatformFS, 0, &mImageManager);
err = mDownloadSpaceAllocator.Init(mAllocator, mConfig.mImageManager.mInstallPath, mPlatformFS, 0, &mImageManager);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize download space allocator");

err = mInstallSpaceAllocator.Init(mConfig.mImageManager.mInstallPath, mPlatformFS, 0, &mImageManager);
err = mInstallSpaceAllocator.Init(mAllocator, mConfig.mImageManager.mInstallPath, mPlatformFS, 0, &mImageManager);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize install space allocator");

err = mDownloader.Init(&mAlerts);
Expand All @@ -87,26 +92,27 @@ void AosCore::Init(const std::string& configFile)
err = mFileServer.Init(mConfig.mFileServerURL, mConfig.mImageManager.mInstallPath.CStr());
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize file server");

err = mImageManager.Init(mConfig.mImageManager, mDatabase, mCommunication, mDownloadSpaceAllocator,
err = mImageManager.Init(mAllocator, mConfig.mImageManager, mDatabase, mCommunication, mDownloadSpaceAllocator,
mInstallSpaceAllocator, mDownloader, mFileServer, mCryptoHelper, mFileInfoProvider, mOCISpec);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize image manager");

err = mNodeInfoProvider.Init(mConfig.mNodeInfoProvider, mIAMClient);
err = mNodeInfoProvider.Init(mAllocator, mConfig.mNodeInfoProvider, mIAMClient);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize node info provider");

err = mMonitoring.Init(mConfig.mMonitoring, mCommunication, mCommunication, mLauncher, mNodeInfoProvider);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize monitoring");

err = mUnitConfig.Init({mConfig.mUnitConfigFile.c_str()}, mNodeInfoProvider, mSMController, mJSONProvider);
err = mUnitConfig.Init(
mAllocator, {mConfig.mUnitConfigFile.c_str()}, mNodeInfoProvider, mSMController, mJSONProvider);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize unit config");

err = mLauncher.Init(mConfig.mLauncher, mNodeInfoProvider, mSMController, mImageManager, mOCISpec, mUnitConfig,
mStorageState, mSMController, mAlerts, mIAMClient, utils::IsUIDValid, utils::IsGIDValid, mDatabase,
err = mLauncher.Init(mAllocator, mConfig.mLauncher, mNodeInfoProvider, mSMController, mImageManager, mOCISpec,
mUnitConfig, mStorageState, mSMController, mAlerts, mIAMClient, utils::IsUIDValid, utils::IsGIDValid, mDatabase,
mCommunication);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize launcher");

err = mUpdateManager.Init({mConfig.mUnitStatusSendTimeout}, mIAMClient, mIAMClient, mUnitConfig, mNodeInfoProvider,
mImageManager, mLauncher, mCommunication, mCommunication, mDatabase);
err = mUpdateManager.Init(mAllocator, {mConfig.mUnitStatusSendTimeout}, mIAMClient, mIAMClient, mUnitConfig,
mNodeInfoProvider, mImageManager, mLauncher, mCommunication, mCommunication, mDatabase);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize update manager");

mDNSServer.Init(mConfig.mDNSStoragePath, mConfig.mDNSPidFile, mConfig.mDNSIP);
Expand Down Expand Up @@ -275,7 +281,7 @@ void AosCore::InitStorageState()
err = config.mStorageDir.Assign(mConfig.mStorageDir.c_str());
AOS_ERROR_CHECK_AND_THROW(err, "can't assign storage dir to storage state config");

err = mStorageState.Init(config, mDatabase, mCommunication, mPlatformFS, mFSWatcher, mCryptoProvider);
err = mStorageState.Init(mAllocator, config, mDatabase, mCommunication, mPlatformFS, mFSWatcher, mCryptoProvider);
AOS_ERROR_CHECK_AND_THROW(err, "can't initialize storage state");
}

Expand Down
6 changes: 5 additions & 1 deletion src/cm/app/aoscore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <core/common/crypto/certloader.hpp>
#include <core/common/crypto/cryptoprovider.hpp>
#include <core/common/spaceallocator/spaceallocator.hpp>
#include <core/common/tools/heapallocator.hpp>

#include <common/downloader/downloader.hpp>
#include <common/fileserver/fileserver.hpp>
Expand Down Expand Up @@ -85,7 +86,10 @@ class AosCore {
void InitStorageState();
void InitSMController();

config::Config mConfig = {};
aos::HeapAllocator mAllocator;

config::Config mConfig = {};

aos::crypto::CertLoader mCertLoader;
aos::crypto::DefaultCryptoProvider mCryptoProvider;
aos::crypto::CryptoHelper mCryptoHelper;
Expand Down
26 changes: 17 additions & 9 deletions src/cm/communication/tests/communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <core/common/tests/utils/log.hpp>
#include <core/common/tests/utils/utils.hpp>
#include <core/common/tools/fs.hpp>
#include <core/common/tools/heapallocator.hpp>
#include <core/common/types/state.hpp>
#include <core/iam/certhandler/certmodules/pkcs11/pkcs11.hpp>

Expand Down Expand Up @@ -132,6 +133,11 @@ std::string CreateDiscoveryResponse(const std::vector<std::string>& connectionIn

class CMCommunicationTest : public Test {
public:
CMCommunicationTest()
: mCertHandler(mAllocator)
{
}

static void SetUpTestSuite() { Poco::Net::initializeSSL(); }

static void TearDownTestSuite() { Poco::Net::uninitializeSSL(); }
Expand All @@ -149,15 +155,15 @@ class CMCommunicationTest : public Test {
return ErrorEnum::eNone;
}));

auto err = mCryptoProvider.Init();
auto err = mCryptoProvider.Init(mAllocator);

ASSERT_TRUE(err.IsNone()) << "Failed to initialize crypto provider: " << tests::utils::ErrorToStr(err);

err = mSOFTHSMEnv.Init("", "certhandler-integration-tests", SOFTHSM_BASE_CM_DIR "/softhsm2.conf",
err = mSOFTHSMEnv.Init(mAllocator, "", "certhandler-integration-tests", SOFTHSM_BASE_CM_DIR "/softhsm2.conf",
SOFTHSM_BASE_CM_DIR "/tokens", SOFTHSM2_LIB);
ASSERT_TRUE(err.IsNone()) << "Failed to initialize SOFTHSM environment: " << tests::utils::ErrorToStr(err);

err = mCertLoader.Init(mCryptoProvider, mSOFTHSMEnv.GetManager());
err = mCertLoader.Init(mAllocator, mCryptoProvider, mSOFTHSMEnv.GetManager());
ASSERT_TRUE(err.IsNone()) << "Failed to initialize certificate loader: " << tests::utils::ErrorToStr(err);

RegisterPKCS11Module(cCertificate);
Expand All @@ -178,8 +184,8 @@ class CMCommunicationTest : public Test {
auto [certPEM, err2] = common::utils::LoadPEMCertificates(certInfo.mCertURL, mCertLoader, mCryptoProvider);
EXPECT_EQ(err2, ErrorEnum::eNone);

err = mCryptoHelper.Init(mCertProviderStub, mCryptoProvider, mCertLoader, mConfig.mServiceDiscoveryURL.c_str(),
mConfig.mCACert.c_str());
err = mCryptoHelper.Init(mAllocator, mCertProviderStub, mCryptoProvider, mCertLoader,
mConfig.mServiceDiscoveryURL.c_str(), mConfig.mCACert.c_str());
ASSERT_TRUE(err.IsNone()) << "Failed to initialize crypto helper: " << tests::utils::ErrorToStr(err);

StartHTTPServer();
Expand Down Expand Up @@ -273,9 +279,11 @@ class CMCommunicationTest : public Test {
auto& pkcs11Module = mPKCS11Modules.Back();
auto& certModule = mCertModules.Back();
ASSERT_TRUE(
pkcs11Module.Init(name, GetPKCS11ModuleConfig(), mSOFTHSMEnv.GetManager(), mCryptoProvider).IsNone());
pkcs11Module.Init(mAllocator, name, GetPKCS11ModuleConfig(), mSOFTHSMEnv.GetManager(), mCryptoProvider)
.IsNone());
ASSERT_TRUE(
certModule.Init(name, GetCertModuleConfig(keyType), mCryptoProvider, pkcs11Module, mStorage).IsNone());
certModule.Init(mAllocator, name, GetCertModuleConfig(keyType), mCryptoProvider, pkcs11Module, mStorage)
.IsNone());
ASSERT_TRUE(mCertHandler.RegisterModule(certModule).IsNone());
}

Expand Down Expand Up @@ -320,6 +328,8 @@ class CMCommunicationTest : public Test {
static constexpr auto cServerCert = CERTIFICATES_CM_DIR "/server_int.cer";
static constexpr auto cCA = CERTIFICATES_CM_DIR "/ca.cer";

HeapAllocator mAllocator;

MessageQueue mDiscoveryReceivedMessages;
MessageQueue mDiscoverySendMessages;

Expand Down Expand Up @@ -431,7 +441,6 @@ TEST_F(CMCommunicationTest, MessageIsRecentIfAckNotReceived)

mUUIDProvider.SetUUID("fb6e8461-2601-4f9a-8957-7ab4e52f304c");

// cppcheck-suppress templateRecursion
auto alerts = std::make_unique<Alerts>();
alerts->mCorrelationID = "id";

Expand Down Expand Up @@ -463,7 +472,6 @@ TEST_F(CMCommunicationTest, SendAlerts)

mUUIDProvider.SetUUID("fb6e8461-2601-4f9a-8957-7ab4e52f304c");

// cppcheck-suppress templateRecursion
auto alerts = std::make_unique<Alerts>();
alerts->mCorrelationID = "id";

Expand Down
4 changes: 2 additions & 2 deletions src/common/utils/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ RetWithError<uintmax_t> CalculateSize(const std::string& path)
}

if (fs::is_directory(path)) {
return std::accumulate(fs::recursive_directory_iterator(path), fs::recursive_directory_iterator(), 0,
[](uintmax_t total, const auto& entry) {
return std::accumulate(fs::recursive_directory_iterator(path), fs::recursive_directory_iterator(),
uintmax_t {0}, [](uintmax_t total, const auto& entry) {
return (fs::is_regular_file(entry)) ? (total + fs::file_size(entry)) : total;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/fsplatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ RetWithError<size_t> FSPlatform::GetTotalSize(const String& dir) const

RetWithError<size_t> FSPlatform::GetDirSize(const String& dir) const
{
return fs::CalculateSize(dir);
return common::utils::CalculateSize(dir.CStr());

@MykolaSuperman MykolaSuperman Aug 2, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Preserve directory sizes above INT_MAX

This switches production space accounting to common::utils::CalculateSize, whose std::accumulate starts with the literal 0, making the accumulator an int. Directories larger than INT_MAX therefore overflow before the result is converted to uintmax_t. Image and install directories can commonly exceed 2 GiB, so SpaceAllocator may receive a bogus size and make incorrect allocation or cleanup decisions. Please initialize the accumulator with uintmax_t {0} and also maybe good to add test on this case.

}

RetWithError<size_t> FSPlatform::GetAvailableSize(const String& dir) const
Expand Down
30 changes: 30 additions & 0 deletions src/common/utils/tests/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <filesystem>
#include <fstream>
#include <limits>

#include <gtest/gtest.h>

Expand Down Expand Up @@ -152,4 +153,33 @@ TEST_F(FSTest, CalculateSize)
EXPECT_EQ(size, 4 * buffer.size());
}

TEST_F(FSTest, CalculateSizeOverflow)
{
// Each file is below INT_MAX, but their sum is above it. This catches accumulators that start
// from an `int` zero and overflow before the result is converted to uintmax_t.

constexpr uintmax_t cFileSize = 1500000000ULL;

const auto root = std::filesystem::path(cTestDir) / "size-overflow-test";
const auto f1 = root / "f1";
const auto f2 = root / "f2";

std::filesystem::create_directories(f1);
std::filesystem::create_directories(f2);

for (const auto& file : {f1 / "f1.bin", f2 / "f2.bin"}) {
std::ofstream stream(file, std::ios::binary);
ASSERT_TRUE(stream.good());

stream.seekp(static_cast<std::streamoff>(cFileSize) - 1);
stream.put('\0');
}

auto [size, err] = CalculateSize(root.string());

ASSERT_EQ(err, aos::ErrorEnum::eNone);
EXPECT_EQ(size, 2 * cFileSize);
EXPECT_GT(size, static_cast<uintmax_t>(std::numeric_limits<int>::max()));
}

} // namespace aos::common::utils
2 changes: 1 addition & 1 deletion src/iam/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set(TARGET_NAME app)
# Sources
# ######################################################################################################################

set(SOURCES app.cpp main.cpp)
set(SOURCES aoscore.cpp app.cpp main.cpp)

# ######################################################################################################################
# Libraries
Expand Down
Loading
Loading