-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C++ crashes when calling any function in nativeaot shared library #110074
Comments
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas |
Tagging subscribers to this area: @dotnet/gc |
If I modify the source code and return directly in the first line of the NUMASupportInitialize function, will it work? |
@CeSun are you running in a docker container? And what is the distro you are using? |
@janvorli Hi, Thanks for your reply, I am using HarmonyOS Next, a new mobile operating system developed by Huawei. This system is similar to Android. And on this system, you can call the native shared library of linux-musl. Currently, this system is in the public beta stage. I have two devices, one with enforcing selinux and the other with disabled selinux. On the device with disabled selinux, the native shared library released by nativeaot works fine, but not on the other. But in the future, the selinux status of the system used by users will be enforcing |
I have also posted a work order in the Huawei Developer Center to seek help from Huawei and am waiting for a response. |
It should work as-if there's no NUMA support, like the non TARGET_LINUX path. |
I also noticed the macro "TARGET_LINUX", but I know nothing about NUMA. |
NUMA refers to Non-Unified Memory Access, for different physical memory controllers connected with different CPU core(s). Accessing memory or cache connected with different memory controller requires going through the slow interconnect bus, like multiple CPU chips. |
@CeSun do you know if the crash happened while calling the syscall or at some later point? |
I was testing with: #include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
int main(void)
{
if (syscall(__NR_get_mempolicy, NULL, NULL, 0, 0, 0) < 0)
printf("syscall failed with errno %d: %s\n", errno, strerror(errno));
else
printf("didn't fail\n");
return 0;
}
|
when calling the syscall |
@am11 |
@CeSun, I couldn't figure out which "class" to use in SELinux profile, so I went with seccomp security model to repro it. To do that, first the host kernel needs to support
With docker mac (whose host doesn't have
We were handling errno 38 but not 1, so this is somewhat of a corner case (host kernel supports |
@CeSun does it crash for you or does it print the "syscall failed with errno ..." message? If it crashes, then I think it is a likely a bug in the syscall implementation. My theory would be that it for some reason may not properly handle the first argument being NULL. |
@am11 Is this what dailybuild is? https://aka.ms/dotnet/9.0/daily/dotnet-runtime-win-x64.exe |
Does the syscall crashes unconditionally, or does it return an error? |
@janvorli Can we work so hard? |
Hi, still crashing |
This seems to have nothing to do with permissions, I tested it under android in c++ arm64 and it crashed at the same time |
I meant change OH_LOG_ERROR to OH_LOG_DEBUG and retry |
Huawei's reply to me is that the phone does not support the "__NR_get_mempolicy" system call If ordinary Linux does not support __NR_get_mempolicy, how should we do defense? |
Then |
Latest news: HarmonyOS uses seccomp to limit the system calls of apps. When the system call is not in the allowed list, the process will be killed. What is the best solution for this problem? Push the system to modify the whitelist or find a way to determine whether the call can be made in runtime? |
Looks like there is more to it. .NET runtime is using the following syscalls:
HarmonyOS is not supporting some of these, and killing the process in response, which is hardly necessary and paranoid level of security measure. The POSIX standard specifies in Section 2.3 (Error Numbers) and in the descriptions of various system calls:
so I am not sure if we want to go out of our way to support this non-complaint system. If there are enough people using dotnet on HarmonyOS, then perhaps it could be considered, no idea. 🤷♀ |
According to Huawei developers, previous systems developed based on Android already use this solution, so I plan to test .NET Android programs (Mono) and NativeAot programs on previous systems. |
I tried it and this system call also crashes on the Android platform. |
HarmonyOS 5.0 (maybe called Next version in some places) is a brand new operating system, and libc is not bionic but musl. So it seems that the situation of HarmonyOS is similar to that of Android, and it needs to be compatible as an independent platform? |
HarmonyOS system versions 1.0~4.0 are operating systems developed based on Android. A set of HarmonyOS system APIs and development frameworks are added on the basis of Android, and they have been iterated and updated for many years. In addition, many people will mention an OpenHarmony project. The OpenHarmony project is an open source project that only implements the HarmonyOS system standard. It has been updated from 1.0 to 5.0, and is compatible with the HarmonyOS source code level, maintaining the same API Level. For me, a brand new mobile operating system is exciting, and after trying it for about a month, I think many interactions are more advanced than Android. Since there is no historical baggage, many obvious lags in the Android system no longer occur. So I want to try to make some contributions to the HarmonyOS ecosystem, for example, I am porting the Avalonia framework to the HarmonyOS platform. As for the number of developers using .NET on HarmonyOS, I can’t give an exact number. In China, more and more companies will port their mobile apps to HarmonyOS. I believe C# is a better choice than TypeScript, which is natively supported by HarmonyOS. |
Porting .NET to a new OS is ususally a non-trivial task and it is not suitable for this issue. #103627 is tracking the HarmonyOS work. This is the first of many errors you have encountered. As for this issue, seccomp profile can be configured in various ways; ranging from whitelisting syscalls to take actions when disallowed/unimplemented syscalls are made. .NET supports standard actions like "system will issue ENOSYS when disallowed/unimplemented syscall is made" and we adjust the code, but not "system will kill the entire process" unless it is specifically built for that seccomp profile (with many #ifdef THAT_PLATFORM). In its most restrictive form, environment running under seccomp model can disallow all syscalls and put a SIGKILL action penalty when attempted. That makes it pretty much unusable for any real-world application. For a general purpose seccomp support, we would need to build a mechanism that collects the profile data ahead of time (during the deployment e.g.) which will contain the list of syscalls, the actions environment has imposed (returning ENOSYS vs. SIGKILL) and the context on which the action would be taken (thread or the whole process). Then we can make calls deterministically. It sounds simpler, but consider the indirect calls (runtime calls a library, that library make a prohibited syscall or calls libc, which makes the syscall). This will require intense amount of testing for each permutation of versatile seccomp options. Such system does not exist in .NET and there are no plans to build one: #92196 (comment). HarmonyOS has a well-known seccomp profile, we can implement that profile as part of the port work properly #103627. Runtime, its libraries and third-party libraries in .NET ecosystem are using many syscalls directly and indirectly, so you can expect many surprises. |
The final solution to this problem is to compile the static library of runtime myself and delete the syscall code of numb. I hope the official has a better solution. Before the official has a better solution, this issue will not be closed to track the latest situation. |
The "better solution" will be just add That will require quite a lot of work in the build system though, (and a new RID will require all unmanaged nuget packages to specifically add support for it, like |
To be precise, the current issue has been resolved. In the branch I modified, I can successfully publish native shared libraries available for (Open) HarmonyOS. The issue of .NET support for HarmonyOS should be discussed in a more appropriate post. |
This is a branch created for adapting HarmonyOS(informal, for experimental use only): https://github.com/CeSun/dotnet-runtime-openharmony |
The system developer said it was caused by selinux permissions. Is there a way to bypass this system call?
runtime/src/coreclr/gc/unix/numasupport.cpp
Line 57 in 35f2b13
syscall Disassembly:201
NUMASupportInitialize() 0x0000005c8b963458
GCToOSInterface::Initialize() 0x0000005c8b962480
::PalInit() 0x0000005c8b96072c
::RhInitialize(bool) 0x0000005c8b91b1f0
InitializeRuntime() 0x0000005c8b914ebc
Thread::EnsureRuntimeInitialized() 0x0000005c8b91d0e8
Thread::ReversePInvokeAttachOrTrapThread(ReversePInvokeFrame*) 0x0000005c8b91d094
libavalonia_Entry_napi_init__RegisterEntryModule napi_init.cs:12
::RegisterAvaloniaNativeModule() napi_init.cpp:16
The text was updated successfully, but these errors were encountered: