WebAssembly support!

Quite a lot going on in this PR:
 * Updates to support recent emscripten, switching to WASM which is now the default
 * Use emscripten's MODULARIZE option rather than wrapping it ourself, since doing
   so in pre-post js doesn't work anymore.
 * Most changes are moving the emscripten runtime functions to top-level
   calls rather than in the Module object.
 * Get rid of duplicated NULL_BYTE_PADDING_LENGTH
 * Fix ciphertext_length used without being declared
 * Fix things that caused the closure compiler to error, eg. using
   OLM_OPTIONS without a declaration.
 * Wait until module is inited to do OLM_ERROR = olm_error()

The main BREAKING CHANGE here is that the module now needs to initialise
asyncronously (because it has to load the wasm file). require()ing olm
now gives a function which needs to be called to create an instance.
The resulting object has a promise-like then() method that can be used
to detect when the module is ready. (We could use MODULARIZE_INSTANCE
to return the module directly as before, rather than the function,
but then we don't get the .then() method).
This commit is contained in:
David Baker 2018-09-21 16:01:51 +01:00
parent ed02c217e6
commit 122867c45c
10 changed files with 84 additions and 76 deletions

View file

@ -20,6 +20,7 @@ DEBUG_TARGET := $(BUILD_DIR)/libolm_debug.so.$(VERSION)
JS_TARGET := javascript/olm.js JS_TARGET := javascript/olm.js
JS_EXPORTED_FUNCTIONS := javascript/exported_functions.json JS_EXPORTED_FUNCTIONS := javascript/exported_functions.json
JS_EXTRA_EXPORTED_RUNTIME_METHODS := ALLOC_STACK
PUBLIC_HEADERS := include/olm/olm.h include/olm/outbound_group_session.h include/olm/inbound_group_session.h include/olm/pk.h PUBLIC_HEADERS := include/olm/olm.h include/olm/outbound_group_session.h include/olm/inbound_group_session.h include/olm/pk.h
@ -60,7 +61,7 @@ CFLAGS += -Wall -Werror -std=c99 -fPIC
CXXFLAGS += -Wall -Werror -std=c++11 -fPIC CXXFLAGS += -Wall -Werror -std=c++11 -fPIC
LDFLAGS += -Wall -Werror LDFLAGS += -Wall -Werror
EMCCFLAGS = --closure 1 --memory-init-file 0 -s NO_FILESYSTEM=1 -s INVOKE_RUN=0 EMCCFLAGS = --closure 1 --memory-init-file 0 -s NO_FILESYSTEM=1 -s INVOKE_RUN=0 -s MODULARIZE=1
# NO_BROWSER is kept for compatibility with emscripten 1.35.24, but is no # NO_BROWSER is kept for compatibility with emscripten 1.35.24, but is no
# longer needed. # longer needed.
EMCCFLAGS += -s NO_BROWSER=1 EMCCFLAGS += -s NO_BROWSER=1
@ -150,6 +151,7 @@ $(JS_TARGET): $(JS_OBJECTS) $(JS_PRE) $(JS_POST) $(JS_EXPORTED_FUNCTIONS)
$(foreach f,$(JS_PRE),--pre-js $(f)) \ $(foreach f,$(JS_PRE),--pre-js $(f)) \
$(foreach f,$(JS_POST),--post-js $(f)) \ $(foreach f,$(JS_POST),--post-js $(f)) \
-s "EXPORTED_FUNCTIONS=@$(JS_EXPORTED_FUNCTIONS)" \ -s "EXPORTED_FUNCTIONS=@$(JS_EXPORTED_FUNCTIONS)" \
-s "EXTRA_EXPORTED_RUNTIME_METHODS=$(JS_EXTRA_EXPORTED_RUNTIME_METHODS)" \
$(JS_OBJECTS) -o $@ $(JS_OBJECTS) -o $@
build_tests: $(TEST_BINARIES) build_tests: $(TEST_BINARIES)

View file

@ -2,4 +2,5 @@
/node_modules /node_modules
/npm-debug.log /npm-debug.log
/olm.js /olm.js
/olm.wasm
/reports /reports

View file

@ -1,9 +1,3 @@
/* The 'length' argument to Pointer_stringify doesn't work if the input includes
* characters >= 128; we therefore need to add a NULL character to all of our
* strings. This acts as a symbolic constant to help show what we're doing.
*/
var NULL_BYTE_PADDING_LENGTH = 1;
function InboundGroupSession() { function InboundGroupSession() {
var size = Module['_olm_inbound_group_session_size'](); var size = Module['_olm_inbound_group_session_size']();
this.buf = malloc(size); this.buf = malloc(size);
@ -77,14 +71,14 @@ InboundGroupSession.prototype['decrypt'] = restore_stack(function(
try { try {
message_buffer = malloc(message.length); message_buffer = malloc(message.length);
Module['writeAsciiToMemory'](message, message_buffer, true); writeAsciiToMemory(message, message_buffer, true);
var max_plaintext_length = inbound_group_session_method( var max_plaintext_length = inbound_group_session_method(
Module['_olm_group_decrypt_max_plaintext_length'] Module['_olm_group_decrypt_max_plaintext_length']
)(this.ptr, message_buffer, message.length); )(this.ptr, message_buffer, message.length);
// caculating the length destroys the input buffer, so we need to re-copy it. // caculating the length destroys the input buffer, so we need to re-copy it.
Module['writeAsciiToMemory'](message, message_buffer, true); writeAsciiToMemory(message, message_buffer, true);
plaintext_buffer = malloc(max_plaintext_length + NULL_BYTE_PADDING_LENGTH); plaintext_buffer = malloc(max_plaintext_length + NULL_BYTE_PADDING_LENGTH);
var message_index = stack(4); var message_index = stack(4);
@ -100,14 +94,14 @@ InboundGroupSession.prototype['decrypt'] = restore_stack(function(
// UTF8ToString requires a null-terminated argument, so add the // UTF8ToString requires a null-terminated argument, so add the
// null terminator. // null terminator.
Module['setValue']( setValue(
plaintext_buffer+plaintext_length, plaintext_buffer+plaintext_length,
0, "i8" 0, "i8"
); );
return { return {
"plaintext": UTF8ToString(plaintext_buffer), "plaintext": UTF8ToString(plaintext_buffer),
"message_index": Module['getValue'](message_index, "i32") "message_index": getValue(message_index, "i32")
} }
} finally { } finally {
if (message_buffer !== undefined) { if (message_buffer !== undefined) {

View file

@ -1,10 +1,3 @@
/* The 'length' argument to Pointer_stringify doesn't work if the input includes
* characters >= 128; we therefore need to add a NULL character to all of our
* strings. This acts as a symbolic constant to help show what we're doing.
*/
var NULL_BYTE_PADDING_LENGTH = 1;
function OutboundGroupSession() { function OutboundGroupSession() {
var size = Module['_olm_outbound_group_session_size'](); var size = Module['_olm_outbound_group_session_size']();
this.buf = malloc(size); this.buf = malloc(size);
@ -66,7 +59,7 @@ OutboundGroupSession.prototype['create'] = restore_stack(function() {
OutboundGroupSession.prototype['encrypt'] = function(plaintext) { OutboundGroupSession.prototype['encrypt'] = function(plaintext) {
var plaintext_buffer, message_buffer, plaintext_length; var plaintext_buffer, message_buffer, plaintext_length;
try { try {
plaintext_length = Module['lengthBytesUTF8'](plaintext); plaintext_length = lengthBytesUTF8(plaintext);
var message_length = outbound_group_session_method( var message_length = outbound_group_session_method(
Module['_olm_group_encrypt_message_length'] Module['_olm_group_encrypt_message_length']
@ -75,7 +68,7 @@ OutboundGroupSession.prototype['encrypt'] = function(plaintext) {
// need to allow space for the terminator (which stringToUTF8 always // need to allow space for the terminator (which stringToUTF8 always
// writes), hence + 1. // writes), hence + 1.
plaintext_buffer = malloc(plaintext_length + 1); plaintext_buffer = malloc(plaintext_length + 1);
Module['stringToUTF8'](plaintext, plaintext_buffer, plaintext_length + 1); stringToUTF8(plaintext, plaintext_buffer, plaintext_length + 1);
message_buffer = malloc(message_length + NULL_BYTE_PADDING_LENGTH); message_buffer = malloc(message_length + NULL_BYTE_PADDING_LENGTH);
outbound_group_session_method(Module['_olm_group_encrypt'])( outbound_group_session_method(Module['_olm_group_encrypt'])(
@ -86,12 +79,12 @@ OutboundGroupSession.prototype['encrypt'] = function(plaintext) {
// UTF8ToString requires a null-terminated argument, so add the // UTF8ToString requires a null-terminated argument, so add the
// null terminator. // null terminator.
Module['setValue']( setValue(
message_buffer+message_length, message_buffer+message_length,
0, "i8" 0, "i8"
); );
return Module['UTF8ToString'](message_buffer); return UTF8ToString(message_buffer);
} finally { } finally {
if (plaintext_buffer !== undefined) { if (plaintext_buffer !== undefined) {
// don't leave a copy of the plaintext in the heap. // don't leave a copy of the plaintext in the heap.

View file

@ -35,9 +35,9 @@ PkEncryption.prototype['encrypt'] = restore_stack(function(
) { ) {
var plaintext_buffer, ciphertext_buffer, plaintext_length; var plaintext_buffer, ciphertext_buffer, plaintext_length;
try { try {
plaintext_length = Module['lengthBytesUTF8'](plaintext) plaintext_length = lengthBytesUTF8(plaintext)
plaintext_buffer = malloc(plaintext_length + 1); plaintext_buffer = malloc(plaintext_length + 1);
Module['stringToUTF8'](plaintext, plaintext_buffer, plaintext_length + 1); stringToUTF8(plaintext, plaintext_buffer, plaintext_length + 1);
var random_length = pk_encryption_method( var random_length = pk_encryption_method(
Module['_olm_pk_encrypt_random_length'] Module['_olm_pk_encrypt_random_length']
)(); )();
@ -50,7 +50,7 @@ PkEncryption.prototype['encrypt'] = restore_stack(function(
Module['_olm_pk_mac_length'] Module['_olm_pk_mac_length']
)(this.ptr); )(this.ptr);
var mac_buffer = stack(mac_length + NULL_BYTE_PADDING_LENGTH); var mac_buffer = stack(mac_length + NULL_BYTE_PADDING_LENGTH);
Module['setValue']( setValue(
mac_buffer+mac_length, mac_buffer+mac_length,
0, "i8" 0, "i8"
); );
@ -58,7 +58,7 @@ PkEncryption.prototype['encrypt'] = restore_stack(function(
Module['_olm_pk_key_length'] Module['_olm_pk_key_length']
)(); )();
var ephemeral_buffer = stack(ephemeral_length + NULL_BYTE_PADDING_LENGTH); var ephemeral_buffer = stack(ephemeral_length + NULL_BYTE_PADDING_LENGTH);
Module['setValue']( setValue(
ephemeral_buffer+ephemeral_length, ephemeral_buffer+ephemeral_length,
0, "i8" 0, "i8"
); );
@ -72,12 +72,12 @@ PkEncryption.prototype['encrypt'] = restore_stack(function(
); );
// UTF8ToString requires a null-terminated argument, so add the // UTF8ToString requires a null-terminated argument, so add the
// null terminator. // null terminator.
Module['setValue']( setValue(
ciphertext_buffer+ciphertext_length, ciphertext_buffer+ciphertext_length,
0, "i8" 0, "i8"
); );
return { return {
"ciphertext": Module['UTF8ToString'](ciphertext_buffer), "ciphertext": UTF8ToString(ciphertext_buffer),
"mac": Pointer_stringify(mac_buffer), "mac": Pointer_stringify(mac_buffer),
"ephemeral": Pointer_stringify(ephemeral_buffer) "ephemeral": Pointer_stringify(ephemeral_buffer)
}; };
@ -169,9 +169,9 @@ PkDecryption.prototype['decrypt'] = restore_stack(function (
) { ) {
var plaintext_buffer, ciphertext_buffer, plaintext_max_length; var plaintext_buffer, ciphertext_buffer, plaintext_max_length;
try { try {
ciphertext_length = Module['lengthBytesUTF8'](ciphertext) var ciphertext_length = lengthBytesUTF8(ciphertext)
ciphertext_buffer = malloc(ciphertext_length + 1); ciphertext_buffer = malloc(ciphertext_length + 1);
Module['stringToUTF8'](ciphertext, ciphertext_buffer, ciphertext_length + 1); stringToUTF8(ciphertext, ciphertext_buffer, ciphertext_length + 1);
var ephemeralkey_array = array_from_string(ephemeral_key); var ephemeralkey_array = array_from_string(ephemeral_key);
var ephemeralkey_buffer = stack(ephemeralkey_array); var ephemeralkey_buffer = stack(ephemeralkey_array);
var mac_array = array_from_string(mac); var mac_array = array_from_string(mac);
@ -190,11 +190,11 @@ PkDecryption.prototype['decrypt'] = restore_stack(function (
); );
// UTF8ToString requires a null-terminated argument, so add the // UTF8ToString requires a null-terminated argument, so add the
// null terminator. // null terminator.
Module['setValue']( setValue(
plaintext_buffer+plaintext_length, plaintext_buffer+plaintext_length,
0, "i8" 0, "i8"
); );
return Module['UTF8ToString'](plaintext_buffer); return UTF8ToString(plaintext_buffer);
} finally { } finally {
if (plaintext_buffer !== undefined) { if (plaintext_buffer !== undefined) {
// don't leave a copy of the plaintext in the heap. // don't leave a copy of the plaintext in the heap.

View file

@ -1,27 +1,17 @@
var runtime = Module['Runtime'];
var malloc = Module['_malloc']; var malloc = Module['_malloc'];
var free = Module['_free']; var free = Module['_free'];
var Pointer_stringify = Module['Pointer_stringify']; var OLM_ERROR;
var OLM_ERROR = Module['_olm_error']();
/* The 'length' argument to Pointer_stringify doesn't work if the input
* includes characters >= 128, which makes Pointer_stringify unreliable. We
* could use it on strings which are known to be ascii, but that seems
* dangerous. Instead we add a NULL character to all of our strings and just
* use UTF8ToString.
*/
var NULL_BYTE_PADDING_LENGTH = 1;
/* allocate a number of bytes of storage on the stack. /* allocate a number of bytes of storage on the stack.
* *
* If size_or_array is a Number, allocates that number of zero-initialised bytes. * If size_or_array is a Number, allocates that number of zero-initialised bytes.
*/ */
function stack(size_or_array) { function stack(size_or_array) {
return Module['allocate'](size_or_array, 'i8', Module['ALLOC_STACK']); return allocate(size_or_array, 'i8', Module['ALLOC_STACK']);
} }
function array_from_string(string) { function array_from_string(string) {
return Module['intArrayFromString'](string, true); return intArrayFromString(string, true);
} }
function random_stack(size) { function random_stack(size) {
@ -33,11 +23,11 @@ function random_stack(size) {
function restore_stack(wrapped) { function restore_stack(wrapped) {
return function() { return function() {
var sp = runtime.stackSave(); var sp = stackSave();
try { try {
return wrapped.apply(this, arguments); return wrapped.apply(this, arguments);
} finally { } finally {
runtime.stackRestore(sp); stackRestore(sp);
} }
} }
} }
@ -315,7 +305,7 @@ Session.prototype['encrypt'] = restore_stack(function(
Module['_olm_encrypt_message_type'] Module['_olm_encrypt_message_type']
)(this.ptr); )(this.ptr);
plaintext_length = Module['lengthBytesUTF8'](plaintext); plaintext_length = lengthBytesUTF8(plaintext);
var message_length = session_method( var message_length = session_method(
Module['_olm_encrypt_message_length'] Module['_olm_encrypt_message_length']
)(this.ptr, plaintext_length); )(this.ptr, plaintext_length);
@ -325,7 +315,7 @@ Session.prototype['encrypt'] = restore_stack(function(
// need to allow space for the terminator (which stringToUTF8 always // need to allow space for the terminator (which stringToUTF8 always
// writes), hence + 1. // writes), hence + 1.
plaintext_buffer = malloc(plaintext_length + 1); plaintext_buffer = malloc(plaintext_length + 1);
Module['stringToUTF8'](plaintext, plaintext_buffer, plaintext_length + 1); stringToUTF8(plaintext, plaintext_buffer, plaintext_length + 1);
message_buffer = malloc(message_length + NULL_BYTE_PADDING_LENGTH); message_buffer = malloc(message_length + NULL_BYTE_PADDING_LENGTH);
@ -338,14 +328,14 @@ Session.prototype['encrypt'] = restore_stack(function(
// UTF8ToString requires a null-terminated argument, so add the // UTF8ToString requires a null-terminated argument, so add the
// null terminator. // null terminator.
Module['setValue']( setValue(
message_buffer+message_length, message_buffer+message_length,
0, "i8" 0, "i8"
); );
return { return {
"type": message_type, "type": message_type,
"body": Module['UTF8ToString'](message_buffer), "body": UTF8ToString(message_buffer),
}; };
} finally { } finally {
if (plaintext_buffer !== undefined) { if (plaintext_buffer !== undefined) {
@ -366,14 +356,14 @@ Session.prototype['decrypt'] = restore_stack(function(
try { try {
message_buffer = malloc(message.length); message_buffer = malloc(message.length);
Module['writeAsciiToMemory'](message, message_buffer, true); writeAsciiToMemory(message, message_buffer, true);
max_plaintext_length = session_method( max_plaintext_length = session_method(
Module['_olm_decrypt_max_plaintext_length'] Module['_olm_decrypt_max_plaintext_length']
)(this.ptr, message_type, message_buffer, message.length); )(this.ptr, message_type, message_buffer, message.length);
// caculating the length destroys the input buffer, so we need to re-copy it. // caculating the length destroys the input buffer, so we need to re-copy it.
Module['writeAsciiToMemory'](message, message_buffer, true); writeAsciiToMemory(message, message_buffer, true);
plaintext_buffer = malloc(max_plaintext_length + NULL_BYTE_PADDING_LENGTH); plaintext_buffer = malloc(max_plaintext_length + NULL_BYTE_PADDING_LENGTH);
@ -385,7 +375,7 @@ Session.prototype['decrypt'] = restore_stack(function(
// UTF8ToString requires a null-terminated argument, so add the // UTF8ToString requires a null-terminated argument, so add the
// null terminator. // null terminator.
Module['setValue']( setValue(
plaintext_buffer+plaintext_length, plaintext_buffer+plaintext_length,
0, "i8" 0, "i8"
); );
@ -474,8 +464,6 @@ olm_exports["get_library_version"] = restore_stack(function() {
]; ];
}); });
})();
// export the olm functions into the environment. // export the olm functions into the environment.
// //
// make sure that we do this *after* populating olm_exports, so that we don't // make sure that we do this *after* populating olm_exports, so that we don't
@ -483,7 +471,11 @@ olm_exports["get_library_version"] = restore_stack(function() {
if (typeof module !== 'undefined' && module.exports) { if (typeof module !== 'undefined' && module.exports) {
// node / browserify // node / browserify
module.exports = olm_exports; for (var olm_export in olm_exports) {
if (olm_exports.hasOwnProperty(olm_export)) {
Module[olm_export] = olm_exports[olm_export];
}
}
} }
if (typeof(window) !== 'undefined') { if (typeof(window) !== 'undefined') {
@ -492,3 +484,7 @@ if (typeof(window) !== 'undefined') {
// Olm in the global scope for browserified and webpacked apps.) // Olm in the global scope for browserified and webpacked apps.)
window["Olm"] = olm_exports; window["Olm"] = olm_exports;
} }
Module.then(function() {
OLM_ERROR = Module['_olm_error']();
});

View file

@ -1,10 +1,8 @@
var olm_exports = {}; var olm_exports = {};
var get_random_values; var get_random_values;
var process; // Shadow the process object so that emscripten won't get
// confused by browserify
if (typeof(window) !== 'undefined') { if (typeof(window) !== 'undefined') {
// We've in a browser (directly, via browserify, or via webpack). // We're in a browser (directly, via browserify, or via webpack).
get_random_values = function(buf) { get_random_values = function(buf) {
window.crypto.getRandomValues(buf); window.crypto.getRandomValues(buf);
}; };
@ -12,7 +10,9 @@ if (typeof(window) !== 'undefined') {
// We're running in node. // We're running in node.
var nodeCrypto = require("crypto"); var nodeCrypto = require("crypto");
get_random_values = function(buf) { get_random_values = function(buf) {
var bytes = nodeCrypto.randomBytes(buf.length); // [''] syntax needed here rather than '.' to prevent
// closure compiler from mangling the import(!)
var bytes = nodeCrypto['randomBytes'](buf.length);
buf.set(bytes); buf.set(bytes);
}; };
process = global["process"]; process = global["process"];
@ -20,14 +20,23 @@ if (typeof(window) !== 'undefined') {
throw new Error("Cannot find global to attach library to"); throw new Error("Cannot find global to attach library to");
} }
(function() {
/* applications should define OLM_OPTIONS in the environment to override /* applications should define OLM_OPTIONS in the environment to override
* emscripten module settings */ * emscripten module settings (we still need to (re) declare the variable
var Module = {}; * otherwise the closure compiler becomes sad).
*/
var OLM_OPTIONS;
if (typeof(OLM_OPTIONS) !== 'undefined') { if (typeof(OLM_OPTIONS) !== 'undefined') {
for (var key in OLM_OPTIONS) { for (var olm_option_key in OLM_OPTIONS) {
if (OLM_OPTIONS.hasOwnProperty(key)) { if (OLM_OPTIONS.hasOwnProperty(olm_option_key)) {
Module[key] = OLM_OPTIONS[key]; Module[olm_option_key] = OLM_OPTIONS[olm_option_key];
} }
} }
} }
/* The 'length' argument to Pointer_stringify doesn't work if the input
* includes characters >= 128, which makes Pointer_stringify unreliable. We
* could use it on strings which are known to be ascii, but that seems
* dangerous. Instead we add a NULL character to all of our strings and just
* use UTF8ToString.
*/
var NULL_BYTE_PADDING_LENGTH = 1;

View file

@ -16,12 +16,15 @@ limitations under the License.
"use strict"; "use strict";
var Olm = require('../olm'); var Olm = require('../olm')();
describe("megolm", function() { describe("megolm", function() {
var aliceSession, bobSession; var aliceSession, bobSession;
beforeEach(function() { beforeEach(function(done) {
Olm.then(function() {
done();
});
aliceSession = new Olm.OutboundGroupSession(); aliceSession = new Olm.OutboundGroupSession();
bobSession = new Olm.InboundGroupSession(); bobSession = new Olm.InboundGroupSession();
}); });

View file

@ -16,7 +16,7 @@ limitations under the License.
"use strict"; "use strict";
var Olm = require('../olm'); var Olm = require('../olm')();
if (!Object.keys) { if (!Object.keys) {
Object.keys = function(o) { Object.keys = function(o) {
@ -30,7 +30,13 @@ describe("olm", function() {
var aliceAccount, bobAccount; var aliceAccount, bobAccount;
var aliceSession, bobSession; var aliceSession, bobSession;
beforeEach(function() { beforeEach(function(done) {
// This should really be in a beforeAll, but jasmine-node
// doesn't support that
Olm.then(function() {
done();
});
aliceAccount = new Olm.Account(); aliceAccount = new Olm.Account();
bobAccount = new Olm.Account(); bobAccount = new Olm.Account();
aliceSession = new Olm.Session(); aliceSession = new Olm.Session();

View file

@ -16,7 +16,7 @@ limitations under the License.
"use strict"; "use strict";
var Olm = require('../olm'); var Olm = require('../olm')();
if (!Object.keys) { if (!Object.keys) {
Object.keys = function(o) { Object.keys = function(o) {
@ -29,7 +29,11 @@ if (!Object.keys) {
describe("pk", function() { describe("pk", function() {
var encryption, decryption; var encryption, decryption;
beforeEach(function() { beforeEach(function(done) {
Olm.then(function() {
done();
});
encryption = new Olm.PkEncryption(); encryption = new Olm.PkEncryption();
decryption = new Olm.PkDecryption(); decryption = new Olm.PkDecryption();
}); });