From 85f3cbb31ff522bda731b493a28a2863abd766eb Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 25 Oct 2026 22:17:08 -0500 Subject: [PATCH] A simpler, peg only alternative to the buffer-pin branch. --- .github/workflows/test.yml | 2 +- src/core/buffer.c | 2 +- src/core/peg.c | 45 +++++++++++++++++++++++++++++++++----- src/core/string.c | 9 ++++++++ test/suite-peg.janet | 31 ++++++++++++++++++++++++++ test/suite-string.janet | 27 +++++++++++++++++++++++ 6 files changed, 109 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6b56aadb..067987ef 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -174,7 +174,7 @@ jobs: test-cosmo: name: Test build for Cosmo - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - name: Checkout the repository uses: actions/checkout@master diff --git a/src/core/buffer.c b/src/core/buffer.c index d5c87950..c785a826 100644 --- a/src/core/buffer.c +++ b/src/core/buffer.c @@ -31,7 +31,7 @@ /* Allow for managed buffers that cannot realloc/free their backing memory */ static void janet_buffer_can_realloc(JanetBuffer *buffer) { if (buffer->gc.flags & JANET_BUFFER_FLAG_NO_REALLOC) { - janet_panic("buffer cannot reallocate foreign memory"); + janet_panic("buffer cannot reallocate pinned memory"); } } diff --git a/src/core/peg.c b/src/core/peg.c index c9ec8a9c..2d8a7e04 100644 --- a/src/core/peg.c +++ b/src/core/peg.c @@ -1953,6 +1953,8 @@ typedef struct { JanetByteView bytes; Janet subst; int32_t start; + JanetBuffer *input_buffer; + int32_t old_flags; } PegCall; /* Initialize state for peg cfunctions */ @@ -1966,11 +1968,18 @@ static PegCall peg_cfun_init(int32_t argc, Janet *argv, int get_replace) { } else { ret.peg = compile_peg(argv[0]); } + int input_loc = 1; if (get_replace) { ret.subst = argv[1]; - ret.bytes = janet_getbytes(argv, 2); + input_loc = 2; + } + ret.bytes = janet_getbytes(argv, input_loc); + if (janet_checktype(argv[input_loc], JANET_BUFFER)) { + ret.input_buffer = janet_unwrap_buffer(argv[input_loc]); + ret.old_flags = ret.input_buffer->gc.flags; + ret.input_buffer->gc.flags |= JANET_BUFFER_FLAG_NO_REALLOC; } else { - ret.bytes = janet_getbytes(argv, 1); + ret.input_buffer = NULL; } if (argc > min) { ret.start = janet_gethalfrange(argv, min, ret.bytes.len, "offset"); @@ -2006,12 +2015,29 @@ static void peg_call_reset(PegCall *c) { c->s.tags->count = 0; } +#define PEG_TRY_SETUP \ + JanetTryState tstate; \ + if (c.input_buffer && janet_try(&tstate)) { \ + c.input_buffer->gc.flags = c.old_flags; \ + Janet result = *janet_vm.return_reg; \ + janet_restore(&tstate); \ + janet_panicv(result); /* re-raise */ \ + } + +#define PEG_TRY_TEARDOWN \ + if (c.input_buffer) { \ + janet_restore(&tstate); \ + c.input_buffer->gc.flags = c.old_flags; \ + } + JANET_CORE_FN(cfun_peg_match, "(peg/match peg text &opt start & args)", "Match a Parsing Expression Grammar to a byte string and return an array of captured values. " "Returns nil if text does not match the language defined by peg. The syntax of PEGs is documented on the Janet website.") { PegCall c = peg_cfun_init(argc, argv, 0); + PEG_TRY_SETUP const uint8_t *result = peg_rule(&c.s, c.s.bytecode, c.bytes.bytes + c.start); + PEG_TRY_TEARDOWN return result ? janet_wrap_array(c.s.captures) : janet_wrap_nil(); } @@ -2019,29 +2045,37 @@ JANET_CORE_FN(cfun_peg_find, "(peg/find peg text &opt start & args)", "Find first index where the peg matches in text. Returns an integer, or nil if not found.") { PegCall c = peg_cfun_init(argc, argv, 0); + PEG_TRY_SETUP + Janet result = janet_wrap_nil(); for (int32_t i = c.start; i < c.bytes.len; i++) { peg_call_reset(&c); - if (peg_rule(&c.s, c.s.bytecode, c.bytes.bytes + i)) - return janet_wrap_integer(i); + if (peg_rule(&c.s, c.s.bytecode, c.bytes.bytes + i)) { + result = janet_wrap_integer(i); + break; + } } - return janet_wrap_nil(); + PEG_TRY_TEARDOWN + return result; } JANET_CORE_FN(cfun_peg_find_all, "(peg/find-all peg text &opt start & args)", "Find all indexes where the peg matches in text. Returns an array of integers.") { PegCall c = peg_cfun_init(argc, argv, 0); + PEG_TRY_SETUP JanetArray *ret = janet_array(0); for (int32_t i = c.start; i < c.bytes.len; i++) { peg_call_reset(&c); if (peg_rule(&c.s, c.s.bytecode, c.bytes.bytes + i)) janet_array_push(ret, janet_wrap_integer(i)); } + PEG_TRY_TEARDOWN return janet_wrap_array(ret); } static Janet cfun_peg_replace_generic(int32_t argc, Janet *argv, int only_one) { PegCall c = peg_cfun_init(argc, argv, 1); + PEG_TRY_SETUP JanetBuffer *ret = janet_buffer(0); int32_t trail = 0; for (int32_t i = c.start; i < c.bytes.len;) { @@ -2066,6 +2100,7 @@ static Janet cfun_peg_replace_generic(int32_t argc, Janet *argv, int only_one) { if (trail < c.bytes.len) { janet_buffer_push_bytes(ret, c.bytes.bytes + trail, (c.bytes.len - trail)); } + PEG_TRY_TEARDOWN return janet_wrap_buffer(ret); } diff --git a/src/core/string.c b/src/core/string.c index 99ec446d..ef9ae9f2 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -380,6 +380,15 @@ struct replace_state { static void replacesetup(int32_t argc, Janet *argv, struct replace_state *s) { janet_arity(argc, 3, 4); + /* A callback may resize a buffer argument, so search a copy instead */ + if (janet_checktype(argv[1], JANET_FUNCTION) || janet_checktype(argv[1], JANET_CFUNCTION)) { + for (int32_t i = 0; i < 3; i += 2) { + if (janet_checktype(argv[i], JANET_BUFFER)) { + JanetBuffer *buffer = janet_unwrap_buffer(argv[i]); + argv[i] = janet_stringv(buffer->data, buffer->count); + } + } + } JanetByteView pat = janet_getbytes(argv, 0); Janet subst = argv[1]; JanetByteView text = janet_getbytes(argv, 2); diff --git a/test/suite-peg.janet b/test/suite-peg.janet index 8dc54cab..9819bc38 100644 --- a/test/suite-peg.janet +++ b/test/suite-peg.janet @@ -963,4 +963,35 @@ ```)) +# A matchtime or replacement callback that grows the buffer being matched +# reallocates its storage. The matcher kept pointing at the old allocation, +# so the rest of the match read freed memory. +(def peg-grow-buf @"aaaaaaaa") +(assert-error + "peg/match callback resizing input buffer" + (peg/match ~(* (cmt (<- "a") + ,(fn [_] + (buffer/push peg-grow-buf (string/repeat "x" 100000)) + true)) + (some "a")) + peg-grow-buf)) + +(def peg-grow-buf2 @"aXbXcXd") +(assert-error + "peg/replace callback resizing input buffer" + (peg/replace "X" + (fn [&] + (buffer/push peg-grow-buf2 (string/repeat "y" 100000)) + "Z") + peg-grow-buf2)) + +(def peg-grow-buf3 @"aXbXcXd") +(assert-error + "peg/replace-all callback resizing input buffer" + (peg/replace-all "X" + (fn [&] + (buffer/push peg-grow-buf3 (string/repeat "y" 100000)) + "Z") + peg-grow-buf3)) + (end-suite) diff --git a/test/suite-string.janet b/test/suite-string.janet index a7cd7e4c..5eb46203 100644 --- a/test/suite-string.janet +++ b/test/suite-string.janet @@ -139,5 +139,32 @@ # Check string formatting, #1600 (assert (= "" (string/format "%.99s" @"")) "string/format %s buffer") +# A replacement callback that grows the text or pattern buffer used to leave +# string/replace and string/replace-all reading freed memory. +(def str-grow-buf @"aXbXcXd") +(assert (= "aZbZcZd" + (string/replace-all "X" + (fn [_] + (buffer/push str-grow-buf (string/repeat "y" 100000)) + "Z") + str-grow-buf)) + "string/replace-all callback resizing text buffer") +(def str-grow-buf2 @"aXbXcXd") +(assert (= "aZbXcXd" + (string/replace "X" + (fn [_] + (buffer/push str-grow-buf2 (string/repeat "y" 100000)) + "Z") + str-grow-buf2)) + "string/replace callback resizing text buffer") +(def str-grow-patt @"X") +(assert (= "aZbZcZ" + (string/replace-all str-grow-patt + (fn [_] + (buffer/push str-grow-patt (string/repeat "y" 100000)) + "Z") + "aXbXcX")) + "string/replace-all callback resizing pattern buffer") + (end-suite)