Skip to content
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

Wasi release #29

Merged
merged 2 commits into from
Nov 26, 2024
Merged
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
2 changes: 1 addition & 1 deletion Wacs.WASIp1/FileUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static ulong GenerateInode(FileInfo fileInfo)
using (var sha256 = SHA256.Create())
{
// Combine file attributes
string data = $"{fileInfo.FullName}|{fileInfo.CreationTimeUtc.Ticks}|{fileInfo.Length}";
string data = $"{fileInfo.FullName}|{fileInfo.CreationTimeUtc.Ticks}|{(fileInfo.Exists ? fileInfo.Length : 0L)}";
byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(data));
// Use the first 8 bytes of the hash as the inode
ulong inode = BitConverter.ToUInt64(hashBytes, 0);
Expand Down
49 changes: 33 additions & 16 deletions Wacs.WASIp1/FsFdStat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,40 @@ public ErrNo FdFilestatGet(ExecContext ctx, fd fd, ptr bufPtr)
if (!GetFd(fd, out var fileDescriptor))
return ErrNo.NoEnt;

var hostPath = _state.PathMapper.MapToHostPath(fileDescriptor.Path);
var fileInfo = new FileInfo(hostPath);

var fileStat = new FileStat
if (fd < 3)
{
Device = 0, // Device ID - can be set later if available
Ino = FileUtil.GenerateInode(fileInfo),
Mode = fileDescriptor.Type,
NLink = 1, // Number of hard links - can be adjusted as needed
Size = (filesize)fileInfo.Length,
ATim = Clock.ToTimestamp(fileInfo.LastAccessTimeUtc),
MTim = Clock.ToTimestamp(fileInfo.LastWriteTimeUtc),
CTim = Clock.ToTimestamp(fileInfo.CreationTimeUtc)
};

mem.WriteStruct(bufPtr, ref fileStat);

var fileStat = new FileStat
{
Device = 0, // Device ID - can be set later if available
Ino = fd,
Mode = fileDescriptor.Type,
NLink = 1, // Number of hard links - can be adjusted as needed
Size = 0,
ATim = 0,
MTim = 0,
CTim = 0,
};

mem.WriteStruct(bufPtr, ref fileStat);
}
else
{
var hostPath = _state.PathMapper.MapToHostPath(fileDescriptor.Path);
var fileInfo = new FileInfo(hostPath);
var fileStat = new FileStat
{
Device = 0, // Device ID - can be set later if available
Ino = FileUtil.GenerateInode(fileInfo),
Mode = fileDescriptor.Type,
NLink = 1, // Number of hard links - can be adjusted as needed
Size = (filesize)fileInfo.Length,
ATim = Clock.ToTimestamp(fileInfo.LastAccessTimeUtc),
MTim = Clock.ToTimestamp(fileInfo.LastWriteTimeUtc),
CTim = Clock.ToTimestamp(fileInfo.CreationTimeUtc)
};

mem.WriteStruct(bufPtr, ref fileStat);
}
return ErrNo.Success;
}

Expand Down
68 changes: 68 additions & 0 deletions Wacs.WASIp1/Sock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// /*
// * Copyright 2024 Kelvin Nishikawa
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */

using System;
using Wacs.Core.Runtime;
using Wacs.Core.WASIp1;
using Wacs.WASIp1.Types;
using fd = System.UInt32;
using ptr = System.UInt32;
using size = System.UInt32;

namespace Wacs.WASIp1
{
public class Sock : IBindable
{
private readonly State _state;

public Sock(State state) => _state = state;

public void BindToRuntime(WasmRuntime runtime)
{
string module = "wasi_snapshot_preview1";
runtime.BindHostFunction<Func<ExecContext, fd, FdFlags, ptr, ErrNo>>((module, "sock_accept"), SockAccept);
runtime.BindHostFunction<Func<ExecContext, fd, ptr, size, RiFlags, ptr, ptr, ErrNo>>((module, "sock_recv"), SockRecv);
runtime.BindHostFunction<Func<ExecContext, fd, ptr, size, SiFlags, ptr, ErrNo>>((module, "sock_send"), SockSend);
runtime.BindHostFunction<Func<ExecContext, fd, SdFlags, ErrNo>>((module, "sock_shutdown"), SockShutdown);
}

public ErrNo SockAccept(ExecContext ctx, fd sock,
FdFlags flags, ptr ro_fd)
{
return ErrNo.NotSup;
}

public ErrNo SockRecv(ExecContext ctx, fd sock,
ptr ri_data, size ri_datalen, RiFlags ri_flags,
ptr ro_data_len, ptr ro_flags)
{
return ErrNo.NotSup;
}

public ErrNo SockSend(ExecContext ctx, fd sock,
ptr si_data, size si_data_len, SiFlags si_flags,
ptr ret_data_len)
{
return ErrNo.NotSup;
}

public ErrNo SockShutdown(ExecContext ctx, fd sock, SdFlags how)
{
return ErrNo.NotSup;
}

}
}
6 changes: 6 additions & 0 deletions Wacs.WASIp1/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// */

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;
using Wacs.WASIp1.Types;

