Memory leak using bitstream filters with shared libs
Baptiste discovered a rather nasty memory leak when using a bitstream filter with shared libraries. In ffmpeg.c:write_frame() the output of bitstream filters is stored in an AVPacket, and its destruct function is set like this (line 417): new_pkt.destruct= av_destruct_packet; Later on, in av_interleave_packet_per_dts(), AVPacket.destruct is compared against av_destruct_packet, like this (utils.c:2439): if(pkt->destruct == av_destruct_packet) The trouble here is that with shared libraries, the address of av_destruct_packet when used in the main executable or libs other than the one defining it resolves to a trampoline that calls the real function using a GOT entry. As a result, this comparison is always false when the destruct pointer was assigned outside libavformat. Does anyone have an idea for fixing this? -- M?ns Rullg?rd mans at mansr.com
On Sun, Mar 09, 2008 at 12:05:33AM +0000, M?ns Rullg?rd wrote:
Baptiste discovered a rather nasty memory leak when using a bitstream filter with shared libraries.
In ffmpeg.c:write_frame() the output of bitstream filters is stored in an AVPacket, and its destruct function is set like this (line 417):
new_pkt.destruct= av_destruct_packet;
Later on, in av_interleave_packet_per_dts(), AVPacket.destruct is compared against av_destruct_packet, like this (utils.c:2439):
if(pkt->destruct == av_destruct_packet)
The trouble here is that with shared libraries, the address of av_destruct_packet when used in the main executable or libs other than the one defining it resolves to a trampoline that calls the real function using a GOT entry. As a result, this comparison is always false when the destruct pointer was assigned outside libavformat.
Does anyone have an idea for fixing this?
It's actually an implementation bug since the C language requires that the addresses match. I suspect it's binutils doing something stupid due to -Bsymbolic. Anyway this sort of problem is common enough that I'd recommend avoiding comparison of function pointers. I'm not familiar enough with the code to suggest an alternative though. Rich
Rich Felker <dalias at aerifal.cx> writes:
On Sun, Mar 09, 2008 at 12:05:33AM +0000, M?ns Rullg?rd wrote:
Baptiste discovered a rather nasty memory leak when using a bitstream filter with shared libraries.
In ffmpeg.c:write_frame() the output of bitstream filters is stored in an AVPacket, and its destruct function is set like this (line 417):
new_pkt.destruct= av_destruct_packet;
Later on, in av_interleave_packet_per_dts(), AVPacket.destruct is compared against av_destruct_packet, like this (utils.c:2439):
if(pkt->destruct == av_destruct_packet)
The trouble here is that with shared libraries, the address of av_destruct_packet when used in the main executable or libs other than the one defining it resolves to a trampoline that calls the real function using a GOT entry. As a result, this comparison is always false when the destruct pointer was assigned outside libavformat.
Does anyone have an idea for fixing this?
It's actually an implementation bug since the C language requires that the addresses match.
I was hoping you'd say that.
I suspect it's binutils doing something stupid due to -Bsymbolic.
That is my theory as well, though I've not confirmed it.
Anyway this sort of problem is common enough that I'd recommend avoiding comparison of function pointers. I'm not familiar enough with the code to suggest an alternative though.
I dread to think what might happen on Windows. -- M?ns Rullg?rd mans at mansr.com
On Sat, 2008-03-08 at 19:23 -0500, Rich Felker wrote:
It's actually an implementation bug since the C language requires that the addresses match. I suspect it's binutils doing something stupid due to -Bsymbolic. Anyway this sort of problem is common enough that
It's binutils doing what -Bsymbolic is documented to do - binding references to global symbols to the definition within the shared library. If the main program is compiled without -fPIC then functions are normally bound to the main program's PLT entry (if one exists for the function in question), and that's what other code will use.
On Sun, Mar 09, 2008 at 04:48:11AM +0200, Uoti Urpala wrote:
On Sat, 2008-03-08 at 19:23 -0500, Rich Felker wrote:
It's actually an implementation bug since the C language requires that the addresses match. I suspect it's binutils doing something stupid due to -Bsymbolic. Anyway this sort of problem is common enough that
It's binutils doing what -Bsymbolic is documented to do - binding references to global symbols to the definition within the shared library. If the main program is compiled without -fPIC then functions are normally bound to the main program's PLT entry (if one exists for the function in question), and that's what other code will use.
"binding references to global symbols to the definition within the shared library" is one thing and thats fine. The bug is that the references from outside the shared lib bind to something else. Its not allowed by the c standard as others have already said and not documented as side effect of -Bsymbolic. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB it is not once nor twice but times without number that the same ideas make their appearance in the world. -- Aristotle
On Sun, 2008-03-09 at 04:25 +0100, Michael Niedermayer wrote:
On Sun, Mar 09, 2008 at 04:48:11AM +0200, Uoti Urpala wrote:
On Sat, 2008-03-08 at 19:23 -0500, Rich Felker wrote:
It's actually an implementation bug since the C language requires that the addresses match. I suspect it's binutils doing something stupid due to -Bsymbolic. Anyway this sort of problem is common enough that
It's binutils doing what -Bsymbolic is documented to do - binding references to global symbols to the definition within the shared library. If the main program is compiled without -fPIC then functions are normally bound to the main program's PLT entry (if one exists for the function in question), and that's what other code will use.
"binding references to global symbols to the definition within the shared library" is one thing and thats fine. The bug is that the references from outside the shared lib bind to something else.
The "bug" is that references in different places bind to different targets. You can't say that changing references in one place to point to a different target is "fine" if you also care about all references being in sync.
Its not allowed by the c standard as others have already said and not documented as side effect of -Bsymbolic.
If you want behavior as closely matching the C standard as possible then don't use -Bsymbolic. There's not much else it could do - you can't both change reference targets in one place AND keep them in sync with other places! Any attempt to make -Bsymbolic also change the addresses seen by a non-PIC main program would need to involve things like adding relocations to be done at runtime to the main program code with values depending on shared library contents... The (at least somewhat) practical alternatives I can think of are: 1. Accept that addresses inside and outside the library will not match 2. Stop using -Bsymbolic (you can still use visibility to bind non-public symbols inside the library) 3. Compile everything with -fPIC, even the program using the library
On Sun, Mar 09, 2008 at 06:37:01AM +0200, Uoti Urpala wrote:
On Sun, 2008-03-09 at 04:25 +0100, Michael Niedermayer wrote:
On Sun, Mar 09, 2008 at 04:48:11AM +0200, Uoti Urpala wrote:
On Sat, 2008-03-08 at 19:23 -0500, Rich Felker wrote:
It's actually an implementation bug since the C language requires that the addresses match. I suspect it's binutils doing something stupid due to -Bsymbolic. Anyway this sort of problem is common enough that
It's binutils doing what -Bsymbolic is documented to do - binding references to global symbols to the definition within the shared library. If the main program is compiled without -fPIC then functions are normally bound to the main program's PLT entry (if one exists for the function in question), and that's what other code will use.
"binding references to global symbols to the definition within the shared library" is one thing and thats fine. The bug is that the references from outside the shared lib bind to something else.
The "bug" is that references in different places bind to different targets. You can't say that changing references in one place to point to a different target is "fine" if you also care about all references being in sync.
Its not allowed by the c standard as others have already said and not documented as side effect of -Bsymbolic.
If you want behavior as closely matching the C standard as possible then don't use -Bsymbolic. There's not much else it could do - you can't both change reference targets in one place AND keep them in sync with other places!
Any attempt to make -Bsymbolic also change the addresses seen by a non-PIC main program would need to involve things like adding relocations to be done at runtime to the main program code with values depending on shared library contents...
Curently AFAIK the address is written in the PLT or the PLT reads it from the GOT in the case you describe, thus in some sense the PLT contains hundreads of relocations. Instead of updating the addresses in these tables the actually used addresses in the program should be updated, it would be one layer of indirection less and be much more efficient as well. Besides that it would conform to the C standard with -Bsymbolic.
The (at least somewhat) practical alternatives I can think of are: 1. Accept that addresses inside and outside the library will not match 2. Stop using -Bsymbolic (you can still use visibility to bind non-public symbols inside the library) 3. Compile everything with -fPIC, even the program using the library
True, these are the things we can do still i consider these workarounds of a bug in binutils. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The educated differ from the uneducated as much as the living from the dead. -- Aristotle
On Sun, 2008-03-09 at 14:14 +0100, Michael Niedermayer wrote:
On Sun, Mar 09, 2008 at 06:37:01AM +0200, Uoti Urpala wrote:
Any attempt to make -Bsymbolic also change the addresses seen by a non-PIC main program would need to involve things like adding relocations to be done at runtime to the main program code with values depending on shared library contents...
Curently AFAIK the address is written in the PLT or the PLT reads it from the GOT in the case you describe, thus
PLT has "jmp *X". Initially the value at X points to a function to do runtime lookup for the function in question. When called for the first time this function writes the lookup result at X. It would be slightly faster to have a direct jump "jmp Y" in the PLT instead (and modify the value Y in the code), but this is not done to avoid having a writable+executable memory area.
in some sense the PLT contains hundreads of relocations. Instead of updating the addresses in these tables the actually used addresses in the program should be updated, it would be one layer of indirection less and be much more efficient as well.
Handling all symbols that way would require keeping track of a huge number of relocations. It would require the code pages to be writable, and modifying the pages would prevent sharing them between processes. Doing a relocation of every library function call (and touching every code page) in a big program at startup would give bad startup performance, and doing on-demand lookup of every call site (as opposed to every called function) would be inefficient.
The (at least somewhat) practical alternatives I can think of are: 1. Accept that addresses inside and outside the library will not match 2. Stop using -Bsymbolic (you can still use visibility to bind non-public symbols inside the library) 3. Compile everything with -fPIC, even the program using the library
True, these are the things we can do still i consider these workarounds of a bug in binutils.
Can you even specify what "less buggy" behavior would be and how it would differ? (I don't think that your "modify code everywhere to do relocations" above is an answer as binutils alone could not do that, even if it were doable otherwise).
Uoti Urpala <uoti.urpala at pp1.inet.fi> writes:
On Sun, 2008-03-09 at 14:14 +0100, Michael Niedermayer wrote:
On Sun, Mar 09, 2008 at 06:37:01AM +0200, Uoti Urpala wrote:
Any attempt to make -Bsymbolic also change the addresses seen by a non-PIC main program would need to involve things like adding relocations to be done at runtime to the main program code with values depending on shared library contents...
Curently AFAIK the address is written in the PLT or the PLT reads it from the GOT in the case you describe, thus
PLT has "jmp *X". Initially the value at X points to a function to do runtime lookup for the function in question. When called for the first time this function writes the lookup result at X.
It would be slightly faster to have a direct jump "jmp Y" in the PLT instead (and modify the value Y in the code), but this is not done to avoid having a writable+executable memory area.
Why can't the code that takes the address of a function get it from the GOT instead of returning the address of the PLT entry? The only requirement for this to work would be forcing any functions whose addresses are taken to be resolved at load time. There is no need for modifying code areas at runtime at all.
The (at least somewhat) practical alternatives I can think of are: 1. Accept that addresses inside and outside the library will not match 2. Stop using -Bsymbolic (you can still use visibility to bind non-public symbols inside the library) 3. Compile everything with -fPIC, even the program using the library
True, these are the things we can do still i consider these workarounds of a bug in binutils.
Can you even specify what "less buggy" behavior would be and how it would differ?
Taking the address of function should give the same result everywhere, regardless of compiler/linker flags. Anything else is in violation of the C standard. -- M?ns Rullg?rd mans at mansr.com
On Sun, 2008-03-09 at 18:35 +0000, M?ns Rullg?rd wrote:
Why can't the code that takes the address of a function get it from the GOT instead of returning the address of the PLT entry? The only requirement for this to work would be forcing any functions whose addresses are taken to be resolved at load time. There is no need for modifying code areas at runtime at all.
That IS what happens in PIC code. The issue that started this thread happened because there was non-PIC code using the address in the process. I already explained in reply to your earlier mail that the code generated for ffmpeg.c ("movq $0x4038a0,0x48(%rsp)") writes a direct value that is included in the assembler instruction itself. You need to use -fPIC if you want the compiler to generate extra indirection through the GOT.
Taking the address of function should give the same result everywhere, regardless of compiler/linker flags. Anything else is in violation of the C standard.
What's your point here? You think a compiler or linker must not have flags like -Bsymbolic that can generate behavior different from the C standard?
Uoti Urpala <uoti.urpala at pp1.inet.fi> writes:
On Sun, 2008-03-09 at 18:35 +0000, M?ns Rullg?rd wrote:
Why can't the code that takes the address of a function get it from the GOT instead of returning the address of the PLT entry? The only requirement for this to work would be forcing any functions whose addresses are taken to be resolved at load time. There is no need for modifying code areas at runtime at all.
That IS what happens in PIC code. The issue that started this thread happened because there was non-PIC code using the address in the process. I already explained in reply to your earlier mail that the code generated for ffmpeg.c ("movq $0x4038a0,0x48(%rsp)") writes a direct value that is included in the assembler instruction itself. You need to use -fPIC if you want the compiler to generate extra indirection through the GOT.
So you're basically saying that calling PIC code from non-PIC code doesn't work reliably, and that you're fine with that?
Taking the address of function should give the same result everywhere, regardless of compiler/linker flags. Anything else is in violation of the C standard.
What's your point here? You think a compiler or linker must not have flags like -Bsymbolic that can generate behavior different from the C standard?
Yes. At the very least, such options should be clearly documented as violating the standard, and when possible, a warning should be issued whenever this happens. -- M?ns Rullg?rd mans at mansr.com
On Sun, 2008-03-09 at 19:30 +0000, M?ns Rullg?rd wrote:
Uoti Urpala <uoti.urpala at pp1.inet.fi> writes:
On Sun, 2008-03-09 at 18:35 +0000, M?ns Rullg?rd wrote:
Why can't the code that takes the address of a function get it from the GOT instead of returning the address of the PLT entry? The only requirement for this to work would be forcing any functions whose addresses are taken to be resolved at load time. There is no need for modifying code areas at runtime at all.
That IS what happens in PIC code. The issue that started this thread happened because there was non-PIC code using the address in the process. I already explained in reply to your earlier mail that the code generated for ffmpeg.c ("movq $0x4038a0,0x48(%rsp)") writes a direct value that is included in the assembler instruction itself. You need to use -fPIC if you want the compiler to generate extra indirection through the GOT.
So you're basically saying that calling PIC code from non-PIC code doesn't work reliably, and that you're fine with that?
No. Everything works reliably as long as you don't use -Bsymbolic. Even if you do use -Bsymbolic calling still works reliably. What -Bsymbolic makes unreliable is the addresses of objects defined in the library. For functions code inside the library will see the address of the main function while code outside the library may see an address in the PLT. For other variables code inside the library will see the address of the original variable whereas code outside may see a copy relocation. The addresses outside can be the same if there is no non-PIC code that references the symbol. So basically IF you use -Bsymbolic AND do not compile the main program with -fPIC then you can only rely on symbols defined in the library to refer to similarly behaving objects inside and outside, not necessarily to refer to the same object at the same address.
Uoti Urpala <uoti.urpala at pp1.inet.fi> writes:
On Sun, 2008-03-09 at 19:30 +0000, M?ns Rullg?rd wrote:
Uoti Urpala <uoti.urpala at pp1.inet.fi> writes:
On Sun, 2008-03-09 at 18:35 +0000, M?ns Rullg?rd wrote:
Why can't the code that takes the address of a function get it from the GOT instead of returning the address of the PLT entry? The only requirement for this to work would be forcing any functions whose addresses are taken to be resolved at load time. There is no need for modifying code areas at runtime at all.
That IS what happens in PIC code. The issue that started this thread happened because there was non-PIC code using the address in the process. I already explained in reply to your earlier mail that the code generated for ffmpeg.c ("movq $0x4038a0,0x48(%rsp)") writes a direct value that is included in the assembler instruction itself. You need to use -fPIC if you want the compiler to generate extra indirection through the GOT.
So you're basically saying that calling PIC code from non-PIC code doesn't work reliably, and that you're fine with that?
No. Everything works reliably as long as you don't use -Bsymbolic. Even if you do use -Bsymbolic calling still works reliably. What -Bsymbolic makes unreliable is the addresses of objects defined in the library. For functions code inside the library will see the address of the main function while code outside the library may see an address in the PLT. For other variables code inside the library will see the address of the original variable whereas code outside may see a copy relocation. The addresses outside can be the same if there is no non-PIC code that references the symbol.
So basically IF you use -Bsymbolic AND do not compile the main program with -fPIC then you can only rely on symbols defined in the library to refer to similarly behaving objects inside and outside, not necessarily to refer to the same object at the same address.
I know how it works, and I am not at all happy with it. It is in blatant violation of the C standard, without so much as a hint about this in the manual. -- M?ns Rullg?rd mans at mansr.com
On Sun, Mar 09, 2008 at 08:52:37PM +0200, Uoti Urpala wrote:
On Sun, 2008-03-09 at 18:35 +0000, M?ns Rullg?rd wrote:
Why can't the code that takes the address of a function get it from the GOT instead of returning the address of the PLT entry? The only requirement for this to work would be forcing any functions whose addresses are taken to be resolved at load time. There is no need for modifying code areas at runtime at all.
That IS what happens in PIC code. The issue that started this thread happened because there was non-PIC code using the address in the process. I already explained in reply to your earlier mail that the code generated for ffmpeg.c ("movq $0x4038a0,0x48(%rsp)") writes a direct value that is included in the assembler instruction itself. You need to use -fPIC if you want the compiler to generate extra indirection through the GOT.
If the code is non-PIC then the 0x4038a0 in the library needs to be patched at runtime anyway to account for the library load address. No harm would be done by the linker generating the correct type of relocation instead of the incorrect (fixed offset from load address) relocation it's generating now.
Taking the address of function should give the same result everywhere, regardless of compiler/linker flags. Anything else is in violation of the C standard.
What's your point here? You think a compiler or linker must not have flags like -Bsymbolic that can generate behavior different from the C standard?
I think it's wrong to have such flags especially when not documenting them as such, and when the sane behavior of the flag would not conflict with the standard. -Bsymbolic should be (and should be understood) as follows: "Make link-time optimizations that are possible based on the requirements the C language places on strictly-conforming programs, disabling overhead that is needed for non-conformant programs relying on ELF symbol resolution semantics." This "should" is purely a moral judgement so I'm uninterested in Uoti's ideas contrary to it. Rich
On Sun, 2008-03-09 at 15:37 -0400, Rich Felker wrote:
On Sun, Mar 09, 2008 at 08:52:37PM +0200, Uoti Urpala wrote:
That IS what happens in PIC code. The issue that started this thread happened because there was non-PIC code using the address in the process. I already explained in reply to your earlier mail that the code generated for ffmpeg.c ("movq $0x4038a0,0x48(%rsp)") writes a direct value that is included in the assembler instruction itself. You need to use -fPIC if you want the compiler to generate extra indirection through the GOT.
If the code is non-PIC then the 0x4038a0 in the library needs to be patched at runtime anyway to account for the library load address. No
Yes that happens if there is non-PIC code in a library. But that's not the case we're talking about here. See above - this is ffmpeg.c code. We're talking about code generated for the main program.
On Sun, Mar 09, 2008 at 08:11:58PM +0200, Uoti Urpala wrote:
On Sun, 2008-03-09 at 14:14 +0100, Michael Niedermayer wrote:
On Sun, Mar 09, 2008 at 06:37:01AM +0200, Uoti Urpala wrote:
Any attempt to make -Bsymbolic also change the addresses seen by a non-PIC main program would need to involve things like adding relocations to be done at runtime to the main program code with values depending on shared library contents...
Curently AFAIK the address is written in the PLT or the PLT reads it from the GOT in the case you describe, thus
PLT has "jmp *X". Initially the value at X points to a function to do runtime lookup for the function in question. When called for the first time this function writes the lookup result at X.
It would be slightly faster to have a direct jump "jmp Y" in the PLT instead (and modify the value Y in the code), but this is not done to avoid having a writable+executable memory area.
Now while this is off topic, avoiding writable+executable memory by having function pointers writeable and in a well known location does not help security much. The GOT by all means should be read only after loading. But again this isnt what we are discussing in this thread ...
in some sense the PLT contains hundreads of relocations. Instead of updating the addresses in these tables the actually used addresses in the program should be updated, it would be one layer of indirection less and be much more efficient as well.
Handling all symbols that way would require keeping track of a huge number of relocations. It would require the code pages to be writable, and modifying the pages would prevent sharing them between processes. Doing a relocation of every library function call (and touching every code page) in a big program at startup would give bad startup performance, and doing on-demand lookup of every call site (as opposed to every called function) would be inefficient.
The (at least somewhat) practical alternatives I can think of are: 1. Accept that addresses inside and outside the library will not match 2. Stop using -Bsymbolic (you can still use visibility to bind non-public symbols inside the library) 3. Compile everything with -fPIC, even the program using the library
True, these are the things we can do still i consider these workarounds of a bug in binutils.
Can you even specify what "less buggy" behavior would be and how it would differ? (I don't think that your "modify code everywhere to do relocations" above is an answer as binutils alone could not do that, even if it were doable otherwise).
First there is richs suggestion of simply only binding calls but not "address of" to local code when -Bsymbolic is used. This is simple, practical and correct even if it isnt the best solution. There are 2 further obvious solutions: 1. Is that an application would have all spots in it which use addresses of symbols be corrected during loading. This is surely faster than the disk from which it is loaded thus no performance penalty compared to a single table of such indirections. If the application is loaded a 2nd time then the memory layout of the first is simply duplicated, so that all libs are at the same spot, not a single text-rel needs to be done here and not a single page from any lib or the app, PIC or not would be duplicated, they all can be shared. The only possible performance penalty would be that the relocations need to be either written to disk in case of page outs or be done during page ins, though i suspect that again disk IO is more limiting than updating the addresses. 2. Would be quite similar to above, the difference would be that symbol addresses would be resolved during prelinking so that no further changes to pages are needed at all during loading or page ins. This system could also fall back to above if new libs had been installed after prelinking. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Observe your enemies, for they first find out your faults. -- Antisthenes
On Sun, 2008-03-09 at 21:59 +0100, Michael Niedermayer wrote:
On Sun, Mar 09, 2008 at 08:11:58PM +0200, Uoti Urpala wrote:
On Sun, 2008-03-09 at 14:14 +0100, Michael Niedermayer wrote:
True, these are the things we can do still i consider these workarounds of a bug in binutils.
Can you even specify what "less buggy" behavior would be and how it would differ? (I don't think that your "modify code everywhere to do relocations" above is an answer as binutils alone could not do that, even if it were doable otherwise).
First there is richs suggestion of simply only binding calls but not "address of" to local code when -Bsymbolic is used. This is simple, practical and correct even if it isnt the best solution.
Correct for some use, but not for what -Bsymbolic is documented to do.
There are 2 further obvious solutions:
These are a lot bigger changes than what could be called fixing a "binutils bug".
1. Is that an application would have all spots in it which use addresses of symbols be corrected during loading. This is surely faster than the disk from which it is loaded thus no performance penalty compared to a single table of such indirections. If the application is loaded a 2nd time then the memory layout of the first is simply duplicated, so that all libs are at the same spot, not a single text-rel needs to be done here and not a single page from any lib or the app, PIC or not would be duplicated, they all can be shared. The only possible performance penalty would be that the relocations need to be either written to disk in case of page outs or be done during page ins, though i suspect that again disk IO is more limiting than updating the addresses.
It's normal for an application not to have its whole code in RAM. If you do the relocations at once when starting then the whole image needs to be loaded from disk to RAM before starting, and then unneeded portions must later be written to swap to free the RAM. This both significantly slows startup and generates extra disk IO. If you try to do relocations on demand when loading pages from disk then you must keep a data structure that allows quickly identifying all needed relocations on any page. There would also be other practical problems but this is going pretty far into hypothetical behavior that no one will implement anyway...
On Mon, Mar 10, 2008 at 12:11:57AM +0200, Uoti Urpala wrote:
On Sun, 2008-03-09 at 21:59 +0100, Michael Niedermayer wrote:
On Sun, Mar 09, 2008 at 08:11:58PM +0200, Uoti Urpala wrote:
On Sun, 2008-03-09 at 14:14 +0100, Michael Niedermayer wrote:
True, these are the things we can do still i consider these workarounds of a bug in binutils.
Can you even specify what "less buggy" behavior would be and how it would differ? (I don't think that your "modify code everywhere to do relocations" above is an answer as binutils alone could not do that, even if it were doable otherwise).
First there is richs suggestion of simply only binding calls but not "address of" to local code when -Bsymbolic is used. This is simple, practical and correct even if it isnt the best solution.
Correct for some use, but not for what -Bsymbolic is documented to do.
-Bsymbolic does not do what its documented to do anyway, its not documented to be incompatible with ANSI/ISO C code.
There are 2 further obvious solutions:
These are a lot bigger changes than what could be called fixing a "binutils bug".
Yes its fixing many bugs at once ...
1. Is that an application would have all spots in it which use addresses of symbols be corrected during loading. This is surely faster than the disk from which it is loaded thus no performance penalty compared to a single table of such indirections. If the application is loaded a 2nd time then the memory layout of the first is simply duplicated, so that all libs are at the same spot, not a single text-rel needs to be done here and not a single page from any lib or the app, PIC or not would be duplicated, they all can be shared. The only possible performance penalty would be that the relocations need to be either written to disk in case of page outs or be done during page ins, though i suspect that again disk IO is more limiting than updating the addresses.
It's normal for an application not to have its whole code in RAM. If you do the relocations at once when starting then the whole image needs to be loaded from disk to RAM before starting, and then unneeded portions must later be written to swap to free the RAM. This both significantly slows startup and generates extra disk IO.
Loading the whole app is much faster than loading pages on demand. Simply because the maybe 20mb a larger app has can be read quite quickly. Loading it all as 4kb pages as they are accessed (with a 10ms seek for each on a fast HD) will take much longer.
If you try to do relocations on demand when loading pages from disk then you must keep a data structure that allows quickly identifying all needed relocations on any page.
For example a linked list: i= get_initial_ptr_for_page_from_locked_mem(page_id); while(i<1024){ unsigened int p=((uint32_t*)page)[i]; i= p & 2047; ((uint32_t*)page)[i]= GOT[p>>11]; } This is O(num of relocs), thus many orders of magnitude faster than disk IO. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Dictatorship naturally arises out of democracy, and the most aggravated form of tyranny and slavery out of the most extreme liberty. -- Plato
Uoti Urpala <uoti.urpala at pp1.inet.fi> writes:
On Sat, 2008-03-08 at 19:23 -0500, Rich Felker wrote:
It's actually an implementation bug since the C language requires that the addresses match. I suspect it's binutils doing something stupid due to -Bsymbolic. Anyway this sort of problem is common enough that
It's binutils doing what -Bsymbolic is documented to do - binding references to global symbols to the definition within the shared library. If the main program is compiled without -fPIC then functions are normally bound to the main program's PLT entry (if one exists for the function in question), and that's what other code will use.
I can't see a reason why both bits of code can't use similar indirection to pick up the real address of the function. The code in question in utils.c is compiled to mov 0x2755d4(%rip),%rax whereas the relevant instruction from ffmpeg.c is movq $0x4038a0,0x48(%rsp) The address 0x4038a0 is a PLT entry with a jump instruction: jmpq *0x20e9a2(%rip) Why can't the movq instruction reference the GOT instead of using the PLT entry? I realise this would prohibit lazy binding of this symbol, but surely there's a way to handle that. -- M?ns Rullg?rd mans at mansr.com
On Sun, 2008-03-09 at 03:42 +0000, M?ns Rullg?rd wrote:
I can't see a reason why both bits of code can't use similar indirection to pick up the real address of the function.
They can - if you compile both with -fPIC.
whereas the relevant instruction from ffmpeg.c is
movq $0x4038a0,0x48(%rsp)
The address 0x4038a0 is a PLT entry with a jump instruction:
jmpq *0x20e9a2(%rip)
Why can't the movq instruction reference the GOT instead of using the PLT entry? I realise this would prohibit lazy binding of this symbol, but surely there's a way to handle that.
Note that the mov is writing a direct value, not reading from any memory address. I assume you compiled it without -fPIC. If you compile with -fPIC then it will use indirection through GOT.
On Sun, Mar 09, 2008 at 04:48:11AM +0200, Uoti Urpala wrote:
On Sat, 2008-03-08 at 19:23 -0500, Rich Felker wrote:
It's actually an implementation bug since the C language requires that the addresses match. I suspect it's binutils doing something stupid due to -Bsymbolic. Anyway this sort of problem is common enough that
It's binutils doing what -Bsymbolic is documented to do - binding references to global symbols to the definition within the shared library. If the main program is compiled without -fPIC then functions are normally bound to the main program's PLT entry (if one exists for the function in question), and that's what other code will use.
This is a bug, plain and simple, in the linker. Binding _call_ references is valid, but binding address-of references is not. The latter must be left either as a GOT lookup (if compiling PIC code) or a textrel (for non-PIC code). Rich
On Sun, 2008-03-09 at 09:26 -0400, Rich Felker wrote:
On Sun, Mar 09, 2008 at 04:48:11AM +0200, Uoti Urpala wrote:
On Sat, 2008-03-08 at 19:23 -0500, Rich Felker wrote:
It's actually an implementation bug since the C language requires that the addresses match. I suspect it's binutils doing something stupid due to -Bsymbolic. Anyway this sort of problem is common enough that
It's binutils doing what -Bsymbolic is documented to do - binding references to global symbols to the definition within the shared library. If the main program is compiled without -fPIC then functions are normally bound to the main program's PLT entry (if one exists for the function in question), and that's what other code will use.
This is a bug, plain and simple, in the linker. Binding _call_ references is valid, but binding address-of references is not. The latter must be left either as a GOT lookup (if compiling PIC code) or a textrel (for non-PIC code).
Must be left as a GOT lookup - lookup of what? If you leave it the same lookup as without -Bsymbolic then it could return a function completely different from the one in the library. That would be completely inconsistent (a direct call and an indirect call that gets the address first calling different functions within the same block of code), and would violate the documented behavior of -Bsymbolic (that it binds references to the definition within the library).
On Sun, Mar 09, 2008 at 08:21:48PM +0200, Uoti Urpala wrote:
On Sun, 2008-03-09 at 09:26 -0400, Rich Felker wrote:
On Sun, Mar 09, 2008 at 04:48:11AM +0200, Uoti Urpala wrote:
On Sat, 2008-03-08 at 19:23 -0500, Rich Felker wrote:
It's actually an implementation bug since the C language requires that the addresses match. I suspect it's binutils doing something stupid due to -Bsymbolic. Anyway this sort of problem is common enough that
It's binutils doing what -Bsymbolic is documented to do - binding references to global symbols to the definition within the shared library. If the main program is compiled without -fPIC then functions are normally bound to the main program's PLT entry (if one exists for the function in question), and that's what other code will use.
This is a bug, plain and simple, in the linker. Binding _call_ references is valid, but binding address-of references is not. The latter must be left either as a GOT lookup (if compiling PIC code) or a textrel (for non-PIC code).
Must be left as a GOT lookup - lookup of what? If you leave it the same lookup as without -Bsymbolic then it could return a function completely different from the one in the library.
Only for code that has undefined behavior already! What binutils is doing, and what you're defending them in regard to, is breaking valid C code for the sake of invalid C code with undefined behavior. Rich
On Sun, Mar 09, 2008 at 12:05:33AM +0000, M?ns Rullg?rd wrote:
Baptiste discovered a rather nasty memory leak when using a bitstream filter with shared libraries.
In ffmpeg.c:write_frame() the output of bitstream filters is stored in an AVPacket, and its destruct function is set like this (line 417):
new_pkt.destruct= av_destruct_packet;
Later on, in av_interleave_packet_per_dts(), AVPacket.destruct is compared against av_destruct_packet, like this (utils.c:2439):
if(pkt->destruct == av_destruct_packet)
This seems like a misuse of pkt->destruct to me anyway, and for a simple optimization in addition. I am still not entirely certain I understood what this does, but I think that a pkt->flag PKT_FREE_ON_DESTRUCT or some such would do. This would still not fix applications outside of libavf without changing their code as well, but IMO it would be more flexible and correct. Apart from that: why is av_destruct_packet even part of the public API? Is there actually any sane case where applications should use this (except working around the fact that av_interleave_packet_per_dts stupidly compares the destruct function instead of checking if pkt->data was malloced or something, like proposed flag would allow)? To me it seems that needing av_destruct_packet to be part of the public API means out API is broken. Greetings, Reimar D?ffinger
On Sun, Mar 09, 2008 at 07:50:16PM +0100, Reimar D?ffinger wrote:
On Sun, Mar 09, 2008 at 12:05:33AM +0000, M?ns Rullg?rd wrote:
Baptiste discovered a rather nasty memory leak when using a bitstream filter with shared libraries.
In ffmpeg.c:write_frame() the output of bitstream filters is stored in an AVPacket, and its destruct function is set like this (line 417):
new_pkt.destruct= av_destruct_packet;
Later on, in av_interleave_packet_per_dts(), AVPacket.destruct is compared against av_destruct_packet, like this (utils.c:2439):
if(pkt->destruct == av_destruct_packet)
This seems like a misuse of pkt->destruct to me anyway, and for a simple optimization in addition. I am still not entirely certain I understood what this does, but I think that a pkt->flag PKT_FREE_ON_DESTRUCT or some such would do.
ugh .... No it would not do. It completely defeats the sense behind destruct() that is to allow a destruct different from what av_free() does. One solution i could see is to: static inline void av_free_packet(AVPacket *pkt) { - if (pkt && pkt->destruct) { - pkt->destruct(pkt); + if(pkt){ + if(pkt->destruct) + pkt->destruct(pkt); + pkt->data = NULL; pkt->size = 0; } } ---- void av_destruct_packet(AVPacket *pkt) { av_free(pkt->data); - pkt->data = NULL; pkt->size = 0; } ---- -void av_destruct_packet_nofree(AVPacket *pkt) -{ - pkt->data = NULL; pkt->size = 0; -} and use NULL instead of av_destruct_packet_nofree Anyway ive no interrest to workaround binutil bugs, if anyone cares he has to do the work and testing. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB No great genius has ever existed without some touch of madness. -- Aristotle
On Sun, 2008-03-09 at 23:00 +0100, Michael Niedermayer wrote:
Anyway ive no interrest to workaround binutil bugs, if anyone cares he has to do the work and testing.
That's not a bug. At most it could be called a documentation flaw for not explaining the caveats clearly enough. If you don't want that behavior then remove -Bsymbolic from the flags. If you want to use -Bsymbolic then make the code work with the behavior it implies.
On Mon, Mar 10, 2008 at 12:38:46AM +0200, Uoti Urpala wrote:
On Sun, 2008-03-09 at 23:00 +0100, Michael Niedermayer wrote:
Anyway ive no interrest to workaround binutil bugs, if anyone cares he has to do the work and testing.
That's not a bug. At most it could be called a documentation flaw for not explaining the caveats clearly enough.
Its a bug, according to the ISO C standard. It can be fixed in binutils or by removing the claim that binutils support ISO C, in that sense yes it can be solved in the "docs". [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Concerning the gods, I have no means of knowing whether they exist or not or of what sort they may be, because of the obscurity of the subject, and the brevity of human life -- Protagoras
On Mon, 2008-03-10 at 01:07 +0100, Michael Niedermayer wrote:
On Mon, Mar 10, 2008 at 12:38:46AM +0200, Uoti Urpala wrote:
On Sun, 2008-03-09 at 23:00 +0100, Michael Niedermayer wrote:
Anyway ive no interrest to workaround binutil bugs, if anyone cares he has to do the work and testing.
That's not a bug. At most it could be called a documentation flaw for not explaining the caveats clearly enough.
Its a bug, according to the ISO C standard. It can be fixed in binutils
This is getting silly... The C standard does not require that you must be able to give tools whatever random options you see on the manpage without breaking anything.
On Mon, Mar 10, 2008 at 03:38:51AM +0200, Uoti Urpala wrote:
On Mon, 2008-03-10 at 01:07 +0100, Michael Niedermayer wrote:
On Mon, Mar 10, 2008 at 12:38:46AM +0200, Uoti Urpala wrote:
On Sun, 2008-03-09 at 23:00 +0100, Michael Niedermayer wrote:
Anyway ive no interrest to workaround binutil bugs, if anyone cares he has to do the work and testing.
That's not a bug. At most it could be called a documentation flaw for not explaining the caveats clearly enough.
Its a bug, according to the ISO C standard. It can be fixed in binutils
This is getting silly... The C standard does not require that you must be able to give tools whatever random options you see on the manpage without breaking anything.
It's not silly. The tool provides options that cause subtle breakage without any documentation of that fact, when avoiding the breakage would be trivial. That is what i call a BUG. Plain and simple. But I'm quite confident that the Redhat^H^H^H^H^H^HGNU folks will think differently after all the crack they've done. Rich
Rich Felker wrote:
It's not silly. The tool provides options that cause subtle breakage without any documentation of that fact, when avoiding the breakage would be trivial. That is what i call a BUG. Plain and simple. But I'm quite confident that the Redhat^H^H^H^H^H^HGNU folks will think differently after all the crack they've done.
Where is the bug number in the gcc.gnu bugzie? lu -- Luca Barbato Gentoo Council Member Gentoo/linux Gentoo/PPC http://dev.gentoo.org/~lu_zero
On Mon, Mar 10, 2008 at 02:23:32PM +0100, Luca Barbato wrote:
Rich Felker wrote:
It's not silly. The tool provides options that cause subtle breakage without any documentation of that fact, when avoiding the breakage would be trivial. That is what i call a BUG. Plain and simple. But I'm quite confident that the Redhat^H^H^H^H^H^HGNU folks will think differently after all the crack they've done.
Where is the bug number in the gcc.gnu bugzie?
Good question ... I guess i should refuse a workaround until the bug has been reported, maybe they do fix it. But even if not it should be reported. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Concerning the gods, I have no means of knowing whether they exist or not or of what sort they may be, because of the obscurity of the subject, and the brevity of human life -- Protagoras
On Tue, 2008-03-11 at 18:52 +0100, Michael Niedermayer wrote:
I guess i should refuse a workaround until the bug has been reported, maybe they do fix it. But even if not it should be reported.
And what is the "bug"? That you can't both change what the symbols are bound to AND keep them the same to match what they're bound to elsewhere? I wouldn't expect a fix for that dilemma... Maybe you could get them to explain the problem more clearly in the documentation, but don't expect them to change the documented semantics of -Bsymbolic. If you want another option that does something different that's a separate feature request, not a bugfix.
On Tue, Mar 11, 2008 at 09:20:06PM +0200, Uoti Urpala wrote:
On Tue, 2008-03-11 at 18:52 +0100, Michael Niedermayer wrote:
I guess i should refuse a workaround until the bug has been reported, maybe they do fix it. But even if not it should be reported.
And what is the "bug"? That you can't both change what the symbols are bound to AND keep them the same to match what they're bound to elsewhere? I wouldn't expect a fix for that dilemma...
There is just one symbol and that is in the library -Bsymbolic as it is documented removes the special feature of ELF shared libs to override such symbols. The C standard (as well as common sense) requires all references to the symbol to be equal. This is not optional or dependant on command line options. Its a strict requirement of C. The bug is that this requirement is broken when the lib is compiled with PIC and -Bsymbolic and the main app is not compiled with PIC.
Maybe you could get them to explain the problem more clearly in the documentation,
And this would be better than the current situation ...
but don't expect them to change the documented semantics of -Bsymbolic.
Where is that documentation which says that the main app will see a different symbol than the lib with -Bsymbolic. Its certainly not in the documentation of -Bsymbolic, as such these are not the "documented semantics".
If you want another option that does something different that's a separate feature request, not a bugfix.
I wonder why you cannot just accept that you were wrong ... [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Let us carefully observe those good qualities wherein our enemies excel us and endeavor to excel them, by avoiding what is faulty, and imitating what is excellent in them. -- Plutarch
On Tue, 2008-03-11 at 21:14 +0100, Michael Niedermayer wrote:
On Tue, Mar 11, 2008 at 09:20:06PM +0200, Uoti Urpala wrote:
On Tue, 2008-03-11 at 18:52 +0100, Michael Niedermayer wrote:
I guess i should refuse a workaround until the bug has been reported, maybe they do fix it. But even if not it should be reported.
And what is the "bug"? That you can't both change what the symbols are bound to AND keep them the same to match what they're bound to elsewhere? I wouldn't expect a fix for that dilemma...
There is just one symbol and that is in the library -Bsymbolic as it is documented removes the special feature of ELF shared libs to override such symbols.
I think the expected use case is some kind of "poor man's visibility support". The documentation only talks from the viewpoint of internal symbols, and doesn't explain the resulting side effects on symbols that are meant to be public.
The C standard (as well as common sense) requires all references to the symbol to be equal. This is not optional or dependant on command line options. Its a strict requirement of C. The bug is that this requirement is broken when the lib is compiled with PIC and -Bsymbolic and the main app is not compiled with PIC.
So if someone manages to generate a video stream with FFmpeg which violates any standard that must be a bug in FFmpeg and the options which make it possible must be removed?
but don't expect them to change the documented semantics of -Bsymbolic.
Where is that documentation which says that the main app will see a different symbol than the lib with -Bsymbolic. Its certainly not in the documentation of -Bsymbolic, as such these are not the "documented semantics".
It is not in itself directly guaranteed behavior (in the sense that you can't RELY on the symbol address being different in the library and in the main program). However it is often a consequence of the documented behavior. You cannot both change the bindings in the library and keep them the same as in the main program. Binutils has no way around the "can't keep them the same if you change them" problem even if it assumes that there are no alternative definitions in other objects. If the main program has copy relocations of data variables then it is not possible for the library code to guarantee both using the same address as the main program (which implies using the copy relocation) and using the library's own definition of the variable (which implies NOT using the copy relocation).
If you want another option that does something different that's a separate feature request, not a bugfix.
I wonder why you cannot just accept that you were wrong ...
I'm the only poster in this thread who has consistently understood the issues. OTOH you earlier posted things like "Mans added -Bsymbolic, everyone wonders why this isnt default and what the ELF designers were smoking" in your blog. Now you cannot admit that you had no idea what you were talking about, and are trying to find other people to blame. If someone posted similar complaints about FFmpeg, for example "We tried giving various nonstandard options to FFmpeg and the resulting file doesn't play in my DVD player any more! That's a horrible bug!" they'd surely be flamed. Even if FFmpeg documentation was less than perfect. Your complaints about -Bsymbolic aren't really any more valid than that.
On Wed, Mar 12, 2008 at 12:13:08AM +0200, Uoti Urpala wrote: [...]
The C standard (as well as common sense) requires all references to the symbol to be equal. This is not optional or dependant on command line options. Its a strict requirement of C. The bug is that this requirement is broken when the lib is compiled with PIC and -Bsymbolic and the main app is not compiled with PIC.
So if someone manages to generate a video stream with FFmpeg which violates any standard that must be a bug in FFmpeg and the options which make it possible must be removed?
If the user asks ffmpeg to lets say mux msmpeg4v3 in mpeg-ps then thats what she asked for and what she gets standard conformant or not. OTOH if she asked ffmpeg to generate intra only video and store it in mpeg-ps and she then got huffyuv in mpeg-ps while otherwise she would have gotten mpeg2 in mpeg-ps then yes this would be a bug. Intra only does not implicate breaking the standard. Similarly -Bsymbolic does not implicate that the application would not see the same symbols. Its an effect of the implementation not anything required by binding symbols in the lib to the ones in the lib.
but don't expect them to change the documented semantics of -Bsymbolic.
Where is that documentation which says that the main app will see a different symbol than the lib with -Bsymbolic. Its certainly not in the documentation of -Bsymbolic, as such these are not the "documented semantics".
It is not in itself directly guaranteed behavior (in the sense that you can't RELY on the symbol address being different in the library and in the main program). However it is often a consequence of the documented behavior. You cannot both change the bindings in the library and keep them the same as in the main program.
Binutils has no way around the "can't keep them the same if you change them" problem even if it assumes that there are no alternative definitions in other objects.
This does not affect that it is required by C. And as such is a bug whereever one now pushes it, be it binutils, the docs or the whole design of the ELF GNU toolchain.
If the main program has copy relocations of data variables then it is not possible for the library code to guarantee both using the same address as the main program (which implies using the copy relocation) and using the library's own definition of the variable (which implies NOT using the copy relocation).
Your reasoning is based on keeping everything constant except the generated library. Get rid of copy relocations and its solved.
If you want another option that does something different that's a separate feature request, not a bugfix.
I wonder why you cannot just accept that you were wrong ...
I'm the only poster in this thread who has consistently understood the issues.
Or maybe you are the only one who didnt ...
OTOH you earlier posted things like "Mans added -Bsymbolic, everyone wonders why this isnt default and what the ELF designers were smoking" in your blog. Now you cannot admit that you had no idea what you were talking about, and are trying to find other people to blame.
We all wondered why its not default, now we know, its buggy.
If someone posted similar complaints about FFmpeg, for example "We tried giving various nonstandard options to FFmpeg and the resulting file doesn't play in my DVD player any more! That's a horrible bug!" they'd surely be flamed. Even if FFmpeg documentation was less than perfect. Your complaints about -Bsymbolic aren't really any more valid than that.
As said the fine difference is that explicitly asking to break the standard and asking to do something unrelated to standard breakage which does _NOT_ require breaking the standard break it are 2 very different things. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The greatest way to live with honor in this world is to be what we pretend to be. -- Socrates
On Tue, 2008-03-11 at 23:54 +0100, Michael Niedermayer wrote:
On Wed, Mar 12, 2008 at 12:13:08AM +0200, Uoti Urpala wrote:
Binutils has no way around the "can't keep them the same if you change them" problem even if it assumes that there are no alternative definitions in other objects.
This does not affect that it is required by C. And as such is a bug whereever one now pushes it, be it binutils, the docs or the whole design of the ELF GNU toolchain.
The C standard does not require that tools have no options to generate nonstandard behavior. The only "fix" that can be done in practice is warn more clearly about the side effects in the documentation.
If the main program has copy relocations of data variables then it is not possible for the library code to guarantee both using the same address as the main program (which implies using the copy relocation) and using the library's own definition of the variable (which implies NOT using the copy relocation).
Your reasoning is based on keeping everything constant except the generated library. Get rid of copy relocations and its solved.
Sure, if you redesign the whole x86 ABI then it's possible to have both. That's not a very realistic solution for FFmpeg's problems though. Actually there seems to be some movement toward PIE (position independent executable, compiling everything including the main program with -fPIC). Such programs won't use copy relocations. I'm not sure you'll be otherwise happy about that direction though...
If someone posted similar complaints about FFmpeg, for example "We tried giving various nonstandard options to FFmpeg and the resulting file doesn't play in my DVD player any more! That's a horrible bug!" they'd surely be flamed. Even if FFmpeg documentation was less than perfect. Your complaints about -Bsymbolic aren't really any more valid than that.
As said the fine difference is that explicitly asking to break the standard and asking to do something unrelated to standard breakage which does _NOT_ require breaking the standard break it are 2 very different things.
And just how would you implement -Bsymbolic so that it binds symbols to the versions in the library but never makes the addresses differ from those seen elsewhere? "Rewrite the ABI for the sake of this option" is not a realistic answer IMO.
On Wed, Mar 12, 2008 at 01:42:39AM +0200, Uoti Urpala wrote:
If someone posted similar complaints about FFmpeg, for example "We tried giving various nonstandard options to FFmpeg and the resulting file doesn't play in my DVD player any more! That's a horrible bug!" they'd surely be flamed. Even if FFmpeg documentation was less than perfect. Your complaints about -Bsymbolic aren't really any more valid than that.
As said the fine difference is that explicitly asking to break the standard and asking to do something unrelated to standard breakage which does _NOT_ require breaking the standard break it are 2 very different things.
And just how would you implement -Bsymbolic so that it binds symbols to the versions in the library but never makes the addresses differ from those seen elsewhere? "Rewrite the ABI for the sake of this option" is not a realistic answer IMO.
There is no need for ABI changes, just generating the correct relocations. This will necessarily involve textrels in either the library of the executable, which I have no objection to but which the GNU toolchain maintainers (not to mention the gentoo kiddies) despise. Rich
On Wed, Mar 12, 2008 at 01:42:39AM +0200, Uoti Urpala wrote: [...]
If the main program has copy relocations of data variables then it is not possible for the library code to guarantee both using the same address as the main program (which implies using the copy relocation) and using the library's own definition of the variable (which implies NOT using the copy relocation).
Your reasoning is based on keeping everything constant except the generated library. Get rid of copy relocations and its solved.
Sure, if you redesign the whole x86 ABI then it's possible to have both. That's not a very realistic solution for FFmpeg's problems though.
I agree with you that its not realistic that the gnu toolchain maintainers will correct this, my argument was that it can be done in priciple and that the final implementation would be superior in many other ways besides this bugfix.
Actually there seems to be some movement toward PIE (position independent executable, compiling everything including the main program with -fPIC). Such programs won't use copy relocations. I'm not sure you'll be otherwise happy about that direction though...
My oppinion about PIE is somewhat mixed. Its certainly a stupid idea for performance relevant code. Though it might have some small security benefit for security relevant code, though ironically ive just a few days ago read a paper describing how to exploit a buffer overflow in apache reliably on a system with grsec non executable stack, heap, and all addresses randomized (and re-randomized after a crash). IIRC all code including the executable was also position independent. PIC and PIE have something in common with the "war on terror" some western countries are playing these days ... lots of advantages in theory and lots of disadavtages in practice. (and thats putting it mildly) [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB If you really think that XML is the answer, then you definitly missunderstood the question -- Attila Kinali
On Sun, Mar 09, 2008 at 11:00:54PM +0100, Michael Niedermayer wrote:
On Sun, Mar 09, 2008 at 07:50:16PM +0100, Reimar D?ffinger wrote:
On Sun, Mar 09, 2008 at 12:05:33AM +0000, M?ns Rullg?rd wrote:
Baptiste discovered a rather nasty memory leak when using a bitstream filter with shared libraries.
In ffmpeg.c:write_frame() the output of bitstream filters is stored in an AVPacket, and its destruct function is set like this (line 417):
new_pkt.destruct= av_destruct_packet;
Later on, in av_interleave_packet_per_dts(), AVPacket.destruct is compared against av_destruct_packet, like this (utils.c:2439):
if(pkt->destruct == av_destruct_packet)
This seems like a misuse of pkt->destruct to me anyway, and for a simple optimization in addition. I am still not entirely certain I understood what this does, but I think that a pkt->flag PKT_FREE_ON_DESTRUCT or some such would do.
ugh .... No it would not do. It completely defeats the sense behind destruct() that is to allow a destruct different from what av_free() does.
Huh? Of course the destruct pointer should stay, this flag would just indicate if the destruct function does some kind of free in the data or not, so that we know which way we need to use to copy etc. the AVPacket. Greetings, Reimar D?ffinger
On Mon, Mar 10, 2008 at 09:53:37AM +0100, Reimar D?ffinger wrote:
On Sun, Mar 09, 2008 at 11:00:54PM +0100, Michael Niedermayer wrote:
On Sun, Mar 09, 2008 at 07:50:16PM +0100, Reimar D?ffinger wrote:
On Sun, Mar 09, 2008 at 12:05:33AM +0000, M?ns Rullg?rd wrote:
Baptiste discovered a rather nasty memory leak when using a bitstream filter with shared libraries.
In ffmpeg.c:write_frame() the output of bitstream filters is stored in an AVPacket, and its destruct function is set like this (line 417):
new_pkt.destruct= av_destruct_packet;
Later on, in av_interleave_packet_per_dts(), AVPacket.destruct is compared against av_destruct_packet, like this (utils.c:2439):
if(pkt->destruct == av_destruct_packet)
This seems like a misuse of pkt->destruct to me anyway, and for a simple optimization in addition. I am still not entirely certain I understood what this does, but I think that a pkt->flag PKT_FREE_ON_DESTRUCT or some such would do.
ugh .... No it would not do. It completely defeats the sense behind destruct() that is to allow a destruct different from what av_free() does.
Huh? Of course the destruct pointer should stay, this flag would just indicate if the destruct function does some kind of free in the data or not, so that we know which way we need to use to copy etc. the AVPacket.
Whats the point of having a flag that says this destruct() does nothing, destuct=NULL is much simpler. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The educated differ from the uneducated as much as the living from the dead. -- Aristotle
On Mon, Mar 10, 2008 at 12:55:56PM +0100, Michael Niedermayer wrote:
On Mon, Mar 10, 2008 at 09:53:37AM +0100, Reimar D?ffinger wrote:
Huh? Of course the destruct pointer should stay, this flag would just indicate if the destruct function does some kind of free in the data or not, so that we know which way we need to use to copy etc. the AVPacket.
Whats the point of having a flag that says this destruct() does nothing, destuct=NULL is much simpler.
Well, I had assumed that the current complicated way was for a reason, not just a brain-fart... Well, probably it was considered simpler like this originally.
Michael Niedermayer wrote:
ugh .... No it would not do. It completely defeats the sense behind destruct() that is to allow a destruct different from what av_free() does. One solution i could see is to: static inline void av_free_packet(AVPacket *pkt) { - if (pkt && pkt->destruct) { - pkt->destruct(pkt); + if(pkt){ + if(pkt->destruct) + pkt->destruct(pkt); + pkt->data = NULL; pkt->size = 0; } } ---- void av_destruct_packet(AVPacket *pkt) { av_free(pkt->data); - pkt->data = NULL; pkt->size = 0; } ---- -void av_destruct_packet_nofree(AVPacket *pkt) -{ - pkt->data = NULL; pkt->size = 0; -}
and use NULL instead of av_destruct_packet_nofree
Anyway ive no interrest to workaround binutil bugs, if anyone cares he has to do the work and testing.
given that it says that you have to use -Bsymbolic with shared objects and that implies PIC (no, x86 is broken for not using PIC and that horse has been already beaten)... Still the change you proposed should cleanup ffmpeg from an API ugliness. lu -- Luca Barbato Gentoo Council Member Gentoo/linux Gentoo/PPC http://dev.gentoo.org/~lu_zero
Hi guys, M?ns Rullg?rd wrote:
Baptiste discovered a rather nasty memory leak when using a bitstream filter with shared libraries.
In ffmpeg.c:write_frame() the output of bitstream filters is stored in an AVPacket, and its destruct function is set like this (line 417):
new_pkt.destruct= av_destruct_packet;
Later on, in av_interleave_packet_per_dts(), AVPacket.destruct is compared against av_destruct_packet, like this (utils.c:2439):
if(pkt->destruct == av_destruct_packet)
The trouble here is that with shared libraries, the address of av_destruct_packet when used in the main executable or libs other than the one defining it resolves to a trampoline that calls the real function using a GOT entry. As a result, this comparison is always false when the destruct pointer was assigned outside libavformat.
Does anyone have an idea for fixing this?
Sorry, but could we try to find an acceptable solution for this issue ? Bistream filters are basically unuseable with shared libs... This is critical IMHO. -- Baptiste COUDURIER GnuPG Key Id: 0x5C1ABAAA SMARTJOG SAS http://www.smartjog.com Key fingerprint 8D77134D20CC9220201FC5DB0AC9325C5C1ABAAA Phone: +33 1 49966312
On Wed, Apr 02, 2008 at 10:50:20AM +0200, Baptiste Coudurier wrote:
Hi guys,
M?ns Rullg?rd wrote:
Baptiste discovered a rather nasty memory leak when using a bitstream filter with shared libraries.
In ffmpeg.c:write_frame() the output of bitstream filters is stored in an AVPacket, and its destruct function is set like this (line 417):
new_pkt.destruct= av_destruct_packet;
Later on, in av_interleave_packet_per_dts(), AVPacket.destruct is compared against av_destruct_packet, like this (utils.c:2439):
if(pkt->destruct == av_destruct_packet)
The trouble here is that with shared libraries, the address of av_destruct_packet when used in the main executable or libs other than the one defining it resolves to a trampoline that calls the real function using a GOT entry. As a result, this comparison is always false when the destruct pointer was assigned outside libavformat.
Does anyone have an idea for fixing this?
Sorry, but could we try to find an acceptable solution for this issue ?
as said elsewhere in this thread: static inline void av_free_packet(AVPacket *pkt) { - if (pkt && pkt->destruct) { - pkt->destruct(pkt); + if(pkt){ + if(pkt->destruct) + pkt->destruct(pkt); + pkt->data = NULL; pkt->size = 0; } } ---- void av_destruct_packet(AVPacket *pkt) { av_free(pkt->data); - pkt->data = NULL; pkt->size = 0; } ---- -void av_destruct_packet_nofree(AVPacket *pkt) -{ - pkt->data = NULL; pkt->size = 0; -} and use NULL instead of av_destruct_packet_nofree ----------------- ... I dont use libav shared libs ... full patch welcome. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB When you are offended at any man's fault, turn to yourself and study your own failings. Then you will forget your anger. -- Epictetus
On Wed, Apr 02, 2008 at 01:37:58PM +0200, Michael Niedermayer wrote:
On Wed, Apr 02, 2008 at 10:50:20AM +0200, Baptiste Coudurier wrote:
Hi guys,
M?ns Rullg?rd wrote:
Baptiste discovered a rather nasty memory leak when using a bitstream filter with shared libraries.
In ffmpeg.c:write_frame() the output of bitstream filters is stored in an AVPacket, and its destruct function is set like this (line 417):
new_pkt.destruct= av_destruct_packet;
Later on, in av_interleave_packet_per_dts(), AVPacket.destruct is compared against av_destruct_packet, like this (utils.c:2439):
if(pkt->destruct == av_destruct_packet)
The trouble here is that with shared libraries, the address of av_destruct_packet when used in the main executable or libs other than the one defining it resolves to a trampoline that calls the real function using a GOT entry. As a result, this comparison is always false when the destruct pointer was assigned outside libavformat.
Does anyone have an idea for fixing this?
Sorry, but could we try to find an acceptable solution for this issue ?
as said elsewhere in this thread: static inline void av_free_packet(AVPacket *pkt) { - if (pkt && pkt->destruct) { - pkt->destruct(pkt); + if(pkt){ + if(pkt->destruct) + pkt->destruct(pkt); + pkt->data = NULL; pkt->size = 0; } } ---- void av_destruct_packet(AVPacket *pkt) { av_free(pkt->data); - pkt->data = NULL; pkt->size = 0; } ---- -void av_destruct_packet_nofree(AVPacket *pkt) -{ - pkt->data = NULL; pkt->size = 0; -}
and use NULL instead of av_destruct_packet_nofree -----------------
The reason I do not like this much is because it means you can't e.g. set destruct to something that does only logging, the destruct function implicitly defines how copies are made. Anyway, while trying to implement this I came about something I don't really understand, in ff_interleave_add_packet:
if(pkt->destruct == av_destruct_packet) pkt->destruct= NULL; // not shared -> must keep original from being freed else av_dup_packet(&this_pktl->pkt); //shared -> must dup
is this just a complete brainfart, or is that comparison against a static function declared in a header on purpose? And are the semantics really supposed to be that when when a user-defined destruct it used it will be called multiple times on the same pkt (more or less)? And are there any reasons why it is assumed that the original AVPacket passed into ff_interleave_add_packet will not be used longer than the copy? Does all this stuff work for any other reason than pure luck (SCNR ;-) ). Greetings, Reimar D?ffinger
On Thu, 2009-02-19 at 19:51 +0100, Reimar D?ffinger wrote:
if(pkt->destruct == av_destruct_packet)
is this just a complete brainfart, or is that comparison against a static function declared in a header on purpose?
I think you misread something, av_destruct_packet() is not static. There are problems with that comparison, but they are caused by the use of the -Bsymbolic option in a way that forces the same function symbol to have a different address inside and outside the library.
On Thu, Feb 19, 2009 at 09:22:08PM +0200, Uoti Urpala wrote:
On Thu, 2009-02-19 at 19:51 +0100, Reimar D?ffinger wrote:
if(pkt->destruct == av_destruct_packet)
is this just a complete brainfart, or is that comparison against a static function declared in a header on purpose?
I think you misread something, av_destruct_packet() is not static.
Yes, I confused it with free_packet, which I was just looking at. I guess I need some sleep and more time.
On Thu, Feb 19, 2009 at 07:51:38PM +0100, Reimar D?ffinger wrote:
On Wed, Apr 02, 2008 at 01:37:58PM +0200, Michael Niedermayer wrote:
On Wed, Apr 02, 2008 at 10:50:20AM +0200, Baptiste Coudurier wrote:
Hi guys,
M?ns Rullg?rd wrote:
Baptiste discovered a rather nasty memory leak when using a bitstream filter with shared libraries.
In ffmpeg.c:write_frame() the output of bitstream filters is stored in an AVPacket, and its destruct function is set like this (line 417):
new_pkt.destruct= av_destruct_packet;
Later on, in av_interleave_packet_per_dts(), AVPacket.destruct is compared against av_destruct_packet, like this (utils.c:2439):
if(pkt->destruct == av_destruct_packet)
The trouble here is that with shared libraries, the address of av_destruct_packet when used in the main executable or libs other than the one defining it resolves to a trampoline that calls the real function using a GOT entry. As a result, this comparison is always false when the destruct pointer was assigned outside libavformat.
Does anyone have an idea for fixing this?
Sorry, but could we try to find an acceptable solution for this issue ?
as said elsewhere in this thread: static inline void av_free_packet(AVPacket *pkt) { - if (pkt && pkt->destruct) { - pkt->destruct(pkt); + if(pkt){ + if(pkt->destruct) + pkt->destruct(pkt); + pkt->data = NULL; pkt->size = 0; } } ---- void av_destruct_packet(AVPacket *pkt) { av_free(pkt->data); - pkt->data = NULL; pkt->size = 0; } ---- -void av_destruct_packet_nofree(AVPacket *pkt) -{ - pkt->data = NULL; pkt->size = 0; -}
and use NULL instead of av_destruct_packet_nofree -----------------
The reason I do not like this much is because it means you can't e.g. set destruct to something that does only logging, the destruct function implicitly defines how copies are made.
noone wants to do that, and if someone wants she can put the loging code in utils.c and recompile
Anyway, while trying to implement this I came about something I don't really understand, in ff_interleave_add_packet:
if(pkt->destruct == av_destruct_packet) pkt->destruct= NULL; // not shared -> must keep original from being freed else av_dup_packet(&this_pktl->pkt); //shared -> must dup
is this just a complete brainfart, or is that comparison against a
the check possibly should be changed to != NULL && != no_free
static function declared in a header on purpose? And are the semantics really supposed to be that when when a user-defined destruct it used it will be called multiple times on the same pkt (more or less)?
And are there any reasons why it is assumed that the original AVPacket passed into ff_interleave_add_packet will not be used longer than the copy?
it can be used longer depending in pkt->destruct behavior if you have a realistic example where the current system is insufficient then a patch that implements something better is welcome. Just keep in mind that packets can be created and freed in different threads so dont forget mutexes and all that ... its just replacing destruct by your reference counting locking thread safe free ... [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Good people do not need laws to tell them to act responsibly, while bad people will find a way around the laws. -- Plato
participants (7)
-
baptiste.coudurier@smartjog.com -
dalias@aerifal.cx -
lu_zero@gentoo.org -
mans@mansr.com -
michaelni@gmx.at -
Reimar.Doeffinger@stud.uni-karlsruhe.de -
uoti.urpala@pp1.inet.fi