File: FuzzDifferential.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (295 lines) | stat: -rw-r--r-- 11,352 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import WasmCAPI
import WasmKit
import WAT
import SystemPackage
import Foundation

protocol Engine {
    var name: String { get }
    func run(moduleBytes: [UInt8]) throws -> ExecResult
}

extension Engine {
    var name: String {
        return String(describing: type(of: self))
    }
}

struct ExecResult {
    let values: [Value]?
    let trap: String?
    let memory: [UInt8]?

    var hasTrap: Bool {
        return trap != nil
    }

    static func check(_ lhs: (result: ExecResult, name: String), _ rhs: (result: ExecResult, name: String)) -> Bool {
        let (lhsName, rhsName) = (lhs.name, rhs.name)
        let (lhs, rhs) = (lhs.result, rhs.result)
        guard lhs.hasTrap == rhs.hasTrap else {
            print("Traps do not match: \(lhsName):\(lhs.trap ?? "nil") vs \(rhsName):\(rhs.trap ?? "nil")")
            return false
        }
        guard lhs.memory == rhs.memory else {
            guard lhs.memory?.count == rhs.memory?.count else {
                print("Memory sizes do not match: \(lhsName):\(lhs.memory?.count ?? 0) vs \(rhsName):\(rhs.memory?.count ?? 0)")
                return false
            }
            for (i, (lhsByte, rhsByte)) in zip(lhs.memory ?? [], rhs.memory ?? []).enumerated() {
                if lhsByte != rhsByte {
                    print("Memory byte \(i) does not match: \(lhsName):\(lhsByte) vs \(rhsName):\(rhsByte)")
                }
            }
            return false
        }
        guard lhs.values?.count == rhs.values?.count else {
            print("Value counts do not match: \(lhsName):\(lhs.values?.count ?? 0) vs \(rhsName):\(rhs.values?.count ?? 0)")
            return false
        }
        for (i, (lhsValue, rhsValue)) in zip(lhs.values ?? [], rhs.values ?? []).enumerated() {
            if !Value.bitwiseEqual(lhsValue, rhsValue) {
                print("Value \(i) does not match: \(lhsName):\(lhsValue) vs \(rhsName):\(rhsValue)")
                return false
            }
        }
        return true
    }
}

struct ExecError: Error, CustomStringConvertible {
    let description: String
    init(_ description: String) {
        self.description = description
    }
}

extension ValueType {
    var defaultValue: Value {
        switch self {
        case .i32: return .i32(0)
        case .i64: return .i64(0)
        case .f32: return .f32(0)
        case .f64: return .f64(0)
        case .ref(.funcRef): return .ref(.function(0))
        case .ref(.externRef): return .ref(.extern(0))
        }
    }
}

extension Value {
    static func bitwiseEqual(_ lhs: Value, _ rhs: Value) -> Bool {
        switch (lhs, rhs) {
        case let (.i32(lhs), .i32(rhs)): return lhs == rhs
        case let (.i64(lhs), .i64(rhs)): return lhs == rhs
        case let (.f32(lhs), .f32(rhs)): return lhs == rhs
        case let (.f64(lhs), .f64(rhs)): return lhs == rhs
        case let (.ref(lhs), .ref(rhs)): return lhs == rhs
        default: return false
        }
    }
}

extension wasm_name_t {
    var string: String {
        return data.withMemoryRebound(to: UInt8.self, capacity: Int(size)) {
            String(decoding: UnsafeBufferPointer(start: $0, count: Int(size)), as: UTF8.self)
        }
    }
}

