sophiakoulen / minishell

A simplified bash-like shell, with pipes, redirections and variable expansion.
4 stars 1 forks source link

real leak in exec_exit env_add it seems #136

Closed sophiakoulen closed 1 year ago

znichola commented 1 year ago

part of it could be a leak from readline!

tested export echo the exported var, and unsettling

==1251== 4,080 bytes in 1 blocks are still reachable in loss record 61 of 82
==1251==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1251==    by 0x4899BAC: xmalloc (in /usr/lib/x86_64-linux-gnu/libreadline.so.8.1)
==1251==    by 0x48953F9: _rl_init_terminal_io (in /usr/lib/x86_64-linux-gnu/libreadline.so.8.1)
==1251==    by 0x4881A0F: rl_initialize (in /usr/lib/x86_64-linux-gnu/libreadline.so.8.1)
==1251==    by 0x4881CD4: readline (in /usr/lib/x86_64-linux-gnu/libreadline.so.8.1)
==1251==    by 0x10A72F: read_input (main.c:60)
==1251==    by 0x10A77B: interactive_shell (main.c:80)
==1251==    by 0x10A63F: main (main.c:31)
znichola commented 1 year ago

but maybe not

==1251== LEAK SUMMARY:
==1251==    definitely lost: 24 bytes in 1 blocks
==1251==    indirectly lost: 2,560 bytes in 68 blocks
==1251==      possibly lost: 0 bytes in 0 blocks
==1251==    still reachable: 208,787 bytes in 252 blocks
==1251==         suppressed: 0 bytes in 0 blocks
==1251== 
==1251== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
znichola commented 1 year ago

the still reachable is the memory that exit() frees for us, we should worry about that definitely lost block.

znichola commented 1 year ago

this is the definitely lost block

==1251== 2,584 (24 direct, 2,560 indirect) bytes in 1 blocks are definitely lost in loss record 57 of 82
==1251==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1251==    by 0x10F4C6: x_malloc (utils_1.c:22)
==1251==    by 0x10D840: env_factory (t_env.c:24)
==1251==    by 0x110699: env_add (env_utils.c:55)
==1251==    by 0x10ACF6: exec_exit (exit.c:51)
==1251==    by 0x10ECFD: exec_builtin (builtin.c:108)
==1251==    by 0x10EB7D: launch_builtin (builtin.c:28)
==1251==    by 0x10E161: exec_pipeline (exec_pipeline.c:42)
==1251==    by 0x10E0A4: exec_tree (exec_pipeline.c:22)
==1251==    by 0x10A901: exec_line (main.c:115)
==1251==    by 0x10A7C7: interactive_shell (main.c:87)
==1251==    by 0x10A63F: main (main.c:31)
znichola commented 1 year ago

ok I've narrowed it down a bit

*[main][/minishell]$ valgrind --leak-check=full \          
         --show-leak-kinds=all \
         --track-origins=yes \
         --log-file=valgrind-out.txt \
         ./minishell
minishell$ export a=1
minishell$ 
exit
*[main][/minishell]$ cat valgrind-out.txt 

...

==1417== 2,551 (24 direct, 2,527 indirect) bytes in 1 blocks are definitely lost in loss record 48 of 73
==1417==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1417==    by 0x10F4C6: x_malloc (utils_1.c:22)
==1417==    by 0x10D840: env_factory (t_env.c:24)
==1417==    by 0x110699: env_add (env_utils.c:55)
==1417==    by 0x10AFAC: exec_export (export.c:64)
==1417==    by 0x10ECFD: exec_builtin (builtin.c:108)
==1417==    by 0x10EB7D: launch_builtin (builtin.c:28)
==1417==    by 0x10E161: exec_pipeline (exec_pipeline.c:42)
==1417==    by 0x10E0A4: exec_tree (exec_pipeline.c:22)
==1417==    by 0x10A901: exec_line (main.c:115)
==1417==    by 0x10A7C7: interactive_shell (main.c:87)
==1417==    by 0x10A63F: main (main.c:31)

...
znichola commented 1 year ago
minishell$ pwd
/minishell
minishell$ 
exit

looks like this is the same leak that it always there when we initialise our env

==1303== 2,523 (24 direct, 2,499 indirect) bytes in 1 blocks are definitely lost in loss record 46 of 71
==1303==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1303==    by 0x10F4C6: x_malloc (in /minishell/minishell)
==1303==    by 0x10D840: env_factory (in /minishell/minishell)
==1303==    by 0x11035B: init_env (in /minishell/minishell)
==1303==    by 0x10A614: main (in /minishell/minishell)
znichola commented 1 year ago

This is turning out to be a bit of an evil leak, can't seems to narrow it down, it's somewhere in export and for some reason the school macs can't see it with instruments. But valgrind definitely shows it as a leak.

minishell$ export a=a
minishell$ export b=b
minishell$ 
exit
*[leaks][/minishell]$ less valgrind-out.txt 
==1200== Memcheck, a memory error detector
==1200== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1200== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==1200== Command: ./minishell
==1200== Parent PID: 1
==1200== 
==1200== 
==1200== HEAP SUMMARY:
==1200==     in use at exit: 208,189 bytes in 228 blocks
==1200==   total heap usage: 1,061 allocs, 833 frees, 370,006 bytes allocated
==1200== 
==1200== 4 bytes in 2 blocks are indirectly lost in loss record 1 of 67
==1200==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1200==    by 0x111C62: ft_x_malloc (in /minishell/minishell)
==1200==    by 0x111A85: ft_strdup (in /minishell/minishell)
==1200==    by 0x1105EE: env_add (env_utils.c:55)
==1200==    by 0x10AFB3: exec_export (export.c:64)
==1200==    by 0x10EC7D: exec_builtin (builtin.c:108)
==1200==    by 0x10EAFD: launch_builtin (builtin.c:28)
==1200==    by 0x10E0E1: exec_pipeline (exec_pipeline.c:48)
==1200==    by 0x10E024: exec_tree (exec_pipeline.c:27)
==1200==    by 0x10A914: exec_line (main.c:116)
==1200==    by 0x10A7FD: interactive_shell (main.c:90)
==1200==    by 0x10A65F: main (main.c:31)
==1200== 
==1200== 4 bytes in 2 blocks are indirectly lost in loss record 2 of 67
==1200==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1200==    by 0x111C62: ft_x_malloc (in /minishell/minishell)
==1200==    by 0x111A85: ft_strdup (in /minishell/minishell)
==1200==    by 0x1105FE: env_add (env_utils.c:55)
==1200==    by 0x10AFB3: exec_export (export.c:64)
==1200==    by 0x10EC7D: exec_builtin (builtin.c:108)
==1200==    by 0x10EAFD: launch_builtin (builtin.c:28)
==1200==    by 0x10E0E1: exec_pipeline (exec_pipeline.c:48)
==1200==    by 0x10E024: exec_tree (exec_pipeline.c:27)
==1200==    by 0x10A914: exec_line (main.c:116)
==1200==    by 0x10A7FD: interactive_shell (main.c:90)
==1200==    by 0x10A65F: main (main.c:31)
==1200== 
==1200== 24 bytes in 1 blocks are indirectly lost in loss record 13 of 67
==1200==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1200==    by 0x10F446: x_malloc (utils_1.c:22)
==1200==    by 0x10D798: env_factory (t_env.c:24)
==1200==    by 0x110619: env_add (env_utils.c:55)
==1200==    by 0x10AFB3: exec_export (export.c:64)
==1200==    by 0x10EC7D: exec_builtin (builtin.c:108)
==1200==    by 0x10EAFD: launch_builtin (builtin.c:28)
==1200==    by 0x10E0E1: exec_pipeline (exec_pipeline.c:48)
==1200==    by 0x10E024: exec_tree (exec_pipeline.c:27)
==1200==    by 0x10A914: exec_line (main.c:116)
==1200==    by 0x10A7FD: interactive_shell (main.c:90)
==1200==    by 0x10A65F: main (main.c:31)
==1200== 
==1200== 56 (24 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 19 of 67
==1200==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==1200==    by 0x10F446: x_malloc (utils_1.c:22)
==1200==    by 0x10D798: env_factory (t_env.c:24)
==1200==    by 0x110619: env_add (env_utils.c:55)
==1200==    by 0x10AFB3: exec_export (export.c:64)
==1200==    by 0x10EC7D: exec_builtin (builtin.c:108)
==1200==    by 0x10EAFD: launch_builtin (builtin.c:28)
==1200==    by 0x10E0E1: exec_pipeline (exec_pipeline.c:48)
==1200==    by 0x10E024: exec_tree (exec_pipeline.c:27)
==1200==    by 0x10A914: exec_line (main.c:116)
==1200==    by 0x10A7FD: interactive_shell (main.c:90)
==1200==    by 0x10A65F: main (main.c:31)
==1200== 
==1200== LEAK SUMMARY:
==1200==    definitely lost: 24 bytes in 1 blocks
==1200==    indirectly lost: 32 bytes in 5 blocks
==1200==      possibly lost: 0 bytes in 0 blocks
==1200==    still reachable: 208,133 bytes in 222 blocks
==1200==         suppressed: 0 bytes in 0 blocks
==1200== Reachable blocks (those to which a pointer was found) are not shown.
==1200== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==1200== 
==1200== For lists of detected and suppressed errors, rerun with: -s
==1200== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)