Double alignment

33 views
Skip to first unread message

Mark Sibly

unread,
Dec 17, 2017, 3:46:48 PM12/17/17
to emscripten-discuss
Hi,

I am having some intermittent problems with what appears to be float point double alignment, where writing to a floating point double field ends up writing to the wrong location. This has been solved in every case so far by manually '8 byte aligning' the double field.

Also, this only seems to be happening when generating asm.js 'apps' - so far, there have been no problems with wasm.

Unfortunately, this is happening in a translator I am writing, and every attempt I have made so far to isolate some reproducable c++ code has failed!

Just wondering of this was a known issue of some kind, or if anyone else has had similar problems? With a bit of luck I'll be able to retire the asm.js target soon in favour of wasm in which case it wont matter, but I just thought I'd check...

Bye,
Mark

Brion Vibber

unread,
Dec 17, 2017, 3:56:08 PM12/17/17
to emscripten Mailing List
In asm.js code, all memory loads and stores must be aligned (wasm does not have this limitation).

Normally alignment rules on structs should 'just work' I think -- what does the structure definition look like?

-- brion

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Sibly

unread,
Dec 17, 2017, 4:58:50 PM12/17/17
to emscripte...@googlegroups.com
> what does the structure definition look like?

Well, it's the output of a transpiler so it's a bit of a mess. But the basic idea is:

struct GCNode{
GCNode *succ,*pred;
char pad[2];
char state;
char flags;

virtual ~GCNode(){
}
};

struct Timer : GCNode{

Timer( double hertz ){
period=1.0/hertz;
}

double period;
};

...where 'Timer' is the user class causing problems and GCNode is a system class.

When built by the transpiler, the end result is that the write to 'period' seems to be accidentally writing to 'state '- so it's possibly worth noting it's therefore not an asm.js alignment issue, the compiler is producing valid but incorrect code. However, if you build/run this as is, it works fine. I have only encountered the problem when running the output of the transpiler, and all attempts to c++-ify an example have failed so far. It's a real heisenbug!

The 'period' field is indeed misligned for a double, as there is malloc size+virtual function table (8 bytes) at start of allocation. And adding an int 'pad' field just before 'period' fixes the problem.

Could be the transpiler I guess, but I haven't had this issue with mingw32 or android/ios 32 bit builds - or even wasm. Just asm.js builds.

To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/l_P-aW8ZYm4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-discuss+unsubscribe@googlegroups.com.

Mark Sibly

unread,
Dec 17, 2017, 6:01:56 PM12/17/17
to emscripte...@googlegroups.com
Actually, think I know what's going on.

I have a custom operator new() which does not necessarily return 8 byte aligned memory (I think)?

 Would this be enough to cause problems in asm.js but not in wasm?


András Kucsma

unread,
Dec 17, 2017, 6:13:10 PM12/17/17
to emscripte...@googlegroups.com
Yes, definitely.

You can also try setting -s SAFE_HEAP=1 to automatically assert on alignment and other memory access errors.

To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

Mark Sibly

unread,
Dec 17, 2017, 9:14:58 PM12/17/17
to emscripte...@googlegroups.com
Look like that was it - thanks for everyone help!

On a completely unrelated topic:

Many of my users find it very hard to install emscripten, or to upgrade to the latest version (1.37.9 here) for use with the transpiler.

I would therefore like to put together a little 'just works' single dir package of the emscripten sdk/tools based on the portable version (if that's stll around).

All I really need is em++, emar, and whatever it is they depend on (emld? etc...ie: the bin dir?). Do I really need 'node.js', 'spidermonkey' (whatever that is) and the other baggage? I realize I probably need python, although I do provide my own server for hosting the 'apps' so don't actually use it for that, so I'm not actually sure what it is for although I gather python is generally how script-ish stuf works on all targets these days?

Anyway, what can I leave out from a super simple, em++/emar  only style standalone package?



To unsubscribe from this group and all its topics, send an email to emscripten-discuss+unsub...@googlegroups.com.

Jukka Jylänki

unread,
Dec 22, 2017, 8:44:13 AM12/22/17
to emscripte...@googlegroups.com
Instead of adding manual "char pad;" fields to structs, I'd recommend
using the __attribute__((packed)) and/or __attribute__((aligned(x)))
modifiers, see https://stackoverflow.com/questions/14332633/attribute-packed-v-s-gcc-attribute-alignedx
. This makes the compiler aware of misalignment, and makes it generate
proper unaligned loads/stores when needed for asm.js.

If you want to create a standalone package, you can do

git clone https://github.com/juj/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest --embedded
cd ..
tar cvzf emsdk-embedded.tar.gz emsdk

Then to install and use:

tar xvf emsdk-embedded.tar.gz
cd emsdk
source ./emsdk_env.sh # Prepares current terminal for PATH and so on

You can customize what files you zip up there. Node.js and python are
needed, SpiderMonkey and Java are not. (Java is needed only if you
want Closure compiler)
>>>>>> an email to emscripten-disc...@googlegroups.com.
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to a topic in the
>>>>> Google Groups "emscripten-discuss" group.
>>>>> To unsubscribe from this topic, visit
>>>>> https://groups.google.com/d/topic/emscripten-discuss/l_P-aW8ZYm4/unsubscribe.
>>>>> To unsubscribe from this group and all its topics, send an email to
>>>>> emscripten-disc...@googlegroups.com.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "emscripten-discuss" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to emscripten-disc...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "emscripten-discuss" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/emscripten-discuss/l_P-aW8ZYm4/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> emscripten-disc...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to emscripten-disc...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages