Skip to content

Commit 773b3a1

Browse files
authored
Replace assert() with baselibs futurecpp assert macros (#279)
Replaces all 37 plain assert() calls across 10 source files with the appropriate SCORE_LANGUAGE_FUTURECPP_* macros from score/assert.hpp, as required by project guidelines. Also adds the explicit @score_baselibs//score/language/futurecpp dep to 7 Bazel targets that now directly depend on the assert header. Macro selection follows existing codebase convention: - PRECONDITION[_PRD][_MESSAGE] for constructor/function-entry argument checks - ASSERT_PRD[_MESSAGE] for mid-function invariants in the safety-critical daemon - Default-level PRECONDITION (no PRD) for health_monitor, matching sibling files The _PRD variants guarantee checks survive any future NDEBUG or ASSERT_LEVEL_PRODUCTION build configuration, which is appropriate for the alive_monitor daemon (AUTOSAR PHM code referencing SWS_PHM_00204). Closes #263
1 parent caf5a18 commit 773b3a1

17 files changed

Lines changed: 238 additions & 143 deletions

File tree

score/health_monitor/src/cpp/common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifndef SCORE_HM_COMMON_H
1414
#define SCORE_HM_COMMON_H
1515

16-
#include <cassert>
16+
#include <score/assert.hpp>
1717
#include <chrono>
1818
#include <optional>
1919

@@ -95,7 +95,7 @@ class TimeRange
9595
public:
9696
TimeRange(std::chrono::milliseconds min_ms, std::chrono::milliseconds max_ms) : min_ms_(min_ms), max_ms_(max_ms)
9797
{
98-
assert(min_ms_ <= max_ms_);
98+
SCORE_LANGUAGE_FUTURECPP_PRECONDITION(min_ms_ <= max_ms_);
9999
}
100100

101101
const uint32_t min_ms() const

score/launch_manager/src/daemon/src/alive_monitor/details/common/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ cc_library(
2626
include_prefix = "score/mw/launch_manager/alive_monitor/details/common",
2727
strip_include_prefix = "/score/launch_manager/src/daemon/src/alive_monitor/details/common",
2828
visibility = ["//score/launch_manager/src/daemon/src/alive_monitor:__subpackages__"],
29+
deps = ["@score_baselibs//score/language/futurecpp"],
2930
)
3031

3132
cc_library(

score/launch_manager/src/daemon/src/alive_monitor/details/common/Observer.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#ifndef OBSERVER_HPP_INCLUDED
1414
#define OBSERVER_HPP_INCLUDED
1515

16+
#include <score/assert.hpp>
1617
#include <algorithm>
17-
#include <cassert>
1818
#include <cstdint>
1919

2020
#include <vector>
@@ -38,7 +38,7 @@ namespace common
3838
template <typename Type_Observable>
3939
class Observer
4040
{
41-
public:
41+
public:
4242
/// @brief Constructor
4343
Observer() = default;
4444

@@ -57,7 +57,7 @@ class Observer
5757
/// @param [in] f_observable_r Observable as reference.
5858
virtual void updateData(const Type_Observable& f_observable_r) noexcept(true) = 0;
5959

60-
protected:
60+
protected:
6161
/// @brief Move Constructor
6262
Observer(Observer&&) = default;
6363
};
@@ -72,7 +72,7 @@ class Observer
7272
template <typename Type_Observable>
7373
class Observable
7474
{
75-
public:
75+
public:
7676
/// @brief Default Constructor
7777
Observable(void) = default;
7878

@@ -107,7 +107,7 @@ class Observable
107107
observers.erase(eraseFirstItConst, observers.cend());
108108
}
109109

110-
protected:
110+
protected:
111111
/// @brief Move Constructor
112112
/// Cannot be noexcept, since the std::vector move constructor is not noexcept
113113
Observable(Observable&&) = default;
@@ -120,12 +120,12 @@ class Observable
120120
{
121121
// We can be sure that *this is of type Type_Observable, anything else would be a programming error.
122122
// The runtime checks performed by dynamic_cast are not necessary.
123-
assert((dynamic_cast<Type_Observable*>(this)) != NULL);
123+
SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD((dynamic_cast<Type_Observable*>(this)) != NULL);
124124
observer->updateData(static_cast<Type_Observable&>(*this));
125125
}
126126
}
127127

128-
private:
128+
private:
129129
/// Observers attached to the observable object
130130
std::vector<Observer<Type_Observable>*> observers{};
131131
};

score/launch_manager/src/daemon/src/alive_monitor/details/daemon/AliveMonitorImpl.cpp

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,71 @@
1515
#include <cstdint>
1616
#include <iostream>
1717

