This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Clean up locale file alignment handling


This patch cleans up the locale file alignment issues I know about to
use the LOCFILE_ALIGN* macros more consistently.  Various places with
their own hardcoded alignment logic are made to use those macros, and
two functions that use obstack interfaces that involve casting to (int
*) have assertions of appropriate alignment added so that any bugs
with inadequate alignment show up architecture-independently rather
than depending on what unaligned accesses do on particular hosts.
(The original cross-localedef changes included changes to avoid using
those obstack interfaces when there wasn't sufficient alignment.
Since we decided to make locale alignment architecture-independent,
the issue those changes had to deal with of locale files using 2-byte
alignment but the host using 4-byte alignment doesn't arise and I
believe those assertions are correct.)

Tested x86_64; the installed shared library binaries and
locale-archive are unchanged by the patch (of course the new
assertions change the localedef binary).

2013-10-07  Joseph Myers  <joseph@codesourcery.com>

	* locale/loadlocale.c (_nl_intern_locale_data): Use
	LOCFILE_ALIGNED_P.
	* locale/programs/3level.h (CONCAT(add_locale_,TABLE)): Use
	LOCFILE_ALIGN_UP and LOCFILE_ALIGN.
	* locale/programs/ld-collate.c (obstack_int32_grow): Assert that
	obstack data is appropriately aligned.
	(obstack_int32_grow_fast): Likewise.
	* locale/programs/ld-ctype.c (ctype_output): Use LOCFILE_ALIGN.
	* locale/programs/locfile.c (add_locale_uint32): Likewise.
	(add_locale_uint32_array): Likewise.

diff --git a/locale/loadlocale.c b/locale/loadlocale.c
index de7e3d0..45162f1 100644
--- a/locale/loadlocale.c
+++ b/locale/loadlocale.c
@@ -148,7 +148,7 @@ _nl_intern_locale_data (int category, const void *data, size_t datasize)
 	newdata->values[cnt].string = newdata->filedata + idx;
       else
 	{
-	  if (idx % __alignof__ (u_int32_t) != 0)
+	  if (!LOCFILE_ALIGNED_P (idx))
 	    goto puntdata;
 	  newdata->values[cnt].word =
 	    *((const u_int32_t *) (newdata->filedata + idx));
diff --git a/locale/programs/3level.h b/locale/programs/3level.h
index b8d6ca0..c5f024f 100644
--- a/locale/programs/3level.h
+++ b/locale/programs/3level.h
@@ -270,7 +270,7 @@ CONCAT(add_locale_,TABLE) (struct locale_file *file, struct TABLE *t)
     + t->level1_size * sizeof (uint32_t)
     + (t->level2_size << t->q) * sizeof (uint32_t)
     + (t->level3_size << t->p) * sizeof (ELEMENT);
-  t->result_size = (last_offset + 3) & ~3ul;
+  t->result_size = LOCFILE_ALIGN_UP (last_offset);
 
   level2_offset =
     5 * sizeof (uint32_t)
@@ -308,7 +308,7 @@ CONCAT(add_locale_,TABLE) (struct locale_file *file, struct TABLE *t)
 			     t->level3_size << t->p);
   else
     abort ();
-  align_locale_data (file, 4);
+  align_locale_data (file, LOCFILE_ALIGN);
   end_locale_structure (file);
 
   if (t->level1_alloc > 0)
diff --git a/locale/programs/ld-collate.c b/locale/programs/ld-collate.c
index 31e2d05..d88a6de 100644
--- a/locale/programs/ld-collate.c
+++ b/locale/programs/ld-collate.c
@@ -44,6 +44,7 @@ static inline void
 __attribute ((always_inline))
 obstack_int32_grow (struct obstack *obstack, int32_t data)
 {
+  assert (LOCFILE_ALIGNED_P (obstack_object_size (obstack)));
   data = maybe_swap_uint32 (data);
   if (sizeof (int32_t) == sizeof (int))
     obstack_int_grow (obstack, data);
@@ -55,6 +56,7 @@ static inline void
 __attribute ((always_inline))
 obstack_int32_grow_fast (struct obstack *obstack, int32_t data)
 {
+  assert (LOCFILE_ALIGNED_P (obstack_object_size (obstack)));
   data = maybe_swap_uint32 (data);
   if (sizeof (int32_t) == sizeof (int))
     obstack_int_grow_fast (obstack, data);
diff --git a/locale/programs/ld-ctype.c b/locale/programs/ld-ctype.c
index 1696294..e7e17b8 100644
--- a/locale/programs/ld-ctype.c
+++ b/locale/programs/ld-ctype.c
@@ -1032,7 +1032,7 @@ ctype_output (struct localedef_t *locale, const struct charmap_t *charmap,
 	    for (cnt = 0; cnt < ctype->nr_charclass; ++cnt)
 	      add_locale_string (&file, ctype->classnames[cnt]);
 	    add_locale_char (&file, 0);
-	    align_locale_data (&file, 4);
+	    align_locale_data (&file, LOCFILE_ALIGN);
 	    end_locale_structure (&file);
 	    break;
 
@@ -1042,7 +1042,7 @@ ctype_output (struct localedef_t *locale, const struct charmap_t *charmap,
 	    for (cnt = 0; cnt < ctype->map_collection_nr; ++cnt)
 	      add_locale_string (&file, ctype->mapnames[cnt]);
 	    add_locale_char (&file, 0);
-	    align_locale_data (&file, 4);
+	    align_locale_data (&file, LOCFILE_ALIGN);
 	    end_locale_structure (&file);
 	    break;
 
diff --git a/locale/programs/locfile.c b/locale/programs/locfile.c
index 4969391..3e76ec9 100644
--- a/locale/programs/locfile.c
+++ b/locale/programs/locfile.c
@@ -627,7 +627,7 @@ add_locale_wstring (struct locale_file *file, const uint32_t *string)
 void
 add_locale_uint32 (struct locale_file *file, uint32_t value)
 {
-  align_locale_data (file, sizeof (uint32_t));
+  align_locale_data (file, LOCFILE_ALIGN);
   record_offset (file);
   value = maybe_swap_uint32 (value);
   obstack_grow (&file->data, &value, sizeof (value));
@@ -639,7 +639,7 @@ void
 add_locale_uint32_array (struct locale_file *file,
 			 const uint32_t *data, size_t n_elems)
 {
-  align_locale_data (file, sizeof (uint32_t));
+  align_locale_data (file, LOCFILE_ALIGN);
   record_offset (file);
   obstack_grow (&file->data, data, n_elems * sizeof (uint32_t));
   maybe_swap_uint32_obstack (&file->data, n_elems);

-- 
Joseph S. Myers
joseph@codesourcery.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]