[vlc-devel] [PATCH] configure: detect 32/64-bit lua 5.3 bytecode

Andrey Gursky andrey.gursky at e-mail.ua
Fri May 20 05:33:54 CEST 2016


Hi Steve,

On Thu, 19 May 2016 10:56:55 +0200
Steve Lhomme <robux4 at gmail.com> wrote:

> On Fri, Apr 8, 2016 at 1:27 AM, Andrey Gursky <andrey.gursky at e-mail.ua> wrote:
> > The ABI magic bytes in lua 5.3 bytecode are located at a different
> > offset than in lua 5.1 or lua 5.2 bytecode. This patch fixes the
> > detection by distinguishing two cases for <5.3 and 5.3 lua versions.
> >
> > For documentation purposes:
> >
> > lua version can be obtained at the offset 4 (e.g. 0051, 0053).
> > ABI magic bytes (32-bit: 0404) can be obtained:
> >  - lua 5.1 and 5.2: at the offset 8;
> >  - lua 5.3: at the offset 12.
> >
> > $ echo | luac -o - - | od -j 0 -N 100 -t x2
> >
> > luac5.1 32-bit:
> > 0000000 4c1b 6175 0051 0401 0404 0008 0007 0000
> > 0000020 733d 6474 6e69 0000 0000 0000 0000 0000
> > 0000040 0200 0102 0000 1e00 8000 0000 0000 0000
> > 0000060 0000 0100 0000 0100 0000 0000 0000 0000
> > 0000100 0000 0000
> >
> > luac5.1 64-bit:
> > 0000000 4c1b 6175 0051 0401 0408 0008 0007 0000
> > 0000020 0000 0000 733d 6474 6e69 0000 0000 0000
> > 0000040 0000 0000 0200 0102 0000 1e00 8000 0000
> > 0000060 0000 0000 0000 0100 0000 0100 0000 0000
> > 0000100 0000 0000 0000 0000
> >
> > luac5.3 32-bit:
> > 0000000 4c1b 6175 0053 9319 0a0d 0a1a 0404 0804
> > 0000020 7808 0056 0000 0000 0000 0000 0000 7728
> > 0000040 0140 3d07 7473 6964 006e 0000 0000 0000
> > 0000060 0000 0201 0001 0000 0026 0080 0000 0000
> > 0000100 0001 0000 0001 0000 0000 0001 0000 0001
> > 0000120 0000 0000 0000 0001 0000 5f05 4e45 0056
> 
> I get different things on Windows with 5.3 32 bits. When luac produces
> a file it gives the same result. But when outputting to stdout it
> produces different data.
> 
> $ echo|luac - && od -j 0 -N 16 -t x2 luac.out
> 0000000 4c1b 6175 0053 9319 0a0d 0a1a 0404 0804
> 
> $ echo|luac -o - -|od -j 0 -N 16 -t x2
> 0000000 4c1b 6175 0053 9319 0d0d 1a0a 0a0d 0404
> 
> Anyone has similar result on other platorms ?

The bytes in question (9319 0a0d 0a1a or broken 9319 0d0d 1a0a 0a0d)
come from ldump.c:

static void DumpHeader (DumpState *D) {
  DumpLiteral(LUA_SIGNATURE, D);
  DumpByte(LUAC_VERSION, D);
  DumpByte(LUAC_FORMAT, D);
  DumpLiteral(LUAC_DATA, D);   /* <-------------- */
  DumpByte(sizeof(int), D);
  DumpByte(sizeof(size_t), D);
  DumpByte(sizeof(Instruction), D);
  DumpByte(sizeof(lua_Integer), D);
  DumpByte(sizeof(lua_Number), D);
  DumpInteger(LUAC_INT, D);
  DumpNumber(LUAC_NUM, D);
}

lundump.h:
#define LUAC_DATA	"\x19\x93\r\n\x1a\n"

stdout is in text mode on Windows, thus LF newlines are converted to CR+LF:
\n (0x0a) --> \r\n (0x0D 0x0A)

19 93 0d 0a 1a 0a --> 19 93 0d 0d 0a 1a 0d 0a

After swapping bytes (19 93 --> 93 19 and so on) you get exactly the
broken stdout output of luac.

Uhh, it turns out using of stdout is dangerous on Windows.

Almost 10 years ago patching of Lua has been proposed:
http://lua-users.org/lists/lua-l/2006-10/msg00650.html

And exactly 1 year ago the same issue is still problem in Lua:
http://lua-users.org/lists/lua-l/2015-05/msg00358.html

> > luac5.3 64-bit:
> > 0000000 4c1b 6175 0053 9319 0a0d 0a1a 0804 0804
> > 0000020 7808 0056 0000 0000 0000 0000 0000 7728
> > 0000040 0140 3d07 7473 6964 006e 0000 0000 0000
> > 0000060 0000 0201 0001 0000 0026 0080 0000 0000
> > 0000100 0001 0000 0001 0000 0000 0001 0000 0001
> > 0000120 0000 0000 0000 0001 0000 5f05 4e45 0056
> > ---
> >  configure.ac | 8 ++++++--
> >  1 file changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/configure.ac b/configure.ac
> > index 1842021..5eb0a47 100644
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -1554,8 +1554,12 @@ then
> >    AS_IF([test "${LUAC}" = "false"], [
> >      AC_MSG_ERROR([Could not find the LUA byte compiler.])
> >    ])
> > -  AS_IF([test -d "${CONTRIB_DIR}" -a -f "${CONTRIB_DIR}/lib/liblua.a" -a `echo|${LUAC} -o - -|od -j 8 -N 2 -t x2|head -n 1|tr -s ' '|cut -d' ' -f2` != 0404], [
> > -    AC_MSG_ERROR([You need 32-bits luac when using lua from contrib.])
> > +  AS_IF([test -d "${CONTRIB_DIR}" -a -f "${CONTRIB_DIR}/lib/liblua.a"], [
> > +    lua_ver=`echo|${LUAC} -o - -|od -j 4 -N 2 -t x2|head -n 1|tr -s ' '|cut -d' ' -f2`
> > +    AS_IF([test ${lua_ver} -lt 53], [lua_abi_offset=8], [lua_abi_offset=12])
> > +    AS_IF([test `echo|${LUAC} -o - -|od -j ${lua_abi_offset} -N 2 -t x2|head -n 1|tr -s ' '|cut -d' ' -f2` != "0404"], [
> > +      AC_MSG_ERROR([You need 32-bits luac when using lua from contrib.])
> > +    ])
> >    ])
> >  fi
> >  AM_CONDITIONAL(BUILD_LUA, [test "${have_lua}" = "yes"])
> > --
> > 2.7.4

Regards,
Andrey


More information about the vlc-devel mailing list