Skip to content

Commit 62be1ce

Browse files
Merge pull request #10439 from sverker/sverker/erts/re-run-trap-killed/OTP-19888
erts: Fix SEGV when killing process during yielding re:run
2 parents f5103fe + 852195f commit 62be1ce

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

erts/emulator/beam/erl_bif_re.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,7 @@ re_run(Process *p, Eterm arg1, Eterm arg2, Eterm arg3, bool first)
17361736
Eterm *hp;
17371737
BUMP_REDS(p, reds_consumed);
17381738
sys_memcpy(restartp,&restart,sizeof(RestartContext));
1739+
pcre2_set_restart_data(restart.match_data, &restartp->restart_data);
17391740
ERTS_VBUMP_ALL_REDS(p);
17401741
hp = HAlloc(p, ERTS_MAGIC_REF_THING_SIZE);
17411742
magic_ref = erts_mk_magic_ref(&hp, &MSO(p), mbp);
@@ -1851,7 +1852,6 @@ static BIF_RETTYPE re_match_trap(BIF_ALIST_3)
18511852
restartp = (RestartContext *) ERTS_MAGIC_BIN_DATA(mbp);
18521853
loop_limit = MIN(reds_initial * LOOP_FACTOR, max_loop_limit);
18531854
pcre2_set_loops_left(restartp->match_data, loop_limit);
1854-
pcre2_set_restart_data(restartp->match_data, &restartp->restart_data);
18551855
pcre2_set_restart_flags(restartp->match_data, 0);
18561856

18571857
rc = pcre2_match(NULL, NULL, 0, 0, 0, restartp->match_data, NULL);

lib/stdlib/test/re_SUITE.erl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
yield_on_subject_validation/1, bad_utf8_subject/1,
3737
error_info/1, subject_is_sub_binary/1, pattern_is_sub_binary/1,
3838
import/1,
39+
kill_yielding/1,
3940

4041
last_test/1]).
4142

@@ -62,6 +63,7 @@ all() ->
6263
yield_on_subject_validation, bad_utf8_subject,
6364
error_info, subject_is_sub_binary, pattern_is_sub_binary,
6465
import,
66+
kill_yielding,
6567

6668
last_test].
6769

@@ -1246,7 +1248,29 @@ build_exported(Hdr1, RE, Opts, Enc) ->
12461248
Hdr2 = <<Title/binary, CRC2:32, HdrRest/binary>>,
12471249
{re_exported_pattern, Hdr2, RE, Opts, Enc}.
12481250

1251+
%% OTP-19888: Verify that process can handle being killed while yielding
1252+
%% inside re:run without beam crash or memory leak.
1253+
kill_yielding(Config) when is_list(Config) ->
1254+
Subject = binary:copy(~"hejsan", 100_000),
1255+
DoIt = fun(RE) ->
1256+
{Pid, MRef} = spawn_monitor(fun() ->
1257+
re:run(Subject, RE)
1258+
end),
1259+
erlang:yield(),
1260+
exit(Pid, kill),
1261+
{'DOWN',MRef,process,Pid,killed} = receive_any()
1262+
end,
1263+
1264+
RE_string = ~B"\w+\d",
1265+
{ok, RE_compiled} = re:compile(RE_string, []),
1266+
DoIt(RE_string),
1267+
DoIt(RE_compiled),
1268+
ok.
12491269

1270+
receive_any() ->
1271+
receive M -> M
1272+
after 1000 -> timeout
1273+
end.
12501274

12511275
last_test(Config) when is_list(Config) ->
12521276
erts_debug:set_internal_state(available_internal_state, true),

0 commit comments

Comments
 (0)