cyclone icon indicating copy to clipboard operation
cyclone copied to clipboard

Segfault when applying procedure

Open arthurmaciel opened this issue 4 years ago • 3 comments

Hi, @justinethier! Could you help me please? What am I missing?

(import (scheme base)
        (scheme write))

(define proc-list
  '(("lambda-add"
     . (lambda ()
         ((lambda (a b c d)
            (+ a b c d))
          1 2 3 4)
         ;; (apply (lambda (a b c d)
         ;;          (+ a b c d))
         ;;        (1 2 3 4)
         ))))

(define (bench proc)
  (let ((proc/lambda (assoc proc proc-list)))
    (if proc/lambda
        (let ((p (cdr proc/lambda)))
          ;; (apply p '())
          (p))
        (error "No procedure found" proc))))

(bench "lambda-add")

Compiler:

$ cyclone test-case.scm && ./test-case
Segmentation fault

Interpreter:

cyclone> Error: Unknown procedure type -- EXECUTE-APPLICATION: (lambda () ((lambda (a b c d) (+ a b c d)) 1 2 3 4))

Both errors are also present when using the apply form inside the procedure "lamba-add" body and also inside bench (presented as comments).

When I copy and paste the code on the interpreter, it runs:

cyclone> ((lambda () ((lambda (a b c d) (+ a b c d)) 1 2 3 4))))
10

Thanks!

arthurmaciel avatar Dec 13 '20 12:12 arthurmaciel

No problem @arthurmaciel and thanks for the report!

This is a really strange case. First of all, you can fix this by adding (import (scheme eval)) to the top of your example program.

What is happening is that the runtime can see that we are attempting to evaluate a lambda S-expression, and it calls eval to execute the code. Unfortunately if (scheme eval) is not loaded the call to eval will segfault. We now raise an error from our runtime instead:

Error: Unable to evaluate: : ((lambda () ((lambda (a b c d) (+ a b c d)) 1 2 3 4))) 
Call history, most recent first:
[1] bench.scm:bench
[2] scheme/write.sld:lib-init:schemewrite
[3] scheme/base.sld:make-parameter
[4] scheme/base.sld:lib-init:schemebase
[5] scheme/cyclone/common.sld:lib-init:schemecyclonecommon

The bizarre aspect about this issue is that if (scheme eval) is imported then everything works!

It would be nice if the compiler could detect that and add the import, but it would require detailed flow-control analysis and could be quite difficult to get right. In the meantime, it might be nice to add more information to this error message, and it would be even better if the error message included more details such as (p) in the call history. Let me leave this issue open for a bit and see if there is more we can do to improve error reporting here and clean up some of our "sharper edges" here.

justinethier avatar Dec 13 '20 16:12 justinethier

@justinethier, thanks! I agree it would be very useful if the compiler could report with more information. Sorry, but I am not able to propose any pull requests atm.

arthurmaciel avatar Jan 16 '21 22:01 arthurmaciel

@arthurmaciel No worries, the immediate work here is done. I felt that we could have a better error message here which is why I left the issue open. I am adding this to the roadmap, though it may make sense as a new ticket with larger scope (EG: improving error reports in general).

justinethier avatar Jan 17 '21 16:01 justinethier