CakeML / CakeML/cakeml

Make chars_to_nums tail-recursive?

Open
#1,392 1 comment 0 reactions 0 assignees View on GitHub
dev experience good first issue help wanted
Dominant language
Standard ML
Stars
1.2k
Forks
104
Avg merge
2d 21h
Merged PRs (30d)
16

Description

When I tried to `perf` Candle using a custom version of the compiler that "simulates" the calls in CakeML by adjusting the C stack (pushing/popping two addresses), I ran into segfaults:
```
warning: failed to parse execution context from corefile: Cannot access memory at address 0x7ffede486ff0
Failed to read a valid object file image from memory.
Core was generated by `./cake --candle'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00005618030e23f8 in cml_decodeProg_chars_to_nums_9849 ()
#0 0x00005618030e23f8 in cml_decodeProg_chars_to_nums_9849 ()
Backtrace stopped: Cannot access memory at address 0x7ffeda487000
rsp 0x7ffeda487000 0x7ffeda487000
rbp 0x7ffeda487000 0x7ffeda487000
rip 0x5618030e23f8 0x5618030e23f8
```
Note that this happens on startup and also happens with `--repl`.

Claude suggests that this is because `chars_to_nums` is not tail-recursive (which it isn't):
```
Definition chars_to_nums_def:
chars_to_nums ns =
if NULL ns then [] else
let (k,ks) = dec_next 0 1 ns in
k :: chars_to_nums ks
```
The "fix" was to bump up my `ulimit -s` to something larger. Claude suggests that it needs to be ~80MB with the current definition.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the chars_to_nums_def definition and trace how chars_to_nums is used during Candle startup and with --repl. Reproduce the failure under the reported stack limit, then verify that the revised traversal is tail-recursive and no longer causes the observed stack growth or segfault.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.