espressif / ESP8266_RTOS_SDK

Latest ESP8266 SDK based on FreeRTOS, esp-idf style.
http://bbs.espressif.com
Apache License 2.0
3.34k stars 1.57k forks source link

snprintf bug (GIT8266O-82) #541

Closed nickfox-taterli closed 5 years ago

nickfox-taterli commented 5 years ago

Environment

Problem Description

使用newlib(nano)时候不报告错误,但是snprintf不兼容标准snprintf,在%f的情况下出错.如果使用newlib(normal),则出现错误,由于单独测试snprintf下没有出现错误.所以对目前这个错误,不明白是什么原因,但是唯一差异是,使用newlib(nano)不出错,使用newlib(normal)出错,使用newlib(normal)但屏蔽snprintf相关代码,也不出错.

Expected Behavior

snprintf应正确处理字符串.

Actual Behavior

只要遇到了vsnprintf代码并且使用newlib(normal)就出错.

Steps to repropduce

  1. 使用我移植的工程https://github.com/nickfox-taterli/esp8266_qcloud/
  2. 使用newlib(normal)编译,并在过程中遇到%f.

Code to reproduce this issue

int HAL_Snprintf(_IN_ char *str, const int len, const char *fmt, ...)
{
    va_list args;
    int rc;

    va_start(args, fmt);
    rc = vsnprintf(str, len, fmt, args);
    va_end(args);

    return rc;
}

Debug Logs

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x40100000, len 7600, room 16 
0x40100000: _stext at ??:?

tail 0
chksum 0xc2
load 0x3ffe8408, len 24, room 8 
tail 0
chksum 0x7b
load 0x3ffe8420, len 4280, room 8 
tail 0
chksum 0xba
I (44) boot: ESP-IDF v3.2-dev-185-geb6d4f7f-dirty 2nd stage bootloader
I (44) boot: compile time 15:45:48
I (44) qio_mode: Enabling default flash chip QIO
D (48) boot: bootloader initialize SPI flash clock and I/O
D (56) boot: magic e9
D (59) boot: segments 03
D (63) boot: spi_mode 00
D (66) boot: spi_speed 0f
D (70) boot: spi_size 02
I (73) boot: SPI Speed      : 80MHz
I (78) boot: SPI Mode       : QIO
I (83) boot: SPI Flash Size : 1MB
D (88) boot: mapped partition table 0x8000 at 0x40208000
D (95) flash_parts: partition table verified, 3 entries
I (103) boot: Partition Table:
I (107) boot: ## Label            Usage          Type ST Offset   Length
D (117) boot: load partition table entry 0x3ffffe14
D (124) boot: type=1 subtype=2
I (129) boot:  0 nvs              WiFi data        01 02 00009000 00006000
D (139) boot: load partition table entry 0x3ffffe14
D (146) boot: type=1 subtype=1
I (150) boot:  1 phy_init         RF data          01 01 0000f000 00001000
D (160) boot: load partition table entry 0x3ffffe14
D (167) boot: type=0 subtype=0
I (171) boot:  2 factory          factory app      00 00 00010000 000f0000
I (182) boot: End of partition table
D (187) boot: Trying partition index -1 offs 0x10000 size 0xf0000
D (196) esp_image: reading image header @ 0x10000
D (202) esp_image: image header: 0xe9 0x04 0x02 0x00 40217938
V (211) esp_image: loading segment header 0 at offset 0x10008
V (219) esp_image: segment data length 0x59ae4 data starts 0x10010
I (228) esp_image: segment 0: paddr=0x00010010 vaddr=0x40210010 size=0x59ae4 (367332) map
V (346) esp_image: loading segment header 1 at offset 0x69af4
V (346) esp_image: segment data length 0xb7c data starts 0x69afc
I (346) esp_image: segment 1: paddr=0x00069afc vaddr=0x3ffe8000 size=0x00b7c (  2940) load
V (359) esp_image: loading segment header 2 at offset 0x6a678
V (367) esp_image: segment data length 0xd8 data starts 0x6a680
I (375) esp_image: segment 2: paddr=0x0006a680 vaddr=0x3ffe8b7c size=0x000d8 (   216) load
V (388) esp_image: loading segment header 3 at offset 0x6a758
V (396) esp_image: segment data length 0x5e10 data starts 0x6a760
I (405) esp_image: segment 3: paddr=0x0006a760 vaddr=0x40100000 size=0x05e10 ( 24080) load
0x40100000: _stext at ??:?

V (424) esp_image: image start 0x00010000 end of last section 0x00070570
I (427) boot: Loaded app from partition at offset 0x10000
mode : sta(b4:e6:2d:39:13:b6)
add if0
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
pm open phy_2,type:2 0 0
cnt 

