sparse, llvm: Fix SIGSEGV for extern symbols
PATCH 1/1 sparse, llvm: Fix SIGSEGV for extern symbols
Jonathan Neuschäfer writes:
compiling a little real-world program with sparse-llvm, it segfaulted. Using a tool called "delta"[1] and some bash scripting, I managed to reduce the code to this test case:
extern struct foo *foop;
extern void func(struct foo *f);
int main(int argc, char **argv) {
func(foop);
}The problem is that pseudo_to_value() does not know abou the extern symbol because Sparse never calls output_data() on it which registers globals with LLVMAddGlobal().
As explained by Linus, 'extern' symbols are just names with types. They don't have any value associated with them, they just have the type and the name. Therefore we need to explicitly call LLVMAddGlobal() for symbols we have not encountered in pseudo_to_value().
Reported by: Jonathan Neuschäfer <[email protected]>
--- sparse-llvm.c | 8 ++++++++ validation/backend/extern.c | 11 +++++++++++ 2 files changed, 19 insertions(+), 0 deletions(-) create mode 100644 validation/backend/extern.c
sparse-llvm.c +8 -0
@@ -335,6 +335,14 @@ static LLVMValueRef pseudo_to_value(struct function *fn, struct instruction *insdefault :assert (0 ); } + }else { +const char *name =show_ident (sym->ident); + + result =LLVMGetNamedGlobal (fn->module, name); +if (!result) { +LLVMTypeRef type =symbol_type (fn->module, sym); + result =LLVMAddGlobal (fn->module, type, name); + } }break ; }
validation/backend/extern.c new file +11 -0
@@ -0,0 +1,11 @@ +extern unsigned long foo; + +static unsigned long bar (void ) +{ +return foo; +} + +/* +* check-name: Extern symbol code generation +* check-command: ./sparsec -c $file -o tmp.o +*/
Signature · 6 lines
1.7.7.6 -- To unsubscribe from this list: send the line "unsubscribe linux-sparse" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 21 lines · Pekka Enberg
Jonathan Neuschäfer writes:
compiling a little real-world program with sparse-llvm, it segfaulted. Using a tool called "delta"[1] and some bash scripting, I managed to reduce the code to this test case:
extern struct foo *foop; extern void func(struct foo *f); int main(int argc, char **argv) { func(foop); }The problem is that
pseudo_to_value()does not know abou the extern symbol because Sparse never callsoutput_data()on it which registers globals withLLVMAddGlobal().As explained by Linus, 'extern' symbols are just names with types. They don't have any value associated with them, they just have the type and the name. Therefore we need to explicitly call
LLVMAddGlobal()for symbols we have not encountered inpseudo_to_value().
Quoted 5 lines
Reported by: Jonathan Neuschäfer <[email protected]>
Cc Christopher Li <[email protected]>Cc Jeff Garzik <[email protected]>Cc Linus Torvalds <[email protected]>Signed-off-by Pekka Enberg <[email protected]>
This fixes the crash, thanks.
Jonathan
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html