Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
1863d35
[WIP] - Start crypto worker and initialize channel.
AaronRobinsonMSFT Mar 10, 2021
d5c4199
Make Emscripten minimizer happy by using getters instead of fields.
AaronRobinsonMSFT Mar 10, 2021
4e2d738
Add new System.Security.Cryptography.Native.Browser library.
AaronRobinsonMSFT Mar 11, 2021
5ec3db7
Initialize WebWorker earlier.
AaronRobinsonMSFT Mar 11, 2021
c7e3888
Update logging and be less strict for output buffer size.
AaronRobinsonMSFT Mar 11, 2021
47b5e6c
Focus SHA tests for non-browser and browser DOM scenarios.
AaronRobinsonMSFT Mar 11, 2021
891b23c
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT Mar 11, 2021
d4ea7d6
Add missing namespaces to test builds.
AaronRobinsonMSFT Mar 12, 2021
8062a2f
Additional locations for that need the crypto_worker.js file.
AaronRobinsonMSFT Mar 12, 2021
2b38fdd
Add new JS and native binaries... everywhere.
AaronRobinsonMSFT Mar 12, 2021
84dd60b
Reference the correct location for the webworker file.
AaronRobinsonMSFT Mar 13, 2021
36b2f56
Specify the correct native directory path for the JS WebWorker file.
AaronRobinsonMSFT Mar 13, 2021
80087a5
Only enable Crypto WebWorker when running in the browser scenario.
AaronRobinsonMSFT Mar 15, 2021
6a3b8a6
Condition more tests based on platform Crypto support needed.
AaronRobinsonMSFT Mar 15, 2021
d78f0e2
Rename crypto_worker.js to dotnet_crypto_worker.js
AaronRobinsonMSFT Mar 15, 2021
2e74b1f
Missing installer directory.
AaronRobinsonMSFT Mar 15, 2021
7eb087d
Style
AaronRobinsonMSFT Mar 16, 2021
b04d0cc
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT Mar 16, 2021
925dcc0
Change internal API name to use generic "digest" name instead
AaronRobinsonMSFT Mar 16, 2021
753c57b
Add test for not supported platform crypto WRT SHA
AaronRobinsonMSFT Mar 17, 2021
74a4e7e
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT Mar 17, 2021
b217fc4
Browser uses Unix binary builds so removing Windows placeholder.
AaronRobinsonMSFT Mar 17, 2021
a95e9d6
Add libSystem.Security.Cryptography.Native.Browser.a for Windows build.
AaronRobinsonMSFT Mar 18, 2021
ce6d2b1
More Windows build changes.
AaronRobinsonMSFT Mar 18, 2021
42eac88
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT Mar 23, 2021
55dfe74
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT Mar 24, 2021
f777cb3
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT Mar 25, 2021
c13a76e
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT Mar 25, 2021
f2e28fc
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT Mar 25, 2021
c749ab5
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT Mar 29, 2021
4c5a3e0
Merge remote-tracking branch 'upstream/main' into wasm_crypto
lewing Apr 22, 2021
44ea919
Merge branch 'main' into wasm_crypto
lewing Apr 22, 2021
742cbc3
Fix the js inclusion logic
lewing Apr 22, 2021
76dd72b
Add libSystem.Globalization.Native explicitly
lewing Apr 23, 2021
bc7ec42
Merge branch 'main' into wasm_crypto
jeffhandley May 11, 2021
3eb7b2f
Merge branch 'main' into wasm_crypto
jeffhandley May 11, 2021
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
Next Next commit
[WIP] - Start crypto worker and initialize channel.
  • Loading branch information
AaronRobinsonMSFT committed Mar 10, 2021
commit 1863d351e6f5277882ae9e86ce6274519f969c6f
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/wasm.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ run-browser:
echo "The tool dotnet-serve could not be found. Install with: $(DOTNET) tool install --global dotnet-serve"; \
exit 1; \
else \
$(DOTNET) serve -d bin/$(CONFIG)/AppBundle -p 8000; \
$(DOTNET) serve -d:bin/$(CONFIG)/AppBundle -p 8000; \
fi

run-console:
Expand Down
9 changes: 6 additions & 3 deletions src/mono/wasm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ $(NATIVE_BIN_DIR)/include/wasm:
$(BUILDS_OBJ_DIR):
mkdir -p $$@

$(NATIVE_BIN_DIR)/dotnet.js: $(BUILDS_OBJ_DIR)/driver.o $(BUILDS_OBJ_DIR)/pinvoke.o $(BUILDS_OBJ_DIR)/corebindings.o runtime/library_mono.js runtime/binding_support.js runtime/dotnet_support.js $(SYSTEM_NATIVE_LIBDIR)/pal_random.js $(2) | $(NATIVE_BIN_DIR)
$(EMCC) $(EMCC_FLAGS) $(1) --js-library runtime/library_mono.js --js-library runtime/binding_support.js --js-library runtime/dotnet_support.js --js-library $(SYSTEM_NATIVE_LIBDIR)/pal_random.js $(BUILDS_OBJ_DIR)/driver.o $(BUILDS_OBJ_DIR)/pinvoke.o $(BUILDS_OBJ_DIR)/corebindings.o $(2) -o $(NATIVE_BIN_DIR)/dotnet.js $(3)
$(NATIVE_BIN_DIR)/dotnet.js: $(BUILDS_OBJ_DIR)/driver.o $(BUILDS_OBJ_DIR)/pinvoke.o $(BUILDS_OBJ_DIR)/corebindings.o runtime/library_mono.js runtime/binding_support.js runtime/dotnet_support.js runtime/library_channel.js $(SYSTEM_NATIVE_LIBDIR)/pal_random.js $(2) | $(NATIVE_BIN_DIR)
$(EMCC) $(EMCC_FLAGS) $(1) --js-library runtime/library_mono.js --js-library runtime/binding_support.js --js-library runtime/dotnet_support.js --post-js runtime/library_channel.js --js-library $(SYSTEM_NATIVE_LIBDIR)/pal_random.js $(BUILDS_OBJ_DIR)/driver.o $(BUILDS_OBJ_DIR)/pinvoke.o $(BUILDS_OBJ_DIR)/corebindings.o $(2) -o $(NATIVE_BIN_DIR)/dotnet.js $(3)

$(BUILDS_OBJ_DIR)/pinvoke-table.h: $(PINVOKE_TABLE) | $(BUILDS_OBJ_DIR)
if cmp -s $(PINVOKE_TABLE) $$@ ; then : ; else cp $(PINVOKE_TABLE) $$@ ; fi
Expand All @@ -116,13 +116,16 @@ $(BUILDS_OBJ_DIR)/pinvoke.o: runtime/pinvoke.c runtime/pinvoke.h $(BUILDS_OBJ_DI
$(BUILDS_OBJ_DIR)/corebindings.o: runtime/corebindings.c | $(BUILDS_OBJ_DIR)
$(EMCC) $(EMCC_FLAGS) $(1) -Oz -I$(MONO_INCLUDE_DIR) runtime/corebindings.c -c -o $$@

$(NATIVE_BIN_DIR)/crypto_worker.js: runtime/crypto_worker.js | $(NATIVE_BIN_DIR)
cp runtime/crypto_worker.js $(NATIVE_BIN_DIR)/crypto_worker.js

$(NATIVE_BIN_DIR)/src/emcc-flags.txt: | $(NATIVE_BIN_DIR)/src Makefile
echo "$(call escape_quote,$(EMCC_FLAGS)) $(1) -Oz" > $$@

$(NATIVE_BIN_DIR)/src/emcc-version.txt: $(EMSDK_PATH)/upstream/.emsdk_version | $(NATIVE_BIN_DIR)/src
$(EMCC) --version | head -1 > $$@

build-native: $(NATIVE_BIN_DIR)/dotnet.js $(NATIVE_BIN_DIR)/src/emcc-flags.txt $(NATIVE_BIN_DIR)/src/emcc-version.txt
build-native: $(NATIVE_BIN_DIR)/dotnet.js $(NATIVE_BIN_DIR)/crypto_worker.js $(NATIVE_BIN_DIR)/src/emcc-flags.txt $(NATIVE_BIN_DIR)/src/emcc-version.txt

endef

Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/build/WasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,14 @@

<_HasDotnetWasm Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.wasm'">true</_HasDotnetWasm>
<_HasDotnetJs Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.js'">true</_HasDotnetJs>
<_HasCryptoWorkerJs Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'crypto_worker.js'">true</_HasCryptoWorkerJs>
</PropertyGroup>

<ItemGroup>
<!-- If dotnet.{wasm,js} weren't added already (eg. AOT can add them), then add the default ones -->
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidDir)native\dotnet.wasm" Condition="'$(_HasDotnetWasm)' != 'true'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidDir)native\dotnet.js" Condition="'$(_HasDotnetJs)' != 'true'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidDir)native\crypto_worker.js" Condition="'$(_HasCryptoWorkerJs)' != 'true'" />

<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidDir)native\$(WasmIcuDataFileName)" Condition="'$(InvariantGlobalization)' != 'true'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidDir)native\dotnet.timezones.blat" />
Expand Down
147 changes: 147 additions & 0 deletions src/mono/wasm/runtime/crypto_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