connected with TaterLiEnt, channel 11
5
heINF|1970-01-01 00:00:04|device.c|iot_device_info_set(65): SDK_Ver: 2.3.1, Product_ID: VUCGL7FQ04, Device_Name: ESP8266
INF|1970-01-01 00:00:04|HAL_TCP_linux.c|HAL_TCP_Connect(104): connected with TCP server: VUCGL7FQ04.iotcloud.tencentdevices.com:1883
DBG|1970-01-01 00:00:04|mqtt_client_common.c|send_mqtt_packet(523):  -- 127
DBG|1970-01-01 00:00:04|mqtt_client_common.c|send_mqtt_packet(532): there was an sent 127 writing the data
INF|1970-01-01 00:00:05|mqtt_client.c|IOT_MQTT_Construct(115): mqtt connect with id: 12ivs success

Task stack [mqtt_main] stack from [0x3fff6918] to [0x3fff7514], total [3072] size

                   0          4          8          c         10         14         18         1c         20         24         28         2c         30         34         38         3c 

3fff6e80  0x00000000 0x00000000 0x3fff1878 0x402279ca 0x40106798 0x3fff0cf0 0x0000d0b7 0x4023d360 0x00000000 0x3fff710c 0x402130b4 0x00000064 0x00000001 0x00000000 0x3fff1878 0x00000000 
0x402279ca: xQueueGenericSend at /home/taterli/esp/ESP8266_RTOS_SDK/components/freertos/freertos/queue.c:2332

0x4023d360: __sfvwrite_r at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/fvwrite.c:252

3fff6ec0  0x00000000 0x3fff7520 0x3fff1878 0x40227af5 0xffffff9c 0x00000004 0x3fff1878 0x40240725 0xffffffff 0xa0000000 0x00000000 0x3fff72cf 0x3fff73d0 0x3fff7104 0x00000001 0x402407b8 
0x40227af5: xQueueGiveMutexRecursive at /home/taterli/esp/ESP8266_RTOS_SDK/components/freertos/freertos/queue.c:2332

0x40240725: lock_release_generic at /home/taterli/esp/ESP8266_RTOS_SDK/components/newlib/newlib/port/locks.c:172

0x402407b8: _lock_release_recursive at /home/taterli/esp/ESP8266_RTOS_SDK/components/newlib/newlib/port/locks.c:214

3fff6f00  0xffffff9c 0x3fff710c 0x00000000 0x4023bac5 0xffffffff 0x00000001 0xa500a5a5 0xa5a5a5a5 0x3fff7290 0x3fff7020 0x402356a0 0x00000000 0x3fff0cf0 0x3fff6ef4 0x00000000 0x00000000 
0x4023bac5: _vfprintf_r at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vfprintf.c:659

0x402356a0: _svfprintf_r at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vfprintf.c:1620

3fff6f40  0x00000000 0x00000001 0x00000000 0x00000000 0xffffffff 0x3fff7050 0x00000010 0x00000000 0x34ff6ef4 0x4021607a 0x00000001 0x40212450 0x00000001 0x3fff6f63 0x00000001 0x00000002 
3fff6f80  0x31373931 0x40212450 0x00000001 0x3fff6f83 0xffffffff 0x00000000 0x00000000 0x00000000 0x32333534 0x40212672 0x00000003 0x3fff6fa1 0x00000003 0x00000003 0x00000001 0x00000008 
3fff6fc0  0x31373931 0x3fff6f84 0x00000000 0x00000000 0x00000000 0x00000001 0x00000000 0x3ffedc20 0x3fff7100 0x3fff70e0 0x00000010 0x00000000 0x35ff6f84 0x4021607a 0x00000001 0x40212450 
3fff7000  0x00000001 0x3fff6ff3 0x00000001 0x00000002 0x00000002 0x00000000 0x00000002 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x393231de 0x40212672 0x00000003 0x3fff7031 
3fff7040  0x00000003 0x00000003 0x00000001 0x00000008 0x3fff7170 0x3fff7150 0x00000018 0x00000000 0x3fff6ff4 0x3fff6f10 0x4021607f 0x35000001 0x31333433 0x40213a30 0x0000000b 0x3fff706f 
3fff7080  0x00000005 0x00000000 0x00000002 0x00000000 0x00000000 0x00000000 0x00000000 0x00000005 0x3f007034 0x3fff6f50 0x4021607f 0x00000001 0x3fff7220 0x3fff7074 0x00000001 0x0000000b 
3fff70c0  0x00000005 0x40212672 0x00000003 0x3fff0cf0 0x3fff7150 0x00000003 0x00000000 0x00000003 0x40002672 0x00000000 0x00000000 0x00000000 0x3fff7390 0x3fff7360 0x00000028 0x00000000 
3fff7100  0x00000003 0x00000000 0xffffffff 0x3fff0cf0 0x3fff7190 0x00000005 0x00000073 0x3fff6f90 0x40213a3d 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000005 0x00000050 
3fff7140  0x00000001 0x00000000 0x40213b4f 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x3fff7250 0x000000ff 0x3fff1918 0x00000000 0x40216071 0x00000001 0x3fff1918 0x3fff72a0 
3fff7180  0x3fff72a0 0x3fff0cf0 0x000000ad 0x40255929 0x3fff72e5 0x2d6d252d 0x00000067 0xffff0208 0x3fff72a0 0x000000ac 0x3fff7200 0x4025588c 0x3ffeeb54 0x00000000 0x402126a4 0x00000000 
0x40255929: _vsnprintf_r at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vsnprintf.c:73

