xref: /openbsd/gnu/gcc/libmudflap/mf-heuristics.c (revision 404b540a)
1*404b540aSrobert /* Mudflap: narrow-pointer bounds-checking by tree rewriting.
2*404b540aSrobert    Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3*404b540aSrobert    Contributed by Frank Ch. Eigler <fche@redhat.com>
4*404b540aSrobert    and Graydon Hoare <graydon@redhat.com>
5*404b540aSrobert 
6*404b540aSrobert This file is part of GCC.
7*404b540aSrobert 
8*404b540aSrobert GCC is free software; you can redistribute it and/or modify it under
9*404b540aSrobert the terms of the GNU General Public License as published by the Free
10*404b540aSrobert Software Foundation; either version 2, or (at your option) any later
11*404b540aSrobert version.
12*404b540aSrobert 
13*404b540aSrobert In addition to the permissions in the GNU General Public License, the
14*404b540aSrobert Free Software Foundation gives you unlimited permission to link the
15*404b540aSrobert compiled version of this file into combinations with other programs,
16*404b540aSrobert and to distribute those combinations without any restriction coming
17*404b540aSrobert from the use of this file.  (The General Public License restrictions
18*404b540aSrobert do apply in other respects; for example, they cover modification of
19*404b540aSrobert the file, and distribution when not linked into a combine
20*404b540aSrobert executable.)
21*404b540aSrobert 
22*404b540aSrobert GCC is distributed in the hope that it will be useful, but WITHOUT ANY
23*404b540aSrobert WARRANTY; without even the implied warranty of MERCHANTABILITY or
24*404b540aSrobert FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25*404b540aSrobert for more details.
26*404b540aSrobert 
27*404b540aSrobert You should have received a copy of the GNU General Public License
28*404b540aSrobert along with GCC; see the file COPYING.  If not, write to the Free
29*404b540aSrobert Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
30*404b540aSrobert 02110-1301, USA.  */
31*404b540aSrobert 
32*404b540aSrobert 
33*404b540aSrobert #include "config.h"
34*404b540aSrobert 
35*404b540aSrobert #include <stdio.h>
36*404b540aSrobert 
37*404b540aSrobert #include "mf-runtime.h"
38*404b540aSrobert #include "mf-impl.h"
39*404b540aSrobert 
40*404b540aSrobert #ifdef _MUDFLAP
41*404b540aSrobert #error "Do not compile this file with -fmudflap!"
42*404b540aSrobert #endif
43*404b540aSrobert 
44*404b540aSrobert 
45*404b540aSrobert extern char _end[];
46*404b540aSrobert extern char ENTRY_POINT[];
47*404b540aSrobert 
48*404b540aSrobert 
49*404b540aSrobert /* Run some quick validation of the given region.
50*404b540aSrobert    Return -1 / 0 / 1 if the access known-invalid, possibly-valid, or known-valid.
51*404b540aSrobert */
52*404b540aSrobert int
__mf_heuristic_check(uintptr_t ptr,uintptr_t ptr_high)53*404b540aSrobert __mf_heuristic_check (uintptr_t ptr, uintptr_t ptr_high)
54*404b540aSrobert {
55*404b540aSrobert   VERBOSE_TRACE ("mf: heuristic check\n");
56*404b540aSrobert 
57*404b540aSrobert   /* XXX: Disable the stack bounding check for libmudflapth.  We do
58*404b540aSrobert      actually have enough information to track stack bounds (see
59*404b540aSrobert      __mf_pthread_info in mf-hooks.c), so with a bit of future work,
60*404b540aSrobert      this heuristic can be turned on.  */
61*404b540aSrobert #ifndef LIBMUDFLAPTH
62*404b540aSrobert 
63*404b540aSrobert   /* The first heuristic is to check stack bounds.  This is a
64*404b540aSrobert      transient condition and quick to check. */
65*404b540aSrobert   if (__mf_opts.heur_stack_bound)
66*404b540aSrobert     {
67*404b540aSrobert       uintptr_t stack_top_guess = (uintptr_t)__builtin_frame_address(0);
68*404b540aSrobert #if defined(__i386__) && defined (__linux__)
69*404b540aSrobert       uintptr_t stack_segment_base = 0xC0000000; /* XXX: Bad assumption. */
70*404b540aSrobert #else
71*404b540aSrobert       /* Cause tests to fail. */
72*404b540aSrobert       uintptr_t stack_segment_base = 0;
73*404b540aSrobert #endif
74*404b540aSrobert 
75*404b540aSrobert       VERBOSE_TRACE ("mf: stack estimated as %p-%p\n",
76*404b540aSrobert 		     (void *) stack_top_guess, (void *) stack_segment_base);
77*404b540aSrobert 
78*404b540aSrobert       if (ptr_high <= stack_segment_base &&
79*404b540aSrobert 	  ptr >= stack_top_guess &&
80*404b540aSrobert 	  ptr_high >= ptr)
81*404b540aSrobert 	{
82*404b540aSrobert 	  return 1;
83*404b540aSrobert 	}
84*404b540aSrobert     }
85*404b540aSrobert #endif
86*404b540aSrobert 
87*404b540aSrobert 
88*404b540aSrobert   /* The second heuristic is to scan the range of memory regions
89*404b540aSrobert      listed in /proc/self/maps, a special file provided by the Linux
90*404b540aSrobert      kernel.  Its results may be cached, and in fact, a GUESS object
91*404b540aSrobert      may as well be recorded for interesting matching sections.  */
92*404b540aSrobert   if (__mf_opts.heur_proc_map)
93*404b540aSrobert     {
94*404b540aSrobert       /* Keep a record of seen records from /proc/self/map.  */
95*404b540aSrobert       enum { max_entries = 500 };
96*404b540aSrobert       struct proc_self_map_entry
97*404b540aSrobert       {
98*404b540aSrobert 	uintptr_t low;
99*404b540aSrobert 	uintptr_t high;
100*404b540aSrobert       };
101*404b540aSrobert       static struct proc_self_map_entry entry [max_entries];
102*404b540aSrobert       static unsigned entry_used [max_entries];
103*404b540aSrobert 
104*404b540aSrobert       /* Look for a known proc_self_map entry that may cover this
105*404b540aSrobert 	 region.  If one exists, then this heuristic has already run,
106*404b540aSrobert 	 and should not be run again.  The check should be allowed to
107*404b540aSrobert 	 fail.  */
108*404b540aSrobert       unsigned i;
109*404b540aSrobert       unsigned deja_vu = 0;
110*404b540aSrobert       for (i=0; i<max_entries; i++)
111*404b540aSrobert 	{
112*404b540aSrobert 	  if (entry_used[i] &&
113*404b540aSrobert 	      (entry[i].low <= ptr) &&
114*404b540aSrobert 	      (entry[i].high >= ptr_high))
115*404b540aSrobert 	    deja_vu = 1;
116*404b540aSrobert 	}
117*404b540aSrobert 
118*404b540aSrobert       if (! deja_vu)
119*404b540aSrobert 	{
120*404b540aSrobert 	  /* Time to run the heuristic.  Rescan /proc/self/maps; update the
121*404b540aSrobert 	     entry[] array; XXX: remove expired entries, add new ones.
122*404b540aSrobert 	     XXX: Consider entries that have grown (e.g., stack).  */
123*404b540aSrobert 	  char buf[512];
124*404b540aSrobert 	  char flags[4];
125*404b540aSrobert 	  void *low, *high;
126*404b540aSrobert 	  FILE *fp;
127*404b540aSrobert 
128*404b540aSrobert 	  fp = fopen ("/proc/self/maps", "r");
129*404b540aSrobert 	  if (fp)
130*404b540aSrobert 	    {
131*404b540aSrobert 	      while (fgets (buf, sizeof(buf), fp))
132*404b540aSrobert 		{
133*404b540aSrobert 		  if (sscanf (buf, "%p-%p %4c", &low, &high, flags) == 3)
134*404b540aSrobert 		    {
135*404b540aSrobert 		      if ((uintptr_t) low <= ptr &&
136*404b540aSrobert 			  (uintptr_t) high >= ptr_high)
137*404b540aSrobert 			{
138*404b540aSrobert 			  for (i=0; i<max_entries; i++)
139*404b540aSrobert 			    {
140*404b540aSrobert 			      if (! entry_used[i])
141*404b540aSrobert 				{
142*404b540aSrobert 				  entry[i].low = (uintptr_t) low;
143*404b540aSrobert 				  entry[i].high = (uintptr_t) high;
144*404b540aSrobert 				  entry_used[i] = 1;
145*404b540aSrobert 				  break;
146*404b540aSrobert 				}
147*404b540aSrobert 			    }
148*404b540aSrobert 
149*404b540aSrobert 			  VERBOSE_TRACE ("mf: registering region #%d "
150*404b540aSrobert 					 "%p-%p given %s",
151*404b540aSrobert 					 i, (void *) low, (void *) high, buf);
152*404b540aSrobert 
153*404b540aSrobert 			  __mfu_register ((void *) low, (size_t) (high-low),
154*404b540aSrobert 					  __MF_TYPE_GUESS,
155*404b540aSrobert 					  "/proc/self/maps segment");
156*404b540aSrobert 
157*404b540aSrobert 			  return 0; /* undecided (tending to cachable) */
158*404b540aSrobert 			}
159*404b540aSrobert 		    }
160*404b540aSrobert 		}
161*404b540aSrobert 	      fclose (fp);
162*404b540aSrobert 	    }
163*404b540aSrobert 	}
164*404b540aSrobert     }
165*404b540aSrobert 
166*404b540aSrobert 
167*404b540aSrobert   /* The third heuristic is to approve all accesses between _start (or its
168*404b540aSrobert      equivalent for the given target) and _end, which should include all
169*404b540aSrobert      text and initialized data.  */
170*404b540aSrobert   if (__mf_opts.heur_start_end)
171*404b540aSrobert     if (ptr >= (uintptr_t) & ENTRY_POINT && ptr_high <= (uintptr_t) & _end)
172*404b540aSrobert       return 1; /* uncacheable */
173*404b540aSrobert 
174*404b540aSrobert   return 0; /* unknown */
175*404b540aSrobert }
176