Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lang/c_sharp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# .Net Framework (C#) as Dynamic Build

## Run C# Natively on Linux

To run the C# helloworld natively on Linux, run the command below inside the `./lang/c_sharp/helloworld` directory:
```
./bin/Debug/net7.0/helloworld
```
Or you can also simply run:
```
dotnet run
```

## Run C# with Unikraft

To run the C# helloworld on Unikraft, run the `run.sh` script from the [`run-app-elfloader` repository](https://github.com/unikraft/run-app-elfloader):

```console
./run.sh -k /path/to/elfloader_qemu-x86_64 -r ../dynamic-apps/lang/c_sharp/ /helloworld/bin/Debug/net7.0/helloworld
```

## Notes for patching

The origin `.Net` runtime creates debug pipes using `SYS_mknodat` syscall, which is not yet implemented in Unikraft.
Setting env `COMPlus_EnableDiagnostics=0` or `DOTNET_EnableDiagnostics=0` could prevent this.

For convenience, we patched [EXTERNAL_EnableDiagnostics](https://github.com/dotnet/runtime/blob/v7.0.7/src/coreclr/inc/clrconfigvalues.h#L162) to be `0` by default.
And it only affects `/usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.7/libcoreclr.so`.

## Notes for procfs

The `.Net` runtime uses [__pthread_getattr_np](https://github.com/bminor/glibc/blob/glibc-2.37/nptl/pthread_getattr_np.c#L85) in [CPalThread::GetStackBase()](https://github.com/dotnet/runtime/blob/v7.0.7/src/coreclr/pal/src/thread/thread.cpp#L2683) and [CPalThread::GetStackLimit()](https://github.com/dotnet/runtime/blob/v7.0.7/src/coreclr/pal/src/thread/thread.cpp#L2723).
And the `glibc` implementation reads `/proc/self/maps` to get stack addresses, see the condition [here](https://github.com/bminor/glibc/blob/glibc-2.37/nptl/pthread_getattr_np.c#L129).

So you have to change the content of `/proc/1/maps` in your `rootfs`, to make your range of stack address contains the real stack area.
Otherwise, the runtime will crash with `Failed to create CoreCLR, HRESULT: 0x8007000E`.

To get to know what's your stack address, see debug information provided in [app-elfloader](https://github.com/unikraft/app-elfloader/blob/bbb92f8c04bd58f0cf52b9e76250b0e03c5fc7e7/main.c#L239).
Change it to `uk_pr_err` and you will see log as follows:
```
ERR: [appelfloader] <main.c @ 239> helloworld: Application stack at 0x400080020 - 0x4000a0020
Failed to create CoreCLR, HRESULT: 0x8007000E
```
Copy the stack addresses into `./proc/1/maps` and replace the example addresses, run again and you will see:
```
Hello world from .Net 7.0!
```
2 changes: 2 additions & 0 deletions lang/c_sharp/helloworld/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
dotnet build
31 changes: 31 additions & 0 deletions lang/c_sharp/helloworld/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.IO;
using System;
using System.Data;

Console.WriteLine("Hello world from .Net 7.0!");

Console.WriteLine("Simple Calculator\n");

while (true)
{
Console.Write("Enter an expression (e.g., 2 + 3): ");
string expression = Console.ReadLine();

try
{
object result = new DataTable().Compute(expression, null);
Console.WriteLine($"Result: {expression} = {result}\n");
}
catch (Exception)
{
Console.WriteLine("Invalid expression or operation.");
}

Console.Write("Do you want to perform another calculation? (y/n): ");
string choice = Console.ReadLine();
if (choice.ToLower() != "y")
break;
Console.WriteLine();
}
Console.WriteLine("Calculator exited.");
Binary file not shown.
23 changes: 23 additions & 0 deletions lang/c_sharp/helloworld/bin/Debug/net7.0/helloworld.deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v7.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v7.0": {
"helloworld/1.0.0": {
"runtime": {
"helloworld.dll": {}
}
}
}
},
"libraries": {
"helloworld/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"runtimeOptions": {
"tfm": "net7.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "7.0.0"
}
}
}
10 changes: 10 additions & 0 deletions lang/c_sharp/helloworld/helloworld.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Binary file added lang/c_sharp/lib64/ld-linux-x86-64.so.2
Binary file not shown.
1 change: 1 addition & 0 deletions lang/c_sharp/proc/1/exe
1 change: 1 addition & 0 deletions lang/c_sharp/proc/1/maps
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
400080020-4000a0020 rw-p 00000000 00:00 0 [stack]
1 change: 1 addition & 0 deletions lang/c_sharp/proc/self
Binary file added lang/c_sharp/usr/lib/libc.so.6
Binary file not shown.
Binary file added lang/c_sharp/usr/lib/libgcc_s.so.1
Binary file not shown.
1 change: 1 addition & 0 deletions lang/c_sharp/usr/lib/libhostfxr.so
Binary file added lang/c_sharp/usr/lib/libicudata.so.73
Binary file not shown.
Binary file added lang/c_sharp/usr/lib/libicuuc.so.73.2.1
Binary file not shown.
Binary file added lang/c_sharp/usr/lib/liblzma.so.5
Binary file not shown.
Binary file added lang/c_sharp/usr/lib/libm.so.6
Binary file not shown.
Binary file added lang/c_sharp/usr/lib/libstdc++.so.6
Binary file not shown.
Binary file added lang/c_sharp/usr/lib/libunwind-x86_64.so.8
Binary file not shown.
Binary file added lang/c_sharp/usr/lib/libunwind.so.8
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
5b20af47d99620150c53eaf5db8636fdf730b126
7.0.7
Binary file not shown.
Loading