koka-lang / koka

Koka language compiler and interpreter
http://koka-lang.org
Other
3.16k stars 151 forks source link

Program crash when parsing file #379

Open chtenb opened 7 months ago

chtenb commented 7 months ago

I'm currently doing the first challenge of advent of code, and I'm hitting a bug in Koka causing my program to segfault during parsing the input file. The smaller example input file works fine, but when parsing the larger input file the program hangs halfway for a few seconds, then aborts.

I'm on Windows using the lastest version of the Koka compiler. How can I help with providing debug logs etc?

chtenb commented 7 months ago

I found a reasonably small repro:

import std/text/parse

pub fun main()
  println("start")
  val input = join(list(0, 999).map(fn(n) "aaaaaaaaa" ++ show(n)), "\n")
  println("parsing " ++ input)
  val x = parse(slice(input))
    many(fn() alpha-num || ({ optional('\r', { char('\r') }); char('\n') }))
  println("exit")

Output:

> main()
(...)
failure during program run:
   ".koka/v2.4.2/clang-cl-debug/interactive"
TimWhiting commented 7 months ago

You'll quickly run out of stack space with the builtin parse function. It induces exponential backtracking, and parsing a single character at a time will make that even worse. On linux you can request infinite stack space using ulimit -s unlimited. I'm not sure if there is an equivalent windows command.

Koka is gaining some attention, but still has a relatively immature standard library, since it has been very research focused.

chtenb commented 7 months ago

Indeed, the following makes the reproduction exit correctly:

editbin.exe .koka\v2.4.2\clang-cl-debug\repro.exe /STACK:100000000

Thanks for the hint