var ChannelWorker = {
_impl: class {
// BEGIN ChannelOwner contract - shared constants.
STATE_IDX = 0;
MSG_SIZE_IDX = 1;

STATE_IDLE = 0;
STATE_REQ = 1;
STATE_RESP = 2;
STATE_REQ_P = 3; // Request has multiple parts
STATE_RESP_P = 4; // Response has multiple parts
STATE_AWAIT = 5; // Awaiting the next part
// END ChannelOwner contract - shared constants.

constructor(comm_buf, msg_buf, msg_char_len) {
this.comm = new Int32Array(comm_buf);
this.msg = new Uint16Array(msg_buf);
this.msg_char_len = msg_char_len;
}

async await_request(async_call) {
console.log("await_request()");

for (;;) {
// Wait for signal to perform operation
Atomics.wait(this.comm, this.STATE_IDX, this.STATE_IDLE);

// Read in request
var req = this._read_request();
console.log("Request: " + req);

// Perform async action based on request
var resp = await async_call(req);

// Send response
this._send_response(resp);
}
}

_read_request() {
var request = "";
for (;;) {
// Get the current state and message size
var state = Atomics.load(this.comm, this.STATE_IDX);
var size_to_read = Atomics.load(this.comm, this.MSG_SIZE_IDX);

// Append the latest part of the message.
request += this._read_from_msg(0, size_to_read);

// The request is complete.
if (state === this.STATE_REQ)
break;

// Reset the size and transition to await state.
Atomics.store(this.comm, this.MSG_SIZE_IDX, 0);
Atomics.store(this.comm, this.STATE_IDX, this.STATE_AWAIT);
Atomics.wait(this.comm, this.STATE_IDX, this.STATE_AWAIT);
}

return request;
}

_read_from_msg(begin, end) {
return String.fromCharCode.apply(null, this.msg.slice(begin, end));
}

_send_response(msg) {
if (Atomics.load(this.comm, this.STATE_IDX) !== this.STATE_REQ)
throw "WORKER: Invalid sync communication channel state.";

var state; // State machine variable
const msg_len = msg.length;
var msg_written = 0;

for (;;) {
// Write the message and return how much was written.
var wrote = this._write_to_msg(msg, msg_written, msg_len);
msg_written += wrote;

// Indicate how much was written to the this.msg buffer.
Atomics.store(this.comm, this.MSG_SIZE_IDX, wrote);

// Indicate if this was the whole message or part of it.
state = msg_written === msg_len ? this.STATE_RESP : this.STATE_RESP_P;

// Update the state
Atomics.store(this.comm, this.STATE_IDX, state);

// Wait for the transition to know the main thread has
// received the response by moving onto a new state.
Atomics.wait(this.comm, this.STATE_IDX, state);

// Done sending response.
if (state === this.STATE_RESP)
break;
}
}

_write_to_msg(input, start, input_len) {
var mi = 0;
var ii = start;
while (mi < this.msg_char_len && ii < input_len) {
this.msg[mi] = input.charCodeAt(ii);
ii++; // Next character
mi++; // Next buffer index
}
return ii - start;
}
},

create: function (comm_buf, msg_buf, msg_char_len) {
return new this._impl(comm_buf, msg_buf, msg_char_len);
}
};

//
// [TODO] Handle crypto calls that uses Promises below.
//

// Operation to perform.
async function async_call(msg) {
var keyPair = await self.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
},
true,
["encrypt", "decrypt"]
);

return msg.split("").reverse().join("");
}

var s_channel;

// Initialize WebWorker
onmessage = function (p) {
console.log(p.data.salutation);
s_channel = ChannelWorker.create(p.data.comm_buf, p.data.msg_buf, p.data.msg_char_len);

s_channel.await_request(async_call);
}
136 changes: 136 additions & 0 deletions src/mono/wasm/runtime/library_channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

