[LLVMbugs] [Bug 4738] New: Invalid strcmp optimization involving weak GV initializers

bugzilla-daemon at cs.uiuc.edu bugzilla-daemon at cs.uiuc.edu
Tue Aug 18 12:04:39 PDT 2009


http://llvm.org/bugs/show_bug.cgi?id=4738

           Summary: Invalid strcmp optimization involving weak GV
                    initializers
           Product: new-bugs
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: new bugs
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: edwintorok at gmail.com
                CC: llvmbugs at cs.uiuc.edu


If a GlobalVariable is weak and has a constant initializer some optimizations
may make optimizations based on faulty assumptions, since the initializer may
be overridden at link time by a non-weak symbol.

One such case is simplifylibcalls, optimizing strcmp, but there may be more.

Here is the testcase for wrong strcmp optimization:
foo.c:
#include <string.h>
#include <stdlib.h>
const char fake_init[] __attribute__((weak)) = "y";

int main()
{
    if (strcmp(fake_init+1, "y") != 0)
        abort();
    return 0;
}

foo2.c:
const char fake_init[] = "yy";

llvm-gcc and clang fails the test:

$ llvm-gcc foo.c foo2.c && ./a.out
$ llvm-gcc foo.c foo2.c -O1 && ./a.out
Aborted
$ clang foo.c foo2.c && ./a.out
$ clang foo.c foo2.c -O1 && ./a.out
Aborted
$ gcc foo.c foo2.c && ./a.out
$ gcc foo.c foo2.c -O1 && ./a.out
$ gcc foo.c foo2.c -O2 && ./a.out

I think the easiest fix would be to make getInitializer() return the
initializer only if the GV cannot be overridden. Code that needs the
initializer for purposes other than optimization (such as backend) could use a
different method that gives you the real initializer.
Or we could check each use of getInitializer() to make sure its guarded by
hasDefinitiveInitializer, or !mayBeOverridden.

Some stats:
$ git grep getInitializer |grep -E Analysis\|Transforms|wc -l
46
$ git grep mayBeOverridden |wc -l
11
git grep hasDefinitiveInitializer |wc -l
7

So it seems there are 28 potential locations where GV->getInitializer is used
for optimization wrongly.


-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list