[Vortex] wrong int to ptr conversion in case of 64 bit

Francis Brosnan Blazquez francis at aspl.es
Wed Nov 19 14:16:09 CET 2008


Hi Szilard,

> There is macro to convert integers to pointer value in libaxl 
> (src/axl_config.h):
> 
> #define INT_TO_PTR(integer)   ((axlPointer) (long) ((int)integer))
> 
> In case of amd64 architecture both the size of long and void * are 64 
> bits, but the size of int is 32 bits, so the macro above truncates the 
> higher 32 bits of the ineteger value.

It's imposible to truncate an integer value as you describe since a
integer (int) have always a 32 bits representation (in any arch), as
long as I know.... 

> Here is a test case:
> 
> #include <axl.h>
> 
> #include <stdio.h>
> 
> int
> main()
> {
>   printf("%ld %ld %ld\n", sizeof(int), sizeof(long), sizeof(void *));
> 
>   long l = 0x8765432112345678L;
>   void *p = INT_TO_PTR(l);
> 
>   printf("0x%lx %p\n", l, p);
> 
>   void *p0 = &l;
>   void *p1 = INT_TO_PTR(PTR_TO_INT(p0));
> 
>   printf("%p %p\n", p0, p1);
>   return 0;
> }

In the same direction, you are passing a (long) variable to the
INT_TO_PTR declaration. This is meant to be used with the (int) type not
with (long).

> Possible solution:
> 
> --- configure.ac    (revision 3658)
> +++ configure.ac    (working copy)
> @@ -126,7 +126,7 @@
>   *
>   * @return A \ref axlPointer reference.
>   */
> -#define INT_TO_PTR(integer)   ((axlPointer) ${pti_cast} ((int)integer))
> +#define INT_TO_PTR(integer)   ((axlPointer) ${pti_cast} (integer))
>  
>  /**
>   * @brief Allows to convert a pointer reference (\ref axlPointer),
> 
> 
> Patch above workarounds the following problem (which cause segmentation 
> fault in amd64 architecture) in libvortex:
> 
> --- src/vortex_connection.c    (revision 3658)
> +++ src/vortex_connection.c    (working copy)
> @@ -3057,7 +3057,7 @@
>  
>      /* channel 0 always exists, and cannot be closed. It's closed
>       * when connection (or session) is closed */

However the real bug is here!!

I can believe we have been running with this bug until now. The
following code you are pointing mustn't use INT_TO_PTR over
connection->channels because it is already a pointer!!

That's why you are experimenting problems in amd64 because your
connection->channels pointer is loosing its higher 32bits part.

I've searched for similar declarations (using INT_TO_PTR) and I've found
similar mistake one more time. 

Thanks for reporting Szilard. Cheers!

> -    channel = vortex_hash_lookup (INT_TO_PTR (connection->channels), 
> INT_TO_PTR(channel_num));
> +    channel = vortex_hash_lookup (connection->channels, 
> INT_TO_PTR(channel_num));
>   
>      if (channel == NULL)
>          vortex_log (LOG_DOMAIN, VORTEX_LEVEL_DEBUG, "failed to get 
> channel=%d", channel_num);



-- 
Francis Brosnan Blazquez <francis at aspl.es>
Advanced Software Production Line, S.L.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vortex.patch
Type: text/x-patch
Size: 998 bytes
Desc: not available
Url : http://lists.aspl.es/pipermail/vortex/attachments/20081119/613413c0/attachment.bin 


More information about the Vortex mailing list