# FFI Program not working on Mac

**URL:** <https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400>\
**Category:** General\
**Created:** [October 18, 2023, 4:45pm UTC](https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400 "2023-10-18T16:45:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SamPhillips](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samphillips/32/15_2.png) [@SamPhillips](https://racket.discourse.group/u/SamPhillips)\
**Post date:** [October 18, 2023, 4:45pm UTC](https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400/1 "2023-10-18T16:45:43Z")

</div>

Hi,

This program works on Linux and fetches the web page, but on Mac it fails. Doing some small sleuthing it appears to not be able to parse the URL, but I do not understand how setting the URL is failing.

Mac M1 Monterey  
Linux x86\_64 Ubuntu 22.04

Any ideas?

> <https://gist.github.com/samdphillips/26f78e17da72fff6d69b272b71bae013>

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [October 18, 2023, 5:17pm UTC](https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400/2 "2023-10-18T17:17:48Z")

</div>

Sorry to be a bit of a pill... would it be possible to minimize this, so it doesn't define syntax or include other extra stuff? My first guess is that it has to do with the string type that gets passed, but my 5-minute timer just went off 🙂

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [October 18, 2023, 7:11pm UTC](https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400/3 "2023-10-18T19:11:05Z")

</div>

I see  
`0 0 0 0`  
as the output on macOS.

What do you see on Windows?

---

<div class="post-metadata">

**Author:** ![SamPhillips](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samphillips/32/15_2.png) [@SamPhillips](https://racket.discourse.group/u/SamPhillips)\
**Post date:** [October 18, 2023, 7:19pm UTC](https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400/4 "2023-10-18T19:19:56Z")

</div>

I don't have a Windows system to check on.

The curl library should also be sending data to the process stdout and stderr, so there may be some side-effects that you cannot see if you run it in DrRacket. That does look like it is working on your system. On my Mac I get `0 0 0 3` in DrRacket and from the command line I get

```scheme
0
0
0
* Closing connection -1
3

```

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [October 18, 2023, 7:26pm UTC](https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400/5 "2023-10-18T19:26:43Z")

</div>

In the terminal I see:

```scheme
0
0
0
* Trying 142.250.186.36:443...
* Connected to www.google.com (142.250.186.36) port 443 (#0)
* ALPN: offers h2
* ALPN: offers http/1.1
* CAfile: /etc/ssl/cert.pem
* CApath: none
* SSL connection using TLSv1.3 / AEAD-AES256-GCM-SHA384
...

```

So it seems it fetches the page fine.  
This is on macOS Ventura.  
I have Apples Developer CommandLineTools installed (under additional downloads).  
Not sure whether it makes a difference.

> **[Downloads and Resources - Xcode - Apple Developer](https://developer.apple.com/xcode/resources/)**
>
> Find Xcode downloads, tools, documentation, tutorials, videos, and more.

---

<div class="post-metadata">

**Author:** ![SamPhillips](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samphillips/32/15_2.png) [@SamPhillips](https://racket.discourse.group/u/SamPhillips)\
**Post date:** [October 18, 2023, 7:30pm UTC](https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400/6 "2023-10-18T19:30:56Z")

</div>

That was the minimized example :D.

Here it is without `define-syntax`.

> <https://gist.github.com/samdphillips/cb18c3975ef47c9e0584e7767ef1719b>

---

<div class="post-metadata">

**Author:** ![SamPhillips](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samphillips/32/15_2.png) [@SamPhillips](https://racket.discourse.group/u/SamPhillips)\
**Post date:** [October 18, 2023, 7:37pm UTC](https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400/7 "2023-10-18T19:37:18Z")

</div>

I have command line tools as well. I've also tried the Homebrew version of `libcurl` and it has the same error.

---

<div class="post-metadata">

**Author:** ![shhyou](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shhyou](https://racket.discourse.group/u/shhyou)\
**Post date:** [October 19, 2023, 2:19am UTC](https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400/8 "2023-10-19T02:19:09Z")

</div>

I tested this program with:

- Racket CS, Mac Monterey, x86: works
- Racket CS, Mac Monterey, M1 (ARM): doesn't work

Maybe it has to do with MacOS differences between different architectures?

---

<div class="post-metadata">

**Author:** ![shhyou](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shhyou](https://racket.discourse.group/u/shhyou)\
**Post date:** [October 19, 2023, 5:10am UTC](https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400/9 "2023-10-19T05:10:01Z")

</div>

Here is the fix!

The third argument of `curl_easy_setopt` is variadic (those `...` in C), so this needs to be reflected in the type signature in order for the calling convention to work.

```plaintext
(define-curl curl_easy_setopt_url
  (_fun #:varargs-after 2
        (h val) ::
        (h : _curl) (_uint = 10002) (val : _string) -> _int)
  #:c-id curl_easy_setopt)

(define-curl curl_easy_setopt_verbose
  (_fun #:varargs-after 2
        (h val) ::
        (h : _curl) (_uint = 41) (val : _long) -> _int)
  #:c-id curl_easy_setopt)

```

This difference is sort of more evident when I tried to translate this into C. If you change the type of the third argument of `dyn_curl_easy_setopt_url` to `char*`, the C program ends up crashing.

```plaintext
#include <stdio.h>
#include <dlfcn.h>
#include <curl/curl.h>

CURLcode (*dyn_curl_global_init)(long flags);
void (*dyn_curl_global_cleanup)(void);
CURL* (*dyn_curl_easy_init)(void);
CURLcode (*dyn_curl_easy_setopt_verbose)(CURL *handle, unsigned int, ...);
CURLcode (*dyn_curl_easy_setopt_url)(CURL *handle, unsigned int, ...);
// ^HERE
CURLcode (*dyn_curl_easy_perform)(CURL *easy_handle);
void (*dyn_curl_easy_cleanup)(CURL *handle);

int main(void) {
  void* hcurl = dlopen("libcurl.dylib", RTLD_NOW);

  dyn_curl_global_init = dlsym(hcurl, "curl_global_init");
  dyn_curl_global_cleanup = dlsym(hcurl, "curl_global_cleanup");
  dyn_curl_easy_init = dlsym(hcurl, "curl_easy_init");
  dyn_curl_easy_setopt_verbose = dlsym(hcurl, "curl_easy_setopt");
  dyn_curl_easy_setopt_url = dlsym(hcurl, "curl_easy_setopt");
  dyn_curl_easy_perform = dlsym(hcurl, "curl_easy_perform");
  dyn_curl_easy_cleanup = dlsym(hcurl, "curl_easy_cleanup");

  dyn_curl_global_init(CURL_GLOBAL_ALL);
  CURL *curl = dyn_curl_easy_init();
  if(curl) {
    CURLcode res = 0;
    dyn_curl_easy_setopt_verbose(curl, CURLOPT_VERBOSE, 1L);
    dyn_curl_easy_setopt_url(curl, CURLOPT_URL, "https://www.google.com");
    res = dyn_curl_easy_perform(curl);
    dyn_curl_easy_cleanup(curl);
  }
  dyn_curl_global_cleanup();

  dlclose(hcurl);
  return 0;
}

```

---

<div class="post-metadata">

**Author:** ![SamPhillips](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samphillips/32/15_2.png) [@SamPhillips](https://racket.discourse.group/u/SamPhillips)\
**Post date:** [October 19, 2023, 2:36pm UTC](https://racket.discourse.group/t/ffi-program-not-working-on-mac/2400/10 "2023-10-19T14:36:26Z")

</div>

I'll give that a try. Thanks for looking at this!
