Hi all,

I decided to investigate #1377, and while I wasn't able to reproduce it
yet, on 32 bits GNU Hurd I got a segfault due to there being no result
size calculation for "enum" foreign types.  I discovered that if the type
is unknown, the result size is assumed to be zero, so the generate C stub
function receives C_SCHEME_UNDEFINED as a "buffer" (see core.scm:1815).

This is obviously not big enough to hold a bignum that might be returned
from an enum on 32-bit systems, thus we're scribbling into unmapped
memory.

This *might* be the fix for #1377, but I don't think it is, since it
should print "Good, unrepresentable C strings cause errors", before
testing all the FFI conversions.  Nevertheless, the attached patch is
pretty crucial I think.

The patch converts the assumption of 0 size for unknown foreign types
in estimate-foreign-result-size to an error, much like the code in
estimate-foreign-result-location-size already did.  I don't know why
they weren't the same, so beware that this patch may break things.

And finally, this patch also converts the foreign type "(enum x)" into
an "integer" scrutiny type.  Before it would just use "number", but
we know it must be an integer rather than, say, a ratnum, cplxnum or
flonum, so let's use that knowledge.

Cheers,
Peter
From a7bd09a7b014547a9c85653adfc90efc4d1a0af1 Mon Sep 17 00:00:00 2001
From: Peter Bex <pe...@more-magic.net>
Date: Mon, 19 Jun 2017 15:01:14 +0200
Subject: [PATCH] Add foreign result size computation for enums and enum
 locations

Also, error out instead of assuming 0 size when encountering an
unknown type, because that is too dangerous an assumption.

Finally, we improve the scrutiny type of "enum" to be "integer" rather
than the nondescript "number".
---
 support.scm | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/support.scm b/support.scm
index 233ad961..a9d67ed9 100644
--- a/support.scm
+++ b/support.scm
@@ -1144,6 +1144,8 @@
 ;;; Compute foreign result size:
 
 (define (estimate-foreign-result-size type)
+  (define (err t)
+    (quit-compiling "cannot compute size for unknown foreign type `~S' result" type))
   (follow-without-loop
    type
    (lambda (t next)
@@ -1168,8 +1170,9 @@
 	       (case (car t)
 		 ((ref nonnull-pointer pointer c-pointer nonnull-c-pointer function instance instance-ref nonnull-instance) 
 		  (words->bytes 3) )
-		 (else 0) ) )
-	      (else 0) ) ) ) )
+		 ((enum) (words->bytes 6)) ; 1 bignum digit on 32-bit (overallocs on 64-bit)
+		 (else (err t)) ) )
+	      (else (err t)) ) ) ) )
    (lambda () (quit-compiling "foreign type `~S' refers to itself" type)) ) )
 
 (define (estimate-foreign-result-location-size type) ; Used only in compiler.scm
@@ -1179,8 +1182,8 @@
    type
    (lambda (t next)
      (case t
-       ((char int short bool unsigned-short unsigned-char unsigned-int long unsigned-long byte unsigned-byte
-	      c-pointer nonnull-c-pointer unsigned-integer integer float c-string symbol
+       ((char int short bool unsigned-short unsigned-char unsigned-int long unsigned-long byte
+	      unsigned-byte c-pointer nonnull-c-pointer unsigned-integer integer float c-string symbol
 	      scheme-pointer nonnull-scheme-pointer int32 unsigned-int32 integer32 unsigned-integer32
               unsigned-c-string unsigned-c-string* nonnull-unsigned-c-string*
 	      nonnull-c-string c-string* nonnull-c-string* c-string-list c-string-list*)
@@ -1193,7 +1196,7 @@
 	      ((pair? t)
 	       (case (car t)
 		 ((ref nonnull-pointer pointer c-pointer nonnull-c-pointer function
-		       scheme-pointer nonnull-scheme-pointer)
+		       scheme-pointer nonnull-scheme-pointer enum)
 		  (words->bytes 1))
 		 (else (err t)) ) )
 	      (else (err t)) ) ) ) )
@@ -1293,7 +1296,7 @@
 		((ref pointer function c-pointer)
 		 '(or boolean pointer locative))
 		((const) (foreign-type->scrutiny-type (cadr t) mode))
-		((enum) 'number)
+		((enum) 'integer)
 		((nonnull-pointer nonnull-c-pointer) 'pointer)
 		(else '*)))
 	     (else '*))))))
-- 
2.11.0

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to