glen herrmannsfeldt wrote:
> One difference is that the PL/I version can be turned on or off
> at the procedure or statement level. In general, that doesn't help,
> but reasonably often you know which parts of a program are more
> likely to have array bounds problems and only turn it on there.
First, my impression is that one often does not know - at least not for
stack allocated memory. Especially not with C code, which intrinsically
lacks bound information. You stack allocate a variable in one file, pass
it as actual argument and after a function calls, you access invalid
memory. However, the sanitizer can only detect this if you have already
instrumented the original stack allocation - and the place where you use
the it.
For Fortran 90 code that's less relevant as the array descriptor
provides the bound information. For Fortran 77 code with assumed-size
arrays (explicit size is a bit better), you run into the same issue.
Secondly, with both -fcheck=... and with -fsanitize=address (or thread),
you can selectively compile only part of your code, which only
instruments those parts. However, most of the time it is much simpler to
turn on all diagnostic and see where it fails. Fortunately, many
failures already occur with smaller examples.
> Java requires bounds checking. As I understand it, either the compiler
> or the JIT at run time can optimize out cases where it isn't needed.
> for(i=0;i<x.length;i++) sum += x[i];
> can't go outside x, and it can tell at compile time.
>
> Do any Fortran compilers have an option to check others, but not
> loops where it is obvious it is safe?
I fail to see why loops are safe. However, with optimization turned on,
the compiler will also optimize checks away. In this case it should be
able to optimize internally added
"if(i >= x.length) { throw out of bounds }"
away.
Actually, with loops one has to be even more careful - one easily
exceeds bounds or integer ranges without realizing. For instance:
do i = n, huge(i)
...
end do
What's the value of "i" after the loop?
Or for bounds, an issue which occurs with SPEC 2006 for a C and a
Fortran code. See the item about -fno-aggressive-loop-optimizations in
http://gcc.gnu.org/gcc-4.8/changes.html
A description of the issue with the C code can be found at
http://blog.regehr.org/archives/918 (Note that GCC warns by default
when it does the mentioned optimization; it didn't do so in GCC 4.8.0rc1
but it does so in the 4.8.0 release.)
Hence, it is actually good to stick to standard conforming code - and
never lie to the compiler. They will do all permitted optimization -
which can break legacy code. And compilers will exploit more
optimization opportunities in the future.
Tobias