New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alignment problems with PROGMEM Strings via LogInfo #16

Closed
tshumay opened this Issue Jun 24, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@tshumay

tshumay commented Jun 24, 2017

Wondering if I'm doing something wrong over here, but this, along with other IoT Hub demos targeted at the ESP8266 Architecture are experiencing alignment exceptions for me on string operations related to LogInfo() calls.

ESP8266 requires 32-bit alignment on reads from PROGMEM, but I don't think anything guarantees this to be the case - I get an exceptions almost immediately when running this demo that looks like the one below. Am I missing a compiler flag or something that fixes this?

⸮Fatal exception 3(LoadStoreErrorCause):
epc1=0x4000bf64, epc2=0x00000000, epc3=0x00000000, excvaddr=0x402472a9, depc=0x00000000

Exception (3):
epc1=0x4000bf64 epc2=0x00000000 epc3=0x00000000 excvaddr=0x402472a9 depc=0x00000000

ctx: cont
sp: 3fff1bd0 end: 3fff1e70 offset: 01a0

stack>>>
3fff1d70: feefeffe feefeffe feefeffe feefeffe
3fff1d80: 401062a3 feefeffe feefeffe feefeffe
3fff1d90: feefeffe feefeffe feefeffe feefeffe
3fff1da0: feefeffe feefeffe feefeffe feefeffe
3fff1db0: feefeffe feefeffe feefeffe 0001c200
3fff1dc0: 0000001c 00000000 3fff0e18 401004d8
3fff1dd0: feefeffe feefeffe 3fff3394 4020baa1
3fff1de0: 00000000 00000001 feefeffe feefeffe
3fff1df0: feefeffe feefeffe feefeffe 0001c200
3fff1e00: 3fff1e20 4020c4e5 3fff0e18 00000000
3fff1e10: 3fffdad0 feefeffe feefeffe feefeffe
3fff1e20: 402472a9 3fffdd3c 00000001 ffffffff
3fff1e30: 0006001c 00000000 3fff0e18 4020c4dc
3fff1e40: feefeffe feefeffe 3fff0e3d 4020c732
3fff1e50: feefeffe feefeffe 3fff0e3d 4020f5b0
3fff1e60: feefeffe feefeffe 3fff0e50 40100718
<<<stack<<<

ets Jan 8 2013,rst cause:2, boot mode:(1,6)

@noopkat

This comment has been minimized.

noopkat commented Jul 6, 2017

+1 on this issue, I've been trying to resolve for a few hours tonight but can't pin down the exact cause. I occasionally also get a watchdog timeout.

@tshumay

This comment has been minimized.

tshumay commented Jul 6, 2017

@noopkat I came up with an interim solution that has been working well over here. The problem is actually in the azure_c_shared_utility library.

The macros for LOG and LogInfo as written in that lib do not properly assure 32-bit aligned reads from Flash memory causing all kinds of problems.

I did a quick-and-dirty hack to put the string buffer used inside those macros onto the main stack. It's not quite ideal in the sense that I'm using stack all over the place, but there's a lot of RAM on the ESP8266 so it hasn't been a problem in practice.

Around line 95 in AzureIoTUtility/src/azure_c_shared_utility/xlogging.h, I replaced the macros as follows:

define LOG(log_category, log_options, FORMAT, ...) {
char tmp[256];
az_c_strncpy_P(tmp, PSTR(FORMAT), 256);
os_printf(tmp, ##VA_ARGS);
os_printf("\r\n");
}

#define LogInfo(FORMAT, ...) {
char tmp[256];
az_c_strncpy_P(tmp, PSTR(FORMAT), 256);
os_printf(tmp, ##VA_ARGS);
os_printf("\r\n");
}

Basically, I am copying the format string out of Flash using the az_c_strncpy_P() call which is alignment safe, and putting it in tmp so that the os_printf() call works on main RAM instead of Flash as it's not guaranteed to handle the 32-bit alignment requirements of Flash RAM access properly.

@SummerSun

This comment has been minimized.

Collaborator

SummerSun commented Aug 3, 2017

@tshumay @noopkat Thank you for reporting this. You are correct. LogInfo causes the alignment issue and then leads to exception in mac. We have removed LogInfo code in the tutorial and use Serial to print. Did not see any benefit of using loginfo instead of serial print.
We have verified that issues all gone. Please try again. Thank you.

@SummerSun SummerSun closed this Aug 3, 2017

@csiebler

This comment has been minimized.

csiebler commented Mar 14, 2018

Just ran into this problem on NodeMCU Amica on macOS High Sierra. Has this been fixed? Just had a look at AzureIoTUtility/src/azure_c_shared_utility/xlogging.h and still found this:

#define LOG(log_category, log_options, FORMAT, ...) { \
        const char* __localFORMAT = PSTR(FORMAT); \
        os_printf(__localFORMAT, ##__VA_ARGS__); \
        os_printf("\r\n"); \
}

#define LogInfo(FORMAT, ...) { \
        const char* __localFORMAT = PSTR(FORMAT); \
        os_printf(__localFORMAT, ##__VA_ARGS__); \
        os_printf("\r\n"); \
}
#define LogError LogInfo

Replacing it with the code from @tshumay worked (at least for now):

#define LOG(log_category, log_options, FORMAT, ...) { \
        char tmp[256]; \
        az_c_strncpy_P(tmp, PSTR(FORMAT), 256); \
        os_printf(tmp, ##__VA_ARGS__); \
        os_printf("\r\n"); \
}

#define LogInfo(FORMAT, ...) { \
        char tmp[256]; \
        az_c_strncpy_P(tmp, PSTR(FORMAT), 256); \
        os_printf(tmp, ##__VA_ARGS__); \
        os_printf("\r\n"); \
}
#define LogError LogInfo

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment