Skip to content

Fixes for musl and FreeBSD#236

Open
250MHz wants to merge 8 commits into
NASA-AMMOS:mainfrom
250MHz:chimera-freebsd-hack
Open

Fixes for musl and FreeBSD#236
250MHz wants to merge 8 commits into
NASA-AMMOS:mainfrom
250MHz:chimera-freebsd-hack

Conversation

@250MHz

@250MHz 250MHz commented Jul 11, 2026

Copy link
Copy Markdown

I'm currently fixing ION's configuration and usage of BSL. Since ION also tries to support musl and FreeBSD, I wanted to also test out my changes on those platforms but ran into some problems. After a day of debugging, here's patches that makes BSL build successfully for me with ./build.sh check passing.

Some concerns:

  • For commit cbe23dc (mock_bpa: Include headers for sockaddr_in, socket, 2026-07-11), I wasn't sure what's the preferred ordering of headers. mock_bpa.c had it grouped like C system headers, other BSL headers, then mock BPA headers. I did that for agent.h, decode.c, and encode.c. But agent.c didn't group them like that, and the ordering was also different
  • Since BSL defines _POSIX_C_SOURCE as 200809L, maybe it's not even worth keeping gettimeofday() around in commit 3e52805 (HostInterface: Use clock_gettime if available, 2026-07-11)?

250MHz added 8 commits July 10, 2026 18:05
By scoping OpenSSL as INTERFACE, libbsl_crypto lacks the OpenSSL
symbols. OpenSSL should be PRIVATE since CryptoInterface.c uses OpenSSL
types, but the public headers do not.

test_CryptoInterface relies on OpenSSL symbols indirectly from
libbsl_crypto (via libbsl_mock_bpa, which gets them via the SC libs).
Since we're making OpenSSL privately scoped, add OpenSSL::Crypto as a
dependency of test_CryptoInterface.

libbsl_crypto only uses M*LIB types internally, so M*LIB should be
scoped as PRIVATE not PUBLIC. libbsl_cose_sc uses M*LIB headers in its
public API, so list M*LIB as a dependency.
Slightly more portable than #!/bin/bash
valgrind-devel installed by pkg or ports on FreeBSD installs libraries
and headers, etc., into /usr/local/include/. Using pkg-config to get the
right directory doesn't work because Valgrind's .pc file's includedir
appends "/valgrind", so CryptoInterface.c including
<valgrind/memcheck.h> doesn't work.

Instead, get the directory of valgrind/memcheck.h with find_path.
When _POSIX_C_SOURCE is defined on FreeBSD, gettimeofday() is hidden.
gettimeofday() was marked obsolescent in POSIX Issue 7 and removed in
Issue 8. The APPLICATION USAGE section [1] recommends clock_gettime()
instead, so prefer that over gettimeofday() if it's available.

[1]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/gettimeofday.html
FreeBSD needs explicit linkage to use POSIX threads. Building
libbsl_mock_bpa fails because pthread_create is an undefined reference.
So link libbsl_mock_bpa with Threads::Threads.
FreeBSD's <arpa/inet.h> doesn't make all the symbols from <netinet/in.h>
visible, and <netinet/in.h> doesn't make all the symbols from
<sys/socket.h> visible. So explicitly include these headers on the files
that need those symbols.
Issue 8 says: "The fseek() and fseeko() functions can be used to set the
file position beyond the current buffer length. It is
implementation-defined whether this extends the buffer to the new
length" [1]. agent.c relies on this behavior to pre-allocate BTSD size.
This works on glibc, but not on musl or FreeBSD. Writing to move the
position to a value larger than the current length will bump the buffer
length up, so do that instead.

But while glibc uses the size_t pointer to report the size of the buffer
[2], FreeBSD follows POSIX in making the variable pointed to by the
size_t pointer contain the smaller of the current position and the
current buffer length [3]. We can't rely on MockBPA_BTSD_Data_s.size
being the buffer length, so introduce the MockBPA_BTSD_Data_s.cap member
to track the size of the buffer.

[1]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/open_memstream.html
[2]: https://manned.org/man.b13f60d3/arch/open_memstream
[3]: https://manned.org/man.64840208/freebsd-15.0/open_memstream
test_CryptoInterface has memory leaks on FreeBSD. test_encrypt has an
empty plaintext_in as one of its values. In this case, strlen() returns
0, so BSL_TestUtils_FlatReader passes size 0 to fmemopen().

POSIX allows different behavior when the buffer size is 0 [1]. Since
glibc 2.22, a zero size successfully creates a stream [2]. On FreeBSD, a
zero size returns NULL [3].

test_encrypt calls BSL_SeqReader_Destroy, which calls the deinit
function of the reader which is BSL_TestUtils_ReadBTSD_Deinit. This
function returns early if either obj or obj->file are NULL, without
calling fclose(obj->file) and BSL_free(obj), and since obj->file is NULL
on FreeBSD, memory leaks are reported.

Fix this by separating the check of obj and obj->file. Always free obj
if obj is not null.

The same pattern happens for BSL_TestUtils_WriteBTSD_Deinit, so apply
the fix there as well.

[1]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/fmemopen.html
[2]: https://manned.org/man.c445d9bf/arch/fmemopen#head10
[3]: https://manned.org/man.7779899a/freebsd-15.0/fmemopen#head6
@250MHz 250MHz requested a review from a team as a code owner July 11, 2026 12:37
@github-project-automation github-project-automation Bot moved this to Todo in BSL Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant