Skip to content
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

config.nims: Support Nim Apps for RISC-V 32-bit and 64-bit #6

Closed
wants to merge 1 commit into from

Conversation

lupyuen
Copy link

@lupyuen lupyuen commented Jan 3, 2024

Summary

NuttX Build fails when it compiles examples/hello_nim for RISC-V (32-bit and 64-bit). That's because the Nim Config Script config.nims couldn't identify the Nim Target Platform: riscv32 or riscv64.

This PR fixes config.nims so that Nim Compiler correctly derives the Nim Target Platform (riscv32 or riscv64), by searching NuttX .config for CONFIG_ARCH_FAMILY=rv32 or rv64.

This logic is slightly different from the Nim Targets arm and arm64, which are currently derived from CONFIG_ARCH=arm and arm64.

config.nims is explained in this article.

Impact

With this PR, Nim Apps will build correctly for 32-bit and 64-bit RISC-V.

There is no impact on other platforms, like Arm and Arm64.

Testing

We tested by building QEMU NuttX with examples/hello_nim enabled for RISC-V 32-bit, RISC-V 64-bit and Arm64 (for regression testing).

RISC-V 32-bit

Before Fixing: NuttX Build fails for hello_nim because arch and cpu in Nim Compiler are empty:

$ tools/configure.sh rv-virt:nsh
$ kconfig-tweak --enable CONFIG_EXAMPLES_HELLO_NIM
$ make
* arch:    
/workspaces/bookworm/apps/config.nims(1, 2)
Error: argument for command line option expected: '--cpu'

(Failed Build Log for 32-bit RISC-V)

After Fixing: NuttX Build succeeds with the correct arch and cpu in Nim Compiler:

$ make
* arch:    riscv32
Hint: used config file '/workspaces/bookworm/apps/config.nims' [Conf]

(Fixed Build Log for 32-bit RISC-V)

RISC-V 64-bit

NuttX Build succeeds with the correct arch and cpu in Nim Compiler:

$ tools/configure.sh rv-virt:nsh64
$ kconfig-tweak --enable CONFIG_EXAMPLES_HELLO_NIM
$ make
* arch:    riscv64
Hint: used config file '/workspaces/bookworm/apps/config.nims' [Conf]

(Fixed Build Log for 64-bit RISC-V)

Arm64

For Regression Testing: NuttX Build succeeds with the correct arch and cpu in Nim Compiler:

$ tools/configure.sh qemu-armv8a:nsh
$ kconfig-tweak --enable CONFIG_EXAMPLES_HELLO_NIM
$ make
* arch:    arm64
Hint: used config file '/workspaces/bookworm/apps/config.nims' [Conf]

(Build Log for Arm64)

Nim Exception

For Arm64: hello_nim runs correctly on Arm64 QEMU:

nsh> hello_nim
Hello from task 1! loops: 0
Hello from task 2! loops: 0
...
Hello from task 3! loops: 2

(NuttX Log for Arm64)

For RISC-V 32-bit and 64-bit: hello_nim halts with a Load Access Fault for 32-bit RISC-V:

nsh> hello_nim
Hello from task 1! loops: 0
riscv_exception: EXCEPTION: Load access fault. MCAUSE: 00000005, EPC: 8000dd10, MTVAL: 00000038

Load Access Fault at nuttx/fs/inode/fs_files.c:607
  /* if f_inode is NULL, fd was closed */
  if ((*filep)->f_inode == NULL)
8000dd10:	451c                	lw	a5,8(a0)

(NuttX Exception for 32-bit RISC-V)

hello_nim also halts with a Load Access Fault for 64-bit RISC-V:

nsh> hello_nim
Hello from task 1! loops: 0
riscv_exception: EXCEPTION: Load access fault. MCAUSE: 0000000000000005, EPC: 0000000080002432, MTVAL: d0ef000555638482

Load Access Fault at nuttx/sched/semaphore/sem_wait.c:93
nxsem_wait():
  /* Make sure we were supplied with a valid semaphore. */
  /* Check if the lock is available */
  if (sem->semcount > 0)
    80002432:	00055783          	lhu	a5,0(a0)

(NuttX Exception for 64-bit RISC-V)

This will be fixed later. For now, we tested a Simple Nim Function that runs correctly on 32-bit and 64-bit RISC-V:

