* [PATCH, MPX wrappers 1/3] Add MPX wrappers library @ 2014-11-14 17:29 Ilya Enkovich 2014-11-14 19:20 ` Joseph Myers 2014-11-15 8:51 ` Jeff Law 0 siblings, 2 replies; 15+ messages in thread From: Ilya Enkovich @ 2014-11-14 17:29 UTC (permalink / raw) To: gcc-patches Hi, This patch introduces a simple library with several wrappers to be used with MPX and Pointer Bounds Checker. Wrappers allow to obtain, copy and just keep alive bounds whrough widely use library calls. It significantly increases checking quality. Thanks, Ilya -- gcc/ 2014-11-14 Ilya Enkovich <ilya.enkovich@intel.com> * gcc.c (MPX_SPEC): Add wrappers library. libmpx/ 2014-11-14 Ilya Enkovich <ilya.enkovich@intel.com> * Makefile.am (SUBDIRS): New. (MAKEOVERRIDES): New. * Makefile.in: Regenerate. * configure.ac: Add mpxintr/Makefile to config files. * configure: Regenerate. * mpxwrap/Makefile.am: New. * mpxwrap/Makefile.in: New. * mpxwrap/libtool-version: New. * mpxwrap/mpx_wrappers.cc: New. diff --git a/gcc/gcc.c b/gcc/gcc.c index 200704b..cf028ed 100644 --- a/gcc/gcc.c +++ b/gcc/gcc.c @@ -806,9 +806,10 @@ proper position among the other output files. */ #ifndef MPX_SPEC #define MPX_SPEC "\ -%{!nostdlib:%{!nodefaultlibs:%{mmpx:\ +%{!nostdlib:%{!nodefaultlibs:%{mmpx:%{fcheck-pointer-bounds:\ %{static:%nMPX runtime is disabled due to -static used}\ - %{!static:-lmpx}}}}" + %{!static:-lmpx}\ + %{!fno-chkp-use-wrappers:-lmpxwrappers}}}}}" #endif /* -u* was put back because both BSD and SysV seem to support it. */ diff --git a/libmpx/Makefile.am b/libmpx/Makefile.am index c6b479f..553ad30 100644 --- a/libmpx/Makefile.am +++ b/libmpx/Makefile.am @@ -1,6 +1,9 @@ ACLOCAL_AMFLAGS = -I .. -I ../config -SUBDIRS = mpxrt +SUBDIRS = mpxrt mpxwrap + +## May be used by toolexeclibdir. +gcc_version := $(shell cat $(top_srcdir)/../gcc/BASE-VER) # Work around what appears to be a GNU make bug handling MAKEFLAGS # values defined in terms of make variables, as is the case for CC and @@ -39,3 +42,5 @@ AM_MAKEFLAGS = \ "PICFLAG=$(PICFLAG)" \ "RANLIB=$(RANLIB)" \ "DESTDIR=$(DESTDIR)" + +MAKEOVERRIDES = diff --git a/libmpx/configure.ac b/libmpx/configure.ac index 9a761c4..f3abead 100644 --- a/libmpx/configure.ac +++ b/libmpx/configure.ac @@ -110,7 +110,7 @@ fi AC_CONFIG_FILES([Makefile]) AC_CONFIG_HEADERS(config.h) -AC_CONFIG_FILES(AC_FOREACH([DIR], [mpxrt], [DIR/Makefile]), +AC_CONFIG_FILES(AC_FOREACH([DIR], [mpxrt mpxwrap], [DIR/Makefile ]), [cat > vpsed$$ << \_EOF s!`test -f '$<' || echo '$(srcdir)/'`!! _EOF diff --git a/libmpx/mpxwrap/Makefile.am b/libmpx/mpxwrap/Makefile.am new file mode 100644 index 0000000..bad206d --- /dev/null +++ b/libmpx/mpxwrap/Makefile.am @@ -0,0 +1,52 @@ +ALCLOCAL_AMFLAGS = -I .. -I ../config + +# May be used by toolexeclibdir. +gcc_version := $(shell cat $(top_srcdir)/../gcc/BASE-VER) + +libmpxwrappers_la_CFLAGS = -fcheck-pointer-bounds -mmpx -fno-chkp-check-read \ + -fno-chkp-check-write -fno-chkp-use-wrappers + +toolexeclib_LTLIBRARIES = libmpxwrappers.la + +libmpxwrappers_la_SOURCES = mpx_wrappers.c + +# Work around what appears to be a GNU make bug handling MAKEFLAGS +# values defined in terms of make variables, as is the case for CC and +# friends when we are called from the top level Makefile. +AM_MAKEFLAGS = \ + "AR_FLAGS=$(AR_FLAGS)" \ + "CC_FOR_BUILD=$(CC_FOR_BUILD)" \ + "CFLAGS=$(CFLAGS)" \ + "CXXFLAGS=$(CXXFLAGS)" \ + "CFLAGS_FOR_BUILD=$(CFLAGS_FOR_BUILD)" \ + "CFLAGS_FOR_TARGET=$(CFLAGS_FOR_TARGET)" \ + "INSTALL=$(INSTALL)" \ + "INSTALL_DATA=$(INSTALL_DATA)" \ + "INSTALL_PROGRAM=$(INSTALL_PROGRAM)" \ + "INSTALL_SCRIPT=$(INSTALL_SCRIPT)" \ + "JC1FLAGS=$(JC1FLAGS)" \ + "LDFLAGS=$(LDFLAGS)" \ + "LIBCFLAGS=$(LIBCFLAGS)" \ + "LIBCFLAGS_FOR_TARGET=$(LIBCFLAGS_FOR_TARGET)" \ + "MAKE=$(MAKE)" \ + "MAKEINFO=$(MAKEINFO) $(MAKEINFOFLAGS)" \ + "PICFLAG=$(PICFLAG)" \ + "PICFLAG_FOR_TARGET=$(PICFLAG_FOR_TARGET)" \ + "SHELL=$(SHELL)" \ + "RUNTESTFLAGS=$(RUNTESTFLAGS)" \ + "exec_prefix=$(exec_prefix)" \ + "infodir=$(infodir)" \ + "libdir=$(libdir)" \ + "prefix=$(prefix)" \ + "includedir=$(includedir)" \ + "AR=$(AR)" \ + "AS=$(AS)" \ + "LD=$(LD)" \ + "LIBCFLAGS=$(LIBCFLAGS)" \ + "NM=$(NM)" \ + "PICFLAG=$(PICFLAG)" \ + "RANLIB=$(RANLIB)" \ + "DESTDIR=$(DESTDIR)" + +MAKEOVERRIDES = + diff --git a/libmpx/mpxwrap/libtool-version b/libmpx/mpxwrap/libtool-version new file mode 100644 index 0000000..bfe84c8 --- /dev/null +++ b/libmpx/mpxwrap/libtool-version @@ -0,0 +1,6 @@ +# This file is used to maintain libtool version info for libmpxintr. See +# the libtool manual to understand the meaning of the fields. This is +# a separate file so that version updates don't involve re-running +# automake. +# CURRENT:REVISION:AGE +1:0:0 diff --git a/libmpx/mpxwrap/mpx_wrappers.c b/libmpx/mpxwrap/mpx_wrappers.c new file mode 100644 index 0000000..5285da4 --- /dev/null +++ b/libmpx/mpxwrap/mpx_wrappers.c @@ -0,0 +1,238 @@ +#include "stdlib.h" +#include "string.h" +#include <sys/mman.h> + +void * +__mpx_wrapper_malloc (size_t size) +{ + void *p = (void *)malloc (size); + if (!p) return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, size); +} + + +void * +__mpx_wrapper_mmap (void *addr, size_t length, int prot, int flags, + int fd, off_t offset) +{ + void *p = mmap (addr, length, prot, flags, fd, offset); + if (!p) return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, length); +} + +void * +__mpx_wrapper_realloc (void *ptr, size_t n) +{ + if (!ptr) + return __mpx_wrapper_malloc (n); + + /* We don't kwnow how much data is copied by realloc + and therefore may check only lower bounds. */ + __bnd_chk_ptr_lbounds (ptr); + ptr = realloc (ptr, n); + + if (!ptr) + return __bnd_null_ptr_bounds (ptr); + + return __bnd_set_ptr_bounds (ptr, n); +} + +void * +__mpx_wrapper_calloc (size_t n_elements, size_t element_size) +{ + void *p = calloc (n_elements, element_size); + if (!p) + return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, n_elements * element_size); +} + +void * +__mpx_wrapper_memset (void *dstpp, int c, size_t len) +{ + if (len > 0) + { + __bnd_chk_ptr_bounds (dstpp, len); + memset (dstpp, c, len); + } + return dstpp; +} + +void +__mpx_wrapper_bzero (void *dst, size_t len) +{ + __mpx_wrapper_memset (dst, 0, len); +} + +void * +__mpx_wrapper_memmove (void *dst, const void *src, size_t n) +{ + const char *s = (const char*)src; + char *d = (char*)dst; + void *ret = dst; + size_t offset_src = ((size_t) s) & (sizeof (void *) - 1); + size_t offset_dst = ((size_t) d) & (sizeof (void *) - 1); + + __bnd_chk_ptr_bounds (dst, n); + __bnd_chk_ptr_bounds (src, n); + + /* Different alignment means that even if + pointers exist in memory, we don't how + pointers are aligned and therefore cann't + copy bounds anyway. */ + if (offset_src != offset_dst) + memmove (dst, src, n); + else + { + if (s < d) + { + d += n; + s += n; + offset_src = (offset_src + n) & (sizeof (void *) -1); + while (n-- && offset_src--) + *--d = *--s; + n++; + if (!n) + return ret; + void **d1 = (void **)d; + void **s1 = (void **)s; + /* This loop will also copy bounds. */ + while (n >= sizeof (void *)) + { + n -= sizeof (void *); + *--d1 = *--s1; + } + s = (char *)s1; + d = (char *)d1; + while (n--) + *--d = *--s; + } + else + { + offset_src = sizeof (void *) - offset_src; + while (n-- && offset_src--) + *d++ = *s++; + n++; + if (!n) + return ret; + void **d1 = (void **)d; + void **s1 = (void **)s; + /* This loop will also copy bounds. */ + while (n >= sizeof (void *)) + { + n -= sizeof (void *); + *d1++ = *s1++; + } + s = (char *)s1; + d = (char *)d1; + while (n--) + *d++ = *s++; + } + } + return ret; +} + + +void * +__mpx_wrapper_memcpy (void *dst, const void *src, size_t n) +{ + return __mpx_wrapper_memmove (dst, src, n); +} + +void * +__mpx_wrapper_mempcpy (void *dst, const void *src, size_t n) +{ + return (char *)__mpx_wrapper_memcpy (dst, src, n) + n; +} + +char * +__mpx_wrapper_strncat (char *dst, const char *src, size_t n) +{ + size_t dst_size = strlen (dst); + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + strncat (dst, src, n); + + return dst; +} + +char * +__mpx_wrapper_strcat (char *dst, const char *src) +{ + size_t dst_size = strlen (dst); + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + strcat (dst, src); + + return dst; +} + +char * +__mpx_wrapper_stpcpy (char *dst, const char *src) +{ + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + memcpy (dst, src, src_size + 1); + + return dst + src_size; +} + +char * +__mpx_wrapper_stpncpy (char *dst, const char *src, size_t n) +{ + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, n); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + return dst + src_size; +} + +char * +__mpx_wrapper_strcpy (char *dst, const char *src) +{ + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + memcpy (dst, src, src_size + 1); + + return dst; +} + +char * +__mpx_wrapper_strncpy (char *dst, const char *src, size_t n) +{ + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, n); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + return dst; +} + +size_t +__mpx_wrapper_strlen (const char *s) +{ + size_t length = strlen (s); + __bnd_chk_ptr_bounds (s, length); + return length; +} ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-11-14 17:29 [PATCH, MPX wrappers 1/3] Add MPX wrappers library Ilya Enkovich @ 2014-11-14 19:20 ` Joseph Myers 2014-11-15 8:51 ` Jeff Law 1 sibling, 0 replies; 15+ messages in thread From: Joseph Myers @ 2014-11-14 19:20 UTC (permalink / raw) To: Ilya Enkovich; +Cc: gcc-patches On Fri, 14 Nov 2014, Ilya Enkovich wrote: > Hi, > > This patch introduces a simple library with several wrappers to be used > with MPX and Pointer Bounds Checker. Wrappers allow to obtain, copy and > just keep alive bounds whrough widely use library calls. It > significantly increases checking quality. Please use symbol versioning in the new library (so the exported functions are versioned at some symbol version, and any functions added in future for GCC 6 that aren't in the version shipped with GCC 5 get a new symbol version, and so on). -- Joseph S. Myers joseph@codesourcery.com ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-11-14 17:29 [PATCH, MPX wrappers 1/3] Add MPX wrappers library Ilya Enkovich 2014-11-14 19:20 ` Joseph Myers @ 2014-11-15 8:51 ` Jeff Law 2014-11-18 16:53 ` Ilya Enkovich 1 sibling, 1 reply; 15+ messages in thread From: Jeff Law @ 2014-11-15 8:51 UTC (permalink / raw) To: Ilya Enkovich, gcc-patches On 11/14/14 10:26, Ilya Enkovich wrote: > Hi, > > This patch introduces a simple library with several wrappers to be used with MPX and Pointer Bounds Checker. Wrappers allow to obtain, copy and just keep alive bounds whrough widely use library calls. It significantly increases checking quality. > > Thanks, > Ilya > -- > gcc/ > > 2014-11-14 Ilya Enkovich <ilya.enkovich@intel.com> > > * gcc.c (MPX_SPEC): Add wrappers library. > > libmpx/ > > 2014-11-14 Ilya Enkovich <ilya.enkovich@intel.com> > > * Makefile.am (SUBDIRS): New. > (MAKEOVERRIDES): New. > * Makefile.in: Regenerate. > * configure.ac: Add mpxintr/Makefile to config > files. > * configure: Regenerate. > * mpxwrap/Makefile.am: New. > * mpxwrap/Makefile.in: New. > * mpxwrap/libtool-version: New. > * mpxwrap/mpx_wrappers.cc: New. As Joseph mentioned, symbol versioning. Anytime a target side library is added to GCC, it should be properly versioned. Don't forget copyright headers in the new files. Remember it has to be suitable for embeddeding in the target without infecting the target with the GPL. LGPL or GPL + exception clause seem the most appropriate to me. Jeff ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-11-15 8:51 ` Jeff Law @ 2014-11-18 16:53 ` Ilya Enkovich 2014-11-18 21:52 ` Jeff Law 0 siblings, 1 reply; 15+ messages in thread From: Ilya Enkovich @ 2014-11-18 16:53 UTC (permalink / raw) To: Jeff Law; +Cc: gcc-patches On 15 Nov 00:10, Jeff Law wrote: > On 11/14/14 10:26, Ilya Enkovich wrote: > >Hi, > > > >This patch introduces a simple library with several wrappers to be used with MPX and Pointer Bounds Checker. Wrappers allow to obtain, copy and just keep alive bounds whrough widely use library calls. It significantly increases checking quality. > > > >Thanks, > >Ilya > >-- > >gcc/ > > > >2014-11-14 Ilya Enkovich <ilya.enkovich@intel.com> > > > > * gcc.c (MPX_SPEC): Add wrappers library. > > > >libmpx/ > > > >2014-11-14 Ilya Enkovich <ilya.enkovich@intel.com> > > > > * Makefile.am (SUBDIRS): New. > > (MAKEOVERRIDES): New. > > * Makefile.in: Regenerate. > > * configure.ac: Add mpxintr/Makefile to config > > files. > > * configure: Regenerate. > > * mpxwrap/Makefile.am: New. > > * mpxwrap/Makefile.in: New. > > * mpxwrap/libtool-version: New. > > * mpxwrap/mpx_wrappers.cc: New. > As Joseph mentioned, symbol versioning. Anytime a target side > library is added to GCC, it should be properly versioned. > > Don't forget copyright headers in the new files. Remember it has to > be suitable for embeddeding in the target without infecting the > target with the GPL. LGPL or GPL + exception clause seem the most > appropriate to me. > > > Jeff > Thank you for review! Here is a version with license and versioning added. Thanks, Ilya -- gcc/ 2014-11-18 Ilya Enkovich <ilya.enkovich@intel.com> * gcc.c (MPX_SPEC): Add wrappers library. libmpx/ 2014-11-18 Ilya Enkovich <ilya.enkovich@intel.com> * Makefile.am (SUBDIRS): New. (MAKEOVERRIDES): New. * Makefile.in: Regenerate. * configure.ac: Add mpxintr/Makefile to config files. * configure: Regenerate. * mpxwrap/Makefile.am: New. * mpxwrap/Makefile.in: New. * mpxwrap/libtool-version: New. * mpxwrap/mpx_wrappers.cc: New. * mpxwrap/libmpxwrappers.map: New. diff --git a/gcc/gcc.c b/gcc/gcc.c index 8ee7bba..56d7e79 100644 --- a/gcc/gcc.c +++ b/gcc/gcc.c @@ -811,9 +811,10 @@ proper position among the other output files. */ #ifndef MPX_SPEC #define MPX_SPEC "\ -%{!nostdlib:%{!nodefaultlibs:%{mmpx:\ +%{!nostdlib:%{!nodefaultlibs:%{mmpx:%{fcheck-pointer-bounds:\ %{static:%nMPX runtime is disabled due to -static used}\ - %{!static:-lmpx}}}}" + %{!static:-lmpx}\ + %{!fno-chkp-use-wrappers:-lmpxwrappers}}}}}" #endif /* -u* was put back because both BSD and SysV seem to support it. */ diff --git a/libmpx/Makefile.am b/libmpx/Makefile.am index c6b479f..553ad30 100644 --- a/libmpx/Makefile.am +++ b/libmpx/Makefile.am @@ -1,6 +1,9 @@ ACLOCAL_AMFLAGS = -I .. -I ../config -SUBDIRS = mpxrt +SUBDIRS = mpxrt mpxwrap + +## May be used by toolexeclibdir. +gcc_version := $(shell cat $(top_srcdir)/../gcc/BASE-VER) # Work around what appears to be a GNU make bug handling MAKEFLAGS # values defined in terms of make variables, as is the case for CC and @@ -39,3 +42,5 @@ AM_MAKEFLAGS = \ "PICFLAG=$(PICFLAG)" \ "RANLIB=$(RANLIB)" \ "DESTDIR=$(DESTDIR)" + +MAKEOVERRIDES = diff --git a/libmpx/configure.ac b/libmpx/configure.ac index 9a761c4..f3abead 100644 --- a/libmpx/configure.ac +++ b/libmpx/configure.ac @@ -110,7 +110,7 @@ fi AC_CONFIG_FILES([Makefile]) AC_CONFIG_HEADERS(config.h) -AC_CONFIG_FILES(AC_FOREACH([DIR], [mpxrt], [DIR/Makefile]), +AC_CONFIG_FILES(AC_FOREACH([DIR], [mpxrt mpxwrap], [DIR/Makefile ]), [cat > vpsed$$ << \_EOF s!`test -f '$<' || echo '$(srcdir)/'`!! _EOF diff --git a/libmpx/mpxwrap/Makefile.am b/libmpx/mpxwrap/Makefile.am new file mode 100644 index 0000000..c254d9b --- /dev/null +++ b/libmpx/mpxwrap/Makefile.am @@ -0,0 +1,54 @@ +ALCLOCAL_AMFLAGS = -I .. -I ../config + +# May be used by toolexeclibdir. +gcc_version := $(shell cat $(top_srcdir)/../gcc/BASE-VER) + +libmpxwrappers_la_CFLAGS = -fcheck-pointer-bounds -mmpx -fno-chkp-check-read \ + -fno-chkp-check-write -fno-chkp-use-wrappers +libmpxwrappers_la_DEPENDENCIES = libmpxwrappers.map +libmpxwrappers_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libmpxwrappers.map + +toolexeclib_LTLIBRARIES = libmpxwrappers.la + +libmpxwrappers_la_SOURCES = mpx_wrappers.c + +# Work around what appears to be a GNU make bug handling MAKEFLAGS +# values defined in terms of make variables, as is the case for CC and +# friends when we are called from the top level Makefile. +AM_MAKEFLAGS = \ + "AR_FLAGS=$(AR_FLAGS)" \ + "CC_FOR_BUILD=$(CC_FOR_BUILD)" \ + "CFLAGS=$(CFLAGS)" \ + "CXXFLAGS=$(CXXFLAGS)" \ + "CFLAGS_FOR_BUILD=$(CFLAGS_FOR_BUILD)" \ + "CFLAGS_FOR_TARGET=$(CFLAGS_FOR_TARGET)" \ + "INSTALL=$(INSTALL)" \ + "INSTALL_DATA=$(INSTALL_DATA)" \ + "INSTALL_PROGRAM=$(INSTALL_PROGRAM)" \ + "INSTALL_SCRIPT=$(INSTALL_SCRIPT)" \ + "JC1FLAGS=$(JC1FLAGS)" \ + "LDFLAGS=$(LDFLAGS)" \ + "LIBCFLAGS=$(LIBCFLAGS)" \ + "LIBCFLAGS_FOR_TARGET=$(LIBCFLAGS_FOR_TARGET)" \ + "MAKE=$(MAKE)" \ + "MAKEINFO=$(MAKEINFO) $(MAKEINFOFLAGS)" \ + "PICFLAG=$(PICFLAG)" \ + "PICFLAG_FOR_TARGET=$(PICFLAG_FOR_TARGET)" \ + "SHELL=$(SHELL)" \ + "RUNTESTFLAGS=$(RUNTESTFLAGS)" \ + "exec_prefix=$(exec_prefix)" \ + "infodir=$(infodir)" \ + "libdir=$(libdir)" \ + "prefix=$(prefix)" \ + "includedir=$(includedir)" \ + "AR=$(AR)" \ + "AS=$(AS)" \ + "LD=$(LD)" \ + "LIBCFLAGS=$(LIBCFLAGS)" \ + "NM=$(NM)" \ + "PICFLAG=$(PICFLAG)" \ + "RANLIB=$(RANLIB)" \ + "DESTDIR=$(DESTDIR)" + +MAKEOVERRIDES = + diff --git a/libmpx/mpxwrap/libmpxwrappers.map b/libmpx/mpxwrap/libmpxwrappers.map new file mode 100644 index 0000000..c2912a7 --- /dev/null +++ b/libmpx/mpxwrap/libmpxwrappers.map @@ -0,0 +1,23 @@ +LIBMPXWRAPPERS_1.0 +{ + global: + __mpx_wrapper_malloc; + __mpx_wrapper_mmap; + __mpx_wrapper_realloc; + __mpx_wrapper_calloc; + __mpx_wrapper_memset; + __mpx_wrapper_bzero; + __mpx_wrapper_memmove; + __mpx_wrapper_memcpy; + __mpx_wrapper_mempcpy; + __mpx_wrapper_strncat; + __mpx_wrapper_strcat; + __mpx_wrapper_stpcpy; + __mpx_wrapper_stpncpy; + __mpx_wrapper_strcpy; + __mpx_wrapper_strncpy; + __mpx_wrapper_strlen; + + local: + *; +}; diff --git a/libmpx/mpxwrap/libtool-version b/libmpx/mpxwrap/libtool-version new file mode 100644 index 0000000..bfe84c8 --- /dev/null +++ b/libmpx/mpxwrap/libtool-version @@ -0,0 +1,6 @@ +# This file is used to maintain libtool version info for libmpxintr. See +# the libtool manual to understand the meaning of the fields. This is +# a separate file so that version updates don't involve re-running +# automake. +# CURRENT:REVISION:AGE +1:0:0 diff --git a/libmpx/mpxwrap/mpx_wrappers.c b/libmpx/mpxwrap/mpx_wrappers.c new file mode 100644 index 0000000..bcff80f --- /dev/null +++ b/libmpx/mpxwrap/mpx_wrappers.c @@ -0,0 +1,258 @@ +/* MPX Wrappers Library + Copyright (C) 2014 Free Software Foundation, Inc. + Contributed by Ilya Enkovich (ilya.enkovich@intel.com) + + This file is part of the libmpxwrappers library. + + This 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. + + This 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 this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ + +#include "stdlib.h" +#include "string.h" +#include <sys/mman.h> + +void * +__mpx_wrapper_malloc (size_t size) +{ + void *p = (void *)malloc (size); + if (!p) return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, size); +} + + +void * +__mpx_wrapper_mmap (void *addr, size_t length, int prot, int flags, + int fd, off_t offset) +{ + void *p = mmap (addr, length, prot, flags, fd, offset); + if (!p) return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, length); +} + +void * +__mpx_wrapper_realloc (void *ptr, size_t n) +{ + if (!ptr) + return __mpx_wrapper_malloc (n); + + /* We don't kwnow how much data is copied by realloc + and therefore may check only lower bounds. */ + __bnd_chk_ptr_lbounds (ptr); + ptr = realloc (ptr, n); + + if (!ptr) + return __bnd_null_ptr_bounds (ptr); + + return __bnd_set_ptr_bounds (ptr, n); +} + +void * +__mpx_wrapper_calloc (size_t n_elements, size_t element_size) +{ + void *p = calloc (n_elements, element_size); + if (!p) + return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, n_elements * element_size); +} + +void * +__mpx_wrapper_memset (void *dstpp, int c, size_t len) +{ + if (len > 0) + { + __bnd_chk_ptr_bounds (dstpp, len); + memset (dstpp, c, len); + } + return dstpp; +} + +void +__mpx_wrapper_bzero (void *dst, size_t len) +{ + __mpx_wrapper_memset (dst, 0, len); +} + +void * +__mpx_wrapper_memmove (void *dst, const void *src, size_t n) +{ + const char *s = (const char*)src; + char *d = (char*)dst; + void *ret = dst; + size_t offset_src = ((size_t) s) & (sizeof (void *) - 1); + size_t offset_dst = ((size_t) d) & (sizeof (void *) - 1); + + __bnd_chk_ptr_bounds (dst, n); + __bnd_chk_ptr_bounds (src, n); + + /* Different alignment means that even if + pointers exist in memory, we don't how + pointers are aligned and therefore cann't + copy bounds anyway. */ + if (offset_src != offset_dst) + memmove (dst, src, n); + else + { + if (s < d) + { + d += n; + s += n; + offset_src = (offset_src + n) & (sizeof (void *) -1); + while (n-- && offset_src--) + *--d = *--s; + n++; + if (!n) + return ret; + void **d1 = (void **)d; + void **s1 = (void **)s; + /* This loop will also copy bounds. */ + while (n >= sizeof (void *)) + { + n -= sizeof (void *); + *--d1 = *--s1; + } + s = (char *)s1; + d = (char *)d1; + while (n--) + *--d = *--s; + } + else + { + offset_src = sizeof (void *) - offset_src; + while (n-- && offset_src--) + *d++ = *s++; + n++; + if (!n) + return ret; + void **d1 = (void **)d; + void **s1 = (void **)s; + /* This loop will also copy bounds. */ + while (n >= sizeof (void *)) + { + n -= sizeof (void *); + *d1++ = *s1++; + } + s = (char *)s1; + d = (char *)d1; + while (n--) + *d++ = *s++; + } + } + return ret; +} + + +void * +__mpx_wrapper_memcpy (void *dst, const void *src, size_t n) +{ + return __mpx_wrapper_memmove (dst, src, n); +} + +void * +__mpx_wrapper_mempcpy (void *dst, const void *src, size_t n) +{ + return (char *)__mpx_wrapper_memcpy (dst, src, n) + n; +} + +char * +__mpx_wrapper_strncat (char *dst, const char *src, size_t n) +{ + size_t dst_size = strlen (dst); + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + strncat (dst, src, n); + + return dst; +} + +char * +__mpx_wrapper_strcat (char *dst, const char *src) +{ + size_t dst_size = strlen (dst); + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + strcat (dst, src); + + return dst; +} + +char * +__mpx_wrapper_stpcpy (char *dst, const char *src) +{ + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + memcpy (dst, src, src_size + 1); + + return dst + src_size; +} + +char * +__mpx_wrapper_stpncpy (char *dst, const char *src, size_t n) +{ + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, n); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + return dst + src_size; +} + +char * +__mpx_wrapper_strcpy (char *dst, const char *src) +{ + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + memcpy (dst, src, src_size + 1); + + return dst; +} + +char * +__mpx_wrapper_strncpy (char *dst, const char *src, size_t n) +{ + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, n); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + return dst; +} + +size_t +__mpx_wrapper_strlen (const char *s) +{ + size_t length = strlen (s); + __bnd_chk_ptr_bounds (s, length); + return length; +} ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-11-18 16:53 ` Ilya Enkovich @ 2014-11-18 21:52 ` Jeff Law 2014-11-21 16:07 ` Ilya Enkovich 0 siblings, 1 reply; 15+ messages in thread From: Jeff Law @ 2014-11-18 21:52 UTC (permalink / raw) To: Ilya Enkovich; +Cc: gcc-patches On 11/18/14 09:48, Ilya Enkovich wrote: > On 15 Nov 00:10, Jeff Law wrote: >> On 11/14/14 10:26, Ilya Enkovich wrote: >>> Hi, >>> >>> This patch introduces a simple library with several wrappers to be used with MPX and Pointer Bounds Checker. Wrappers allow to obtain, copy and just keep alive bounds whrough widely use library calls. It significantly increases checking quality. >>> >>> Thanks, >>> Ilya >>> -- >>> gcc/ >>> >>> 2014-11-14 Ilya Enkovich <ilya.enkovich@intel.com> >>> >>> * gcc.c (MPX_SPEC): Add wrappers library. >>> >>> libmpx/ >>> >>> 2014-11-14 Ilya Enkovich <ilya.enkovich@intel.com> >>> >>> * Makefile.am (SUBDIRS): New. >>> (MAKEOVERRIDES): New. >>> * Makefile.in: Regenerate. >>> * configure.ac: Add mpxintr/Makefile to config >>> files. >>> * configure: Regenerate. >>> * mpxwrap/Makefile.am: New. >>> * mpxwrap/Makefile.in: New. >>> * mpxwrap/libtool-version: New. >>> * mpxwrap/mpx_wrappers.cc: New. >> As Joseph mentioned, symbol versioning. Anytime a target side >> library is added to GCC, it should be properly versioned. >> >> Don't forget copyright headers in the new files. Remember it has to >> be suitable for embeddeding in the target without infecting the >> target with the GPL. LGPL or GPL + exception clause seem the most >> appropriate to me. >> >> >> Jeff >> > > Thank you for review! Here is a version with license and versioning added. > > Thanks, > Ilya > -- > gcc/ > > 2014-11-18 Ilya Enkovich <ilya.enkovich@intel.com> > > * gcc.c (MPX_SPEC): Add wrappers library. > > libmpx/ > > 2014-11-18 Ilya Enkovich <ilya.enkovich@intel.com> > > * Makefile.am (SUBDIRS): New. > (MAKEOVERRIDES): New. > * Makefile.in: Regenerate. > * configure.ac: Add mpxintr/Makefile to config > files. > * configure: Regenerate. > * mpxwrap/Makefile.am: New. > * mpxwrap/Makefile.in: New. > * mpxwrap/libtool-version: New. > * mpxwrap/mpx_wrappers.cc: New. > * mpxwrap/libmpxwrappers.map: New. OK. Jeff ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-11-18 21:52 ` Jeff Law @ 2014-11-21 16:07 ` Ilya Enkovich 2014-11-22 0:27 ` Joseph Myers 0 siblings, 1 reply; 15+ messages in thread From: Ilya Enkovich @ 2014-11-21 16:07 UTC (permalink / raw) To: Jeff Law; +Cc: gcc-patches On 18 Nov 14:15, Jeff Law wrote: > On 11/18/14 09:48, Ilya Enkovich wrote: > >On 15 Nov 00:10, Jeff Law wrote: > >>On 11/14/14 10:26, Ilya Enkovich wrote: > >>>Hi, > >>> > >>>This patch introduces a simple library with several wrappers to be used with MPX and Pointer Bounds Checker. Wrappers allow to obtain, copy and just keep alive bounds whrough widely use library calls. It significantly increases checking quality. > >>> > >>>Thanks, > >>>Ilya > >>>-- > >>>gcc/ > >>> > >>>2014-11-14 Ilya Enkovich <ilya.enkovich@intel.com> > >>> > >>> * gcc.c (MPX_SPEC): Add wrappers library. > >>> > >>>libmpx/ > >>> > >>>2014-11-14 Ilya Enkovich <ilya.enkovich@intel.com> > >>> > >>> * Makefile.am (SUBDIRS): New. > >>> (MAKEOVERRIDES): New. > >>> * Makefile.in: Regenerate. > >>> * configure.ac: Add mpxintr/Makefile to config > >>> files. > >>> * configure: Regenerate. > >>> * mpxwrap/Makefile.am: New. > >>> * mpxwrap/Makefile.in: New. > >>> * mpxwrap/libtool-version: New. > >>> * mpxwrap/mpx_wrappers.cc: New. > >>As Joseph mentioned, symbol versioning. Anytime a target side > >>library is added to GCC, it should be properly versioned. > >> > >>Don't forget copyright headers in the new files. Remember it has to > >>be suitable for embeddeding in the target without infecting the > >>target with the GPL. LGPL or GPL + exception clause seem the most > >>appropriate to me. > >> > >> > >>Jeff > >> > > > >Thank you for review! Here is a version with license and versioning added. > > > >Thanks, > >Ilya > >-- > >gcc/ > > > >2014-11-18 Ilya Enkovich <ilya.enkovich@intel.com> > > > > * gcc.c (MPX_SPEC): Add wrappers library. > > > >libmpx/ > > > >2014-11-18 Ilya Enkovich <ilya.enkovich@intel.com> > > > > * Makefile.am (SUBDIRS): New. > > (MAKEOVERRIDES): New. > > * Makefile.in: Regenerate. > > * configure.ac: Add mpxintr/Makefile to config > > files. > > * configure: Regenerate. > > * mpxwrap/Makefile.am: New. > > * mpxwrap/Makefile.in: New. > > * mpxwrap/libtool-version: New. > > * mpxwrap/mpx_wrappers.cc: New. > > * mpxwrap/libmpxwrappers.map: New. > OK. > Jeff > Hi, There is a missing check in libmpx configure. We may try to build mpxwrappers when binutils don't support MPX and thus get build failure. I added a check for MPX support in used assembler and mpxwrappers library is now built conditionally. Since the latest version of runtime library supports static link, I also supported -static-libmpxwrappers option. Does it look OK? Thanks, Ilya -- gcc/ 2014-11-21 Ilya Enkovich <ilya.enkovich@intel.com> * gcc.c (LIBMPX_WRAPPERSSPEC): New. (MPX_SPEC): Add wrappers library. * c-family/c.opt (static-libmpxwrappers): New. libmpx/ 2014-11-21 Ilya Enkovich <ilya.enkovich@intel.com> * Makefile.am (SUBDIRS): Add mpxwrap when used AS supports MPX. (MAKEOVERRIDES): New. * Makefile.in: Regenerate. * configure.ac: Check AS supports MPX. Add mpxintr/Makefile to config files. * configure: Regenerate. * mpxwrap/Makefile.am: New. * mpxwrap/Makefile.in: New. * mpxwrap/libtool-version: New. * mpxwrap/mpx_wrappers.cc: New. * mpxwrap/libmpxwrappers.map: New. diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 8f5d76c..283c632 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1043,6 +1043,9 @@ Instrument only functions marked with bnd_instrument attribute. static-libmpx Driver +static-libmpxwrappers +Driver + fcilkplus C ObjC C++ ObjC++ LTO Report Var(flag_cilkplus) Init(0) Enable Cilk Plus diff --git a/gcc/gcc.c b/gcc/gcc.c index 75e5767..aa8c9a3 100644 --- a/gcc/gcc.c +++ b/gcc/gcc.c @@ -828,9 +828,23 @@ proper position among the other output files. */ #endif #endif +#ifndef LIBMPXWRAPPERS_SPEC +#if defined(HAVE_LD_STATIC_DYNAMIC) +#define LIBMPXWRAPPERS_SPEC "\ +%{mmpx:%{fcheck-pointer-bounds:%{!fno-chkp-use-wrappers:\ + %{static:-lmpxwrappers}\ + %{!static:%{static-libmpxwrappers:" LD_STATIC_OPTION " --whole-archive}\ + -lmpxwrappers %{static-libmpxwrappers:--no-whole-archive "\ + LD_DYNAMIC_OPTION "}}}}}" +#else +#define LIBMPXWRAPPERS_SPEC "\ +%{mmpx:%{fcheck-pointer-bounds:{!fno-chkp-use-wrappers:-lmpxwrappers}}}" +#endif +#endif + #ifndef MPX_SPEC #define MPX_SPEC "\ -%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC "}}" +%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC LIBMPXWRAPPERS_SPEC "}}" #endif /* -u* was put back because both BSD and SysV seem to support it. */ diff --git a/libmpx/Makefile.am b/libmpx/Makefile.am index 6cee4ac..bd0a8b6 100644 --- a/libmpx/Makefile.am +++ b/libmpx/Makefile.am @@ -2,6 +2,9 @@ ACLOCAL_AMFLAGS = -I .. -I ../config if LIBMPX_SUPPORTED SUBDIRS = mpxrt +if MPX_AS_SUPPORTED +SUBDIRS += mpxwrap +endif nodist_toolexeclib_HEADERS = libmpx.spec endif @@ -45,3 +48,5 @@ AM_MAKEFLAGS = \ "PICFLAG=$(PICFLAG)" \ "RANLIB=$(RANLIB)" \ "DESTDIR=$(DESTDIR)" + +MAKEOVERRIDES = diff --git a/libmpx/configure.ac b/libmpx/configure.ac index bd7a5eb..180503c 100644 --- a/libmpx/configure.ac +++ b/libmpx/configure.ac @@ -93,6 +93,18 @@ AC_CHECK_TOOL(AS, as) AC_CHECK_TOOL(AR, ar) AC_CHECK_TOOL(RANLIB, ranlib, :) +# Check we may build wrappers library +echo "test: bndmov %bnd0, %bnd1" > conftest.s +if AC_TRY_COMMAND([$AS -o conftest.o conftest.s 1>&AS_MESSAGE_LOG_FD]) +then + mpx_as=yes +else + mpx_as=no + echo "configure: no MPX support fo as" >&AS_MESSAGE_LOG_FD +fi +rm -f conftest.o conftest.s +AM_CONDITIONAL(MPX_AS_SUPPORTED, [test "x$mpx_as" = "xyes"]) + # Configure libtool AC_LIBTOOL_DLOPEN AM_PROG_LIBTOOL @@ -110,7 +122,7 @@ fi AC_CONFIG_FILES([Makefile libmpx.spec]) AC_CONFIG_HEADERS(config.h) -AC_CONFIG_FILES(AC_FOREACH([DIR], [mpxrt], [DIR/Makefile]), +AC_CONFIG_FILES(AC_FOREACH([DIR], [mpxrt mpxwrap], [DIR/Makefile ]), [cat > vpsed$$ << \_EOF s!`test -f '$<' || echo '$(srcdir)/'`!! _EOF diff --git a/libmpx/mpxwrap/Makefile.am b/libmpx/mpxwrap/Makefile.am new file mode 100644 index 0000000..c254d9b --- /dev/null +++ b/libmpx/mpxwrap/Makefile.am @@ -0,0 +1,54 @@ +ALCLOCAL_AMFLAGS = -I .. -I ../config + +# May be used by toolexeclibdir. +gcc_version := $(shell cat $(top_srcdir)/../gcc/BASE-VER) + +libmpxwrappers_la_CFLAGS = -fcheck-pointer-bounds -mmpx -fno-chkp-check-read \ + -fno-chkp-check-write -fno-chkp-use-wrappers +libmpxwrappers_la_DEPENDENCIES = libmpxwrappers.map +libmpxwrappers_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libmpxwrappers.map + +toolexeclib_LTLIBRARIES = libmpxwrappers.la + +libmpxwrappers_la_SOURCES = mpx_wrappers.c + +# Work around what appears to be a GNU make bug handling MAKEFLAGS +# values defined in terms of make variables, as is the case for CC and +# friends when we are called from the top level Makefile. +AM_MAKEFLAGS = \ + "AR_FLAGS=$(AR_FLAGS)" \ + "CC_FOR_BUILD=$(CC_FOR_BUILD)" \ + "CFLAGS=$(CFLAGS)" \ + "CXXFLAGS=$(CXXFLAGS)" \ + "CFLAGS_FOR_BUILD=$(CFLAGS_FOR_BUILD)" \ + "CFLAGS_FOR_TARGET=$(CFLAGS_FOR_TARGET)" \ + "INSTALL=$(INSTALL)" \ + "INSTALL_DATA=$(INSTALL_DATA)" \ + "INSTALL_PROGRAM=$(INSTALL_PROGRAM)" \ + "INSTALL_SCRIPT=$(INSTALL_SCRIPT)" \ + "JC1FLAGS=$(JC1FLAGS)" \ + "LDFLAGS=$(LDFLAGS)" \ + "LIBCFLAGS=$(LIBCFLAGS)" \ + "LIBCFLAGS_FOR_TARGET=$(LIBCFLAGS_FOR_TARGET)" \ + "MAKE=$(MAKE)" \ + "MAKEINFO=$(MAKEINFO) $(MAKEINFOFLAGS)" \ + "PICFLAG=$(PICFLAG)" \ + "PICFLAG_FOR_TARGET=$(PICFLAG_FOR_TARGET)" \ + "SHELL=$(SHELL)" \ + "RUNTESTFLAGS=$(RUNTESTFLAGS)" \ + "exec_prefix=$(exec_prefix)" \ + "infodir=$(infodir)" \ + "libdir=$(libdir)" \ + "prefix=$(prefix)" \ + "includedir=$(includedir)" \ + "AR=$(AR)" \ + "AS=$(AS)" \ + "LD=$(LD)" \ + "LIBCFLAGS=$(LIBCFLAGS)" \ + "NM=$(NM)" \ + "PICFLAG=$(PICFLAG)" \ + "RANLIB=$(RANLIB)" \ + "DESTDIR=$(DESTDIR)" + +MAKEOVERRIDES = + diff --git a/libmpx/mpxwrap/libmpxwrappers.map b/libmpx/mpxwrap/libmpxwrappers.map new file mode 100644 index 0000000..c2912a7 --- /dev/null +++ b/libmpx/mpxwrap/libmpxwrappers.map @@ -0,0 +1,23 @@ +LIBMPXWRAPPERS_1.0 +{ + global: + __mpx_wrapper_malloc; + __mpx_wrapper_mmap; + __mpx_wrapper_realloc; + __mpx_wrapper_calloc; + __mpx_wrapper_memset; + __mpx_wrapper_bzero; + __mpx_wrapper_memmove; + __mpx_wrapper_memcpy; + __mpx_wrapper_mempcpy; + __mpx_wrapper_strncat; + __mpx_wrapper_strcat; + __mpx_wrapper_stpcpy; + __mpx_wrapper_stpncpy; + __mpx_wrapper_strcpy; + __mpx_wrapper_strncpy; + __mpx_wrapper_strlen; + + local: + *; +}; diff --git a/libmpx/mpxwrap/libtool-version b/libmpx/mpxwrap/libtool-version new file mode 100644 index 0000000..bfe84c8 --- /dev/null +++ b/libmpx/mpxwrap/libtool-version @@ -0,0 +1,6 @@ +# This file is used to maintain libtool version info for libmpxintr. See +# the libtool manual to understand the meaning of the fields. This is +# a separate file so that version updates don't involve re-running +# automake. +# CURRENT:REVISION:AGE +1:0:0 diff --git a/libmpx/mpxwrap/mpx_wrappers.c b/libmpx/mpxwrap/mpx_wrappers.c new file mode 100644 index 0000000..bcff80f --- /dev/null +++ b/libmpx/mpxwrap/mpx_wrappers.c @@ -0,0 +1,258 @@ +/* MPX Wrappers Library + Copyright (C) 2014 Free Software Foundation, Inc. + Contributed by Ilya Enkovich (ilya.enkovich@intel.com) + + This file is part of the libmpxwrappers library. + + This 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. + + This 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 this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ + +#include "stdlib.h" +#include "string.h" +#include <sys/mman.h> + +void * +__mpx_wrapper_malloc (size_t size) +{ + void *p = (void *)malloc (size); + if (!p) return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, size); +} + + +void * +__mpx_wrapper_mmap (void *addr, size_t length, int prot, int flags, + int fd, off_t offset) +{ + void *p = mmap (addr, length, prot, flags, fd, offset); + if (!p) return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, length); +} + +void * +__mpx_wrapper_realloc (void *ptr, size_t n) +{ + if (!ptr) + return __mpx_wrapper_malloc (n); + + /* We don't kwnow how much data is copied by realloc + and therefore may check only lower bounds. */ + __bnd_chk_ptr_lbounds (ptr); + ptr = realloc (ptr, n); + + if (!ptr) + return __bnd_null_ptr_bounds (ptr); + + return __bnd_set_ptr_bounds (ptr, n); +} + +void * +__mpx_wrapper_calloc (size_t n_elements, size_t element_size) +{ + void *p = calloc (n_elements, element_size); + if (!p) + return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, n_elements * element_size); +} + +void * +__mpx_wrapper_memset (void *dstpp, int c, size_t len) +{ + if (len > 0) + { + __bnd_chk_ptr_bounds (dstpp, len); + memset (dstpp, c, len); + } + return dstpp; +} + +void +__mpx_wrapper_bzero (void *dst, size_t len) +{ + __mpx_wrapper_memset (dst, 0, len); +} + +void * +__mpx_wrapper_memmove (void *dst, const void *src, size_t n) +{ + const char *s = (const char*)src; + char *d = (char*)dst; + void *ret = dst; + size_t offset_src = ((size_t) s) & (sizeof (void *) - 1); + size_t offset_dst = ((size_t) d) & (sizeof (void *) - 1); + + __bnd_chk_ptr_bounds (dst, n); + __bnd_chk_ptr_bounds (src, n); + + /* Different alignment means that even if + pointers exist in memory, we don't how + pointers are aligned and therefore cann't + copy bounds anyway. */ + if (offset_src != offset_dst) + memmove (dst, src, n); + else + { + if (s < d) + { + d += n; + s += n; + offset_src = (offset_src + n) & (sizeof (void *) -1); + while (n-- && offset_src--) + *--d = *--s; + n++; + if (!n) + return ret; + void **d1 = (void **)d; + void **s1 = (void **)s; + /* This loop will also copy bounds. */ + while (n >= sizeof (void *)) + { + n -= sizeof (void *); + *--d1 = *--s1; + } + s = (char *)s1; + d = (char *)d1; + while (n--) + *--d = *--s; + } + else + { + offset_src = sizeof (void *) - offset_src; + while (n-- && offset_src--) + *d++ = *s++; + n++; + if (!n) + return ret; + void **d1 = (void **)d; + void **s1 = (void **)s; + /* This loop will also copy bounds. */ + while (n >= sizeof (void *)) + { + n -= sizeof (void *); + *d1++ = *s1++; + } + s = (char *)s1; + d = (char *)d1; + while (n--) + *d++ = *s++; + } + } + return ret; +} + + +void * +__mpx_wrapper_memcpy (void *dst, const void *src, size_t n) +{ + return __mpx_wrapper_memmove (dst, src, n); +} + +void * +__mpx_wrapper_mempcpy (void *dst, const void *src, size_t n) +{ + return (char *)__mpx_wrapper_memcpy (dst, src, n) + n; +} + +char * +__mpx_wrapper_strncat (char *dst, const char *src, size_t n) +{ + size_t dst_size = strlen (dst); + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + strncat (dst, src, n); + + return dst; +} + +char * +__mpx_wrapper_strcat (char *dst, const char *src) +{ + size_t dst_size = strlen (dst); + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + strcat (dst, src); + + return dst; +} + +char * +__mpx_wrapper_stpcpy (char *dst, const char *src) +{ + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + memcpy (dst, src, src_size + 1); + + return dst + src_size; +} + +char * +__mpx_wrapper_stpncpy (char *dst, const char *src, size_t n) +{ + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, n); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + return dst + src_size; +} + +char * +__mpx_wrapper_strcpy (char *dst, const char *src) +{ + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + memcpy (dst, src, src_size + 1); + + return dst; +} + +char * +__mpx_wrapper_strncpy (char *dst, const char *src, size_t n) +{ + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, n); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + return dst; +} + +size_t +__mpx_wrapper_strlen (const char *s) +{ + size_t length = strlen (s); + __bnd_chk_ptr_bounds (s, length); + return length; +} ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-11-21 16:07 ` Ilya Enkovich @ 2014-11-22 0:27 ` Joseph Myers 2014-11-24 14:53 ` Ilya Enkovich 0 siblings, 1 reply; 15+ messages in thread From: Joseph Myers @ 2014-11-22 0:27 UTC (permalink / raw) To: Ilya Enkovich; +Cc: Jeff Law, gcc-patches On Fri, 21 Nov 2014, Ilya Enkovich wrote: > * c-family/c.opt (static-libmpxwrappers): New. New options need documenting in invoke.texi. This includes driver options. > diff --git a/libmpx/mpxwrap/mpx_wrappers.c b/libmpx/mpxwrap/mpx_wrappers.c > new file mode 100644 > index 0000000..bcff80f > --- /dev/null > +++ b/libmpx/mpxwrap/mpx_wrappers.c > @@ -0,0 +1,258 @@ > +/* MPX Wrappers Library > + Copyright (C) 2014 Free Software Foundation, Inc. > + Contributed by Ilya Enkovich (ilya.enkovich@intel.com) > + > + This file is part of the libmpxwrappers library. > + > + This 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. > + > + This 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 this library; if not, write to the Free Software > + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ Normally GCC target libraries assigned to the FSF would use GPL+exception rather than LGPL (especially if the library might be linked in statically), to keep predictable what requirements are imposed by linking your program with GCC. libquadmath is an exception because it contains LGPL code not assigned to the FSF. -- Joseph S. Myers joseph@codesourcery.com ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-11-22 0:27 ` Joseph Myers @ 2014-11-24 14:53 ` Ilya Enkovich 2014-12-01 21:16 ` Jeff Law 0 siblings, 1 reply; 15+ messages in thread From: Ilya Enkovich @ 2014-11-24 14:53 UTC (permalink / raw) To: Joseph Myers; +Cc: Jeff Law, gcc-patches On 21 Nov 23:32, Joseph Myers wrote: > On Fri, 21 Nov 2014, Ilya Enkovich wrote: > > > * c-family/c.opt (static-libmpxwrappers): New. > > New options need documenting in invoke.texi. This includes driver > options. > I'll include it into patch with all checker options documented bacause of options cross-references. > > diff --git a/libmpx/mpxwrap/mpx_wrappers.c b/libmpx/mpxwrap/mpx_wrappers.c > > new file mode 100644 > > index 0000000..bcff80f > > --- /dev/null > > +++ b/libmpx/mpxwrap/mpx_wrappers.c > > @@ -0,0 +1,258 @@ > > +/* MPX Wrappers Library > > + Copyright (C) 2014 Free Software Foundation, Inc. > > + Contributed by Ilya Enkovich (ilya.enkovich@intel.com) > > + > > + This file is part of the libmpxwrappers library. > > + > > + This 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. > > + > > + This 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 this library; if not, write to the Free Software > > + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ > > Normally GCC target libraries assigned to the FSF would use GPL+exception > rather than LGPL (especially if the library might be linked in > statically), to keep predictable what requirements are imposed by linking > your program with GCC. libquadmath is an exception because it contains > LGPL code not assigned to the FSF. I'm OK to put it under GPL+exception. > > -- > Joseph S. Myers > joseph@codesourcery.com Here is an updated version. Thanks, Ilya -- gcc/ 2014-11-24 Ilya Enkovich <ilya.enkovich@intel.com> * gcc.c (LIBMPX_WRAPPERSSPEC): New. (MPX_SPEC): Add wrappers library. * c-family/c.opt (static-libmpxwrappers): New. libmpx/ 2014-11-24 Ilya Enkovich <ilya.enkovich@intel.com> * Makefile.am (SUBDIRS): Add mpxwrap when used AS supports MPX. (MAKEOVERRIDES): New. * Makefile.in: Regenerate. * configure.ac: Check AS supports MPX. Add mpxintr/Makefile to config files. * configure: Regenerate. * mpxwrap/Makefile.am: New. * mpxwrap/Makefile.in: New. * mpxwrap/libtool-version: New. * mpxwrap/mpx_wrappers.cc: New. * mpxwrap/libmpxwrappers.map: New. diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 8f5d76c..283c632 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1043,6 +1043,9 @@ Instrument only functions marked with bnd_instrument attribute. static-libmpx Driver +static-libmpxwrappers +Driver + fcilkplus C ObjC C++ ObjC++ LTO Report Var(flag_cilkplus) Init(0) Enable Cilk Plus diff --git a/gcc/gcc.c b/gcc/gcc.c index 75e5767..aa8c9a3 100644 --- a/gcc/gcc.c +++ b/gcc/gcc.c @@ -828,9 +828,23 @@ proper position among the other output files. */ #endif #endif +#ifndef LIBMPXWRAPPERS_SPEC +#if defined(HAVE_LD_STATIC_DYNAMIC) +#define LIBMPXWRAPPERS_SPEC "\ +%{mmpx:%{fcheck-pointer-bounds:%{!fno-chkp-use-wrappers:\ + %{static:-lmpxwrappers}\ + %{!static:%{static-libmpxwrappers:" LD_STATIC_OPTION " --whole-archive}\ + -lmpxwrappers %{static-libmpxwrappers:--no-whole-archive "\ + LD_DYNAMIC_OPTION "}}}}}" +#else +#define LIBMPXWRAPPERS_SPEC "\ +%{mmpx:%{fcheck-pointer-bounds:{!fno-chkp-use-wrappers:-lmpxwrappers}}}" +#endif +#endif + #ifndef MPX_SPEC #define MPX_SPEC "\ -%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC "}}" +%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC LIBMPXWRAPPERS_SPEC "}}" #endif /* -u* was put back because both BSD and SysV seem to support it. */ diff --git a/libmpx/Makefile.am b/libmpx/Makefile.am index 6cee4ac..bd0a8b6 100644 --- a/libmpx/Makefile.am +++ b/libmpx/Makefile.am @@ -2,6 +2,9 @@ ACLOCAL_AMFLAGS = -I .. -I ../config if LIBMPX_SUPPORTED SUBDIRS = mpxrt +if MPX_AS_SUPPORTED +SUBDIRS += mpxwrap +endif nodist_toolexeclib_HEADERS = libmpx.spec endif @@ -45,3 +48,5 @@ AM_MAKEFLAGS = \ "PICFLAG=$(PICFLAG)" \ "RANLIB=$(RANLIB)" \ "DESTDIR=$(DESTDIR)" + +MAKEOVERRIDES = diff --git a/libmpx/configure.ac b/libmpx/configure.ac index dbfad02..4669525 100644 --- a/libmpx/configure.ac +++ b/libmpx/configure.ac @@ -100,6 +100,18 @@ AC_CHECK_TOOL(AS, as) AC_CHECK_TOOL(AR, ar) AC_CHECK_TOOL(RANLIB, ranlib, :) +# Check we may build wrappers library +echo "test: bndmov %bnd0, %bnd1" > conftest.s +if AC_TRY_COMMAND([$AS -o conftest.o conftest.s 1>&AS_MESSAGE_LOG_FD]) +then + mpx_as=yes +else + mpx_as=no + echo "configure: no MPX support fo as" >&AS_MESSAGE_LOG_FD +fi +rm -f conftest.o conftest.s +AM_CONDITIONAL(MPX_AS_SUPPORTED, [test "x$mpx_as" = "xyes"]) + # Configure libtool AC_LIBTOOL_DLOPEN AM_PROG_LIBTOOL @@ -117,7 +129,7 @@ fi AC_CONFIG_FILES([Makefile libmpx.spec]) AC_CONFIG_HEADERS(config.h) -AC_CONFIG_FILES(AC_FOREACH([DIR], [mpxrt], [DIR/Makefile]), +AC_CONFIG_FILES(AC_FOREACH([DIR], [mpxrt mpxwrap], [DIR/Makefile ]), [cat > vpsed$$ << \_EOF s!`test -f '$<' || echo '$(srcdir)/'`!! _EOF diff --git a/libmpx/mpxwrap/Makefile.am b/libmpx/mpxwrap/Makefile.am new file mode 100644 index 0000000..c254d9b --- /dev/null +++ b/libmpx/mpxwrap/Makefile.am @@ -0,0 +1,54 @@ +ALCLOCAL_AMFLAGS = -I .. -I ../config + +# May be used by toolexeclibdir. +gcc_version := $(shell cat $(top_srcdir)/../gcc/BASE-VER) + +libmpxwrappers_la_CFLAGS = -fcheck-pointer-bounds -mmpx -fno-chkp-check-read \ + -fno-chkp-check-write -fno-chkp-use-wrappers +libmpxwrappers_la_DEPENDENCIES = libmpxwrappers.map +libmpxwrappers_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libmpxwrappers.map + +toolexeclib_LTLIBRARIES = libmpxwrappers.la + +libmpxwrappers_la_SOURCES = mpx_wrappers.c + +# Work around what appears to be a GNU make bug handling MAKEFLAGS +# values defined in terms of make variables, as is the case for CC and +# friends when we are called from the top level Makefile. +AM_MAKEFLAGS = \ + "AR_FLAGS=$(AR_FLAGS)" \ + "CC_FOR_BUILD=$(CC_FOR_BUILD)" \ + "CFLAGS=$(CFLAGS)" \ + "CXXFLAGS=$(CXXFLAGS)" \ + "CFLAGS_FOR_BUILD=$(CFLAGS_FOR_BUILD)" \ + "CFLAGS_FOR_TARGET=$(CFLAGS_FOR_TARGET)" \ + "INSTALL=$(INSTALL)" \ + "INSTALL_DATA=$(INSTALL_DATA)" \ + "INSTALL_PROGRAM=$(INSTALL_PROGRAM)" \ + "INSTALL_SCRIPT=$(INSTALL_SCRIPT)" \ + "JC1FLAGS=$(JC1FLAGS)" \ + "LDFLAGS=$(LDFLAGS)" \ + "LIBCFLAGS=$(LIBCFLAGS)" \ + "LIBCFLAGS_FOR_TARGET=$(LIBCFLAGS_FOR_TARGET)" \ + "MAKE=$(MAKE)" \ + "MAKEINFO=$(MAKEINFO) $(MAKEINFOFLAGS)" \ + "PICFLAG=$(PICFLAG)" \ + "PICFLAG_FOR_TARGET=$(PICFLAG_FOR_TARGET)" \ + "SHELL=$(SHELL)" \ + "RUNTESTFLAGS=$(RUNTESTFLAGS)" \ + "exec_prefix=$(exec_prefix)" \ + "infodir=$(infodir)" \ + "libdir=$(libdir)" \ + "prefix=$(prefix)" \ + "includedir=$(includedir)" \ + "AR=$(AR)" \ + "AS=$(AS)" \ + "LD=$(LD)" \ + "LIBCFLAGS=$(LIBCFLAGS)" \ + "NM=$(NM)" \ + "PICFLAG=$(PICFLAG)" \ + "RANLIB=$(RANLIB)" \ + "DESTDIR=$(DESTDIR)" + +MAKEOVERRIDES = + diff --git a/libmpx/mpxwrap/libmpxwrappers.map b/libmpx/mpxwrap/libmpxwrappers.map new file mode 100644 index 0000000..c2912a7 --- /dev/null +++ b/libmpx/mpxwrap/libmpxwrappers.map @@ -0,0 +1,23 @@ +LIBMPXWRAPPERS_1.0 +{ + global: + __mpx_wrapper_malloc; + __mpx_wrapper_mmap; + __mpx_wrapper_realloc; + __mpx_wrapper_calloc; + __mpx_wrapper_memset; + __mpx_wrapper_bzero; + __mpx_wrapper_memmove; + __mpx_wrapper_memcpy; + __mpx_wrapper_mempcpy; + __mpx_wrapper_strncat; + __mpx_wrapper_strcat; + __mpx_wrapper_stpcpy; + __mpx_wrapper_stpncpy; + __mpx_wrapper_strcpy; + __mpx_wrapper_strncpy; + __mpx_wrapper_strlen; + + local: + *; +}; diff --git a/libmpx/mpxwrap/libtool-version b/libmpx/mpxwrap/libtool-version new file mode 100644 index 0000000..bfe84c8 --- /dev/null +++ b/libmpx/mpxwrap/libtool-version @@ -0,0 +1,6 @@ +# This file is used to maintain libtool version info for libmpxintr. See +# the libtool manual to understand the meaning of the fields. This is +# a separate file so that version updates don't involve re-running +# automake. +# CURRENT:REVISION:AGE +1:0:0 diff --git a/libmpx/mpxwrap/mpx_wrappers.c b/libmpx/mpxwrap/mpx_wrappers.c new file mode 100644 index 0000000..63cbb40 --- /dev/null +++ b/libmpx/mpxwrap/mpx_wrappers.c @@ -0,0 +1,263 @@ +/* MPX Wrappers Library + Copyright (C) 2014 Free Software Foundation, Inc. + Contributed by Ilya Enkovich (ilya.enkovich@intel.com) + + This file is part of GCC. + + GCC is free software; you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation; either version 3, or (at your option) any later + version. + + GCC 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 General Public License + for more details. + + Under Section 7 of GPL version 3, you are granted additional + permissions described in the GCC Runtime Library Exception, version + 3.1, as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License and + a copy of the GCC Runtime Library Exception along with this program; + see the files COPYING3 and COPYING.RUNTIME respectively. If not, see + <http://www.gnu.org/licenses/>. */ + +#include "stdlib.h" +#include "string.h" +#include <sys/mman.h> + +void * +__mpx_wrapper_malloc (size_t size) +{ + void *p = (void *)malloc (size); + if (!p) return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, size); +} + + +void * +__mpx_wrapper_mmap (void *addr, size_t length, int prot, int flags, + int fd, off_t offset) +{ + void *p = mmap (addr, length, prot, flags, fd, offset); + if (!p) return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, length); +} + +void * +__mpx_wrapper_realloc (void *ptr, size_t n) +{ + if (!ptr) + return __mpx_wrapper_malloc (n); + + /* We don't kwnow how much data is copied by realloc + and therefore may check only lower bounds. */ + __bnd_chk_ptr_lbounds (ptr); + ptr = realloc (ptr, n); + + if (!ptr) + return __bnd_null_ptr_bounds (ptr); + + return __bnd_set_ptr_bounds (ptr, n); +} + +void * +__mpx_wrapper_calloc (size_t n_elements, size_t element_size) +{ + void *p = calloc (n_elements, element_size); + if (!p) + return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, n_elements * element_size); +} + +void * +__mpx_wrapper_memset (void *dstpp, int c, size_t len) +{ + if (len > 0) + { + __bnd_chk_ptr_bounds (dstpp, len); + memset (dstpp, c, len); + } + return dstpp; +} + +void +__mpx_wrapper_bzero (void *dst, size_t len) +{ + __mpx_wrapper_memset (dst, 0, len); +} + +void * +__mpx_wrapper_memmove (void *dst, const void *src, size_t n) +{ + const char *s = (const char*)src; + char *d = (char*)dst; + void *ret = dst; + size_t offset_src = ((size_t) s) & (sizeof (void *) - 1); + size_t offset_dst = ((size_t) d) & (sizeof (void *) - 1); + + __bnd_chk_ptr_bounds (dst, n); + __bnd_chk_ptr_bounds (src, n); + + /* Different alignment means that even if + pointers exist in memory, we don't how + pointers are aligned and therefore cann't + copy bounds anyway. */ + if (offset_src != offset_dst) + memmove (dst, src, n); + else + { + if (s < d) + { + d += n; + s += n; + offset_src = (offset_src + n) & (sizeof (void *) -1); + while (n-- && offset_src--) + *--d = *--s; + n++; + if (!n) + return ret; + void **d1 = (void **)d; + void **s1 = (void **)s; + /* This loop will also copy bounds. */ + while (n >= sizeof (void *)) + { + n -= sizeof (void *); + *--d1 = *--s1; + } + s = (char *)s1; + d = (char *)d1; + while (n--) + *--d = *--s; + } + else + { + offset_src = sizeof (void *) - offset_src; + while (n-- && offset_src--) + *d++ = *s++; + n++; + if (!n) + return ret; + void **d1 = (void **)d; + void **s1 = (void **)s; + /* This loop will also copy bounds. */ + while (n >= sizeof (void *)) + { + n -= sizeof (void *); + *d1++ = *s1++; + } + s = (char *)s1; + d = (char *)d1; + while (n--) + *d++ = *s++; + } + } + return ret; +} + + +void * +__mpx_wrapper_memcpy (void *dst, const void *src, size_t n) +{ + return __mpx_wrapper_memmove (dst, src, n); +} + +void * +__mpx_wrapper_mempcpy (void *dst, const void *src, size_t n) +{ + return (char *)__mpx_wrapper_memcpy (dst, src, n) + n; +} + +char * +__mpx_wrapper_strncat (char *dst, const char *src, size_t n) +{ + size_t dst_size = strlen (dst); + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + strncat (dst, src, n); + + return dst; +} + +char * +__mpx_wrapper_strcat (char *dst, const char *src) +{ + size_t dst_size = strlen (dst); + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + strcat (dst, src); + + return dst; +} + +char * +__mpx_wrapper_stpcpy (char *dst, const char *src) +{ + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + memcpy (dst, src, src_size + 1); + + return dst + src_size; +} + +char * +__mpx_wrapper_stpncpy (char *dst, const char *src, size_t n) +{ + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, n); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + return dst + src_size; +} + +char * +__mpx_wrapper_strcpy (char *dst, const char *src) +{ + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + memcpy (dst, src, src_size + 1); + + return dst; +} + +char * +__mpx_wrapper_strncpy (char *dst, const char *src, size_t n) +{ + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, n); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + return dst; +} + +size_t +__mpx_wrapper_strlen (const char *s) +{ + size_t length = strlen (s); + __bnd_chk_ptr_bounds (s, length); + return length; +} ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-11-24 14:53 ` Ilya Enkovich @ 2014-12-01 21:16 ` Jeff Law 2014-12-01 23:45 ` Joseph Myers 2014-12-03 14:29 ` Ilya Enkovich 0 siblings, 2 replies; 15+ messages in thread From: Jeff Law @ 2014-12-01 21:16 UTC (permalink / raw) To: Ilya Enkovich, Joseph Myers; +Cc: gcc-patches On 11/24/14 07:06, Ilya Enkovich wrote: >> Normally GCC target libraries assigned to the FSF would use GPL+exception >> rather than LGPL (especially if the library might be linked in >> statically), to keep predictable what requirements are imposed by linking >> your program with GCC. libquadmath is an exception because it contains >> LGPL code not assigned to the FSF. > > I'm OK to put it under GPL+exception. Well, if copyright is assigned to the FSF in the usual manner, then the FSF can relicense as appropriate. > > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt > index 8f5d76c..283c632 100644 > --- a/gcc/c-family/c.opt > +++ b/gcc/c-family/c.opt > @@ -1043,6 +1043,9 @@ Instrument only functions marked with bnd_instrument attribute. > static-libmpx > Driver > > +static-libmpxwrappers > +Driver Isn't something more needed here? > + > fcilkplus > C ObjC C++ ObjC++ LTO Report Var(flag_cilkplus) Init(0) > Enable Cilk Plus > diff --git a/gcc/gcc.c b/gcc/gcc.c > index 75e5767..aa8c9a3 100644 > --- a/gcc/gcc.c > +++ b/gcc/gcc.c > @@ -828,9 +828,23 @@ proper position among the other output files. */ > #endif > #endif > > +#ifndef LIBMPXWRAPPERS_SPEC > +#if defined(HAVE_LD_STATIC_DYNAMIC) > +#define LIBMPXWRAPPERS_SPEC "\ > +%{mmpx:%{fcheck-pointer-bounds:%{!fno-chkp-use-wrappers:\ > + %{static:-lmpxwrappers}\ > + %{!static:%{static-libmpxwrappers:" LD_STATIC_OPTION " --whole-archive}\ > + -lmpxwrappers %{static-libmpxwrappers:--no-whole-archive "\ > + LD_DYNAMIC_OPTION "}}}}}" > +#else > +#define LIBMPXWRAPPERS_SPEC "\ > +%{mmpx:%{fcheck-pointer-bounds:{!fno-chkp-use-wrappers:-lmpxwrappers}}}" > +#endif > +#endif My concern here is we're embedding target specific bits (existence of -mmpx flag) into gcc.c. Shouldn't this be buried in the x86 backend which is allowed to know about the specifics? > + > #ifndef MPX_SPEC > #define MPX_SPEC "\ > -%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC "}}" > +%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC LIBMPXWRAPPERS_SPEC "}}" > #endif Ugh. Somehow I missed that MPX_SPEC was in gcc.c along with the uses of LIBMPX_SPEC. Aren't all these target specific and thus belong in the x86 specific files? Presumably all this is dependent on the libmpx bits being accepted, right (and yes, I realize that I've got a TODO on that :-) And presumably the wrappers aren't really specific to MPX, they're a mechanism for you to get more information to the checker, regardless of whether it's MPX based on a pure software solution, right? Jeff ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-12-01 21:16 ` Jeff Law @ 2014-12-01 23:45 ` Joseph Myers 2014-12-03 21:28 ` Jeff Law 2014-12-03 14:29 ` Ilya Enkovich 1 sibling, 1 reply; 15+ messages in thread From: Joseph Myers @ 2014-12-01 23:45 UTC (permalink / raw) To: Jeff Law; +Cc: Ilya Enkovich, gcc-patches On Mon, 1 Dec 2014, Jeff Law wrote: > > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt > > index 8f5d76c..283c632 100644 > > --- a/gcc/c-family/c.opt > > +++ b/gcc/c-family/c.opt > > @@ -1043,6 +1043,9 @@ Instrument only functions marked with bnd_instrument > > attribute. > > static-libmpx > > Driver > > > > +static-libmpxwrappers > > +Driver > Isn't something more needed here? Driver --help output is still hardcoded in gcc.c rather than using help strings from .opt files. -- Joseph S. Myers joseph@codesourcery.com ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-12-01 23:45 ` Joseph Myers @ 2014-12-03 21:28 ` Jeff Law 0 siblings, 0 replies; 15+ messages in thread From: Jeff Law @ 2014-12-03 21:28 UTC (permalink / raw) To: Joseph Myers; +Cc: Ilya Enkovich, gcc-patches On 12/01/14 16:45, Joseph Myers wrote: > On Mon, 1 Dec 2014, Jeff Law wrote: > >>> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt >>> index 8f5d76c..283c632 100644 >>> --- a/gcc/c-family/c.opt >>> +++ b/gcc/c-family/c.opt >>> @@ -1043,6 +1043,9 @@ Instrument only functions marked with bnd_instrument >>> attribute. >>> static-libmpx >>> Driver >>> >>> +static-libmpxwrappers >>> +Driver >> Isn't something more needed here? > > Driver --help output is still hardcoded in gcc.c rather than using help > strings from .opt files. Ah, nevermind my comment then :-) jeff ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-12-01 21:16 ` Jeff Law 2014-12-01 23:45 ` Joseph Myers @ 2014-12-03 14:29 ` Ilya Enkovich 2014-12-05 22:52 ` Jeff Law 1 sibling, 1 reply; 15+ messages in thread From: Ilya Enkovich @ 2014-12-03 14:29 UTC (permalink / raw) To: Jeff Law; +Cc: Joseph Myers, gcc-patches 2014-12-02 0:16 GMT+03:00 Jeff Law <law@redhat.com>: > On 11/24/14 07:06, Ilya Enkovich wrote: >>> >>> Normally GCC target libraries assigned to the FSF would use GPL+exception >>> rather than LGPL (especially if the library might be linked in >>> statically), to keep predictable what requirements are imposed by linking >>> your program with GCC. libquadmath is an exception because it contains >>> LGPL code not assigned to the FSF. >> >> >> I'm OK to put it under GPL+exception. > > Well, if copyright is assigned to the FSF in the usual manner, then the FSF > can relicense as appropriate. > > >> >> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt >> index 8f5d76c..283c632 100644 >> --- a/gcc/c-family/c.opt >> +++ b/gcc/c-family/c.opt >> @@ -1043,6 +1043,9 @@ Instrument only functions marked with bnd_instrument >> attribute. >> static-libmpx >> Driver >> >> +static-libmpxwrappers >> +Driver > > Isn't something more needed here? > >> + >> fcilkplus >> C ObjC C++ ObjC++ LTO Report Var(flag_cilkplus) Init(0) >> Enable Cilk Plus >> diff --git a/gcc/gcc.c b/gcc/gcc.c >> index 75e5767..aa8c9a3 100644 >> --- a/gcc/gcc.c >> +++ b/gcc/gcc.c >> @@ -828,9 +828,23 @@ proper position among the other output files. */ >> #endif >> #endif >> >> +#ifndef LIBMPXWRAPPERS_SPEC >> +#if defined(HAVE_LD_STATIC_DYNAMIC) >> +#define LIBMPXWRAPPERS_SPEC "\ >> +%{mmpx:%{fcheck-pointer-bounds:%{!fno-chkp-use-wrappers:\ >> + %{static:-lmpxwrappers}\ >> + %{!static:%{static-libmpxwrappers:" LD_STATIC_OPTION " >> --whole-archive}\ >> + -lmpxwrappers %{static-libmpxwrappers:--no-whole-archive "\ >> + LD_DYNAMIC_OPTION "}}}}}" >> +#else >> +#define LIBMPXWRAPPERS_SPEC "\ >> +%{mmpx:%{fcheck-pointer-bounds:{!fno-chkp-use-wrappers:-lmpxwrappers}}}" >> +#endif >> +#endif > > My concern here is we're embedding target specific bits (existence of -mmpx > flag) into gcc.c. Shouldn't this be buried in the x86 backend which is > allowed to know about the specifics? > > >> + >> #ifndef MPX_SPEC >> #define MPX_SPEC "\ >> -%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC "}}" >> +%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC LIBMPXWRAPPERS_SPEC "}}" >> #endif > > Ugh. Somehow I missed that MPX_SPEC was in gcc.c along with the uses of > LIBMPX_SPEC. Aren't all these target specific and thus belong in the x86 > specific files? Is config/i386/linux-common.h is a proper place for these specs then? > > Presumably all this is dependent on the libmpx bits being accepted, right > (and yes, I realize that I've got a TODO on that :-) > > And presumably the wrappers aren't really specific to MPX, they're a > mechanism for you to get more information to the checker, regardless of > whether it's MPX based on a pure software solution, right? Right. Wrappers code doesn't use anything specific to MPX. In case of pure software solution we should be able to compile and use this library without changes (except compilation flags). But in case pure software solution exists MPX option should still be available and we should have two builds for this library. Ilya > > Jeff ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-12-03 14:29 ` Ilya Enkovich @ 2014-12-05 22:52 ` Jeff Law 2014-12-09 8:27 ` Ilya Enkovich 0 siblings, 1 reply; 15+ messages in thread From: Jeff Law @ 2014-12-05 22:52 UTC (permalink / raw) To: Ilya Enkovich; +Cc: Joseph Myers, gcc-patches On 12/03/14 07:28, Ilya Enkovich wrote: >>> #ifndef MPX_SPEC >>> #define MPX_SPEC "\ >>> -%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC "}}" >>> +%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC LIBMPXWRAPPERS_SPEC "}}" >>> #endif >> >> Ugh. Somehow I missed that MPX_SPEC was in gcc.c along with the uses of >> LIBMPX_SPEC. Aren't all these target specific and thus belong in the x86 >> specific files? > > Is config/i386/linux-common.h is a proper place for these specs then? Depends on whether or not we expect MPX to show up on other systems such as *bsd, mingw, solaris, etc. So I'd say linux-common.h is better than gcc.c, but perhaps not the best location. Uros should chime in here. > Right. Wrappers code doesn't use anything specific to MPX. In case of > pure software solution we should be able to compile and use this > library without changes (except compilation flags). But in case pure > software solution exists MPX option should still be available and we > should have two builds for this library. Ok. Just wanted to be sure I understood how the pieces fit together. I don't really expect a software implementation, but keeping it in mind helps us reasonably consider where certain things belong implementation wise. jeff ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-12-05 22:52 ` Jeff Law @ 2014-12-09 8:27 ` Ilya Enkovich 2014-12-09 19:46 ` Jeff Law 0 siblings, 1 reply; 15+ messages in thread From: Ilya Enkovich @ 2014-12-09 8:27 UTC (permalink / raw) To: Jeff Law; +Cc: Joseph Myers, gcc-patches On 05 Dec 15:52, Jeff Law wrote: > On 12/03/14 07:28, Ilya Enkovich wrote: > >>> #ifndef MPX_SPEC > >>> #define MPX_SPEC "\ > >>>-%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC "}}" > >>>+%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC LIBMPXWRAPPERS_SPEC "}}" > >>> #endif > >> > >>Ugh. Somehow I missed that MPX_SPEC was in gcc.c along with the uses of > >>LIBMPX_SPEC. Aren't all these target specific and thus belong in the x86 > >>specific files? > > > >Is config/i386/linux-common.h is a proper place for these specs then? > Depends on whether or not we expect MPX to show up on other systems > such as *bsd, mingw, solaris, etc. > > So I'd say linux-common.h is better than gcc.c, but perhaps not the > best location. Uros should chime in here. > > >Right. Wrappers code doesn't use anything specific to MPX. In case of > >pure software solution we should be able to compile and use this > >library without changes (except compilation flags). But in case pure > >software solution exists MPX option should still be available and we > >should have two builds for this library. > Ok. Just wanted to be sure I understood how the pieces fit > together. I don't really expect a software implementation, but > keeping it in mind helps us reasonably consider where certain things > belong implementation wise. > > jeff > Here is a version with linker specs moved into linux-common.h. Thanks, Ilya -- gcc/ 2014-12-09 Ilya Enkovich <ilya.enkovich@intel.com> * config/i386/linux-common.h (LIBMPX_WRAPPERSSPEC): New. (CHKP_SPEC): Add wrappers library. * c-family/c.opt (static-libmpxwrappers): New. libmpx/ 2014-12-09 Ilya Enkovich <ilya.enkovich@intel.com> * Makefile.am (SUBDIRS): Add mpxwrap when used AS supports MPX. (MAKEOVERRIDES): New. * Makefile.in: Regenerate. * configure.ac: Check AS supports MPX. Add mpxintr/Makefile to config files. * configure: Regenerate. * mpxwrap/Makefile.am: New. * mpxwrap/Makefile.in: New. * mpxwrap/libtool-version: New. * mpxwrap/mpx_wrappers.cc: New. * mpxwrap/libmpxwrappers.map: New. diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 65731cc..2632c3f 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1023,6 +1023,9 @@ Instrument only functions marked with bnd_instrument attribute. static-libmpx Driver +static-libmpxwrappers +Driver + fcilkplus C ObjC C++ ObjC++ LTO Report Var(flag_cilkplus) Init(0) Enable Cilk Plus diff --git a/gcc/config/i386/linux-common.h b/gcc/config/i386/linux-common.h index 07cd1a2..854af46 100644 --- a/gcc/config/i386/linux-common.h +++ b/gcc/config/i386/linux-common.h @@ -73,7 +73,21 @@ along with GCC; see the file COPYING3. If not see #endif #endif +#ifndef LIBMPXWRAPPERS_SPEC +#if defined(HAVE_LD_STATIC_DYNAMIC) +#define LIBMPXWRAPPERS_SPEC "\ +%{mmpx:%{fcheck-pointer-bounds:%{!fno-chkp-use-wrappers:\ + %{static:-lmpxwrappers}\ + %{!static:%{static-libmpxwrappers:" LD_STATIC_OPTION " --whole-archive}\ + -lmpxwrappers %{static-libmpxwrappers:--no-whole-archive "\ + LD_DYNAMIC_OPTION "}}}}}" +#else +#define LIBMPXWRAPPERS_SPEC "\ +%{mmpx:%{fcheck-pointer-bounds:{!fno-chkp-use-wrappers:-lmpxwrappers}}}" +#endif +#endif + #ifndef CHKP_SPEC #define CHKP_SPEC "\ -%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC "}}" +%{!nostdlib:%{!nodefaultlibs:" LIBMPX_SPEC LIBMPXWRAPPERS_SPEC "}}" #endif diff --git a/libmpx/Makefile.am b/libmpx/Makefile.am index 6cee4ac..bd0a8b6 100644 --- a/libmpx/Makefile.am +++ b/libmpx/Makefile.am @@ -2,6 +2,9 @@ ACLOCAL_AMFLAGS = -I .. -I ../config if LIBMPX_SUPPORTED SUBDIRS = mpxrt +if MPX_AS_SUPPORTED +SUBDIRS += mpxwrap +endif nodist_toolexeclib_HEADERS = libmpx.spec endif @@ -45,3 +48,5 @@ AM_MAKEFLAGS = \ "PICFLAG=$(PICFLAG)" \ "RANLIB=$(RANLIB)" \ "DESTDIR=$(DESTDIR)" + +MAKEOVERRIDES = diff --git a/libmpx/configure.ac b/libmpx/configure.ac index dbfad02..4669525 100644 --- a/libmpx/configure.ac +++ b/libmpx/configure.ac @@ -100,6 +100,18 @@ AC_CHECK_TOOL(AS, as) AC_CHECK_TOOL(AR, ar) AC_CHECK_TOOL(RANLIB, ranlib, :) +# Check we may build wrappers library +echo "test: bndmov %bnd0, %bnd1" > conftest.s +if AC_TRY_COMMAND([$AS -o conftest.o conftest.s 1>&AS_MESSAGE_LOG_FD]) +then + mpx_as=yes +else + mpx_as=no + echo "configure: no MPX support fo as" >&AS_MESSAGE_LOG_FD +fi +rm -f conftest.o conftest.s +AM_CONDITIONAL(MPX_AS_SUPPORTED, [test "x$mpx_as" = "xyes"]) + # Configure libtool AC_LIBTOOL_DLOPEN AM_PROG_LIBTOOL @@ -117,7 +129,7 @@ fi AC_CONFIG_FILES([Makefile libmpx.spec]) AC_CONFIG_HEADERS(config.h) -AC_CONFIG_FILES(AC_FOREACH([DIR], [mpxrt], [DIR/Makefile]), +AC_CONFIG_FILES(AC_FOREACH([DIR], [mpxrt mpxwrap], [DIR/Makefile ]), [cat > vpsed$$ << \_EOF s!`test -f '$<' || echo '$(srcdir)/'`!! _EOF diff --git a/libmpx/mpxwrap/Makefile.am b/libmpx/mpxwrap/Makefile.am new file mode 100644 index 0000000..c254d9b --- /dev/null +++ b/libmpx/mpxwrap/Makefile.am @@ -0,0 +1,54 @@ +ALCLOCAL_AMFLAGS = -I .. -I ../config + +# May be used by toolexeclibdir. +gcc_version := $(shell cat $(top_srcdir)/../gcc/BASE-VER) + +libmpxwrappers_la_CFLAGS = -fcheck-pointer-bounds -mmpx -fno-chkp-check-read \ + -fno-chkp-check-write -fno-chkp-use-wrappers +libmpxwrappers_la_DEPENDENCIES = libmpxwrappers.map +libmpxwrappers_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libmpxwrappers.map + +toolexeclib_LTLIBRARIES = libmpxwrappers.la + +libmpxwrappers_la_SOURCES = mpx_wrappers.c + +# Work around what appears to be a GNU make bug handling MAKEFLAGS +# values defined in terms of make variables, as is the case for CC and +# friends when we are called from the top level Makefile. +AM_MAKEFLAGS = \ + "AR_FLAGS=$(AR_FLAGS)" \ + "CC_FOR_BUILD=$(CC_FOR_BUILD)" \ + "CFLAGS=$(CFLAGS)" \ + "CXXFLAGS=$(CXXFLAGS)" \ + "CFLAGS_FOR_BUILD=$(CFLAGS_FOR_BUILD)" \ + "CFLAGS_FOR_TARGET=$(CFLAGS_FOR_TARGET)" \ + "INSTALL=$(INSTALL)" \ + "INSTALL_DATA=$(INSTALL_DATA)" \ + "INSTALL_PROGRAM=$(INSTALL_PROGRAM)" \ + "INSTALL_SCRIPT=$(INSTALL_SCRIPT)" \ + "JC1FLAGS=$(JC1FLAGS)" \ + "LDFLAGS=$(LDFLAGS)" \ + "LIBCFLAGS=$(LIBCFLAGS)" \ + "LIBCFLAGS_FOR_TARGET=$(LIBCFLAGS_FOR_TARGET)" \ + "MAKE=$(MAKE)" \ + "MAKEINFO=$(MAKEINFO) $(MAKEINFOFLAGS)" \ + "PICFLAG=$(PICFLAG)" \ + "PICFLAG_FOR_TARGET=$(PICFLAG_FOR_TARGET)" \ + "SHELL=$(SHELL)" \ + "RUNTESTFLAGS=$(RUNTESTFLAGS)" \ + "exec_prefix=$(exec_prefix)" \ + "infodir=$(infodir)" \ + "libdir=$(libdir)" \ + "prefix=$(prefix)" \ + "includedir=$(includedir)" \ + "AR=$(AR)" \ + "AS=$(AS)" \ + "LD=$(LD)" \ + "LIBCFLAGS=$(LIBCFLAGS)" \ + "NM=$(NM)" \ + "PICFLAG=$(PICFLAG)" \ + "RANLIB=$(RANLIB)" \ + "DESTDIR=$(DESTDIR)" + +MAKEOVERRIDES = + diff --git a/libmpx/mpxwrap/libmpxwrappers.map b/libmpx/mpxwrap/libmpxwrappers.map new file mode 100644 index 0000000..c2912a7 --- /dev/null +++ b/libmpx/mpxwrap/libmpxwrappers.map @@ -0,0 +1,23 @@ +LIBMPXWRAPPERS_1.0 +{ + global: + __mpx_wrapper_malloc; + __mpx_wrapper_mmap; + __mpx_wrapper_realloc; + __mpx_wrapper_calloc; + __mpx_wrapper_memset; + __mpx_wrapper_bzero; + __mpx_wrapper_memmove; + __mpx_wrapper_memcpy; + __mpx_wrapper_mempcpy; + __mpx_wrapper_strncat; + __mpx_wrapper_strcat; + __mpx_wrapper_stpcpy; + __mpx_wrapper_stpncpy; + __mpx_wrapper_strcpy; + __mpx_wrapper_strncpy; + __mpx_wrapper_strlen; + + local: + *; +}; diff --git a/libmpx/mpxwrap/libtool-version b/libmpx/mpxwrap/libtool-version new file mode 100644 index 0000000..bfe84c8 --- /dev/null +++ b/libmpx/mpxwrap/libtool-version @@ -0,0 +1,6 @@ +# This file is used to maintain libtool version info for libmpxintr. See +# the libtool manual to understand the meaning of the fields. This is +# a separate file so that version updates don't involve re-running +# automake. +# CURRENT:REVISION:AGE +1:0:0 diff --git a/libmpx/mpxwrap/mpx_wrappers.c b/libmpx/mpxwrap/mpx_wrappers.c new file mode 100644 index 0000000..63cbb40 --- /dev/null +++ b/libmpx/mpxwrap/mpx_wrappers.c @@ -0,0 +1,263 @@ +/* MPX Wrappers Library + Copyright (C) 2014 Free Software Foundation, Inc. + Contributed by Ilya Enkovich (ilya.enkovich@intel.com) + + This file is part of GCC. + + GCC is free software; you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation; either version 3, or (at your option) any later + version. + + GCC 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 General Public License + for more details. + + Under Section 7 of GPL version 3, you are granted additional + permissions described in the GCC Runtime Library Exception, version + 3.1, as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License and + a copy of the GCC Runtime Library Exception along with this program; + see the files COPYING3 and COPYING.RUNTIME respectively. If not, see + <http://www.gnu.org/licenses/>. */ + +#include "stdlib.h" +#include "string.h" +#include <sys/mman.h> + +void * +__mpx_wrapper_malloc (size_t size) +{ + void *p = (void *)malloc (size); + if (!p) return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, size); +} + + +void * +__mpx_wrapper_mmap (void *addr, size_t length, int prot, int flags, + int fd, off_t offset) +{ + void *p = mmap (addr, length, prot, flags, fd, offset); + if (!p) return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, length); +} + +void * +__mpx_wrapper_realloc (void *ptr, size_t n) +{ + if (!ptr) + return __mpx_wrapper_malloc (n); + + /* We don't kwnow how much data is copied by realloc + and therefore may check only lower bounds. */ + __bnd_chk_ptr_lbounds (ptr); + ptr = realloc (ptr, n); + + if (!ptr) + return __bnd_null_ptr_bounds (ptr); + + return __bnd_set_ptr_bounds (ptr, n); +} + +void * +__mpx_wrapper_calloc (size_t n_elements, size_t element_size) +{ + void *p = calloc (n_elements, element_size); + if (!p) + return __bnd_null_ptr_bounds (p); + return __bnd_set_ptr_bounds (p, n_elements * element_size); +} + +void * +__mpx_wrapper_memset (void *dstpp, int c, size_t len) +{ + if (len > 0) + { + __bnd_chk_ptr_bounds (dstpp, len); + memset (dstpp, c, len); + } + return dstpp; +} + +void +__mpx_wrapper_bzero (void *dst, size_t len) +{ + __mpx_wrapper_memset (dst, 0, len); +} + +void * +__mpx_wrapper_memmove (void *dst, const void *src, size_t n) +{ + const char *s = (const char*)src; + char *d = (char*)dst; + void *ret = dst; + size_t offset_src = ((size_t) s) & (sizeof (void *) - 1); + size_t offset_dst = ((size_t) d) & (sizeof (void *) - 1); + + __bnd_chk_ptr_bounds (dst, n); + __bnd_chk_ptr_bounds (src, n); + + /* Different alignment means that even if + pointers exist in memory, we don't how + pointers are aligned and therefore cann't + copy bounds anyway. */ + if (offset_src != offset_dst) + memmove (dst, src, n); + else + { + if (s < d) + { + d += n; + s += n; + offset_src = (offset_src + n) & (sizeof (void *) -1); + while (n-- && offset_src--) + *--d = *--s; + n++; + if (!n) + return ret; + void **d1 = (void **)d; + void **s1 = (void **)s; + /* This loop will also copy bounds. */ + while (n >= sizeof (void *)) + { + n -= sizeof (void *); + *--d1 = *--s1; + } + s = (char *)s1; + d = (char *)d1; + while (n--) + *--d = *--s; + } + else + { + offset_src = sizeof (void *) - offset_src; + while (n-- && offset_src--) + *d++ = *s++; + n++; + if (!n) + return ret; + void **d1 = (void **)d; + void **s1 = (void **)s; + /* This loop will also copy bounds. */ + while (n >= sizeof (void *)) + { + n -= sizeof (void *); + *d1++ = *s1++; + } + s = (char *)s1; + d = (char *)d1; + while (n--) + *d++ = *s++; + } + } + return ret; +} + + +void * +__mpx_wrapper_memcpy (void *dst, const void *src, size_t n) +{ + return __mpx_wrapper_memmove (dst, src, n); +} + +void * +__mpx_wrapper_mempcpy (void *dst, const void *src, size_t n) +{ + return (char *)__mpx_wrapper_memcpy (dst, src, n) + n; +} + +char * +__mpx_wrapper_strncat (char *dst, const char *src, size_t n) +{ + size_t dst_size = strlen (dst); + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + strncat (dst, src, n); + + return dst; +} + +char * +__mpx_wrapper_strcat (char *dst, const char *src) +{ + size_t dst_size = strlen (dst); + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, dst_size + src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + strcat (dst, src); + + return dst; +} + +char * +__mpx_wrapper_stpcpy (char *dst, const char *src) +{ + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + memcpy (dst, src, src_size + 1); + + return dst + src_size; +} + +char * +__mpx_wrapper_stpncpy (char *dst, const char *src, size_t n) +{ + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, n); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + return dst + src_size; +} + +char * +__mpx_wrapper_strcpy (char *dst, const char *src) +{ + size_t src_size = strlen (src); + + __bnd_chk_ptr_bounds (dst, src_size + 1); + __bnd_chk_ptr_bounds (src, src_size + 1); + + memcpy (dst, src, src_size + 1); + + return dst; +} + +char * +__mpx_wrapper_strncpy (char *dst, const char *src, size_t n) +{ + size_t src_size = strnlen (src, n); + + __bnd_chk_ptr_bounds (dst, n); + if (src_size < n) + __bnd_chk_ptr_bounds (src, src_size + 1); + else + __bnd_chk_ptr_bounds (src, src_size); + + return dst; +} + +size_t +__mpx_wrapper_strlen (const char *s) +{ + size_t length = strlen (s); + __bnd_chk_ptr_bounds (s, length); + return length; +} ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH, MPX wrappers 1/3] Add MPX wrappers library 2014-12-09 8:27 ` Ilya Enkovich @ 2014-12-09 19:46 ` Jeff Law 0 siblings, 0 replies; 15+ messages in thread From: Jeff Law @ 2014-12-09 19:46 UTC (permalink / raw) To: Ilya Enkovich; +Cc: Joseph Myers, gcc-patches On 12/09/14 01:27, Ilya Enkovich wrote: > > 2014-12-09 Ilya Enkovich <ilya.enkovich@intel.com> > > * config/i386/linux-common.h (LIBMPX_WRAPPERSSPEC): New. > (CHKP_SPEC): Add wrappers library. > * c-family/c.opt (static-libmpxwrappers): New. > > libmpx/ > > 2014-12-09 Ilya Enkovich <ilya.enkovich@intel.com> > > * Makefile.am (SUBDIRS): Add mpxwrap when used > AS supports MPX. > (MAKEOVERRIDES): New. > * Makefile.in: Regenerate. > * configure.ac: Check AS supports MPX. Add > mpxintr/Makefile to config files. > * configure: Regenerate. > * mpxwrap/Makefile.am: New. > * mpxwrap/Makefile.in: New. > * mpxwrap/libtool-version: New. > * mpxwrap/mpx_wrappers.cc: New. > * mpxwrap/libmpxwrappers.map: New. > > OK once prerequisites are approved. jeff ^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2014-12-09 19:46 UTC | newest] Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2014-11-14 17:29 [PATCH, MPX wrappers 1/3] Add MPX wrappers library Ilya Enkovich 2014-11-14 19:20 ` Joseph Myers 2014-11-15 8:51 ` Jeff Law 2014-11-18 16:53 ` Ilya Enkovich 2014-11-18 21:52 ` Jeff Law 2014-11-21 16:07 ` Ilya Enkovich 2014-11-22 0:27 ` Joseph Myers 2014-11-24 14:53 ` Ilya Enkovich 2014-12-01 21:16 ` Jeff Law 2014-12-01 23:45 ` Joseph Myers 2014-12-03 21:28 ` Jeff Law 2014-12-03 14:29 ` Ilya Enkovich 2014-12-05 22:52 ` Jeff Law 2014-12-09 8:27 ` Ilya Enkovich 2014-12-09 19:46 ` Jeff Law
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).