emscripten-core / emscripten-core/emscripten

wchar_t sting literals compiled with -fshort-wchar generate alignmentfault errors

Open
#18,618 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

**Version of emscripten/emsdk:**
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.30 (cfe2bdfe2692457cb5f5770672f6e5ccb3ffc2f2)
clang version 16.0.0 (https://github.com/llvm/llvm-project 800f0f1546b2352ba42a4777149afb13cb874fcd)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\emsdk\upstream\bin

**Failing command line in full:**
- Not a compile time failure but here is my compile command
`em++ main.cpp -std=c++20 -fshort-wchar -sWASM=1 -g3 -pthread -s PTHREAD_POOL_SIZE=1 -sALLOW_BLOCKING_ON_MAIN_THREAD -s LLD_REPORT_UNDEFINED -s SAFE_HEAP=1 -o index.js`

Here is an extremely minimal version of the code. This contains the error in it:
```
#include
#include
#include

const wchar_t *wszValue = L"";
const wchar_t *wszText = L"text";
const wchar_t *wszVal = L"Val";

class Test
{
public:
const wchar_t *m_wszVal = L"";
const wchar_t *m_wszTest = L"Test";
const wchar_t *m_wszTest2 = L"Test2";
};

Test test;

int main()
{
const wchar_t *str = L"str2";
const wchar_t *str2 = L"";
const wchar_t *str3 = L"str";

wprintf(L"%u %u\n",sizeof(wchar_t),sizeof(empty));
wprintf(L"%u %u %u\n",wcslen(str),wcslen(str2),wcslen(str3));
wprintf(L"%u %u %u\n",wcslen(wszValue),wcslen(wszText),wcslen(wszVal));
wprintf(L"%u %u %u\n",wcslen(test.m_wszVal),wcslen(test.m_wszTest),wcslen(test.m_wszTest2));
wprintf(L"%S %S %S\n",str,str2,str3);
wprintf(L"%S %S %S\n",wszValue,wszText,wszVal);
wprintf(L"%S %S %S\n",test.m_wszVal,test.m_wszTest,test.m_wszTest2);

return EXIT_SUCCESS;
}
```

Basically the error I am running into is that the compiler is placing these 2 byte wchar literals at unaligned memory locations. So you cannot read from them properly without crashing from alignment faults.

For completeness here is a `index.html`
```




var canv = document.getElementById('canvas');
var Module = {
canvas: canv
};


```
A quick and dirty server `server.js`
```
var http = require('http')
var url = require('url')
var fs = require('fs')
var path = require('path')
var baseDirectory = __dirname // or whatever base directory you want

var port = 8080

http.createServer(function (request, response) {
try {
var requestUrl = url.parse(request.url)

// need to use path.normalize so people can't access directories underneath baseDirectory
var fsPath = baseDirectory+path.normalize(requestUrl.pathname)

var fileStream = fs.createReadStream(fsPath)
fileStream.pipe(response)
fileStream.on('open', function() {
// response.writeHead(200)
if ( requestUrl.pathname.endsWith('.wasm') )
response.writeHead(200, { 'Content-type': 'application/wasm', 'Cross-Origin-Embedder-Policy': 'require-corp', 'Cross-Origin-Opener-Policy': 'same-origin' } )
else if ( requestUrl.pathname.endsWith('.js') )
response.writeHead(200, { 'Content-type': 'text/javascript', 'Cross-Origin-Embedder-Policy': 'require-corp', 'Cross-Origin-Opener-Policy': 'same-origin' } )
else
response.writeHead(200, { 'Cross-Origin-Embedder-Policy': 'require-corp', 'Cross-Origin-Opener-Policy': 'same-origin' } )
})
fileStream.on('error',function(e) {
response.writeHead(404) // assume the file doesn't exist
response.end()
})
} catch(e) {
response.writeHead(500)
response.end() // end the response so browsers don't hang
console.log(e.stack)
}
}).listen(port)
```
and a command to run the server `node server.js`
and then connect to `http://127.0.0.1:8080/index.html` in the browser

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.