Skip to content

Commit 647f2c3

Browse files
committed
ffi support and tests
1 parent 7f49dba commit 647f2c3

6 files changed

Lines changed: 91 additions & 3 deletions

File tree

score/health_monitor/src/cpp/health_monitor.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ using namespace score::mw::health::logic;
2626
FFICode health_monitor_builder_create(FFIHandle* health_monitor_builder_handle_out);
2727
FFICode health_monitor_builder_destroy(FFIHandle health_monitor_builder_handle);
2828
FFICode health_monitor_builder_build(FFIHandle health_monitor_builder_handle,
29+
const size_t* allocator_capacity,
2930
const uint64_t* supervisor_cycle_ms,
3031
const uint64_t* internal_cycle_ms,
3132
FFIHandle thread_parameters_handle,
@@ -114,6 +115,12 @@ HealthMonitorBuilder HealthMonitorBuilder::add_logic_monitor(const MonitorTag& m
114115
return std::move(*this);
115116
}
116117

118+
HealthMonitorBuilder HealthMonitorBuilder::allocator_capacity(size_t allocator_capacity) &&
119+
{
120+
allocator_capacity_ = allocator_capacity;
121+
return std::move(*this);
122+
}
123+
117124
HealthMonitorBuilder HealthMonitorBuilder::with_internal_processing_cycle(std::chrono::milliseconds cycle_duration) &&
118125
{
119126
auto count{cycle_duration.count()};
@@ -142,8 +149,13 @@ score::cpp::expected<HealthMonitor, Error> HealthMonitorBuilder::build() &&
142149
SCORE_LANGUAGE_FUTURECPP_PRECONDITION(health_monitor_builder_handle.has_value());
143150

144151
// Handle optional parameters.
152+
const size_t* allocator_capacity{nullptr};
145153
const uint64_t* supervisor_api_cycle_ms{nullptr};
146154
const uint64_t* internal_processing_cycle_ms{nullptr};
155+
if (allocator_capacity_.has_value())
156+
{
157+
allocator_capacity = &allocator_capacity_.value();
158+
}
147159
if (supervisor_api_cycle_ms_.has_value())
148160
{
149161
supervisor_api_cycle_ms = &supervisor_api_cycle_ms_.value();
@@ -164,6 +176,7 @@ score::cpp::expected<HealthMonitor, Error> HealthMonitorBuilder::build() &&
164176

165177
FFIHandle health_monitor_handle{nullptr};
166178
auto result{health_monitor_builder_build(health_monitor_builder_handle.value(),
179+
allocator_capacity,
167180
supervisor_api_cycle_ms,
168181
internal_processing_cycle_ms,
169182
thread_parameters_handle,

score/health_monitor/src/cpp/health_monitor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class HealthMonitorBuilder final
5252
/// Adds a logic monitor for a specific identifier tag.
5353
HealthMonitorBuilder add_logic_monitor(const MonitorTag& monitor_tag, logic::LogicMonitorBuilder&& monitor) &&;
5454

55+
/// Set protected memory allocator capacity.
56+
HealthMonitorBuilder allocator_capacity(size_t allocator_capacity) &&;
57+
5558
/// Sets the cycle duration for supervisor API notifications.
5659
/// This duration determines how often the health monitor notifies the supervisor that the system is alive.
5760
HealthMonitorBuilder with_supervisor_api_cycle(std::chrono::milliseconds cycle_duration) &&;
@@ -69,6 +72,7 @@ class HealthMonitorBuilder final
6972
private:
7073
internal::DroppableFFIHandle health_monitor_builder_handle_;
7174

75+
std::optional<size_t> allocator_capacity_;
7276
std::optional<uint64_t> supervisor_api_cycle_ms_;
7377
std::optional<uint64_t> internal_processing_cycle_ms_;
7478
std::optional<ThreadParameters> thread_parameters_;

score/health_monitor/src/rust/deadline/ffi.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ mod tests {
337337
null_mut(),
338338
null_mut(),
339339
null_mut(),
340+
null_mut(),
340341
&mut health_monitor_handle as *mut FFIHandle,
341342
);
342343
let _ = health_monitor_get_deadline_monitor(
@@ -387,6 +388,7 @@ mod tests {
387388
null_mut(),
388389
null_mut(),
389390
null_mut(),
391+
null_mut(),
390392
&mut health_monitor_handle as *mut FFIHandle,
391393
);
392394
let _ = health_monitor_get_deadline_monitor(
@@ -444,6 +446,7 @@ mod tests {
444446
null_mut(),
445447
null_mut(),
446448
null_mut(),
449+
null_mut(),
447450
&mut health_monitor_handle as *mut FFIHandle,
448451
);
449452
let _ = health_monitor_get_deadline_monitor(
@@ -491,6 +494,7 @@ mod tests {
491494
null_mut(),
492495
null_mut(),
493496
null_mut(),
497+
null_mut(),
494498
&mut health_monitor_handle as *mut FFIHandle,
495499
);
496500
let _ = health_monitor_get_deadline_monitor(
@@ -542,6 +546,7 @@ mod tests {
542546
null_mut(),
543547
null_mut(),
544548
null_mut(),
549+
null_mut(),
545550
&mut health_monitor_handle as *mut FFIHandle,
546551
);
547552
let _ = health_monitor_get_deadline_monitor(
@@ -592,6 +597,7 @@ mod tests {
592597
null_mut(),
593598
null_mut(),
594599
null_mut(),
600+
null_mut(),
595601
&mut health_monitor_handle as *mut FFIHandle,
596602
);
597603
let _ = health_monitor_get_deadline_monitor(
@@ -649,6 +655,7 @@ mod tests {
649655
null_mut(),
650656
null_mut(),
651657
null_mut(),
658+
null_mut(),
652659
&mut health_monitor_handle as *mut FFIHandle,
653660
);
654661
let _ = health_monitor_get_deadline_monitor(

score/health_monitor/src/rust/ffi.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ use core::ops::{Deref, DerefMut};
2222
use core::time::Duration;
2323
use score_log::ScoreDebug;
2424

25-
// TODO: add support for protected memory allocator.
26-
2725
pub type FFIHandle = *mut core::ffi::c_void;
2826

2927
/// FFI return codes.
@@ -112,6 +110,7 @@ pub extern "C" fn health_monitor_builder_destroy(health_monitor_builder_handle:
112110
#[unsafe(no_mangle)]
113111
pub extern "C" fn health_monitor_builder_build(
114112
health_monitor_builder_handle: FFIHandle,
113+
allocator_capacity: *const usize,
115114
supervisor_cycle_ms: *const u64,
116115
internal_cycle_ms: *const u64,
117116
thread_parameters_handle: FFIHandle,
@@ -129,8 +128,12 @@ pub extern "C" fn health_monitor_builder_build(
129128
unsafe { Box::from_raw(health_monitor_builder_handle as *mut HealthMonitorBuilder) };
130129

131130
// SAFETY:
132-
// `supervisor_cycle_ms` and `internal_cycle_ms` must be a valid pointer when non-null.
131+
// `allocator_capacity`, `supervisor_cycle_ms` and `internal_cycle_ms` must be a valid pointer when non-null.
133132
// Values are considered unset on null.
133+
if !allocator_capacity.is_null() {
134+
let allocator_capacity = unsafe { *allocator_capacity };
135+
health_monitor_builder.allocator_capacity_internal(allocator_capacity);
136+
}
134137
if !internal_cycle_ms.is_null() {
135138
let internal_cycle_ms = Duration::from_millis(unsafe { *internal_cycle_ms });
136139
health_monitor_builder.with_internal_processing_cycle_internal(internal_cycle_ms);
@@ -482,6 +485,7 @@ mod tests {
482485
null_mut(),
483486
null_mut(),
484487
null_mut(),
488+
null_mut(),
485489
&mut health_monitor_handle as *mut FFIHandle,
486490
);
487491
assert!(!health_monitor_handle.is_null());
@@ -512,6 +516,7 @@ mod tests {
512516
let internal_cycle_ms = 100u64;
513517
let health_monitor_builder_build_result = health_monitor_builder_build(
514518
health_monitor_builder_handle,
519+
null_mut(),
515520
&supervisor_cycle_ms as *const _,
516521
&internal_cycle_ms as *const _,
517522
null_mut(),
@@ -526,6 +531,36 @@ mod tests {
526531
assert_eq!(health_monitor_destroy_result, FFICode::Success);
527532
}
528533

534+
#[test]
535+
fn health_monitor_builder_build_invalid_allocator_capacity() {
536+
let mut health_monitor_builder_handle: FFIHandle = null_mut();
537+
let mut health_monitor_handle: FFIHandle = null_mut();
538+
let mut deadline_monitor_builder_handle = null_mut();
539+
540+
let _ = health_monitor_builder_create(&mut health_monitor_builder_handle as *mut FFIHandle);
541+
let deadline_monitor_tag = MonitorTag::from("deadline_monitor");
542+
let _ = deadline_monitor_builder_create(&mut deadline_monitor_builder_handle as *mut FFIHandle);
543+
let _ = health_monitor_builder_add_deadline_monitor(
544+
health_monitor_builder_handle,
545+
&deadline_monitor_tag as *const MonitorTag,
546+
deadline_monitor_builder_handle,
547+
);
548+
549+
let allocator_capacity = 0usize;
550+
let health_monitor_builder_build_result = health_monitor_builder_build(
551+
health_monitor_builder_handle,
552+
&allocator_capacity as *const _,
553+
null_mut(),
554+
null_mut(),
555+
null_mut(),
556+
&mut health_monitor_handle as *mut FFIHandle,
557+
);
558+
assert!(health_monitor_handle.is_null());
559+
assert_eq!(health_monitor_builder_build_result, FFICode::InvalidArgument);
560+
561+
// Clean-up not needed - health monitor builder was already consumed by the `build`.
562+
}
563+
529564
#[test]
530565
fn health_monitor_builder_build_invalid_cycle_intervals() {
531566
let mut health_monitor_builder_handle: FFIHandle = null_mut();
@@ -537,6 +572,7 @@ mod tests {
537572
let internal_cycle_ms = 100u64;
538573
let health_monitor_builder_build_result = health_monitor_builder_build(
539574
health_monitor_builder_handle,
575+
null_mut(),
540576
&supervisor_cycle_ms as *const _,
541577
&internal_cycle_ms as *const _,
542578
null_mut(),
@@ -560,6 +596,7 @@ mod tests {
560596
null_mut(),
561597
null_mut(),
562598
null_mut(),
599+
null_mut(),
563600
&mut health_monitor_handle as *mut FFIHandle,
564601
);
565602
assert_eq!(health_monitor_builder_build_result, FFICode::WrongState);
@@ -576,6 +613,7 @@ mod tests {
576613
null_mut(),
577614
null_mut(),
578615
null_mut(),
616+
null_mut(),
579617
&mut health_monitor_handle as *mut FFIHandle,
580618
);
581619
assert!(health_monitor_handle.is_null());
@@ -594,6 +632,7 @@ mod tests {
594632
null_mut(),
595633
null_mut(),
596634
null_mut(),
635+
null_mut(),
597636
);
598637
assert_eq!(health_monitor_builder_build_result, FFICode::NullParameter);
599638

@@ -623,6 +662,7 @@ mod tests {
623662
health_monitor_builder_handle,
624663
null_mut(),
625664
null_mut(),
665+
null_mut(),
626666
thread_parameters_handle,
627667
&mut health_monitor_handle as *mut FFIHandle,
628668
);
@@ -914,6 +954,7 @@ mod tests {
914954
null_mut(),
915955
null_mut(),
916956
null_mut(),
957+
null_mut(),
917958
&mut health_monitor_handle as *mut FFIHandle,
918959
);
919960

@@ -951,6 +992,7 @@ mod tests {
951992
null_mut(),
952993
null_mut(),
953994
null_mut(),
995+
null_mut(),
954996
&mut health_monitor_handle as *mut FFIHandle,
955997
);
956998

@@ -997,6 +1039,7 @@ mod tests {
9971039
null_mut(),
9981040
null_mut(),
9991041
null_mut(),
1042+
null_mut(),
10001043
&mut health_monitor_handle as *mut FFIHandle,
10011044
);
10021045

@@ -1032,6 +1075,7 @@ mod tests {
10321075
null_mut(),
10331076
null_mut(),
10341077
null_mut(),
1078+
null_mut(),
10351079
&mut health_monitor_handle as *mut FFIHandle,
10361080
);
10371081

@@ -1066,6 +1110,7 @@ mod tests {
10661110
null_mut(),
10671111
null_mut(),
10681112
null_mut(),
1113+
null_mut(),
10691114
&mut health_monitor_handle as *mut FFIHandle,
10701115
);
10711116

@@ -1100,6 +1145,7 @@ mod tests {
11001145
null_mut(),
11011146
null_mut(),
11021147
null_mut(),
1148+
null_mut(),
11031149
&mut health_monitor_handle as *mut FFIHandle,
11041150
);
11051151

@@ -1137,6 +1183,7 @@ mod tests {
11371183
null_mut(),
11381184
null_mut(),
11391185
null_mut(),
1186+
null_mut(),
11401187
&mut health_monitor_handle as *mut FFIHandle,
11411188
);
11421189

@@ -1183,6 +1230,7 @@ mod tests {
11831230
null_mut(),
11841231
null_mut(),
11851232
null_mut(),
1233+
null_mut(),
11861234
&mut health_monitor_handle as *mut FFIHandle,
11871235
);
11881236

@@ -1218,6 +1266,7 @@ mod tests {
12181266
null_mut(),
12191267
null_mut(),
12201268
null_mut(),
1269+
null_mut(),
12211270
&mut health_monitor_handle as *mut FFIHandle,
12221271
);
12231272

@@ -1252,6 +1301,7 @@ mod tests {
12521301
null_mut(),
12531302
null_mut(),
12541303
null_mut(),
1304+
null_mut(),
12551305
&mut health_monitor_handle as *mut FFIHandle,
12561306
);
12571307

@@ -1285,6 +1335,7 @@ mod tests {
12851335
null_mut(),
12861336
null_mut(),
12871337
null_mut(),
1338+
null_mut(),
12881339
&mut health_monitor_handle as *mut FFIHandle,
12891340
);
12901341

@@ -1321,6 +1372,7 @@ mod tests {
13211372
null_mut(),
13221373
null_mut(),
13231374
null_mut(),
1375+
null_mut(),
13241376
&mut health_monitor_handle as *mut FFIHandle,
13251377
);
13261378

@@ -1366,6 +1418,7 @@ mod tests {
13661418
null_mut(),
13671419
null_mut(),
13681420
null_mut(),
1421+
null_mut(),
13691422
&mut health_monitor_handle as *mut FFIHandle,
13701423
);
13711424

@@ -1400,6 +1453,7 @@ mod tests {
14001453
null_mut(),
14011454
null_mut(),
14021455
null_mut(),
1456+
null_mut(),
14031457
&mut health_monitor_handle as *mut FFIHandle,
14041458
);
14051459

@@ -1433,6 +1487,7 @@ mod tests {
14331487
null_mut(),
14341488
null_mut(),
14351489
null_mut(),
1490+
null_mut(),
14361491
&mut health_monitor_handle as *mut FFIHandle,
14371492
);
14381493

@@ -1468,6 +1523,7 @@ mod tests {
14681523
null_mut(),
14691524
null_mut(),
14701525
null_mut(),
1526+
null_mut(),
14711527
&mut health_monitor_handle as *mut FFIHandle,
14721528
);
14731529

@@ -1504,6 +1560,7 @@ mod tests {
15041560
null_mut(),
15051561
null_mut(),
15061562
null_mut(),
1563+
null_mut(),
15071564
&mut health_monitor_handle as *mut FFIHandle,
15081565
);
15091566

score/health_monitor/src/rust/heartbeat/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ mod tests {
166166
null_mut(),
167167
null_mut(),
168168
null_mut(),
169+
null_mut(),
169170
&mut health_monitor_handle as *mut FFIHandle,
170171
);
171172
let _ = health_monitor_get_heartbeat_monitor(

0 commit comments

Comments
 (0)