Skip to content

JS library fixes for 2GB+ heaps - #10602

Merged
kripken merged 3 commits into
masterfrom
js4gb
Mar 26, 2020
Merged

kripken merged 3 commits into
masterfrom
js4gb

Conversation

@kripken

@kripken kripken commented Feb 28, 2020

Copy link
Copy Markdown
Member

Almost all of these are behind the new flag, but a handful would
require annoying refactoring, so I just use >>> for those.

This is part of a set of 3+ PRs for #6566, together with #10600, #10601

@kripken
kripken requested a review from juj March 3, 2020 01:07
Comment thread src/embind/embind.js
// assumes 4-byte alignment
var length = getLength();
var ptr = _malloc(4 + length + 1);
#if CAN_ADDRESS_2GB

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Instead of having a CAN_ADDRESS_2GB option, could we reuse WASM_MEM_MAX option? (#if WASM_MEM_MAX >= 2GB)? (if not, would it better be CAN_ADDRESS_4GB?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We can get rid of the setting in settings.js, but then we'd need to compute "can we refer to address 2GB+" in both JS and Python separately. It's not a hard computation, though - check if growth or not, and if yes, what the limit is and if there is no limit. I can do it either way, but an internal setting seems like less code and more uniform between the two world (JS and Python).

I think CAN_ADDRESS_2GB is more accurate, as this problem exists for people doing 2GB + 64KB, 3GB, etc., and not just 4GB. They key issue is at address 2GB and above, pointers start to use the high bit.

(WASM_MEM_MAX was renamed MAXIMUM_MEMORY, btw - but old one still works)

Comment thread src/library_fs.js
msync: function(stream, buffer, offset, length, mmapFlags) {
#if CAN_ADDRESS_2GB
offset >>>= 0;
#endif

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hmm, why are the above four casts needed? These functions are not directly called from wasm code?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

They aren't call from wasm directly, yeah, but a user might call FS.msync(..) with a 32-bit pointer. Perhaps the pointer arrived from wasm, or perhaps the user did a | 0 after an add or such.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If the pointer arrives from wasm, I think it should be converted right there and then when it arrives, because that value could be used (incorrectly) somewhere else as well, leaving the missing conversion bug around?

Also, here in these functions this kind of cast would hide real errors that might arise from incorrect calls with negative numbers, since negative numbers would be treated as positive. (e.g. a real FS.msync() call with erroneous negative offset)

Talking about using {{{ }}} syntax, perhaps here would be a good case to use one? Perhaps we should integrate interop with unsigned, string, int64 conversion, e.g. with

malloc__sig: 'pz', // ('p' for pointer, 'z' for size_t)
foo: function() {
  var nb = 10;
  var ptr = {{{ callWasm('_malloc', 10) }}};
}

would turn into

malloc__sig: 'pz', // ('p' for pointer, 'z' for size_t)
foo: function() {
  var nb = 10;
  var ptr = _malloc(10) >>> 0;
}

in CAN_ADDRESS_2GB mode? Also could expand that with

someFunc__sig: 'sU', // ('s' for UTF-8 string, 'U' for uint64_t)
foo: function() {
  var nb = 10;
  var jsString = {{{ callWasm('_someFunc', 10) }}};
  var jsString = {{{ callWasmSig('_someFunc', 'sU', 10) }}}; // alternative form, specify sig directly?
}

would turn into

someFunc__sig: 'sU', // ('s' for UTF-8 string, 'U' for uint64_t)
foo: function() {
  var jsString = UTF8ToString(_someFunc(10, 10/4294967296));
}

That way JS callers could automate type conversion for different function calls? (also would obviate needing to declare bindings with webidl_binder.py)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If the pointer arrives from wasm, I think it should be converted right there and then when it arrives, because that value could be used (incorrectly) somewhere else as well, leaving the missing conversion bug around?

Do you mean we should instrument all exports for this? Otherwise I'm not sure how to process all incoming pointers - the code calling wasm can be arbitrary code the user wrote in JS. Fixing up the pointers when they arrive in our FS code seems like a safe thing to do.

Also, here in these functions this kind of cast would hide real errors that might arise from incorrect calls with negative numbers, since negative numbers would be treated as positive. (e.g. a real FS.msync() call with erroneous negative offset)

Maybe I don't see your point here, but I don't think this would hide a bug? It's a pointer, so if someone passes in -10 that's going to be a huge positive number and we'll fail when we try to load or store to it.

If this were an offset (like an offset in a file) then I agree it would be bad to >>>= it, but this is a pointer?

var jsString = {{{ callWasm('_someFunc', 10) }}};

Interesting idea! I'd like to separate that from this PR, but it definitely is worth considering. I am somewhat concered about the cost of needing to write the signature at each call, which makes me think it doesn't improve readability.

If we do think this is a high-risk area and we want to cover all calls into and out of wasm, then I think instrumenting all wasm exports (as mentioned at the top) might be better - we sacrifice no code readability, and it's guaranteed to apply everywhere. However, it has some downsides, like it adds overhead to each call :(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Perhaps autoinstrumenting might be an overkill, but a {{{ callWasm() }}} utility might be nice.

I'd like to separate that from this PR, but it definitely is worth considering.

If we agree that it would be nicer to shift right in the wasm->JS boundary rather than in misc utility functions, we can certainly leave that work for later cleanup as well? (although I must say that I do not particularly care about the code in library_fs.js since that is not in our critical path)

I am somewhat concered about the cost of needing to write the signature at each call, which makes me think it doesn't improve readability.

Yeah, that is a good point.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

think instrumenting all wasm exports (as mentioned at the top) might be better

The issue with autoinstrumenting that comes to mind is if one routes a wasm function directly to another, e.g.

_callWasmFunction2FromJSThatTakesAPtr(_callWasmFunction1FromJsThatReturnsAPtr());

there we would not need to shift, but automatic code transformations would not know that.

Comment thread src/library_memfs.js
expandFileStorage: function(node, newCapacity) {
#if CAN_ADDRESS_2GB
newCapacity >>>= 0;
#endif

@juj juj Mar 3, 2020 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same here and below - this function is never called from wasm directly?

Comment thread src/runtime_strings.js
function UTF8ArrayToString(u8Array, idx, maxBytesToRead) {
function UTF8ArrayToString(heap, idx, maxBytesToRead) {
#if CAN_ADDRESS_2GB
idx >>>= 0;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Likewise, this function is never called directly from wasm, but from a Number... it seems to be that the shift should have occurred already earlier?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's true that optimally the shift would occur in an earlier place. But imagine we have user code like

var x = foo();
x = x + 10 | 0;
var y = UTF8ArrayToString(HEAP8, x);

Given user-written JS code, I don't think we can assume that they will properly keep the value unsigned. I think the safe thing is to treat the small number of pointer-using functions (like UTF8ArrayToString) as receiving input with uncertain sign, so we fix up the sign, just like at the beginning of an asm.js function.

Basically, my logic is similar to LLVM or asm.js: pointers can be passed around and stored with incorrect sign, but when we do an operation where the sign matters, we make sure to set the sign correctly. In these functions it is easy to do that by just unsigning the input (but a few cases do require more unsign operations in the middle, replacing | 0s etc.).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Given user-written JS code, I don't think we can assume that they will properly keep the value unsigned.

I think we can, and we should? The code example you posted would be invalid for user code to have in the first place under the 4GB address space mode.

I think the safe thing is to treat the small number of pointer-using functions (like UTF8ArrayToString) as receiving input with uncertain sign, so we fix up the sign, just like at the beginning of an asm.js function.

but when we do an operation where the sign matters, we make sure to set the sign correctly.

The danger here might be that if we do the shifts in utility functions to patch up "after the fact", then the shift only applies to those utility functions, and users might still be doing other operations to the data in their own code, and those operations would be then incorrect. Then they'll still need to fix up their own code, and we result in duplicate/redundant shifts.

In other words, it feels like these shifts are only a partial fix "in the wrong place", whereas the "proper" fix would be to have the shifts right when the call is made.

To me it seems that like C string -> JS string marshalling, we should have a rule/convention for marshalling uint32->Number back from wasm that users need to adhere to, either manually or by using a {{{ callWasm(...) }}} type of construct.

I think the only exception to this is perhaps the marshalling related functions, UTF8ToString() etc. since it is more convenient to call UTF8ToString(_someWasmFuncThatReturnsAPtr()) rather than always needing to do UTF8ToString(_someWasmFuncThatReturnsAPtr()>>>0) - but that exception seems to be appropriate specifically for functions that deal with type wasm-JS type marshalling.

But for other library code that accesses the heap with pointers - it seems that there are boatloads of library functions that do heap accesses, vs the number of functions that call back from wasm; doing the shifts in the wasm-js boundary would result in fewer shifts overall vs annotating all heap accesses in all library functions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In general for heap accesses we have #10601 that will instrument them automatically. It also only operates when 2GB+ is relevant, so we don't increase code size in <2GB builds (as >> is smaller than >>>).

So the remaining risks are when JS does manual computation of pointers and then uses them directly, like say using the pointer as the key of a hash map, or comparing between two pointers.

I agree this PR might "paper over" problems in user code. But I really think getting this right in user code is a high bar - this is a completely new requirement for users to think about, that only happens in 2GB+ heaps, and will likely be a cause of bugs for them. We should document this a lot more, but I also think >>>=s in key places in our API, where we receive pointers from users and then proceed to use them in ways where the sign matters (rare combination), we should fix up the sign.

@juj

juj commented Mar 3, 2020

Copy link
Copy Markdown
Collaborator

Looks good, though I am surprised by the locations of these shifts?

@juj

juj commented Mar 5, 2020 •

Copy link
Copy Markdown
Collaborator

Re-reading, I suppose all the shifts are in locations where I either agree (UTF8ToString etc, since they are marshalling functions, so best to have the shifts there for convenience), or not in critical path for us (library_fs.js, library_syscall.js, so any convenience in them is ok). Perhaps a future wasmCall interop helper might ease things around this for users, though not definitely needed for this PR.

lgtm!

@kripken

kripken commented Mar 5, 2020

Copy link
Copy Markdown
Member Author

Sounds good, thanks for the review comments! This can land after #10601

@kripken
kripken merged commit 60d3efd into master Mar 26, 2020
@kripken
kripken deleted the js4gb branch March 26, 2020 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants