Unexpected behavior for static declaration of memset
| Bugzilla Link | 46677 |
| Version | unspecified |
| OS | Windows NT |
| CC | @zygoloid |
Extended Description
Take the following at -O0:
static int memset(int z){ return 3; }
int g(int*a) { if (a[0] > 1) return *(volatile char*)0; return 0; }
int f() { int z[1000] = {0}; z[0]++; g(z); return memset(1); }
int main() { f(); f(); }
The program crashes; according to the C standard, it shouldn't. (Strictly speaking, the name memset can be used for internal symbols if C library headers aren't included.)
Another testcase; suppose we have something like the following at -O2:
__attribute((noinline)) void *fast_memset(void *s, int c, unsigned long n) {
for (int i = 0; i < n; ++i)
((char*)s)[i] = c;
return s;
}
__attribute((noinline)) static void *memset(void *s, int c, unsigned long n) {
return fast_memset(s, c, n);
}
void f2(char* x) { memset(x, 3, 100); }
int g(volatile int *a) { if (a[0] > 1) return *(volatile char*)0; return 0; }
void f() { volatile int z[1000] = {0}; z[0]++; g(z); }
int main() { f(); f(); }
This crashes; according to the C standard, it also shouldn't crash. (The "memset" looks like it should work on the surface, but it doesn't due to constant propagation.)
It's possible there isn't any reasonable way to fix this due to various compatibility constraints. But we should at least emit a warning in that case.
Is it possible to emit a reference to an unresolved external symbol with a given name, from an object file that also defines an internal symbol with the same name? Presumably we can add functionality to at least the integrated assembler to support that if the object file format and linkers permit it, but we might be out of luck in the no-integrated-as case.
I guess technically, there's nothing stopping us from emitting multiple symbols with the same name in the symbol table in ELF. There isn't any assembler support for that at the moment; we'd need to invent some syntax. Not sure about other object formats.
I'd rather not do something novel for an obscure issue like this, though.
Is it possible to emit a reference to an unresolved external symbol with a given name, from an object file that also defines an internal symbol with the same name? Presumably we can add functionality to at least the integrated assembler to support that if the object file format and linkers permit it, but we might be out of luck in the no-integrated-as case.
- Teach clang to emit an alternative inline sequence for struct operations if we detect this situation, instead of using memcpy/memset libcalls.
Presumably this would mean teaching something towards the backend of LLVM rather than clang per se, since the frontend has no visibility to @llvm.memset calls invented by mid-level optimizations? This seems OK, but not ideal.
- Do something with compiler-rt: we could provide a function or alias for memcpy/memset that's "__" prefixed, and therefore can't conflict with standard C code.
We could do. It'd be a bit wasteful to include symbols that are only used in this corner case, but it's a small cost. We'd presumably need to fall back to a different strategy in the --rtlib=libgcc case.
The second testcase is, unfortunately, not completely synthetic; the definitions of memset/fast_memset are loosely based on a user's codebase.
The biggest problem with rewriting internal symbol names is the interaction with inline asm, yes. If a static symbol is marked with the "used" attribute, inline asm is allowed to refer to it. So it's not clear what should happen at that point; do we try to rewrite the inline asm? Do we ignore the "used"? Do we emit an error?
There's also potentially issues with aliases; maybe solvable, but we'd want to be careful.
Fortunately, we have some flexibility for code without unusual attributes; we don't promise anything about the names/functionality of internal symbols at that point. So there isn't really anything stopping us from rewriting the name to something arbitrary in the given testcases.
There are probably other reasons we should avoid rewriting symbol names even if we can; some tools, like debuggers, depend on those names. But there isn't any actual rule at that point.
The scope of this issue as I've presented it is, I think, limited specifically to symbols named memset and memcpy, since we unconditionally synthesize calls to them whether or not they're "nobuiltin". To be on the safe side, we might want to handle everything in llvm/include/llvm/IR/RuntimeLibcalls.def (at least, the stuff that's not "__"-prefixed).
Two alternative options I've thought of:
- Teach clang to emit an alternative inline sequence for struct operations if we detect this situation, instead of using memcpy/memset libcalls.
- Do something with compiler-rt: we could provide a function or alias for memcpy/memset that's "__" prefixed, and therefore can't conflict with standard C code.
Your second testcase in particular is amazing.
It seems to me that we could handle this in the frontend by using a different symbol name for internal-linkage symbols that collide with certain well-known names. Can we do that without breaking references to the symbol from inline asm in the same compilation unit?
Nothing has changed as far as I can tell: First test case: https://godbolt.org/z/o645coqq6 Second test case: https://godbolt.org/z/9s4nqWjPo
Quick note, gcc emits a ud2 for return *(volatile char*)0; : https://github.com/llvm/llvm-project/issues/65504
Even more minimal reproducer. Compiled with -Os to prevent lowering @llvm.memset calls to inline memset by the backend.
#include <assert.h>
__attribute__((used))
static void *memset(void *s, int c, unsigned long n) {
assert(0 && "static memset was called");
return s;
}
int main() {
// This will emit a call to 'memset', which will be resolved to our static 'memset'.
volatile int z[1000] = {0};
return 0;
}