lansd@dgp.toronto.edu (Robert Lansdale) (03/14/91)
For the longest while I've have had problems with longjmp()'s
and level 2 optimizations in my rendering system. If an error occured
during the loading of a script file then the program would go into an
endless loop re-reading the first line of the file. The problem was
non-existent for the non-optimized version.
A few printf's finally narrowed the problem down to a small
area of the code. The problem is best exemplified with the following
piece of code which I wrote to recreate the problem:
=========================================================================
/* A small program to demonstrate a level 2 optimization problem/bug. */
/* By Rob Lansdale, March 14, 1991. lansd@dgp.toronto.edu */
#include <stdio.h>
#include <setjmp.h>
#define TRUE 1
jmp_buf start;
int user_script_stacked;
main(argc, argv)
int argc;
char *argv[];
{
char *tempstr;
char *user_script_file = "test.file";
setjmp(start);
if (user_script_file != (char *) NULL) {
tempstr = user_script_file;
/* Set pointer to NULL so an abort will not try to read the script in again */
/* vvvvvvv THIS STATEMENT IS THROWN AWAY BY OPTIMIZER vvvv */
user_script_file = (char *) NULL;
open_script_file(tempstr);
user_script_stacked = TRUE;
}
/* This comment is needed so that the optimizer will check to see */
/* whether 'user_script_file' is used somewhere in the remainder */
/* of the procedure. Since it isn't, the optimizer will trash the */
/* 'user_script_file = (char *) NULL' above. */
}
open_script_file(tempstr)
char *tempstr;
{
printf("In open_script_file()\n");
longjmp(start, 0);
}
===========================================================================
When this is run after being compiled with 'cc -O2', the program will
repeatedly print out 'In open_script_file()'. The problem can be summarized
as being due to the optimizer throwing away the 'user_script_file = (char *)
NULL' statement since it is not used in the remainder of main(). While this
is a valid optimization, it does not hold true for such cases as above where
a longjmp() is used.
Is this a bug or a feature of the optimizer? The problem can be
corrected by including a reference to the 'user_script_file' where the
4 line comment is.
--> Rob Lansdale
--
Robert Lansdale - (416) 978-6619 Dynamic Graphics Project
Internet: lansd@dgp.toronto.edu Computer Systems Research Institute
UUCP: ..!uunet!dgp.toronto.edu!lansd University of Toronto
Bitnet: lansd@dgp.utoronto Toronto, Ontario M5S 1A4, CANADAdavea@quasar.wpd.sgi.com (David B.Anderson) (03/15/91)
In article <1991Mar14.014517.26603@jarvis.csri.toronto.edu> lansd@dgp.toronto.edu (Robert Lansdale) writes: > > For the longest while I've have had problems with longjmp()'s >and level 2 optimizations in my rendering system. If an error occured >during the loading of a script file then the program would go into an >endless loop re-reading the first line of the file. The problem was >non-existent for the non-optimized version. [stuff deleted ] > char *user_script_file = "test.file"; [stuff deleted ] For correct operation, modify this to: char * volatile user_script_file = "test.file"; The extra assignment you found effective is not guaranteed to work. See the ANSI C Rationale on longjmp for further information. Page 86. Also see the longjmp man page. Regards, [ David B. Anderson Silicon Graphics (415)335-1548 davea@sgi.com ] [``What can go wrong?'' --Calvin to Hobbes]