[PATCH] Some more alignment tests, memstream cleanup
Jakub Jelinek
jakub@redhat.com
Wed Sep 17 16:58:00 GMT 2003
Hi!
Two separate things actually:
1) new alignment tests
2) after latest change memstream.c has static unused _IO_wmem_jumps
(and removing that shows two more static unused functions)
2003-09-17 Jakub Jelinek <jakub@redhat.com>
* elf/Makefile (distribute): Add tst-alignmod.c.
(tests): Add tst-align.
(modules-names): Add tst-alignmod.
(CFLAGS-tst-align.c, CFLAGS-tst-alignmod.c): Add
$(stack-align-test-flags).
($(objpfx)tst-align): Depend on libdl.
($(objpfx)tst-align.out): Depend on tst-alignmod.so.
* elf/tst-align.c: New test.
* elf/tst-alignmod.c: New test.
* misc/Makefile (CFLAGS-tst-tsearch.c): Add $(stack-align-test-flags).
* misc/tst-tsearch.c: Include tst-stack-align.h.
(stack_align_check): New array.
(cmp_fn, walk_action): Use TEST_STACK_ALIGN ().
(main): Report error if stack was not enough aligned
in cmp_fn or walk_action.
* stdlib/Makefile (CFLAGS-tst-qsort.c): Add $(stack-align-test-flags).
* stdlib/tst-qsort.c: Include tst-stack-align.h.
(align_check): New var.
(compare): Use TEST_STACK_ALIGN () macro.
(main): Report error if stack was not enough aligned in compare.
* sysdeps/i386/i686/Makefile (stack-align-test-flags): Remove
unneeded -mpreferred-stack-boundary=4.
* sysdeps/x86_64/tst-stack-align.h: New file.
* libio/memstream.c (_IO_wmem_jumps): Remove unused structure.
(_IO_wmem_sync, _IO_wmem_finish): Remove unused functions.
--- libc/elf/Makefile.jj 2003-09-17 07:42:30.000000000 -0400
+++ libc/elf/Makefile 2003-09-17 12:34:18.000000000 -0400
@@ -71,7 +71,7 @@ distribute := rtld-Rules \
tst-tlsmod1.c tst-tlsmod2.c tst-tlsmod3.c tst-tlsmod4.c \
tst-tlsmod5.c tst-tlsmod6.c tst-tlsmod7.c tst-tlsmod8.c \
tst-tlsmod9.c tst-tlsmod10.c tst-tlsmod11.c \
- tst-tlsmod12.c tst-tls10.h \
+ tst-tlsmod12.c tst-tls10.h tst-alignmod.c \
circlemod1.c circlemod1a.c circlemod2.c circlemod2a.c \
circlemod3.c circlemod3a.c nodlopenmod2.c \
tls-macros.h \
@@ -150,7 +150,7 @@ tests += loadtest restest1 preloadtest l
neededtest3 neededtest4 unload2 lateglobal initfirst global \
restest2 next dblload dblunload reldep5 reldep6 reldep7 reldep8 \
circleload1 tst-tls3 tst-tls4 tst-tls5 tst-tls6 tst-tls7 tst-tls8 \
- tst-tls10 tst-tls11 tst-tls12 tst-tls13 tst-tls14
+ tst-tls10 tst-tls11 tst-tls12 tst-tls13 tst-tls14 tst-align
# reldep9
test-srcs = tst-pathopt
tests-vis-yes = vismain
@@ -177,7 +177,8 @@ modules-names = testobj1 testobj2 testob
circlemod1 circlemod1a circlemod2 circlemod2a \
circlemod3 circlemod3a \
reldep8mod1 reldep8mod2 reldep8mod3 \
- reldep9mod1 reldep9mod2 reldep9mod3
+ reldep9mod1 reldep9mod2 reldep9mod3 \
+ tst-alignmod
ifeq (yes,$(have-initfini-array))
modules-names += tst-array2dep
endif
@@ -648,6 +649,11 @@ $(objpfx)tst-tls13.out: $(objpfx)tst-tls
$(objpfx)tst-tls14: $(objpfx)tst-tlsmod14a.so $(libdl)
$(objpfx)tst-tls14.out:$(objpfx)tst-tlsmod14b.so
+CFLAGS-tst-align.c = $(stack-align-test-flags)
+CFLAGS-tst-alignmod.c = $(stack-align-test-flags)
+$(objpfx)tst-align: $(libdl)
+$(objpfx)tst-align.out: $(objpfx)tst-alignmod.so
+
ifdef libdl
$(objpfx)tst-tls9-static: $(common-objpfx)dlfcn/libdl.a
$(objpfx)tst-tls9-static.out: $(objpfx)tst-tlsmod5.so $(objpfx)tst-tlsmod6.so
--- libc/elf/tst-align.c.jj 2003-09-17 12:03:05.000000000 -0400
+++ libc/elf/tst-align.c 2003-09-17 12:07:05.000000000 -0400
@@ -0,0 +1,54 @@
+/* Copyright (C) 2003 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <dlfcn.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static int
+do_test (void)
+{
+ static const char modname[] = "tst-alignmod.so";
+ int result = 0;
+ void (*fp) (int *);
+ void *h;
+
+ h = dlopen (modname, RTLD_LAZY);
+ if (h == NULL)
+ {
+ printf ("cannot open '%s': %s\n", modname, dlerror ());
+ exit (1);
+ }
+
+ fp = dlsym (h, "in_dso");
+ if (fp == NULL)
+ {
+ printf ("cannot get symbol 'in_dso': %s\n", dlerror ());
+ exit (1);
+ }
+
+ fp (&result);
+
+ dlclose (h);
+
+ return result;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
--- libc/elf/tst-alignmod.c.jj 2003-09-17 12:05:52.000000000 -0400
+++ libc/elf/tst-alignmod.c 2003-09-17 12:16:50.000000000 -0400
@@ -0,0 +1,53 @@
+/* Copyright (C) 2003 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdio.h>
+#include <tst-stack-align.h>
+
+static int res, *resp;
+
+static void __attribute__((constructor))
+con (void)
+{
+ res = TEST_STACK_ALIGN () ? -1 : 1;
+}
+
+void
+in_dso (int *result)
+{
+ if (!res)
+ {
+ puts ("constructor has not been run");
+ *result = 1;
+ }
+ else if (res != 1)
+ {
+ puts ("constructor has been run without sufficient alignment");
+ *result = 1;
+ }
+
+ resp = result;
+}
+
+static void __attribute__((destructor))
+des (void)
+{
+ if (TEST_STACK_ALIGN ())
+ *resp = 1;
+}
--- libc/libio/memstream.c.jj 2003-09-15 06:37:15.000000000 -0400
+++ libc/libio/memstream.c 2003-09-17 11:12:20.000000000 -0400
@@ -32,8 +32,6 @@ struct _IO_FILE_memstream
static int _IO_mem_sync __P ((_IO_FILE* fp));
static void _IO_mem_finish __P ((_IO_FILE* fp, int));
-static int _IO_wmem_sync __P ((_IO_FILE* fp));
-static void _IO_wmem_finish __P ((_IO_FILE* fp, int));
static struct _IO_jump_t _IO_mem_jumps =
@@ -60,30 +58,6 @@ static struct _IO_jump_t _IO_mem_jumps =
JUMP_INIT(imbue, _IO_default_imbue)
};
-static struct _IO_jump_t _IO_wmem_jumps =
-{
- JUMP_INIT_DUMMY,
- JUMP_INIT (finish, (_IO_finish_t) _IO_wmem_finish),
- JUMP_INIT (overflow, (_IO_overflow_t) _IO_wstr_overflow),
- JUMP_INIT (underflow, (_IO_underflow_t) _IO_wstr_underflow),
- JUMP_INIT (uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
- JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
- JUMP_INIT (xsputn, (_IO_xsputn_t) INTUSE(_IO_wdefault_xsputn)),
- JUMP_INIT (xsgetn, (_IO_xsgetn_t) INTUSE(_IO_wdefault_xsgetn)),
- JUMP_INIT (seekoff, _IO_wstr_seekoff),
- JUMP_INIT (seekpos, _IO_default_seekpos),
- JUMP_INIT (setbuf, _IO_default_setbuf),
- JUMP_INIT (sync, (_IO_sync_t) _IO_wmem_sync),
- JUMP_INIT (doallocate, INTUSE(_IO_wdefault_doallocate)),
- JUMP_INIT (read, _IO_default_read),
- JUMP_INIT (write, _IO_default_write),
- JUMP_INIT (seek, _IO_default_seek),
- JUMP_INIT (close, _IO_default_close),
- JUMP_INIT (stat, _IO_default_stat),
- JUMP_INIT(showmanyc, _IO_default_showmanyc),
- JUMP_INIT(imbue, _IO_default_imbue)
-};
-
/* Open a stream that writes into a malloc'd buffer that is expanded as
necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
and the number of characters written on fflush or fclose. */
@@ -172,55 +146,3 @@ _IO_mem_finish (fp, dummy)
INTUSE(_IO_default_finish) (fp, 0);
}
-
-
-static int
-_IO_wmem_sync (fp)
- _IO_FILE* fp;
-{
- struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
- int res;
-
- res = _IO_default_sync (fp);
- if (res < 0)
- return res;
-
- if (fp->_wide_data->_IO_write_ptr == fp->_wide_data->_IO_write_end)
- {
- _IO_wstr_overflow (fp, L'\0');
- --fp->_wide_data->_IO_write_ptr;
- }
- else
- *fp->_wide_data->_IO_write_ptr = '\0';
-
- *mp->bufloc = (char *) fp->_wide_data->_IO_write_base;
- *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
- - fp->_wide_data->_IO_write_base);
-
- return 0;
-}
-
-
-static void
-_IO_wmem_finish (fp, dummy)
- _IO_FILE* fp;
- int dummy;
-{
- struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
-
- *mp->bufloc = (char *) realloc (fp->_wide_data->_IO_write_base,
- (fp->_wide_data->_IO_write_ptr
- - fp->_wide_data->_IO_write_base + 1)
- * sizeof (wchar_t));
- if (*mp->bufloc != NULL)
- {
- ((wchar_t *) (*mp->bufloc))[fp->_wide_data->_IO_write_ptr
- - fp->_wide_data->_IO_write_base] = '\0';
- *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
- - fp->_wide_data->_IO_write_base);
- }
-
- fp->_wide_data->_IO_buf_base = NULL;
-
- INTUSE(_IO_default_finish) (fp, 0);
-}
--- libc/misc/tst-tsearch.c.jj 2001-07-06 00:55:36.000000000 -0400
+++ libc/misc/tst-tsearch.c 2003-09-17 12:22:55.000000000 -0400
@@ -1,5 +1,5 @@
/* Test program for tsearch et al.
- Copyright (C) 1997, 2000, 2001 Free Software Foundation, Inc.
+ Copyright (C) 1997, 2000, 2001, 2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -25,6 +25,7 @@
#include <stdlib.h>
#include <string.h>
#include <search.h>
+#include <tst-stack-align.h>
#define SEED 0
#define BALANCED 1
@@ -72,10 +73,14 @@ static int depths[SIZE];
/* Maximum depth during a tree walk. */
static int max_depth;
+static int stack_align_check[2];
+
/* Compare two keys. */
static int
cmp_fn (const void *a, const void *b)
{
+ if (!stack_align_check[0])
+ stack_align_check[0] = TEST_STACK_ALIGN () ? -1 : 1;
return *(const int *) a - *(const int *) b;
}
@@ -103,6 +108,9 @@ walk_action (const void *nodep, const VI
{
int key = **(int **) nodep;
+ if (!stack_align_check[1])
+ stack_align_check[1] = TEST_STACK_ALIGN () ? -1 : 1;
+
if (depth > max_depth)
max_depth = depth;
if (which == leaf || which == preorder)
@@ -329,5 +337,17 @@ main (int argc, char **argv)
total_error |= error;
}
+ for (i = 0; i < 2; ++i)
+ if (stack_align_check[i] == 0)
+ {
+ printf ("stack alignment check %d not run\n", i);
+ total_error |= 1;
+ }
+ else if (stack_align_check[i] != 1)
+ {
+ printf ("stack insufficiently aligned in check %d\n", i);
+ total_error |= 1;
+ }
+
return total_error;
}
--- libc/misc/Makefile.jj 2003-09-17 07:42:31.000000000 -0400
+++ libc/misc/Makefile 2003-09-17 12:23:52.000000000 -0400
@@ -89,6 +89,7 @@ CFLAGS-mkstemp64.c = -fexceptions
CFLAGS-getsysstats.c = -fexceptions
CFLAGS-getusershell.c = -fexceptions
CFLAGS-err.c = -fexceptions
+CFLAGS-tst-tsearch.c = $(stack-align-test-flags)
include ../Rules
--- libc/stdlib/Makefile.jj 2003-09-17 07:42:32.000000000 -0400
+++ libc/stdlib/Makefile 2003-09-17 10:57:18.000000000 -0400
@@ -97,6 +97,7 @@ CFLAGS-atexit.c = -DHAVE_DOT_HIDDEN
endif
CFLAGS-tst-bsearch.c = $(stack-align-test-flags)
+CFLAGS-tst-qsort.c = $(stack-align-test-flags)
include ../Rules
--- libc/stdlib/tst-qsort.c.jj 2002-01-29 04:05:09.000000000 -0500
+++ libc/stdlib/tst-qsort.c 2003-09-17 10:59:48.000000000 -0400
@@ -1,17 +1,24 @@
/* Test case by Paul Eggert <eggert@twinsun.com> */
#include <stdio.h>
#include <stdlib.h>
+#include <tst-stack-align.h>
struct big { char c[4 * 1024]; };
struct big *array;
struct big *array_end;
+static int align_check;
+
int
compare (void const *a1, void const *b1)
{
struct big const *a = a1;
struct big const *b = b1;
+
+ if (!align_check)
+ align_check = TEST_STACK_ALIGN () ? -1 : 1;
+
if (! (array <= a && a < array_end
&& array <= b && b < array_end))
{
@@ -38,5 +45,11 @@ main (int argc, char **argv)
qsort (array, array_members, sizeof *array, compare);
+ if (align_check == -1)
+ {
+ puts ("stack not sufficiently aligned");
+ exit (EXIT_FAILURE);
+ }
+
return 0;
}
--- libc/sysdeps/i386/i686/Makefile.jj 2003-09-17 05:12:39.000000000 -0400
+++ libc/sysdeps/i386/i686/Makefile 2003-09-17 08:59:02.000000000 -0400
@@ -4,4 +4,4 @@ static-only-routines += hp-timing
endif
# So that we can test __m128's alignment
-stack-align-test-flags += -msse -mpreferred-stack-boundary=4
+stack-align-test-flags += -msse
--- libc/sysdeps/x86_64/tst-stack-align.h.jj 2003-09-17 08:58:42.000000000 -0400
+++ libc/sysdeps/x86_64/tst-stack-align.h 2003-09-17 08:58:11.000000000 -0400
@@ -0,0 +1,47 @@
+/* Copyright (C) 2003 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdio.h>
+#include <stdint.h>
+
+#define TEST_STACK_ALIGN() \
+ ({ \
+ /* AMD64 ABI mandates 16byte aligned stack. \
+ Unfortunately, current GCC doesn't support __int128 or __float128 \
+ types, so use aligned attribute instead. */ \
+ struct _S \
+ { \
+ int _i __attribute__((aligned (16))); \
+ int _pad[3]; \
+ } _s = { ._i = 18 }; \
+ double _d = 12.0; \
+ long double _ld = 15.0; \
+ int _ret = 0; \
+ printf ("__int128: %d %p %zu\n", _s._i, &_s, __alignof (_s)); \
+ if ((((uintptr_t) &_s) & (__alignof (_s) - 1)) != 0) \
+ _ret = 1; \
+ \
+ printf ("double: %g %p %zu\n", _d, &_d, __alignof (double)); \
+ if ((((uintptr_t) &_d) & (__alignof (double) - 1)) != 0) \
+ _ret = 1; \
+ \
+ printf ("ldouble: %Lg %p %zu\n", _ld, &_ld, __alignof (long double)); \
+ if ((((uintptr_t) &_ld) & (__alignof (long double) - 1)) != 0) \
+ _ret = 1; \
+ _ret; \
+ })
Jakub
More information about the Libc-hacker
mailing list