为什么在将指针传递到 FreeRTOS 队列时会出现堆损坏错误?

Why do I get a heap corruption error when passing pointers to a FreeRTOS queue?

我正在使用 ESP32-Wrover-DevKit using Eclipse CDT 12.2019, the ESP-IDF framework and FreeRTOS

我正在使用单个队列从多个任务中收集数据(传感器读数)。单个队列接收器将通过 TCP 套接字输出数据。由于队列项目相当大,我决定只放置一个指向队列项目的指针,根据 FreeRTOS documentation,只要内存处理正确,这应该没问题。

这是我用于队列项的数据结构,请注意结构末尾的灵活数组:

typedef struct mb32_packet_t {
    uint16_t preamble;
    uint8_t  system_id;
    uint8_t  message_id;
    uint8_t  reserved;
    uint16_t checksum;
    uint32_t pay_len;
    uint8_t  payload[];
} __attribute__((packed)) mb32_packet_t;

队列声明和定义:

#define MAX_QUEUE_SEND_ITEMS (25)

QueueHandle_t sys_link_send_queue;

sys_link_send_queue = xQueueCreate(MAX_QUEUE_SEND_ITEMS, sizeof(mb32_packet_t*));

这是将项目放入队列的传感器读取任务之一的片段:

mb32_packet_t *packet;
uint32_t pay_len = 8;                        // payload: 8 bytes
uint32_t pac_len = sizeof(*packet)+pay_len;  // header: 11 bytes
packet = malloc(pac_len);
// ... code to assign header fields
// ... code to assign payload bytes

if(xQueueSend(sys_link_send_queue, &packet, portMAX_DELAY) != pdPASS) {
    // release allocated memory in case the queue rejected the item
    free(packet);
}

这是单个接收器的片段:

void sys_link_task(void *pvParameters) {
    while(1) {
        mb32_packet_t* packet;
        if(xQueueReceive(sys_link_send_queue, &packet, portMAX_DELAY) == pdPASS) {
            // put packet bytes on the TCP stream (blocking mode)
            tcp_server_send((uint8_t*)packet, packet->pay_len+11);
            // finally release the packet memory
            free(packet);
        } else {
            ESP_LOGE(TAG, "Failed to get message from queue.");
        }
    }
}

最后是 tcp_server_send() 函数的实现:

void tcp_server_send(uint8_t* buffer, size_t size) {
    // send() can return less bytes than supplied length. Walk-around for robust implementation.
    if(client_sock > 0) {
        int to_write = size;
        while(to_write > 0) {
            int written = send(client_sock, buffer+(size-to_write), to_write, 0);
            if(written < 0) {
                printf("Failed to send data [w=%d]: %d", written, errno);
                break;
            }
            to_write -= written;
        }
    }
}

现在只有一个传感器任务,一切都很好运行。一旦我执行第二个传感器任务,我迟早会遇到堆损坏错误。有时它可以正常运行几秒钟,有时我会立即收到这些错误。

错误如下所示:

CORRUPT HEAP: multi_heap.c:288 detected at 0x3ffc75e8
abort() was called at PC 0x4008da2e on core 1

ELF file SHA256: c4fc5b20ae785f9a890274f05fd4fcfcada76b29ea16a9f736ceabbea34086ad

Backtrace: 0x400913e9:0x3ffc95c0 0x40091785:0x3ffc95e0 0x4008da2e:0x3ffc9600 0x4008dda5:0x3ffc9620 0x4008413d:0x3ffc9640 0x4008416d:0x3ffc9660 0x40093a71:0x3ffc9680 0x40094557:0x3ffc96a0 0x400f4946:0x3ffc96c0 0x400f4987:0x3ffc96e0 0x400f4b0d:0x3ffc9700 0x400f4e8e:0x3ffc9720 0x400f4ee5:0x3ffc9770 0x400e2e43:0x3ffc97a0 0x400e2f52:0x3ffc97d0 0x400d3f89:0x3ffc97f0 0x4000bd83:0x3ffc9810 0x4000182a:0x3ffc9830 0x400d5e9c:0x3ffc9850 0x400d608c:0x3ffc9880 0x40093cd1:0x3ffc98b0

CPU halted.

然后我 运行 xtensa-esp32-elf-gdb 并在程序计数器 (PC) 中查找符号:

PC 0x4008da2e -> split_if_necessary + 206 in section .iram0.text

知道如何解决这个问题吗?

我的想法:

问题中描述的队列处理应该没问题。请与 FreeRTOS 论坛上的 this discussion 进行比较。

从 Github 更新到最新的 ESP-IDF 后,问题消失了。