CTSRD-CHERI / clang

DO NOT USE. Use llvm-project instead
Other
9 stars 8 forks source link

When assigning a negative number to __intcap_t, capability offset is incorrect #110

Closed khilangudka closed 7 years ago

khilangudka commented 7 years ago

The following code: __intcap_t minusfive = -5

results in this IR:

%minusfive = alloca i8 addrspace(200)*, align 32
%0 = call i8 addrspace(200)* @llvm.memcap.cap.offset.set(i8 addrspace(200)* null, i64 4294967291)
store i8 addrspace(200)* %0, i8 addrspace(200)** %minusfive, align 32

Note that the value being set as the offset is -5 in 32-bit two's compliment rather than 64-bit. This causes the CHERI C test intcapmath to fail.

I think this patch fixes this bug: https://gist.github.com/kgudka/b0c7b3c59d1f96ab1545fa257417f81c

davidchisnall commented 7 years ago

I think that this patch will break for numbers that are between 2^31 and 2^32 being assigned to __uintcap_t

khilangudka commented 7 years ago

I can confirm that it works fine with assigning 2^32-1 to a __uintcap_t. The underlying APInt has a bit width of 64 bits.

davidchisnall commented 7 years ago

Please commit the fix then.