swiftwasm/WasmKit

Why memory is empty

996ccs opened this issue · 11 comments

I imported the wasm file to parse the wasm output data. The memory is empty.

wat file

(module
 (type $0 (func (result i32)))
 (memory $0 1)
 (data $0 (i32.const 1036) ",")
 (data $0.1 (i32.const 1048) "\02\00\00\00\16\00\00\00h\00e\00l\00l\00o\00 \00w\00o\00r\00l\00d")
 (export "getHelloWorld" (func $assembly/index/getHelloWorld))
 (export "memory" (memory $0))
 (func $assembly/index/getHelloWorld (result i32)
  i32.const 1056
 )
)
Module(types: [WasmKit.FunctionType(parameters: [], results: [i32])], functions: [WasmKit.GuestFunction(type: 0, defaultLocals: UnsafeBufferPointer(start: 0x00006000022583a0, count: 0), _bodyStorage: nil, materializer: (Function))], tables: [], memories: [WasmKit.Memory(type: WasmKit.Limits(min: 1, max: nil, isMemory64: false))], globals: [], elements: [], dataCount: Optional(2), data: [WasmKit.DataSegment.active(WasmKit.DataSegment.Active(index: 0, offset: [WasmKit.Instruction.numericConst(I32(1036))], initializer: ArraySlice([44]))), WasmKit.DataSegment.active(WasmKit.DataSegment.Active(index: 0, offset: [WasmKit.Instruction.numericConst(I32(1048))], initializer: ArraySlice([2, 0, 0, 0, 22, 0, 0, 0, 104, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 119, 0, 111, 0, 114, 0, 108, 0, 100])))], start: nil, imports: [], exports: [WasmKit.Export(name: "hello_world", descriptor: WasmKit.ExportDescriptor.function(0)), WasmKit.Export(name: "memory", descriptor: WasmKit.ExportDescriptor.memory(0))], customSections: [WasmKit.CustomSection(name: "sourceMappingURL", bytes: ArraySlice([18, 46, 47, 114, 101, 108, 101, 97, 115, 101, 46, 119, 97, 115, 109, 46, 109, 97, 112]))])

What do you mean "memory is empty"? The parsed module has WasmKit.Memory in memories and WasmKit.DataSegment in data.

What do you mean "memory is empty"? The parsed module has WasmKit.Memory in memories and WasmKit.DataSegment in data.

Yes, look at the output of WasmKit.Export(name: "memory", descriptor: WasmKit.ExportDescriptor.memory(0))]

@996ccs WasmKit.ExportDescriptor.memory(0) means the export entry points to the index 0 of memories

@996ccs WasmKit.ExportDescriptor.memory(0) means the export entry points to the index 0 of memories

Thank you very much for your reply. Please tell me how to obtain the initializer data in WasmKit.DataSegment. Like this data WasmKit.DataSegment.Active(index: 0, offset: [WasmKit.Instruction.numericConst(I32(1048))], initializer: ArraySlice([2, 0, 0, 0, 22, 0, 0, 0 , 104, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 119, 0, 111, 0, 114, 0, 108, 0, 100]))) How do I get initializer.

I don't understand what you are trying to do but you can get the data segment initializer by something like below:

let initializer = switch dataSegment {
case .active(let active): Array(active.initializer)
case .passive(let initializer): initializer
}

I don't understand what you are trying to do but you can get the data segment initializer by something like below:

let initializer = switch dataSegment {
case .active(let active): Array(active.initializer)
case .passive(let initializer): initializer
}

I used the code you provided and encountered the same problem myself. The error message 'initializer' is inaccessible due to 'internal' protection level

Could you try it again with main branch of WasmKit?

Could you try it again with main branch of WasmKit?

I updated version 0.04, and now the data under the module cannot be accessed. My need is to get the initializer: ArraySlice in WasmParser.DataSegment.active. Can it be exposed? Thanks

Ah, sorry. If you just want to extract data segment, you can use WasmParser module directly.

Ah, sorry. If you just want to extract data segment, you can use WasmParser module directly.

Can you tell me what I should do? Because I am not good at wasmkit and swift development

I added several API documents on WasmParser: https://swiftpackageindex.com/swiftwasm/WasmKit/main/documentation/wasmparser
(It may take a few hours to be up-to-date)

And I think the following code snippet works for your use case as is. Feel free to re-open again if you have more question. Thanks.

import WasmParser

let bytes = try Array(Data(contentsOf: URL(fileURLWithPath: "wasm/hello.wasm")))

var parser = WasmParser.Parser(bytes: bytes)
while let payload = try parser.parseNext() {
    switch payload {
    case .dataSection(let dataSection):
        for segment in dataSection {
            switch segment {
            case .passive(let data):
                print("Data segment: \(data)")
            case .active(let active):
                print("Data segment: \(active.index) \(active.offset) \(active.initializer)")
            }
        }
    default: break
    }
}