Module [ 'channel' ] = {
_impl: class {
// Index constants for the communication buffer.
STATE_IDX = 0;
MSG_SIZE_IDX = 1;
COMM_LAST_IDX = this.MSG_SIZE_IDX;

// Communication states.
STATE_IDLE = 0;
STATE_REQ = 1;
STATE_RESP = 2;
STATE_REQ_P = 3; // Request has multiple parts
STATE_RESP_P = 4; // Response has multiple parts
STATE_AWAIT = 5; // Awaiting the next part

constructor(msg_char_len) {
this.msg_char_len = msg_char_len;

const int_bytes = 4;
const comm_byte_len = int_bytes * (this.COMM_LAST_IDX + 1);
this.comm_buf = new SharedArrayBuffer(comm_byte_len);

// JavaScript character encoding is UTF-16.
const char_bytes = 2;
const msg_byte_len = char_bytes * this.msg_char_len;
this.msg_buf = new SharedArrayBuffer(msg_byte_len);

// Create the local arrays to use.
this.comm = new Int32Array(this.comm_buf);
this.msg = new Uint16Array(this.msg_buf);
}

get_msg_len() { return this.msg_char_len; }
get_msg_buffer() { return this.msg_buf; }
get_comm_buffer() { return this.comm_buf; }

send_msg(msg) {
if (Atomics.load(this.comm, this.STATE_IDX) !== this.STATE_IDLE) {
throw "OWNER: Invalid sync communication channel state.";
}
this._send_request(msg);
return this._read_response();
}

_send_request(msg) {
var state;
const msg_len = msg.length;
var msg_written = 0;

for (;;) {
// Write the message and return how much was written.
var wrote = this._write_to_msg(msg, msg_written, msg_len);
msg_written += wrote;

// Indicate how much was written to the this.msg buffer.
Atomics.store(this.comm, this.MSG_SIZE_IDX, wrote);

// Indicate if this was the whole message or part of it.
state = msg_written === msg_len ? this.STATE_REQ : this.STATE_REQ_P;

// Notify webworker
Atomics.store(this.comm, this.STATE_IDX, state);
Atomics.notify(this.comm, this.STATE_IDX);

// The send message is complete.
if (state === this.STATE_REQ)
break;

// Wait for the worker to be ready for the next part.
// - Atomics.wait() is not permissible on the main thread.
do {
state = Atomics.load(this.comm, this.STATE_IDX);
} while (state !== this.STATE_AWAIT);
}
}

_write_to_msg(input, start, input_len) {
var mi = 0;
var ii = start;
while (mi < this.msg_char_len && ii < input_len) {
this.msg[mi] = input.charCodeAt(ii);
ii++; // Next character
mi++; // Next buffer index
}
return ii - start;
}

_read_response() {
var state;
var response = "";
for (;;) {
// Wait for webworker response.
// - Atomics.wait() is not permissible on the main thread.
do {
state = Atomics.load(this.comm, this.STATE_IDX);
} while (state !== this.STATE_RESP && state !== this.STATE_RESP_P);

var size_to_read = Atomics.load(this.comm, this.MSG_SIZE_IDX);

// Append the latest part of the message.
response += this._read_from_msg(0, size_to_read);

// The response is complete.
if (state === this.STATE_RESP)
break;

// Reset the size and transition to await state.
Atomics.store(this.comm, this.MSG_SIZE_IDX, 0);
Atomics.store(this.comm, this.STATE_IDX, this.STATE_AWAIT);
Atomics.notify(this.comm, this.STATE_IDX);
}

// Reset the communication channel's state and let the
// webworker know we are done.
Atomics.store(this.comm, this.STATE_IDX, this.STATE_IDLE);
Atomics.notify(this.comm, this.STATE_IDX);

return response;
}

_read_from_msg(begin, end) {
return String.fromCharCode.apply(null, this.msg.slice(begin, end));
}

},

create: function (msg_char_len) {
if (msg_char_len === undefined) {
msg_char_len = 1024; // Default size is arbitrary but is in 'char' units (i.e. UTF-16 code points).
}
return new this._impl(msg_char_len);
}
};
21 changes: 21 additions & 0 deletions src/mono/wasm/runtime/library_mono.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ var MonoSupportLib = {
_vt_stack: [],
mono_wasm_runtime_is_ready : false,
mono_wasm_ignore_pdb_load_errors: true,
mono_wasm_crypto: {
channel: null,
worker: null
},

/** @type {object.<string, object>} */
_id_table: {},
Expand Down Expand Up @@ -1699,6 +1703,19 @@ var MonoSupportLib = {
return Module.ccall ('mono_wasm_get_icudt_name', 'string', ['string'], [culture]);
},

_initialize_workers: function () {
var chan = Module.channel.create();
var worker = new Worker ("crypto_worker.js");
worker.postMessage ({
salutation:"Message from main",
comm_buf: chan.get_comm_buffer(),
msg_buf: chan.get_msg_buffer(),
msg_char_len: chan.get_msg_len()
});
MONO.mono_wasm_crypto.channel = chan;
MONO.mono_wasm_crypto.worker = worker;
},

_finalize_startup: function (args, ctx) {
var loaded_files_with_debug_info = [];

Expand Down Expand Up @@ -1736,6 +1753,10 @@ var MonoSupportLib = {
tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
} catch {}
MONO.mono_wasm_setenv ("TZ", tz || "UTC");

// Now that the runtime has been loaded, spin off any web workers needed
// for ancillary operations.
MONO._initialize_workers ();
MONO.mono_wasm_runtime_ready ();
args.loaded_cb ();
},
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/wasm.proj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Copy SourceFiles="$(NativeBinDir)dotnet.js;
$(NativeBinDir)dotnet.wasm;
$(NativeBinDir)dotnet.timezones.blat;
$(NativeBinDir)crypto_worker.js;
$(NativeBinDir)libicuuc.a;
$(NativeBinDir)libicui18n.a"
DestinationFolder="$(MicrosoftNetCoreAppRuntimePackNativeDir)"
Expand Down