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 in tmarshall.nim #9754

Closed
timotheecour opened this issue Nov 19, 2018 · 9 comments
Closed

memory corruption in tmarshall.nim #9754

timotheecour opened this issue Nov 19, 2018 · 9 comments

Comments

@timotheecour
Copy link
Member

comment either and it'll work;

import tests/stdlib/tmarshal
import tests/stdlib/tparsesql

nim c -r bug/run_all.nim

{"age": 12, "bio": "Я Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"}
true
true
alpha 100
omega 200
Traceback (most recent call last)
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tparsesql.nim(196) tparsesql
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(1514) parseSQL
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(1506) parseSQL
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(1181) parse
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(1170) parseStmt
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(1082) parseSelect
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(950) parseWhere
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(786) parseExpr
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(778) lowestExprAux
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(771) lowestExprAux
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(737) primary
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(697) identOrLiteral
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(576) newNode
/Users/timothee/git_clone/nim/Nim/lib/system/gc.nim(496) newObjRC1
/Users/timothee/git_clone/nim/Nim/lib/system/alloc.nim(781) rawAlloc
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Error: execution of an external program failed: '/Users/timothee/git_clone/nim/Nim/bug/run_all '

note

I ran into this while working on #9581
this seems like a different bug from #9752

@timotheecour
Copy link
Member Author

marking high priority as it affects PR's like #10355 and subsequent PR's that aim at making CI run faster by joining more tests in megatest

@timotheecour
Copy link
Member Author

EDIT: removed high priority label because I found a workaround, see #10355

@LemonBoy
Copy link
Contributor

No wonder it's crashing, the test7 in tmarshal.nim serializes a bunch of pointers that become invalid before the object is reconstructed.

Notabug @Araq ?

@Araq
Copy link
Member

Araq commented Jan 23, 2019

Seems like a valid bug to me, marshal is supposed to work with ref.

@LemonBoy
Copy link
Contributor

I don't get how it's supposed to work safely. Here you marshal the ref pointer as, well, an integer... and then the object it points to gets collected by the GC before the un-marshaling is done.

@Araq
Copy link
Member

Araq commented Jan 23, 2019

The marshalling is supposed to produce (ID, value) pairs for the first occurence of the data and then only ID for backrefs and this whole mechanism will produce a copy of the linked list, the GC can collect the old list. Marshal is supposed to be memory safe and the test doesn't use cast.

@LemonBoy
Copy link
Contributor

Alright, I had a deeper look at this. If you turn on -d:nimBurnFree you can easily see that we got a double free here and the root of the problem seems to be the definition of testit: if you replace it with the following definition that uses a temporary variable the test case runs just fine with -d:nimBurnFree -d:useGcAssert -d:useSysAssert

template testit(x) =
  let x1 = to[type(x)]($$(x))
  discard $$x1

It seems that the node we end up freeing twice is somehow stuck in the ZCT but I'm not 100% this is the case since I'm not a GC expert.

@Araq Araq added Severe and removed Test Suite labels Jun 11, 2019
@Araq
Copy link
Member

Araq commented Jul 4, 2019

Note: it works with --gc:markAndSweep.

@krux02
Copy link
Contributor

krux02 commented Jul 29, 2019

This is not a compiler crash.

Araq added a commit that referenced this issue Sep 16, 2020
@Araq Araq closed this as completed in 8b66412 Sep 16, 2020
narimiran pushed a commit that referenced this issue Sep 21, 2020
(cherry picked from commit 8b66412)
ringabout added a commit to ringabout/Nim that referenced this issue Nov 13, 2020
Araq added a commit that referenced this issue Nov 15, 2020
* make workaround for #15934 and #15620
* add testcase for #9754
narimiran pushed a commit that referenced this issue Nov 16, 2020
(cherry picked from commit a968e7d)
PMunch pushed a commit to PMunch/Nim that referenced this issue Jan 6, 2021
mildred pushed a commit to mildred/Nim that referenced this issue Jan 11, 2021
mildred pushed a commit to mildred/Nim that referenced this issue Jan 11, 2021
irdassis pushed a commit to irdassis/Nim that referenced this issue Mar 16, 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