bug in mmap() and mremap() return value - and a fix

Massimiliano Ghilardi massimiliano.ghilardi at gmail.com
Mon Jan 20 18:06:51 UTC 2014


On 01/09/14 02:08, Luís Oliveira wrote:
> On Thu, Jan 9, 2014 at 12:15 AM, Massimiliano Ghilardi
> <massimiliano.ghilardi at gmail.com> wrote:
>> 2) Thinking that (or actually defining) MAP_FAILED = -1 is misleading.
>>     MAP_FAILED is (void *)-1.
>
> Your patch looks good. Perhaps we could teach cffi-grovel how to
> grovel pointers, then we'd be able to ditch the *map-failed-pointer*
> bits. I've attached an untested patch that does that. Does using
> (constant ... :type pointer) for the MAP_* constants solve the problem
> you found?
>
> Cheers,
>

Hello Luís,

as highlighted by you and Stelian Ionescu, there is room to improve CFFI 
with regards to pointers and automatic type discovery.

With regard to Osicat: do I need to do something more for the 
mmap()/mremap() patch (attached) to be accepted and integrated?

Best wishes,

Massimiliano Ghilardi
-------------- next part --------------
diff --git a/posix/early.lisp b/posix/early.lisp
index 88167d7..4f5af24 100644
--- a/posix/early.lisp
+++ b/posix/early.lisp
@@ -119,6 +119,18 @@
    (object :initarg :object :reader errno-object)
    (function-name :initarg :function-name :reader function-name)))
 
+;; NOTE: this is an ugly type-punning. An alternative is to compute
+;; this value from (cffi:foreign-type-size :uintptr), but it requires
+;; also knowing how many bits are in a byte.
+(defconstant +most-positive-uintptr+ (with-foreign-object (p :intptr)
+                                       (setf (mem-ref p :intptr) -1)
+                                       (mem-ref p :uintptr)))
+
+;; on some systems, map-failed is groveled as -1 :(
+#-windows
+(defvar *map-failed-pointer* (make-pointer (logand map-failed +most-positive-uintptr+)))
+
+
 ;;; FIXME: undocumented in cffi-grovel.
 (defun make-from-pointer-function-name (type-name)
   (format-symbol t "~A-~A-~A-~A" '#:make type-name '#:from '#:pointer))
diff --git a/posix/wrappers.lisp b/posix/wrappers.lisp
index ae49ad8..dfc2a15 100644
--- a/posix/wrappers.lisp
+++ b/posix/wrappers.lisp
@@ -60,9 +60,8 @@
 
 #-windows
 (defwrapper "mmap" ("void*" (errno-wrapper
-                             :long
-                             :error-predicate (lambda (p) (= p map-failed))
-                             :return-filter make-pointer))
+                             :pointer
+                             :error-predicate (lambda (p) (pointer-eq p *map-failed-pointer*))))
   (start :pointer)
   (length ("size_t" size))
   (prot :int)
@@ -72,9 +71,8 @@
 
 #+linux
 (defwrapper "mremap" ("void*" (errno-wrapper
-                               :long
-                               :error-predicate (lambda (p) (= p map-failed))
-                               :return-filter make-pointer))
+                               :pointer
+                               :error-predicate (lambda (p) (pointer-eq p *map-failed-pointer*))))
   (old-address :pointer)
   (old-size ("size_t" size))
   (new-size ("size_t" size))


More information about the osicat-devel mailing list