18+
#include <score/assert.hpp>
19+
1820
#include "score/mw/launch_manager/alive_monitor/details/daemon/AliveMonitorImpl.hpp"
1921
#include "score/mw/launch_manager/alive_monitor/details/logging/PhmLogger.hpp"
2022
#include "score/mw/launch_manager/alive_monitor/details/watchdog/WatchdogImpl.hpp"
2123

22-
namespace score {
23-
namespace lcm {
24-
namespace saf {
25-
namespace daemon {
24+
namespace score
25+
{
26+
namespace lcm
27+
{
28+
namespace saf
29+
{
30+
namespace daemon
31+
{
2632

27-
AliveMonitorImpl::AliveMonitorImpl(std::shared_ptr<score::lcm::IRecoveryClient> recovery_client, std::unique_ptr<watchdog::IWatchdogIf> watchdog, std::unique_ptr<score::lcm::IProcessStateReceiver> process_state_receiver)
28-
: m_recovery_client(recovery_client), m_watchdog(std::move(watchdog)), m_logger{score::lcm::saf::logging::PhmLogger::getLogger(score::lcm::saf::logging::PhmLogger::EContext::factory)}, m_process_state_receiver{std::move(process_state_receiver)} {}
33+
AliveMonitorImpl::AliveMonitorImpl(std::shared_ptr<score::lcm::IRecoveryClient> recovery_client,
34+
std::unique_ptr<watchdog::IWatchdogIf> watchdog,
35+
std::unique_ptr<score::lcm::IProcessStateReceiver> process_state_receiver)
36+
: m_recovery_client(recovery_client),
37+
m_watchdog(std::move(watchdog)),
38+
m_logger{score::lcm::saf::logging::PhmLogger::getLogger(score::lcm::saf::logging::PhmLogger::EContext::factory)},
39+
m_process_state_receiver{std::move(process_state_receiver)}
40+
{
41+
}
2942

30-
EInitCode AliveMonitorImpl::init() noexcept {
43+
EInitCode AliveMonitorImpl::init() noexcept
44+
{
3145
score::lcm::saf::daemon::EInitCode initResult{score::lcm::saf::daemon::EInitCode::kGeneralError};
32-
try {
46+
try
47+
{
3348
m_osClock.startMeasurement();
3449

35-
m_daemon = std::make_unique<score::lcm::saf::daemon::PhmDaemon>(m_osClock, m_logger, std::move(m_watchdog), std::move(m_process_state_receiver));
50+
m_daemon = std::make_unique<score::lcm::saf::daemon::PhmDaemon>(
51+
m_osClock, m_logger, std::move(m_watchdog), std::move(m_process_state_receiver));
3652
initResult = m_daemon->init(m_recovery_client);
3753

38-
if (initResult == score::lcm::saf::daemon::EInitCode::kNoError) {
54+
if (initResult == score::lcm::saf::daemon::EInitCode::kNoError)
55+
{
3956
const long ms{m_osClock.endMeasurement()};
4057
m_logger.LogDebug() << "AliveMonitor: Initialization took " << ms << " ms";
41-
} else {
42-
m_logger.LogError() << "AliveMonitor: Initialization failed with error code:" << static_cast<int>(initResult);
4358
}
44-
} catch (const std::exception& e) {
59+
else
60+
{
61+
m_logger.LogError() << "AliveMonitor: Initialization failed with error code:"
62+
<< static_cast<int>(initResult);
63+
}
64+
}
65+
catch (const std::exception& e)
66+
{
4567
std::cerr << "AliveMonitor: Initialization failed due to standard exception: " << e.what() << ".\n";
4668
initResult = EInitCode::kGeneralError;
47-
} catch (...) {
69+
}
70+
catch (...)
71+
{
4872
std::cerr << "AliveMonitor: Initialization failed due to exception!\n";
4973
initResult = EInitCode::kGeneralError;
5074
}
5175

5276
return initResult;
5377
}
5478

55-
bool AliveMonitorImpl::run(std::atomic_bool& cancel_thread) noexcept {
56-
assert(m_daemon != nullptr && "HealthMonitor: Instance is not initialized!");
79+
bool AliveMonitorImpl::run(std::atomic_bool& cancel_thread) noexcept
80+
{
81+
SCORE_LANGUAGE_FUTURECPP_PRECONDITION_PRD_MESSAGE(m_daemon != nullptr,
82+
"HealthMonitor: Instance is not initialized!");
5783
return m_daemon->startCyclicExec(cancel_thread);
5884
}
5985

score/launch_manager/src/daemon/src/alive_monitor/details/daemon/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,6 @@ cc_library(
8686
":i_health_monitor",
8787
"//score/launch_manager/src/daemon/src/alive_monitor/details/logging:phm_logging",
8888
"//score/launch_manager/src/daemon/src/alive_monitor/details/watchdog:watchdog_impl",
89+
"@score_baselibs//score/language/futurecpp",
8990
],
9091
)

score/launch_manager/src/daemon/src/alive_monitor/details/factory/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ cc_library(
5151
"//score/launch_manager/src/daemon/src/alive_monitor/details/timers:timers_os_clock",
5252
"//score/launch_manager/src/daemon/src/alive_monitor/details/watchdog:i_device_config_factory",
5353
"@score_baselibs//score/flatbuffers:flatbufferscpp",
54+
"@score_baselibs//score/language/futurecpp",
5455
] + select({
5556
"@platforms//os:qnx": [],
5657
"@platforms//os:linux": ["//externals/acl"],

score/launch_manager/src/daemon/src/alive_monitor/details/factory/MachineConfigFactory.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
********************************************************************************/
1313
#include "score/mw/launch_manager/alive_monitor/details/factory/MachineConfigFactory.hpp"
1414

15+
#include <fstream>
1516
#include <limits>
1617
#include <string_view>
17-
#include <fstream>
18-
#include "score/mw/launch_manager/alive_monitor/details/timers/TimeConversion.hpp"
19-
#include "score/mw/launch_manager/alive_monitor/config/hmcore_flatcfg_generated.h"
20-
#include "flatbuffers/flatbuffers.h"
2118

19+
#include "flatbuffers/flatbuffers.h"
20+
#include "score/mw/launch_manager/alive_monitor/config/hmcore_flatcfg_generated.h"
21+
#include "score/mw/launch_manager/alive_monitor/details/timers/TimeConversion.hpp"
22+
#include <score/assert.hpp>
2223

2324
namespace score
2425
{
@@ -33,7 +34,7 @@ namespace
3334
{
3435
/// @brief Prefix for all log messages
3536
// coverity[autosar_cpp14_a2_10_4_violation:FALSE] Empty namespace ensures uniqueness for cpp file scope
36-
static constexpr char const* kLogPrefix{"Factory for FlatCfg MachineConfig:"};
37+
static constexpr const char* kLogPrefix{"Factory for FlatCfg MachineConfig:"};
3738

3839
/// @brief Update a field in case the provided value is not the flatbuffer default value
3940
/// @note In case of optional integer values in flatbuffer files, the flatbuffer API will just return 0 if the value was
@@ -50,12 +51,14 @@ void updateNonDefaultValue(std::uint16_t& f_field_r, const std::uint16_t f_value
5051
}
5152
}
5253

53-
std::unique_ptr<char[]> read_flatbuffer_file(const std::string& f_filename_r) {
54+
std::unique_ptr<char[]> read_flatbuffer_file(const std::string& f_filename_r)
55+
{
5456
const std::string configFilePath = std::string("etc/") + f_filename_r.c_str();
5557

5658
std::ifstream infile;
5759
infile.open(configFilePath, std::ios::binary | std::ios::in);
58-
if (!infile.is_open()) {
60+
if (!infile.is_open())
61+
{
5962
return nullptr;
6063
}
6164
infile.seekg(0, std::ios::end);
@@ -68,14 +71,13 @@ std::unique_ptr<char[]> read_flatbuffer_file(const std::string& f_filename_r) {
6871
}
6972
} // namespace
7073

71-
MachineConfigFactory::MachineConfigFactory() noexcept(true) : watchdog::IDeviceConfigFactory()
72-
{
73-
}
74+
MachineConfigFactory::MachineConfigFactory() noexcept(true) : watchdog::IDeviceConfigFactory() {}
7475

7576
bool MachineConfigFactory::init() noexcept(false)
7677
{
7778
std::unique_ptr<char[]> loadBuffer_p = read_flatbuffer_file("hmcore.bin");
78-
if(!loadBuffer_p) {
79+
if (!loadBuffer_p)
80+
{
7981
logger_r.LogInfo() << kLogPrefix << "No HM Machine Configuration found. Using default configuration.";
8082
logConfiguration();
8183
return true;
@@ -116,7 +118,7 @@ void MachineConfigFactory::loadWatchdogDevices(const HMCOREFlatBuffer::HMCOREEcu
116118
{
117119
watchdog::DeviceConfig config{};
118120

119-
assert(wdg->maxTimeout() <= std::numeric_limits<std::uint16_t>::max());
121+
SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(wdg->maxTimeout() <= std::numeric_limits<std::uint16_t>::max());
120122
// coverity[autosar_cpp14_a4_7_1_violation] SDG definitions guarantee uint16 boundaries
121123
config.timeoutMax = static_cast<std::uint16_t>(wdg->maxTimeout());
122124

@@ -161,8 +163,8 @@ void MachineConfigFactory::loadHmSettings(const HMCOREFlatBuffer::HMCOREEcuCfg&
161163
}
162164
}
163165

164-
std::optional<watchdog::IDeviceConfigFactory::DeviceConfigurations>
165-
MachineConfigFactory::getDeviceConfigurations() const
166+
std::optional<watchdog::IDeviceConfigFactory::DeviceConfigurations> MachineConfigFactory::getDeviceConfigurations()
167+
const
166168
{
167169
return watchdogConfigs;
168170
}
@@ -180,7 +182,8 @@ const MachineConfigFactory::SupervisionBufferConfig& MachineConfigFactory::getSu
180182

181183
void MachineConfigFactory::logConfiguration() noexcept(true)
182184
{
183-
/* RULECHECKER_comment(0, 18, check_conditional_as_sub_expression, "Ternary operation is very simple", true_no_defect) */
185+
/* RULECHECKER_comment(0, 18, check_conditional_as_sub_expression, "Ternary operation is very simple",
186+
* true_no_defect) */
184187
logger_r.LogDebug() << kLogPrefix << "Alive Supervision buffer size:" << supBufferCfg.bufferSizeAliveSupervision;
185188
logger_r.LogDebug() << kLogPrefix << "Monitor buffer size:" << supBufferCfg.bufferSizeMonitor;
186189
logger_r.LogDebug() << kLogPrefix << "Periodicity:" << getCycleTimeInNs() << "ns";
@@ -195,7 +198,8 @@ void MachineConfigFactory::logConfiguration() noexcept(true)
195198
logger_r.LogDebug() << kLogPrefix << "Watchdog" << wdgCount << "- needs magic close:" << wdgMagicCloseBool;
196199
logger_r.LogDebug() << kLogPrefix << "Watchdog" << wdgCount
197200
<< "- deactivate on hm shutdown:" << wdgDeactivatedBool;
198-
// coverity[autosar_cpp14_a4_7_1_violation] Value limited by amount of watchdog configurations, which is smaller.
201+
// coverity[autosar_cpp14_a4_7_1_violation] Value limited by amount of watchdog configurations, which is
202+
// smaller.
199203
++wdgCount;
200204
}
201205

score/launch_manager/src/daemon/src/alive_monitor/details/supervision/Alive.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include "score/mw/launch_manager/alive_monitor/details/supervision/Alive.hpp"
1515

16-
#include <cassert>
16+
#include <score/assert.hpp>
1717
#include <string_view>
1818

1919
#include "score/mw/launch_manager/alive_monitor/details/common/Types.hpp"
@@ -43,12 +43,14 @@ Alive::Alive(const AliveSupervisionCfg& f_aliveCfg_r)
4343
timeSortingUpdateEventBuffer(common::TimeSortingBuffer<TimeSortedUpdateEvent>(f_aliveCfg_r.checkpointBufferSize))
4444
{
4545
f_aliveCfg_r.checkpoint_r.attachObserver(*this);
46-
assert((k_aliveReferenceCycle != 0U) && "k_aliveReferenceCycle=0 causes infinite loop during evaluation.");
46+
SCORE_LANGUAGE_FUTURECPP_PRECONDITION_PRD_MESSAGE(
47+
(k_aliveReferenceCycle != 0U), "k_aliveReferenceCycle=0 causes infinite loop during evaluation.");
4748

48-
assert((aliveStatus == EStatus::kDeactivated) &&
49-
("Alive Supervision must start in deactivated state, see SWS_PHM_00204"));
49+
SCORE_LANGUAGE_FUTURECPP_PRECONDITION_PRD_MESSAGE(
50+
(aliveStatus == EStatus::kDeactivated), "Alive Supervision must start in deactivated state, see SWS_PHM_00204");
5051

51-
assert((recoveryClient_p != nullptr) && "Recovery client must be provided");
52+
SCORE_LANGUAGE_FUTURECPP_PRECONDITION_PRD_MESSAGE((recoveryClient_p != nullptr),
53+
"Recovery client must be provided");
5254
}
5355

5456
// coverity[exn_spec_violation:FALSE] std::length_error is not thrown from push() which uses fixed-size-vector
@@ -78,10 +80,9 @@ void Alive::updateData(const ifexm::ProcessState& f_observable_r) noexcept(true)
7880
{
7981
const ifexm::ProcessState::EProcState state{f_observable_r.getState()};
8082

81-
const bool isRelevant =
82-
(state == ifexm::ProcessState::EProcState::running) ||
83-
(state == ifexm::ProcessState::EProcState::sigterm) ||
84-
(state == ifexm::ProcessState::EProcState::off);
83+
const bool isRelevant = (state == ifexm::ProcessState::EProcState::running) ||
84+
(state == ifexm::ProcessState::EProcState::sigterm) ||
85+
(state == ifexm::ProcessState::EProcState::off);
8586

8687
if (isRelevant)
8788
{
@@ -122,8 +123,9 @@ void Alive::evaluate(const timers::NanoSecondType f_syncTimestamp)
122123
while (sortedUpdateEvent_p != nullptr)
123124
{
124125
timers::NanoSecondType timestampOfUpdateEvent{getTimestampOfUpdateEvent(*sortedUpdateEvent_p)};
125-
assert((timestampOfUpdateEvent <= f_syncTimestamp) &&
126-
"Alive supervision: Checkpoint events are reported beyond syncTimestamp.");
126+
SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD_MESSAGE(
127+
(timestampOfUpdateEvent <= f_syncTimestamp),
128+
"Alive supervision: Checkpoint events are reported beyond syncTimestamp.");
127129

128130
// Check if evaluation is to be triggered before processing current sorted update event
129131
const bool isEvaluationEvent{detectEvaluationEvent(timestampOfUpdateEvent, *sortedUpdateEvent_p)};
@@ -290,7 +292,7 @@ Alive::EUpdateEventType Alive::getAliveEventType(bool f_isEvaluationEvent,
290292
}
291293

292294
// SyncSnapshot
293-
assert(std::holds_alternative<SyncSnapshot>(f_updateEvent));
295+
SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(std::holds_alternative<SyncSnapshot>(f_updateEvent));
294296
return EUpdateEventType::kSync;
295297
}
296298

@@ -489,7 +491,7 @@ void Alive::switchToExpired(Alive::EReason reason) noexcept(true)
489491
<< ") switched to EXPIRED, due to buffer overflow.";
490492
break;
491493
default:
492-
assert(dataLossReason != EDataLossReason::kNoDataLoss);
494+
SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(dataLossReason != EDataLossReason::kNoDataLoss);
493495
logger_r.LogError() << "Alive Supervision (" << getConfigName()
494496
<< ") switched to EXPIRED, due to unknown data loss case.";
495497
break;
@@ -593,7 +595,7 @@ timers::NanoSecondType Alive::getTimestampOfUpdateEvent(const TimeSortedUpdateEv
593595
}
594596
else
595597
{
596-
assert(std::holds_alternative<SyncSnapshot>(f_updateEvent));
598+
SCORE_LANGUAGE_FUTURECPP_ASSERT_PRD(std::holds_alternative<SyncSnapshot>(f_updateEvent));
597599
// coverity[cert_exp34_c_violation] SyncSnapshot type is stored also check assert above
598600
// coverity[dereference] SyncSnapshot type is stored also check assert above
599601
timestamp = std::get<SyncSnapshot>(f_updateEvent);

score/launch_manager/src/daemon/src/alive_monitor/details/supervision/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ cc_library(
5353
"//score/launch_manager/src/daemon/src/alive_monitor/details/logging:phm_logging",
5454
"//score/launch_manager/src/daemon/src/alive_monitor/details/timers:timers_os_clock",
5555
"//score/launch_manager/src/daemon/src/recovery_client",
56+
"@score_baselibs//score/language/futurecpp",
5657
],
5758
)
5859

score/launch_manager/src/daemon/src/alive_monitor/details/watchdog/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ cc_library(
4444
include_prefix = "score/mw/launch_manager/alive_monitor/details/watchdog",
4545
strip_include_prefix = "/score/launch_manager/src/daemon/src/alive_monitor/details/watchdog",
4646
visibility = ["//score/launch_manager/src/daemon/src/alive_monitor:__subpackages__"],
47+
deps = ["@score_baselibs//score/language/futurecpp"],
4748
)
4849

4950
cc_library(
@@ -60,5 +61,6 @@ cc_library(
6061
":watchdog",
6162
"//score/launch_manager/src/daemon/src/alive_monitor/details/logging:phm_logging",
6263
"//score/launch_manager/src/daemon/src/alive_monitor/details/timers:os_clock_interface",
64+
"@score_baselibs//score/language/futurecpp",
6365
],
6466
)

0 commit comments

Comments
 (0)