惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
I
InfoQ
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园_首页
罗磊的独立博客
V
V2EX
李成银的技术随笔
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
True Tiger Recordings
Vercel News
Vercel News
Cyberwarzone
Cyberwarzone
Cisco Talos Blog
Cisco Talos Blog
F
Fox-IT International blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
M
Microsoft Research Blog - Microsoft Research
Know Your Adversary
Know Your Adversary
爱范儿
爱范儿
The Register - Security
The Register - Security
G
Google Developers Blog
The Hacker News
The Hacker News
Malwarebytes
Malwarebytes
S
Securelist
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
S
SegmentFault 最新的问题
博客园 - 叶小钗
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
博客园 - 聂微东
T
Threatpost
博客园 - 【当耐特】
D
Docker
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
V
Visual Studio Blog
C
Cisco Blogs
IT之家
IT之家
S
Security Archives - TechRepublic
Latest news
Latest news
阮一峰的网络日志
阮一峰的网络日志

Kevin Blog

产品随想 2 产品力与人才密度 产品随想 1 做营销还是做产品 Play PyTorch Stable Diffusion and ONNX, Ollama on Intel Core Ultra 5 225H Ubuntu 25.04 Play with ROCm, PyTorch, Ollama on Ubuntu 24.04 and 780m 写在新的旅程开始前 想念自然与青春 懒猫微服体验——自由协作的神器 没有光纤的日子怎么上网?自制 Home WI-FI! Swift on Server Tour 6 关联 User 和 Post Swift on Server Tour 5 创建 Users Swift on Server Tour 4 构建 Post Controller Swift on Server Tour 3 构建 Post 的 API 将不懂的日语一拍扫尽,介绍捧读全新的「OCR 工作台」功能 Swift on Server Tour 2 连通你的数据库与服务器 Swift on Server Tour 1 你的第一个 Server App 以及它背后的故事 Swift on Server Tour 0: 为什么这可能是你的好选择 纪念左耳朵耗子 How to learn Japanese by reading Novels and News with the help of Oyomi. BenQ WiT ScreenBar Halo 体验报告 记录 2021 年考驾照的体验 捧读的 EPUB 日语轻小说阅读器来了 使用 Go Mobile 开发跨平台 Library 使用 Kotlin Native 开发跨平台 Library 小番茄 - 一个只有陪伴的自习室
Write WebAssembly in Swift and use it in Swift App
2022-06-11 · via Kevin Blog

Background

I've been developing a new app for a while, one of the coolest ideas is to let the user write their own script to extend the app's ability.

But what kind of scripting language should I support? Why not support them all? So the decision is to adapt WebAssembly which grants the user's flavor.

What's more, We will also use Swift to write wasm thanks to the great SwiftWasm project.

Communication

WebAssembly was designed to be a 32-bit sandbox VM, it's safe and isolated from our 64-bit Swift host app.

The only way to communicate with each other is to copy memory from the host app into the VM and read the processed memory from VM back later.

This's a big challenge, but we will overcome it step by step.

Reference

If you are not quite familiar with Swift pointer & memory layout, check out these talks.

Exploring Swift Memory Layout Size, Stride, Alignment Unsafe Swift: Using Pointers and Interacting With C

Setup SwiftWasm

1. Install swiftenv

We will use swiftenv to manage the toolchain, So please install it first.

2. Install swiftwasm

Since swiftwasm has not been merged into the repo, we gonna install it on our own.

Here is the Github release page of the toolchain.

3. Set swift env in project

Use swiftenv to check the version of your swiftwasm

swiftenv versions

Mine is wasm-5.6.0 at the time, so at the root of your project folder, run swiftenv local wasm-5.6.0 to set the project level swift env.

Basic WebAssembly App

Finished project can be found here https://github.com/kevinzhow/write-wasm-in-swift-demo

As we know before, the Host app can only communicate with WebAssembly VM through memory copy, with the help of protobuf, we can transport data between 32-bit wasm VM and 64-bit host app easily.

But we also need to implement a few functions to handle these.

  1. allocate memory with size and return memory pointer
  2. deallocate memory at the pointer
  3. function to do the real work with memory address and size.

Quick look

import Foundation

@_cdecl("allocate")
func allocate(size: Int) -> UnsafeMutableRawPointer {
  return UnsafeMutableRawPointer.allocate(byteCount: size, alignment: MemoryLayout<UInt8>.alignment)
}
@_cdecl("deallocate")
func deallocate(pointer: UnsafeMutableRawPointer) {
  pointer.deallocate()
}

