Gaier, Bjoern via llvm-dev
2020-Jan-08 15:29 UTC
[llvm-dev] Position independent code writes absolute pointer
Hello everyone, I have an issue with some code that I jit/load as position independent code. I have a feeling that it is not possible to solve the issue but I wanted to give it a try. #include <stdio.h> int magicValue = 123; int magicValue2 = 321; volatile int *pValue = &magicValue; void printMagicValue() { printf("Planschi...\n"); printf("The magic value is %i 0x%p && 0x%p\n", magicValue, &magicValue, pValue); } void setMagicValue(int value) { magicValue = value; } This is the code which I will load as PIC, for the JTMB I use the following settings: JTMB->setRelocationModel(llvm::Reloc::PIC_); JTMB->setCodeModel(llvm::CodeModel::Small); The code will be loaded into a shared memory. Two process will execute the memory from there, calling "printMagicValue", "setMagicValue(120)" and "printMagicValue" again. Only the first process will JIT the code, every other process will access it from the shared memory. The first Process will say: Planschi... The magic value is 123 0x00000270BB090038 && 0x00000270BB090038 Planschi... The magic value is 120 0x00000270BB090038 && 0x00000270BB090038 The second Process will say: Planschi... The magic value is 120 0x00000237A5DE0038 && 0x00000270BB090038 Planschi... The magic value is 120 0x00000237A5DE0038 && 0x00000270BB090038 The values will be read correctly! Hurray! But my problem is, that the pointer 'pValue' was written with an absolute value and not with a PIC conform value. The second process will now print the address from the first process. I hoped, that - since the code is PIC - that also the pointers are written PIC like. I think I understand why this is not the case, but can I somehow change this behaviour without calculating the offset myself? My overall goal is to share the entire code between two processes. I hope my question is somewhat understandable and I hope even more, that there is a solution to this... Thank you for any help in advance and kind greetings Björn Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200108/a02d5996/attachment-0001.html>
Gaier, Bjoern via llvm-dev
2020-Jan-09 08:14 UTC
[llvm-dev] Position independent code writes absolute pointer
I wanted to add an thought to this: Could it be possible to modify the code on the IR-Level to store PIC/offset address and not absolute address? I'm not familiar with the LLVM IR so I don't know what is possible and how it effects the code at all. From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of Gaier, Bjoern via llvm-dev Sent: 08 January 2020 16:29 To: llvm-dev at lists.llvm.org Subject: [llvm-dev] Position independent code writes absolute pointer Hello everyone, I have an issue with some code that I jit/load as position independent code. I have a feeling that it is not possible to solve the issue but I wanted to give it a try. #include <stdio.h> int magicValue = 123; int magicValue2 = 321; volatile int *pValue = &magicValue; void printMagicValue() { printf("Planschi...\n"); printf("The magic value is %i 0x%p && 0x%p\n", magicValue, &magicValue, pValue); } void setMagicValue(int value) { magicValue = value; } This is the code which I will load as PIC, for the JTMB I use the following settings: JTMB->setRelocationModel(llvm::Reloc::PIC_); JTMB->setCodeModel(llvm::CodeModel::Small); The code will be loaded into a shared memory. Two process will execute the memory from there, calling "printMagicValue", "setMagicValue(120)" and "printMagicValue" again. Only the first process will JIT the code, every other process will access it from the shared memory. The first Process will say: Planschi... The magic value is 123 0x00000270BB090038 && 0x00000270BB090038 Planschi... The magic value is 120 0x00000270BB090038 && 0x00000270BB090038 The second Process will say: Planschi... The magic value is 120 0x00000237A5DE0038 && 0x00000270BB090038 Planschi... The magic value is 120 0x00000237A5DE0038 && 0x00000270BB090038 The values will be read correctly! Hurray! But my problem is, that the pointer 'pValue' was written with an absolute value and not with a PIC conform value. The second process will now print the address from the first process. I hoped, that - since the code is PIC - that also the pointers are written PIC like. I think I understand why this is not the case, but can I somehow change this behaviour without calculating the offset myself? My overall goal is to share the entire code between two processes. I hope my question is somewhat understandable and I hope even more, that there is a solution to this... Thank you for any help in advance and kind greetings Björn Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200109/9d817aa7/attachment-0001.html>
Tim Northover via llvm-dev
2020-Jan-09 10:08 UTC
[llvm-dev] Position independent code writes absolute pointer
Hi Gaier, There's no way to do this automatically in LLVM at the moment. It sounds kind of related to pointer compression techniques (also not supported right now). On Thu, 9 Jan 2020 at 08:14, Gaier, Bjoern via llvm-dev <llvm-dev at lists.llvm.org> wrote:> Could it be possible to modify the code on the IR-Level to store PIC/offset address and not absolute address? I’m not familiar with the LLVM IR so I don’t know what is possible and how it effects the code at all.It depends how much control you have over the code. You could instrument code so that it converted all stores of pointers to be relative to some fixed global (PC-relative doesn't work there because it will be loaded at a different address, and "relative to the address it's being stored to" would break memcpy). But that has some major issues: 1. It's an ABI break, so you have to be able to recompile all code, including any system libraries you make use of. 2. LLVM can only convert the pointers it knows about, so it would still be broken by someone storing a pointer via an intptr_t cast and probably other things I haven't thought of. 3. There probably isn't even a relocation for any statically initialized pointers. You might be able to convert all of them to use a dynamic module initializer instead though. 4. I'd expect debugging to go horribly wrong. Cheers. Tim.