## Simple Nim Function that prints something
$ cat ../apps/examples/hello_nim/hello_nim_async.nim
proc hello_nim() {.exportc, cdecl.} =
  echo "Hello Nim!"
  GC_runOrc()

$ make
$ qemu-system-riscv32 \
  -M virt,aclint=on \
  -cpu rv32 \
  -smp 8 \
  -bios none \
  -kernel nuttx \
  -nographic

NuttShell (NSH) NuttX-12.0.3
nsh> hello_nim
Hello Nim!

(Simple Nim Log for 32-bit RISC-V)

(Simple Nim Log for 64-bit RISC-V)

NuttX Build fails when it compiles `examples/hello_nim` for RISC-V (32-bit and 64-bit). That's because the Nim Config Script `config.nims` couldn't identify the Nim Target Platform: `riscv32` or `riscv64`.

This PR fixes `config.nims` so that Nim Compiler correctly derives the Nim Target Platform (`riscv32` or `riscv64`), by searching NuttX `.config` for `CONFIG_ARCH_FAMILY=rv32` or `rv64`.

This logic is slightly different from the Nim Targets `arm` and `arm64`, which are currently derived from `CONFIG_ARCH=arm` and `arm64`.

`config.nims` is explained in this article: https://lupyuen.github.io/articles/nim#inside-nim-on-nuttx
@lupyuen lupyuen closed this Jan 3, 2024
lupyuen pushed a commit that referenced this pull request Dec 24, 2024
…ter was read

    #0 0x43c7443a in strnlen nuttx/libs/libc/string/lib_strnlen.c:42
    #1 0x43c698aa in vsprintf_internal nuttx/libs/libc/stdio/lib_libvsprintf.c:933
    #2 0x43c6ca80 in lib_vsprintf nuttx/libs/libc/stdio/lib_libvsprintf.c:1383
    #3 0x4409c0bd in vsnprintf nuttx/libs/libc/stdio/lib_vsnprintf.c:72
    #4 0x47137cd0 in vcmocka_print_error apps/testing/cmocka/cmocka/src/cmocka.c:2097
    #5 0x47139573 in cmocka_print_error apps/testing/cmocka/cmocka/src/cmocka.c:2422
    #6 0x471376ff in string_equal_display_error apps/testing/cmocka/cmocka/src/cmocka.c:1410
    #7 0x471379a0 in _assert_string_equal apps/testing/cmocka/cmocka/src/cmocka.c:1952
    #8 0x4433d972 in read_default apps/testing/drivertest/drivertest_uart.c:242
    #9 0x4713c6cd in cmocka_run_one_test_or_fixture apps/testing/cmocka/cmocka/src/cmocka.c:3029
    apache#10 0x4713d487 in cmocka_run_one_tests apps/testing/cmocka/cmocka/src/cmocka.c:3143
    apache#11 0x4713f2ca in _cmocka_run_group_tests apps/testing/cmocka/cmocka/src/cmocka.c:3294
    apache#12 0x443444c6 in cmocka_driver_uart_main apps/testing/drivertest/drivertest_uart.c:358
    apache#13 0x4409a472 in nxtask_startup nuttx/libs/libc/sched/task_startup.c:72
    apache#14 0x43dc92e7 in nxtask_start nuttx/sched/task/task_start.c:116
    apache#15 0x43e31f00 in pre_start nuttx/arch/sim/src/sim/sim_initialstate.c:52

Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
lupyuen pushed a commit that referenced this pull request Jan 20, 2025
…int to a stack space

==1805058==ERROR: AddressSanitizer: heap-use-after-free on address 0xe18126a0 at pc 0x52b06320 bp 0xd7b13ee8 sp 0xd7b13ed8
READ of size 1 at 0xe18126a0 thread T0
    #0 0x52b0631f in tre_parse regex/regcomp.c:1356
    #1 0x52b2b1d0 in regcomp regex/regcomp.c:3710
    #2 0x48f55435 in c_regexmatch cmocka/src/cmocka.c:494
    #3 0x48f65bcf in _cmocka_run_group_tests cmocka/src/cmocka.c:3252
    #4 0x48f67e2d in cmocka_fs_test_main apps/testing/testsuites/kernel/fs/cmocka_fs_test.c:201
    #5 0x46210b2a in nxtask_startup sched/task_startup.c:72
    #6 0x45ff40fb in nxtask_start task/task_start.c:116
    #7 0x462695bb in pre_start sim/sim_initialstate.c:52

Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant