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.65k stars 1.47k forks source link

memory corrupted; possibly by try/catch using `import segfaults` #9752

Open timotheecour opened 6 years ago

timotheecour commented 6 years ago

/cc @Araq see reproducible test case here: https://github.com/nim-lang/Nim/pull/9751

these all work:

nim c -r bug/trunall_13_tests_stdlib_tstdlib_various
nim c -r bug/trunall_62_tests_stdlib_twalker
nim c -r bug/trunall_63_tests_stdlib_tmarshal
nim c -r bug/trunall_64_tests_stdlib_tparsesql

but this fails if importing all of the above:

nim c -r bug/run_bug.nim

with bug/run_bug.nim:

import trunall_13_tests_stdlib_tstdlib_various
import trunall_62_tests_stdlib_twalker
import trunall_63_tests_stdlib_tmarshal
import trunall_64_tests_stdlib_tparsesql
true
alpha 100
omega 200
tests/stdlib/tparsesql.nim
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(175) trunall_13_tests_stdlib_tstdlib_various
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(167) main
[[reraised from:
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(175) trunall_13_tests_stdlib_tstdlib_various
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(167) main
]]
[[reraised from:
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(175) trunall_13_tests_stdlib_tstdlib_various
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(167) main
]]
[[reraised from:
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(175) trunall_13_tests_stdlib_tstdlib_various
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(167) main
]]
[[reraised from:
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(175) trunall_13_tests_stdlib_tstdlib_various
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(167) main
]]
[[reraised from:
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(175) trunall_13_tests_stdlib_tstdlib_various
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tstdlib_various.nim(167) main
]]
[[reraised from:
/Users/timothee/git_clone/nim/Nim/tests/stdlib/tparsesql.nim(162) trunall_64_tests_stdlib_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(1160) parseStmt
/Users/timothee/git_clone/nim/Nim/lib/pure/parsesql.nim(977) parseIndexDef
/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
]]
Error: unhandled exception:  [NilAccessError]
Error: execution of an external program failed: '/Users/timothee/git_clone/nim/Nim/bug/run_bug '

nim --version Nim Compiler Version 0.19.9 [MacOSX: amd64] Compiled at 2018-11-16 Copyright (c) 2006-2018 by Andreas Rumpf

git hash: 6279b0587a9551d8c205e682d28a731f2986c456 active boot switches: -d:release

note

I ran into this while working on https://github.com/nim-lang/Nim/issues/9581

note

wondering if it's because of import segfaults in tstdlib_various

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; otherwise it will be closed. Thank you for your contributions.

ringabout commented 4 years ago

@timotheecour

It seems not to happen anymore. Though I couldn't find twalker.nim. I will make a PR to show this/ It seems to be fixed by https://github.com/nim-lang/Nim/pull/15342

ringabout commented 4 years ago

I think it is only related to two files

include "../tests/stdlib/tstdlib_various.nim"
include "../tests/stdlib/tmarshal.nim"

Gave:

abc
def
definition
prefix
xyz
def
definition
Hi Andreas! How do you feel, Rumpf?

