Skip to content

WITOverlayGenerator: Support Embedded target build #103

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

Merged
merged 5 commits into from
Jul 17, 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
52 changes: 39 additions & 13 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,41 @@ jobs:
# Swift 5.8.1
- os: macos-13
xcode: Xcode_14.3.1
development-toolchain-tag: swift-DEVELOPMENT-SNAPSHOT-2024-07-08-a
wasi-swift-sdk-download: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi.artifactbundle.zip"
wasi-swift-sdk-id: DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi
# Swift 5.9.0
- os: macos-13
xcode: Xcode_15.0.1
development-toolchain-tag: swift-DEVELOPMENT-SNAPSHOT-2024-07-08-a
wasi-swift-sdk-download: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi.artifactbundle.zip"
wasi-swift-sdk-id: DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- id: setup-swiftwasm
uses: swiftwasm/setup-swiftwasm@v1
with:
swift-version: "wasm-5.8.0-RELEASE"
add-to-path: false
- id: setup-development
run: |
toolchain_path="/Library/Developer/Toolchains/${{ matrix.development-toolchain-tag }}.xctoolchain"
pkg="$(mktemp -d)/InstallMe.pkg"
development_toolchain_download="https://download.swift.org/development/xcode/${{ matrix.development-toolchain-tag }}/${{ matrix.development-toolchain-tag }}-osx.pkg"
curl -L "$development_toolchain_download" -o $pkg
sudo installer -pkg $pkg -target /
echo "toolchain-path=$toolchain_path" >> $GITHUB_OUTPUT
"$toolchain_path/usr/bin/swift" sdk install "${{ matrix.wasi-swift-sdk-download }}"
wasi_sdk_path=$("$toolchain_path/usr/bin/swift" sdk configure --show-configuration "${{ matrix.wasi-swift-sdk-id }}" wasm32-unknown-wasi | grep sdkRootPath: | cut -d: -f2)
echo "wasi-swift-sdk-path=$(dirname $wasi_sdk_path)" >> $GITHUB_OUTPUT

- name: Select Xcode version
run: sudo xcode-select -switch /Applications/${{ matrix.xcode }}.app
- name: Configure Tests/default.json
run: |
cat <<EOS > Tests/default.json
{
"swiftExecutablePath": "${{ steps.setup-swiftwasm.outputs.toolchain-path }}/usr/bin/swift",
"wasiSwiftSDKPath": "${{ steps.setup-swiftwasm.outputs.wasi-swift-sdk-path }}",
"hostSwiftExecutablePath": "$(xcrun --find swift)",
"hostSdkRootPath": "$(xcrun --show-sdk-path --sdk macosx)"
}
Expand All @@ -46,13 +60,21 @@ jobs:
matrix:
include:
- swift: "5.8-focal"
swiftwasm-download: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.8.0-RELEASE/swift-wasm-5.8.0-RELEASE-ubuntu20.04_x86_64.tar.gz"
development-toolchain-download: "https://download.swift.org/development/ubuntu2004/swift-DEVELOPMENT-SNAPSHOT-2024-07-08-a/swift-DEVELOPMENT-SNAPSHOT-2024-07-08-a-ubuntu20.04.tar.gz"
wasi-swift-sdk-download: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi.artifactbundle.zip"
wasi-swift-sdk-id: DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi
- swift: "5.8-amazonlinux2"
swiftwasm-download: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.8.0-RELEASE/swift-wasm-5.8.0-RELEASE-amazonlinux2_x86_64.tar.gz"
development-toolchain-download: "https://download.swift.org/development/amazonlinux2/swift-DEVELOPMENT-SNAPSHOT-2024-07-08-a/swift-DEVELOPMENT-SNAPSHOT-2024-07-08-a-amazonlinux2.tar.gz"
wasi-swift-sdk-download: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi.artifactbundle.zip"
wasi-swift-sdk-id: DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi
- swift: "5.9-focal"
swiftwasm-download: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.9.2-RELEASE/swift-wasm-5.9.2-RELEASE-ubuntu20.04_x86_64.tar.gz"
development-toolchain-download: "https://download.swift.org/development/ubuntu2004/swift-DEVELOPMENT-SNAPSHOT-2024-07-08-a/swift-DEVELOPMENT-SNAPSHOT-2024-07-08-a-ubuntu20.04.tar.gz"
wasi-swift-sdk-download: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi.artifactbundle.zip"
wasi-swift-sdk-id: DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi
- swift: "5.9-amazonlinux2"
swiftwasm-download: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-5.9.2-RELEASE/swift-wasm-5.9.2-RELEASE-amazonlinux2_x86_64.tar.gz"
development-toolchain-download: "https://download.swift.org/development/amazonlinux2/swift-DEVELOPMENT-SNAPSHOT-2024-07-08-a/swift-DEVELOPMENT-SNAPSHOT-2024-07-08-a-amazonlinux2.tar.gz"
wasi-swift-sdk-download: "https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi.artifactbundle.zip"
wasi-swift-sdk-id: DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi

