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

SIGSEGV: setLen on a seq doesn't construct objects at CT #9872

Closed
timotheecour opened this issue Dec 6, 2018 · 3 comments · Fixed by #9874
Closed

SIGSEGV: setLen on a seq doesn't construct objects at CT #9872

timotheecour opened this issue Dec 6, 2018 · 3 comments · Fixed by #9874
Labels
Compiler Crash VM see also `const` label

Comments

@timotheecour
Copy link
Member

/cc @Araq

#[
KEY ICE crash

/Users/timothee/git_clone/nim/timn/config.nims [config.nims used]
/Users/timothee/git_clone/nim/Nim/compiler/nim.nim(109) nim
/Users/timothee/git_clone/nim/Nim/compiler/nim.nim(73) handleCmdLine
/Users/timothee/git_clone/nim/Nim/compiler/cmdlinehelper.nim(91) loadConfigsAndRunMainCommand
/Users/timothee/git_clone/nim/Nim/compiler/main.nim(168) mainCommand
/Users/timothee/git_clone/nim/Nim/compiler/main.nim(78) commandCompileToC
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(134) compileProject
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(79) compileModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(194) processModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(86) processTopLevelStmt
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(603) myProcess
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(571) semStmtAndGenerateGenerics
/Users/timothee/git_clone/nim/Nim/compiler/semstmts.nim(1987) semStmt
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(915) semExprNoType
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2548) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semstmts.nim(1927) semStmtList
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2436) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(897) semDirectOp
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(790) afterCallActions
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(466) semMacroExpr
/Users/timothee/git_clone/nim/Nim/compiler/vm.nim(1999) evalMacroCall
/Users/timothee/git_clone/nim/Nim/compiler/vm.nim(580) rawExecute
/Users/timothee/git_clone/nim/Nim/lib/system.nim(2969) sysFatal
Error: unhandled exception: index out of bounds [IndexError]
]#

type Foo = object
  index: int

macro fun(): untyped =
  let n = 1
  var foos: seq[Foo]
  foos.setLen n
  for i in 0 ..< n:
    discard foos[i] # works
    echo foos[i].index # Error: unhandled exception: index out of bounds [IndexError]

fun()
@timotheecour
Copy link
Member Author

timotheecour commented Dec 6, 2018

  • actually it's not specific to macros, as -d:case2 shows below, it affects CT evaluation:
  • removing High priority Label because I found a workaround: using
var foos = newSeq[Foo](n)

insetad of

var foos: seq[Foo]
    foos.setLen n
#[
KEY ICE crash
[SIGSEGV when accessing seq element inside macro (in evalMacroCall) · Issue #9872 · nim-lang/Nim](https://github.com/nim-lang/Nim/issues/9872)
]#

type Foo = object
  index: int

when defined(case1):
#[
/Users/timothee/git_clone/nim/timn/config.nims [config.nims used]
/Users/timothee/git_clone/nim/Nim/compiler/nim.nim(109) nim
/Users/timothee/git_clone/nim/Nim/compiler/nim.nim(73) handleCmdLine
/Users/timothee/git_clone/nim/Nim/compiler/cmdlinehelper.nim(91) loadConfigsAndRunMainCommand
/Users/timothee/git_clone/nim/Nim/compiler/main.nim(168) mainCommand
/Users/timothee/git_clone/nim/Nim/compiler/main.nim(78) commandCompileToC
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(134) compileProject
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(79) compileModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(194) processModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(86) processTopLevelStmt
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(603) myProcess
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(571) semStmtAndGenerateGenerics
/Users/timothee/git_clone/nim/Nim/compiler/semstmts.nim(1987) semStmt
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(915) semExprNoType
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2548) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semstmts.nim(1927) semStmtList
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2436) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(897) semDirectOp
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(790) afterCallActions
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(466) semMacroExpr
/Users/timothee/git_clone/nim/Nim/compiler/vm.nim(1999) evalMacroCall
/Users/timothee/git_clone/nim/Nim/compiler/vm.nim(580) rawExecute
/Users/timothee/git_clone/nim/Nim/lib/system.nim(2969) sysFatal
Error: unhandled exception: index out of bounds [IndexError]
]#
  macro fun(): untyped =
    let n = 1
    var foos: seq[Foo]
    foos.setLen n
    for i in 0 ..< n:
      discard foos[i] # works
      echo foos[i].index # Error: unhandled exception: index out of bounds [IndexError]
  fun()

when defined(case2):
  #[
