Euphoria
Ticket #788:
c_func returning values with the wrong signedness
-
Reported by
SDPringle
Sep 26, 2012
I recently migrated my EUPHORIA tree to a Linux dev box 32-bit. I found that there were many tests failing that have DATA_EXECUTE and SAFE enabled. It turns out when mmap was called it was often returning a negative value in EUPHORIA. This is simply bad behavior from the EUPHORIA C wrapper for it is declared to return a pointer which should always be positive. The error condition for mmap can be computed by:
power(256,sizeof(dll:C_POINTER))-1
The values returned by mmap is not this error value. They are page-aligned values. I used the following expression to change the signedness and all of the tests passed:
ptr = #1_0000_0000 - ptr
Although a more portable expression could be:
ptr = power(256,sizeof(dll:C_POINTER)) - ptr
mmap is declared with dll:C_POINTER. Yet somehow c_func must be finding the return value type corrupted as a signed integer or it is reading the wrong routine record.
Details
1. Comment by mattlewis
Sep 26, 2012
Here is a test program for this issue (if it prints -1, then it failed):
include std/dll.e
function c()
return -1
end function
integer C = define_c_func( "", call_back( routine_id("c") ), {}, C_POINTER )
? c_func( C, {} )
It looks like our handling of unsigned ints is correct in 4.0, but not in 4.1.
2. Comment by SDPringle
Sep 26, 2012
I get the same results as you do on my Linux32 box. I am marking this as 4.1
3. Comment by SDPringle
Sep 26, 2012
See: hg:euphoria/rev/6346895f0a1a
changeset: 5684:6346895f0a1a user: Shawn Pringle <shawn.pringle@gmail.com> date: Wed Sep 26 16:12:34 2012 -0300 files: source/be_callc.c description:
- make pointer returns unsigned
- fixes ticket 788
4. Comment by SDPringle
Sep 26, 2012
I discovered that though my box is 32-bit, the 'or 32-bit section is not used at least in Linux.
5. Comment by SDPringle
Sep 27, 2012
See: hg:euphoria/rev/4a7e45aa56b8
changeset: 5690:4a7e45aa56b8 parent: 5686:b6d68bf0b012 user: Shawn Pringle <shawn.pringle@gmail.com> date: Thu Sep 27 14:13:16 2012 -0300 files: source/Makefile.gnu source/be_callc.c description:
- this ensures we use the more optimized 32 bit version on Linux/32
- made sure be_callc.c is being compiled with -DARCH switch
- ensure in be_callc.c that ARCH is defined.
- added comments
- ticket 788
6. Comment by mattlewis
Oct 08, 2012
See: hg:euphoria/rev/a18c7f2b8ff4
changeset: 5728:a18c7f2b8ff4 parent: 5698:3dbca84a0458 user: Matt Lewis date: Sat Sep 29 14:09:42 2012 -0400 files: source/be_callc.c description:
- use portable eudouble cast instead of double to prevent loss of precision when returning a C_POINTER value
- fixes ticket 788