From e714e376efd86ff3cde4a853a1be479743827ce4 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Thu, 30 Sep 2021 20:15:21 -0500 Subject: [PATCH 01/11] Add unreleased section in changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d249e1..7cb20f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Change Log All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). +## [Unreleased] + ## [0.1.192] - 2021-09-30 ### Added - An `::ffi/address` key to wrapper functions' metadata @@ -28,6 +30,7 @@ All notable changes to this project will be documented in this file. This change - Support for serializing and deserializing arbitrary Clojure functions - Support for serializing and deserializing arbitrary Clojure data structures +[Unreleased]: https://github.com/IGJoshua/coffi/compare/v0.1.192...HEAD [0.1.192]: https://github.com/IGJoshua/coffi/compare/v0.1.184...v0.1.192 [0.1.184]: https://github.com/IGJoshua/coffi/compare/v0.1.176...v0.1.184 [0.1.176]: https://github.com/IGJoshua/coffi/compare/v0.1.169...v0.1.176 From 66eefc7a13bddb2de8cb62cdc379c22aa58ef191 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Thu, 30 Sep 2021 20:15:58 -0500 Subject: [PATCH 02/11] Fixed bug with serialize-into and primitives --- CHANGELOG.md | 2 ++ src/clj/coffi/mem.clj | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cb20f4..3a0887b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). ## [Unreleased] +### Fixed +- Invalid implementation of serialize-into for primitive types ## [0.1.192] - 2021-09-30 ### Added diff --git a/src/clj/coffi/mem.clj b/src/clj/coffi/mem.clj index 69118f6..1aa6dcb 100644 --- a/src/clj/coffi/mem.clj +++ b/src/clj/coffi/mem.clj @@ -352,7 +352,7 @@ (defmethod serialize-into :default [obj type segment scope] (if-some [prim-layout (primitive-type type)] - (with-acquired [(segment-scope scope) scope] + (with-acquired [(segment-scope segment) scope] (serialize-into (serialize* obj type scope) prim-layout segment scope)) (throw (ex-info "Attempted to serialize an object to a type that has not been overriden" {:type type From 22343b851241124c0e444e676e39bc471b344f6d Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 1 Oct 2021 10:37:38 -0500 Subject: [PATCH 03/11] Fix a bug where void returns on upcalls crash the JVM --- CHANGELOG.md | 1 + src/clj/coffi/mem.clj | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a0887b..e663a6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This change ## [Unreleased] ### Fixed +- Void return types on upcalls crash the JVM - Invalid implementation of serialize-into for primitive types ## [0.1.192] - 2021-09-30 diff --git a/src/clj/coffi/mem.clj b/src/clj/coffi/mem.clj index 1aa6dcb..ff0ce26 100644 --- a/src/clj/coffi/mem.clj +++ b/src/clj/coffi/mem.clj @@ -315,7 +315,8 @@ (defmethod serialize* :default [obj type _scope] (if-let [prim (primitive-type type)] - ((primitive-cast prim) obj) + (when-not (= ::void prim) + ((primitive-cast prim) obj)) (throw (ex-info "Attempted to serialize a non-primitive type with primitive methods" {:type type :object obj})))) From b98fdc05ea6562b5b34cbb06b39e0c511b108d7e Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 1 Oct 2021 10:38:42 -0500 Subject: [PATCH 04/11] Fix primitive arguments to upcalls not compiling --- CHANGELOG.md | 1 + src/clj/coffi/ffi.clj | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e663a6b..07295f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This change ## [Unreleased] ### Fixed +- Primitive-serializing types fail to load as arguments to upcall functions - Void return types on upcalls crash the JVM - Invalid implementation of serialize-into for primitive types diff --git a/src/clj/coffi/ffi.clj b/src/clj/coffi/ffi.clj index 6fa1e32..66f5772 100644 --- a/src/clj/coffi/ffi.clj +++ b/src/clj/coffi/ffi.clj @@ -320,7 +320,7 @@ [:getfield :this "upcall_ifn" IFn] (map-indexed (fn [idx arg] - [[(load-instructions arg :aload) (inc idx)] + [[(load-instructions (mem/primitive-type arg) :aload) (inc idx)] (to-object-asm arg)]) arg-types) [:invokeinterface IFn "invoke" (repeat (inc (count arg-types)) Object)] From 673a6532003672d63c5b27edbfca3efd959aa813 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 1 Oct 2021 10:39:32 -0500 Subject: [PATCH 05/11] Fix bug with primitive-serializing arguments to downcalls not compiling --- CHANGELOG.md | 1 + src/clj/coffi/ffi.clj | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07295f6..717ee20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This change ## [Unreleased] ### Fixed +- Primitive-serializing types fail to compile as arguments to downcall handles - Primitive-serializing types fail to load as arguments to upcall functions - Void return types on upcalls crash the JVM - Invalid implementation of serialize-into for primitive types diff --git a/src/clj/coffi/ffi.clj b/src/clj/coffi/ffi.clj index 66f5772..75c6b6a 100644 --- a/src/clj/coffi/ffi.clj +++ b/src/clj/coffi/ffi.clj @@ -108,9 +108,10 @@ (defn- insn-layout "Gets the type keyword or class for referring to the type in bytecode." [type] - (if (some-> (mem/primitive-type type) (not= ::mem/pointer)) - (keyword (name type)) - (mem/java-layout type))) + (when-some [prim (mem/primitive-type type)] + (if (not= prim ::mem/pointer) + (keyword (name prim)) + (mem/java-layout type)))) (def ^:private unbox-fn-for-type "Map from type name to the name of its unboxing function." From 81002955670420b2340d601607c7d7aae24fc0d3 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 1 Oct 2021 12:03:25 -0500 Subject: [PATCH 06/11] Ensure serializing nil as a pointer returns null --- src/clj/coffi/mem.clj | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/clj/coffi/mem.clj b/src/clj/coffi/mem.clj index ff0ce26..c885226 100644 --- a/src/clj/coffi/mem.clj +++ b/src/clj/coffi/mem.clj @@ -323,13 +323,14 @@ (defmethod serialize* ::pointer [obj type scope] - (when-not (null? obj) + (if-not (null? obj) (if (sequential? type) (with-acquired [scope] (let [segment (alloc-instance (second type) scope)] (serialize-into obj (second type) segment scope) (address-of segment))) - obj))) + obj) + (MemoryAddress/NULL))) (defmulti serialize-into "Writes a serialized version of the `obj` to the given `segment`. From 7d621f82dbe4c2a923c0aafb5337b3b0a98974d8 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 1 Oct 2021 12:03:39 -0500 Subject: [PATCH 07/11] Consider nil as `null?` --- CHANGELOG.md | 1 + src/clj/coffi/mem.clj | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 717ee20..1a6a7ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This change ## [Unreleased] ### Fixed +- `nil` was not considered a null pointer - Primitive-serializing types fail to compile as arguments to downcall handles - Primitive-serializing types fail to load as arguments to upcall functions - Void return types on upcalls crash the JVM diff --git a/src/clj/coffi/mem.clj b/src/clj/coffi/mem.clj index c885226..fb050c3 100644 --- a/src/clj/coffi/mem.clj +++ b/src/clj/coffi/mem.clj @@ -128,7 +128,7 @@ (defn null? "Checks if a memory address is null." [addr] - (.equals (MemoryAddress/NULL) addr)) + (or (.equals (MemoryAddress/NULL) addr) (not addr))) (defn slice-global "Gets a slice of the global address space. From 88b2a72f07132d572bd1d860bccad25364cd1a65 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Mon, 4 Oct 2021 19:20:47 -0500 Subject: [PATCH 08/11] Add an address predicate --- CHANGELOG.md | 3 +++ src/clj/coffi/mem.clj | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a6a7ba..581ea51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). ## [Unreleased] +### Added +- An `address?` predicate + ### Fixed - `nil` was not considered a null pointer - Primitive-serializing types fail to compile as arguments to downcall handles diff --git a/src/clj/coffi/mem.clj b/src/clj/coffi/mem.clj index fb050c3..a237442 100644 --- a/src/clj/coffi/mem.clj +++ b/src/clj/coffi/mem.clj @@ -130,6 +130,13 @@ [addr] (or (.equals (MemoryAddress/NULL) addr) (not addr))) +(defn address? + "Checks if an object is a memory address. + + `nil` is considered an address." + [addr] + (or (nil? addr) (instance? MemoryAddress addr))) + (defn slice-global "Gets a slice of the global address space. From 96f3153c5f8a9cade712b57d640903f813c93ec1 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Mon, 4 Oct 2021 19:20:55 -0500 Subject: [PATCH 09/11] Fix bug where compound types were not permitted as return values in defcfn --- CHANGELOG.md | 1 + src/clj/coffi/ffi.clj | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 581ea51..d57c584 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. This change - An `address?` predicate ### Fixed +- Compound types were not allowed as return types in `defcfn` - `nil` was not considered a null pointer - Primitive-serializing types fail to compile as arguments to downcall handles - Primitive-serializing types fail to load as arguments to upcall functions diff --git a/src/clj/coffi/ffi.clj b/src/clj/coffi/ffi.clj index 75c6b6a..24ba6db 100644 --- a/src/clj/coffi/ffi.clj +++ b/src/clj/coffi/ffi.clj @@ -504,7 +504,7 @@ (s/or :string string? :symbol simple-symbol?)) :native-arglist (s/coll-of ::mem/type :kind vector?) - :return-type qualified-keyword? + :return-type ::mem/type :wrapper (s/? (s/cat :native-fn simple-symbol? From 014901233cc3009a4b0b2851f5ad06417f09afd6 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Wed, 6 Oct 2021 08:53:02 -0500 Subject: [PATCH 10/11] Fix bug where compound types in arglist metadata failed --- CHANGELOG.md | 1 + src/clj/coffi/ffi.clj | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d57c584..87a0c43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. This change - An `address?` predicate ### Fixed +- Compound types caused problems in arglists meta on expansion of `defcfn` - Compound types were not allowed as return types in `defcfn` - `nil` was not considered a null pointer - Primitive-serializing types fail to compile as arguments to downcall handles diff --git a/src/clj/coffi/ffi.clj b/src/clj/coffi/ffi.clj index 24ba6db..bfb497f 100644 --- a/src/clj/coffi/ffi.clj +++ b/src/clj/coffi/ffi.clj @@ -582,7 +582,11 @@ (or old-list (seq arglists) (list - (mapv (comp symbol name) + (mapv (fn [type] + (-> (cond-> type + (vector? type) first) + name + symbol)) (:native-arglist args))))))) (assoc (:attr-map args) ::address address))) From 6b6899b818feee70fb19aae6f6254dc714a1a5f7 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Wed, 6 Oct 2021 11:38:39 -0500 Subject: [PATCH 11/11] Update release tags --- CHANGELOG.md | 4 ++-- README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87a0c43..a35b1e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Change Log All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). -## [Unreleased] +## [0.1.205] ### Added - An `address?` predicate @@ -41,7 +41,7 @@ All notable changes to this project will be documented in this file. This change - Support for serializing and deserializing arbitrary Clojure functions - Support for serializing and deserializing arbitrary Clojure data structures -[Unreleased]: https://github.com/IGJoshua/coffi/compare/v0.1.192...HEAD +[0.1.205]: https://github.com/IGJoshua/coffi/compare/v0.1.192...v0.1.205 [0.1.192]: https://github.com/IGJoshua/coffi/compare/v0.1.184...v0.1.192 [0.1.184]: https://github.com/IGJoshua/coffi/compare/v0.1.176...v0.1.184 [0.1.176]: https://github.com/IGJoshua/coffi/compare/v0.1.169...v0.1.176 diff --git a/README.md b/README.md index c5ac49a..49ff808 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ This library is available on Clojars. Add one of the following entries to the `:deps` key of your `deps.edn`: ```clojure -org.suskalo/coffi {:mvn/version "0.1.192"} -io.github.IGJoshua/coffi {:git/tag "v0.1.192" :git/sha "9d65f47"} +org.suskalo/coffi {:mvn/version "0.1.205"} +io.github.IGJoshua/coffi {:git/tag "v0.1.205" :git/sha "0149012"} ``` If you use this library as a git dependency, you will need to prepare the