runs-on: ubuntu-20.04
name: "build-linux (${{ matrix.swift }})"
Expand All @@ -65,19 +87,23 @@ jobs:
echo 'docker exec -i build-container "$@"' > ./build-exec
chmod +x ./build-exec

- name: Install WebAssembly toolchain
id: setup-swiftwasm
- name: Install Development toolchain
id: setup-development
run: |
toolchain_path="/opt/swiftwasm"
./build-exec mkdir -p "$toolchain_path"
curl -L ${{ matrix.swiftwasm-download }} | ./build-exec tar xz --strip-component 1 -C "$toolchain_path"
curl -L ${{ matrix.development-toolchain-download }} | ./build-exec tar xz --strip-component 1 -C "$toolchain_path"
echo "toolchain-path=$toolchain_path" >> $GITHUB_OUTPUT
./build-exec "$toolchain_path/usr/bin/swift" sdk install "${{ matrix.wasi-swift-sdk-download }}"
wasi_sdk_path=$(./build-exec "$toolchain_path/usr/bin/swift" sdk configure --show-configuration "${{ matrix.wasi-swift-sdk-id }}" wasm32-unknown-wasi | grep sdkRootPath: | cut -d: -f2)
echo "wasi-swift-sdk-path=$(dirname $wasi_sdk_path)" >> $GITHUB_OUTPUT

- name: Configure Tests/default.json
run: |
cat <<EOS > Tests/default.json
{
"swiftExecutablePath": "${{ steps.setup-swiftwasm.outputs.toolchain-path }}/usr/bin/swift",
"swiftExecutablePath": "${{ steps.setup-development.outputs.toolchain-path }}/usr/bin/swift",
"wasiSwiftSDKPath": "${{ steps.setup-development.outputs.wasi-swift-sdk-path }}",
"hostSwiftExecutablePath": "/usr/bin/swift"
}
EOS
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
.testTarget(
name: "WITOverlayGeneratorTests",
dependencies: ["WITOverlayGenerator", "WasmKit", "WasmKitWASI"],
exclude: ["Fixtures", "Compiled", "Generated"],
exclude: ["Fixtures", "Compiled", "Generated", "EmbeddedSupport"],
plugins: [.plugin(name: "GenerateOverlayForTesting")]
),
.plugin(
Expand Down
17 changes: 12 additions & 5 deletions Sources/WITOverlayGenerator/GuestGenerators/GuestPrelude.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ let guestPrelude = """
// Generated by the wit-overlay-generator

#if arch(wasm32)
#if canImport(WASILibc)
import WASILibc
#endif
@_implementationOnly import _CabiShims

fileprivate enum Prelude {
Expand Down Expand Up @@ -76,12 +78,14 @@ let guestPrelude = """
_ array: [Element], elementSize: Int, elementAlignment: Int,
storeElement: (Element, UnsafeMutableRawPointer) -> Void
) -> (pointer: UInt, length: UInt) {
let newBuffer = malloc(elementSize * array.count)
let newBuffer = UnsafeMutableRawPointer.allocate(
byteCount: elementSize * array.count, alignment: elementAlignment
)
for (i, element) in array.enumerated() {
storeElement(element, newBuffer!.advanced(by: i * elementSize))
storeElement(element, newBuffer.advanced(by: i * elementSize))
}
return (
newBuffer.map { UInt(bitPattern: $0) } ?? 0, UInt(array.count)
UInt(bitPattern: newBuffer), UInt(array.count)
)
}

Expand Down Expand Up @@ -129,9 +133,12 @@ let guestPrelude = """

// TODO: use `@_expose(wasm)`
// NOTE: multiple objects in a component can have cabi_realloc definition so use `@_weakLinked` here.
#if canImport(WASILibc)
@_weakLinked
@_cdecl("cabi_realloc")
func cabi_realloc(old: UnsafeMutableRawPointer, oldSize: UInt, align: UInt, newSize: UInt) -> UnsafeMutableRawPointer {
realloc(old, Int(newSize))
func cabi_realloc(old: UnsafeMutableRawPointer?, oldSize: UInt, align: UInt, newSize: UInt) -> UnsafeMutableRawPointer? {
@_extern(c, "realloc") func realloc(_ old: UnsafeMutableRawPointer?, _ newSize: Int) -> UnsafeMutableRawPointer?
return realloc(old, Int(newSize))
}
#endif
"""
67 changes: 67 additions & 0 deletions Tests/WITOverlayGeneratorTests/EmbeddedSupport/MinLibc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This file is used to provide a "just enough" implementation of some C standard library functions
// required by Embedded target builds.
#include <stdint.h>
#include <stddef.h>
#define WASM_PAGE_SIZE 0x10000

size_t __builtin_wasm_memory_grow(int32_t index, size_t delta);
void *__builtin_memcpy(void *dest, const void *src, size_t n);

static void* alignedAlloc(size_t alignment, size_t size) {
size_t basePageSize = __builtin_wasm_memory_grow(0, (size + 0xffff) / 0x10000);
if (basePageSize == (size_t)-1) {
return NULL;
}
size_t base = basePageSize * WASM_PAGE_SIZE;
base = (base + alignment - 1) & -alignment;
return (void*)base;
}

/// NOTE: always allocates a new memory page by `memory.grow`
int posix_memalign(void** memptr, size_t alignment, size_t size) {
void* ptr = alignedAlloc(alignment, size);
if (ptr == NULL) {
return -1;
}
*memptr = ptr;
return 0;
}

/// NOTE: always allocates a new memory page by `memory.grow` and copies the old data
void* cabi_realloc(void* old, size_t oldSize, size_t align, size_t newSize) {
if (old != NULL) {
void* new = alignedAlloc(align, newSize);
if (new != NULL) {
__builtin_memcpy(new, old, oldSize < newSize ? oldSize : newSize);
}
return new;
} else {
return alignedAlloc(align, newSize);
}
}

void *memmove(void *dest, const void *src, size_t n) {
return __builtin_memcpy(dest, src, n);
}

void *memcpy(void *dest, const void *src, size_t n) {
// `memory.copy` is safe even if `src` and `dest` overlap
// > Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
// > https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md
return __builtin_memcpy(dest, src, n);
}

/// NOTE: does nothing as we don't manage memory chunks
void free(void *ptr) {}

/// NOTE: just returns the input character as is, no output is produced
int putchar(int c) {
return c;
}