@[0, 2, 1]
@[1, 0, 2]
@[1, 2, 0]
@[2, 0, 1]
@[2, 1, 0]
@[2, 0, 1]
@[1, 2, 0]
@[1, 0, 2]
@[0, 2, 1]
@[0, 1, 2]
055this should be the casehugh@["(", "+", " 1", " 2", ")"]
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
[5]
[4, 5]
[3, 4, 5]
[2, 3, 4, 5]
[2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
true
<h1><a href="http://force7.de/nim">Nim</a></h1>
{"age": 12, "bio": "Я Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"}
true
true
alpha 100
omega 200
Some(null)
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(175) run_bug
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(167) main
[[reraised from:
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(175) run_bug
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(167) main
]]
[[reraised from:
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(175) run_bug
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(167) main
]]
[[reraised from:
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(175) run_bug
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(167) main
]]
[[reraised from:
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(175) run_bug
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(167) main
]]
[[reraised from:
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(175) run_bug
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tstdlib_various.nim(167) main
]]
[[reraised from:
D:\QQPCmgr\Desktop\Nim\tests\stdlib\tmarshal.nim(142) run_bug
C:\Users\blue\.choosenim\toolchains\nim-#devel\lib\pure\marshal.nim(343) to
C:\Users\blue\.choosenim\toolchains\nim-#devel\lib\pure\marshal.nim(276) loadAny
C:\Users\blue\.choosenim\toolchains\nim-#devel\lib\pure\marshal.nim(196) loadAny
C:\Users\blue\.choosenim\toolchains\nim-#devel\lib\pure\marshal.nim(211) loadAny
C:\Users\blue\.choosenim\toolchains\nim-#devel\lib\core\typeinfo.nim(330) setPointer
C:\Users\blue\.choosenim\toolchains\nim-#devel\lib\system\assign.nim(139) genericAssign
C:\Users\blue\.choosenim\toolchains\nim-#devel\lib\system\assign.nim(134) genericAssignAux
]]

If I use devel, it works ad expected:

abc
def
definition
prefix
xyz
def
definition
Hi Andreas! How do you feel, Rumpf?

@[0, 2, 1]
@[1, 0, 2]
@[1, 2, 0]
@[2, 0, 1]
@[2, 1, 0]
@[2, 0, 1]
@[1, 2, 0]
@[1, 0, 2]
@[0, 2, 1]
@[0, 1, 2]
055this should be the casehugh@["(", "+", " 1", " 2", ")"]
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
caught a crash!
[5]
[4, 5]
[3, 4, 5]
[2, 3, 4, 5]
[2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
true
<h1><a href="http://force7.de/nim">Nim</a></h1>
{"age": 12, "bio": "Я Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"}
true
true
alpha 100
omega 200
Some(null)
None[JsonNode]
(numeric: "")
timotheecour commented 4 years ago

@xflywind cool; if possible could you try to minimize a bit while maintaining the crash before that PR and succeeding after that PR?

ringabout commented 4 years ago

Some tests are changing and doesn't work in Nim v 1.2.6 I will try to minimize that.

Araq commented 4 years ago

Instead remove import segfaults from the tests, I don't want to support it further, it's stupid. At some point one has to let processes crash and invest some effort into testing one's code.

timotheecour commented 4 years ago

import segfaults is useful I don't see why keeping at least 1 test is a maintenance burden. I have real use cases for import segfaults, including for a repl I'm working on (already usable), where the process can't crash. The repl, unlike nim secret, works at RT (so avoids some limitations of VM), and unlike inim, doesn't recompile the whole program on each command, and is in particular faster.

There are other use cases for processes that can't crash that take user input such as shared library plugins.

johnnovak commented 3 years ago

I don't want to support it further, it's stupid. At some point one has to let processes crash and invest some effort into testing one's code.

Yes, and in an ideal world, you'd have all the time (and ability!) to produce code with zero bugs. It doesn't happen in the real world, though, does it?

It's not just about "processes" that you run on your machine, what about applications shipped to users? Being able to catch and log any error or segfault is an extremely useful (and I'd even say, essential) requirement for such apps; then the user can provide the crash log in the bug report.

This has been a standard feature of all high-level languages for decades. I know Nim is not exactly "high level" in the same sense, but we can still aim to be better than C in this regard.

timotheecour commented 3 years ago

FWIW, I wrote a branch which allowed to raise a Defect upon nil dereferencing (without resorting to signals, but instead by codegen instrumentation), which can help for debugging, in particular can give column info and is safe since doesn't rely on signals.

(exp_SIGSEGV_debug, no PR ready though, but it did work) (controlled by a flag)

Araq commented 3 years ago

Yes, and in an ideal world, you'd have all the time (and ability!) to produce code with zero bugs. It doesn't happen in the real world, though, does it?

Yeah, exactly. I haven't been able to write segfaults without bugs and I don't have all the time to maintain it either. Ironic, isn't it.

johnnovak commented 3 years ago

Yes, and in an ideal world, you'd have all the time (and ability!) to produce code with zero bugs. It doesn't happen in the real world, though, does it?

Yeah, exactly. I haven't been able to write segfaults without bugs and I don't have all the time to maintain it either. Ironic, isn't it.

Good point, but I find this to be an important part of the core language.