perf(iec104): 参数区按 17 项真实消费者收窄并接上真实定值模型
104 协议侧参数通道此前镜像了厂商点表的全部 430 条参数(697 槽), 但其中 413 条在本设备上没有任何消费者——104 与设备侧真实定值模型 prot_set 之间零调用,主站改定值只落在无人读的 RAM 镜像里,保护功能 毫无感知。本次按"严格按消费者收窄"口径收口: 可见性以 iec104_dtu_protection_mapping() 语义桥表为唯一判据,只放行 三段过流投退/定值/延时(9 项)、低压与过压定值/延时(4 项)、PT 与 CT 一次/二次额定(4 项)。其余参数在位表枚举层即不可见,主站读取 得到 COT=47 + P/N=1 的显式否定确认,不静默丢弃。values[697]→values[53]、 staged[264]→staged[17],并删除生成式 iec104_source_ranks.h。 桥表接真实源:iec104_serv 的 parameter_read/apply 从 NULL 改为直读 prot_set 与整批原子提交,Q10 量化后一次事务落地,持久化仍由 defaultTask 的脏标志闭环负责。 prot_set 因此出现两个运行时写者(netTask 的 104 与 uiTask 的本地整 定),原"写者靠启动时序串行"的约定失效。加入写事务互斥与 seqlock 序号:四个写入口全部进锁,校验与提前返回一律在 enter 之前;序号卡 奇数连续 8 拍即判保护闭锁,避免自检被永久跳过而静默失去防篡改能力。 锁的安装在 defaultTask 的存储 bring-up 阶段,不在 app_main_init—— 后者早于 osKernelInitialize,内核启动前禁止任何 RTOS 创建类 API。 实测:RW_IRAM1 117,304→113,720 B(−3,584 B,89.50%→86.76%); ER_IROM1 413,392→411,668 B(−1,724 B);iec104_source.o 5,656→2,056 B; prot_set.o 344→352 B(+8 B,写事务序号与锁句柄)。Keil 构建 0 错误 0 警告;主机侧 prot_set 175/175、iec104_dtu 7/7;真实 TCP 验收 13/13;暂存口径门禁 18 项全通过。 已知边界:模拟器不链接 iec104_serv.c,TCP 验收不覆盖 prot_set 绑定, 板端需实机确认主站写入能反映到本地定值页;17 项中 15 项的出厂默认 值与点表字面量不同,主站读到的是真实模型的值;绑定真实源后定值区 切换一律拒绝(单套活跃值无原子整区事务)。
This commit is contained in:
@@ -22,7 +22,7 @@ static bool parameter_apply(void *ctx, const iec104_source_change_t *v, size_t n
|
||||
{
|
||||
(void)ctx; (void)v; (void)n; (void)validate; ++apply_count; return true;
|
||||
}
|
||||
static void exchange(const uint8_t *frame, size_t length, uint8_t expected_cot)
|
||||
static void exchange(const uint8_t *frame, size_t length, uint8_t expected_type, uint8_t expected_cot)
|
||||
{
|
||||
size_t start = fake.sent_bytes, at;
|
||||
unsigned i; bool found = false;
|
||||
@@ -30,7 +30,7 @@ static void exchange(const uint8_t *frame, size_t length, uint8_t expected_cot)
|
||||
for (i = 0U; i < 20U; ++i) { turn(); }
|
||||
for (at = start; at + 6U <= fake.sent_bytes; at += (size_t)fake.output[at + 1U] + 2U) {
|
||||
CHECK(fake.output[at] == 0x68U);
|
||||
if (at + 12U <= fake.sent_bytes && !(fake.output[at + 2U] & 1U) && fake.output[at + 6U] == 203U) {
|
||||
if (at + 12U <= fake.sent_bytes && !(fake.output[at + 2U] & 1U) && fake.output[at + 6U] == expected_type) {
|
||||
CHECK(fake.output[at + 8U] == expected_cot); found = true;
|
||||
}
|
||||
}
|
||||
@@ -39,16 +39,23 @@ static void exchange(const uint8_t *frame, size_t length, uint8_t expected_cot)
|
||||
static void borrowed_failure(void)
|
||||
{
|
||||
iec104_source_bindings_t bind = {0};
|
||||
iec104_source_value_t before, after;
|
||||
static const uint8_t preset[] = {0x68,19,0,0,0,0,203,1,6,17,1,0,0,0,128,0x20,0x88,0,1,1,0};
|
||||
size_t i; uint16_t area0 = 0U, area1 = 0U;
|
||||
/* While real bindings are installed, the area-0 low<=high cross-check reads
|
||||
* semantic id 17 (PS_OV_VOL) through parameter_read. The fake fails id 17,
|
||||
* so a staged 0x8027 preset must be refused at solidify with no apply. */
|
||||
static const uint8_t preset[] = {0x68,22,0,0,0,0,203,1,6,17,1,0,0,0,128,0x27,0x80,0,38,4,0,0,200,0x42};
|
||||
static const uint8_t solidify[] = {0x68,13,2,0,0,0,203,0,6,17,1,0,0,0,0};
|
||||
bind.parameter_read = parameter_read; bind.parameter_apply = parameter_apply;
|
||||
iec104_source_bind(&bind);
|
||||
CHECK(iec104_source_remote_read(0U, 0x8820U, &before));
|
||||
exchange(preset, sizeof(preset), 7U);
|
||||
exchange(solidify, sizeof(solidify), 0x47U);
|
||||
CHECK(iec104_source_remote_read(0U, 0x8820U, &after));
|
||||
CHECK(before.bytes[0] == after.bytes[0] && apply_count == 0U);
|
||||
exchange(preset, sizeof(preset), 203U, 7U);
|
||||
exchange(solidify, sizeof(solidify), 203U, 0x47U);
|
||||
CHECK(apply_count == 0U);
|
||||
for (i = 0U; i < IEC104_DTU_PARAMETER_COUNT; ++i) {
|
||||
if (iec104_dtu_parameter_visible(i, 0U)) { ++area0; }
|
||||
if (iec104_dtu_parameter_visible(i, 1U)) { ++area1; }
|
||||
}
|
||||
CHECK(area0 == IEC104_DTU_CONSUMER_COUNT);
|
||||
CHECK(area1 == IEC104_DTU_SCOPED_CONSUMER_COUNT);
|
||||
}
|
||||
int main(void)
|
||||
{
|
||||
|
||||
@@ -20,6 +20,15 @@ def reference():
|
||||
parameters[int(ioa, 16)] = (int(tag), value.strip('"'))
|
||||
assert [len(tables[x]) for x in tables] == [59, 30, 4, 19, 430]
|
||||
assert len(parameters) == 345
|
||||
# Accepted consumer subset. This list is the acceptance specification and
|
||||
# must match the semantic bridge map in iec104_dtu_table.c item for item.
|
||||
# Every other configured profile row must be refused by the protocol layer.
|
||||
consumer_ioas = frozenset((
|
||||
0x8841, 0x8842, 0x8843, 0x8845, 0x8846, 0x8847, 0x8849, 0x884A, 0x884B,
|
||||
0x8027, 0x8028, 0x8029, 0x802A, 0x8024, 0x8025, 0x8040, 0x8041,
|
||||
))
|
||||
parameters = {ioa: row for ioa, row in parameters.items() if ioa in consumer_ioas}
|
||||
assert len(parameters) == 17
|
||||
assert set(tables["signals"]) == set(range(1, 0x42)) - set(range(0x2C, 0x32))
|
||||
assert set(tables["measurements"]) == set(range(0x4001, 0x401F))
|
||||
gi = {ioa: 1 for ioa in tables["signals"]}
|
||||
|
||||
@@ -186,9 +186,9 @@ class Acceptance:
|
||||
assert self.client.response(201, 7)[9:11] == b"\x00\x00"
|
||||
|
||||
def test_typed_preset_commit_cancel_readback(self):
|
||||
addresses = (0x8820, 0x8823, 0x882D, 0x802E)
|
||||
addresses = (0x8841, 0x8842, 0x8843, 0x8027)
|
||||
before = self.remote_read(addresses)
|
||||
change = [(0x8820, 1, 0), (0x8823, 38, 8.25), (0x882D, 45, 2), (0x802E, 35, 16777217)]
|
||||
change = [(0x8841, 1, 1), (0x8842, 38, 2.5), (0x8843, 38, 0.75), (0x8027, 38, 90.0)]
|
||||
original = [(ioa, before[ioa]["tag"], before[ioa]["value"]) for ioa in addresses]
|
||||
try:
|
||||
self.preset(change)
|
||||
@@ -200,6 +200,7 @@ class Acceptance:
|
||||
assert self.remote_read(addresses) == after, "TCP reconnect lost committed RAM values"
|
||||
self.preset(original)
|
||||
self.connect()
|
||||
self.remote_read(addresses)
|
||||
self.finish(pn=True)
|
||||
assert self.remote_read(addresses) == after, "disconnected stage became active"
|
||||
self.preset(original)
|
||||
@@ -214,10 +215,10 @@ class Acceptance:
|
||||
def test_unknown_and_bad_typed_requests(self):
|
||||
self.client.request(asdu(102, u24(0xFFFFFF), cot=5))
|
||||
self.client.response(102, 47, True)
|
||||
for ioa in (0xFFFFFF, 0x9601):
|
||||
for ioa in (0xFFFFFF, 0x9601, 0x8820, 0x8823):
|
||||
self.client.request(asdu(202, b"\x00\x00" + u24(ioa)))
|
||||
self.client.response(202, 47, True)
|
||||
for value in ([(0x8820, 1, 2)], [(0x8823, 45, 8)], [(0x8823, 38, float("nan"))]):
|
||||
for value in ([(0x8841, 1, 2)], [(0x8842, 45, 8)], [(0x8842, 38, float("nan"))]):
|
||||
self.preset(value, pn=True)
|
||||
self.finish(cancel=True)
|
||||
|
||||
|
||||
@@ -21,10 +21,11 @@ class WireOracleTests(unittest.TestCase):
|
||||
self.assertEqual(len(ref["gi"]), 225)
|
||||
self.assertEqual(len(ref["read"]), 121)
|
||||
self.assertEqual(len(ref["counters"]), 8)
|
||||
self.assertEqual(len(ref["remote"]), 345)
|
||||
self.assertEqual(len(ref["remote"]), 17)
|
||||
self.assertEqual(len(ref["diagnostic_aliases_pending_scope"]), 150)
|
||||
self.assertNotIn(0x9601, ref["remote"])
|
||||
self.assertEqual(ref["remote"][0x8858], (38, "25"))
|
||||
self.assertNotIn(0x8858, ref["remote"])
|
||||
self.assertEqual(ref["remote"][0x8842], (38, "1.2"))
|
||||
|
||||
def test_pn_zero_positive_and_one_negative(self):
|
||||
self.assertFalse(header(bytes.fromhex("64010711010000000014"))["pn"])
|
||||
|
||||
@@ -116,6 +116,26 @@ static uint8_t g_prot_dirty, g_run_dirty;
|
||||
uint8_t prot_set_dirty_peek(void) { return g_prot_dirty; }
|
||||
uint8_t run_par_dirty_peek(void) { return g_run_dirty; }
|
||||
|
||||
static float g_prot_value;
|
||||
static prot_set_change_t g_last_batch[PROT_SET_BATCH_MAX];
|
||||
static size_t g_last_batch_n;
|
||||
static int g_last_validate_only, g_apply_batch_calls;
|
||||
|
||||
float prot_set_get_f(prot_set_id_t id)
|
||||
{
|
||||
(void)id;
|
||||
return g_prot_value;
|
||||
}
|
||||
int prot_set_apply_batch(const prot_set_change_t *changes, size_t n, int validate_only)
|
||||
{
|
||||
size_t i;
|
||||
g_apply_batch_calls++;
|
||||
g_last_batch_n = n;
|
||||
g_last_validate_only = validate_only;
|
||||
for (i = 0U; i < n && i < PROT_SET_BATCH_MAX; ++i) { g_last_batch[i] = changes[i]; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int g_wd_beats;
|
||||
static task_wd_id_t g_last_wd;
|
||||
void task_wd_beat(task_wd_id_t id) { g_wd_beats++; g_last_wd = id; }
|
||||
@@ -139,6 +159,11 @@ static void reset_all(void)
|
||||
g_meas_result = 0;
|
||||
memset(g_cal_flags, 0, sizeof(g_cal_flags));
|
||||
g_prot_dirty = g_run_dirty = 0u;
|
||||
g_prot_value = 0u;
|
||||
g_apply_batch_calls = 0;
|
||||
g_last_batch_n = 0u;
|
||||
g_last_validate_only = 0;
|
||||
memset(g_last_batch, 0, sizeof(g_last_batch));
|
||||
g_wd_beats = 0;
|
||||
g_i104_info_count = g_i104_error_count = 0;
|
||||
g_cfg_register_calls = g_cfg_save_calls = 0;
|
||||
@@ -161,6 +186,37 @@ static void reset_all(void)
|
||||
g_i104_info_count = g_i104_error_count = 0;
|
||||
}
|
||||
|
||||
static void tc_parameter_binding(void)
|
||||
{
|
||||
iec104_source_change_t changes[2];
|
||||
float value = 0.0f;
|
||||
|
||||
reset_all();
|
||||
CHECK(g_bound.parameter_read != NULL && g_bound.parameter_apply != NULL,
|
||||
"parameter callbacks installed as a pair");
|
||||
g_prot_value = 12.5f;
|
||||
CHECK(g_bound.parameter_read(NULL, (uint16_t)PS_ID_COUNT, &value) == false,
|
||||
"out-of-range semantic id refused");
|
||||
CHECK(g_bound.parameter_read(NULL, 2u, &value) == true && value == 12.5f,
|
||||
"valid semantic id reads live prot_set value");
|
||||
CHECK(g_bound.parameter_read(NULL, 2u, NULL) == false,
|
||||
"null output refused");
|
||||
changes[0].id = 2u; changes[0].value = 1.25f;
|
||||
changes[1].id = 3u; changes[1].value = 500.0f;
|
||||
CHECK(g_bound.parameter_apply(NULL, changes, 2u, true) == true,
|
||||
"validate-only batch accepted");
|
||||
CHECK(g_apply_batch_calls == 1 && g_last_batch_n == 2u && g_last_validate_only == 1,
|
||||
"validate-only forwarded once without commit");
|
||||
CHECK(g_last_batch[0].id == 2u && g_last_batch[0].q10 == PROT_SET_Q10(1.25f) &&
|
||||
g_last_batch[1].id == 3u && g_last_batch[1].q10 == PROT_SET_Q10(500.0f),
|
||||
"semantic values quantized into prot_set batch");
|
||||
CHECK(g_bound.parameter_apply(NULL, changes, 0u, false) == false,
|
||||
"empty batch refused");
|
||||
CHECK(g_bound.parameter_apply(NULL, changes, 2u + IEC104_DTU_CONSUMER_COUNT, false) == false,
|
||||
"over-capacity batch refused");
|
||||
CHECK(g_apply_batch_calls == 1, "rejected batches never reach prot_set");
|
||||
}
|
||||
|
||||
static void tc_monotonic(void)
|
||||
{
|
||||
uint32_t last = 100u, carry = 7u;
|
||||
@@ -381,6 +437,7 @@ int main(void)
|
||||
printf("=== IEC104 Service Host Unit Tests ===\n");
|
||||
tc_monotonic();
|
||||
tc_measure();
|
||||
tc_parameter_binding();
|
||||
tc_stats_and_link();
|
||||
tc_tick_control();
|
||||
printf("=== IEC104 Serv Tests: %d/%d PASSED (failed=%d) ===\n",
|
||||
|
||||
@@ -23,6 +23,21 @@
|
||||
* TC-PROTSET-011 OF/UF 定值行(B8):40 项 / blob 版本 2 / 本地项地址 0 /
|
||||
* 默认值与范围 / 返回系数独立性 / 64 位 bad_mask(bit 39)/
|
||||
* B7 旧档(32 项)换代拒绝
|
||||
* 批量原子写与写事务锁(104 定值下发接线):
|
||||
* tc_batch_write_and_seal 整批写 3 项 → 读回 / 自检通过 / 未闭锁 / 置脏
|
||||
* tc_batch_reject_is_all_or_nothing 一项越界 → 整批拒且值区一字未改
|
||||
* tc_batch_reject_duplicate_id 同批重复 id → 拒
|
||||
* tc_batch_reject_over_limit 项数超上限 → 拒
|
||||
* tc_validate_only_has_no_side_effect 预检全合法 → 值区/脏位/锁位/钩子全不动
|
||||
* tc_validate_only_reports_bad_item 预检有越界 → 拒且全不动
|
||||
* tc_read_batch_guards 正常读回;越界 id / n 为 0 / 空指针 → 拒
|
||||
* tc_write_lock_pairs 成功写 enter/exit 各一次;预检与失败分支不进钩子
|
||||
* tc_selfcheck_tolerates_commit_window 提交窗口内自检只跳过、不误锁
|
||||
* tc_lock_uninstall 卸载钩子后仍能正常写
|
||||
* tc_selfcheck_odd_streak_failsafe 写者永久停在提交窗口内 → 8 拍宽限后闭锁
|
||||
* (fail-safe;用故意漏 exit 的缺陷写者样本驱动)
|
||||
* tc_bad_mask_tracks_whole_table_only 位图只跟整表装载/解析走,增量写入
|
||||
* 既不置也不清(契约钉死)
|
||||
*
|
||||
* 通过:全部 TC PASS(main 返回 0)。
|
||||
* 创建日期:2026-10-11;2026-10-12 扩 OF/UF(B8)
|
||||
@@ -321,6 +336,486 @@ static void tc_of_uf_meta(void) {
|
||||
CHECK(prot_set_selfcheck() == 0, "TC-PROTSET-011 active table intact after legacy reject");
|
||||
}
|
||||
|
||||
/* 整表快照:值区 + 脏位 + 闭锁位("一字未改"与"零副作用"的比对基准) */
|
||||
typedef struct {
|
||||
int32_t vals[PS_ID_COUNT];
|
||||
uint8_t dirty;
|
||||
uint8_t locked;
|
||||
} tc_snapshot_t;
|
||||
|
||||
/*
|
||||
* 函数功能:把当前定值状态拍成快照
|
||||
* 入口参数:s - 输出快照
|
||||
* 返 回 值:无
|
||||
*/
|
||||
static void tc_snapshot(tc_snapshot_t *s) {
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0u; i < (uint32_t)PS_ID_COUNT; i++) {
|
||||
s->vals[i] = prot_set_get((prot_set_id_t)i);
|
||||
}
|
||||
s->dirty = prot_set_dirty_peek();
|
||||
s->locked = prot_set_locked();
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:比对两份快照是否逐字相同
|
||||
* 入口参数:a/b - 待比快照
|
||||
* 返 回 值:1 完全相同 / 0 有差异
|
||||
*/
|
||||
static int tc_snapshot_same(const tc_snapshot_t *a, const tc_snapshot_t *b) {
|
||||
uint32_t i;
|
||||
|
||||
if ((a->dirty != b->dirty) || (a->locked != b->locked)) {
|
||||
return 0;
|
||||
}
|
||||
for (i = 0u; i < (uint32_t)PS_ID_COUNT; i++) {
|
||||
if (a->vals[i] != b->vals[i]) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* 计数版写事务钩子:证明"哪条分支真的进了临界区" */
|
||||
static int s_hook_enter = 0;
|
||||
static int s_hook_exit = 0;
|
||||
|
||||
/*
|
||||
* 函数功能:计数钩子——进入写事务
|
||||
* 入口参数:无
|
||||
* 返 回 值:无
|
||||
*/
|
||||
static void tc_hook_enter(void) {
|
||||
s_hook_enter++;
|
||||
}
|
||||
|
||||
/*
|
||||
* 函数功能:计数钩子——退出写事务
|
||||
* 入口参数:无
|
||||
* 返 回 值:无
|
||||
*/
|
||||
static void tc_hook_exit(void) {
|
||||
s_hook_exit++;
|
||||
}
|
||||
|
||||
static const prot_set_lock_t s_test_lock = { tc_hook_enter, tc_hook_exit };
|
||||
|
||||
/*
|
||||
* 函数功能:装钩子并清零计数
|
||||
* 入口参数:无
|
||||
* 返 回 值:无
|
||||
*/
|
||||
static void tc_lock_on(void) {
|
||||
s_hook_enter = 0;
|
||||
s_hook_exit = 0;
|
||||
prot_set_lock_install(&s_test_lock);
|
||||
}
|
||||
|
||||
/* 批量写:正常整批提交的闭环(值/自检/闭锁/脏位) */
|
||||
static void tc_batch_write_and_seal(void) {
|
||||
prot_set_change_t chg[3];
|
||||
prot_set_id_t ids[3];
|
||||
int32_t got[3];
|
||||
|
||||
prot_set_init();
|
||||
prot_set_lock_install(0);
|
||||
CHECK(prot_set_dirty_peek() == 0u, "batch write: clean table before commit");
|
||||
|
||||
chg[0].id = PS_OC1_CUR; chg[0].q10 = PROT_SET_Q10(7.5);
|
||||
chg[1].id = PS_OC2_DLY; chg[1].q10 = PROT_SET_Q10(250.0);
|
||||
chg[2].id = PS_GLOBAL_EN; chg[2].q10 = 0;
|
||||
CHECK(prot_set_apply_batch(chg, 3u, 0) == 0, "batch write: three items committed");
|
||||
|
||||
CHECK(prot_set_get(PS_OC1_CUR) == PROT_SET_Q10(7.5), "batch write: OC1 current updated");
|
||||
CHECK(prot_set_get(PS_OC2_DLY) == PROT_SET_Q10(250.0), "batch write: OC2 delay updated");
|
||||
CHECK(prot_set_get_ctl(PS_GLOBAL_EN) == 0u, "batch write: global plate switched out");
|
||||
CHECK(prot_set_selfcheck() == 0, "batch write: selfcheck passes right after commit");
|
||||
CHECK(prot_set_locked() == 0u, "batch write: no lock after commit");
|
||||
CHECK(prot_set_dirty_peek() == 1u, "batch write: commit marks dirty for persistence");
|
||||
|
||||
ids[0] = PS_OC1_CUR;
|
||||
ids[1] = PS_OC2_DLY;
|
||||
ids[2] = PS_GLOBAL_EN;
|
||||
CHECK(prot_set_read_batch(ids, 3u, got) == 0, "batch write: read back same three items");
|
||||
CHECK((got[0] == PROT_SET_Q10(7.5)) && (got[1] == PROT_SET_Q10(250.0)) && (got[2] == 0),
|
||||
"batch write: read-back values match what was written");
|
||||
}
|
||||
|
||||
/* 批量写:一项越界 → 整批拒绝且值区一字未改(all-or-nothing) */
|
||||
static void tc_batch_reject_is_all_or_nothing(void) {
|
||||
prot_set_change_t chg[3];
|
||||
tc_snapshot_t before;
|
||||
tc_snapshot_t after;
|
||||
|
||||
prot_set_init();
|
||||
prot_set_lock_install(0);
|
||||
(void)prot_set_set_sealed(PS_OC1_CUR, PROT_SET_Q10(9.0)); /* 先建立一个非默认现状 */
|
||||
(void)prot_set_dirty_take();
|
||||
tc_snapshot(&before);
|
||||
|
||||
chg[0].id = PS_OC2_CUR; chg[0].q10 = PROT_SET_Q10(6.0); /* 合法 */
|
||||
chg[1].id = PS_OC3_DLY; chg[1].q10 = PROT_SET_Q10(60001.0); /* 越上限 */
|
||||
chg[2].id = PS_OL_CUR; chg[2].q10 = PROT_SET_Q10(2.0); /* 合法 */
|
||||
CHECK(prot_set_apply_batch(chg, 3u, 0) == -1, "batch reject: one bad item rejects the batch");
|
||||
|
||||
tc_snapshot(&after);
|
||||
CHECK(tc_snapshot_same(&before, &after),
|
||||
"batch reject: every item, dirty flag and lock state untouched");
|
||||
CHECK(prot_set_get(PS_OC1_CUR) == PROT_SET_Q10(9.0),
|
||||
"batch reject: pre-existing value survives the rejected batch");
|
||||
CHECK(prot_set_selfcheck() == 0, "batch reject: table still self-consistent");
|
||||
}
|
||||
|
||||
/* 批量写:同批重复编号 → 拒(语义不确定) */
|
||||
static void tc_batch_reject_duplicate_id(void) {
|
||||
prot_set_change_t chg[2];
|
||||
tc_snapshot_t before;
|
||||
tc_snapshot_t after;
|
||||
|
||||
prot_set_init();
|
||||
prot_set_lock_install(0);
|
||||
tc_snapshot(&before);
|
||||
|
||||
chg[0].id = PS_OC2_CUR; chg[0].q10 = PROT_SET_Q10(6.0);
|
||||
chg[1].id = PS_OC2_CUR; chg[1].q10 = PROT_SET_Q10(7.0);
|
||||
CHECK(prot_set_apply_batch(chg, 2u, 0) == -1, "batch reject: duplicate id rejected");
|
||||
|
||||
tc_snapshot(&after);
|
||||
CHECK(tc_snapshot_same(&before, &after), "batch reject duplicate: table untouched");
|
||||
}
|
||||
|
||||
/* 批量写:项数超上限 → 拒(整表只有 40 项,批量接口不接受更多) */
|
||||
static void tc_batch_reject_over_limit(void) {
|
||||
prot_set_change_t chg[PROT_SET_BATCH_MAX + 1u];
|
||||
tc_snapshot_t before;
|
||||
tc_snapshot_t after;
|
||||
size_t i;
|
||||
|
||||
prot_set_init();
|
||||
prot_set_lock_install(0);
|
||||
tc_snapshot(&before);
|
||||
|
||||
for (i = 0u; i < (PROT_SET_BATCH_MAX + 1u); i++) {
|
||||
chg[i].id = (prot_set_id_t)(i % (size_t)PS_ID_COUNT);
|
||||
chg[i].q10 = prot_set_get(chg[i].id); /* 值全合法,只考项数 */
|
||||
}
|
||||
CHECK(prot_set_apply_batch(chg, PROT_SET_BATCH_MAX + 1u, 0) == -1,
|
||||
"batch reject: item count above limit rejected");
|
||||
CHECK(prot_set_apply_batch(chg, 0u, 0) == -1, "batch reject: empty batch rejected");
|
||||
CHECK(prot_set_apply_batch(0, 1u, 0) == -1, "batch reject: NULL change list rejected");
|
||||
|
||||
tc_snapshot(&after);
|
||||
CHECK(tc_snapshot_same(&before, &after), "batch reject over limit: table untouched");
|
||||
}
|
||||
|
||||
/* 预检(validate_only):全合法时零副作用——值区、脏位、闭锁位、钩子全不动 */
|
||||
static void tc_validate_only_has_no_side_effect(void) {
|
||||
prot_set_change_t chg[2];
|
||||
tc_snapshot_t before;
|
||||
tc_snapshot_t after;
|
||||
|
||||
prot_set_init();
|
||||
(void)prot_set_set_sealed(PS_OC1_CUR, PROT_SET_Q10(8.0));
|
||||
(void)prot_set_dirty_take();
|
||||
tc_lock_on();
|
||||
tc_snapshot(&before);
|
||||
|
||||
chg[0].id = PS_OC1_CUR; chg[0].q10 = PROT_SET_Q10(9.0);
|
||||
chg[1].id = PS_UF_HZ; chg[1].q10 = PROT_SET_Q10(47.0);
|
||||
CHECK(prot_set_apply_batch(chg, 2u, 1) == 0, "validate only: valid batch reported OK");
|
||||
|
||||
tc_snapshot(&after);
|
||||
CHECK(tc_snapshot_same(&before, &after),
|
||||
"validate only: values, dirty flag and lock state all unchanged");
|
||||
CHECK(prot_set_get(PS_OC1_CUR) == PROT_SET_Q10(8.0),
|
||||
"validate only: proposed value NOT applied");
|
||||
CHECK(prot_set_get(PS_UF_HZ) == PROT_SET_Q10(49.0),
|
||||
"validate only: untouched item keeps its old value");
|
||||
CHECK((s_hook_enter == 0) && (s_hook_exit == 0),
|
||||
"validate only: write lock hook never entered");
|
||||
prot_set_lock_install(0);
|
||||
}
|
||||
|
||||
/* 预检(validate_only):有越界项 → 拒,同样全不动 */
|
||||
static void tc_validate_only_reports_bad_item(void) {
|
||||
prot_set_change_t chg[2];
|
||||
tc_snapshot_t before;
|
||||
tc_snapshot_t after;
|
||||
|
||||
prot_set_init();
|
||||
tc_lock_on();
|
||||
tc_snapshot(&before);
|
||||
|
||||
chg[0].id = PS_OC1_CUR; chg[0].q10 = PROT_SET_Q10(9.0);
|
||||
chg[1].id = PS_UF_HZ; chg[1].q10 = PROT_SET_Q10(44.0); /* 低于下限 45 */
|
||||
CHECK(prot_set_apply_batch(chg, 2u, 1) == -1, "validate only: out-of-range item reported");
|
||||
|
||||
tc_snapshot(&after);
|
||||
CHECK(tc_snapshot_same(&before, &after), "validate only reject: nothing changed");
|
||||
CHECK((s_hook_enter == 0) && (s_hook_exit == 0),
|
||||
"validate only reject: write lock hook never entered");
|
||||
prot_set_lock_install(0);
|
||||
}
|
||||
|
||||
/* 批量读:正常读回;越界编号 / 零项 / 空指针 → 拒 */
|
||||
static void tc_read_batch_guards(void) {
|
||||
prot_set_id_t ids[2];
|
||||
prot_set_id_t bad[1];
|
||||
int32_t got[2];
|
||||
|
||||
prot_set_init();
|
||||
(void)prot_set_set_sealed(PS_OC2_CUR, PROT_SET_Q10(6.5));
|
||||
|
||||
ids[0] = PS_OC2_CUR;
|
||||
ids[1] = PS_RET_UF;
|
||||
got[0] = 0;
|
||||
got[1] = 0;
|
||||
CHECK(prot_set_read_batch(ids, 2u, got) == 0, "read batch: two items read");
|
||||
CHECK(got[0] == PROT_SET_Q10(6.5), "read batch: first value matches");
|
||||
CHECK(got[1] == PROT_SET_Q10(1.01), "read batch: second value matches");
|
||||
|
||||
bad[0] = PS_ID_COUNT;
|
||||
CHECK(prot_set_read_batch(bad, 1u, got) == -1, "read batch: out-of-range id rejected");
|
||||
CHECK(prot_set_read_batch(ids, 0u, got) == -1, "read batch: zero count rejected");
|
||||
CHECK(prot_set_read_batch(0, 2u, got) == -1, "read batch: NULL id list rejected");
|
||||
CHECK(prot_set_read_batch(ids, 2u, 0) == -1, "read batch: NULL output rejected");
|
||||
}
|
||||
|
||||
/* 写事务锁:成功写 enter/exit 各一次;预检与失败分支不进钩子 */
|
||||
static void tc_write_lock_pairs(void) {
|
||||
prot_set_change_t chg[2];
|
||||
int32_t tbl[PS_ID_COUNT];
|
||||
uint32_t i;
|
||||
|
||||
prot_set_init();
|
||||
tc_lock_on();
|
||||
|
||||
chg[0].id = PS_OC1_CUR; chg[0].q10 = PROT_SET_Q10(7.0);
|
||||
chg[1].id = PS_OL_CUR; chg[1].q10 = PROT_SET_Q10(1.2);
|
||||
CHECK(prot_set_apply_batch(chg, 2u, 0) == 0, "lock pairs: batch commit succeeds");
|
||||
CHECK((s_hook_enter == 1) && (s_hook_exit == 1),
|
||||
"lock pairs: batch commit enters and exits the write lock exactly once");
|
||||
|
||||
s_hook_enter = 0;
|
||||
s_hook_exit = 0;
|
||||
chg[0].id = PS_OC1_CUR; chg[0].q10 = PROT_SET_Q10(999.0); /* 越界 */
|
||||
CHECK(prot_set_apply_batch(chg, 1u, 0) == -1, "lock pairs: out-of-range batch rejected");
|
||||
CHECK((s_hook_enter == 0) && (s_hook_exit == 0),
|
||||
"lock pairs: rejected batch never enters the write lock");
|
||||
|
||||
s_hook_enter = 0;
|
||||
s_hook_exit = 0;
|
||||
CHECK(prot_set_set_sealed(PS_OC2_CUR, PROT_SET_Q10(4.0)) == 0,
|
||||
"lock pairs: single item write succeeds");
|
||||
CHECK((s_hook_enter == 1) && (s_hook_exit == 1),
|
||||
"lock pairs: single item write enters and exits exactly once");
|
||||
|
||||
s_hook_enter = 0;
|
||||
s_hook_exit = 0;
|
||||
CHECK(prot_set_set_sealed(PS_ID_COUNT, 0) == -1, "lock pairs: bad id rejected");
|
||||
CHECK((s_hook_enter == 0) && (s_hook_exit == 0),
|
||||
"lock pairs: rejected single write never enters the write lock");
|
||||
|
||||
for (i = 0u; i < (uint32_t)PS_ID_COUNT; i++) {
|
||||
tbl[i] = prot_set_get((prot_set_id_t)i);
|
||||
}
|
||||
tbl[PS_OC3_CUR] = PROT_SET_Q10(3.0);
|
||||
s_hook_enter = 0;
|
||||
s_hook_exit = 0;
|
||||
CHECK(prot_set_load_raw(tbl) == 0, "lock pairs: whole table load succeeds");
|
||||
CHECK((s_hook_enter == 1) && (s_hook_exit == 1),
|
||||
"lock pairs: whole table load enters and exits exactly once");
|
||||
|
||||
s_hook_enter = 0;
|
||||
s_hook_exit = 0;
|
||||
tbl[PS_OC3_CUR] = PROT_SET_Q10(999.0);
|
||||
CHECK(prot_set_load_raw(tbl) == -1, "lock pairs: bad table rejected");
|
||||
CHECK((s_hook_enter == 0) && (s_hook_exit == 0),
|
||||
"lock pairs: rejected table load never enters the write lock");
|
||||
|
||||
prot_set_lock_install(0);
|
||||
}
|
||||
|
||||
/* 提交窗口内自检不得误判失配(多写者引入的安全级风险) */
|
||||
static void tc_selfcheck_tolerates_commit_window(void) {
|
||||
uint32_t keep_sum;
|
||||
uint32_t keep_sumn;
|
||||
|
||||
prot_set_init();
|
||||
|
||||
/* 模拟"封存对已改、活跃区未交换"的窗口:这是另一写者抢占时的真实中间态,
|
||||
* 巡检撞上它绝不能闭锁整台保护 */
|
||||
keep_sum = s_sum;
|
||||
keep_sumn = s_sumn;
|
||||
prot_write_enter();
|
||||
s_sum = prot_sum_buf(s_val[s_act]) + 1u;
|
||||
s_sumn = ~s_sum;
|
||||
CHECK(prot_set_selfcheck() == 0, "commit window: selfcheck skips instead of latching");
|
||||
CHECK(prot_set_locked() == 0u, "commit window: no lock latched during commit");
|
||||
s_sum = keep_sum;
|
||||
s_sumn = keep_sumn;
|
||||
prot_write_exit();
|
||||
CHECK(prot_set_selfcheck() == 0, "commit window: selfcheck passes once commit settles");
|
||||
|
||||
/* 无提交在飞时的真失配仍必须闭锁(防篡改语义不许被上述跳过放宽) */
|
||||
s_val[s_act][(uint32_t)PS_OC1_CUR] ^= 1u;
|
||||
CHECK(prot_set_selfcheck() == -1, "tamper: real mismatch still caught when idle");
|
||||
CHECK(prot_set_locked() == 1u, "tamper: real mismatch still latches the lock");
|
||||
prot_set_init();
|
||||
CHECK(prot_set_selfcheck() == 0, "tamper: re-init clears the lock");
|
||||
}
|
||||
|
||||
/* 卸载钩子后仍能正常写(模拟器 / 主机单测的单写者假设) */
|
||||
static void tc_lock_uninstall(void) {
|
||||
prot_set_change_t chg[1];
|
||||
|
||||
prot_set_init();
|
||||
tc_lock_on();
|
||||
prot_set_lock_install(0);
|
||||
s_hook_enter = 0;
|
||||
s_hook_exit = 0;
|
||||
|
||||
chg[0].id = PS_OC1_CUR; chg[0].q10 = PROT_SET_Q10(6.25);
|
||||
CHECK(prot_set_apply_batch(chg, 1u, 0) == 0, "lock uninstall: batch commit works without hook");
|
||||
CHECK(prot_set_get(PS_OC1_CUR) == PROT_SET_Q10(6.25), "lock uninstall: value applied");
|
||||
CHECK(prot_set_selfcheck() == 0, "lock uninstall: table self-consistent");
|
||||
CHECK((s_hook_enter == 0) && (s_hook_exit == 0), "lock uninstall: hook not called anymore");
|
||||
}
|
||||
|
||||
/*
|
||||
* 缺陷写者样本:**故意**在 prot_write_enter() 之后提前 return、忘了 exit。
|
||||
* 这是 prot_set.c 里明令禁止的形态(四条真实路径都把校验放在 enter 之前),
|
||||
* 但"以后有人加路径时忘了这条纪律"是真实可能——本用例用它把自检的 fail-safe
|
||||
* 兜底钉死:序号一旦永久停在奇数,防篡改自检必须自己发现写者已死、置闭锁,
|
||||
* 而不是永远"本轮跳过"。
|
||||
*/
|
||||
static int tc_buggy_writer_early_return(const prot_set_change_t *changes, size_t n) {
|
||||
uint32_t cur_act;
|
||||
|
||||
prot_write_enter();
|
||||
if ((n == 0u) || (changes == 0)) {
|
||||
return -1; /* 缺陷:锁内提前返回,没有 prot_write_exit() */
|
||||
}
|
||||
if ((changes[0].q10 < g_prot_set_meta[changes[0].id].min)
|
||||
|| (changes[0].q10 > g_prot_set_meta[changes[0].id].max)) {
|
||||
return -1; /* 缺陷同上 */
|
||||
}
|
||||
cur_act = s_act;
|
||||
prot_copy_buf(s_val[cur_act ^ 1u], s_val[cur_act]);
|
||||
s_val[cur_act ^ 1u][(uint32_t)changes[0].id] = (uint32_t)changes[0].q10;
|
||||
s_sum = prot_sum_buf(s_val[cur_act ^ 1u]);
|
||||
s_sumn = ~s_sum;
|
||||
s_locked = 0u;
|
||||
s_act = (uint8_t)(cur_act ^ 1u);
|
||||
s_dirty = 1u;
|
||||
prot_write_exit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 自检 fail-safe:写者永久停在提交窗口内 → 有界宽限后必须闭锁 */
|
||||
static void tc_selfcheck_odd_streak_failsafe(void) {
|
||||
prot_set_change_t chg[1];
|
||||
uint32_t i;
|
||||
int early_ok = 1;
|
||||
int all_clear = 1;
|
||||
|
||||
prot_set_init();
|
||||
CHECK(prot_set_selfcheck() == 0, "odd streak: healthy table selfchecks clean");
|
||||
|
||||
/* 真实缺陷路径:enter 之后越界提前 return → 序号永久停在奇数 */
|
||||
chg[0].id = PS_OC1_CUR;
|
||||
chg[0].q10 = g_prot_set_meta[PS_OC1_CUR].max + 1;
|
||||
CHECK(tc_buggy_writer_early_return(chg, 1u) == -1, "odd streak: buggy writer returns -1");
|
||||
CHECK((s_seq & 1u) == 1u, "odd streak: sequence number left permanently odd");
|
||||
|
||||
/* ① 宽限期内的前 7 拍:跳过但不误锁 */
|
||||
for (i = 0u; i < (uint32_t)(PROT_SET_ODD_STREAK_MAX - 1u); i++) {
|
||||
if ((prot_set_selfcheck() != 0) || (prot_set_locked() != 0u)) {
|
||||
early_ok = 0;
|
||||
}
|
||||
}
|
||||
CHECK(early_ok, "odd streak: first seven ticks skip WITHOUT latching the lock");
|
||||
|
||||
/* ② 第 8 拍起:判写者已死 → 闭锁并返回 -1 */
|
||||
CHECK(prot_set_selfcheck() == -1, "odd streak: eighth tick trips the fail-safe lock");
|
||||
CHECK(prot_set_locked() == 1u, "odd streak: fail-safe latches the protection lock");
|
||||
CHECK(prot_set_selfcheck() == -1, "odd streak: lock stays latched on later ticks");
|
||||
|
||||
/* ③ 恢复偶数(写者"复活")→ 宽限计数归零,但闭锁仍按既有语义保持到 seal/init */
|
||||
prot_write_exit();
|
||||
CHECK((s_seq & 1u) == 0u, "odd streak: sequence number back to even after recovery");
|
||||
CHECK(prot_set_selfcheck() == -1, "odd streak: lock latched until an explicit seal");
|
||||
CHECK(s_odd_streak == 0u, "odd streak: grace counter cleared once sequence settles even");
|
||||
|
||||
/* ④ fail-safe 的闭锁必须是**可恢复**的:写者恢复后走合法封存路径能解锁,
|
||||
* 否则一次写者崩溃就等于设备永久报废,现场只能整机返修 */
|
||||
prot_set_seal();
|
||||
CHECK(prot_set_locked() == 0u, "odd streak: seal is the documented unlock path after a fail-safe lock");
|
||||
CHECK(prot_set_selfcheck() == 0, "odd streak: selfcheck clean after re-seal");
|
||||
|
||||
prot_set_init();
|
||||
CHECK(s_odd_streak == 0u, "odd streak: init also zeroes the grace counter");
|
||||
CHECK(prot_set_locked() == 0u, "odd streak: init clears the fail-safe lock");
|
||||
CHECK(prot_set_selfcheck() == 0, "odd streak: selfcheck healthy again after init");
|
||||
|
||||
/* 反复"撞一下立刻恢复"的健康抖动不该累积:每拍都能看到偶数就每次归零 */
|
||||
for (i = 0u; i < (uint32_t)(PROT_SET_ODD_STREAK_MAX * 3u); i++) {
|
||||
prot_write_enter();
|
||||
prot_write_exit();
|
||||
if ((prot_set_selfcheck() != 0) || (prot_set_locked() != 0u)) {
|
||||
all_clear = 0;
|
||||
}
|
||||
}
|
||||
CHECK(all_clear, "odd streak: short commit windows never accumulate into a false lock");
|
||||
}
|
||||
|
||||
/* 契约:bad_mask 只跟整表装载/解析走,增量写入既不置也不清 */
|
||||
static void tc_bad_mask_tracks_whole_table_only(void) {
|
||||
int32_t tbl[PS_ID_COUNT];
|
||||
prot_set_change_t chg[2];
|
||||
uint64_t bad_before;
|
||||
uint32_t i;
|
||||
|
||||
prot_set_init();
|
||||
CHECK(prot_set_bad_mask() == 0u, "bad mask: clean after init");
|
||||
|
||||
for (i = 0u; i < (uint32_t)PS_ID_COUNT; i++) {
|
||||
tbl[i] = prot_set_get((prot_set_id_t)i);
|
||||
}
|
||||
tbl[PS_OC3_DLY] = PROT_SET_Q10(60001.0); /* 越上限 */
|
||||
CHECK(prot_set_load_raw(tbl) == -1, "bad mask: whole-table load with a bad item rejected");
|
||||
CHECK(prot_set_bad_mask() == (1uLL << PS_OC3_DLY),
|
||||
"bad mask: whole-table load reports the offending item");
|
||||
bad_before = prot_set_bad_mask();
|
||||
|
||||
/* 一笔成功的批量提交不碰位图——这是刻意契约(不是漏刷) */
|
||||
chg[0].id = PS_OC1_CUR; chg[0].q10 = PROT_SET_Q10(7.0);
|
||||
chg[1].id = PS_OL_CUR; chg[1].q10 = PROT_SET_Q10(1.5);
|
||||
CHECK(prot_set_apply_batch(chg, 2u, 0) == 0, "bad mask: successful batch committed");
|
||||
CHECK(prot_set_bad_mask() == bad_before,
|
||||
"bad mask: successful batch leaves the stale bitmap untouched (by contract)");
|
||||
|
||||
/* 一笔失败的批量提交同样不碰位图 */
|
||||
chg[0].id = PS_OC1_CUR; chg[0].q10 = PROT_SET_Q10(999.0);
|
||||
CHECK(prot_set_apply_batch(chg, 1u, 0) == -1, "bad mask: bad batch rejected");
|
||||
CHECK(prot_set_bad_mask() == bad_before,
|
||||
"bad mask: failed batch does not record anything in the bitmap");
|
||||
|
||||
/* 单项写入同理 */
|
||||
CHECK(prot_set_set_sealed(PS_OC2_CUR, PROT_SET_Q10(3.0)) == 0, "bad mask: single write ok");
|
||||
CHECK(prot_set_bad_mask() == bad_before, "bad mask: single write leaves the bitmap untouched");
|
||||
|
||||
/* 只有整表操作与 init 会覆写它(用各项出厂默认值组一张必然合法的表) */
|
||||
for (i = 0u; i < (uint32_t)PS_ID_COUNT; i++) {
|
||||
tbl[i] = g_prot_set_meta[i].def;
|
||||
}
|
||||
CHECK(prot_set_load_raw(tbl) == 0, "bad mask: all-legal table load accepted");
|
||||
CHECK(prot_set_bad_mask() == 0u, "bad mask: successful whole-table load rewrites it to clean");
|
||||
prot_set_init();
|
||||
CHECK(prot_set_bad_mask() == 0u, "bad mask: init clears it");
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
printf("=== Prot Set Tests ===\n");
|
||||
tc_init();
|
||||
@@ -334,6 +829,18 @@ int main(void) {
|
||||
tc_store_roundtrip();
|
||||
tc_store_corrupt();
|
||||
tc_of_uf_meta();
|
||||
tc_batch_write_and_seal();
|
||||
tc_batch_reject_is_all_or_nothing();
|
||||
tc_batch_reject_duplicate_id();
|
||||
tc_batch_reject_over_limit();
|
||||
tc_validate_only_has_no_side_effect();
|
||||
tc_validate_only_reports_bad_item();
|
||||
tc_read_batch_guards();
|
||||
tc_write_lock_pairs();
|
||||
tc_selfcheck_tolerates_commit_window();
|
||||
tc_lock_uninstall();
|
||||
tc_selfcheck_odd_streak_failsafe();
|
||||
tc_bad_mask_tracks_whole_table_only();
|
||||
|
||||
printf("\n=== Prot Set Tests: %d/%d PASSED (failed=%d) ===\n",
|
||||
s_passed, s_total, s_failed);
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
|
||||
/* 真实定值层(blob 编解码与脏标志在这里)+ 被测胶水层,均原样纳入 */
|
||||
#include "prot_set.c"
|
||||
#include "stub/cmsis_stub.c"
|
||||
#include "prot_set_store.c"
|
||||
|
||||
/* ===================== 断言框架 ===================== */
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/* Host-only stub: CMSIS-RTOS v2 mutex surface used by prot_set_store.c.
|
||||
* The host build has no kernel; lock init is exercised as no-lock fallback. */
|
||||
#ifndef CMSIS_OS2_H
|
||||
#define CMSIS_OS2_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void *osMutexId_t;
|
||||
#define osWaitForever 0xFFFFFFFFU
|
||||
|
||||
osMutexId_t osMutexNew(const void *attr);
|
||||
uint32_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout);
|
||||
uint32_t osMutexRelease(osMutexId_t mutex_id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CMSIS_OS2_H */
|
||||
@@ -0,0 +1,21 @@
|
||||
/* Host-only CMSIS mutex stub: never creates a lock, so the production
|
||||
* no-lock fallback remains the exercised path in the pure host test. */
|
||||
#include "cmsis_os2.h"
|
||||
|
||||
osMutexId_t osMutexNew(const void *attr)
|
||||
{
|
||||
(void)attr;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout)
|
||||
{
|
||||
(void)mutex_id; (void)timeout;
|
||||
return 0U;
|
||||
}
|
||||
|
||||
uint32_t osMutexRelease(osMutexId_t mutex_id)
|
||||
{
|
||||
(void)mutex_id;
|
||||
return 0U;
|
||||
}
|
||||
Reference in New Issue
Block a user