[ros-diffs] [sir_richard] 48475: [KERNEL32]: While working on the CMAKE branch, Amine and myself discovered a rather serious issue in kernel32 (and perhaps other libraries as well). Unlike rbuild, CMake does not allow you to export non-existant DLL functions (try it: add "poopyhead" in kernel32's exports under RBuild, and will it export "poopyhead", God knowing what that will actually link to). As an additional feature on top of the "allow non-existing functions to be exported" "feature", because rbuild generates and links STDCALL function names without the proper decoration (vs. enforcing decoration at linking, but only removing it at export-time), this allows the definition (as an export) of a STDCALL function that is completely different from the actual function itself. For example, the 5-parameter Foo function is normally Foo at 20, while the 3-parameter Foo function woudl be Foo at 12. Linking one against the other would fail (say, 2 parameters were added to Foo in a newer version). However, under RBUILD, both of these would appear as "Foo", and the linker/compiler would happilly connect the caller of Foo at 3 (that has pushed 3 parameters) to the receiving side of Foo at 5 (that is about to pop 5 parameters). Even -if- decorations WERE to be applied, Foo at 12 would STILL succeed, because of the first feature, which would enable the export of Foo at 12 even though no such function exist. In a further, bizare, twist of fate, the behavior of this RBUILD "feature", when the target function is not found, is to link the exported DLL TO ITSELF. Therefore, one can see how, previously to this patch, kernel32.dll would import a dozen functions from itself (all the non-existing functions). To really seal the deal, the behavior of exported functions used by kernel32, but that are actually forwarded to another DLL deserves a special mention. GetLastError, for example, merely forwards to RtlGetLastWin32Error, so it is normal behavior to use a #define in the C code so that all internal calls to the function are routed correctly. This did not happen, so instead, kernel32 tried importing/linking/exporting GetLastError, but this symbol is not found in the binary, because it is only a forwarder. This caused kernel32 to import from itself (the behavior when an exported symbol is not found). When importing from itself, the loader would now find the _forwarded_ for GetLastError, and correctly link with ntdll. What should be a one-liner of assembly (inline TEB access) thus became a triple-call indirection (GetLastError at 0->StubLastError at 0->__impGetLastError at 0->__impRtlGetLastWin32Error->RtlGetLastWin32Error. While analyzing these issues, we also realized a strange macro SetLastErrorByStatus that manually tried to perform what there already exists a function for: RtlSetLastNtStatusFromWin32Error. And, in an exciting coda, we also realized that our Server 2003 Kernel32 exports more than a dozen Windows 95 APIs, through an auto-stub generation mechanism within winebuild, that gets linked as an object behind the scenes. [KERNEL32]: Removed all Win95 exports, cleaned up exports. [KERNEL32]: Fixed up set/get error macros by making them inline and/or calling the correct ntdll function. [KERNEL32]: Removed bizare calls to Wine-internal/specific APIs from our core Win32 DLL. [KERNEL32]: Wrote stubs for all functions which should be exported, and set the correct number of parameters for them. [KERNEL32]: Kernel32 is smaller, loads faster, does not export Windows 95 functions, does not export non-existing functions, and does not import from itself anymore. Note: This is one of the many failings of RBUILD the CMAKE system has helped us discover. I believe these issues are serious enough to warrant an immediate sync with trunk, but rest assured, there are many more completely broken, infinitely-regressing things that we discovered while switching to CMAKE.

sir_richard at svn.reactos.org sir_richard at svn.reactos.org
Sat Aug 7 05:02:59 UTC 2010


Author: sir_richard
Date: Sat Aug  7 05:02:58 2010
New Revision: 48475

URL: http://svn.reactos.org/svn/reactos?rev=48475&view=rev
Log:
[KERNEL32]: While working on the CMAKE branch, Amine and myself discovered a rather serious issue in kernel32 (and perhaps other libraries as well). Unlike rbuild, CMake does not allow you to export non-existant DLL functions (try it: add "poopyhead" in kernel32's exports under RBuild, and will it export "poopyhead", God knowing what that will actually link to).
            As an additional feature on top of the "allow non-existing functions to be exported" "feature", because rbuild generates and links STDCALL function names without the proper decoration (vs. enforcing decoration at linking, but only removing it at export-time), this allows the definition (as an export) of a STDCALL function that is completely different from the actual function itself.
            For example, the 5-parameter Foo function is normally Foo at 20, while the 3-parameter Foo function woudl be Foo at 12. Linking one against the other would fail (say, 2 parameters were added to Foo in a newer version). However, under RBUILD, both of these would appear as "Foo", and the linker/compiler would happilly connect the caller of Foo at 3 (that has pushed 3 parameters) to the receiving side of Foo at 5 (that is about to pop 5 parameters).
            Even -if- decorations WERE to be applied, Foo at 12 would STILL succeed, because of the first feature, which would enable the export of Foo at 12 even though no such function exist.
            In a further, bizare, twist of fate, the behavior of this RBUILD "feature", when the target function is not found, is to link the exported DLL TO ITSELF.
            Therefore, one can see how, previously to this patch, kernel32.dll would import a dozen functions from itself (all the non-existing functions).
            To really seal the deal, the behavior of exported functions used by kernel32, but that are actually forwarded to another DLL deserves a special mention.
            GetLastError, for example, merely forwards to RtlGetLastWin32Error, so it is normal behavior to use a #define in the C code so that all internal calls to the function are routed correctly.
            This did not happen, so instead, kernel32 tried importing/linking/exporting GetLastError, but this symbol is not found in the binary, because it is only a forwarder.
            This caused kernel32 to import from itself (the behavior when an exported symbol is not found). When importing from itself, the loader would now find the _forwarded_ for GetLastError, and correctly link with ntdll.
            What should be a one-liner of assembly (inline TEB access) thus became a triple-call indirection (GetLastError at 0->StubLastError at 0->__impGetLastError at 0->__impRtlGetLastWin32Error->RtlGetLastWin32Error.
            While analyzing these issues, we also realized a strange macro SetLastErrorByStatus that manually tried to perform what there already exists a function for: RtlSetLastNtStatusFromWin32Error.
	    And, in an exciting coda, we also realized that our Server 2003 Kernel32 exports more than a dozen Windows 95 APIs, through an auto-stub generation mechanism within winebuild, that gets linked as an object behind the scenes.
[KERNEL32]: Removed all Win95 exports, cleaned up exports.
[KERNEL32]: Fixed up set/get error macros by making them inline and/or calling the correct ntdll function.
[KERNEL32]: Removed bizare calls to Wine-internal/specific APIs from our core Win32 DLL.
[KERNEL32]: Wrote stubs for all functions which should be exported, and set the correct number of parameters for them.
[KERNEL32]: Kernel32 is smaller, loads faster, does not export Windows 95 functions, does not export non-existing functions, and does not import from itself anymore.
Note: This is one of the many failings of RBUILD the CMAKE system has helped us discover. I believe these issues are serious enough to warrant an immediate sync with trunk, but rest assured, there are many more completely broken, infinitely-regressing things that we discovered while switching to CMAKE.


Added:
    trunk/reactos/dll/win32/kernel32/kernel32.def
      - copied, changed from r48474, trunk/reactos/dll/win32/kernel32/kernel32.pspec
Removed:
    trunk/reactos/dll/win32/kernel32/kernel32.pspec
Modified:
    trunk/reactos/dll/win32/kernel32/file/backup.c
    trunk/reactos/dll/win32/kernel32/file/bintype.c
    trunk/reactos/dll/win32/kernel32/file/cnotify.c
    trunk/reactos/dll/win32/kernel32/file/copy.c
    trunk/reactos/dll/win32/kernel32/file/create.c
    trunk/reactos/dll/win32/kernel32/file/curdir.c
    trunk/reactos/dll/win32/kernel32/file/delete.c
    trunk/reactos/dll/win32/kernel32/file/dir.c
    trunk/reactos/dll/win32/kernel32/file/dosdev.c
    trunk/reactos/dll/win32/kernel32/file/file.c
    trunk/reactos/dll/win32/kernel32/file/find.c
    trunk/reactos/dll/win32/kernel32/file/hardlink.c
    trunk/reactos/dll/win32/kernel32/file/iocompl.c
    trunk/reactos/dll/win32/kernel32/file/lfile.c
    trunk/reactos/dll/win32/kernel32/file/lock.c
    trunk/reactos/dll/win32/kernel32/file/mailslot.c
    trunk/reactos/dll/win32/kernel32/file/move.c
    trunk/reactos/dll/win32/kernel32/file/npipe.c
    trunk/reactos/dll/win32/kernel32/file/pipe.c
    trunk/reactos/dll/win32/kernel32/file/rw.c
    trunk/reactos/dll/win32/kernel32/file/tape.c
    trunk/reactos/dll/win32/kernel32/file/volume.c
    trunk/reactos/dll/win32/kernel32/include/kernel32.h
    trunk/reactos/dll/win32/kernel32/kernel32.rbuild
    trunk/reactos/dll/win32/kernel32/misc/actctx.c
    trunk/reactos/dll/win32/kernel32/misc/console.c
    trunk/reactos/dll/win32/kernel32/misc/format_msg.c
    trunk/reactos/dll/win32/kernel32/misc/lcformat.c
    trunk/reactos/dll/win32/kernel32/misc/stubs.c
    trunk/reactos/dll/win32/kernel32/misc/version.c
    trunk/reactos/dll/win32/kernel32/process/session.c

[This mail would be too long, it was shortened to contain the URLs only.]

Modified: trunk/reactos/dll/win32/kernel32/file/backup.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/backup.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/bintype.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/bintype.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/cnotify.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/cnotify.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/copy.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/copy.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/create.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/create.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/curdir.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/curdir.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/delete.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/delete.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/dir.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/dir.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/dosdev.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/dosdev.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/file.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/file.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/find.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/find.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/hardlink.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/hardlink.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/iocompl.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/iocompl.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/lfile.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/lfile.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/lock.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/lock.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/mailslot.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/mailslot.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/move.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/move.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/npipe.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/npipe.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/pipe.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/pipe.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/rw.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/rw.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/tape.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/tape.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/file/volume.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/volume.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/include/kernel32.h
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/include/kernel32.h?rev=48475&r1=48474&r2=48475&view=diff

Copied: trunk/reactos/dll/win32/kernel32/kernel32.def (from r48474, trunk/reactos/dll/win32/kernel32/kernel32.pspec)
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/kernel32.def?p2=trunk/reactos/dll/win32/kernel32/kernel32.def&p1=trunk/reactos/dll/win32/kernel32/kernel32.pspec&r1=48474&r2=48475&rev=48475&view=diff

Removed: trunk/reactos/dll/win32/kernel32/kernel32.pspec
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/kernel32.pspec?rev=48474&view=auto

Modified: trunk/reactos/dll/win32/kernel32/kernel32.rbuild
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/kernel32.rbuild?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/misc/actctx.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/misc/actctx.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/misc/console.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/misc/console.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/misc/format_msg.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/misc/format_msg.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/misc/lcformat.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/misc/lcformat.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/misc/stubs.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/misc/stubs.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/misc/version.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/misc/version.c?rev=48475&r1=48474&r2=48475&view=diff

Modified: trunk/reactos/dll/win32/kernel32/process/session.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/process/session.c?rev=48475&r1=48474&r2=48475&view=diff




More information about the Ros-diffs mailing list