On Wed, Nov 08, 2017 at 04:01:18PM +0100, lemonboy wrote:
> Hello hackers,
> here's the patch for #1424, I've prepared them for both C4 and C5.

Nice, here's a signed-off copy that also updates NEWS (as it's an
important fix, IMO)

Cheers,
Peter
From e61a230e87751f23b4de3dea14278bd5fc2a637b Mon Sep 17 00:00:00 2001
From: LemonBoy <[email protected]>
Date: Wed, 8 Nov 2017 15:36:29 +0100
Subject: [PATCH] Correctly parse FFI types wrapped in `const' forms

Keep parsing the cdr when calculating the total size of the type.

Signed-off-by: Peter Bex <[email protected]>
---
 NEWS                     | 4 ++++
 support.scm              | 2 ++
 tests/compiler-tests.scm | 9 +++++++++
 3 files changed, 15 insertions(+)

diff --git a/NEWS b/NEWS
index c849aede..052093cc 100644
--- a/NEWS
+++ b/NEWS
@@ -159,6 +159,10 @@
   - The scrutinizer no longer uses 'fixnum as the type for fixnums
     that might not fit into a fixnum on 32-bit architectures.
 
+- Foreign function interface
+  - Correctly calculate memory requirements of Scheme objects produced
+    from foreign types with "const" qualifiers, avoiding memory
+    corruption (#1424, thanks to Vasilij Schneidermann and Lemonboy)
 
 4.12.0
 
diff --git a/support.scm b/support.scm
index 905f7ae8..8a489217 100644
--- a/support.scm
+++ b/support.scm
@@ -1183,6 +1183,7 @@
 	       (case (car t)
 		 ((ref nonnull-pointer pointer c-pointer nonnull-c-pointer function instance instance-ref nonnull-instance) 
 		  (words->bytes 3) )
+                 ((const) (next (cadr t)))
 		 ((enum) (words->bytes 6)) ; 1 bignum digit on 32-bit (overallocs on 64-bit)
 		 (else (err t))))
 	      (else (err t))))))
@@ -1211,6 +1212,7 @@
 		 ((ref nonnull-pointer pointer c-pointer nonnull-c-pointer function
 		       scheme-pointer nonnull-scheme-pointer enum)
 		  (words->bytes 1))
+                 ((const) (next (cadr t)))
 		 (else (err t)) ) )
 	      (else (err t)) ) ) ) )
    (lambda () (quit-compiling "foreign type `~S' refers to itself" type)) ) )
diff --git a/tests/compiler-tests.scm b/tests/compiler-tests.scm
index c2f0c068..61eabd66 100644
--- a/tests/compiler-tests.scm
+++ b/tests/compiler-tests.scm
@@ -426,3 +426,12 @@
  (define (also-do-it arg)
    (get-value arg 3))
 )
+
+; let-location with const-wrapped type
+(let-location ((foo (const c-string) "boo"))
+  (assert (equal? foo "boo")))
+
+; #1424: a foreign lambda with const return type was wrongly rejected
+(let ((v0 ((foreign-lambda* c-string () "C_return(\"str\");")))
+      (v1 ((foreign-lambda* (const c-string) () "C_return(\"str\");"))))
+  (assert (equal? v0 v1)))
-- 
2.11.0

From 113f07d45cee81b24989f31117cddc34ba445935 Mon Sep 17 00:00:00 2001
From: LemonBoy <[email protected]>
Date: Wed, 8 Nov 2017 15:56:21 +0100
Subject: [PATCH] Correctly parse FFI types wrapped in `const' forms

Keep parsing the cdr when calculating the total size of the type.

Signed-off-by: Peter Bex <[email protected]>
---
 NEWS                     | 4 ++++
 support.scm              | 2 ++
 tests/compiler-tests.scm | 9 +++++++++
 3 files changed, 15 insertions(+)

diff --git a/NEWS b/NEWS
index 397fd532..6b150f0f 100644
--- a/NEWS
+++ b/NEWS
@@ -40,6 +40,10 @@
   - The scrutinizer no longer uses 'fixnum as the type for fixnums
     that might not fit into a fixnum on 32-bit architectures.
 
+- Foreign function interface
+  - Correctly calculate memory requirements of Scheme objects produced
+    from foreign types with "const" qualifiers, avoiding memory
+    corruption (#1424, thanks to Vasilij Schneidermann and Lemonboy)
 
 4.12.0
 
diff --git a/support.scm b/support.scm
index fcf0b0c1..79065def 100644
--- a/support.scm
+++ b/support.scm
@@ -1198,6 +1198,7 @@
 	       (case (car t)
 		 [(ref nonnull-pointer pointer c-pointer nonnull-c-pointer function instance instance-ref nonnull-instance) 
 		  (words->bytes 3) ]
+                 [(const) (next (cadr t))]
 		 [else 0] ) ]
 	      [else 0] ) ) ) )
    (lambda () (quit "foreign type `~S' refers to itself" type)) ) )
@@ -1226,6 +1227,7 @@
 		 [(ref nonnull-pointer pointer c-pointer nonnull-c-pointer function
 		       scheme-pointer nonnull-scheme-pointer)
 		  (words->bytes 1)]
+                 [(const) (next (cadr t))]
 		 [else (err t)] ) ]
 	      [else (err t)] ) ) ) )
    (lambda () (quit "foreign type `~S' refers to itself" type)) ) )
diff --git a/tests/compiler-tests.scm b/tests/compiler-tests.scm
index 53de9711..4c2bf806 100644
--- a/tests/compiler-tests.scm
+++ b/tests/compiler-tests.scm
@@ -319,3 +319,12 @@
  (define (also-do-it arg)
    (get-value arg 3))
 )
+
+; let-location with const-wrapped type
+(let-location ((foo (const c-string) "boo"))
+  (assert (equal? foo "boo")))
+
+; #1424: a foreign lambda with const return type was wrongly rejected
+(let ((v0 ((foreign-lambda* c-string () "C_return(\"str\");")))
+      (v1 ((foreign-lambda* (const c-string) () "C_return(\"str\");"))))
+  (assert (equal? v0 v1)))
-- 
2.11.0

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Chicken-hackers mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to