From a45fcce631e8ce1dbe4f23af702394c1445090e5 Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Mon, 22 Aug 2022 11:56:51 +0800 Subject: [PATCH] newlib: Add workaround for printf functions using 32-bit time_t on first call sizeof(time_t) was previously switched from 4 to 8, ROM functions that use time_t or dependent types (such as "struct stat") are no longer called due as they still treat sizeof(time_t) as 4 (see commit 24c20d18). However, there is a ROM callpath that was left out. If putchar is the first stdio print related call, the call path will result in cantwrite() -> __swsetup_r() -> __smakebuf_r() -> __swhatbuf_r() using the ROM "struct stat" (where sizeof(time_t)==4). Instead of removing all printf related ROM newlib functions (which will result in increased binary size), this commit adds a workaround to setup the stdio files before any print related calls occur. This results in cantwrite() always returning false, thus the callpath described above never being reached. Closes https://github.com/espressif/esp-idf/issues/9269 --- components/esp_rom/esp32/esp_rom_caps.h | 19 ++++++------------- components/esp_rom/esp32c3/esp_rom_caps.h | 1 + components/esp_rom/esp32s2/esp_rom_caps.h | 19 ++++++------------- components/esp_rom/esp32s3/esp_rom_caps.h | 1 + components/esp_system/startup.c | 15 +++++++++++++++ 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/components/esp_rom/esp32/esp_rom_caps.h b/components/esp_rom/esp32/esp_rom_caps.h index 6188ef52a7..1d19be8571 100644 --- a/components/esp_rom/esp32/esp_rom_caps.h +++ b/components/esp_rom/esp32/esp_rom_caps.h @@ -1,16 +1,8 @@ -// Copyright 2020 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once @@ -18,3 +10,4 @@ #define ESP_ROM_HAS_CRC_BE (1) // ROM CRC library supports Big Endian #define ESP_ROM_HAS_JPEG_DECODE (1) // ROM has JPEG decode library #define ESP_ROM_SUPPORT_MULTIPLE_UART (1) // ROM has multiple UARTs available for logging +#define ESP_ROM_NEEDS_SWSETUP_WORKAROUND (1) // ROM uses 32-bit time_t. A workaround is required to prevent printf functions from crashing diff --git a/components/esp_rom/esp32c3/esp_rom_caps.h b/components/esp_rom/esp32c3/esp_rom_caps.h index 7a210f1a53..977d73fc90 100644 --- a/components/esp_rom/esp32c3/esp_rom_caps.h +++ b/components/esp_rom/esp32c3/esp_rom_caps.h @@ -21,3 +21,4 @@ #define ESP_ROM_USB_SERIAL_DEVICE_NUM (3) // UART uses USB_SERIAL_JTAG port in ROM. #define ESP_ROM_HAS_RETARGETABLE_LOCKING (1) // ROM was built with retargetable locking #define ESP_ROM_HAS_ERASE_0_REGION_BUG (1) // ROM has esp_flash_erase_region(size=0) bug +#define ESP_ROM_NEEDS_SWSETUP_WORKAROUND (1) // ROM uses 32-bit time_t. A workaround is required to prevent printf functions from crashing diff --git a/components/esp_rom/esp32s2/esp_rom_caps.h b/components/esp_rom/esp32s2/esp_rom_caps.h index 0f63dfb542..1078ff8997 100644 --- a/components/esp_rom/esp32s2/esp_rom_caps.h +++ b/components/esp_rom/esp32s2/esp_rom_caps.h @@ -1,18 +1,11 @@ -// Copyright 2020 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once #define ESP_ROM_HAS_CRC_LE (1) // ROM CRC library supports Little Endian #define ESP_ROM_SUPPORT_MULTIPLE_UART (1) // ROM has multiple UARTs available for logging +#define ESP_ROM_NEEDS_SWSETUP_WORKAROUND (1) // ROM uses 32-bit time_t. A workaround is required to prevent printf functions from crashing diff --git a/components/esp_rom/esp32s3/esp_rom_caps.h b/components/esp_rom/esp32s3/esp_rom_caps.h index 5955f2c371..65847c4064 100644 --- a/components/esp_rom/esp32s3/esp_rom_caps.h +++ b/components/esp_rom/esp32s3/esp_rom_caps.h @@ -22,3 +22,4 @@ #define ESP_ROM_HAS_RETARGETABLE_LOCKING (1) // ROM was built with retargetable locking #define ESP_ROM_USB_SERIAL_DEVICE_NUM (4) // The serial port ID (UART, USB, ...) of USB_SERIAL_JTAG in the ROM. #define ESP_ROM_HAS_ERASE_0_REGION_BUG (1) // ROM has esp_flash_erase_region(size=0) bug +#define ESP_ROM_NEEDS_SWSETUP_WORKAROUND (1) // ROM uses 32-bit time_t. A workaround is required to prevent printf functions from crashing diff --git a/components/esp_system/startup.c b/components/esp_system/startup.c index 001d8cf56a..4b6f4e491f 100644 --- a/components/esp_system/startup.c +++ b/components/esp_system/startup.c @@ -54,6 +54,7 @@ #include "brownout.h" +#include "esp_rom_caps.h" #include "esp_rom_sys.h" // [refactor-todo] make this file completely target-independent @@ -293,6 +294,20 @@ static void do_core_init(void) _GLOBAL_REENT->_stdin = fopen(default_stdio_dev, "r"); _GLOBAL_REENT->_stdout = fopen(default_stdio_dev, "w"); _GLOBAL_REENT->_stderr = fopen(default_stdio_dev, "w"); +#if ESP_ROM_NEEDS_SWSETUP_WORKAROUND + /* + - This workaround for printf functions using 32-bit time_t after the 64-bit time_t upgrade + - The 32-bit time_t usage is triggered through ROM Newlib functions printf related functions calling __swsetup_r() on + the first call to a particular file pointer (i.e., stdin, stdout, stderr) + - Thus, we call the toolchain version of __swsetup_r() now (before any printf calls are made) to setup all of the + file pointers. Thus, the ROM newlib code will never call the ROM version of __swsetup_r(). + - See IDFGH-7728 for more details + */ + extern int __swsetup_r(struct _reent *, FILE *); + __swsetup_r(_GLOBAL_REENT, _GLOBAL_REENT->_stdout); + __swsetup_r(_GLOBAL_REENT, _GLOBAL_REENT->_stderr); + __swsetup_r(_GLOBAL_REENT, _GLOBAL_REENT->_stdin); +#endif // ESP_ROM_NEEDS_SWSETUP_WORKAROUND #else // defined(CONFIG_VFS_SUPPORT_IO) && !defined(CONFIG_ESP_CONSOLE_NONE) _REENT_SMALL_CHECK_INIT(_GLOBAL_REENT); #endif // defined(CONFIG_VFS_SUPPORT_IO) && !defined(CONFIG_ESP_CONSOLE_NONE)