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

gc:arc - SIGSEGV for rawAlloc on windows #16365

Closed
SFR0815 opened this issue Dec 15, 2020 · 12 comments
Closed

gc:arc - SIGSEGV for rawAlloc on windows #16365

SFR0815 opened this issue Dec 15, 2020 · 12 comments

Comments

@SFR0815
Copy link

SFR0815 commented Dec 15, 2020

Hi!

This replaces issue #15954.

Example

I execute a_label.nim (see https://gist.github.com/SFR0815/59cf9a9be74fec43be01a46a0f53a47d) and execute using

nim c -r --gc:arc  a_label.nim

using

$ nim -v
Nim Compiler Version 1.4.2 [Windows: amd64] 
Compiled at 2020-11-30
Copyright (c) 2006-2020 by Andreas 

Current Output

[Suite] Uiapi List - SubEntitySelectionList 
[OK]  .. 1 
[OK]  .. 2      
[OK]  .. 3   
[OK]  .. 4    
[OK]  .. 5     
[OK]  .. 6      
[OK]  .. 7 
Traceback (most recent call last)                                                                                                                                                                                                                                                                                        ...\ut_label.nim(145) ut_label       
...\ut_label.nim(100) doSomething           
...\iso8601.nim(399) getDateTime  
...\.choosenim\toolchains\nim-1.4.2\lib\pure\times.nim(1643) initTimeFormat                                                                                                                                                                                                                                   SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Expected Output

... like executing using nim c -r a_label.nim ...

[Suite] Uiapi List - SubEntitySelectionList       
[OK]  .. 1   
[OK]  .. 2     
[OK]  .. 3    
[OK]  .. 4  
[OK]  .. 5      
[OK]  .. 6
[OK]  .. 7   
[OK]  .. 8         
[OK]  .. 9        
[OK]  .. 10   
[OK]  .. 11      
[OK]  .. 12      
[OK]  .. 13    
[OK]  .. 14       
[OK]  .. 15        
[OK]  .. 16              
[OK]  .. 17        
[OK]  .. 18

Possible Solution

@timotheecour
Copy link
Member

timotheecour commented Dec 15, 2020

@LucasUllon
Copy link

compiling the file with nim c --gc: arc -d: useMalloc -g ut_label.nim and using valgrind to see exactly where the code fails you get this: https://gist.github.com/LucasUllon/a55a8f54b0876ac8466d524085362111

@timotheecour
Copy link
Member

timotheecour commented Dec 15, 2020

that's good but I mean minimizing the source code to get a minimum reproducible example, as is the case for most other issues that are reported in this repo

@SFR0815
Copy link
Author

SFR0815 commented Dec 16, 2020

  1. ad nim c -r a_label.nim: Executes the test as above without any error. No issue in practice. Everything works nicely.

  2. ad minimization:
    Hope the following is short enough:

Example

import 
  unittest, strutils

proc getShortName(
              name: string
              ): string =
  result = name
  result.removeSuffix("_something")

proc composeString(
              some_string: string
              ): string = 
  for i in some_string.items:
    result.add ":" & $i
    # derived from times, line 1744 (formatPattern):
    #    result.add h & ":" & m

suite "Dings":
  test " .. 1":
    discard getShortName(name = "name_something")
  test " .. 2":
    discard composeString("something") 
    discard getShortName(name = "name_something")

Output

[Suite] Dings
  [OK]  .. 1
Traceback (most recent call last)
…\ut_label.nim(22) ut_label
…\ut_label.nim(14) composeString
…\.choosenim\toolchains\nim-1.4.2\lib\system\alloc.nim(958) alloc0
…\.choosenim\toolchains\nim-1.4.2\lib\system\alloc.nim(955) alloc
…\.choosenim\toolchains\nim-1.4.2\lib\system\alloc.nim(787) rawAlloc
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Expected Output

... produced by executing nim c -r a_label.nim ...

[Suite] Dings
  [OK]  .. 1
  [OK]  .. 2

Comments:

  • no exceptions raised if procs are executed outside the test suite
  • no exceptions raised if the first test is equal to the second (including the composeString)
  • no exceptions raised if the second test is equal to the first (excluding the composeString)

@timotheecour
Copy link
Member

timotheecour commented Dec 16, 2020

much better thanks! can't reproduce on osx so maybe it's windows specific

nim r --gc:arc main
[Suite] Dings
  [OK]  .. 1
  [OK]  .. 2

ditto with c -r, ditto with devel 1.5.1 9ef7c01 or 1.4.2

next steps:

  • trying on earlier versions to see if it's a regression or if it has always been a bug
  • further minimizing, ideally down to zero imports

(not on windows, sorry)

@timotheecour timotheecour changed the title gc:arc - Illegal storage access II gc:arc - SIGSEGV for rawAlloc on windows Dec 16, 2020
@SFR0815
Copy link
Author

SFR0815 commented Dec 16, 2020

We will run on Linux. If not reproducible we will have to investigate further (as original issue showed up on Linux as well).

@SFR0815
Copy link
Author

SFR0815 commented Dec 16, 2020

On windows outcome is the same for 1.4.2 (see above), 1.5.1 (most recent) and 1.2.6

@SFR0815
Copy link
Author

SFR0815 commented Dec 16, 2020

Example (zero imports)

File "a_label.nim":

template doThis(body) =
  try:
    body
  except:
    discard
  body
 
# Task 1: 
doThis:
  # Task 1_a: 
  var test_string_a = "name_something"
  echo test_string_a.len()
  let new_len_a = test_string_a.len - "_something".len()
  test_string_a.setLen new_len_a
  
  echo "First tasks completed."

# Task 2: 
doThis:
  # Task 2_a 
  var test_string: string
  let some_string = "something"            
  for i in some_string.items:
    test_string.add $i
  
  # Task 2_b
  var test_string_b = "name_something"
  let new_len_b = test_string_b.len - "_something".len()
  test_string_b.setLen new_len_b
  
  echo "Second tasks completed."

Output

First tasks completed.
First tasks completed.
Second tasks completed.
Traceback (most recent call last)
...\ut_label.nim(24) a_label
...\.choosenim\toolchains\nim-#devel\lib\system\alloc.nim(958) alloc0
...\.choosenim\toolchains\nim-#devel\lib\system\alloc.nim(955) alloc
...\.choosenim\toolchains\nim-#devel\lib\system\alloc.nim(787) rawAlloc
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Observations

The code with the following changes as follows and running with nim c -r --gc:arc a_label.nim does NOT throw an exception:

  1. Removing the try/except from doThis
  2. Removing Task 1
  3. Removing Task 2_b from Task 2
  4. Changing the value of test_string_a/b to "e_something" (strings with length < 12 seem to work)
  5. Changing test_string.add $i to test_string.add i

nim c -r a_label.nim works without any issue.

@LucasUllon
Copy link

Hi @timotheecour , testing the a_label code on Linux I get the same output error, so the error is not only on windows.

OUTPUT

First tasks completed.
First tasks completed.
Second tasks completed.
Traceback (most recent call last)
/home/lucas/Desktop/valgrind test/a_label.nim(24) a_label
/home/lucas/.choosenim/toolchains/nim-1.4.0/lib/system/alloc.nim(976) realloc
/home/lucas/.choosenim/toolchains/nim-1.4.0/lib/system/alloc.nim(955) alloc
/home/lucas/.choosenim/toolchains/nim-1.4.0/lib/system/alloc.nim(787) rawAlloc
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

@Araq
Copy link
Member

Araq commented Dec 17, 2020

Almost have a fix ready...

Araq added a commit that referenced this issue Dec 17, 2020
@Araq Araq closed this as completed in 868c31e Dec 17, 2020
@SFR0815
Copy link
Author

SFR0815 commented Dec 18, 2020

Great! Works :-)

Thanks for your help!

Gesegnetes Weihnachtsfest aus Florida in die Heimat!

Regards
Stephan

@Araq
Copy link
Member

Araq commented Dec 18, 2020

Danke, gleichfalls!

narimiran pushed a commit that referenced this issue Dec 19, 2020
(cherry picked from commit 868c31e)
mildred pushed a commit to mildred/Nim that referenced this issue Jan 11, 2021
ardek66 pushed a commit to ardek66/Nim that referenced this issue Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants