Skip to content

Fix API calls to WebGL where an optional size parameter is zero bytes#16837

Merged
juj merged 1 commit intoemscripten-core:mainfrom
juj:fix_zero_byte_webgl_transfers
Apr 28, 2022
Merged

Fix API calls to WebGL where an optional size parameter is zero bytes#16837
juj merged 1 commit intoemscripten-core:mainfrom
juj:fix_zero_byte_webgl_transfers

Conversation

@juj
Copy link
Collaborator

@juj juj commented Apr 28, 2022

Fix API calls to WebGL where an optional size parameter is zero bytes, which would upload the whole WebAssembly heap since WebGL has a different semantics when zero size is passed. Fixes #16799.

…, which would upload the whole WebAssembly heap since WebGL has a different semantics when zero size is passed. Fixes emscripten-core#16799.
@juj juj added the GL label Apr 28, 2022
@juj juj requested review from kainino0x and kripken April 28, 2022 13:29
@juj
Copy link
Collaborator Author

juj commented Apr 28, 2022

We don't have tests for this, so recommend a careful review.

Copy link
Collaborator

@kainino0x kainino0x left a comment

Choose a reason for hiding this comment

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

Nice catch, and good example of sentinel 0 values being a footgun. Glad we removed them in WebGPU.

assert(GL.currentContext.version >= 2);
#endif
GLctx.uniform1iv(webglGetUniformLocation(location), HEAP32, value>>2, count);
count && GLctx.uniform1iv(webglGetUniformLocation(location), HEAP32, value>>2, count);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this just another way of writing if (count) .... but saving two bytes?

Seems a little odd to do the micro-optimization here but not globally across the codebase.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is this just another way of writing if (count) .... but saving two bytes?

That's right.

Seems a little odd to do the micro-optimization here but not globally across the codebase.

Maybe. It is used consistently in WebGL library, and that is probably more on the hot code size path that some of the other libraries.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, ok. I didn't realize there was precedent for this.

Copy link
Member

Choose a reason for hiding this comment

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

I think closure does this? If not, it would be easy to add a js optimizer pass. I think depending on the optimizer for this kind of thing is probably best (keeps code more readable and the optimization applies to more places).

@sbc100
Copy link
Collaborator

sbc100 commented Apr 28, 2022

It seems odd that we don't have tests for any of this .. are they hard to write?

@juj
Copy link
Collaborator Author

juj commented Apr 28, 2022

It seems odd that we don't have tests for any of this .. are they hard to write?

There are 26 API entry points that are affected, which is a lot of tests to write. Writing tests that check that the whole Wasm heap is not being serialized probably would require some kind of mocking/polyfilling on the ArrayBuffer.

@juj juj merged commit ccddf51 into emscripten-core:main Apr 28, 2022
sbc100 added a commit to sbc100/emscripten that referenced this pull request Mar 20, 2024
The previous fix for this was in emscripten-core#16837, but I looks like that only
covered the new "garbage-free" webgl2 path.  When old webgl1 path was
still using the zero count value.  For example the following line was
unguarded:

```
var view = miniTempWebGLFloatBuffers[4 * count - 1];
```

This recently resurfaced because I introduced
`WEBGL_USE_GARBAGE_FREE_APIS` which is currently disabled for memories
larger 2gb.  This meant that users with large memories were forces onto
the old path where the bug still existed.

Rather than adding yet more `count &&` prefixes, this patch simply adds
a single early return at the top of each function.

Fixes: emscripten-core#21567
sbc100 added a commit to sbc100/emscripten that referenced this pull request Mar 20, 2024
The previous fix for this was in emscripten-core#16837, but I looks like that only
covered the new "garbage-free" webgl2 path.  When old webgl1 path was
still using the zero count value.  For example the following line was
unguarded:

```
var view = miniTempWebGLFloatBuffers[4 * count - 1];
```

This recently resurfaced because I introduced
`WEBGL_USE_GARBAGE_FREE_APIS` which is currently disabled for memories
larger 2gb.  This meant that users with large memories were forces onto
the old path where the bug still existed.

Rather than adding yet more `count &&` prefixes, this patch simply adds
a single early return at the top of each function.

Fixes: emscripten-core#21567
sbc100 added a commit to sbc100/emscripten that referenced this pull request Mar 20, 2024
The previous fix for this was in emscripten-core#16837, but I looks like that only
covered the new "garbage-free" webgl2 path.  When old webgl1 path was
still using the zero count value.  For example the following line was
unguarded:

```
var view = miniTempWebGLFloatBuffers[4 * count - 1];
```

This recently resurfaced because I introduced
`WEBGL_USE_GARBAGE_FREE_APIS` which is currently disabled for memories
larger 2gb.  This meant that users with large memories were forces onto
the old path where the bug still existed.

Rather than adding yet more `count &&` prefixes, this patch simply adds
a single early return at the top of each function.

Fixes: emscripten-core#21567
sbc100 added a commit to sbc100/emscripten that referenced this pull request Mar 20, 2024
The previous fix for this was in emscripten-core#16837, but I looks like that only
covered the new "garbage-free" webgl2 path.  When old webgl1 path was
still using the zero count value.  For example the following line was
unguarded:

```
var view = miniTempWebGLFloatBuffers[4 * count - 1];
```

This recently resurfaced because I introduced
`WEBGL_USE_GARBAGE_FREE_APIS` which is currently disabled for memories
larger 2gb.  This meant that users with large memories were forces onto
the old path where the bug still existed.

Rather than adding yet more `count &&` prefixes, this patch simply adds
a single early return at the top of each function.

As part of this change I resurrected
`test_webgl_draw_triangle_with_uniform_color.c` which has not actually
be used since emscripten-core#20925.

Fixes: emscripten-core#21567
sbc100 added a commit to sbc100/emscripten that referenced this pull request Mar 21, 2024
The previous fix for this was in emscripten-core#16837, but I looks like that only
covered the new "garbage-free" webgl2 path.  When old webgl1 path was
still using the zero count value.

As part of this change I resurrected
`test_webgl_draw_triangle_with_uniform_color.c` which has not actually
be used since emscripten-core#20925.

Fixes: emscripten-core#21567
sbc100 added a commit to sbc100/emscripten that referenced this pull request Mar 21, 2024
The previous fix for this was in emscripten-core#16837, but I looks like that only
covered the new "garbage-free" webgl2 path.  When old webgl1 path was
still using the zero count value.

As part of this change I resurrected
`test_webgl_draw_triangle_with_uniform_color.c` which has not actually
be used since emscripten-core#20925.

Fixes: emscripten-core#21567
sbc100 added a commit that referenced this pull request Mar 26, 2024
The previous fix for this was in #16837, but I looks like that only
covered the new "garbage-free" webgl2 path.  When old webgl1 path was
still using the zero count value.  For example the following line was
unguarded:

```
var view = miniTempWebGLFloatBuffers[4 * count - 1];
```

This recently resurfaced because I introduced
`WEBGL_USE_GARBAGE_FREE_APIS` which is currently disabled for memories
larger 2gb.  This meant that users with large memories were forces onto
the old path where the bug still existed.

Rather than adding yet more `count &&` prefixes, this patch simply adds
a single early return at the top of each function.

As part of this change I resurrected
`test_webgl_draw_triangle_with_uniform_color.c` which has not actually
be used since #20925.

Fixes: #21567
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

glBufferSubData overflows when size == 0

4 participants