File 0633-non-JIT-BEAM-Avoid-redundant-GC-in-binary-syntax.patch of Package erlang

From 03323f3959006332d6ba8d82dc4f1ad41e9e8725 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Wed, 18 Jan 2023 16:18:25 +0100
Subject: [PATCH 3/3] non-JIT BEAM: Avoid redundant GC in binary syntax

In the binary syntax, avoid doing a garbage collection when matching
out an integer if the match is known to fail because the binary is
too short. This test is already done in the JIT.
---
 erts/emulator/beam/emu/bs_instrs.tab      | 11 ++++++++++-
 erts/emulator/test/bs_match_int_SUITE.erl |  8 +++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/erts/emulator/beam/emu/bs_instrs.tab b/erts/emulator/beam/emu/bs_instrs.tab
index c8bba1e70a..846e4b6f2c 100644
--- a/erts/emulator/beam/emu/bs_instrs.tab
+++ b/erts/emulator/beam/emu/bs_instrs.tab
@@ -1404,10 +1404,19 @@ bs_get_integer.head() {
 
 bs_get_integer.fetch(Ctx, Size, Live) {
     Uint wordsneeded;
+    ErlBinMatchBuffer* mb;
     Ms = $Ctx;
     Sz = $Size;
     wordsneeded = 1+WSIZE(NBYTES(Sz));
-    $GC_TEST_PRESERVE(wordsneeded, $Live, Ms);
+
+    /* Check bits size before potential GC. We do not want a GC
+     * and then realize we don't need the allocated space (if the
+     * op fails).
+     */
+    mb = ms_matchbuffer(Ms);
+    if (mb->size - mb->offset >= Sz) {
+        $GC_TEST_PRESERVE(wordsneeded, $Live, Ms);
+    }
 }
 
 bs_get_integer.fetch_small(Ctx, Size) {
diff --git a/erts/emulator/test/bs_match_int_SUITE.erl b/erts/emulator/test/bs_match_int_SUITE.erl
index 0268ba18c8..f124183cf9 100644
--- a/erts/emulator/test/bs_match_int_SUITE.erl
+++ b/erts/emulator/test/bs_match_int_SUITE.erl
@@ -275,7 +275,13 @@ match_huge_int(Config) when is_list(Config) ->
                         4 -> lists:seq(25, 32);
                         8 -> []
                     end ++ lists:seq(50, 64),
-            ok = overflow_huge_int_unit128(Bin, Sizes)
+            ok = overflow_huge_int_unit128(Bin, Sizes),
+
+            %% GH-6701: [vm] crash with -emu_flavor emu:
+            %% "no next heap size found: 18446744072702918678, offset 0"
+            {'EXIT',{function_clause,_}} =
+                (catch fun(<<X:2147483647/unit:98>>) -> X end(<<>>)),
+            ok
     end.
 
 overflow_huge_int_unit128(Bin, [Sz0|Sizes]) ->
-- 
2.35.3

openSUSE Build Service is sponsored by