microsoft / vs-pty.net

Fork pseudoterminals in C#
MIT License
49 stars 18 forks source link

forkpty() / execvpe() causing Segmentation fault under Ubuntu Linux 22.04.1 LTS (.Net 7.0.3) #32

Open ylmw0131 opened 1 year ago

ylmw0131 commented 1 year ago

Entry call:

using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using Pty.Net;

namespace TestApp
{
    internal class Application
    {
        static void Main(string[] args)
        {
            var app = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Path.Combine(Environment.SystemDirectory, "cmd.exe") : "sh";

            var options = new PtyOptions
            {
                Name = "Custom terminal",
                Cols = 80,
                Rows = 25,
                Cwd = Environment.CurrentDirectory,
                App = app,
                Environment = new Dictionary<string, string>()
                              {
                                { "FOO", "bar" },
                                { "Bazz", string.Empty },
                              },
            };

            PtyProvider.SpawnAsync(options, new CancellationToken());

            Task.Delay(-1).Wait();
        }
    }
}

Segmentation fault happened calling forkpty() / execvpe()

Pty.Net\Linux\PtyProvider.cs

int controller = 0;
int pid = forkpty(ref controller, null, ref term, ref winSize);

if (pid == -1)
{
    throw new InvalidOperationException($"forkpty(4) failed with error {Marshal.GetLastWin32Error()}");
}

if (pid == 0)
{
    // We are in a forked process! See http://man7.org/linux/man-pages/man2/fork.2.html for details.
    // Only our thread is running. We inherited open file descriptors and get a copy of the parent process memory.
    Environment.CurrentDirectory = options.Cwd;
    execvpe(options.App, terminalArgs, options.Environment);

    // Unreachable code after execvpe()
}

**

I have tested for .Net 6, it works.

Wonding whether it related to the large VSZ (Virtual Memory Size) on .Net 7 / .Net 8 which lead to forking Segmentation fault issue.

**

NetSDK: .Net 7.0.3 Application Type: Console Distributor ID: Ubuntu Description: Ubuntu 22.04.1 LTS x64 Release: 22.04 Codename: jammy

Saddingt0n commented 1 year ago

Hi @ylmw0131, how do you use this library in your project? I tried to run Pty.Net.Tests but I ran into multiple signature issues.

`Package 'MicroBuild.VisualStudio 2.0.55' from source 'https://pkgs.dev.azure.com/azure-public/vside/_packaging/vs-impl/nuget/v3/index.json': signatureValidationMode is set to require, so packages are allowed only if signed by trusted signers; however, this package is unsigned.

Package 'MicroBuild 2.0.55' from source 'https://pkgs.dev.azure.com/azure-public/vside/_packaging/vs-impl/nuget/v3/index.json': signatureValidationMode is set to require, so packages are allowed only if signed by trusted signers; however, this package is unsigned.

Package 'MicroBuild.Core 0.3.0' from source 'https://api.nuget.org/v3/index.json': This package is signed but not by a trusted signer.

Unable to resolve the .NET SDK version as specified in the global.json located at C:\Users\user\Downloads\vs-pty.net-main\global.json.

... `

ylmw0131 commented 1 year ago

Hi @ylmw0131, how do you use this library in your project? I tried to run Pty.Net.Tests but I ran into multiple signature issues.

`Package 'MicroBuild.VisualStudio 2.0.55' from source 'https://pkgs.dev.azure.com/azure-public/vside/_packaging/vs-impl/nuget/v3/index.json': signatureValidationMode is set to require, so packages are allowed only if signed by trusted signers; however, this package is unsigned.

Package 'MicroBuild 2.0.55' from source 'https://pkgs.dev.azure.com/azure-public/vside/_packaging/vs-impl/nuget/v3/index.json': signatureValidationMode is set to require, so packages are allowed only if signed by trusted signers; however, this package is unsigned.

Package 'MicroBuild.Core 0.3.0' from source 'https://api.nuget.org/v3/index.json': This package is signed but not by a trusted signer.

Unable to resolve the .NET SDK version as specified in the global.json located at C:\Users\anhnq41\Downloads\vs-pty.net-main\global.json.

... `

Hi Sadding,

I have ran the init.cmd ([init.ps1]), it help import all the valid packages already.

However, I bad not build the /dep/@Terminal project as it is quite complex and many other dependency. (and it should related to Windows support only)

Finally I take this library to spwan a Linux Shell only, so I have modified my own copy to remove all /Windows support which would be more clean and light weighted.

SofiaXu commented 1 year ago

I have same problem with .NET 7 in Ubuntu 20.04

RHamers commented 10 months ago

Same for me

shi3975 commented 6 months ago

"Wonding whether it related to the large VSZ (Virtual Memory Size) on .Net 7 / .Net 8 which lead to forking" I have a screenshot of the child process after executing forkpty(), with. net8 on the left and. net6 on the right image

