[erlang-questions] Wrong large 64bit return-values from Erlang Linkedin Driver

Thijs Terlouw <>
Fri Jun 18 08:41:14 CEST 2010

Hi Mikael,

I have attached a self-contained test. It consists of a few erlang
files (bignum.erl (driver) , gen_linkedin_driver.erl (generic driver)
bignumtest.erl (test to use the driver) and reloader.erl (for easy
debugging)). It also contains a modified version of the C driver. On
my machine (Linux i686) I just run "make". I have Erlang
R13B04: (erts-5.7.5 [source] [smp:4:4] [rq:4] [async-threads:30]
[hipe] [kernel-poll:true]). The file also contains a readme.txt how to
start the test. It uses eunit for the unit test. Which strangely
enough succeeds!

tar -xzvf bignumtest.tar.gz
cd bignumtest

1st test:
"2) 322122547200 =:= 4294967296 => true"
"2) 4294967296 =:= 4294967296 => true"

Obviously something is very wrong here. It prints a value that is not
equal, but when the values are compared, they are equal?

2nd test (unit test)
The first run all unit tests pass. The second run, it will always
result in a core-dump.
no core dump

A colleague (echouzhou) found a bug inside the function
"erts_sint64_to_big" in "erts/emulator/beam/big.c". On lines 1518 and
1528 the *hpp pointer is not incremented correctly. It should be "*hpp
+= 3;" and "*hpp += 2;". After we changed this and recompiled Erlang,
the above tests all succeed without crashing and print the correct
values. I didn't look if the same bug is also in other parts of the


On Thu, Jun 17, 2010 at 8:40 PM, Mikael Pettersson <> wrote:
> Thijs Terlouw writes:
>  > Hello,
>  >
>  > I'm trying to return signed 64bit integers (on a 32bit system) from a
>  > custom Erlang linkedin driver. I try to use the ERL_DRV_INT64 type on
>  > the C-side. Because ERL_DRV_INT64 requires a pointer to ErlDrvSInt64,
>  > I first malloc a buffer, store it there, get a pointer to the result
>  > and then return that pointer. For small values this works great.
>  >
>  > Whenever I use values >134217727 (0x7FFFFFF) it will return wrong
>  > values.
> Can you please provide a self-contained test? I.e., an Erlang
> module that loads your driver, invokes it, and checks the result?
> And whatever build instructions are needed for your driver.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bignumtest.tar.gz
Type: application/x-gzip
Size: 25234 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20100618/843b719f/attachment.bin>

More information about the erlang-questions mailing list