Files
janet/test/suite-asm.janet
nabeel-dev21 8132a7368e reject out-of-range captured environment index in janet_verify (#1926)
* reject out-of-range captured environment index in janet_verify

* treat any negative env index as capture-current-frame in the vm
2026-10-24 09:26:12 -05:00

85 lines
3.3 KiB
Janet

# Copyright (c) 2026 Calvin Rose
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
(import ./helper :prefix "" :exit true)
(start-suite)
(setdyn *lint-warn* :none)
# Assembly test
# Fibonacci sequence, implemented with naive recursion.
# a679f60
(def fibasm (asm '{
:arity 1
:bytecode [
(ltim 1 0 0x2) # $1 = $0 < 2
(jmpif 1 :done) # if ($1) goto :done
(lds 1) # $1 = self
(addim 0 0 -0x1) # $0 = $0 - 1
(push 0) # push($0), push argument for next function call
(call 2 1) # $2 = call($1)
(addim 0 0 -0x1) # $0 = $0 - 1
(push 0) # push($0)
(call 0 1) # $0 = call($1)
(add 0 0 2) # $0 = $0 + $2 (integers)
:done
(ret 0) # return $0
]
}))
(assert (= 0 (fibasm 0)) "fibasm 1")
(assert (= 1 (fibasm 1)) "fibasm 2")
(assert (= 55 (fibasm 10)) "fibasm 3")
(assert (= 6765 (fibasm 20)) "fibasm 4")
# dacbe29
(def f (asm (disasm (fn [x] (fn [y] (+ x y))))))
(assert (= ((f 10) 37) 47) "asm environment tables")
# issue #1424
(assert-no-error "arity > used slots (issue #1424)"
(asm
(disasm
(fn []
(def foo (fn [one two] one))
(foo 100 200)))))
# Reject out-of-range captured environment index. Only -1 (capture current
# frame) or a non-negative index is valid; a value below -1 would make
# JOP_CLOSURE read func->envs out of bounds.
(assert-error "environment index below -1"
(asm ~{:arity 1 :min-arity 1 :max-arity 1 :slotcount 2
:bytecode [(clo 1 0) (ret 1)]
:defs @[@{:arity 0 :min-arity 0 :max-arity 0 :slotcount 1
:bytecode [(ldi 0 0) (ret 0)]
:environments @[-1000000]}]}))
# Optimization in assembler
# a bit brittle, but easy to fix
(def optimized (disasm (asm ~{:bytecode [(ldi 0 10) (movn 1 0) (add 2 0 1) (add 3 1 1) (add 4 0 0) (add 5 2 3) (ret 5)] :slotcount 40} :o) :bytecode))
(assert (deep= optimized '@[(ldi 0 40) (ret 0)]) "bytecode optimization 1")
# short sourcemap/symbolmap entries must not read past the tuple
(assert-error "short sourcemap entry" (asm '{:arity 0 :bytecode [(ret 0)] :sourcemap [[1]]}))
(assert-error "short symbolmap entry" (asm '{:arity 0 :bytecode [(ret 0)] :symbolmap [[1 2 3]]}))
(end-suite)