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

[Regression] Nim crash in sumGeneric #23249

Closed
yglukhov opened this issue Jan 22, 2024 · 3 comments · Fixed by #23250
Closed

[Regression] Nim crash in sumGeneric #23249

yglukhov opened this issue Jan 22, 2024 · 3 comments · Fixed by #23250

Comments

@yglukhov
Copy link
Member

yglukhov commented Jan 22, 2024

Description

type Control* = object
proc onAction*(c: Control, handler: proc(e: int) {.gcsafe.}) = discard
proc onAction*(c: Control, handler: proc() {.gcsafe.}) = discard

template setControlHandlerBlock(c: Control, p: untyped, a: untyped) =
    when compiles(c.p(nil)):
        c.p() do() {.gcsafe.}: a
    else:
        c.p = proc() {.gcsafe.} =
            a

proc mkLayout() =
  var b: Control
  setControlHandlerBlock(b, onAction):
    echo "hi"
nim c test.nim

Nim Version

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

git hash: d3f5056
active boot switches: -d:release

Current Output

SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Expected Output

Compiles without errors

Additional Information

First bad commit: 6ed33b6

@ringabout
Copy link
Member

!nim c

type Control* = object
proc onAction*(c: Control, handler: proc(e: int) {.gcsafe.}) = discard
proc onAction*(c: Control, handler: proc() {.gcsafe.}) = discard

template setControlHandlerBlock(c: Control, p: untyped, a: untyped) =
    when compiles(c.p(nil)):
        c.p() do() {.gcsafe.}: a
    else:
        c.p = proc() {.gcsafe.} =
            a

proc mkLayout() =
  var b: Control
  setControlHandlerBlock(b, onAction):
    echo "hi"

@ringabout ringabout self-assigned this Jan 22, 2024
Copy link
Contributor

🐧 Linux bisect by @ringabout (member)
devel 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

IR

Compiled filesize 0 bytes (0 bytes)

Stats

  • Started 2024-01-22T14:24:16
  • Finished 2024-01-22T14:24:17
  • Duration

AST

nnkStmtList.newTree(
  nnkTypeSection.newTree(
    nnkTypeDef.newTree(
      nnkPostfix.newTree(
        newIdentNode("*"),
        newIdentNode("Control")
      ),
      newEmptyNode(),
      nnkObjectTy.newTree(
        newEmptyNode(),
        newEmptyNode(),
        newEmptyNode()
      )
    )
  ),
  nnkProcDef.newTree(
    nnkPostfix.newTree(
      newIdentNode("*"),
      newIdentNode("onAction")
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkFormalParams.newTree(
      newEmptyNode(),
      nnkIdentDefs.newTree(
        newIdentNode("c"),
        newIdentNode("Control"),
        newEmptyNode()
      ),
      nnkIdentDefs.newTree(
        newIdentNode("handler"),
        nnkProcTy.newTree(
          nnkFormalParams.newTree(
            newEmptyNode(),
            nnkIdentDefs.newTree(
              newIdentNode("e"),
              newIdentNode("int"),
              newEmptyNode()
            )
          ),
          nnkPragma.newTree(
            newIdentNode("gcsafe")
          )
        ),
        newEmptyNode()
      )
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkDiscardStmt.newTree(
        newEmptyNode()
      )
    )
  ),
  nnkProcDef.newTree(
    nnkPostfix.newTree(
      newIdentNode("*"),
      newIdentNode("onAction")
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkFormalParams.newTree(
      newEmptyNode(),
      nnkIdentDefs.newTree(
        newIdentNode("c"),
        newIdentNode("Control"),
        newEmptyNode()
      ),
      nnkIdentDefs.newTree(
        newIdentNode("handler"),
        nnkProcTy.newTree(
          nnkFormalParams.newTree(
            newEmptyNode()
          ),
          nnkPragma.newTree(
            newIdentNode("gcsafe")
          )
        ),
        newEmptyNode()
      )
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkDiscardStmt.newTree(
        newEmptyNode()
      )
    )
  ),
  nnkTemplateDef.newTree(
    newIdentNode("setControlHandlerBlock"),
    newEmptyNode(),
    newEmptyNode(),
    nnkFormalParams.newTree(
      newEmptyNode(),
      nnkIdentDefs.newTree(
        newIdentNode("c"),
        newIdentNode("Control"),
        newEmptyNode()
      ),
      nnkIdentDefs.newTree(
        newIdentNode("p"),
        newIdentNode("untyped"),
        newEmptyNode()
      ),
      nnkIdentDefs.newTree(
        newIdentNode("a"),
        newIdentNode("untyped"),
        newEmptyNode()
      )
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkWhenStmt.newTree(
        nnkElifBranch.newTree(
          nnkCall.newTree(
            newIdentNode("compiles"),
            nnkCall.newTree(
              nnkDotExpr.newTree(
                newIdentNode("c"),
                newIdentNode("p")
              ),
              newNilLit()
            )
          ),
          nnkStmtList.newTree(
            nnkCall.newTree(
              nnkDotExpr.newTree(
                newIdentNode("c"),
                newIdentNode("p")
              ),
              nnkDo.newTree(
                newEmptyNode(),
                newEmptyNode(),
                newEmptyNode(),
                nnkFormalParams.newTree(
                  newEmptyNode()
                ),
                nnkPragma.newTree(
                  newIdentNode("gcsafe")
                ),
                newEmptyNode(),
                nnkStmtList.newTree(
                  newIdentNode("a")
                )
              )
            )
          )
        ),
        nnkElse.newTree(
          nnkStmtList.newTree(
            nnkAsgn.newTree(
              nnkDotExpr.newTree(
                newIdentNode("c"),
                newIdentNode("p")
              ),
              nnkLambda.newTree(
                newEmptyNode(),
                newEmptyNode(),
                newEmptyNode(),
                nnkFormalParams.newTree(
                  newEmptyNode()
                ),
                nnkPragma.newTree(
                  newIdentNode("gcsafe")
                ),
                newEmptyNode(),
                nnkStmtList.newTree(
                  newIdentNode("a")
                )
              )
            )
          )
        )
      )
    )
  ),
  nnkProcDef.newTree(
    newIdentNode("mkLayout"),
    newEmptyNode(),
    newEmptyNode(),
    nnkFormalParams.newTree(
      newEmptyNode()
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkVarSection.newTree(
        nnkIdentDefs.newTree(
          newIdentNode("b"),
          newIdentNode("Control"),
          newEmptyNode()
        )
      ),
      nnkCall.newTree(
        newIdentNode("setControlHandlerBlock"),
        newIdentNode("b"),
        newIdentNode("onAction"),
        nnkStmtList.newTree(
          nnkCommand.newTree(
            newIdentNode("echo"),
            newLit("hi")
          )
        )
      )
    )
  )
)
stable 👍 OK

Output


IR

Compiled filesize 90.65 Kb (92,824 bytes)
#define NIM_INTBITS 64
#include "nimbase.h"
N_LIB_PRIVATE N_NIMCALL(void, nimTestErrorFlag)(void);
N_LIB_PRIVATE N_NIMCALL(void, atmdotdotatsdotdotatsdotdotatsdotchoosenimatstoolchainsatsnimminus2dot0dot2atslibatssystemdotnim_Init000)(void);
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void);
N_LIB_PRIVATE void PreMainInner(void) {
}
N_LIB_PRIVATE int cmdCount;
N_LIB_PRIVATE char** cmdLine;
N_LIB_PRIVATE char** gEnv;
N_LIB_PRIVATE void PreMain(void) {
#if 0
 void (*volatile inner)(void);
 inner = PreMainInner;
 atmdotdotatsdotdotatsdotdotatsdotchoosenimatstoolchainsatsnimminus2dot0dot2atslibatssystemdotnim_Init000();
 (*inner)();
#else
 atmdotdotatsdotdotatsdotdotatsdotchoosenimatstoolchainsatsnimminus2dot0dot2atslibatssystemdotnim_Init000();
 PreMainInner();
#endif
}
N_LIB_PRIVATE N_CDECL(void, NimMainInner)(void) {
 NimMainModule();
}
N_CDECL(void, NimMain)(void) {
#if 0
 void (*volatile inner)(void);
 PreMain();
 inner = NimMainInner;
 (*inner)();
#else
 PreMain();
 NimMainInner();
#endif
}
int main(int argc, char** args, char** env) {
 cmdLine = args;
 cmdCount = argc;
 gEnv = env;
 NimMain();
 return nim_program_result;
}
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
{
 nimTestErrorFlag();
}
}

Stats

  • Started 2024-01-22T14:24:17
  • Finished 2024-01-22T14:24:18
  • Duration
2.0.0 👍 OK

Output


IR

Compiled filesize 90.64 Kb (92,816 bytes)
#define NIM_INTBITS 64
#include "nimbase.h"
N_LIB_PRIVATE N_NIMCALL(void, nimTestErrorFlag)(void);
N_LIB_PRIVATE N_NIMCALL(void, atmdotdotatsdotdotatsdotdotatsdotchoosenimatstoolchainsatsnimminus2dot0dot0atslibatssystemdotnim_Init000)(void);
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void);
N_LIB_PRIVATE void PreMainInner(void) {
}
N_LIB_PRIVATE int cmdCount;
N_LIB_PRIVATE char** cmdLine;
N_LIB_PRIVATE char** gEnv;
N_LIB_PRIVATE void PreMain(void) {
#if 0
 void (*volatile inner)(void);
 inner = PreMainInner;
 atmdotdotatsdotdotatsdotdotatsdotchoosenimatstoolchainsatsnimminus2dot0dot0atslibatssystemdotnim_Init000();
 (*inner)();
#else
 atmdotdotatsdotdotatsdotdotatsdotchoosenimatstoolchainsatsnimminus2dot0dot0atslibatssystemdotnim_Init000();
 PreMainInner();
#endif
}
N_LIB_PRIVATE N_CDECL(void, NimMainInner)(void) {
 NimMainModule();
}
N_CDECL(void, NimMain)(void) {
#if 0
 void (*volatile inner)(void);
 PreMain();
 inner = NimMainInner;
 (*inner)();
#else
 PreMain();
 NimMainInner();
#endif
}
int main(int argc, char** args, char** env) {
 cmdLine = args;
 cmdCount = argc;
 gEnv = env;
 NimMain();
 return nim_program_result;
}
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
{
 nimTestErrorFlag();
}
}

Stats

  • Started 2024-01-22T14:24:20
  • Finished 2024-01-22T14:24:21
  • Duration
1.6.14 👍 OK

Output


IR

Compiled filesize 95.29 Kb (97,576 bytes)
#define NIM_INTBITS 64
#include "nimbase.h"
#define nimfr_(x, y)
#define nimln_(x, y)
static N_INLINE(void, initStackBottomWith)(void* locals);
N_LIB_PRIVATE N_NOINLINE(void, nimGC_setStackBottom)(void* theStackBottom);
N_LIB_PRIVATE N_NIMCALL(void, atmdotdotatsdotdotatsdotdotatsdotchoosenimatstoolchainsatsnimminus1dot6dot14atslibatssystemdotnim_DatInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, atmdotdotatsdotdotatsdotdotatsdotchoosenimatstoolchainsatsnimminus1dot6dot14atslibatssystemdotnim_Init000)(void);
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void);
static N_INLINE(void, initStackBottomWith)(void* locals) {
 nimGC_setStackBottom(locals);
}
N_LIB_PRIVATE void PreMainInner(void) {
}
 N_LIB_PRIVATE int cmdCount;
 N_LIB_PRIVATE char** cmdLine;
 N_LIB_PRIVATE char** gEnv;
N_LIB_PRIVATE void PreMain(void) {
 void (*volatile inner)(void);
 inner = PreMainInner;
 atmdotdotatsdotdotatsdotdotatsdotchoosenimatstoolchainsatsnimminus1dot6dot14atslibatssystemdotnim_DatInit000();
 initStackBottomWith((void *)&inner);
 atmdotdotatsdotdotatsdotdotatsdotchoosenimatstoolchainsatsnimminus1dot6dot14atslibatssystemdotnim_Init000();
 (*inner)();
}
N_LIB_PRIVATE N_CDECL(void, NimMainInner)(void) {
 NimMainModule();
}
N_CDECL(void, NimMain)(void) {
 void (*volatile inner)(void);
 PreMain();
 inner = NimMainInner;
 initStackBottomWith((void *)&inner);
 (*inner)();
}
int main(int argc, char** args, char** env) {
 cmdLine = args;
 cmdCount = argc;
 gEnv = env;
 NimMain();
 return nim_program_result;
}
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
{
}
}

Stats

  • Started 2024-01-22T14:24:24
  • Finished 2024-01-22T14:24:24
  • Duration
1.4.8 👍 OK

Output


IR

Compiled filesize 91.16 Kb (93,344 bytes)
#define NIM_INTBITS 64
#include "nimbase.h"
#define nimfr_(x, y)
#define nimln_(x, y)
static N_INLINE(void, initStackBottomWith)(void* locals);
N_LIB_PRIVATE N_NOINLINE(void, nimGC_setStackBottom)(void* theStackBottom);
N_LIB_PRIVATE N_NIMCALL(void, systemDatInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, systemInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void);
static N_INLINE(void, initStackBottomWith)(void* locals) {
 nimGC_setStackBottom(locals);
}
N_LIB_PRIVATE void PreMainInner(void) {
}
N_LIB_PRIVATE int cmdCount;
N_LIB_PRIVATE char** cmdLine;
N_LIB_PRIVATE char** gEnv;
N_LIB_PRIVATE void PreMain(void) {
 void (*volatile inner)(void);
 inner = PreMainInner;
 systemDatInit000();
 initStackBottomWith((void *)&inner);
 systemInit000();
 (*inner)();
}
N_LIB_PRIVATE N_CDECL(void, NimMainInner)(void) {
 NimMainModule();
}
N_CDECL(void, NimMain)(void) {
 void (*volatile inner)(void);
 PreMain();
 inner = NimMainInner;
 initStackBottomWith((void *)&inner);
 (*inner)();
}
int main(int argc, char** args, char** env) {
 cmdLine = args;
 cmdCount = argc;
 gEnv = env;
 NimMain();
 return nim_program_result;
}
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
{
}
}