struct WasmKitEngine: Engine {
    var name: String { return "wasmkit" }
    func run(moduleBytes: [UInt8]) throws -> ExecResult {
        let module = try WasmKit.parseWasm(bytes: moduleBytes)
        let engine = WasmKit.Engine()
        let store = WasmKit.Store(engine: engine)
        let instance = try module.instantiate(store: store)
        let exports = instance.exports.sorted(by: { $0.name < $1.name })
        let memories: [Memory] = exports.compactMap {
            guard case let .memory(memory) = $0.value else {
                return nil
            }
            return memory
        }
        guard memories.count <= 1 else {
            throw ExecError("Multiple memories are not supported")
        }
        let memory = memories.first
        let funcs: [Function] = exports.compactMap {
            guard case let .function(fn) = $0.value else {
                return nil
            }
            return fn
        }
        guard let fn = funcs.first else {
            throw ExecError("No functions found")
        }
        let type = fn.type
        let arguments = type.parameters.map { $0.defaultValue }
        do {
            let results = try fn(arguments)
            return ExecResult(values: results, trap: nil, memory: memory?.data)
        } catch {
            return ExecResult(values: nil, trap: String(describing: error), memory: memory?.data)
        }
    }
}

struct ReferenceEngine: Engine {
    var name: String { return "reference" }
    func run(moduleBytes: [UInt8]) throws -> ExecResult {
        return try moduleBytes.withUnsafeBytes { (module: UnsafeRawBufferPointer) -> ExecResult in
            try run(module: module)
        }
    }

    func run(module: UnsafeRawBufferPointer) throws -> ExecResult {
        let engine = WasmCAPI.wasm_engine_new()
        let store = WasmCAPI.wasm_store_new(engine)
        var bytes = WasmCAPI.wasm_byte_vec_t()
        wasm_byte_vec_new(&bytes, module.count, module.baseAddress)
        defer { wasm_byte_vec_delete(&bytes) }

        guard let module = WasmCAPI.wasm_module_new(store, &bytes) else {
            throw ExecError("Failed to create module")
        }
        defer { WasmCAPI.wasm_module_delete(module) }

        var rawExportTypes = WasmCAPI.wasm_exporttype_vec_t()
        WasmCAPI.wasm_module_exports(module, &rawExportTypes)
        defer { WasmCAPI.wasm_exporttype_vec_delete(&rawExportTypes) }

        var imports = WasmCAPI.wasm_extern_vec_t()

        guard let instance = WasmCAPI.wasm_instance_new(store, module, &imports, nil) else {
            throw ExecError("Failed to create instance")
        }
        defer { WasmCAPI.wasm_instance_delete(instance) }

        var rawExports = WasmCAPI.wasm_extern_vec_t()
        WasmCAPI.wasm_instance_exports(instance, &rawExports)
        defer { WasmCAPI.wasm_extern_vec_delete(&rawExports) }

        var memory: OpaquePointer?
        var fn: OpaquePointer?

        let exportTypes = UnsafeBufferPointer(start: rawExportTypes.data, count: Int(rawExportTypes.size))
        let exports = UnsafeBufferPointer(start: rawExports.data, count: Int(rawExports.size))
        func compareExportIndices(lhs: (OpaquePointer?, OpaquePointer?), rhs: (OpaquePointer?, OpaquePointer?)) -> Bool {
            let name1 = WasmCAPI.wasm_exporttype_name(lhs.0).pointee.string
            let name2 = WasmCAPI.wasm_exporttype_name(rhs.0).pointee.string
            return name1 < name2
        }
        let sortedExports: [OpaquePointer?] = zip(exportTypes, exports).sorted(by: compareExportIndices).map { $0.1 }
        for export in sortedExports {
            let kind = WasmCAPI.wasm_extern_kind(export)
            switch wasm_externkind_enum(rawValue: UInt32(kind)) {
            case WASM_EXTERN_FUNC:
                guard fn == nil else { continue }
                fn = WasmCAPI.wasm_extern_as_func(export)
            case WASM_EXTERN_MEMORY:
                guard memory == nil else { continue }
                memory = WasmCAPI.wasm_extern_as_memory(export)
            default:
                break
            }
        }

        guard let fn = fn else {
            throw ExecError("No functions found")
        }
        let type = WasmCAPI.wasm_func_type(fn)
        let paramTypes = WasmCAPI.wasm_functype_params(type)
        let resultTypes = WasmCAPI.wasm_functype_results(type)
        var arguments = WasmCAPI.wasm_val_vec_t()
        WasmCAPI.wasm_val_vec_new_uninitialized(&arguments, Int(paramTypes?.pointee.size ?? 0))
        defer { WasmCAPI.wasm_val_vec_delete(&arguments) }
        for i in 0..<Int(paramTypes?.pointee.size ?? 0) {
            let kind = wasm_valtype_kind(paramTypes?.pointee.data[i])
            var value = WasmCAPI.wasm_val_t()
            value.kind = kind
            switch wasm_valkind_enum(rawValue: UInt32(kind)) {
            case WASM_I32: value.of.i32 = 0
            case WASM_I64: value.of.i64 = 0
            case WASM_F32: value.of.f32 = 0
            case WASM_F64: value.of.f64 = 0
            case WASM_FUNCREF: value.of.ref = nil
            case WASM_EXTERNREF: value.of.ref = nil
            default:
                throw ExecError("Unsupported value type")
            }
            arguments.data[i] = value
        }
        var results = WasmCAPI.wasm_val_vec_t()
        WasmCAPI.wasm_val_vec_new_uninitialized(&results, Int(resultTypes?.pointee.size ?? 0))

        let trap = WasmCAPI.wasm_func_call(fn, &arguments, &results)
        let memoryData = memory.flatMap { memory in
            let size = WasmCAPI.wasm_memory_data_size(memory)
            let data = WasmCAPI.wasm_memory_data(memory)
            return data?.withMemoryRebound(to: UInt8.self, capacity: Int(size)) {
                Array(UnsafeBufferPointer(start: $0, count: Int(size)))
            }
        }
        if let trap = trap {
            var message = WasmCAPI.wasm_message_t()
            WasmCAPI.wasm_trap_message(trap, &message)
            return ExecResult(values: nil, trap: message.string, memory: memoryData)
        }

        let numberOfResults = Int(resultTypes?.pointee.size ?? 0)
        let values = try (0..<numberOfResults).map { (index) -> Value in
            let kind = wasm_valtype_kind(resultTypes?.pointee.data[index])
            let value = results.data[index]
            switch wasm_valkind_enum(rawValue: UInt32(kind)) {
            case WASM_I32: return .i32(UInt32(bitPattern: value.of.i32))
            case WASM_I64: return .i64(UInt64(bitPattern: value.of.i64))
            case WASM_F32: return .f32(value.of.f32.bitPattern)
            case WASM_F64: return .f64(value.of.f64.bitPattern)
            default:
                throw ExecError("Unsupported value type: \(kind)")
            }
        }
        return ExecResult(values: values, trap: nil, memory: memoryData)
    }
}