0x4025588c: __tz_unlock at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/time/../../../.././newlib/libc/time/tzlock.c:56

3fff71c0  0x40216338 0x00000100 0x40212664 0x4025597d 0x3fff7220 0x3fff7210 0x0000000c 0x00000081 0x3fff7220 0x3fff7210 0x0000000c 0x00000081 0x40213a76 0x00000100 0x40212688 0x4024322c 
0x4025597d: vsnprintf at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vsnprintf.c:42

0x4024322c: HAL_Snprintf at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/platform/os/linux/HAL_OS_linux.c:110

3fff7200  0x3fff7220 0x000000ad 0x40213a17 0x4025597d 0x3fff7390 0x3fff7360 0x00000014 0x40212688 0x3fff7390 0x3fff7360 0x00000014 0x00000000 0x3fff7390 0x00000014 0x3fff7360 0x40242f05 
0x4025597d: vsnprintf at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vsnprintf.c:42

0x40242f05: Log_writter at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/sdk-impl/qcloud_iot_sdk_impl.c:116

3fff7240  0x40213a76 0x00000081 0x00000004 0x4025597d 0x7c474244 0x30373931 0x2d31302d 0x30203130 0x30303a30 0x7c35303a 0x7474716d 0x696c635f 0x5f746e65 0x73627573 0x62697263 0x7c632e65 
0x4025597d: vsnprintf at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vsnprintf.c:42

3fff7280  0x6f6c6371 0x695f6475 0x6d5f746f 0x5f747471 0x73627573 0x62697263 0x32312865 0x203a2939 0x69706f74 0x6d614e63 0x73243d65 0x6f646168 0x706f2f77 0x74617265 0x2f6e6f69 0x75736572 
3fff72c0  0x562f746c 0x4c474355 0x30514637 0x53452f34 0x36323850 0x61707c36 0x74656b63 0x3d64695f 0x33343335 0x00000031 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 
3fff7300  0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 
3fff7340  0x00000000 0x00000000 0x00000000 0x00000000 0x3fff7390 0x3fff7360 0x00000014 0x3fff7430 0xffffff99 0x4010b070 0x40107070 0x40242d46 0xffffff99 0x40106798 0x40107070 0x402431f8 
0x40242d46: pthread_mutex_unlock at /home/taterli/esp/ESP8266_RTOS_SDK/components/pthread/src/pthread.c:152

0x402431f8: HAL_MutexUnlock at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/platform/os/linux/HAL_OS_linux.c:74

3fff7380  0x00000004 0x40213a17 0x402139f3 0x402450e2 0xffffff99 0x401065fc 0x40107070 0x3fff7430 0x0000d0b7 0x4010b070 0x40107070 0x40246648 0x0000d0b7 0x00000000 0x0000000c 0x401003b4 
0x402450e2: get_next_packet_id at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/mqtt/src/mqtt_client_common.c:65 (discriminator 4)

0x40246648: qcloud_iot_mqtt_subscribe at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/mqtt/src/mqtt_client_subscribe.c:131

0x401003b4: vPortETSIntrUnlock at /home/taterli/esp/ESP8266_RTOS_SDK/components/freertos/port/esp8266/port.c:256

3fff73c0  0x3fff7400 0x3fff73f0 0x0000000c 0x402299aa 0x00000007 0x0002a733 0x00000000 0x00000000 0x40106798 0x3fff73f0 0x0000000c 0x00000000 0x40107070 0x401065fc 0x401065fc 0x00000000 
0x402299aa: _heap_caps_malloc at /home/taterli/esp/ESP8266_RTOS_SDK/components/heap/src/esp_heap_caps.c:200

