public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [patch][gold] Avoid invalid unaligned write
@ 2015-06-08  1:11 Rafael Espíndola
  2015-06-11 17:07 ` Cary Coutant
  0 siblings, 1 reply; 4+ messages in thread
From: Rafael Espíndola @ 2015-06-08  1:11 UTC (permalink / raw)
  To: Binutils; +Cc: Cary Coutant

[-- Attachment #1: Type: text/plain, Size: 359 bytes --]

A simple testcase with just

movl $foo, %edi
foo:

will cause an unaligned access in gold. A backtrace found with
-fsanitize=alignment is attached.

The attached patch avoids the unaligned access.

Cheers,
Rafael

2015-06-07  Rafael Ávila de Espíndola  <rafael.espindola@gmail.com>

* elfcpp_swap.h (Swap::writeval): Avoid unaligned access.

[-- Attachment #2: trace --]
[-- Type: application/octet-stream, Size: 3081 bytes --]

#0  __ubsan_handle_type_mismatch () at /home/espindola/llvm/llvm/projects/compiler-rt/lib/ubsan/ubsan_handlers.cc:72
#1  0x00000000004eb8cf in elfcpp::Swap<32, false>::writeval (wv=0x7ffff7fee0b1, v=4194485) at ../../binutils/gold/../elfcpp/elfcpp_swap.h:237
#2  0x000000000056f2f7 in gold::Relocate_functions<64, false>::rela<32> (view=0x7ffff7fee0b1 "", object=0x38179f0, psymval=0x3817db8, addend=5) at ../../binutils/gold/reloc.h:403
#3  0x000000000056ef3b in gold::Relocate_functions<64, false>::rela32 (view=0x7ffff7fee0b1 "", object=0x38179f0, psymval=0x3817db8, addend=5) at ../../binutils/gold/reloc.h:624
#4  0x0000000000527c60 in (anonymous namespace)::Target_x86_64<64>::Relocate::relocate (this=0x7fffffff65c0, relinfo=0x7fffffff6af0, target=0x3817930, relnum=0, rela=..., r_type=10, gsym=0x0, psymval=0x3817db8, view=0x7ffff7fee0b1 "", 
    address=4194481, view_size=5) at ../../binutils/gold/x86_64.cc:3438
#5  0x00000000005265cf in gold::relocate_section<64, false, (anonymous namespace)::Target_x86_64<64>, 4, (anonymous namespace)::Target_x86_64<64>::Relocate, gold::Default_comdat_behavior> (relinfo=0x7fffffff6af0, target=0x3817930, 
    prelocs=0x7ffff7ff00f8 "\001", reloc_count=1, output_section=0x3818400, needs_special_offset_handling=false, view=0x7ffff7fee0b0 "\277", view_address=4194480, view_size=5, reloc_symbol_changes=0x0) at ../../binutils/gold/target-reloc.h:403
#6  0x00000000005091ec in (anonymous namespace)::Target_x86_64<64>::relocate_section (this=0x3817930, relinfo=0x7fffffff6af0, sh_type=4, prelocs=0x7ffff7ff00f8 "\001", reloc_count=1, output_section=0x3818400, needs_special_offset_handling=false, 
    view=0x7ffff7fee0b0 "\277", address=4194480, view_size=5, reloc_symbol_changes=0x0) at ../../binutils/gold/x86_64.cc:4248
#7  0x00000000012e1b24 in gold::Sized_relobj_file<64, false>::do_relocate_sections (this=0x38179f0, symtab=0x7fffffff7c48, layout=0x7fffffff77f8, pshdrs=0x7ffff7ff0110 "", of=0x381ad00, pviews=0x7fffffff6dc8) at ../../binutils/gold/reloc.cc:1013
#8  0x000000000106fcf1 in gold::Sized_relobj_file<64, false>::relocate_sections (this=0x38179f0, symtab=0x7fffffff7c48, layout=0x7fffffff77f8, pshdrs=0x7ffff7ff0110 "", of=0x381ad00, pviews=0x7fffffff6dc8) at ../../binutils/gold/object.h:2546
#9  0x00000000012d3b41 in gold::Sized_relobj_file<64, false>::do_relocate (this=0x38179f0, symtab=0x7fffffff7c48, layout=0x7fffffff77f8, of=0x381ad00) at ../../binutils/gold/reloc.cc:670
#10 0x00000000012ea851 in gold::Relobj::relocate (this=0x38179f0, symtab=0x7fffffff7c48, layout=0x7fffffff77f8, of=0x381ad00) at ../../binutils/gold/object.h:1185
#11 0x00000000012b6b12 in gold::Relocate_task::run (this=0x381aec0) at ../../binutils/gold/reloc.cc:239
#12 0x00000000014dde8d in gold::Workqueue::find_and_run_task (this=0x7fffffff8088, thread_number=0) at ../../binutils/gold/workqueue.cc:319
#13 0x00000000014df23f in gold::Workqueue::process (this=0x7fffffff8088, thread_number=0) at ../../binutils/gold/workqueue.cc:495
#14 0x0000000000421d37 in main (argc=2, argv=0x7fffffffde78) at ../../binutils/gold/main.cc:252

[-- Attachment #3: t.patch --]
[-- Type: text/x-patch, Size: 457 bytes --]

diff --git a/elfcpp/elfcpp_swap.h b/elfcpp/elfcpp_swap.h
index 5b9a915..cddc0b3 100644
--- a/elfcpp/elfcpp_swap.h
+++ b/elfcpp/elfcpp_swap.h
@@ -234,7 +234,10 @@ struct Swap
 
   static inline void
   writeval(Valtype* wv, Valtype v)
-  { *wv = Convert<size, big_endian>::convert_host(v); }
+  {
+    Valtype T = Convert<size, big_endian>::convert_host(v);
+    memcpy(wv, &T, sizeof(T));
+  }
 
   static inline Valtype
   readval(const unsigned char* wv)

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-06-15 18:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-08  1:11 [patch][gold] Avoid invalid unaligned write Rafael Espíndola
2015-06-11 17:07 ` Cary Coutant
2015-06-11 21:29   ` Rafael Espíndola
2015-06-15 18:59     ` Rafael Espíndola

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).