emscripten-core / emscripten-core/emscripten

shared_ptr copy constructor misaligns incoming reference

Open
#20,360 4 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

Please include the following in your bug report:

**Version of emscripten/emsdk:**
emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.21 (f9e81472f1ce541eddece1d27c3632e148e9031a)
clang version 16.0.0 (https://github.com/llvm/llvm-project a4a29438f451370ed241dde30bfcaab0fdf2ab71)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: D:\repos\emsdk\upstream\bin

I'm running on Windows 11 with both Chrome and the Edge Chrome browsers.

At run time I'm seeing an issue with the below line of code but only in our full project, I've not been able to yet identify a minimal repro. Small amounts of exactly the same code work just fine in a minimal context.

```
std::shared_ptr BoundingRectangle() const noexcept override
{
return m_boundingRectangle; // the problematic line
}

```
When this is run the return is shifted by 4 bytes from the original m_boundingRectangle which causes a crash later on. if I step into the shared_ptr constructor:

```
_LIBCPP_HIDE_FROM_ABI
shared_ptr(const shared_ptr& __r) _NOEXCEPT
: __ptr_(__r.__ptr_),
__cntrl_(__r.__cntrl_)
{
if (__cntrl_
__cntrl_->__add_shared();
}

```
__r.__ptr__ is assigned a value of addressof(m_boundingRectangle) + 0x4 bytes as soon as the constructor is entered. This is 100% reproducible in this context and seems even persist with different compiler/linker flags, different optimization levels and moving the code around to hopefully get some different webassembly output. Other data types that are present in this class seem to not be affected though everything else is either a simple string or a vector of some sort.

If it's helpful the member variables that are causing issues are defined as

```
class BaseResult
{
private:
std::shared_ptr m_boundingRectangle {};
std::shared_ptr m_rotatedBoundingRectangle {};
```

I've got a library and a wasm compiled via emscripten with the library doing all of the work described above. With a relationship like

browser -> bar.wasm -> foo.a

Lastly my cmake link options

```
target_link_options(${PROJECT_NAME} PRIVATE
$<$: -Oz>
$<$: -g -sDEMANGLE_SUPPORT=1>
--bind
-sALLOW_MEMORY_GROWTH=1
-sWASM=1
-sENVIRONMENT=web
-sASYNCIFY
-sMODULARIZE=1
-sASSERTIONS=1
-sSAFE_HEAP=1

```
Similar code has worked in the past, I'm trying to identify what's different but haven't found anything meaningful to date. Does anyone have any idea how can I go about diagnosing this further or work around this?

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.