From abcd7ee5c09d7fbd26410b6f880e5534b0bbb51a Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 18 May 2022 08:10:50 -0700 Subject: ffi: alignment bug in undimensioned arrays. Because the varray behavior for undimensioned arrays was introduced in dubious commit 7880c9b565ab438e1bf0250a967acdbf8d04cb42 in 2017, which used make_ffi_type_pointer to register the type, claiming that the C representation is pointer (which was not true in that commit, nor ever since). As a result, though, undimensioned arrays received the alignment of pointers, rather than deriving it from the element type. Thus (array char) has 4 or 8 byte alignment whereas (array 4 char) correctly has 1 byte alignment. * ffi.c (ffi_type_compile): Use make_ffi_type_array for the two-element array syntax, just like for the dimensioned case with three elements. Then override some of the functions with the varray versions. * tests/017/ffi-misc.tl: Fix the test case which exposed this. In the type (struct flex (a char) (b (zarray char)), the array b must be at offset 1. I didn't notice that the offset of 4 being confirmed by the test case was wrong, but this showed up when running the test case on a platform with 8 byte pointers. --- tests/017/ffi-misc.tl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/017/ffi-misc.tl b/tests/017/ffi-misc.tl index 6e41f482..1b729bbc 100644 --- a/tests/017/ffi-misc.tl +++ b/tests/017/ffi-misc.tl @@ -92,5 +92,5 @@ (ffi (struct flex (x char) (y (zarray char)))) (mtest - (ffi-put (new flex x #\a y "bcd") (ffi (struct flex))) #b'6100000062636400' - (ffi-get #b'6100000062636400' (ffi (struct flex))) #S(flex x #\a y "bcd")) + (ffi-put (new flex x #\a y "bcd") (ffi (struct flex))) #b'6162636400' + (ffi-get #b'6162636400' (ffi (struct flex))) #S(flex x #\a y "bcd")) -- cgit v1.2.3