Expand All @@ -41,5 +43,9 @@ public uint GetNextFd {


public VirtualPathMapper PathMapper { get; set; } = new();


public Dictionary<int, Socket> socketTable = new Dictionary<int, Socket>();
public int nextSocketDescriptor = 1;
}
}
31 changes: 31 additions & 0 deletions Wacs.WASIp1/Types/RiFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// /*
// * Copyright 2024 Kelvin Nishikawa
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */

using System;
using Wacs.Core.Attributes;
using Wacs.Core.Types;

namespace Wacs.WASIp1.Types
{
[Flags]
[WasmType(nameof(ValType.I32))]
public enum RiFlags : ushort
{
None = 0b0000,
RecvPeek = 0b0001,
RecvWaitAll = 0b0010,
}
}
30 changes: 30 additions & 0 deletions Wacs.WASIp1/Types/RoFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// /*
// * Copyright 2024 Kelvin Nishikawa
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */

using System;
using Wacs.Core.Attributes;
using Wacs.Core.Types;

namespace Wacs.WASIp1.Types
{
[Flags]
[WasmType(nameof(ValType.I32))]
public enum RoFlags : ushort
{
None = 0b0000,
RecvDataTruncated = 0b0001,
}
}
31 changes: 31 additions & 0 deletions Wacs.WASIp1/Types/SdFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// /*
// * Copyright 2024 Kelvin Nishikawa
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */

using System;
using Wacs.Core.Attributes;
using Wacs.Core.Types;

namespace Wacs.WASIp1.Types
{
[Flags]
[WasmType(nameof(ValType.I32))]
public enum SdFlags : ushort
{
None = 0b0000,
Rd = 0b0001,
Wr = 0b0010,
}
}
29 changes: 29 additions & 0 deletions Wacs.WASIp1/Types/SiFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// /*
// * Copyright 2024 Kelvin Nishikawa
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */

using System;
using Wacs.Core.Attributes;
using Wacs.Core.Types;

namespace Wacs.WASIp1.Types
{
[Flags]
[WasmType(nameof(ValType.I32))]
public enum SiFlags : ushort
{
None = 0b0000,
}
}
3 changes: 3 additions & 0 deletions Wacs.WASIp1/VirtualPathMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public void MoveHostPath(string oldHostPath, string newHostPath)

public string MapToHostPath(string guestPath)
{
if (guestPath.Split(Path.DirectorySeparatorChar)[0] == "dev")
return guestPath;

if (string.IsNullOrWhiteSpace(guestPath))
throw new ArgumentException("Guest path cannot be empty", nameof(guestPath));

Expand Down
4 changes: 2 additions & 2 deletions Wacs.WASIp1/Wacs.WASIp1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<RepositoryUrl>https://github.com/kelnishi/WACS</RepositoryUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<AssemblyName>Wacs.WASIp1</AssemblyName>
<AssemblyVersion>0.9.2</AssemblyVersion>
<AssemblyVersion>0.9.3</AssemblyVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.9.2</Version>
<Version>0.9.3</Version>
<RepositoryType>git</RepositoryType>
<TargetFrameworks>netstandard2.1;net8.0</TargetFrameworks>
</PropertyGroup>
Expand Down
8 changes: 6 additions & 2 deletions Wacs.WASIp1/Wasi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,30 @@ public class Wasi
private readonly Proc _proc;
private readonly Random _random;
private readonly State _state;
private readonly Poll _poll;
private readonly Sock _sock;

public Wasi(WasiConfiguration config)
{
_config = config;
_state = new State();

_proc = new Proc(_state);
_poll = new Poll(_state);
_env = new Env(config);
_clock = new Clock(config);
_random = new Random();

_sock = new Sock(_state);
_fs = new Filesystem(config, _state);
}

public void BindToRuntime(WasmRuntime runtime)
{
_proc.BindToRuntime(runtime);
_poll.BindToRuntime(runtime);
_env.BindToRuntime(runtime);
_clock.BindToRuntime(runtime);
_random.BindToRuntime(runtime);
_sock.BindToRuntime(runtime);
_fs.BindToRuntime(runtime);
}
}
Expand Down
Loading