Skip to content

Can't import Component Model functions in WASI apps #94513

Closed
@mhmd-azeez

Description

@mhmd-azeez

Description

While you can import functions in WASI apps (see #90786), the process breaks down when the imported namespace contains : and /. This is problematic because these are valid characters and are used widely in Wasm Component Model spec. For example, wasi:http/handler is a valid import namespace:

(component
  (import "custom-hook" (func (param string) (result string)))
  (import "wasi:http/handler" (instance
    (export "request" (type $request (sub resource)))
    (export "response" (type $response (sub resource)))
    (export "handle" (func (param (own $request)) (result (own $response))))
  ))
  (import "url=<https://mycdn.com/my-component.wasm>" (component ...))
  (import "relative-url=<./other-component.wasm>,integrity=<sha256-X9ArH3k...>" (component ...))
  (import "locked-dep=<my-registry:sqlite@1.2.3>,integrity=<sha256-H8BRh8j...>" (component ...))
  (import "unlocked-dep=<my-registry:imagemagick@{>=1.0.0}>" (instance ...))
  (import "integrity=<sha256-Y3BsI4l...>" (component ...))
  ... impl
  (export "wasi:http/handler" (instance $http_handler_impl))
  (export "get-JSON" (func $get_json_impl))
)

Reproduction Steps

csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
    <OutputType>Exe</OutputType>
    <PublishTrimmed>true</PublishTrimmed>
    <WasmSingleFileBundle>true</WasmSingleFileBundle>
    <WasmBuildNative>true</WasmBuildNative>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

  <ItemGroup>
    <NativeFileReference Include="$(MSBuildThisFileDirectory)extism.c" />
    <_WasmNativeFileForLinking Include="@(NativeFileReference)" />
  </ItemGroup>

</Project>

Program.cs:

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class Program
{
    [DllImport("extism:host/user")]
    public static extern void do_something();

    public static void Main(string[] args)
    {
        
    }
}

extism.c:

#include <string.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>

#define IMPORT(a, b) __attribute__((import_module(a), import_name(b)))

IMPORT("extism:host/user", "do_something")
extern void do_something_wrapper();

void do_something() {
    return do_something_wrapper();
}

Then run:

PS D:\x\dotnet\component-model-repro> dotnet build

And then:

PS D:\x\dotnet\component-model-repro\obj\Debug\net8.0\wasi-wasm\wasm\for-build> wasm-objdump --details --section=import .\ComponentModelRepro.wasm | Select-String -Pattern 'extism'

Full Repro:
https://github.com/mhmd-azeez/component-model-repro

Expected behavior

PS D:\x\dotnet\component-model-repro\obj\Debug\net8.0\wasi-wasm\wasm\for-build> wasm-objdump --details --section=import .\ComponentModelRepro.wasm | Select-String -Pattern 'extism'

 - func[0] sig=0 <extism:host/user.do_something> <- extism:host/user.do_something

Actual behavior

PS D:\x\dotnet\component-model-repro\obj\Debug\net8.0\wasi-wasm\wasm\for-build> wasm-objdump --details --section=import .\ComponentModelRepro.wasm | Select-String -Pattern 'extism'

Notice: extism:host/user.do_something is not imported.

Also, in pinvoke-table.h there is no entry for do_something.

Regression?

No response

Known Workarounds

Simplify the import statemet in Program.cs:

[DllImport("extism")]
public static extern void do_something();

But keep extism.c as is:

#include <string.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>

#define IMPORT(a, b) __attribute__((import_module(a), import_name(b)))

IMPORT("extism:host/user", "do_something")
extern void do_something_wrapper();

void do_something() {
    return do_something_wrapper();
}

Configuration

PS D:\x\dotnet\component-model-repro> dotnet --info
.NET SDK:
 Version:   8.0.100-rc.2.23502.2
 Commit:    0abacfc2b6

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.100-rc.2.23502.2\

.NET workloads installed:
 [wasi-experimental]
   Installation Source: SDK 8.0.100-rc.2
   Manifest Version:    8.0.0-rc.2.23479.6/8.0.100-rc.2
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100-rc.2\microsoft.net.workload.mono.toolchain.current\8.0.0-rc.2.23479.6\WorkloadManifest.json
   Install Type:              Msi


Host:
  Version:      8.0.0-rc.2.23479.6
  Architecture: x64
  Commit:       0b25e38ad3

.NET SDKs installed:
  7.0.403 [C:\Program Files\dotnet\sdk]
  8.0.100-rc.2.23502.2 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-rc.2.23480.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-rc.2.23479.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.0-rc.2.23479.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]     

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download
PS D:\x\dotnet\component-model-repro> dotnet workload list

Installed Workload Id      Manifest Version                     Installation Source
-----------------------------------------------------------------------------------
wasi-experimental          8.0.0-rc.2.23479.6/8.0.100-rc.2      SDK 8.0.100-rc.2   

Use `dotnet workload search` to find additional workloads to install.

Other information

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions