This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Build failed for a baremetal

hello,

While I am trying to execute a simple bubble sort program in c as provided as an example in the arm development studio IDE, I am facing 3 errors like as shown below:  (Target and source must be capability)

/bubblesort.c:45:37: error: invalid target type 'uint32_t *' (aka 'unsigned int *') for __cheri_tocap: target must be a capability
    seq_t sequence = (__cheri_tocap seq_t)malloc(sizeof(uint32_t) * SEQ_LEN);

../bubblesort.c:57:33: error: invalid source type 'uint32_t *' (aka 'unsigned int *') for __cheri_fromcap: source must be a capability
    free((__cheri_fromcap void*)sequence);

/bubblesort.c:70:6: error: conflicting types for 'print'
void print (seq_t __cheri_input sequence, int size) {

N.B: I am using LLVM 13.0.0 with Morello support.

  • Neither points to a Morello toolchain. There is nothing wrong with using Morello LLVM, as you currently are as I understand, otherwise there is also the Morello GNU toolchain:

    developer.arm.com/.../arm-gnu-toolchain-for-morello-downloads

  • Many thanks for your reply. I'll try and update you. Can you please suggest from the list of 2 toolchains which one to use for building the project

    https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads

    https://developer.arm.com/Tools%20and%20Software/GNU%20Toolchain

    I am assuming maybe I am not using the correct compiler version.

  • Dropping the cheri.h include and adding stdint.h (for uint32_t) that code compiles just fine with the latest Morello LLVM and CheriBSD: https://cheri-compiler-explorer.cl.cam.ac.uk/z/o9rcr6 (though I would recommend a typedef rather than a #define for defining a new type)

    What's in your cheri.h?

  • Many thanks for your reply, however it is already defined in line highlighted with bold red as shown in the code below but still the build failed:



    #include "cheri.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>



    #define seq_t uint32_t* __capability
    // The __capability qualifier here is not necessary when compiling for "pure" mode
    // (because all pointers have capabilities in "pure" mode), but might be needed
    // if compiling for "hybrid" mode, so has been left in for convenience and clarity.

    #define SEQ_LEN 15

    void populate (seq_t __cheri_output sequence, int size);
    void print (seq_t __cheri_input sequence, int size);
    void sort (seq_t sequence, int size);
    void swap (seq_t first, seq_t second);


    int main (void) {
        printf("Morello bare-metal bubble sort example\n\n");

        seq_t sequence = (__cheri_tocap seq_t)malloc(sizeof(uint32_t) * SEQ_LEN);

        populate(sequence, SEQ_LEN);

        printf("Original sequence:\n  ");
        print(sequence, SEQ_LEN);

        sort(sequence, SEQ_LEN);

        printf("Sorted sequence:\n  ");
        print(sequence, SEQ_LEN);

        free((__cheri_fromcap void*)sequence);

        return 0;
    }

    void populate (seq_t __cheri_output sequence, int size) {
        srand(time(NULL));

        for (int i = 0; i < size; i++) {
            sequence[i] = rand() % (101);
        }
    }

    void print (seq_t __cheri_input sequence, int size) {
        for (int i = 0; i < size; i++) {
            if (i > 0) {
                printf(", ");
            }
            printf("%d", sequence[i]);
        }
        printf("\n");
    }

    void sort (seq_t sequence, int size) {
        for (int i = 0; i < (size - 1); i++) {
            for (int j = 0; j < (size - i - 1); j++) {
                if (sequence[j] > sequence[j+1]) {
                    swap(&sequence[j], &sequence[j+1]);
                }
            }
        }
    }

    void swap (seq_t first, seq_t second) {
        uint32_t temp = *first;
        *first = *second;
        *second = temp;
    }

  • `seq_t` looks to be defined as `typedef uint32_t *seq_t` which, in the hybrid ABI, is not a capability, and so __cheri_tocap/__cheri_fromcap are telling you that you aren't casting to/from a capability respectively. If you want to use a capability you need `typedef uint32_t * __capability seq_t`.