Skip to content
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

Fix carton dev crashing with SO sanitizer #239

Merged
merged 20 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
14 changes: 7 additions & 7 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [main]
pull_request:
branches: [main]
branches: "*"

jobs:
swift-test:
Expand Down Expand Up @@ -50,15 +50,15 @@ jobs:
curl https://get.wasmer.io -sSfL | sh
if: startsWith(matrix.os, 'ubuntu')

- name: Build and install JavaScript resources
- name: Build the project
run: swift build

- name: Build and install JavaScript and sanitizer resources
run: |
npm install
npm run build
rm -rf $HOME/.carton/static
swift run carton-release hash-archive
mkdir -p $HOME/.carton
cp -r static $HOME/.carton/static
- name: Build Project
run: swift build

- name: Run Tests
run: |
set -ex
Expand Down
5 changes: 5 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
"type": "shell",
"command": "swift build && cd Tests/Fixtures/TestApp && ../../../.build/debug/carton bundle --custom-index-page index.html"
},
{
"label": "test",
"type": "shell",
"command": "swift test"
},
{
"type": "npm",
"script": "build",
Expand Down
10 changes: 1 addition & 9 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,6 @@ let package = Package(
name: "Carton",
dependencies: [
"CartonCLI",
// commented out for now. Will remove once confirmed working
// .product(name: "ArgumentParser", package: "swift-argument-parser"),
// .product(name: "AsyncHTTPClient", package: "async-http-client"),
// .product(name: "Crypto", package: "swift-crypto"),
// .product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
// .product(name: "Vapor", package: "vapor"),
// "CartonHelpers",
// openCombineProduct,
// "SwiftToolchain",
]
),
.target(
Expand Down Expand Up @@ -95,6 +86,7 @@ let package = Package(
.product(name: "AsyncHTTPClient", package: "async-http-client"),
openCombineProduct,
"Splash",
"WasmTransformer",
]
),
// This target is used only for release automation tasks and
Expand Down
12 changes: 2 additions & 10 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let package = Package(
.package(url: "https://github.com/vapor/vapor.git", from: "4.29.3"),
.package(url: "https://github.com/apple/swift-crypto.git", from: "1.1.0"),
.package(url: "https://github.com/JohnSundell/Splash.git", from: "0.14.0"),
.package(url: "https://github.com/swiftwasm/WasmTransformer", .upToNextMinor(from: "0.0.1")),
.package(url: "https://github.com/swiftwasm/WasmTransformer", .upToNextMinor(from: "0.0.3")),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module
Expand All @@ -43,15 +43,6 @@ let package = Package(
name: "Carton",
dependencies: [
"CartonCLI",
// commented out for now. Will remove once confirmed working
// .product(name: "ArgumentParser", package: "swift-argument-parser"),
// .product(name: "AsyncHTTPClient", package: "async-http-client"),
// .product(name: "Crypto", package: "swift-crypto"),
// .product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
// .product(name: "Vapor", package: "vapor"),
// "CartonHelpers",
// openCombineProduct,
// "SwiftToolchain",
]
),
.target(
Expand Down Expand Up @@ -88,6 +79,7 @@ let package = Package(
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
"OpenCombine",
"Splash",
"WasmTransformer",
]
),
// This target is used only for release automation tasks and
Expand Down
14 changes: 7 additions & 7 deletions Sources/CartonCLI/Commands/Bundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ struct Bundle: ParsableCommand {
let toolchain = try Toolchain(localFileSystem, terminal)

let flavor = buildFlavor()
let (_, mainWasmPath, inferredProduct) = try toolchain.buildCurrentProject(
let build = try toolchain.buildCurrentProject(
product: product,
flavor: flavor
)
try terminal.logLookup(
"Right after building the main binary size is ",
localFileSystem.humanReadableFileSize(mainWasmPath),
localFileSystem.humanReadableFileSize(build.mainWasmPath),
newline: true
)

try strip(mainWasmPath)
try strip(build.mainWasmPath)

try terminal.logLookup(
"After stripping debug info the main binary size is ",
localFileSystem.humanReadableFileSize(mainWasmPath),
localFileSystem.humanReadableFileSize(build.mainWasmPath),
newline: true
)

Expand All @@ -81,7 +81,7 @@ struct Bundle: ParsableCommand {
try localFileSystem.createDirectory(bundleDirectory)
let optimizedPath = AbsolutePath(bundleDirectory, "main.wasm")
try ProcessRunner(
["wasm-opt", "-Os", mainWasmPath.pathString, "-o", optimizedPath.pathString],
["wasm-opt", "-Os", build.mainWasmPath.pathString, "-o", optimizedPath.pathString],
terminal
).waitUntilFinished()
try terminal.logLookup(
Expand All @@ -93,10 +93,10 @@ struct Bundle: ParsableCommand {
try copyToBundle(
terminal: terminal,
optimizedPath: optimizedPath,
buildDirectory: mainWasmPath.parentDirectory,
buildDirectory: build.mainWasmPath.parentDirectory,
bundleDirectory: bundleDirectory,
toolchain: toolchain,
product: inferredProduct
product: build.product
)

terminal.write("Bundle generation finished successfully\n", inColor: .green, bold: true)
Expand Down
12 changes: 6 additions & 6 deletions Sources/CartonCLI/Commands/Dev.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct Dev: ParsableCommand {
}

let flavor = buildFlavor()
let (arguments, mainWasmPath, inferredProduct) = try toolchain.buildCurrentProject(
let build = try toolchain.buildCurrentProject(
product: product,
flavor: flavor
)
Expand All @@ -114,24 +114,24 @@ struct Dev: ParsableCommand {
let sources = try paths.flatMap { try localFileSystem.traverseRecursively($0) }

try Server(
with: .init(
.init(
builder: Builder(
arguments: arguments,
mainWasmPath: mainWasmPath,
arguments: build.arguments,
mainWasmPath: build.mainWasmPath,
pathsToWatch: sources,
flavor,
localFileSystem,
terminal
),
mainWasmPath: mainWasmPath,
mainWasmPath: build.mainWasmPath,
verbose: verbose,
skipAutoOpen: skipAutoOpen,
port: port,
host: host,
customIndexContent: HTML.readCustomIndexPage(at: customIndexPage, on: localFileSystem),
// swiftlint:disable:next force_try
manifest: try! toolchain.manifest.get(),
product: inferredProduct,
product: build.product,
entrypoint: Self.entrypoint
),
terminal
Expand Down
2 changes: 1 addition & 1 deletion Sources/CartonCLI/Commands/Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct Test: ParsableCommand {
try runner.waitUntilFinished()
} else {
try Server(
with: .init(
.init(
builder: nil,
mainWasmPath: testBundlePath,
verbose: true,
Expand Down
2 changes: 1 addition & 1 deletion Sources/CartonKit/Model/Entrypoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import TSCUtility
instead of the forthcoming release, because the corresponding new release tag doesn't exist yet.
*/
private let staticArchiveURL =
"https://github.com/swiftwasm/carton/releases/download/0.8.2/static.zip"
"https://github.com/swiftwasm/carton/releases/download/0.9.1/static.zip"

private let verifyHash = Equality<ByteString, String> {
"""
Expand Down
2 changes: 1 addition & 1 deletion Sources/CartonKit/Server/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension Application {
let onWebSocketClose: (WebSocket) -> ()
}

func configure(with configuration: Configuration) {
func configure(_ configuration: Configuration) {
http.server.configuration.port = configuration.port
http.server.configuration.hostname = configuration.host

Expand Down
11 changes: 6 additions & 5 deletions Sources/CartonKit/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public final class Server {
}

public init(
with configuration: Configuration,
_ configuration: Configuration,
_ terminal: InteractiveWriter
) throws {
if let builder = configuration.builder {
Expand All @@ -133,7 +133,7 @@ public final class Server {
try LoggingSystem.bootstrap(from: &env)
app = Application(env)
app.configure(
with: .init(
.init(
port: configuration.port,
host: configuration.host,
mainWasmPath: configuration.mainWasmPath,
Expand All @@ -143,7 +143,7 @@ public final class Server {
entrypoint: configuration.entrypoint,
onWebSocketOpen: { [weak self] ws, environment in
if let handler = self?.createWSHandler(
with: configuration,
configuration,
in: environment,
terminal: terminal
) {
Expand Down Expand Up @@ -177,7 +177,7 @@ public final class Server {
.store(in: &subscriptions)
}

/// Blocking function that starts the HTTP server
/// Blocking function that starts the HTTP server.
public func run() throws {
defer { app.shutdown() }
try app.run()
Expand Down Expand Up @@ -205,8 +205,9 @@ public final class Server {
}

extension Server {
/// Returns a handler that responds to WebSocket messages coming from the browser.
func createWSHandler(
with configuration: Configuration,
_ configuration: Configuration,
in environment: DestinationEnvironment,
terminal: InteractiveWriter
) -> (WebSocket, String) -> () {
Expand Down
10 changes: 5 additions & 5 deletions Sources/CartonKit/Server/StaticArchive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import TSCBasic

public let devEntrypointSHA256 = ByteString([
0xB1, 0x75, 0x6E, 0xC8, 0x39, 0x45, 0xE2, 0x06, 0x00, 0xDD, 0x2E, 0x4D, 0x67, 0xD4, 0xD6, 0xCE,
0x35, 0x62, 0xA0, 0x3D, 0x9F, 0x63, 0x6E, 0x63, 0xAD, 0x79, 0xF2, 0x2D, 0x9D, 0x3C, 0x5C, 0x0A,
0x35, 0x62, 0xA0, 0x3D, 0x9F, 0x63, 0x6E, 0x63, 0xAD, 0x79, 0xF2, 0x2D, 0x9D, 0x3C, 0x5C, 0x0A
])

public let bundleEntrypointSHA256 = ByteString([
0x2B, 0xD2, 0xB5, 0x0C, 0x08, 0xFB, 0x5A, 0x8D, 0x55, 0xC4, 0x4B, 0x3A, 0x51, 0x63, 0x80, 0x1F,
0xB9, 0x15, 0x50, 0xD2, 0xB6, 0x12, 0xC3, 0xDE, 0x3A, 0x7D, 0xEB, 0xB0, 0x1F, 0x40, 0x06, 0x3F,
0xB9, 0x15, 0x50, 0xD2, 0xB6, 0x12, 0xC3, 0xDE, 0x3A, 0x7D, 0xEB, 0xB0, 0x1F, 0x40, 0x06, 0x3F
])

public let testEntrypointSHA256 = ByteString([
0xCE, 0x38, 0xCE, 0x55, 0x05, 0x45, 0x93, 0x75, 0xE6, 0xCD, 0xCE, 0x1C, 0xA6, 0x67, 0x14, 0x16,
0xBB, 0xAE, 0xF3, 0xFC, 0xFB, 0x86, 0xCB, 0x98, 0x1A, 0x66, 0x5C, 0xC7, 0x6C, 0x3B, 0x81, 0xF4,
0xBB, 0xAE, 0xF3, 0xFC, 0xFB, 0x86, 0xCB, 0x98, 0x1A, 0x66, 0x5C, 0xC7, 0x6C, 0x3B, 0x81, 0xF4
])

public let staticArchiveHash = ByteString([
0x4E, 0xC8, 0x39, 0xCA, 0xEE, 0x4F, 0x09, 0x8A, 0xA4, 0x95, 0x17, 0xD1, 0x13, 0xE1, 0xD9, 0x79,
0xB9, 0x05, 0xE2, 0xF7, 0x9A, 0x75, 0xA2, 0x8D, 0x20, 0xDC, 0x6F, 0xEB, 0xDC, 0x00, 0xCA, 0x80,
0x7A, 0x27, 0x1A, 0x64, 0x7A, 0xEB, 0xE1, 0x94, 0x24, 0xE6, 0x4A, 0x98, 0x14, 0x5F, 0xC3, 0xA7,
0xB5, 0x6B, 0x81, 0x51, 0x75, 0xA0, 0x8E, 0xF4, 0x8D, 0x48, 0xC9, 0x9A, 0x60, 0x25, 0x24, 0x2F,
])
22 changes: 22 additions & 0 deletions Sources/SwiftToolchain/BuildDescription.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021 Carton contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import PackageModel
import TSCBasic

public struct BuildDescription {
public let arguments: [String]
public let mainWasmPath: AbsolutePath
public let product: ProductDescription
}
29 changes: 23 additions & 6 deletions Sources/SwiftToolchain/Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public final class Builder {
self.fileSystem = fileSystem
}

public func run() -> AnyPublisher<String, Error> {
private func processPublisher(builderArguments: [String]) -> AnyPublisher<String, Error> {
let buildStarted = Date()
let process = ProcessRunner(
arguments,
builderArguments,
loadingMessage: "Compiling...",
parser: DiagnosticsParser(),
parser: nil,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @carson-katri I'm disabling this parser for now since it sometimes fails to parse project errors, but lets through the GTK warning. This causes confusing diagnostic messages as reported in #241. A better long-term solution would be to implement #181 instead, I hope to get to it at some point, unless someone else is willing to take this over.

terminal
)
currentProcess = process
Expand All @@ -69,8 +69,7 @@ public final class Builder {
String(format: "%.2f seconds", abs(buildStarted.timeIntervalSinceNow))
)

var transformers: [(inout InputByteStream, inout InMemoryOutputWriter) throws -> ()] = [
]
var transformers: [(inout InputByteStream, inout InMemoryOutputWriter) throws -> ()] = []
if self.flavor.environment != .other {
transformers.append(I64ImportTransformer().transform)
}
Expand All @@ -85,7 +84,7 @@ public final class Builder {
guard !transformers.isEmpty else { return }

// FIXME: errors from these `try` expressions should be recoverable, not sure how to
// do that in `handleEvents`, and `flatMap` doesnt' fit here as we need to track
// do that in `handleEvents`, and `flatMap` doesn't fit here as we need to track
// publisher completion.
// swiftlint:disable force_try
let binary = try! self.fileSystem.readFileContents(self.mainWasmPath)
Expand All @@ -110,6 +109,24 @@ public final class Builder {
.eraseToAnyPublisher()
}

public func run() -> AnyPublisher<String, Error> {
switch flavor.sanitize {
case .none:
return processPublisher(builderArguments: arguments)
case .stackOverflow:
let sanitizerFile =
fileSystem.homeDirectory.appending(components: ".carton", "static", "so_sanitizer.wasm")

var modifiedArguments = arguments
modifiedArguments.append(contentsOf: [
"-Xlinker", sanitizerFile.pathString,
// stack-overflow-sanitizer depends on "--stack-first"
"-Xlinker", "--stack-first",
])
return processPublisher(builderArguments: modifiedArguments)
}
}

public func runAndWaitUntilFinished() throws {
try tsc_await { completion in
subscription = run()
Expand Down
Loading