From patchwork Fri Nov 14 17:26:01 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Enkovich X-Patchwork-Id: 410944 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 9528E140082 for ; Sat, 15 Nov 2014 04:26:27 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=EkZFWa2TBuhyxMCRzqbjjOGDWDS/UfdMhZuha9zGq/I9Rj/n40pnA +kVAouBvoO9AgYb2yLyzvfKjZX30OtqGNOoeAjBxJMggSv0I93YnCtqaO3RAyUn+ C7LiijZ2X+dYtcgLobqb31c8aUJDXv9hOcISUSKZP+6kXHOP2ejXb4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=NuZQ1LS9YoOpFxPpzvTDCChIZqI=; b=Hlc0u4JbhUNzOK1AtwUK uMUk/vE/EmMDkEBDhSTj7E5uvgryJUp/K69/xPg2X1nun5vi5mGn2kADon9mdNhh iS7Gv7kYz0tNpw4w3kMJ88rUxg2gMiVHAr2Qax49OweNbUVPY+ZXi5fe+oEzkxK9 lJW0Gy6AUV1fVx1czktgIoQ= Received: (qmail 16895 invoked by alias); 14 Nov 2014 17:26:20 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 16883 invoked by uid 89); 14 Nov 2014 17:26:19 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-pd0-f170.google.com Received: from mail-pd0-f170.google.com (HELO mail-pd0-f170.google.com) (209.85.192.170) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Fri, 14 Nov 2014 17:26:17 +0000 Received: by mail-pd0-f170.google.com with SMTP id fp1so853627pdb.1 for ; Fri, 14 Nov 2014 09:26:15 -0800 (PST) X-Received: by 10.67.22.99 with SMTP id hr3mr11558741pad.20.1415985975398; Fri, 14 Nov 2014 09:26:15 -0800 (PST) Received: from msticlxl57.ims.intel.com (fmdmzpr02-ext.fm.intel.com. [192.55.55.37]) by mx.google.com with ESMTPSA id w5sm28119439pds.25.2014.11.14.09.26.13 for (version=TLSv1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 14 Nov 2014 09:26:14 -0800 (PST) Date: Fri, 14 Nov 2014 20:26:01 +0300 From: Ilya Enkovich To: gcc-patches@gcc.gnu.org Subject: [PATCH, MPX wrappers 1/3] Add MPX wrappers library Message-ID: <20141114172601.GA20207@msticlxl57.ims.intel.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes 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 * gcc.c (MPX_SPEC): Add wrappers library. libmpx/ 2014-11-14 Ilya Enkovich * 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 + +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; +}