C/Ada memory alignment for BIT STRING type #290
maxime-esa
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I inspected the memory layout of the
C
andAda
backends forBIT STRING
s, and it turns out that they do not align – and there is no way they can align with the current design.In the
C
backend theBIT STRING
types are stored and packed in an array of bytes, withMSB0
convention (bit 0 on the left, as per ASN.1 standard):In
Ada
, an array ofBIT
(unsigned int with range 0..1) is generated. And unfortunately while eachBIT
element uses 1 bit making the total size identical betweenC
andAda
, the memory layout is different.In
Ada
, the indexing of theBIT
array does not relate to how it is stored in memory. In practice, the compiler stores the bits inLSB0
and in little endian form (on x86).So if we have:
In C if I set bit 0 and bit 9 to 1, the layout will be :
But in Ada (doing
var.data(1) := 1
andvar.data(10) := 1
) it will be:As you see they don’t match, and there is no way to make them match (I checked all Ada Representation Clauses, but none will solve this).
To understand why, look at the indexing of the Ada array: it’s from 1 to 10, and not from 0 to 10. So already here there is a problem: at no point we say that we want the bit 0 on the left to be set. The index numbering is independent from the memory layout decided by the compiler
We could use an array index from 0 to 9 but that would not solve the issue. Actually I tried to make 100% sure, and I used gdb to inspect the memory layout:
(Note: there is actually one Ada representation clause (
Bit_Order
) that covers the bit ordering, but unfortunately it does not apply to arrays of bits or booleans. Only to records containing a sequence of named Boolean fields. In that case you can indicate in what order you want them stored.)I think the only way to solve the issue is to generate the same structure as in C : an array of bytes containing the packed bits in MSB0 order. It won’t be possible to assign individual bits as easily but it is the only way I see to keep C/Ada memory alignment.
Since I don’t think there are many users of
BIT STRING
s in Ada right now (Opengeode
in TASTE is the main one, and it is transparent for the user anyway) I think the modification will not break too many people.The modification in the type definition is trivial of course but I am not sure about the consequences for the uPER/ACN/XER encoders.
Beta Was this translation helpful? Give feedback.
All reactions