yt6983138 commented 5 months ago

Not sure if it's the vsz, i checked the definition of struct termios here, and the definition is a bit different from ms' code

  1. cc_t c_line is unsigned char aka byte in .net, not sure why they defined it with public sbyte line;, the other fields seem to follow the type in original header file
  2. the control character they defined has weird value image as shown in the doc i linked, it should be 1 ~ 16 but they defined some unknown number (can't find where they are from)
  3. TermSpecialControlCharacter does not have VSWTC defined

if i make sbyte back to byte, add VSWTC definition and replace the value i mentioned in 2. to value defined in doc, it no longer segmentation fault, but i still need to figure out why it does not show anything in output though

Edit: it was not faulting in first ~10 tries then it starts faulting again, bruh. though it reduced the chance to like 30%, sometimes it can step through

Edit2: sometimes it throws AccessViolationExeception at random location but it's rare to happen

aaasoft commented 3 months ago

Not sure if it's the vsz, i checked the definition of struct termios here, and the definition is a bit different from ms' code

1. `cc_t c_line` is `unsigned char` aka byte in .net, not sure why they defined it with `public sbyte line;`, the other fields seem to follow the type in original header file

2. the control character they defined has weird value
   ![image](https://private-user-images.githubusercontent.com/83499886/335834652-5aa39347-51e8-4bce-9fac-0768c16a5f8a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE3Mjk3MTgsIm5iZiI6MTcyMTcyOTQxOCwicGF0aCI6Ii84MzQ5OTg4Ni8zMzU4MzQ2NTItNWFhMzkzNDctNTFlOC00YmNlLTlmYWMtMDc2OGMxNmE1ZjhhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzIzVDEwMTAxOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWZiZDQxYjJhMWNjOTk1ZjIwNWMyYzY4MWJkZmYxYmE5YWZkNzA3MTA3Yzk2YmQwZjQwMzg1ZjRlZjkwYWRmYTkmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.OBUrZVj2ZXpuXGXrC741o6CLkLyDbXPem0ggQJd5ICA)
   as shown in the doc i linked, it should be 1 ~ 16 but they defined some unknown number (can't find where they are from)

3. `TermSpecialControlCharacter` does not have `VSWTC` defined

if i make sbyte back to byte, add VSWTC definition and replace the value i mentioned in 2. to value defined in doc, it no longer segmentation fault, but i still need to figure out why it does not show anything in output though

Edit: it was not faulting in first ~10 tries then it starts faulting again, bruh. though it reduced the chance to like 30%, sometimes it can step through

Edit2: sometimes it throws AccessViolationExeception at random location but it's rare to happen

@yt6983138 Thanks for your work.This bug stoping me to upgrade my project to .NET 8.Hope you can fix this bug.

ylmw0131 commented 3 months ago

Thank you

On Tue, 23 Jul 2024 at 18:13, 张鹏 @.***> wrote:

Not sure if it's the vsz, i checked the definition of struct termios here https://sites.uclouvain.be/SystInfo/usr/include/bits/termios.h.html, and the definition is a bit different from ms' code

  1. cc_t c_line is unsigned char aka byte in .net, not sure why they defined it with public sbyte line;, the other fields seem to follow the type in original header file

  2. the control character they defined has weird value image as shown in the doc i linked, it should be 1 ~ 16 but they defined some unknown number (can't find where they are from)

  3. TermSpecialControlCharacter does not have VSWTC defined

if i make sbyte back to byte, add VSWTC definition and replace the value i mentioned in 2. to value defined in doc, it no longer segmentation fault, but i still need to figure out why it does not show anything in output though

Edit: it was not faulting in first ~10 tries then it starts faulting again, bruh. though it reduced the chance to like 30%, sometimes it can step through

Edit2: sometimes it throws AccessViolationExeception at random location but it's rare to happen

@yt6983138 https://github.com/yt6983138 Thanks for your work.This bug stoping me to upgrade my project to .NET 8.Hope you can fix this bug.

— Reply to this email directly, view it on GitHub https://github.com/microsoft/vs-pty.net/issues/32#issuecomment-2244805341, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAWYAECGPYA53N6WZ7MLKRDZNYUFLAVCNFSM6AAAAAAVUZXSWOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBUHAYDKMZUGE . You are receiving this because you were mentioned.Message ID: @.***>

swety2003 commented 1 month ago

What about this issue now And is there any solution?

aaasoft commented 1 month ago

Hi. I found a workaround at https://github.com/dotnet/runtime/issues/79469#issuecomment-1345148117

Add an enviroment variable as below before run: DOTNET_EnableWriteXorExecute=0

Or use script: export DOTNET_EnableWriteXorExecute=0

yt6983138 commented 1 month ago

nice you found a solution, I cannot/no longer work at the project now to test, so wish it works for all other people