Fix struct_by_value_big (backport)

Merged upstream:
https://github.com/libffi/libffi/pull/738

Signed-off-by: David Abdurachmanov <davidlt@rivosinc.com>
This commit is contained in:
David Abdurachmanov 2022-10-18 09:23:53 +03:00
parent 27ec531acd
commit 93f40cb825
Signed by: davidlt
GPG Key ID: 8B7F1DA0E2C9FDBB
2 changed files with 64 additions and 9 deletions

View File

@ -0,0 +1,58 @@
From 4b0c358e28fae22164bf0d423f183dfed8a1ba10 Mon Sep 17 00:00:00 2001
From: Andreas Schwab <schwab@linux-m68k.org>
Date: Mon, 10 Oct 2022 17:57:47 +0200
Subject: [PATCH] riscv: make copies of structs passed by reference (#738)
Co-authored-by: Andreas Schwab <schwab@suse.de>
---
src/riscv/ffi.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/src/riscv/ffi.c b/src/riscv/ffi.c
index f08191dcc..b455b12ae 100644
--- a/src/riscv/ffi.c
+++ b/src/riscv/ffi.c
@@ -61,6 +61,7 @@ typedef struct call_builder
int used_integer;
int used_float;
size_t *used_stack;
+ void *struct_stack;
} call_builder;
/* integer (not pointer) less than ABI XLEN */
@@ -227,7 +228,9 @@ static void marshal(call_builder *cb, ffi_type *type, int var, void *data) {
#endif
if (type->size > 2 * __SIZEOF_POINTER__) {
- /* pass by reference */
+ /* copy to stack and pass by reference */
+ data = memcpy (cb->struct_stack, data, type->size);
+ cb->struct_stack = (size_t *) FFI_ALIGN ((char *) cb->struct_stack + type->size, __SIZEOF_POINTER__);
marshal_atom(cb, FFI_TYPE_POINTER, &data);
} else if (IS_INT(type->type) || type->type == FFI_TYPE_POINTER) {
marshal_atom(cb, type->type, data);
@@ -335,10 +338,12 @@ ffi_call_int (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue,
that all remaining arguments are long long / __int128 */
size_t arg_bytes = cif->nargs <= 3 ? 0 :
FFI_ALIGN(2 * sizeof(size_t) * (cif->nargs - 3), STKALIGN);
+ /* Allocate space for copies of big structures. */
+ size_t struct_bytes = FFI_ALIGN (cif->bytes, STKALIGN);
size_t rval_bytes = 0;
if (rvalue == NULL && cif->rtype->size > 2*__SIZEOF_POINTER__)
rval_bytes = FFI_ALIGN(cif->rtype->size, STKALIGN);
- size_t alloc_size = arg_bytes + rval_bytes + sizeof(call_context);
+ size_t alloc_size = arg_bytes + rval_bytes + struct_bytes + sizeof(call_context);
/* the assembly code will deallocate all stack data at lower addresses
than the argument region, so we need to allocate the frame and the
@@ -358,8 +363,9 @@ ffi_call_int (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue,
call_builder cb;
cb.used_float = cb.used_integer = 0;
- cb.aregs = (call_context*)(alloc_base + arg_bytes + rval_bytes);
+ cb.aregs = (call_context*)(alloc_base + arg_bytes + rval_bytes + struct_bytes);
cb.used_stack = (void*)alloc_base;
+ cb.struct_stack = (void *) (alloc_base + arg_bytes + rval_bytes);
int return_by_ref = passed_by_ref(&cb, cif->rtype, 0);
if (return_by_ref)

View File

@ -4,7 +4,7 @@
Name: libffi
Version: 3.4.3
Release: 1.0.riscv64%{?dist}
Release: 1.1.riscv64%{?dist}
Summary: A portable foreign function interface library
License: MIT
URL: http://sourceware.org/libffi
@ -13,6 +13,8 @@ Source0: https://github.com/libffi/libffi/releases/download/v3.4.3/libffi-3.4.3.
Source1: ffi-multilib.h
Source2: ffitarget-multilib.h
Patch10: 4b0c358e28fae22164bf0d423f183dfed8a1ba10.patch
# https://github.com/libffi/libffi/pull/689
#Patch0: libffi-3.4.2-ppc.patch
@ -73,15 +75,7 @@ developing applications that use %{name}.
%check
%if %{without bootstrap}
%ifnarch riscv64
%make_build check
%else
# Failing test: libffi.call/struct_by_value_big.c
# See: https://github.com/libffi/libffi/issues/694
# See: https://github.com/libffi/libffi/commit/482b37f00467325e3389bab322525099860dd9aa
# See: https://github.com/libffi/libffi/commit/e409225b41b60c490a094bb068e639a2364202fd
%make_build check || true
%endif
%endif
%install
@ -129,6 +123,9 @@ rm -rf ${RPM_BUILD_ROOT}%{_libdir}/lp64d
%{_infodir}/libffi.info.*
%changelog
* Fri Sep 23 2022 David Abdurachmanov <davidlt@rivosinc.com> - 3.4.3-1.1.riscv64
- Backport fix for 738 (fix for struct_by_value_big)
* Fri Sep 23 2022 David Abdurachmanov <davidlt@rivosinc.com> - 3.4.3-1.0.riscv64
- Update to 3.4.3