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.22k stars 1.46k forks source link

Devel: Illegal storage access in alloc.nim #23686

Closed simonkrauter closed 3 weeks ago

simonkrauter commented 3 weeks ago

Description

Test program:

import std / json

proc createJsonData(): string =
  for i in 1..50_000:
    if i == 1:
      result.add("{")
    else:
      result.add(",")
    result.add("\"foo")
    result.add($i)
    result.add("\":0")
  result.add("}")

type
  Request = ref object
    headers: seq[string]

var headers: seq[string]
headers.add("entry1")

proc threadFunc(req: Request) {.thread.} =
  req.headers.add("entry2")

proc callThread() =
  var req = new Request
  req.headers = headers

  var thread: Thread[Request]
  createThread(thread, threadFunc, req)
  joinThreads(thread)

for i in 1..10:
  callThread()

  discard createJsonData().parseJson()

  echo getOccupiedMem()

Nim Version

Nim Compiler Version 2.1.1 [Linux: amd64] Compiled at 2024-06-06 Copyright (c) 2006-2024 by Andreas Rumpf

git hash: 8f5ae28fab113c425d22e5abc230f456fa627744 active boot switches: -d:release

Current Output

lib/pure/json.nim(1047) parseJson
lib/pure/parsejson.nim(113) parseJson
lib/system/alloc.nim(1068) dealloc
lib/system/alloc.nim(952) rawDealloc
lib/system/alloc.nim(556) listRemove
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Expected Output

Should output zeros.

Possible Solution

No response

Additional Information

Related to https://github.com/nim-lang/Nim/commit/69d0b73d667c4be9383f29cda3f70e411995d9af ?

Araq commented 3 weeks ago

Nothing in this program is supported. Neither local variables of type Thread, nor ref for thread, nor accessing stuff without locks. Use Malebolgia or something comparable.