chrox/lua-serialize — pull request #1 by mesailde (2014-06-21 02:56:52+00:00)
Title: General fix for alignment issues on ARM

We are currently working on running busted tests in the Kindle device itself and have found a couple of issues which didn't arise when running these tests on a PC.
When koreader is compiled with neon instructions, the `return *pn` in `case 4` of the `_get_number` function is compiled to `vldr` and `vcvt.f64.s32` instructions which require 4-byte alignment of the `pn` pointer. Failure to align the pointer causes a SIGILL:
```
Program received signal SIGILL, Illegal instruction.
0x40706e68 in ?? () from common/serialize.so
(gdb) disassemble $pc-16,+32
Dump of assembler code from 0x40746e58 to 0x40746e78:
0x40746e58:	bl	0x40744eb8
0x40746e5c:	subs	r5, r0, #0
0x40746e60:	beq	0x40746f44
0x40746e64:	vldr	s0, [r5]
=> 0x40746e68:	vcvt.f64.s32	d0, s0
0x40746e6c:	add	sp, sp, #12
0x40746e70:	pop	{r4, r5, r6, r7, r8, r9, pc}
0x40746e74:	add	r5, sp, #8
```
With this patch `rb_read` will always return the `buffer` pointer provided by caller, which should be naturally aligned. Thus the fix specific for `case 8` is not needed anymore, and alignment issues should be solved for all cases.
We are also working on running Valgrind in Kindle. The following message is shown when running the tests inside Valgrind:
```
==25521== Invalid write of size 1
==25521== at 0x4831F94: memcpy (mc_replace_strmem.c:883)
==25521== by 0x5260E7D: rb_read (serialize.c:151)
==25521== by 0x5260F39: _get_buffer (serialize.c:471)
==25521== by 0x52624DB: _push_value (serialize.c:538)
==25521== by 0x526282D: _unpack_one (serialize.c:556)
==25521== by 0x5262517: _push_value (serialize.c:499)
==25521== by 0x526274B: _deserialize (serialize.c:674)
==25521== by 0x447BF: lj_BC_FUNCC (lj_vm.s:917)
==25521== by 0x39E87: lua_pcall (lj_api.c:1041)
==25521== by 0xB509: docall (luajit.c:121)
==25521== by 0xBECB: pmain (luajit.c:288)
==25521== by 0x447BF: lj_BC_FUNCC (lj_vm.s:917)
==25521== Address 0xbdc347df is on thread 1's stack
```
Perhaps the stack is not big enough to hold `tmp` in `_get_buffer` in some situation. This patch replaces the stack allocation by a heap allocation using malloc and also adds the `rb_read` null pointer return check which was missing.