Skip to content

Q: runtime pal.h declarations #37310

Closed
Closed

Description

@tmds

When updating versions of clang we had some compiler errors due to mismatch in declarations between pal.h and system headers (mainly: missing throw specifiers). We can fix these issues by making pal.h match with the system headers.

What is the rationale for having these declaration in pal.h, instead of including system headers?

cc @jkotas @janvorli @omajid

Activity

  1. added
    area-PAL-coreclr
    untriaged
    on Jun 2, 2020
  2. omajid commented on Jun 2, 2020

    @omajid
    Member

    For example, this is the sort of error we ran into with clang 10: #32837

  3. jkotas commented on Jun 2, 2020

    @jkotas
    Member

    The PAL is trying to emulate Windows for historic reasons. It made the initial port to Unix easy. Definitions of number of functions on Windows are different from equivalent definitions on Unix. It means that we cannot just include system headers because of the definitions would collide.

    Ideally, we would make this Windows emulator go away. A step in this direction would be to define PAL_STDCPP_COMPAT always and make sure that everything still builds and works. It would enable us to include system headers instead of duplicating selected parts of them.

  4. janvorli commented on Jun 2, 2020

    @janvorli
    Member

    The runtime code on Unix never includes anything from the actual platform, just the pal.h that provides all the functions the runtime depends on. That includes emulated Windows APIs, Windows data types, some extra PAL APIs that don't match windows ones (names starting with PAL_) and some standard C functions like various printf / scanf etc.

    The code in PAL implementation also includes the pal.h since it needs to use the Windows data types or define the functions declared in the pal.h. However, the pal.h is not included directly, but rather through palinternal.h that besides other things also handles conflicting function and type names between the Windows and Unix worlds. There are details on that in comments at the beginning of the palinternal.h.

    The throw specifier case is kind of special as it needs to be on the function declarations in the pal.h and since these are included from both PAL and runtime, they need to match what the platform uses.

  5. tmds commented on Jun 4, 2020

    @tmds
    MemberAuthor

    A step in this direction would be to define PAL_STDCPP_COMPAT always and make sure that everything still builds and works.

    It would be interesting to give this a try to avoid these compiler issues in the future.

  6. tmds commented on Jun 9, 2020

    @tmds
    MemberAuthor

    I experimented with this a bit, and came to a better understanding of what type things PAL deals with. 2 examples:

    • wchar_t is 2-byte utf-16 on Windows, and 4-byte 'unspecified encoding' on Linux. Since strings in managed .NET are UTF-16 at some point you need native functions that work with UTF-16, and those will be distinct, on non-Windows, from system functions that accept wchar_t.

    • There are some Windows-specific sal annotations on buffers using __in and __out:

      #define __in _SAL1_Source_(__in, (), _In_)
      #define __out _SAL1_Source_(__out, (), _Out_)

      Some Linux headers use __in and __out as identifiers, so they can't be defined to disappear.

    All such cases can be death with, though the effort is higher than what I had imagined.

  7. removed
    untriaged
    on Jun 22, 2020
  8. added this to the 6.0.0 milestone on Jun 22, 2020
  9. modified the milestones: 6.0.0, 7.0.0 on Jul 19, 2021
  10. yowl commented on Dec 30, 2021

    @yowl
    Contributor

    I'm confused with the wchar_t problem on Linux. If you have PAL_STDCPP_COMPAT then you dont get the, for example, PAL_fwprintf implementation as its within a #ifndef PAL_STDCPP_COMPAT in pal.h What's the solution here, always substitute fwprintf and other wide char functions on Linux?

  11. jkotas commented on Dec 31, 2021

    @jkotas
    Member

    #define fwprintf PAL_fwprintf is within #ifndef PAL_STDCPP_COMPAT. PAL_fwprintf is outside #ifndef PAL_STDCPP_COMPAT. You should be able to replace fwprintf with PAL_fwprintf to make things compile.

  12. yowl commented on Dec 31, 2021

    @yowl
    Contributor

    Thanks,

    Uses size_t but at its include point that's been defined to DUMMY_size_t, the undef comes after, and hence:

    In file included from /home/scott/github/runtimelab/src/coreclr/nativeresources/../pal/src/include/pal/palinternal.h:335:
      /home/scott/github/runtimelab/src/coreclr/pal/inc/mbusafecrt.h:48:40: error: unknown type name 'DUMMY_size_t'
      extern errno_t strcat_s( char* ioDest, size_t inDestBufferSize, const char* inSrc );
                                             ^
      /home/scott/github/runtimelab/src/coreclr/nativeresources/../pal/src/include/pal/palinternal.h:207:16: note: expanded from macro 'size_t'
      #define size_t DUMMY_size_t
                     ^
    

    I tried adding to mbusafecrt.h:

    #ifdef PAL_STDCPP_COMPAT
    #undef size_t
    #include <stddef.h>
    #endif
    

    which clears this one, but moves to the next:

      In file included from /home/scott/github/runtimelab/src/coreclr/nativeresources/resourcestring.cpp:6:
      In file included from /home/scott/github/runtimelab/src/coreclr/inc/utilcode.h:14:
      In file included from /home/scott/github/runtimelab/src/coreclr/inc/winwrap.h:41:
      In file included from /home/scott/github/runtimelab/src/coreclr/nativeresources/../pal/src/include/pal/malloc.hpp:22:
      In file included from /home/scott/github/runtimelab/src/coreclr/nativeresources/../pal/src/include/pal/corunix.hpp:23:
      In file included from /home/scott/github/runtimelab/src/coreclr/nativeresources/../pal/src/include/pal/palinternal.h:335:
      /home/scott/github/runtimelab/src/coreclr/pal/inc/mbusafecrt.h:53:16: error: declaration of 'strcat_s' has a different language linkage
      extern errno_t strcat_s( char* ioDest, size_t inDestBufferSize, const char* inSrc );
                     ^
      /home/scott/github/runtimelab/src/coreclr/pal/inc/rt/safecrt.h:666:17: note: previous definition is here
      errno_t __cdecl strcat_s(char *_Dst, size_t _SizeInBytes, const char *_Src)
                      ^
    

    Comments in mbusafecrt.h indicates its for MacOS but its included unconditionally by palinternal.h. Maybe the right thing is not to include it for Linux ?

  13. jkotas commented on Dec 31, 2021

    @jkotas
    Member

    Comments in mbusafecrt.h indicates its for MacOS

    These comments are stale.

    I think you can try putting the whole block of the #define ... DUMMY_.... defines in palinternal.h under #ifndef PAL_STDCPP_COMPAT.

  14. 12 remaining items

  15. jkotas commented on Jan 3, 2022

    @jkotas
    Member

    Maybe I shouldn't have included that in compiler.h

    Agree.

  16. yowl commented on Jan 3, 2022

    @yowl
    Contributor

    Thanks, do you think all these places where I've included palinternal.h are wrong?:

    Name	Line	Text	Path
    methodcontextreader.cpp	16	#include "pal/palinternal.h"	\\wsl$\Ubuntu-20.04\home\scott\github\runtimelab\src\coreclr\ToolBox\superpmi\superpmi-shared
    commandline.cpp	13	#include "pal/palinternal.h"	\\wsl$\Ubuntu-20.04\home\scott\github\runtimelab\src\coreclr\ToolBox\superpmi\mcs
    utilcode.h	38	#include "pal/palinternal.h"	\\wsl$\Ubuntu-20.04\home\scott\github\runtimelab\src\coreclr\inc
    fgdiagnostic.cpp	10	#include "pal/palinternal.h"	\\wsl$\Ubuntu-20.04\home\scott\github\runtimelab\src\coreclr\jit
    utils.cpp	27	#include "pal/palinternal.h" // _strdup	\\wsl$\Ubuntu-20.04\home\scott\github\runtimelab\src\coreclr\jit
    
    
  17. jkotas commented on Jan 3, 2022

    @jkotas
    Member

    I think palinternal.h should stay internal to the PAL. It should not be included anywhere else.

  18. janvorli commented on Jan 3, 2022

    @janvorli
    Member

    Right, palinternal.h is supposed to be used only in the PAL itself.

  19. yowl commented on Jan 3, 2022

    @yowl
    Contributor

    Should _wfopen be returning PAL_FILE* ?

    Yes, PAL _wfopen should be returning PAL_FILE. I think this one should be fixed by changing the local variable type to PAL_FILE.

    Where is the actual definition of PAL_FILE I can just see the partial declaration in pal.h:

    struct _PAL_FILE;
    typedef struct _PAL_FILE PAL_FILE;
    

    And a reference in cruntime.h:

    /*++
    
    struct PAL_FILE.
    Used to mimic the behavior of windows.
    fwrite under windows can set the ferror flag,
    under BSD fwrite doesn't.
    --*/
    struct _FILE
    {
    

    But that's just _FILE, not actually defining the struct PAL_FILE What am i missing here? At dynamic link of the clrjit time it fails that it is undefined

       /home/scott/github/runtimelab/artifacts/bin/coreclr/Linux.x64.Debug/libclrjit_browser_wasm32_x64.so: error: symbol lookup error: undefined symbol: _Z5flogfP9_PAL_FILEPKcz (fatal)
    
  20. jkotas commented on Jan 3, 2022

    @jkotas
    Member

    I can just see the partial declaration in pal.h:

    The partial definition in pal.h that I see is:

    struct _FILE;
    typedef struct _FILE FILE;
    typedef struct _FILE PAL_FILE;
    

    struct _FILE is then defined in the PAL implementation.

  21. yowl commented on Jan 3, 2022

    @yowl
    Contributor

    Thanks, I'll move that outside of the #if

  22. janvorli commented on Jul 12, 2022

    @janvorli
    Member

    It seems that all the questions have been asnwered, closing the issue.

  23. ghost locked as resolved and limited conversation to collaborators on Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-PAL-coreclr

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions