Potential bug in chPoolAlloc Topic is solved

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
jayalfredprufrock
Posts: 36
Joined: Tue Jan 13, 2015 6:33 am
Has thanked: 8 times

Potential bug in chPoolAlloc

Post by jayalfredprufrock »

I use the mempool+mailbox pattern quite a lot in a project I'm working on and recently came across some strange behavior in one of my modules where successive calls to chPoolAlloc would return the exact same object address. I figured it was some kind of overflow or memory mismanagement issue since I use practically the exact code elsewhere, just with a different structure definition. While trying to track the bug down, I discovered the issue was related to the order in which my struct fields were defined. The bare minimum code to produce the behavior looks something like this:

Code: Select all


#define BLE_CMD_MAX_QUEUED             4
#define BLE_CMD_MAX_PACKET_SIZE      64

typedef struct {
   binary_semaphore_t semaphore;
   uint8_t packet[BLE_CMD_MAX_PACKET_SIZE];
   uint8_t packet_length;
   uint8_t class_id;
   uint8_t id;
} ble_cmd_t;

static MEMORYPOOL_DECL(cmd_pool, sizeof(ble_cmd_t), NULL);
static ble_cmd_t cmd_pool_data[BLE_CMD_MAX_QUEUED] __attribute__((aligned(sizeof(stkalign_t))));
static mailbox_t cmd_mailbox;
static msg_t cmd_messages[BLE_CMD_MAX_QUEUED];

void bleInit()
{
   uint8_t i;
   
   for (i=0; i < BLE_CMD_MAX_QUEUED; i++)
   {
      chPoolFree(&cmd_pool, &(cmd_pool_data[i]));
      chBSemObjectInit(&(cmd_pool_data[i].semaphore), false);
   }

   chMBObjectInit(&cmd_mailbox, cmd_messages, BLE_CMD_MAX_QUEUED);

   ble_cmd_t* cmd = (ble_cmd_t*)chPoolAlloc(&cmd_pool);
   ble_cmd_t* cmd2 = (ble_cmd_t*)chPoolAlloc(&cmd_pool);
   
   //at this point cmd == cmd2
}



After changing the ble_cmd_t struct definition to the below, making the semaphore the last field, cmd and cmd2 were correctly allocated and assigned two distinct addresses:

Code: Select all

typedef struct {
   uint8_t packet[BLE_CMD_MAX_PACKET_SIZE];
   uint8_t packet_length;
   uint8_t class_id;
   uint8_t id;
   binary_semaphore_t semaphore;
} ble_cmd_t;


I really just can't wrap my head around why the order of the fields matter, other than some kind of memory alignment issue. Does anything in the above code look wrong or can anybody suggest reasons why I might be experiencing this behavior? Even though it seems to be working now I won't be satisfied until I can explain the behavior somehow. Thanks!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Potential bug in chPoolAlloc

Post by Giovanni »

Hi,

Objects in a pool gets their first 4 bytes overwritten by the pool link pointer, this means that you cannot put initialized objects into a pool.

You have 2 options:

1) Initialize objects after getting them from the pool.
2) Arrange your object fields to not have critical data in the first 2/4 bytes (sizeof (void *) to be formally correct).

Giovanni
jayalfredprufrock
Posts: 36
Joined: Tue Jan 13, 2015 6:33 am
Has thanked: 8 times

Re: Potential bug in chPoolAlloc

Post by jayalfredprufrock »

Thanks Giovanni, that's exactly the kind of answer I was hoping to get!
Post Reply