Skip to content

Commit 1228af2

Browse files
committed
nrf_rpc: fix initialization corner case
When initializing multiple groups that share the same transport, it may happen that a device receives an init packet for a group whose internal members have not been initialized yet, leading to the bus fault. Make sure that internal members of all groups have been initialized before ANY transport is started to prevent that. Signed-off-by: Damian Krolik <[email protected]>
1 parent 7aa44cd commit 1228af2

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

nrf_rpc/nrf_rpc.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -426,18 +426,6 @@ static int transport_init(nrf_rpc_tr_receive_handler_t receive_cb)
426426
const struct nrf_rpc_tr *transport = group->transport;
427427
struct nrf_rpc_group_data *data = group->data;
428428

429-
NRF_RPC_ASSERT(transport != NULL);
430-
431-
/* Initialize all dependencies of `receive_handler` before calling the transport
432-
* init to avoid possible data race if `receive_handler` was invoked before this
433-
* function was completed. */
434-
if (auto_free_rx_buf(transport)) {
435-
err = nrf_rpc_os_event_init(&data->decode_done_event);
436-
if (err < 0) {
437-
continue;
438-
}
439-
}
440-
441429
err = transport->api->init(transport, receive_cb, NULL);
442430
if (err) {
443431
NRF_RPC_ERR("Failed to initialize transport, err: %d", err);
@@ -1138,6 +1126,7 @@ int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
11381126
for (NRF_RPC_AUTO_ARR_FOR(iter, group, &nrf_rpc_groups_array,
11391127
const struct nrf_rpc_group)) {
11401128
struct nrf_rpc_group_data *data = group->data;
1129+
const struct nrf_rpc_tr *transport = group->transport;
11411130

11421131
if (group_id >= 0xFF) {
11431132
return -NRF_ENOMEM;
@@ -1149,6 +1138,18 @@ int nrf_rpc_init(nrf_rpc_err_handler_t err_handler)
11491138
if (group->flags & NRF_RPC_FLAGS_WAIT_ON_INIT) {
11501139
wait_count++;
11511140
}
1141+
1142+
NRF_RPC_ASSERT(transport != NULL);
1143+
1144+
/* Initialize all groups' members before starting transports to avoid the risk of
1145+
* receiving a packet for a group that hasn't been initialized yet in case a single
1146+
* transport is used across multiple groups. */
1147+
if (auto_free_rx_buf(transport)) {
1148+
err = nrf_rpc_os_event_init(&data->decode_done_event);
1149+
if (err < 0) {
1150+
return err;
1151+
}
1152+
}
11521153
}
11531154

11541155
group_count = group_id;

0 commit comments

Comments
 (0)