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

random crash caused by createThread #6875

Closed
kvinwang opened this issue Dec 6, 2017 · 6 comments
Closed

random crash caused by createThread #6875

kvinwang opened this issue Dec 6, 2017 · 6 comments

Comments

@kvinwang
Copy link
Contributor

kvinwang commented Dec 6, 2017

proc testCreateThread(): Thread[int] =
  createThread(result, proc(a: int) = discard, 0)

let t = testCreateThread()
t.joinThread()

Crashes without any hint.

In threads.nim

    if pthread_create(t.sys, a, threadProcWrapper[TArg], addr(t)) != 0:

it use the addr(t) directly. so it has problem if t is on stack.

@genotrance
Copy link
Contributor

Works fine on Windows, crashes on Linux.

timotheecour added a commit to timotheecour/Nim that referenced this issue Jul 7, 2021
@timotheecour
Copy link
Member

so the problem is indeed that pthread_create(t.sys, a, threadProcWrapper[TArg], addr(t)) != 0: consumes a stack address that gets invalidated, and nim doesn't flag this as illegal. Thread already has =copy disabled with {.error.} but that's unrelated and doesn't prevent this.

possible solution: use escape analysis, refs #14976 to detect this and at least issue a warning.

@Varriount
Copy link
Contributor

so the problem is indeed that pthread_create(t.sys, a, threadProcWrapper[TArg], addr(t)) != 0: consumes a stack address that gets invalidated, and nim doesn't flag this as illegal. Thread already has =copy disabled with {.error.} but that's unrelated and doesn't prevent this.

possible solution: use escape analysis, refs #14976 to detect this and at least issue a warning.

Can you explain a bit more?

@Araq
Copy link
Member

Araq commented Aug 27, 2023

createThread is a sharp tool and you cannot use it in this way.

@Araq Araq closed this as completed Aug 27, 2023
@kvinwang
Copy link
Contributor Author

createThread is a sharp tool and you cannot use it in this way.

I don't think this is a responsible answer.

@Araq
Copy link
Member

Araq commented Aug 28, 2023

Too bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants