7 Bug-Reported-by: Frode Nordahl <frode.nordahl@canonical.com>
8 Bug-Reference-ID: <20221119070714.351759-1-frode.nordahl@canonical.com>
9 Bug-Reference-URL: https://lists.gnu.org/archive/html/bug-bash/2022-11/msg00078.html
13 There are several cases where bash is too aggressive when optimizing out forks
14 in subshells. For example, `eval' and traps should never be optimized.
16 Patch (apply with `patch -p0'):
18 *** ../bash-5.2-patched/builtins/common.h 2022-11-23 17:09:18.000000000 -0500
19 --- builtins/common.h 2022-11-19 18:03:59.000000000 -0500
23 #define SEVAL_ONECMD 0x100 /* only allow a single command */
24 #define SEVAL_NOHISTEXP 0x200 /* inhibit history expansion */
25 + #define SEVAL_NOOPTIMIZE 0x400 /* don't try to set optimization flags */
27 /* Flags for describe_command, shared between type.def and command.def */
28 *** ../bash-5.2-patched/builtins/evalstring.c 2022-11-05 17:27:44.000000000 -0400
29 --- builtins/evalstring.c 2022-11-19 18:23:21.000000000 -0500
32 (command->value.Connection->connector == AND_AND || command->value.Connection->connector == OR_OR || command->value.Connection->connector == ';') &&
33 (command->value.Connection->second->flags & CMD_TRY_OPTIMIZING) &&
34 ! ((startup_state == 2 && should_suppress_fork (command->value.Connection->second)) ||
35 ! ((subshell_environment & SUBSHELL_PAREN) && should_optimize_fork (command->value.Connection->second, 0))))
37 command->value.Connection->second->flags |= CMD_NO_FORK;
39 (command->value.Connection->connector == AND_AND || command->value.Connection->connector == OR_OR || command->value.Connection->connector == ';') &&
40 (command->value.Connection->second->flags & CMD_TRY_OPTIMIZING) &&
41 ! (should_suppress_fork (command->value.Connection->second) ||
42 ! ((subshell_environment & SUBSHELL_PAREN) && should_optimize_fork (command->value.Connection->second, 0))))
44 command->value.Connection->second->flags |= CMD_NO_FORK;
48 (flags & SEVAL_RESETLINE) -> reset line_number to 1
49 (flags & SEVAL_NOHISTEXP) -> history_expansion_inhibited -> 1
50 + (flags & SEVAL_NOOPTIMIZE) -> don't try to turn on optimizing flags
55 series of connection commands is
56 command->value.Connection->second. */
57 ! else if (command->type == cm_connection && can_optimize_connection (command))
59 command->value.Connection->second->flags |= CMD_TRY_OPTIMIZING;
61 series of connection commands is
62 command->value.Connection->second. */
63 ! else if (command->type == cm_connection &&
64 ! (flags & SEVAL_NOOPTIMIZE) == 0 &&
65 ! can_optimize_connection (command))
67 command->value.Connection->second->flags |= CMD_TRY_OPTIMIZING;
68 *** ../bash-5.2-patched/builtins/eval.def 2016-01-25 13:28:37.000000000 -0500
69 --- builtins/eval.def 2022-11-19 18:04:25.000000000 -0500
72 list = loptend; /* skip over possible `--' */
74 ! return (list ? evalstring (string_list (list), "eval", SEVAL_NOHIST) : EXECUTION_SUCCESS);
77 list = loptend; /* skip over possible `--' */
79 ! return (list ? evalstring (string_list (list), "eval", SEVAL_NOHIST|SEVAL_NOOPTIMIZE) : EXECUTION_SUCCESS);
81 *** ../bash-5.2-patched/trap.c 2022-08-10 08:59:45.000000000 -0400
82 --- trap.c 2022-12-12 10:57:51.000000000 -0500
86 volatile int save_return_catch_flag, function_code;
87 procenv_t save_return_catch;
88 + char *trap_command, *old_trap;
89 #if defined (ARRAY_VARS)
96 + old_trap = trap_list[sig];
97 + trap_command = savestring (old_trap);
99 save_parser_state (&pstate);
100 save_subst_varlist = subst_assign_varlist;
104 if (function_code == 0)
105 ! x = parse_and_execute (savestring (trap_list[sig]), "trap", SEVAL_NONINT|SEVAL_NOHIST|SEVAL_RESETLINE);
110 if (function_code == 0)
111 ! /* XXX is x always last_command_exit_value? */
112 ! x = parse_and_execute (trap_command, "trap", SEVAL_NONINT|SEVAL_NOHIST|SEVAL_RESETLINE|SEVAL_NOOPTIMIZE);
119 ! parse_and_execute (trap_command, "exit trap", SEVAL_NONINT|SEVAL_NOHIST|SEVAL_RESETLINE);
121 else if (code == ERREXIT)
125 ! parse_and_execute (trap_command, "exit trap", SEVAL_NONINT|SEVAL_NOHIST|SEVAL_RESETLINE|SEVAL_NOOPTIMIZE);
127 else if (code == ERREXIT)
132 ! flags = SEVAL_NONINT|SEVAL_NOHIST;
133 if (sig != DEBUG_TRAP && sig != RETURN_TRAP && sig != ERROR_TRAP)
134 flags |= SEVAL_RESETLINE;
138 ! flags = SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOOPTIMIZE;
139 if (sig != DEBUG_TRAP && sig != RETURN_TRAP && sig != ERROR_TRAP)
140 flags |= SEVAL_RESETLINE;
141 *** ../bash-5.2-patched/parse.y 2022-11-23 17:09:18.000000000 -0500
142 --- parse.y 2022-11-19 18:15:34.000000000 -0500
145 last_lastarg = savestring (last_lastarg);
147 ! parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST);
149 restore_parser_state (&ps);
151 last_lastarg = savestring (last_lastarg);
153 ! parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOOPTIMIZE);
155 restore_parser_state (&ps);
156 *** ../bash-5.2-patched/jobs.c 2022-07-18 10:19:56.000000000 -0400
157 --- jobs.c 2022-11-19 18:10:24.000000000 -0500
160 for (i = 0; i < nchild; i++)
162 ! parse_and_execute (savestring (trap_command), "trap", SEVAL_NOHIST|SEVAL_RESETLINE);
166 for (i = 0; i < nchild; i++)
168 ! parse_and_execute (savestring (trap_command), "trap", SEVAL_NOHIST|SEVAL_RESETLINE|SEVAL_NOOPTIMIZE);
171 *** ../bash-5.2-patched/y.tab.c 2022-11-23 17:09:18.000000000 -0500
172 --- y.tab.c 2022-11-23 17:21:17.000000000 -0500
175 last_lastarg = savestring (last_lastarg);
177 ! parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST);
179 restore_parser_state (&ps);
181 last_lastarg = savestring (last_lastarg);
183 ! parse_and_execute (savestring (command), vname, SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOOPTIMIZE);
185 restore_parser_state (&ps);
186 *** ../bash-5.2-patched/execute_cmd.c 2022-11-05 17:27:41.000000000 -0400
187 --- execute_cmd.c 2022-11-22 17:09:38.000000000 -0500
190 and set CMD_TRY_OPTIMIZING for simple commands on the right side of an
191 and-or or `;' list to test for optimizing forks when they are executed. */
192 ! if (user_subshell && command->type == cm_subshell)
193 optimize_subshell_command (command->value.Subshell->command);
196 and set CMD_TRY_OPTIMIZING for simple commands on the right side of an
197 and-or or `;' list to test for optimizing forks when they are executed. */
198 ! if (user_subshell && command->type == cm_subshell &&
199 ! (command->flags & (CMD_TIME_PIPELINE|CMD_INVERT_RETURN)) == 0)
200 optimize_subshell_command (command->value.Subshell->command);
202 *** ../bash-5.2/patchlevel.h 2020-06-22 14:51:03.000000000 -0400
203 --- patchlevel.h 2020-10-01 11:01:28.000000000 -0400
206 looks for to find the patch level (for the sccs version string). */
208 ! #define PATCHLEVEL 14
210 #endif /* _PATCHLEVEL_H_ */
212 looks for to find the patch level (for the sccs version string). */
214 ! #define PATCHLEVEL 15
216 #endif /* _PATCHLEVEL_H_ */