It's probably a rare bug, but it is still worth noting. I have been debugging this issue for a day. I am using SDK 6.1 at the moment.
The problem was that Device manager was failing to initialize itself, if optimizations were set to -Os and p_init_param->clear_persistent_data == false in dm_init(). Digging deeper i found out that
*err_code = pstorage_load((uint8_t )&m_peer_table[index], &block_handle, sizeof(peer_id_t), 0);
was returning NRF_ERROR_INVALID_ADDR because
static peer_id_t m_peer_table[DEVICE_MANAGER_MAX_BONDS];
was not aligned by word (4 bytes) is RAM, which the function checked. The simplest solution was to force the compiler to align the array as was expected in pstorage_load():
static peer_id_t m_peer_table[DEVICE_MANAGER_MAX_BONDS] attribute ((aligned (4)));
This problem did not occur when using other optimization levels. One thing i do not understand is why do we need to align the memory in RAM? As far as i know it is not written by pages. Am I wrong?