infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
402_variadic_functions.c File Reference

Tests FFI calls for variadic functions. More...

#include "common/double_tap.h"
#include "common/infix_config.h"
#include "types.h"
#include <infix/infix.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
Include dependency graph for 402_variadic_functions.c:

Macros

#define DBLTAP_IMPLEMENTATION
 

Functions

int forward_variadic_checker (char *buffer, size_t size, const char *format,...)
 A custom checker function to transparently validate variadic arguments.
 
int forward_variadic_aggregate_checker (int fixed_arg,...)
 A custom checker for variadic aggregate arguments.
 
int variadic_reverse_handler (const char *topic,...)
 A handler for a reverse trampoline with a variadic signature.
 
 subtest ("Forward variadic call")
 
 subtest ("Forward variadic call (aggregates)")
 
 subtest ("Reverse variadic callback")
 
 subtest ("Platform ABI: macOS AArch64 variadic struct passing")
 
 subtest ("Platform ABI: Windows x64 variadic float/double passing")
 

Variables

 TEST
 

Detailed Description

Tests FFI calls for variadic functions.

Copyright (c) 2025 Sanko Robinson

This source code is dual-licensed under the Artistic License 2.0 or the MIT License. You may choose to use this code under the terms of either license.

SPDX-License-Identifier: (Artistic-2.0 OR MIT)

The documentation blocks within this file are licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).

SPDX-License-Identifier: CC-BY-4.0

This test suite verifies the library's ability to handle variadic functions (those with ... in their signature) for both forward and reverse FFI calls. Variadic argument passing involves special ABI rules that differ significantly between platforms, making this a critical area to test.

This file consolidates all previous variadic tests and covers:

  1. Forward Call: A custom C function (forward_variadic_checker) is called with a mix of fixed and variadic arguments (const char*, int, double) to verify the basic mechanism. Using a custom checker provides clearer diagnostics than calling an opaque library function like snprintf.
  2. Reverse Variadic Callback: A reverse trampoline is created for a handler with a variadic signature, confirming that the JIT stub can correctly marshal both fixed and variadic arguments.
  3. Platform: Windows x64: A specific test, compiled only on Windows, that verifies that a variadic double is correctly passed in both a GPR (for va_arg) and an XMM register (for direct use by the callee).
  4. Platform: macOS on AArch64: A specific test, compiled only on macOS ARM, that verifies the unique rule where all variadic arguments (including structs) are passed on the stack.

Macro Definition Documentation

◆ DBLTAP_IMPLEMENTATION

#define DBLTAP_IMPLEMENTATION

Function Documentation

◆ forward_variadic_aggregate_checker()

int forward_variadic_aggregate_checker ( int  fixed_arg,
  ... 
)

A custom checker for variadic aggregate arguments.

This function's behavior depends on the ABI.

  • On System V, the struct is passed by value on the stack, and va_arg retrieves the struct directly.
  • On Windows x64, a pointer to the struct is passed on the stack, so we must use va_arg(args, Point*) and dereference it.

◆ forward_variadic_checker()

int forward_variadic_checker ( char *  buffer,
size_t  size,
const char *  format,
  ... 
)

A custom checker function to transparently validate variadic arguments.

This function replaces snprintf from previous tests. It accepts a variadic argument list and uses the double_tap harness to ok() each argument it receives. This provides precise feedback on which argument, if any, is being passed incorrectly.

Returns
1 on success (all checks passed), 0 on failure.

◆ subtest() [1/5]

subtest ( "Forward variadic call (aggregates)"  )

◆ subtest() [2/5]

subtest ( "Forward variadic call"  )

◆ subtest() [3/5]

subtest ( "Platform ABI: macOS AArch64 variadic struct passing"  )

◆ subtest() [4/5]

subtest ( "Platform ABI: Windows x64 variadic float/double passing"  )

◆ subtest() [5/5]

subtest ( "Reverse variadic callback"  )

◆ variadic_reverse_handler()

int variadic_reverse_handler ( const char *  topic,
  ... 
)

A handler for a reverse trampoline with a variadic signature.

Variable Documentation

◆ TEST

TEST
Initial value:
{
plan(5)
#define plan(count)
Definition double_tap.h:132