@_cdecl("change_article_proto")
func changeBookProto(protoData: UnsafeMutableRawPointer,  size: Int,  newAuthor: UnsafeRawPointer, authorSize: Int, newSize: UnsafeMutablePointer<Int>) -> UnsafeRawPointer {
    // Decode proto binary data
    let data = Data(bytes: protoData, count: size)
    var book = try! BookInfo(serializedData: data)

    // Change author
    book.author =  String(data: Data(bytes: newAuthor, count: authorSize), encoding: .utf8)!

    let newData = try! book.serializedData()
    newSize.pointee = newData.count

    // get the data pointer of the new book proto data
    let pointer = newData.withUnsafeBytes{ (bufferRawBufferPointer) -> UnsafeRawPointer in

        let bufferPointer: UnsafePointer<UInt8> = bufferRawBufferPointer.baseAddress!.assumingMemoryBound(to: UInt8.self)
        return UnsafeRawPointer(bufferPointer)
    }

    return pointer
}

Now we can build our wasm with command

swift build --triple wasm32-unknown-wasi  -c release -Xlinker --allow-undefined

We pass --allow-undefined to make sure all @_cdecl functions will be exported.

Then copy it out

cp .build/release/swiftwasm.wasm ./swiftwasm.wasm

Swift Host App

Finished Project can be found here https://github.com/kevinzhow/swiftwasm-host-app-demo

First, we implement a Wasm Module to handle the memory exchange and method call.

import Foundation
import WasmInterpreter
import SwiftProtobuf

public struct WasmModule {
    private let _vm: WasmInterpreter

    init() throws {
        _vm = try WasmInterpreter(module: Bundle.module.url(forResource: "swiftwasm", withExtension: "wasm")!)
    }

    /// Allocate memory on heap
    /// It returns byteoffset
    func allocate(size: Int) throws -> Int {
        return Int(try _vm.call("allocate", Int32(size)) as Int32)
    }
    
    func deallocate(byteOffset: Int) throws {
        try _vm.call("deallocate", Int32(byteOffset))
    }
    
    /// Allocate size on heap
    /// It returns byteoffset
    func allocateSize() throws -> Int {
        
        let length = MemoryLayout<Int32>.size
        
        let newSizePointer = try! allocate(size: length)
        
        return newSizePointer
    }
    
    /// Write string to heap
    /// It returns byteoffset
    func writeString(string: String) throws -> (Int, Int) {
        
        let length = Data(string.utf8).count
        
        let pointer = try! allocate(size: length)
        
        try _vm.writeToHeap(string: string, byteOffset: pointer)
        
        return (pointer, length)
    }
    
    /// Write Data to heap
    /// It returns byteoffset
    func writeData(data: Data) throws -> Int {
        
        let length = data.count
        
        let pointer = try! allocate(size: length)
        
        try _vm.writeToHeap(data: data, byteOffset: pointer)

        return pointer
    }

    /// Send Protobuf binary into
    func changeBook(_ book: BookInfo, author: String) throws -> BookInfo {
         let data = try! book.serializedData()
        
        let (newAuthorPtr, newAuthorSize) = try! writeString(string: author)
        
        let newSizePointer = try! allocateSize()
        
        let dataPointer = try writeData(data: data)
        
        let newArticlePointer = Int(try _vm.call("change_article_proto",
                                                 Int32(dataPointer),
                                                 Int32(data.count),
                                                 Int32(newAuthorPtr),
                                                 Int32(newAuthorSize),
                                                 Int32(newSizePointer)) as Int32)
        
        let newSizeValue = Int(try _vm.valueFromHeap(byteOffset: newSizePointer) as Int32)
        
        let newData = try _vm.dataFromHeap(byteOffset: newArticlePointer, length: newSizeValue)
        
        let newBook = try! BookInfo(serializedData: newData)
        
        try! deallocate(byteOffset: newAuthorPtr)
        try! deallocate(byteOffset: newSizePointer)
        try! deallocate(byteOffset: dataPointer)
        try! deallocate(byteOffset: newArticlePointer)
        
        return newBook
    }
}

Finally, we can use it.

main.swift

import WasmInterpreter
print("Hello, world!")

let module = try! WasmModule()
var book = BookInfo()
book.id = 1
book.author = "Apple"
book.title = "Swift Programming"
let newBook = try! module.changeBook(book, author: "Apple Stuff")
print(newBook.author)