different errors with different compiler optimization settings

Hello,

I'm having a very strange issue where changing the compiler optimization level will generate different errors in the ble_ess_init() function (sometimes even in sd_enable() )

in particular:

optimization set to None = everything works fine

optimization set to Level 2 for size= NRF_ERROR_DATA_SIZE

optimization set to Level 2 for speed = NRF_ERROR_INVALID_ADDR

 

how do I fix this issue?

Regards,

Rocco

Parents
  • Hi Rocco

    Is this based on one of the standard examples, or have you made your own?

    Do you know which code line it is that is generating the error?

    Are you able to share the code with me so I can have a look at the implementation?

    Normally if changing the optimization level causes an error it is a sign of some vulnerability in the code. 

    Best regards
    Torbjørn

  • hi , 

    in attachment the module causing the issue. the function is ble_ess_init()

    /* Attention!
     * To maintain compliance with Nordic Semiconductor ASA's Bluetooth profile
     * qualification listings, this section of source code must not be modified.
     */
    #include "sdk_common.h"
    #if NRF_MODULE_ENABLED(BLE_ESS)
    #include "ble_ess.h"
    #include <string.h>
    #include "ble_srv_common.h"
    
    
    #define OPCODE_LENGTH 1                                                              /**< Length of opcode inside Temperature Measurement packet. */
    #define HANDLE_LENGTH 2                                                              /**< Length of handle inside Temperature Measurement packet. */
    #define MAX_TPM_LEN      (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - OPCODE_LENGTH - HANDLE_LENGTH) /**< Maximum size of a transmitted Temperature Measurement. */
    
    
    /**@brief Function for handling the Connect event.
     *
     * @param[in]   p_ess       Environmental Sensing Service structure.
     * @param[in]   p_ble_evt   Event received from the BLE stack.
     */
    static void on_connect(ble_ess_t * p_ess, ble_evt_t const * p_ble_evt)
    {
        p_ess->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
    }
    
    
    /**@brief Function for handling the Disconnect event.
     *
     * @param[in]   p_ess       Environmental Sensing Service structure.
     * @param[in]   p_ble_evt   Event received from the BLE stack.
     */
    static void on_disconnect(ble_ess_t * p_ess, ble_evt_t const * p_ble_evt)
    {
        UNUSED_PARAMETER(p_ble_evt);
        p_ess->conn_handle = BLE_CONN_HANDLE_INVALID;
    }
    
    
    /**@brief Function for handling write events to the Environmental Sensing Temperature characteristic.
     *
     * @param[in]   p_ess         Environmental Sensing Service structure.
     * @param[in]   p_evt_write   Write event received from the BLE stack.
     */
    static void on_tpm_cccd_write(ble_ess_t * p_ess, ble_gatts_evt_write_t const * p_evt_write)
    {
        if (p_evt_write->len == 2)
        {
            // CCCD written, update notification state
            if (p_ess->evt_handler != NULL)
            {
                ble_ess_evt_t evt;
    
                if (ble_srv_is_notification_enabled(p_evt_write->data))
                {
                    evt.evt_type = BLE_ESS_EVT_NOTIFICATION_ENABLED;
                }
                else
                {
                    evt.evt_type = BLE_ESS_EVT_NOTIFICATION_DISABLED;
                }
    
                p_ess->evt_handler(p_ess, &evt);
            }
        }
    }
    
    
    /**@brief Function for handling the Write event.
     *
     * @param[in]   p_ess       Environmental Sensing Service structure.
     * @param[in]   p_ble_evt   Event received from the BLE stack.
     */
    static void on_write(ble_ess_t * p_ess, ble_evt_t const * p_ble_evt)
    {
        ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
    
        if (p_evt_write->handle == p_ess->tpm_handles.cccd_handle)
        {
            on_tpm_cccd_write(p_ess, p_evt_write);
        }
    }
    
    
    void ble_ess_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ble_ess_t * p_ess = (ble_ess_t *) p_context;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                on_connect(p_ess, p_ble_evt);
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                on_disconnect(p_ess, p_ble_evt);
                break;
    
            case BLE_GATTS_EVT_WRITE:
                on_write(p_ess, p_ble_evt);
                break;
    
            default:
                // No implementation needed.
                break;
        }
    }
    
    
    uint32_t ble_ess_init(ble_ess_t * p_ess, const ble_ess_init_t * p_ess_init)
    {
        uint32_t              err_code;
        ble_uuid_t            ble_uuid;
        ble_add_char_params_t add_char_params;
    
    
        // Initialize service structure
        p_ess->evt_handler                 = p_ess_init->evt_handler;
        p_ess->conn_handle                 = BLE_CONN_HANDLE_INVALID;
        p_ess->max_tpm_len                 = CC_TPM_FRAME_LENGTH;
    
        // Add service
        BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_ENVIRONMENTAL_SENSING_SERVICE);
    
        err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                            &ble_uuid,
                                            &p_ess->service_handle);
    
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    
        // Add TPM measurement characteristic
        memset(&add_char_params, 0, sizeof(add_char_params));
    
        add_char_params.uuid              = BLE_UUID_CC_CHARACTERISTC_UUID;
        add_char_params.char_props.read   = 1;
        add_char_params.char_props.notify = 1;
        add_char_params.uuid_type         = 0;
    
        add_char_params.max_len           = CC_TPM_FRAME_LENGTH;
        add_char_params.init_len          = CC_TPM_FRAME_LENGTH;
        add_char_params.p_init_value      = 0;
        add_char_params.is_var_len        = true;
        add_char_params.cccd_write_access = p_ess_init->esm_cccd_wr_sec;
    
        err_code = characteristic_add(p_ess->service_handle, &add_char_params, &(p_ess->tpm_handles));
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    
        return NRF_SUCCESS;
    }
    
    
    uint32_t ble_ess_temperature_measurement_send(ble_ess_t * p_ess, uint8_t *temperature)
    {
        uint32_t err_code;
    
        // Send value if connected and notifying
        if (p_ess->conn_handle != BLE_CONN_HANDLE_INVALID)
        {
            uint8_t                encoded_tpm[MAX_TPM_LEN];
            uint16_t               len;
            uint16_t               hvx_len;
            ble_gatts_hvx_params_t hvx_params;
    
            len     = CC_TPM_FRAME_LENGTH;
            hvx_len = len;
    
            memset(&hvx_params, 0, sizeof(hvx_params));
    
            hvx_params.handle = p_ess->tpm_handles.value_handle;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
            hvx_params.offset = 0;
            hvx_params.p_len  = &hvx_len;
            hvx_params.p_data = temperature;
    
            err_code = sd_ble_gatts_hvx(p_ess->conn_handle, &hvx_params);
            if ((err_code == NRF_SUCCESS) && (hvx_len != len))
            {
                err_code = NRF_ERROR_DATA_SIZE;
            }
        }
        else
        {
            err_code = NRF_ERROR_INVALID_STATE;
        }
    
        return err_code;
    }
    
    
    void ble_ess_on_gatt_evt(ble_ess_t * p_ess, nrf_ble_gatt_evt_t const * p_gatt_evt)
    {
        if (    (p_ess->conn_handle == p_gatt_evt->conn_handle)
            &&  (p_gatt_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED))
        {
            p_ess->max_tpm_len = p_gatt_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
        }
    }
    
    #endif // NRF_MODULE_ENABLED(BLE_ESS)
    

