Skip to content

Commit

Permalink
Embed support object file generated from C instead of hand assembled …
Browse files Browse the repository at this point in the history
…code
  • Loading branch information
kateinoigakukun committed Apr 6, 2022
1 parent 1f63075 commit 1a5821f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
update-fixture: Sources/WasmTransformer/Transformers/StackOverflowSanitizer+Fixtures.swift

Fixtures/build/stack_sanitizer_support.o:
$(MAKE) -C Fixtures build/stack_sanitizer_support.o
Sources/WasmTransformer/Transformers/StackOverflowSanitizer+Fixtures.swift: Fixtures/build/stack_sanitizer_support.o Tools/GenerateStackOverflowSanitizerSupport.swift
swift ./Tools/GenerateStackOverflowSanitizerSupport.swift Fixtures/build/stack_sanitizer_support.o > Sources/WasmTransformer/Transformers/StackOverflowSanitizer+Fixtures.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// GENERATED FROM Tools/GenerateStackOverflowSanitizerSupport.swift

extension StackOverflowSanitizer {
public static let supportObjectFile: [UInt8] = [
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x84, 0x80, 0x80, 0x80, 0x00, 0x01, 0x60, 0x00, 0x00, 0x02, 0xc2, 0x80, 0x80, 0x80, 0x00, 0x02, 0x03, 0x65, 0x6e, 0x76, 0x0f, 0x5f, 0x5f, 0x6c, 0x69, 0x6e, 0x65, 0x61, 0x72, 0x5f, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x00, 0x11, 0x5f, 0x5f, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x5f, 0x73, 0x61, 0x6e, 0x69, 0x74, 0x69, 0x7a, 0x65, 0x72, 0x15, 0x72, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x5f, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x5f, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77, 0x00, 0x00, 0x03, 0x82, 0x80, 0x80, 0x80, 0x00, 0x01, 0x00, 0x0a, 0x8b, 0x80, 0x80, 0x80, 0x00, 0x01, 0x09, 0x00, 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, 0x0f, 0x0b, 0x00, 0xc6, 0x80, 0x80, 0x80, 0x00, 0x07, 0x6c, 0x69, 0x6e, 0x6b, 0x69, 0x6e, 0x67, 0x02, 0x08, 0xb7, 0x80, 0x80, 0x80, 0x00, 0x02, 0x00, 0x04, 0x01, 0x07, 0x5f, 0x5f, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x00, 0x50, 0x00, 0x27, 0x5f, 0x5f, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x5f, 0x73, 0x61, 0x6e, 0x69, 0x74, 0x69, 0x7a, 0x65, 0x72, 0x5f, 0x72, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x5f, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x5f, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77, 0x00, 0x90, 0x80, 0x80, 0x80, 0x00, 0x0a, 0x72, 0x65, 0x6c, 0x6f, 0x63, 0x2e, 0x43, 0x4f, 0x44, 0x45, 0x03, 0x01, 0x00, 0x04, 0x01, 0x00, 0x89, 0x81, 0x80, 0x80, 0x00, 0x09, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x73, 0x01, 0x0c, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x65, 0x64, 0x2d, 0x62, 0x79, 0x01, 0x0f, 0x53, 0x77, 0x69, 0x66, 0x74, 0x57, 0x61, 0x73, 0x6d, 0x20, 0x63, 0x6c, 0x61, 0x6e, 0x67, 0x5f, 0x31, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x20, 0x28, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x77, 0x69, 0x66, 0x74, 0x77, 0x61, 0x73, 0x6d, 0x2f, 0x6c, 0x6c, 0x76, 0x6d, 0x2d, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x2e, 0x67, 0x69, 0x74, 0x20, 0x31, 0x36, 0x35, 0x64, 0x38, 0x61, 0x62, 0x32, 0x64, 0x31, 0x39, 0x62, 0x65, 0x63, 0x63, 0x30, 0x64, 0x39, 0x61, 0x39, 0x63, 0x62, 0x33, 0x31, 0x39, 0x32, 0x39, 0x65, 0x30, 0x39, 0x36, 0x63, 0x38, 0x30, 0x36, 0x37, 0x34, 0x36, 0x65, 0x38, 0x29
]
}
38 changes: 0 additions & 38 deletions Sources/WasmTransformer/Transformers/StackOverflowSanitizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,6 @@ public struct StackOverflowSanitizer {

public init() {}

/// This hardcoded byte array is produced from the following wat file with hand-assembled "linking" section.
/// The dummy function is defined to avoid unused-import elimination.
/// This object file is required to embed the import entry and the entry will be used while assertion.
/// ```
/// (module
/// (type (;0;) (func (param i32) (result i32)))
/// (type (;1;) (func))
/// (import "__stack_sanitizer" "report_stack_overflow" (func (;0;) (type 0)))
/// (func (;1;) (type 1)
/// i32.const 0
/// call 0
/// return))
/// ```
public static let supportObjectFile: [UInt8] = [
// magic
0x00, 0x61, 0x73, 0x6d,
// version
0x01, 0x00, 0x00, 0x00,
// type section
0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
// import section
0x02, 0x2b, 0x01, 0x11, 0x5f, 0x5f, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x5f, 0x73, 0x61, 0x6e, 0x69,
0x74, 0x69, 0x7a, 0x65, 0x72, 0x15, 0x72, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x5f, 0x73, 0x74, 0x61,
0x63, 0x6b, 0x5f, 0x6f, 0x76, 0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77, 0x00, 0x00,
// function section
0x03, 0x02, 0x01, 0x00,
// code section
0x0a, 0x09, 0x01, 0x07, 0x00, 0x41, 0x00, 0x10, 0x00, 0x0f, 0x0b,
// Custom Section "linking"
0x00, 0x37, 0x07, 0x6c, 0x69, 0x6e, 0x6b, 0x69, 0x6e, 0x67, 0x02,
// symbol table
0x08, 0x2c, 0x01, 0x00, 0x50, 0x00,
0x27, 0x5f, 0x5f, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x5f, 0x73, 0x61, 0x6e, 0x69, 0x74,
0x69, 0x7a, 0x65, 0x72,
0x5f, 0x72, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x5f, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x5f, 0x6f, 0x76,
0x65, 0x72, 0x66, 0x6c, 0x6f, 0x77,
]

public func transform<Writer: OutputWriter>(_ input: inout InputByteStream, writer: inout Writer) throws {
let version = try input.readHeader()
try writer.writeBytes(magic)
Expand Down
20 changes: 20 additions & 0 deletions Tools/GenerateStackOverflowSanitizerSupport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

guard CommandLine.arguments.count == 2 else {
print("Usage: \(CommandLine.arguments[0]) <support.o>")
exit(1)
}

let objectFile = try Data(contentsOf: URL(fileURLWithPath: CommandLine.arguments[1]))

let sourceCode = """
// GENERATED FROM Tools/GenerateStackOverflowSanitizerSupport.swift
extension StackOverflowSanitizer {
public static let supportObjectFile: [UInt8] = [
\(objectFile.map { String(format: "0x%02x", $0) }.joined(separator: ", "))
]
}
"""

print(sourceCode)

0 comments on commit 1a5821f

Please sign in to comment.