/Users/timothee/git_clone/nim/timn/config.nims [config.nims used]
/Users/timothee/git_clone/nim/Nim/compiler/nim.nim(109) nim
/Users/timothee/git_clone/nim/Nim/compiler/nim.nim(73) handleCmdLine
/Users/timothee/git_clone/nim/Nim/compiler/cmdlinehelper.nim(91) loadConfigsAndRunMainCommand
/Users/timothee/git_clone/nim/Nim/compiler/main.nim(168) mainCommand
/Users/timothee/git_clone/nim/Nim/compiler/main.nim(78) commandCompileToC
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(134) compileProject
/Users/timothee/git_clone/nim/Nim/compiler/modules.nim(79) compileModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(194) processModule
/Users/timothee/git_clone/nim/Nim/compiler/passes.nim(86) processTopLevelStmt
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(603) myProcess
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(571) semStmtAndGenerateGenerics
/Users/timothee/git_clone/nim/Nim/compiler/semstmts.nim(1987) semStmt
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(915) semExprNoType
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2548) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semstmts.nim(1927) semStmtList
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2472) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2075) semWhen
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2548) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semstmts.nim(1927) semStmtList
/Users/timothee/git_clone/nim/Nim/compiler/semexprs.nim(2552) semExpr
/Users/timothee/git_clone/nim/Nim/compiler/semstmts.nim(558) semConst
/Users/timothee/git_clone/nim/Nim/compiler/sem.nim(353) semConstExpr
/Users/timothee/git_clone/nim/Nim/compiler/vm.nim(1908) evalConstExpr
/Users/timothee/git_clone/nim/Nim/compiler/vm.nim(1904) evalConstExprAux
/Users/timothee/git_clone/nim/Nim/compiler/vm.nim(580) rawExecute
/Users/timothee/git_clone/nim/Nim/lib/system.nim(2969) sysFatal
Error: unhandled exception: index out of bounds [IndexError]
  ]#
  proc fun():auto =
    let n = 1
    var foos: seq[Foo]
    foos.setLen n
    # var foos = newSeq[Foo](n)
    for i in 0 ..< n:
      discard foos[i] # works
      echo foos[i].index # Error: unhandled exception: index out of bounds [IndexError]
    0

  const foos = fun()

when defined(case1b):
  # this works
  macro fun(): untyped =
    let n = 1
    var foos = newSeq[Foo](n)
    for i in 0 ..< n:
      discard foos[i] # works
      echo foos[i].index # Error: unhandled exception: index out of bounds [IndexError]
  fun()

when defined(case2b):
  # this works
  proc fun():auto =
    let n = 1
    var foos = newSeq[Foo](n)
    for i in 0 ..< n:
      discard foos[i] # works
      echo foos[i].index # Error: unhandled exception: index out of bounds [IndexError]
    0
  const foos = fun()

@timotheecour timotheecour changed the title SIGSEGV when accessing seq element inside macro (in evalMacroCall) SIGSEGV when accessing seq[ObjectType] element at CT Dec 6, 2018
@timotheecour timotheecour changed the title SIGSEGV when accessing seq[ObjectType] element at CT SIGSEGV: setLen on a seq doesn't construct objects at CT Dec 6, 2018
@timotheecour
Copy link
Member Author

looks like implementation is wrong:

proc setLenSeq(c: PCtx; node: PNode; newLen: int; info: TLineInfo) =
  # FIXME: this doesn't attempt to solve incomplete
  # support of tyPtr, tyRef in VM.
  let typ = node.typ.skipTypes(abstractInst+{tyRange}-{tyTypeDesc})
  let typeEntry = typ.sons[0].skipTypes(abstractInst+{tyRange}-{tyTypeDesc})
  let typeKind = case typeEntry.kind
                 of tyUInt..tyUInt64: nkUIntLit
                 of tyRange, tyEnum, tyBool, tyChar, tyInt..tyInt64: nkIntLit
                 of tyFloat..tyFloat128: nkFloatLit
                 of tyString: nkStrLit
                 of tyObject: nkObjConstr
                 of tySequence: nkNilLit
                 of tyProc, tyTuple: nkTupleConstr
                 else: nkEmpty

  let oldLen = node.len
  setLen(node.sons, newLen)
  if oldLen < newLen:
    # TODO: This is still not correct for tyPtr, tyRef default value
    for i in oldLen ..< newLen:
      node.sons[i] = newNodeI(typeKind, info)

it's missing object construction I guess?

node.sons[i] = newNodeI(typeKind, info)
=> need something here

@timotheecour
Copy link
Member Author

note:

this is also wrong for ref's, the ref should be nil

when defined(case2c):
  proc fun():auto =
    let n = 3
    var foos: seq[ref Foo]
    foos.setLen n
    for i in 0 ..< n:
      let temp = foos[i]
      echo temp == nil # BUG: prints as false, should be true
    0
  const foos = fun()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compiler Crash VM see also `const` label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant