sb-alien, cl:boolean, and the C _Bool type

Bug #914612 reported by Attila Lendvai
12
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
New
Undecided
Unassigned

Bug Description

1.0.55 x86-64

(define-alien-type-translator boolean (&optional (bits sb!vm:n-word-bits))
  ...)

but on the C side:

typedef int boolean;

so, the C side is 32 bits, while the default size for the type translator is 64 bits.

this is very misleading and results in e.g.:

(setf (sb-alien:extern-alien "lose_on_corruption_p" boolean) t)

for a user who doesn't look twice it looks completely fine, but it silently corrupts memory.

Tags: alien
Revision history for this message
Attila Lendvai (attila-lendvai) wrote :

err, sorry, scratch that. it's due to something i call, and that used to work, but not anymore.

CL-USER> (sb-alien:extern-alien "lose_on_corruption_p" boolean)
T
CL-USER> (sb-alien:extern-alien "internal_errors_enabled" boolean)
T
CL-USER> (setf (sb-alien:extern-alien "lose_on_corruption_p" boolean) t)
T
CL-USER> (sb-alien:extern-alien "internal_errors_enabled" boolean)
NIL

i guess the way boolean type is given to extern-alien results in a wider setf than what C uses for booleans... any hints on how to do it the right way?

this is on linux x64.

Revision history for this message
Paul Khuong (pvk) wrote :

C runtime: typedef int boolean;

boolean alien type definition: (define-alien-type-translator boolean (&optional (bits sb!vm:n-word-bits)) ...)

Use int, or pass the right number of bit (32) on x86-64.

Changed in sbcl:
status: New → Invalid
Revision history for this message
Attila Lendvai (attila-lendvai) wrote :

any way to add a warning or some other hint when misusing cl:boolean the way I used with aliens?

this misuse was already suggested by someone else to me, seems like it's a prevalent mistake...

summary: - internal_errors_enabled is NIL in a save-lisp-and-die executable image
- long after startup
+ the default bit size of the boolean alien-type-translator doesn't match
+ the C side
description: updated
Changed in sbcl:
status: Invalid → New
Revision history for this message
Paul Khuong (pvk) wrote : Re: the default bit size of the boolean alien-type-translator doesn't match the C side

This type isn't about the C runtime. If it were, it wouldn't be exported. There is no one boolean type in C. What there are are integer values that are used for their zero/non-zero-ness. In that context, there is no correct width.

It might hit more common cases right if we default the width that for int instead of long, but, either way, I don't see why it would follow any change to the runtime.

Revision history for this message
Attila Lendvai (attila-lendvai) wrote :

ok, i see that it can't really be made to Just Work.

but if that's the case, then how about some steps to make it less of a trap? e.g. how about making the size argument of the boolean type translator mandatory instead of defaulting to a value that is mostly wrong?

tags: added: alien
Revision history for this message
Attila Lendvai (attila-lendvai) wrote :

copying over the description of another bug that i once again unwittingly reported 4 years later.

on linux x64:

(sb-alien:alien-size cl:boolean) => 64
sizeof(_Bool) => 1

i would expect sb-alien either not to define a boolean type, or map it to C's _Bool.

summary: - the default bit size of the boolean alien-type-translator doesn't match
- the C side
+ sb-alien, cl:boolean, and the C _Bool type
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.