Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Valgrind invalid reads and writes with `addExitProc` from `std/exitprocs` in Nim 2.0 but not `devel`

Open tersec opened this issue 1 year ago • 0 comments

Description

import std/exitprocs
proc exitProc() = discard
addExitProc(exitProc)

Compiled with nim c -d:useMalloc b && valgrind ./b with version-2-0 commit 35ec9c31bd0bd413f1740feea90f3b97ad5d1b65 results in a length Valgrind report while devel commit 35ec9c31bd0bd413f1740feea90f3b97ad5d1b65 appears to run cleanly.

Nim Version

Produces Valgrind-reported errors:

Nim Compiler Version 2.0.3 [Linux: amd64]
Compiled at 2024-02-15
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 3be251468365250a1ad2e86bb2fa634ff7297b61
active boot switches: -d:release

Does not produce Valgrind-reported errors:

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

git hash: 35ec9c31bd0bd413f1740feea90f3b97ad5d1b65
active boot switches: -d:release

Current Output

/tmp$ valgrind ./b
==2994738== Memcheck, a memory error detector
==2994738== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==2994738== Using Valgrind-3.20.0 and LibVEX; rerun with -h for copyright info
==2994738== Command: ./b
==2994738== 
==2994738== Invalid read of size 8
==2994738==    at 0x1125C8: callClosures__stdZexitprocs_u19 (in /tmp/b)
==2994738==    by 0x489C954: __run_exit_handlers (exit.c:111)
==2994738==    by 0x489CA89: exit (exit.c:141)
==2994738==    by 0x48856D0: (below main) (libc_start_call_main.h:74)
==2994738==  Address 0x4a43048 is 8 bytes inside a block of size 32 free'd
==2994738==    at 0x48431EF: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10BD2D: deallocImpl__system_u1765 (in /tmp/b)
==2994738==    by 0x10BD48: deallocSharedImpl__system_u1778 (in /tmp/b)
==2994738==    by 0x10BD63: deallocShared (in /tmp/b)
==2994738==    by 0x10BD89: alignedDealloc (in /tmp/b)
==2994738==    by 0x112AED: eqdestroy___stdZexitprocs_u301 (in /tmp/b)
==2994738==    by 0x112E1F: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738==  Block was alloc'd at
==2994738==    at 0x48459F3: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10CFDF: alloc0Impl__system_u1763 (in /tmp/b)
==2994738==    by 0x10D001: allocShared0Impl__system_u1776 (in /tmp/b)
==2994738==    by 0x10D02E: alignedAlloc0__system_u1947 (in /tmp/b)
==2994738==    by 0x10D30F: newSeqPayload (in /tmp/b)
==2994738==    by 0x10D879: prepareSeqAdd (in /tmp/b)
==2994738==    by 0x111674: add__stdZexitprocs_u226 (in /tmp/b)
==2994738==    by 0x11297B: addExitProc__stdZexitprocs_u220 (in /tmp/b)
==2994738==    by 0x112DE4: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738== 
==2994738== Invalid read of size 8
==2994738==    at 0x1125CC: callClosures__stdZexitprocs_u19 (in /tmp/b)
==2994738==    by 0x489C954: __run_exit_handlers (exit.c:111)
==2994738==    by 0x489CA89: exit (exit.c:141)
==2994738==    by 0x48856D0: (below main) (libc_start_call_main.h:74)
==2994738==  Address 0x4a43050 is 16 bytes inside a block of size 32 free'd
==2994738==    at 0x48431EF: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10BD2D: deallocImpl__system_u1765 (in /tmp/b)
==2994738==    by 0x10BD48: deallocSharedImpl__system_u1778 (in /tmp/b)
==2994738==    by 0x10BD63: deallocShared (in /tmp/b)
==2994738==    by 0x10BD89: alignedDealloc (in /tmp/b)
==2994738==    by 0x112AED: eqdestroy___stdZexitprocs_u301 (in /tmp/b)
==2994738==    by 0x112E1F: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738==  Block was alloc'd at
==2994738==    at 0x48459F3: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10CFDF: alloc0Impl__system_u1763 (in /tmp/b)
==2994738==    by 0x10D001: allocShared0Impl__system_u1776 (in /tmp/b)
==2994738==    by 0x10D02E: alignedAlloc0__system_u1947 (in /tmp/b)
==2994738==    by 0x10D30F: newSeqPayload (in /tmp/b)
==2994738==    by 0x10D879: prepareSeqAdd (in /tmp/b)
==2994738==    by 0x111674: add__stdZexitprocs_u226 (in /tmp/b)
==2994738==    by 0x11297B: addExitProc__stdZexitprocs_u220 (in /tmp/b)
==2994738==    by 0x112DE4: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738== 
==2994738== Invalid read of size 8
==2994738==    at 0x1125D7: callClosures__stdZexitprocs_u19 (in /tmp/b)
==2994738==    by 0x489C954: __run_exit_handlers (exit.c:111)
==2994738==    by 0x489CA89: exit (exit.c:141)
==2994738==    by 0x48856D0: (below main) (libc_start_call_main.h:74)
==2994738==  Address 0x4a43058 is 24 bytes inside a block of size 32 free'd
==2994738==    at 0x48431EF: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10BD2D: deallocImpl__system_u1765 (in /tmp/b)
==2994738==    by 0x10BD48: deallocSharedImpl__system_u1778 (in /tmp/b)
==2994738==    by 0x10BD63: deallocShared (in /tmp/b)
==2994738==    by 0x10BD89: alignedDealloc (in /tmp/b)
==2994738==    by 0x112AED: eqdestroy___stdZexitprocs_u301 (in /tmp/b)
==2994738==    by 0x112E1F: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738==  Block was alloc'd at
==2994738==    at 0x48459F3: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10CFDF: alloc0Impl__system_u1763 (in /tmp/b)
==2994738==    by 0x10D001: allocShared0Impl__system_u1776 (in /tmp/b)
==2994738==    by 0x10D02E: alignedAlloc0__system_u1947 (in /tmp/b)
==2994738==    by 0x10D30F: newSeqPayload (in /tmp/b)
==2994738==    by 0x10D879: prepareSeqAdd (in /tmp/b)
==2994738==    by 0x111674: add__stdZexitprocs_u226 (in /tmp/b)
==2994738==    by 0x11297B: addExitProc__stdZexitprocs_u220 (in /tmp/b)
==2994738==    by 0x112DE4: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738== 
==2994738== Invalid read of size 1
==2994738==    at 0x1122F2: eqdestroy___stdZexitprocs_u94 (in /tmp/b)
==2994738==    by 0x1111ED: reset__stdZexitprocs_u58 (in /tmp/b)
==2994738==    by 0x1112FA: shrink__stdZexitprocs_u38 (in /tmp/b)
==2994738==    by 0x111394: setLen__stdZexitprocs_u31 (in /tmp/b)
==2994738==    by 0x112780: callClosures__stdZexitprocs_u19 (in /tmp/b)
==2994738==    by 0x489C954: __run_exit_handlers (exit.c:111)
==2994738==    by 0x489CA89: exit (exit.c:141)
==2994738==    by 0x48856D0: (below main) (libc_start_call_main.h:74)
==2994738==  Address 0x4a43048 is 8 bytes inside a block of size 32 free'd
==2994738==    at 0x48431EF: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10BD2D: deallocImpl__system_u1765 (in /tmp/b)
==2994738==    by 0x10BD48: deallocSharedImpl__system_u1778 (in /tmp/b)
==2994738==    by 0x10BD63: deallocShared (in /tmp/b)
==2994738==    by 0x10BD89: alignedDealloc (in /tmp/b)
==2994738==    by 0x112AED: eqdestroy___stdZexitprocs_u301 (in /tmp/b)
==2994738==    by 0x112E1F: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738==  Block was alloc'd at
==2994738==    at 0x48459F3: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10CFDF: alloc0Impl__system_u1763 (in /tmp/b)
==2994738==    by 0x10D001: allocShared0Impl__system_u1776 (in /tmp/b)
==2994738==    by 0x10D02E: alignedAlloc0__system_u1947 (in /tmp/b)
==2994738==    by 0x10D30F: newSeqPayload (in /tmp/b)
==2994738==    by 0x10D879: prepareSeqAdd (in /tmp/b)
==2994738==    by 0x111674: add__stdZexitprocs_u226 (in /tmp/b)
==2994738==    by 0x11297B: addExitProc__stdZexitprocs_u220 (in /tmp/b)
==2994738==    by 0x112DE4: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738== 
==2994738== Invalid read of size 8
==2994738==    at 0x112309: eqdestroy___stdZexitprocs_u94 (in /tmp/b)
==2994738==    by 0x1111ED: reset__stdZexitprocs_u58 (in /tmp/b)
==2994738==    by 0x1112FA: shrink__stdZexitprocs_u38 (in /tmp/b)
==2994738==    by 0x111394: setLen__stdZexitprocs_u31 (in /tmp/b)
==2994738==    by 0x112780: callClosures__stdZexitprocs_u19 (in /tmp/b)
==2994738==    by 0x489C954: __run_exit_handlers (exit.c:111)
==2994738==    by 0x489CA89: exit (exit.c:141)
==2994738==    by 0x48856D0: (below main) (libc_start_call_main.h:74)
==2994738==  Address 0x4a43058 is 24 bytes inside a block of size 32 free'd
==2994738==    at 0x48431EF: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10BD2D: deallocImpl__system_u1765 (in /tmp/b)
==2994738==    by 0x10BD48: deallocSharedImpl__system_u1778 (in /tmp/b)
==2994738==    by 0x10BD63: deallocShared (in /tmp/b)
==2994738==    by 0x10BD89: alignedDealloc (in /tmp/b)
==2994738==    by 0x112AED: eqdestroy___stdZexitprocs_u301 (in /tmp/b)
==2994738==    by 0x112E1F: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738==  Block was alloc'd at
==2994738==    at 0x48459F3: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10CFDF: alloc0Impl__system_u1763 (in /tmp/b)
==2994738==    by 0x10D001: allocShared0Impl__system_u1776 (in /tmp/b)
==2994738==    by 0x10D02E: alignedAlloc0__system_u1947 (in /tmp/b)
==2994738==    by 0x10D30F: newSeqPayload (in /tmp/b)
==2994738==    by 0x10D879: prepareSeqAdd (in /tmp/b)
==2994738==    by 0x111674: add__stdZexitprocs_u226 (in /tmp/b)
==2994738==    by 0x11297B: addExitProc__stdZexitprocs_u220 (in /tmp/b)
==2994738==    by 0x112DE4: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738== 
==2994738== Invalid write of size 8
==2994738==    at 0x484ACD6: memset (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10AD60: nimSetMem__systemZmemory_u7 (in /tmp/b)
==2994738==    by 0x10ADB2: nimZeroMem (in /tmp/b)
==2994738==    by 0x111206: reset__stdZexitprocs_u58 (in /tmp/b)
==2994738==    by 0x1112FA: shrink__stdZexitprocs_u38 (in /tmp/b)
==2994738==    by 0x111394: setLen__stdZexitprocs_u31 (in /tmp/b)
==2994738==    by 0x112780: callClosures__stdZexitprocs_u19 (in /tmp/b)
==2994738==    by 0x489C954: __run_exit_handlers (exit.c:111)
==2994738==    by 0x489CA89: exit (exit.c:141)
==2994738==    by 0x48856D0: (below main) (libc_start_call_main.h:74)
==2994738==  Address 0x4a43048 is 8 bytes inside a block of size 32 free'd
==2994738==    at 0x48431EF: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10BD2D: deallocImpl__system_u1765 (in /tmp/b)
==2994738==    by 0x10BD48: deallocSharedImpl__system_u1778 (in /tmp/b)
==2994738==    by 0x10BD63: deallocShared (in /tmp/b)
==2994738==    by 0x10BD89: alignedDealloc (in /tmp/b)
==2994738==    by 0x112AED: eqdestroy___stdZexitprocs_u301 (in /tmp/b)
==2994738==    by 0x112E1F: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738==  Block was alloc'd at
==2994738==    at 0x48459F3: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10CFDF: alloc0Impl__system_u1763 (in /tmp/b)
==2994738==    by 0x10D001: allocShared0Impl__system_u1776 (in /tmp/b)
==2994738==    by 0x10D02E: alignedAlloc0__system_u1947 (in /tmp/b)
==2994738==    by 0x10D30F: newSeqPayload (in /tmp/b)
==2994738==    by 0x10D879: prepareSeqAdd (in /tmp/b)
==2994738==    by 0x111674: add__stdZexitprocs_u226 (in /tmp/b)
==2994738==    by 0x11297B: addExitProc__stdZexitprocs_u220 (in /tmp/b)
==2994738==    by 0x112DE4: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738== 
==2994738== Invalid write of size 8
==2994738==    at 0x484ACF2: memset (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10AD60: nimSetMem__systemZmemory_u7 (in /tmp/b)
==2994738==    by 0x10ADB2: nimZeroMem (in /tmp/b)
==2994738==    by 0x111206: reset__stdZexitprocs_u58 (in /tmp/b)
==2994738==    by 0x1112FA: shrink__stdZexitprocs_u38 (in /tmp/b)
==2994738==    by 0x111394: setLen__stdZexitprocs_u31 (in /tmp/b)
==2994738==    by 0x112780: callClosures__stdZexitprocs_u19 (in /tmp/b)
==2994738==    by 0x489C954: __run_exit_handlers (exit.c:111)
==2994738==    by 0x489CA89: exit (exit.c:141)
==2994738==    by 0x48856D0: (below main) (libc_start_call_main.h:74)
==2994738==  Address 0x4a43050 is 16 bytes inside a block of size 32 free'd
==2994738==    at 0x48431EF: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10BD2D: deallocImpl__system_u1765 (in /tmp/b)
==2994738==    by 0x10BD48: deallocSharedImpl__system_u1778 (in /tmp/b)
==2994738==    by 0x10BD63: deallocShared (in /tmp/b)
==2994738==    by 0x10BD89: alignedDealloc (in /tmp/b)
==2994738==    by 0x112AED: eqdestroy___stdZexitprocs_u301 (in /tmp/b)
==2994738==    by 0x112E1F: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738==  Block was alloc'd at
==2994738==    at 0x48459F3: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2994738==    by 0x10CFDF: alloc0Impl__system_u1763 (in /tmp/b)
==2994738==    by 0x10D001: allocShared0Impl__system_u1776 (in /tmp/b)
==2994738==    by 0x10D02E: alignedAlloc0__system_u1947 (in /tmp/b)
==2994738==    by 0x10D30F: newSeqPayload (in /tmp/b)
==2994738==    by 0x10D879: prepareSeqAdd (in /tmp/b)
==2994738==    by 0x111674: add__stdZexitprocs_u226 (in /tmp/b)
==2994738==    by 0x11297B: addExitProc__stdZexitprocs_u220 (in /tmp/b)
==2994738==    by 0x112DE4: NimMainModule (in /tmp/b)
==2994738==    by 0x112D05: NimMainInner (in /tmp/b)
==2994738==    by 0x112D16: NimMain (in /tmp/b)
==2994738==    by 0x112D50: main (in /tmp/b)
==2994738== 
==2994738== 
==2994738== HEAP SUMMARY:
==2994738==     in use at exit: 0 bytes in 0 blocks
==2994738==   total heap usage: 1 allocs, 1 frees, 32 bytes allocated
==2994738== 
==2994738== All heap blocks were freed -- no leaks are possible
==2994738== 
==2994738== For lists of detected and suppressed errors, rerun with: -s
==2994738== ERROR SUMMARY: 8 errors from 7 contexts (suppressed: 0 from 0)

Expected Output

ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Possible Solution

Backport relevant commit(s) from devel which fix this to version-2-0

Additional Information

No response

tersec avatar Feb 15 '24 02:02 tersec