@main struct Main {
    static func main() {
        let shrinking = ProcessInfo.processInfo.environment["SHRINKING"] == "1"
        let ok = _main()
        if shrinking {
            // While shrinking, failure is "interesting" and reducer expects non-zero exit code
            // for interesting cases.
            exit(ok ? 1 : 0)
        }
        exit(ok ? 0 : 1)
    }
    static func _main() -> Bool {
        do {
            return try run(moduleFile: CommandLine.arguments[1])
        } catch {
            // Ignore errors
            return true
        }
    }

    static func run(moduleFile: String) throws -> Bool {
        let engines: [Engine] = [
            WasmKitEngine(),
            ReferenceEngine()
        ]
        let moduleBytes: [UInt8]
        if moduleFile.hasSuffix(".wat") {
            moduleBytes = try wat2wasm(String(contentsOf: URL(fileURLWithPath: moduleFile)))
        } else {
            moduleBytes = try Array(Data(contentsOf: URL(fileURLWithPath: moduleFile)))
        }
        let results = try engines.map { try ($0.run(moduleBytes: moduleBytes), $0.name) }
        guard results.count > 1 else {
            throw ExecError("Expected at least two engines")
        }
        return results.dropFirst().allSatisfy({ ExecResult.check(results[0], $0) })
    }
}