Stats

  • Started 2024-01-22T14:24:28
  • Finished 2024-01-22T14:24:28
  • Duration
1.2.18 👍 OK

Output


IR

Compiled filesize 86.88 Kb (88,968 bytes)
#define NIM_INTBITS 64
#include "nimbase.h"
#define nimfr_(x, y)
#define nimln_(x, y)
static N_INLINE(void, initStackBottomWith)(void* locals);
N_LIB_PRIVATE N_NOINLINE(void, nimGC_setStackBottom)(void* theStackBottom);
N_LIB_PRIVATE N_NIMCALL(void, systemDatInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, systemInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void);
static N_INLINE(void, initStackBottomWith)(void* locals) {
 nimGC_setStackBottom(locals);
}
N_LIB_PRIVATE void PreMainInner(void) {
}
N_LIB_PRIVATE int cmdCount;
N_LIB_PRIVATE char** cmdLine;
N_LIB_PRIVATE char** gEnv;
N_LIB_PRIVATE void PreMain(void) {
 void (*volatile inner)(void);
 inner = PreMainInner;
 systemDatInit000();
 initStackBottomWith((void *)&inner);
 systemInit000();
 (*inner)();
}
N_LIB_PRIVATE N_CDECL(void, NimMainInner)(void) {
 NimMainModule();
}
N_CDECL(void, NimMain)(void) {
 void (*volatile inner)(void);
 PreMain();
 inner = NimMainInner;
 initStackBottomWith((void *)&inner);
 (*inner)();
}
int main(int argc, char** args, char** env) {
 cmdLine = args;
 cmdCount = argc;
 gEnv = env;
 NimMain();
 return nim_program_result;
}
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
{
}
}

Stats

  • Started 2024-01-22T14:24:30
  • Finished 2024-01-22T14:24:31
  • Duration
1.0.10 👍 OK

Output


IR

Compiled filesize 81.98 Kb (83,952 bytes)
#define NIM_INTBITS 64
#include "nimbase.h"
#define nimfr_(x, y)
#define nimln_(x, y)
static N_INLINE(void, initStackBottomWith)(void* locals);
N_NOINLINE(void, nimGC_setStackBottom)(void* theStackBottom);
N_LIB_PRIVATE N_NIMCALL(void, systemDatInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, systemInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void);
static N_INLINE(void, initStackBottomWith)(void* locals) {	nimGC_setStackBottom(locals);
}
void PreMainInner(void) {
}
int cmdCount;
char** cmdLine;
char** gEnv;
void PreMain(void) {
 void (*volatile inner)(void);
 inner = PreMainInner;
 systemDatInit000();
 initStackBottomWith((void *)&inner);
 systemInit000();
 (*inner)();
}
N_CDECL(void, NimMainInner)(void) {
 NimMainModule();
}
N_CDECL(void, NimMain)(void) {
 void (*volatile inner)(void);
 PreMain();
 inner = NimMainInner;
 initStackBottomWith((void *)&inner);
 (*inner)();
}
int main(int argc, char** args, char** env) {
 cmdLine = args;
 cmdCount = argc;
 gEnv = env;
 NimMain();
 return nim_program_result;
}
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
{
}
}

Stats

  • Started 2024-01-22T14:24:33
  • Finished 2024-01-22T14:24:34
  • Duration
??? ➡️ 🐛

Diagnostics

The commit that introduced the bug can not be found, but the bug is in the commits:

(Can not find the commit because Nim can not be re-built commit-by-commit to bisect).

Stats
  • GCC 11.4.0
  • Clang 14.0.0
  • NodeJS 19.0
  • Created 2024-01-22T14:23:45Z
  • Comments 1
  • Commands nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim

🤖 Bug found in 19 minutes bisecting 447 commits at 23 commits per second

@metagn
Copy link
Collaborator

metagn commented Jan 22, 2024

Encountered in #23104, presumable fix here

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.

3 participants