mal icon indicating copy to clipboard operation
mal copied to clipboard

Vala in self-hosted mode (i.e. running mal) has rare memory corruption (probably in throw/try/catch)

Open kanaka opened this issue 6 months ago • 1 comments

I was seeing this issue in step9 tests and simplified it down to the following test file (vala-memory-corruption.mal):

(try* (nth () 1) (catch* e (prn "e:" e)))
(try* (list 1) (catch* e (prn "e:" e)))
(try* (throw "e") (catch* e (do (prn "e:" e) 7)))
(try* (do (try* "t1" (catch* e "c1")) (throw "e1")) (catch* e "c2"))
(try* (try* (throw "e1") (catch* e (throw "e2"))) (catch* e "c2"))
(try* (map throw (list "my err")) (catch* e e))
(try* (eval (read-string "(+ 1")) (catch* e (prn :e e)))

;; This fails
(map (fn* (x) (list? x)) (list 1 2 3))
;=>(false false false)

Note that it is very sensitive to exactly the content above. If you duplicate or remove any of the try/catch or modify them in any significant way then the problem in the last test goes away (fairly strong indication of memory corruption).

Run the test above in self-hosted mode (vala stepA running mal step9) like this:

$ docker run -it --rm -u $(id -u) -v $(pwd):/mal -w /mal/impls/vala -e STEP=step9_try -e MAL_IMPL=vala ghcr.io/kanaka/mal-test-vala:20200211_055016-g8a19f603 ../../runtest.py ../../vala-memory-corruption.mal -- ../mal/run
TEST: '(try* (nth () 1) (catch* e (prn "e:" e)))' -> ['',] -> SUCCESS (result ignored)
...
TEST: '(map (fn* (x) (list? x)) (list 1 2 3))' -> ['',(false false false)] -> FAIL (line 11):
    Expected : '.*\n\\(false\\ false\\ false\\)'
    Got      : "(map (fn* (x) (list? x)) (list 1 2 3))\nUncaught exception: 'ast' not found \n  in mal EVAL: (map (fn* (x) (list? x)) (list 1 2 3))"

@sgtatham Since you contributed the vala implementation, any chance you want to take a look at this? If it's not immediately obvious, I would probably try and leverage valgrind (unless vala has a more bespoke memory leak debug program).

kanaka avatar Aug 21 '24 14:08 kanaka