3fff7400  0x40106798 0x401065fc 0x3fff0bde 0x402443bc 0x514d0000 0x04004354 0x40106750 0x40249475 0x40107070 0x00000000 0x40106744 0x40248574 0x00000000 0x40248da0 0x00000000 0x40249084 
0x402443bc: IOT_MQTT_Subscribe at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/mqtt/src/mqtt_client.c:193

0x40249475: subscribe_operation_result_to_cloud at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/shadow/src/shadow_client_manager.c:205

0x40248574: list_new at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/utils/farra/utils_list.c:215

0x40248da0: _on_operation_result_handler at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/shadow/src/shadow_client_manager.c:251

0x40249084: qcloud_iot_shadow_init at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/shadow/src/shadow_client_manager.c:104

3fff7440  0xa5a5a5a5 0xa5a5a5a5 0x3fff7480 0x00000000 0x40107070 0x00000000 0x401065fc 0x40243c13 0x3fff7480 0x3fff7470 0x00000004 0x40211420 0x40211030 0x40211028 0x4021103c 0x000007d0 
0x40243c13: IOT_Shadow_Construct at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/shadow/src/shadow_client.c:637

3fff7480  0x0003a980 0x00000101 0x4024388c 0x401065fc 0x00000000 0x00000000 0x40214614 0x40232be6 0xffffffff 0x00000001 0x3fff5640 0x00000000 0x00006568 0xffffffff 0x40211030 0x40211028 
0x4024388c: _shadow_event_handler at /home/taterli/qcloud/esp8266_qcloud/qcloud/qcloud-iot-sdk-embedded-c/src/shadow/src/shadow_client.c:637

0x40232be6: mqtt_loop at /home/taterli/qcloud/esp8266_qcloud/main/main.c:382

3fff74c0  0x4021103c 0x000007d0 0x0003a980 0x00000101 0x402323c4 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x40232df9 
0x402323c4: mqtt_event_handler at /home/taterli/qcloud/esp8266_qcloud/main/main.c:204

0x40232df9: mqtt_main at /home/taterli/qcloud/esp8266_qcloud/main/main.c:465

3fff7500  0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 

        PC: 0x4000bf80        PS: 0x00000030        A0: 0x40234e5a        A1: 0x3fff6e80
0x40234e5a: _svfprintf_r at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vfprintf.c:1529

        A2: 0x00000000        A3: 0xfffffffc        A4: 0x000000ff        A5: 0x0000ff00
        A6: 0x00ff0000        A7: 0xff000000        A8: 0xffffffff        A9: 0x3fff6e90
       A10: 0x3fff6e90       A11: 0x40213a3b       A12: 0x3fff6e90       A13: 0x3fff707c
       A14: 0x00000000       A15: 0x3fff6e90       SAR: 0x00000004  EXCCAUSE: 0x0000001c

Project Include elf and sdkconfig

https://send.firefox.com/download/5aa866a32947a9c1/#8mHzcV1xFABwv7QPYxeKng

donghengqaz commented 5 years ago

这是有空指针传递进去了吧。

donghengqaz commented 5 years ago

你的工程怎么操作?我试了 patch 打不上。

FayeY commented 5 years ago

想请问,这个问题有什么进展吗?

nickfox-taterli commented 5 years ago

空指针没有传递,只要不涉及snprintf进行%f修饰,就不会有问题,进行%d修饰同样的变量,没有问题.

donghengqaz commented 5 years ago

我试下使用你的代码没有什么问题,最近解决了一些踩内存的问题,你可以试试 master 分支。

int HAL_Snprintf(char *str, const int len, const char *fmt, ...)
{
    va_list args;
    int rc;

    va_start(args, fmt);
    rc = vsnprintf(str, len, fmt, args);
    va_end(args);

    return rc;
}

/******************************************************************************
 * FunctionName : app_main
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void app_main(void)
{
    char buf[128];
    float f_data = 3.1415926;

    HAL_Snprintf(buf, 128, "%s, %f\n", esp_get_idf_version(), f_data);

    printf("SDK version:%s\n", buf);
}
donghengqaz commented 5 years ago

Log 如下:

rom BLK0 of EFUSE
I (535) phy_init: phy ver: 1055_12
I (535) reset_reason: RTC reset 1 wakeup 0 store 0, reason is 1
SDK version:v3.2-dev-300-gb2f76cd9-dirty, 3.141593
FayeY commented 5 years ago

由于长时间无更新,此问题暂时关闭;如果最新 master 仍有问题,欢迎 reopen issue。