Reply
  • hi , 

    in attachment the module causing the issue. the function is ble_ess_init()

    /* Attention!
     * To maintain compliance with Nordic Semiconductor ASA's Bluetooth profile
     * qualification listings, this section of source code must not be modified.
     */
    #include "sdk_common.h"
    #if NRF_MODULE_ENABLED(BLE_ESS)
    #include "ble_ess.h"
    #include <string.h>
    #include "ble_srv_common.h"
    
    
    #define OPCODE_LENGTH 1                                                              /**< Length of opcode inside Temperature Measurement packet. */
    #define HANDLE_LENGTH 2                                                              /**< Length of handle inside Temperature Measurement packet. */
    #define MAX_TPM_LEN      (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - OPCODE_LENGTH - HANDLE_LENGTH) /**< Maximum size of a transmitted Temperature Measurement. */
    
    
    /**@brief Function for handling the Connect event.
     *
     * @param[in]   p_ess       Environmental Sensing Service structure.
     * @param[in]   p_ble_evt   Event received from the BLE stack.
     */
    static void on_connect(ble_ess_t * p_ess, ble_evt_t const * p_ble_evt)
    {
        p_ess->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
    }
    
    
    /**@brief Function for handling the Disconnect event.
     *
     * @param[in]   p_ess       Environmental Sensing Service structure.
     * @param[in]   p_ble_evt   Event received from the BLE stack.
     */
    static void on_disconnect(ble_ess_t * p_ess, ble_evt_t const * p_ble_evt)
    {
        UNUSED_PARAMETER(p_ble_evt);
        p_ess->conn_handle = BLE_CONN_HANDLE_INVALID;
    }
    
    
    /**@brief Function for handling write events to the Environmental Sensing Temperature characteristic.
     *
     * @param[in]   p_ess         Environmental Sensing Service structure.
     * @param[in]   p_evt_write   Write event received from the BLE stack.
     */
    static void on_tpm_cccd_write(ble_ess_t * p_ess, ble_gatts_evt_write_t const * p_evt_write)
    {
        if (p_evt_write->len == 2)
        {
            // CCCD written, update notification state
            if (p_ess->evt_handler != NULL)
            {
                ble_ess_evt_t evt;
    
                if (ble_srv_is_notification_enabled(p_evt_write->data))
                {
                    evt.evt_type = BLE_ESS_EVT_NOTIFICATION_ENABLED;
                }
                else
                {
                    evt.evt_type = BLE_ESS_EVT_NOTIFICATION_DISABLED;
                }
    
                p_ess->evt_handler(p_ess, &evt);
            }
        }
    }
    
    
    /**@brief Function for handling the Write event.
     *
     * @param[in]   p_ess       Environmental Sensing Service structure.
     * @param[in]   p_ble_evt   Event received from the BLE stack.
     */
    static void on_write(ble_ess_t * p_ess, ble_evt_t const * p_ble_evt)
    {
        ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
    
        if (p_evt_write->handle == p_ess->tpm_handles.cccd_handle)
        {
            on_tpm_cccd_write(p_ess, p_evt_write);
        }
    }
    
    
    void ble_ess_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ble_ess_t * p_ess = (ble_ess_t *) p_context;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                on_connect(p_ess, p_ble_evt);
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                on_disconnect(p_ess, p_ble_evt);
                break;
    
            case BLE_GATTS_EVT_WRITE:
                on_write(p_ess, p_ble_evt);
                break;
    
            default:
                // No implementation needed.
                break;
        }
    }
    
    
    uint32_t ble_ess_init(ble_ess_t * p_ess, const ble_ess_init_t * p_ess_init)
    {
        uint32_t              err_code;
        ble_uuid_t            ble_uuid;
        ble_add_char_params_t add_char_params;
    
    
        // Initialize service structure
        p_ess->evt_handler                 = p_ess_init->evt_handler;
        p_ess->conn_handle                 = BLE_CONN_HANDLE_INVALID;
        p_ess->max_tpm_len                 = CC_TPM_FRAME_LENGTH;
    
        // Add service
        BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_ENVIRONMENTAL_SENSING_SERVICE);
    
        err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                            &ble_uuid,
                                            &p_ess->service_handle);
    
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    
        // Add TPM measurement characteristic
        memset(&add_char_params, 0, sizeof(add_char_params));
    
        add_char_params.uuid              = BLE_UUID_CC_CHARACTERISTC_UUID;
        add_char_params.char_props.read   = 1;
        add_char_params.char_props.notify = 1;
        add_char_params.uuid_type         = 0;
    
        add_char_params.max_len           = CC_TPM_FRAME_LENGTH;
        add_char_params.init_len          = CC_TPM_FRAME_LENGTH;
        add_char_params.p_init_value      = 0;
        add_char_params.is_var_len        = true;
        add_char_params.cccd_write_access = p_ess_init->esm_cccd_wr_sec;
    
        err_code = characteristic_add(p_ess->service_handle, &add_char_params, &(p_ess->tpm_handles));
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    
        return NRF_SUCCESS;
    }
    
    
    uint32_t ble_ess_temperature_measurement_send(ble_ess_t * p_ess, uint8_t *temperature)
    {
        uint32_t err_code;
    
        // Send value if connected and notifying
        if (p_ess->conn_handle != BLE_CONN_HANDLE_INVALID)
        {
            uint8_t                encoded_tpm[MAX_TPM_LEN];
            uint16_t               len;
            uint16_t               hvx_len;
            ble_gatts_hvx_params_t hvx_params;
    
            len     = CC_TPM_FRAME_LENGTH;
            hvx_len = len;
    
            memset(&hvx_params, 0, sizeof(hvx_params));
    
            hvx_params.handle = p_ess->tpm_handles.value_handle;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
            hvx_params.offset = 0;
            hvx_params.p_len  = &hvx_len;
            hvx_params.p_data = temperature;
    
            err_code = sd_ble_gatts_hvx(p_ess->conn_handle, &hvx_params);
            if ((err_code == NRF_SUCCESS) && (hvx_len != len))
            {
                err_code = NRF_ERROR_DATA_SIZE;
            }
        }
        else
        {
            err_code = NRF_ERROR_INVALID_STATE;
        }
    
        return err_code;
    }
    
    
    void ble_ess_on_gatt_evt(ble_ess_t * p_ess, nrf_ble_gatt_evt_t const * p_gatt_evt)
    {
        if (    (p_ess->conn_handle == p_gatt_evt->conn_handle)
            &&  (p_gatt_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED))
        {
            p_ess->max_tpm_len = p_gatt_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
        }
    }
    
    #endif // NRF_MODULE_ENABLED(BLE_ESS)
    

Children
Related