zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.5k stars 6.43k forks source link

LINKER_SORT_BY_ALIGNMENT causing crash in Zephyr start-up code #78063

Closed kamaln16 closed 6 days ago

kamaln16 commented 1 week ago

Hello,

I am working in ncs1.5.0 on a nrf52840dk based custom board. I have found an issue where adding a certain number of characteristics in a single BLE service causing my code to crash during start-up. I ran the debugger and see code hitting a Zephyr Fatal Error in SystemInit() as shown in the debugger screenshot.

I have investigated the issue and found that if I remove one of the Client Characteristic Configuration Declarations, everything works fine. The firmware will also work fine with all the characteristics are included and CONFIG_LINKER_SORT_BY_ALIGNMENT is disabled.

I compared the generated map files and found that the failing code results in a fill of 0x3 bytes that the working code does not have. The working code has one less Client Characteristic Configuration Declaration.

I also tried to add attribute ((aligned (4))) to the BLE service definition which had no effect on the issue and still crashed.

For now, I have removed one of the characteristics in the problematic BLE service to avoid this crash. I do not want to disable CONFIG_LINKER_SORT_BY_ALIGNMENT because that results in using too much memory for padding. Please see the attached code and screenshots for more info.

`/ The following code will cause a crash. /

BT_GATT_SERVICE_DEFINE(     new_service,     BT_GATT_PRIMARY_SERVICE(BT_UUID_EXAMPLE_SRV),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE1,                            BT_GATT_CHRC_WRITE | BT_GATT_CHRC_NOTIFY,                            BT_GATT_PERM_WRITE,                            NULL, CommandReceived, NULL),         BT_GATT_CCC(new_ccc_ex1_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE2,                            BT_GATT_CHRC_READ | BT_GATT_CHRC_INDICATE,                            BT_GATT_PERM_READ,                            ReadStatusRequested, NULL, NULL),         BT_GATT_CCC(new_ccc_ex2_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE3,                            BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,                            BT_GATT_PERM_READ,                            ReadStatusHeartbeatRequested, NULL, NULL),         BT_GATT_CCC(new_ccc_ex3_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE4,                            EXAMPLE_CHAR_PACKET_TYPE,                            0,                            NULL, NULL, NULL),         BT_GATT_CCC(new_ccc_ex4_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE5,                            BT_GATT_CHRC_INDICATE,                            0,                            NULL, NULL, NULL),         BT_GATT_CCC(new_ccc_ex5_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE6,                            EXAMPLE_CHAR_PACKET_TYPE,                            0,                            NULL, NULL, NULL),         BT_GATT_CCC(new_ccc_ex6_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE7,                            BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,                            BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,                            SerialRead, SerialWrite, NULL), ); `

`/ One of the Client Characteristic Configuration Declarations has been commented out (line 36). This change resolves the firmware crash./

BT_GATT_SERVICE_DEFINE(     new_service,     BT_GATT_PRIMARY_SERVICE(BT_UUID_EXAMPLE_SRV),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE1,                            BT_GATT_CHRC_WRITE | BT_GATT_CHRC_NOTIFY,                            BT_GATT_PERM_WRITE,                            NULL, CommandReceived, NULL),         BT_GATT_CCC(new_ccc_ex1_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE2,                            BT_GATT_CHRC_READ | BT_GATT_CHRC_INDICATE,                            BT_GATT_PERM_READ,                            ReadStatusRequested, NULL, NULL),         BT_GATT_CCC(new_ccc_ex2_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE3,                            BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,                            BT_GATT_PERM_READ,                            ReadStatusHeartbeatRequested, NULL, NULL),         BT_GATT_CCC(new_ccc_ex3_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE4,                            EXAMPLE_CHAR_PACKET_TYPE,                            0,                            NULL, NULL, NULL),         BT_GATT_CCC(new_ccc_ex4_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE5,                            BT_GATT_CHRC_INDICATE,                            0,                            NULL, NULL, NULL),         // BT_GATT_CCC(new_ccc_ex5_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE6,                            EXAMPLE_CHAR_PACKET_TYPE,                            0,                            NULL, NULL, NULL),         BT_GATT_CCC(new_ccc_ex6_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),

    BT_GATT_CHARACTERISTIC(BT_UUID_EXAMPLE7,                            BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,                            BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,                            SerialRead, SerialWrite, NULL), ); `

failing_ble_service_definition working_ble_service_definition

debug_screenshot

map_file_comparison

github-actions[bot] commented 1 week ago

Hi @kamaln16! We appreciate you submitting your first issue for our open-source project. 🌟

Even though I'm a bot, I can assure you that the whole community is genuinely grateful for your time and effort. 🤖💙

henrikbrixandersen commented 1 week ago

Thank you for reporting this. However - unless you are able to reproduce this issue with upstream Zephyr main - please report issues with the nRF Connect SDK (NCS) on the Nordic Semiconductor DevZone.

mmahadevan108 commented 6 days ago

@kamaln16, as @henrikbrixandersen mentioned, can you check if this issue can be reproduced with upstream Zephyr main branch.

kamaln16 commented 6 days ago

I cannot change to a newer Zephyr so you can close this ticket. I am going to post the same question in Nordic DevZone

kamaln16 commented 6 days ago

Can not replicate in newer Zephyr version due to project constraints.