Skip to content

[Fulminate] Fix memory-access instrumentation of prefix ++ and -- - #708

Open
sliedes wants to merge 1 commit into
rems-project:mainfrom
sliedes:fulminate-prefix-incr-decr
Open

sliedes wants to merge 1 commit into
rems-project:mainfrom
sliedes:fulminate-prefix-incr-decr

Conversation

@sliedes

@sliedes sliedes commented Nov 17, 2026

Copy link
Copy Markdown

This PR was prepared with Claude Code. I have reviewed it (but admit I do not understand the code fully).


With ownership checking on (the default), cn instrument fails on any prefix increment or decrement. On main this minimal program

int main(void)
{
  int n = 1;
  --n;
  return n;
}

gives

cn: internal error, uncaught exception:
    Invalid_argument("Bytes.create")
    Raised by primitive operation at Stdlib.really_input_string in file "stdlib.ml", line 439, characters 10-26
    Called from Cn__Fulminate__Source_injection.move_to.aux in file "lib/fulminate/source_injection.ml", line 152, characters 10-49

Cerberus desugars ++E and --E to E += 1 and E -= 1. Since rems-project/cerberus@636b75880 (in the Cerberus pin since #701) the synthesized 1 has the location of the whole --E, so the StoreOp case of memory_accesses_injections treats it as the right operand of a compound assignment and emits a ,-, edit from the end of E back to the start of --E. That edit runs backwards, and move_to asks really_input_string for a negative length. Before that Cerberus change, the 1 had no location and took the `Other branch added in #84. That branch does not work with give_parenthesis_aware_precedence_map either: its empty injection strings have no last character, so instrumenting such a file failed with an index out of bounds there.

This change handles both forms of a prefix operator the same way: the operator becomes CN_STORE_OP( and the call is closed after the lvalue. The lvalue's source, and the instrumentation of any loads inside it, stay in place, so --*p becomes CN_STORE_OP(*CN_LOAD(p),-,1), and ++a[i] and --q->x work too. Ordinary compound assignments are instrumented as before.

The new test tests/cn/prefix_increments.c is a prefix counterpart of increments.c. It covers prefix operators on locals, inside a condition and an assignment, and on *p, a[i] and q->x, with postconditions that check the results. It verifies, and runs under tests/run-cn-exec.sh. It crashed cn instrument before this change.

Testing: tests/diff-prog.py cn tests/cn/verify.json (214 passed) and tests/run-cn-exec.sh (213 passed) pass, and dune build @fmt is clean. A prefix -- on memory the function does not own now fails at run time with "Store failed", as a plain store does.

Related: #80 (the same Bytes.create crash on ++a, fixed then by #84) and rems-project/cerberus#807.

Cerberus desugars ++E and --E to E += 1 and E -= 1. Since
rems-project/cerberus@636b75880 the synthesized 1 carries the location
of the whole ++E, so the store-op instrumentation took it for the
right-hand operand of a compound assignment and emitted an edit whose
end precedes its start. With ownership checking, any prefix increment
or decrement then made `cn instrument` fail with
Invalid_argument("Bytes.create") in Source_injection.move_to.

The earlier path for an unlocated 1 (before that Cerberus change) did
not work with give_parenthesis_aware_precedence_map either: its empty
injections have no last character to look for a closing parenthesis
in, so it failed with an index out of bounds (or reached its
`assert false`).

Handle both forms of a prefix operator the same way: replace the
operator with "CN_STORE_OP(" and close the call after E. E's source,
and the instrumentation of any loads inside it, stay in place, so
--*p, ++a[i] and --q->x work as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013zncxneLMKZJE5V7oqV7mo
@rbanerjee20

Copy link
Copy Markdown
Contributor

I might like to implement this fix on the Cerberus side instead of hacking around the positions any further (taking this opportunity to clean it up a bit). Once that's in place I'll close this PR.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants