FreeRTOS / Lab-Project-FreeRTOS-FAT

This repository contains FreeRTOS+FAT source code, which could be consumed as a submodule.
https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/index.html
MIT License
86 stars 51 forks source link

Compiler Warnings on 64-Bit Build #20

Closed cmdrf closed 3 years ago

cmdrf commented 3 years ago

Yes, there are 64-bit MCUs out there. You might also want to compile this project on a Desktop PC for testing purposes.

FreeRTOS-FAT/ff_fat.c: In function 'FF_FindEndOfChain':
FreeRTOS-FAT/ff_fat.c:717:60: warning: conversion from 'long unsigned int' to 'uint32_t' {aka 'unsigned int'} changes value from '18446744073709551615' to '4294967295' [-Woverflow]
  717 |         ulFatEntry = FF_TraverseFAT( pxIOManager, ulStart, ~0UL, &xError );
      |                                                            ^~~~
In file included from <redacted>:
../FreeRTOS-FAT/include/ff_stdio.h: In function 'stdioSET_ERRNO':
../FreeRTOS-FAT/include/ff_stdio.h:131:82: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
  131 |         vTaskSetThreadLocalStoragePointer( NULL, ffconfigCWD_THREAD_LOCAL_INDEX, ( void * ) ( iErrno ) );
      |                                                                                  ^
../FreeRTOS-FAT/include/ff_stdio.h: In function 'stdioGET_ERRNO':
../FreeRTOS-FAT/include/ff_stdio.h:139:16: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  139 |         return ( int ) pvResult;
      |                ^
../FreeRTOS-FAT/include/ff_stdio.h: In function 'stdioSET_FF_ERROR':
../FreeRTOS-FAT/include/ff_stdio.h:151:85: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
  151 |         vTaskSetThreadLocalStoragePointer( NULL, stdioFF_ERROR_THREAD_LOCAL_OFFSET, ( void * ) ( iFF_ERROR ) );
      |                                                                                     ^
../FreeRTOS-FAT/include/ff_stdio.h: In function 'stdioGET_FF_ERROR':
../FreeRTOS-FAT/include/ff_stdio.h:163:16: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  163 |         return ( FF_Error_t ) pvResult;
      |                ^

The first one looks particularly bad, but it works regardless.

Compiler:

$ riscv64-unknown-elf-gcc -v
Using built-in specs.
COLLECT_GCC=<redacted>/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-apple-darwin/bin/riscv64-unknown-elf-gcc
COLLECT_LTO_WRAPPER=<redacted>/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-apple-darwin/bin/../libexec/gcc/riscv64-unknown-elf/10.2.0/lto-wrapper
Target: riscv64-unknown-elf
Configured with: /scratch/jenkins/workspace/tpp-freedom-tools/tpp01--build-binary-packages--parameterized/obj/x86_64-apple-darwin/build/riscv64-unknown-elf-gcc/riscv-gcc/configure --target=riscv64-unknown-elf --prefix=/scratch/jenkins/workspace/tpp-freedom-tools/tpp01--build-binary-packages--parameterized/obj/x86_64-apple-darwin/install/riscv64-unknown-elf-gcc-10.2.0-2020.12.8-x86_64-apple-darwin --with-pkgversion='SiFive GCC-Metal 10.2.0-2020.12.8' --with-bugurl=https://github.com/sifive/freedom-tools/issues --disable-shared --disable-threads --enable-languages=c,c++ --enable-tls --with-newlib --with-sysroot=/scratch/jenkins/workspace/tpp-freedom-tools/tpp01--build-binary-packages--parameterized/obj/x86_64-apple-darwin/install/riscv64-unknown-elf-gcc-10.2.0-2020.12.8-x86_64-apple-darwin/riscv64-unknown-elf --with-native-system-header-dir=/include --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libgomp --disable-nls --disable-tm-clone-registry --src=../riscv-gcc --with-system-zlib --enable-checking=yes --enable-multilib --with-abi=lp64d --with-arch=rv64imafdc CFLAGS=-O2 CXXFLAGS=-O2 'CFLAGS_FOR_TARGET=-Os -mcmodel=medany' 'CXXFLAGS_FOR_TARGET=-Os -mcmodel=medany'
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (SiFive GCC-Metal 10.2.0-2020.12.8) 
htibosch commented 3 years ago

Thank you Fabian, for reporting this.

I must admit that I haven't had the chance yet to try out FreeRTOS+FAT on a 64-bit platform. I'm very glad to hear that it works.

Within the library, there are two occurrences of ~0UL. Those should be replaced with ~0U. After that it will compile properly, I assume?

Would you mind to compile the following code on your platform:

/* The errno is stored in a thread local buffer. */
    static portINLINE void stdioSET_ERRNO( int iErrno )
    {
        /* Local storage pointers can only store pointers. This function
         * wants to store a signed errno value, which needs a cast. */
        /* Cast from an integer to a signed value of a pointer size. */
        intptr_t xErrno = ( intptr_t ) iErrno;
        /* Cast from a numeric value to a pointer. */
        void * pvValue = ( void * ) ( xErrno );

        vTaskSetThreadLocalStoragePointer( NULL, ffconfigCWD_THREAD_LOCAL_INDEX, pvValue );
    }

    static portINLINE int stdioGET_ERRNO( void )
    {
        void * pvResult;

        pvResult = pvTaskGetThreadLocalStoragePointer( ( TaskHandle_t ) NULL, ffconfigCWD_THREAD_LOCAL_INDEX );
        /* Cast from a pointer to a number of the same size. */
        intptr_t xErrno = ( intptr_t ) pvResult;
        /* Cast it to an integer. */
        int iValue = ( int ) ( xErrno );
        return iValue;
    }

/*
 * Store the FreeRTOS+FAT error code, which provides more detail than errno.
 */
    static portINLINE void stdioSET_FF_ERROR( FF_Error_t iFF_ERROR )
    {
        /* Cast it to an unsigned long. */
        uint32_t ulError = ( uint32_t ) iFF_ERROR;
        /* Cast it to a number with the size of a pointer. */
        uintptr_t uxErrno = ( uintptr_t ) ulError;
        /* Cast it to a void pointer. */
        void * pvValue = ( void * ) ( uxErrno );
        vTaskSetThreadLocalStoragePointer( NULL, stdioFF_ERROR_THREAD_LOCAL_OFFSET, pvValue );
    }

/*
 * Read back the FreeRTOS+FAT error code, which provides more detail than
 * errno.
 */
    static portINLINE FF_Error_t stdioGET_FF_ERROR( void )
    {
        void * pvResult;

        pvResult = pvTaskGetThreadLocalStoragePointer( NULL, stdioFF_ERROR_THREAD_LOCAL_OFFSET );
        /* Cast it to an integer with the same size as a pointer. */
        intptr_t uxErrno = ( intptr_t ) pvResult;
        /* Cast it to a int32_t. */
        FF_Error_t xError = ( FF_Error_t ) uxErrno;

        return xError;
    }

I test compiled this code for a 64-bit Ultrascale. Could you also try to compile it?

cmdrf commented 3 years ago

Thanks! Yes, it compiles without warnings.

htibosch commented 3 years ago

I just submitted PR #21 “FAT: Changes for 64-bit platforms and solved compiler warnings”, which should solve several compiler issues. It also contains the changes as proposed here above. Thanks a lot @cmdrf for reporting. Hein

gshvang commented 3 years ago

PR has been raised for the issue PR #21 “FAT: Changes for 64-bit platforms and solved compiler warnings” hence closing the issue. Please reach out to us if you have any further questions.