nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.58k stars 1.47k forks source link

Float to string causes SIGSEGV with --hotcodereloading:on #15081

Open stisa opened 4 years ago

stisa commented 4 years ago

Converting a float to string causes a crash at runtime with --hotcodereloading:on.

Example

# logic.nim
proc logic*() =
  echo 4.21  # this works
  var x = 12.4
  echo x # this crashes
logic()

compile and run with:

nim c -r --hotcodereloading:on logic.nim

This will crash with SIGSEGV: Illegal storage access. at strmantle.nim(94) addFloat

Current Output

Hint: used config file 'C:\Dev\nim-git\config\nim.cfg' [Conf]
Hint: used config file 'C:\Dev\nim-git\config\config.nims' [Conf]
....
Hint: 21871 lines; 0.361s; 20.5MiB peakmem; Debug build; 
proj: C:\Users\stisa\OneDrive\Progetti\hcr\logic.nim; out: C:\Users\stisa\OneDrive\Progetti\hcr\logic.exe [SuccessX]
Hint: C:\Users\stisa\OneDrive\Progetti\hcr\logic.exe  [Exec]
4.21
Traceback (most recent call last)
C:\Users\stisa\OneDrive\Progetti\hcr\logic.nim(6) logic
C:\Users\stisa\OneDrive\Progetti\hcr\logic.nim(4) logic
C:\Dev\nim-git\lib\system\strmantle.nim(99) nimFloatToStr
C:\Dev\nim-git\lib\system\strmantle.nim(94) addFloat
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Error: execution of an external program failed: 'C:\Users\stisa\OneDrive\Progetti\hcr\logic.exe '

Expected Output

4.21
12.4

Additional Information

Compiler version (nim -v):

Compiled at 2020-07-23
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 925dd92e07bd8b031c9f237ceb9e352c34a97d64
active boot switches: -d:release

Discovered this when working on the hcr branch of jupyternim (here)

stisa commented 4 years ago

May be linked to #11974 ?

stisa commented 4 years ago

More info and possible workaround: moving writeFloatToBuffer from formatfloat.nim to strmantle (exporting the other two procs and using from formatfloat import nil to avoid some ambiguous calls) seems enough to fix this.

darthdeus commented 1 year ago

I just tested this on Linux and it seems to work without any issues

[pipik] ~/projects/nimtest/bug !dot!
$ LD_LIBRARY_PATH=. nim c -r --hotcodereloading:on logic.nim
Hint: used config file '/home/darth/.choosenim/toolchains/nim-1.6.8/config/nim.cfg' [Conf]
Hint: used config file '/home/darth/.choosenim/toolchains/nim-1.6.8/config/config.nims' [Conf]
.........................................................
Hint: gc: refc; opt: none (DEBUG BUILD, `-d:release` generates faster code)
26696 lines; 0.131s; 25.523MiB peakmem; proj: /home/darth/projects/nimtest/bug/logic.nim; out: /home/darth/projects/nimtest/bug/logic [SuccessX]
Hint: /home/darth/projects/nimtest/bug/logic  [Exec]
4.21
12.4

Using latest stable

$ nim -v
Nim Compiler Version 1.6.8 [Linux: amd64]
Compiled at 2022-09-27
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: c9f46ca8c9eeca8b5f68591b1abe14b962f80a4c
active boot switches: -d:release

Maybe the bug is Windows only?

stisa commented 1 year ago

Might be, or it was fixed as a side effect of something else. I'll try it out on my machine and report back