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

memory corruption for seq.add(seq) with gc:arc and d:useMalloc #14983

Closed
ghost opened this issue Jul 14, 2020 · 10 comments
Closed

memory corruption for seq.add(seq) with gc:arc and d:useMalloc #14983

ghost opened this issue Jul 14, 2020 · 10 comments

Comments

@ghost
Copy link

ghost commented Jul 14, 2020

Example

nim c -d:release -d:useMalloc --gc:arc file.nim

var s = @[1,2,3]
for _ in 1..6:
  s.add(s)
echo s

Current Output

@[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1769808, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

Expected Output

only with numbers 1,2,3

Possible Solution

  • not using gc:arc and d:useMalloc together
  • passing s2 which is a shallowCopy of s as second parameter for add

Additional Information

Nim Compiler Version 1.3.5 [Windows: amd64]
Compiled at 2020-07-02
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 366b9a7e4a067cece3b1215de7fb4dffc703e88a
active boot switches: -d:release
@ghost
Copy link
Author

ghost commented Jul 14, 2020

Maybe this is the same as here #14472, my nim version is from 2020-07-02.

@Yardanico
Copy link
Collaborator

Yardanico commented Jul 14, 2020

Can't reproduce on latest devel, I get same results with refc/arc/arc + useMalloc
Can see valgrind errors

@ghost
Copy link
Author

ghost commented Jul 14, 2020

It also happens with

Nim Compiler Version 1.3.5 [Windows: amd64]
Compiled at 2020-07-13
Copyright (c) 2006-2020 by Andreas Rumpf

active boot switches: -d:release

and

gcc (Rev5, Built by MSYS2 project) 5.3.0

@cooldome
Copy link
Member

cooldome commented Sep 4, 2020

Retested.
Works for me as of 04-Sep-2020

@ghost
Copy link
Author

ghost commented Sep 4, 2020

I can't confirm that it works with

Nim Compiler Version 1.3.5 [Windows: amd64]
Compiled at 2020-09-04
Copyright (c) 2006-2020 by Andreas Rumpf

active boot switches: -d:release

and

gcc (Rev5, Built by MSYS2 project) 5.3.0

Update: I now compiled the latest nim version from source and the result is the same as before.

Compiling with nim cpp -d:release -d:useMalloc --gc:arc file.nim instead of c gives the correct output, but maybe there are errors with valgrind too.

@ghost
Copy link
Author

ghost commented Sep 5, 2020

Here is a similar code showing the problem more obviously:

Compile with nim c -d:release -d:useMalloc --gc:arc file.nim

file.nim

proc add2*[T](x: var seq[T], y: openArray[T]) =
  var y2 = @y # works without this line
  let xl = x.len
  setLen(x, xl + y.len)
  for i in 0..high(y):
    x[xl+i] = y[i]


var s = @[1,2,3]
add2(s, s)
echo s

@cooldome
Copy link
Member

cooldome commented Sep 6, 2020

So, it is the parameter aliasing problem. First parameter is var and second is not. If you pass the same s as both arguments, you get undefined behavior.

IMO, we are leaning towards that the compiler should not allow it, detecting it is the aliasing problem. In future it would not compile and users will have to write:

var s = @[1,2,3]
add2(s, copy(s))
echo s

@Araq
Copy link
Member

Araq commented Sep 7, 2020

I'm leaning to the compiler producing a copy instead when the aliasing can cause trouble. No need to break code.

Araq added a commit that referenced this issue Sep 13, 2020
@ghost
Copy link
Author

ghost commented Sep 13, 2020

@Araq should I open a new issue for the generic problem like shown in #14983 (comment) ?
I think your fix #15320 adresses only the specific case for system.add.

@Araq Araq removed the Showstopper label Sep 14, 2020
@Araq
Copy link
Member

Araq commented Sep 14, 2020

The more general problem is covered in https://nim-lang.org/docs/manual_experimental.html#aliasing-restrictions-in-parameter-passing with an expected implementation in 2021.

@Araq Araq closed this as completed in e9fa486 Sep 25, 2020
mildred pushed a commit to mildred/Nim that referenced this issue Jan 11, 2021
* fixes nim-lang#14983

* allow bootstrapping with 0.20

* added a test case for the new system.add with a sink parameter

* make npeg green again
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

3 participants