Fix tools/setjmp_t to prevent nested_sp inlining
authorIvan Maidanski <ivmai@mail.ru>
Wed, 5 Oct 2016 08:04:25 +0000 (11:04 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Wed, 5 Oct 2016 08:05:21 +0000 (11:05 +0300)
Inlined nested_sp might cause incorrect result of nested_sp()<sp.

* tools/setjmp_t.c (nested_sp): Change return from int* to word.
* tools/setjmp_t.c (nested_sp_fn): New global volatile variable
initialized to nested_sp.
* tools/setjmp_t.c (main): Use nested_sp_fn instead of nested_sp;
remove redundant cast.

tools/setjmp_t.c

index 9ba9d28..1bd715d 100644 (file)
@@ -60,13 +60,16 @@ struct {
   char * a_b;
 } a;
 
-int * nested_sp(void)
+word nested_sp(void)
 {
     volatile word sp;
     sp = (word)(&sp);
-    return (int *)sp;
+    return sp;
 }
 
+/* To prevent nested_sp inlining. */
+word (*volatile nested_sp_fn)(void) = nested_sp;
+
 int g(int x);
 
 int main(void)
@@ -79,7 +82,7 @@ int main(void)
 
     sp = (word)(&sp);
     printf("This appears to be a %s running %s\n", MACH_TYPE, OS_TYPE);
-    if ((word)nested_sp() < sp) {
+    if (nested_sp_fn() < sp) {
       printf("Stack appears to grow down, which is the default.\n");
       printf("A good guess for STACKBOTTOM on this machine is 0x%lx.\n",
              ((unsigned long)sp + ps) & ~(ps-1));