Skip to content

Error in UTF8ToString when calling wgpuDeviceCreateShaderModule #20453

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

Closed
Ravbug opened this issue Oct 13, 2023 · 2 comments
Closed

Error in UTF8ToString when calling wgpuDeviceCreateShaderModule #20453

Ravbug opened this issue Oct 13, 2023 · 2 comments
Labels

Comments

@Ravbug
Copy link

Ravbug commented Oct 13, 2023

When calling wgpuDeviceCreateShaderModule, occasionally the call will fail with
Invalid UTF-8 leading byte 0x00000090 encountered when deserializing a UTF-8 string in wasm memory to a JS string!

Code that's causing the error:

void ShaderLibraryWG::InitFromShaderSource(decltype(owningDevice) owningDevice, const std::string_view shadercode){
        WGPUShaderModuleWGSLDescriptor shaderCodeDesc;
        shaderCodeDesc.chain.next = nullptr;
        shaderCodeDesc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
        shaderCodeDesc.code = shadercode.data();
        WGPUShaderModuleDescriptor moduleDesc;
        moduleDesc.nextInChain = &shaderCodeDesc.chain;
        shaderModule = wgpuDeviceCreateShaderModule(owningDevice->device, &moduleDesc);
    }

The shader string:

diagnostic(off, derivative_uniformity);

var<private> outColor : vec4f;

var<private> fragColor : vec3f;

fn main_1() {
  let x_14 : vec3f = fragColor;
  outColor = vec4f(x_14.x, x_14.y, x_14.z, 1.0f);
  return;
}

struct main_out {
  @location(0)
  outColor_1 : vec4f,
}

@fragment
fn main(@location(0) fragColor_param : vec3f) -> main_out {
  fragColor = fragColor_param;
  main_1();
  return main_out(outColor);
}

Call stack:
image

Maybe the issue is related to #20124 and that the shader source string in WGPUShaderModuleWGSLDescriptor is non-owning? Sometimes the code works without issues.

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.48-git (5b63268c904dd0e542f9c45e2b136c3d4a0bd638)
clang version 18.0.0 (https://github.com/llvm/llvm-project 25da11541c0740950e812a85d332a6d5cf4a5f87)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\Users\Admin\Documents\emsdk\upstream\bin
@kripken
Copy link
Member

kripken commented Oct 16, 2023

cc @kainino0x

@kainino0x
Copy link
Collaborator

The string in WGPUShaderModuleWGSLDescriptor is indeed non-owning. But it should certainly be valid for duration of the wgpuDeviceCreateShaderModule call, and proxying would block the calling thread to maintain that guarantee. I'm guessing that's what the 'sync' means, in:

      LibraryWebGPU[key + '__proxy'] = 'sync';

It seems more likely the issue is in your code. Actually, looking more closely, I think the problem is that WGPUShaderModuleWGSLDescriptor requires a null-terminated string, but std::string_view::data() does not return one. (You need std::string::c_str(), which isn't available on std::string_view.) So unless you're manually null-terminating your string before it's passed here, we (Emscripten) will read garbage past the end of your string.

@kainino0x kainino0x closed this as not planned Won't fix, can't repro, duplicate, stale Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants