[Tarantool-patches] [PATCH luajit 3/3] FFI/ARM64/OSX: Fix vararg call handling.

Igor Munkin imun at tarantool.org
Tue May 11 01:09:07 MSK 2021


From: Mike Pall <mike>

Thanks to Igor Munkin.

(cherry picked from commit 521b367567dc5d91d7f9ae29c257998953e24e53)

This patch fixes the issue introduced by commit
2e2fb8f6b5118e1a7996b76600c6ee98bfd5f203 ('OSX/iOS: Handle iOS simulator
and ARM64 Macs.'). Within the mentioned commit LJ_TARGET_IOS define is
set via Apple system header to enable several features (e.g. JIT and
external unwinder) on ARM64 Macs, but its usage was not adjusted
source-wide. This is done for FFI machinery within this commit.

Since all LJ_TARGET_IOS usage is done with LJ_TARGET_ARM64 define being
set, we can simply replace all occurrences with LJ_TARGET_OSX.

Igor Munkin:
* added the description and the test for the problem

Part of tarantool/tarantool#5629
Relates to tarantool/tarantool#5983

Reported-by: Nikita Pettik <korablev at tarantool.org>
Signed-off-by: Igor Munkin <imun at tarantool.org>
---
 src/lj_ccall.c                                   |  8 ++++----
 src/lj_ccallback.c                               |  2 +-
 .../lj-695-ffi-vararg-call.test.lua              | 16 ++++++++++++++++
 3 files changed, 21 insertions(+), 5 deletions(-)
 create mode 100644 test/tarantool-tests/lj-695-ffi-vararg-call.test.lua

diff --git a/src/lj_ccall.c b/src/lj_ccall.c
index 5c252e5b..06d1b3bd 100644
--- a/src/lj_ccall.c
+++ b/src/lj_ccall.c
@@ -334,7 +334,7 @@
   isfp = sz == 2*sizeof(float) ? 2 : 1;
 
 #define CCALL_HANDLE_REGARG \
-  if (LJ_TARGET_IOS && isva) { \
+  if (LJ_TARGET_OSX && isva) { \
     /* IOS: All variadic arguments are on the stack. */ \
   } else if (isfp) {  /* Try to pass argument in FPRs. */ \
     int n2 = ctype_isvector(d->info) ? 1 : n*isfp; \
@@ -344,10 +344,10 @@
       goto done; \
     } else { \
       nfpr = CCALL_NARG_FPR;  /* Prevent reordering. */ \
-      if (LJ_TARGET_IOS && d->size < 8) goto err_nyi; \
+      if (LJ_TARGET_OSX && d->size < 8) goto err_nyi; \
     } \
   } else {  /* Try to pass argument in GPRs. */ \
-    if (!LJ_TARGET_IOS && (d->info & CTF_ALIGN) > CTALIGN_PTR) \
+    if (!LJ_TARGET_OSX && (d->info & CTF_ALIGN) > CTALIGN_PTR) \
       ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \
     if (ngpr + n <= maxgpr) { \
       dp = &cc->gpr[ngpr]; \
@@ -355,7 +355,7 @@
       goto done; \
     } else { \
       ngpr = maxgpr;  /* Prevent reordering. */ \
-      if (LJ_TARGET_IOS && d->size < 8) goto err_nyi; \
+      if (LJ_TARGET_OSX && d->size < 8) goto err_nyi; \
     } \
   }
 
diff --git a/src/lj_ccallback.c b/src/lj_ccallback.c
index 846827b1..9158c2d3 100644
--- a/src/lj_ccallback.c
+++ b/src/lj_ccallback.c
@@ -406,7 +406,7 @@ void lj_ccallback_mcode_free(CTState *cts)
       nfpr = CCALL_NARG_FPR;  /* Prevent reordering. */ \
     } \
   } else { \
-    if (!LJ_TARGET_IOS && n > 1) \
+    if (!LJ_TARGET_OSX && n > 1) \
       ngpr = (ngpr + 1u) & ~1u;  /* Align to regpair. */ \
     if (ngpr + n <= maxgpr) { \
       sp = &cts->cb.gpr[ngpr]; \
diff --git a/test/tarantool-tests/lj-695-ffi-vararg-call.test.lua b/test/tarantool-tests/lj-695-ffi-vararg-call.test.lua
new file mode 100644
index 00000000..04be1998
--- /dev/null
+++ b/test/tarantool-tests/lj-695-ffi-vararg-call.test.lua
@@ -0,0 +1,16 @@
+local tap = require('tap')
+
+local test = tap.test('lj-695-ffi-vararg-call')
+test:plan(2)
+
+local ffi = require('ffi')
+local str = ffi.new('char[256]')
+ffi.cdef('int sprintf(char *str, const char *format, ...)')
+local strlen = ffi.C.sprintf(str, 'try vararg function: %s:%.2f(%d) - %llu',
+                             'imun', 9, 9LL, -1ULL)
+
+local result = ffi.string(str)
+test:is(#result, strlen)
+test:is(result, 'try vararg function: imun:9.00(9) - 18446744073709551615')
+
+os.exit(test:check() and 0 or 1)
-- 
2.25.0



More information about the Tarantool-patches mailing list