/// NOTE: fills the buffer with a constant value
void arc4random_buf(void *buf, size_t n) {
for (size_t i = 0; i < n; i++) {
((uint8_t *)buf)[i] = (uint8_t)42;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import WIT
class RuntimeSmokeTests: XCTestCase {
func testCallExportByGuest() throws {
var harness = try RuntimeTestHarness(fixture: "Smoke")
let (runtime, instance) = try harness.build(link: SmokeTestWorld.link(_:))
let component = SmokeTestWorld(moduleInstance: instance)
_ = try component.hello(runtime: runtime)
try harness.build(link: SmokeTestWorld.link(_:)) { (runtime, instance) in
let component = SmokeTestWorld(moduleInstance: instance)
_ = try component.hello(runtime: runtime)
}
}
}
103 changes: 85 additions & 18 deletions Tests/WITOverlayGeneratorTests/Runtime/RuntimeTestHarness.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import XCTest
struct RuntimeTestHarness {
struct Configuration: Codable {
let swiftExecutablePath: URL
let wasiSwiftSDKPath: URL

var swiftCompilerExecutablePath: URL {
swiftExecutablePath.deletingLastPathComponent().appendingPathComponent("swiftc")
Expand Down Expand Up @@ -53,9 +54,11 @@ struct RuntimeTestHarness {
throw XCTSkip("""
Please create 'Tests/default.json' with this or similar contents:
{
"swiftExecutablePath": "/Library/Developer/Toolchains/swift-wasm-5.8.0-RELEASE.xctoolchain/usr/bin/swift"
"swiftExecutablePath": "$HOME/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-07-08-a.xctoolchain/usr/bin/swift",
"wasiSwiftSDKPath": "$HOME/Library/org.swift.swiftpm/swift-sdks/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi.artifactbundle/DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasi/wasm32-unknown-wasi"
}


or specify `configuration` parameter in your test code.
""")
}
Expand Down Expand Up @@ -116,32 +119,70 @@ struct RuntimeTestHarness {
}

/// Build up WebAssembly module from the fixture and instantiate WasmKit runtime with the module.
mutating func build(link: (inout [String: HostModule]) -> Void) throws -> (Runtime, ModuleInstance) {
defer { cleanupTemporaryFiles() }
let compiled = try compile(inputFiles: collectGuestInputFiles())

let wasi = try WASIBridgeToHost(args: [compiled.path])
var hostModules: [String: HostModule] = wasi.hostModules
link(&hostModules)

let module = try parseWasm(filePath: .init(compiled.path))
let runtime = Runtime(hostModules: hostModules)
let instance = try runtime.instantiate(module: module)
return (runtime, instance)
mutating func build(
link: (inout [String: HostModule]) -> Void,
run: (Runtime, ModuleInstance) throws -> Void
) throws {
for compile in [compileForEmbedded, compileForWASI] {
defer { cleanupTemporaryFiles() }
let compiled = try compile(collectGuestInputFiles())

let wasi = try WASIBridgeToHost(args: [compiled.path])
var hostModules: [String: HostModule] = wasi.hostModules
link(&hostModules)

let module = try parseWasm(filePath: .init(compiled.path))
let runtime = Runtime(hostModules: hostModules)
let instance = try runtime.instantiate(module: module)
try run(runtime, instance)
}
}

func compileForEmbedded(inputFiles: [String]) throws -> URL {
let embeddedSupport = Self.testsDirectory.appendingPathComponent("EmbeddedSupport")

let libc = embeddedSupport.appendingPathComponent("MinLibc.c")
let libcObjFile = Self.testsDirectory
.appendingPathComponent("Compiled")
.appendingPathComponent("MinLibc.o")
FileManager.default.createFile(atPath: libcObjFile.deletingLastPathComponent().path, contents: nil, attributes: nil)
try compileToObj(cInputFiles: [libc.path], arguments: [
"-target", "wasm32-unknown-none-wasm",
// Enable bulk memory operations for `realloc`
"-mbulk-memory",
], outputPath: libcObjFile)

return try compile(inputFiles: inputFiles + [libcObjFile.path], arguments: [
"-target", "wasm32-unknown-none-wasm",
"-enable-experimental-feature", "Embedded",
"-wmo", "-Xcc", "-fdeclspec",
"-Xfrontend", "-disable-stack-protector",
"-Xlinker", "--no-entry", "-Xclang-linker", "-nostdlib",
])
}

func compileForWASI(inputFiles: [String]) throws -> URL {
return try compile(inputFiles: inputFiles, arguments: [
"-target", "wasm32-unknown-wasi",
"-static-stdlib",
"-Xclang-linker", "-mexec-model=reactor",
"-resource-dir", configuration.wasiSwiftSDKPath.appendingPathComponent( "/swift.xctoolchain/usr/lib/swift_static").path,
"-sdk", configuration.wasiSwiftSDKPath.appendingPathComponent("WASI.sdk").path,
"-Xclang-linker", "-resource-dir",
"-Xclang-linker", configuration.wasiSwiftSDKPath.appendingPathComponent("swift.xctoolchain/usr/lib/swift_static/clang").path
])
}

/// Compile the given input Swift source files into core Wasm module
func compile(inputFiles: [String]) throws -> URL {
func compile(inputFiles: [String], arguments: [String]) throws -> URL {
let outputPath = Self.testsDirectory
.appendingPathComponent("Compiled")
.appendingPathComponent("\(fixtureName).core.wasm")
try fileManager.createDirectory(at: outputPath.deletingLastPathComponent(), withIntermediateDirectories: true)
let process = Process()
process.launchPath = configuration.swiftCompilerExecutablePath.path
process.arguments = inputFiles + [
"-target", "wasm32-unknown-wasi",
process.arguments = inputFiles + arguments + [
"-I\(Self.sourcesDirectory.appendingPathComponent("_CabiShims").appendingPathComponent("include").path)",
"-Xclang-linker", "-mexec-model=reactor",
// TODO: Remove `--export-all` linker option by replacing `@_cdecl` with `@_expose(wasm)`
"-Xlinker", "--export-all",
"-o", outputPath.path
Expand All @@ -161,7 +202,9 @@ struct RuntimeTestHarness {
"""
}.joined(separator: "\n====================\n")
let message = """
Failed to execute \(process.arguments?.joined(separator: " ") ?? " ")
Failed to execute \(
([configuration.swiftCompilerExecutablePath.path] + (process.arguments ?? [])).joined(separator: " ")
)
Exit status: \(process.terminationStatus)
Input files:
\(fileContents)
Expand All @@ -170,4 +213,28 @@ struct RuntimeTestHarness {
}
return outputPath
}

/// Compile the given input Swift source files into an object file
func compileToObj(cInputFiles: [String], arguments: [String], outputPath: URL) throws {
let process = Process()
// Assume that clang is placed alongside swiftc
process.launchPath = configuration.swiftCompilerExecutablePath
.deletingLastPathComponent().appendingPathComponent("clang").path
process.arguments = cInputFiles + arguments + [
"-c",
"-o", outputPath.path
]
process.environment = [:]
process.launch()
process.waitUntilExit()
guard process.terminationStatus == 0 else {
let message = """
Failed to execute \(
([configuration.swiftCompilerExecutablePath.path] + (process.arguments ?? [])).joined(separator: " ")
)
Exit status: \(process.terminationStatus)
"""
throw Error(description: message)
}
}
}
Loading
Loading