apple / swift-matter-examples

An Embedded Swift Matter application running on ESP32-C6
https://apple.github.io/swift-matter-examples/tutorials/tutorial-table-of-contents/
Apache License 2.0
212 stars 16 forks source link

Call to Int.random is causing an abort() #5

Closed Henryforce closed 2 weeks ago

Henryforce commented 1 month ago

I am not sure if I should post the issue here or in the embedded Swift repo but in the latest ESP IDF v5.2.2 with the latest Swift toolchain org.swift.59202406131a the call to Int.random(0..<360) in the provided blink example is causing an abort().

Logs are as follows:

abort() was called at PC 0x42004f1f on core 0
0x42004f1f: syscall_not_implemented_aborts at /Users/henryforce/esp/v5.2.2/esp-idf/components/newlib/syscalls.c:27

Stack dump detected
Core  0 register dump:
MEPC    : 0x408004b4  RA      : 0x40805a90  SP      : 0x40810a60  GP      : 0x4080b170  
0x408004b4: panic_abort at /Users/henryforce/esp/v5.2.2/esp-idf/components/esp_system/panic.c:466
0x40805a90: __ubsan_include at /Users/henryforce/esp/v5.2.2/esp-idf/components/esp_system/ubsan.c:313

TP      : 0x40805d70  T0      : 0x37363534  T1      : 0x7271706f  T2      : 0x33323130  
0x40805d70: xQueueGenericCreateStatic at /Users/henryforce/esp/v5.2.2/esp-idf/components/freertos/FreeRTOS-Kernel/queue.c:415 (discriminator 1)

S0/FP   : 0x40810a9c  S1      : 0x40810a9c  A0      : 0x40810a9c  A1      : 0x40810a7e  
A2      : 0x00000000  A3      : 0x40810ac9  A4      : 0x00000001  A5      : 0x4080d000  
A6      : 0x00000000  A7      : 0x76757473  S2      : 0x40810a80  S3      : 0x40810b68  
S4      : 0x4080d000  S5      : 0x00000064  S6      : 0x0000e000  S7      : 0x00000014  
S8      : 0x4202994c  S9      : 0xd0008000  S10     : 0x00000000  S11     : 0x00000000  
T3      : 0x6e6d6c6b  T4      : 0x6a696867  T5      : 0x66656463  T6      : 0x62613938  
MSTATUS : 0x00001881  MTVEC   : 0x40800001  MCAUSE  : 0x00000007  MTVAL   : 0x00000000  
0x40800001: _vector_table at ??:?

MHARTID : 0x00000000  

Backtrace:

panic_abort (details=details@entry=0x40810a9c "abort() was called at PC 0x42004f1f on core 0") at /Users/henryforce/esp/v5.2.2/esp-idf/components/esp_system/panic.c:466
466     *((volatile int *) 0) = 0; // NOLINT(clang-analyzer-core.NullDereference) should be an invalid operation on targets
#0  panic_abort (details=details@entry=0x40810a9c "abort() was called at PC 0x42004f1f on core 0") at /Users/henryforce/esp/v5.2.2/esp-idf/components/esp_system/panic.c:466
#1  0x40805a90 in esp_system_abort (details=details@entry=0x40810a9c "abort() was called at PC 0x42004f1f on core 0") at /Users/henryforce/esp/v5.2.2/esp-idf/components/esp_system/port/esp_system_chip.c:93
#2  0x4080990a in abort () at /Users/henryforce/esp/v5.2.2/esp-idf/components/newlib/abort.c:38
#3  0x42004f22 in syscall_not_implemented_aborts () at /Users/henryforce/esp/v5.2.2/esp-idf/components/newlib/syscalls.c:27
#4  0x42015d08 in _getentropy_fail () at /Users/brnomac003/.gitlab-runner/builds/qR2TxTby/1/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdlib/arc4random.h:69
#5  _rs_stir () at /Users/brnomac003/.gitlab-runner/builds/qR2TxTby/1/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdlib/arc4random.c:102
#6  _rs_stir_if_needed (len=len@entry=8) at /Users/brnomac003/.gitlab-runner/builds/qR2TxTby/1/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdlib/arc4random.c:125
#7  0x42015e3a in _rs_random_buf (n=8, _buf=0x40810b68) at /Users/brnomac003/.gitlab-runner/builds/qR2TxTby/1/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdlib/arc4random.c:162
#8  arc4random_buf (buf=0x40810b68, n=8) at /Users/brnomac003/.gitlab-runner/builds/qR2TxTby/1/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdlib/arc4random.c:214
#9  0x4200e4d0 in $sSGsE4next10upperBoundqd__qd___ts17FixedWidthIntegerRd__SURd__lFs27SystemRandomNumberGeneratorV_SuTg5 ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
ELF file SHA256: 796cfb6e8

I (761) esp_core_dump_flash: Save core dump to flash...
I (767) esp_core_dump_flash: Erase flash 4096 bytes @ 0x3e6000
I (805) esp_core_dump_flash: Write end offset 0xd44, check sum length 4
I (805) esp_core_dump_flash: Core dump has been saved to flash.

The logs seem to confirm an issue with the random call with the current setup in my local machine.

My current workaround is to avoid using Int.random and use the native ESP Random Number Generator API by replacing:

led.color = .hueSaturation(Int.random(in: 0 ..< 360), 100)

for

let randomColor = Int(esp_random() % 360)
led.color = .hueSaturation(randomColor, 100)

around line 24 of Main.swift (note that I also included #include "esp_random.h" in the BridgingHeader.h file)

kubamracek commented 3 weeks ago

I think that this thread is discussing the same issue and it mentions some workarounds/solutions: https://github.com/apple/swift-embedded-examples/issues/17

Henryforce commented 2 weeks ago

Thanks for sharing! I will close this item as it is being tracked in that thread instead.