# Help with FFI errors (for #lang clingo integration)

**URL:** <https://racket.discourse.group/t/help-with-ffi-errors-for-lang-clingo-integration/2585>\
**Category:** Questions & Answers\
**Tags:** ffi\
**Created:** [December 10, 2023, 7:35am UTC](https://racket.discourse.group/t/help-with-ffi-errors-for-lang-clingo-integration/2585 "2023-12-10T07:35:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiranandcode](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/kiranandcode/32/1504_2.png) [@kiranandcode](https://racket.discourse.group/u/kiranandcode)\
**Post date:** [December 10, 2023, 7:35am UTC](https://racket.discourse.group/t/help-with-ffi-errors-for-lang-clingo-integration/2585/1 "2023-12-10T07:35:03Z")

</div>

Heya!! This is a long shot, but I figured I'd post here to see if anyone could possibly provide some advice on an FFI integration that I'm currently building and running into some really mind-bending errors.

The repository is here: [GitHub - Gopiandcode/clingo-lang: #lang clingo for Racket (WIP)](https://github.com/Gopiandcode/clingo-lang)

For context, this package implements a binding to the libclingo ASP solver. The file [`unsafe.rkt`](https://github.com/Gopiandcode/clingo-lang/blob/main/unsafe.rkt) lists my fairly exhaustive FFI binding for all of the functions from [`clingo.h`](https://github.com/potassco/clingo/blob/master/libclingo/clingo.h).

The file [`main.rkt`](https://github.com/Gopiandcode/clingo-lang/blob/main/main.rkt) lists my initial experiments with using the FFI API to make queries from Racket.

The core of my program is:

```scheme
;; create a context
(define ctrl (clingo-control-new '[] #f #f 20))
(configure-to-enumerate-all-models ctrl)
;; add facts to database
(clingo-control-add ctrl "base" '[] "a :- not b. b :- not a. - a :- b.")
(clingo-control-ground ctrl `[,(make-clingo-part "base" #f 0)] #f #f)

;; ask the solver to solve this database
(define solve-handle (clingo-control-solve ctrl 'clingo-solve-mode-yield '[] #f #f))

;; loop over solutions and print them
(define (loop)
  (clingo-solve-handle-resume solve-handle)
  (define model (clingo-solve-handle-model solve-handle))
  (when model
    (print-model model)
    (loop)))
(loop)

;; clean up the solver handle
(define solve-result (clingo-solve-handle-get solve-handle))
(println solve-result)
(clingo-solve-handle-close solve-handle)

```

This actually works pretty well, and I can send queries, and print out the results (using a function from the api to convert solutions to strings) and they match with what I expect.

Inside `print-model`, I then started writing some code to translate from clingo's internal representation back to a more racket friendly encoding, via a function `symbol->term`, where `term` is defined as:

```scheme
(define term?
  (flat-rec-contract term?
   (or/c
    infinum? ;; #inf
    supremum? ;; #sup
    number? ;; 1
    string? ;; "hello"
    symbol? ;; a
    (list/c '-
            (or/c
             symbol?
             (cons/c symbol? (listof term?)))) ;; (- a) or (- (f x y))
    (cons/c symbol? (listof term?))))) ;; (f x y)

```

My problem is that my function `symbol->term` is not behaving properly, and I'm getting really weird results.

```scheme
(define/contract (symbol->term sym)
  (-> any/c term?)
  (define symbol-type (clingo-symbol-type sym))
  (match symbol-type
    ...
    ['clingo-symbol-type-function
     (define is-positive (clingo-symbol-is-positive sym))
     (define is-negative (clingo-symbol-is-negative sym))
     (define f (string->symbol (clingo-symbol-name sym)))
     (define args (map symbol->term (clingo-symbol-arguments sym)))
     (define expr (if (null? args) f (cons f args)))
     (if is-negative
         (list '- expr)
         expr)]))

```

Above I have the relevant snippet from symbol-\>term; the errors relate to the outputs of the function `clingo-symbol-is-negative`, and `clingo-symbol-is-positive`, which seem to sometimes return the "wrong" results (sometimes they both return `#true`, which is definitely wrong.

I double checked my bindings, and I can't think there's something wrong [there](https://github.com/Gopiandcode/clingo-lang/blob/main/unsafe.rkt#L594):

```scheme
(define-clingo clingo-symbol-is-positive
  (_fun _clingo-symbol (positive : (_ptr o _bool)) -> (res : _bool) ->
        (if res positive (raise-clingo-error))))

(define-clingo clingo-symbol-is-negative
  (_fun _clingo-symbol (negative : (_ptr o _bool)) -> (res : _bool) ->
        (if res negative (raise-clingo-error))))

```

I've looked at the [source](https://github.com/potassco/clingo/blob/ec2c706656d7e8751b666aa31199c598c6487af6/libclingo/src/control.cc#L339) of the corresponding functions in the c-code, and they definitely should never return the same results at the very least:

```scheme
extern "C" bool clingo_symbol_is_negative(clingo_symbol_t val, bool *sign) {
    GRINGO_CLINGO_TRY {
        clingo_expect(Symbol(val).type() == SymbolType::Fun);
        *sign = Symbol(val).sign();
    } GRINGO_CLINGO_CATCH;
}

extern "C" bool clingo_symbol_is_positive(clingo_symbol_t val, bool *sign) {
    GRINGO_CLINGO_TRY {
        clingo_expect(Symbol(val).type() == SymbolType::Fun);
        *sign = !Symbol(val).sign();
    } GRINGO_CLINGO_CATCH;
}

```

I assume I've wrapped something wrong, and now am getting some kind of stack corruption 😱 😱 , but for the life of me, I don't know where... 😭

Could anyone give some advice as to what I might be doing wrong? or how could I go about debugging this issue?

---

<div class="post-metadata">

**Author:** ![kiranandcode](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/kiranandcode/32/1504_2.png) [@kiranandcode](https://racket.discourse.group/u/kiranandcode)\
**Post date:** [December 10, 2023, 7:46am UTC](https://racket.discourse.group/t/help-with-ffi-errors-for-lang-clingo-integration/2585/2 "2023-12-10T07:46:15Z")

</div>

Hm... If I call the same functions multiple times in a row, I end up getting different results sometimes:

```scheme
     (println "start")
     (define is-positive (clingo-symbol-is-positive sym))
     (define is-negative (clingo-symbol-is-negative sym))
     (println [list is-positive is-negative])
     (set! is-positive (clingo-symbol-is-positive sym))
     (set! is-negative (clingo-symbol-is-negative sym))
     (println [list is-positive is-negative])
     (println "done")

```

which outputs:

```scheme
"start"
'(#t #t)
'(#t #f)
"done"

```

which seems to suggest that the error isn't from the `clingo-symbol-is-negative` function side? because if it were stack corruption, I would have expected one of the other C-ffi functions to accidentally end up overwriting the values for is-positive and is-negative. Maybe I've messed something up and the garbage collector is causing this??

---

<div class="post-metadata">

**Author:** ![kiranandcode](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/kiranandcode/32/1504_2.png) [@kiranandcode](https://racket.discourse.group/u/kiranandcode)\
**Post date:** [December 10, 2023, 7:54am UTC](https://racket.discourse.group/t/help-with-ffi-errors-for-lang-clingo-integration/2585/3 "2023-12-10T07:54:38Z")

</div>

hmmm.... changing `(_ptr o _bool)` to `(_box _bool)` seems to make the results consistent, but I'm pretty sure I haven't actually solved the problem here, just kicked it down the line a bit...

```scheme
(define is-negative-result-box (box #false))
(define-clingo clingo-symbol-is-negative-unsafe
  (_fun _clingo-symbol (negative : (_box _bool)) -> (res : _bool) ->
        (if res negative (raise-clingo-error)))
  #:c-id clingo_symbol_is_negative)
(define (clingo-symbol-is-negative symbol)
  (clingo-symbol-is-negative-unsafe symbol is-negative-result-box)
  (unbox is-negative-result-box))

```

---

<div class="post-metadata">

**Author:** ![mflatt](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/mflatt/32/6_2.png) [@mflatt](https://racket.discourse.group/u/mflatt)\
**Post date:** [December 10, 2023, 1:48pm UTC](https://racket.discourse.group/t/help-with-ffi-errors-for-lang-clingo-integration/2585/4 "2023-12-10T13:48:45Z")

</div>

Does it help to use `_stdbool` instead of `_bool`?

---

<div class="post-metadata">

**Author:** ![kiranandcode](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/kiranandcode/32/1504_2.png) [@kiranandcode](https://racket.discourse.group/u/kiranandcode)\
**Post date:** [December 10, 2023, 2:01pm UTC](https://racket.discourse.group/t/help-with-ffi-errors-for-lang-clingo-integration/2585/5 "2023-12-10T14:01:43Z")

</div>

Huh. Yeah, it does? (well, I return to getting consistent correct results when I changed to use `(_ptr o _stdbool)`. Do you know why this was happening? What's the explanation? I would have thought the size of \_bool and \_stdbool would be the same?

---

<div class="post-metadata">

**Author:** ![mflatt](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/mflatt/32/6_2.png) [@mflatt](https://racket.discourse.group/u/mflatt)\
**Post date:** [December 10, 2023, 2:20pm UTC](https://racket.discourse.group/t/help-with-ffi-errors-for-lang-clingo-integration/2585/6 "2023-12-10T14:20:19Z")

</div>

The size of `_stdbool` is 1 byte (on all systems I know about), and the size of `_bool` is 4 bytes (everywhere that Racket runs).

See also the [\_stdbool docs](https://docs.racket-lang.org/foreign/Other_Atomic_Types.html#%28def._%28%28quote._~23~25foreign%29.__stdbool%29%29).

---

<div class="post-metadata">

**Author:** ![kiranandcode](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/kiranandcode/32/1504_2.png) [@kiranandcode](https://racket.discourse.group/u/kiranandcode)\
**Post date:** [December 10, 2023, 2:37pm UTC](https://racket.discourse.group/t/help-with-ffi-errors-for-lang-clingo-integration/2585/7 "2023-12-10T14:37:30Z")

</div>

Ah, gotcha; I figured it might be due to a size difference.

Is there a good rule of thumb when to use which? From the docs, I guess it mentions that \_bool is for pre-99 conventions, so should I prefer \_stdbool instead of \_bool?

The library I'm binding to is C++ based, could I take that as an indicator that I should use \_stdbool instead of \_bool (I checked the sources and it doesn't `#include <stdbool.h>`, but I assume this can be assumed of c++ code?)

---

<div class="post-metadata">

**Author:** ![mflatt](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/mflatt/32/6_2.png) [@mflatt](https://racket.discourse.group/u/mflatt)\
**Post date:** [December 10, 2023, 2:56pm UTC](https://racket.discourse.group/t/help-with-ffi-errors-for-lang-clingo-integration/2585/8 "2023-12-10T14:56:56Z")

</div>

Yes, if a library uses the `bool` type in modern C or C++, then that's `_stdbool`.

Otherwise, the library probably says `int` or it defines something like `some_lib_bool_t` as an alias, typically for `int`. For example, `gboolean` (in GLib) is defined as `int`.
