Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a hint for external functions with aggregate return types #1352

Open
mhasel opened this issue Oct 30, 2024 · 0 comments
Open

Add a hint for external functions with aggregate return types #1352

mhasel opened this issue Oct 30, 2024 · 0 comments
Labels
enhancement New feature or request validation candidate for syntactic or semantic validation

Comments

@mhasel
Copy link
Member

mhasel commented Oct 30, 2024

When writing an external C function with an aggregate return type to be used in a PLC program, our API-guideline requires the return type to be passed as a pointer as the first parameter. This, however, is not very intuitive and might easily be missed.
If that is the case, the remaining input parameters are simply written over the memory location at which the return pointer would start, resulting in corrupted data.

To reproduce:

ffi.c:

#include <stdint.h>
#include <stdio.h>
typedef struct {
    int64_t ErrorID;
} FFIReturnType;

FFIReturnType FFIFunc(char * Name, int32_t Flags) {
    // expected output 'some name'
    printf("%s\n", Name);
    FFIReturnType ret = { 0 };
    return ret;
}

header.st:

TYPE
    FFIReturnType: STRUCT
        ErrorID: LINT;
    END_STRUCT
END_TYPE

{external}
FUNCTION FFIFunc: FFIReturnType
VAR_INPUT
    Name: STRING;
    Flags: DINT;
END_VAR
END_FUNCTION

ffi_used_in_plc.st:

FUNCTION main: DINT
VAR
    ret: FFIReturnType;
END_VAR
    ret := FFIFunc('some name', 0);
END_FUNCTION

After compiling with

clang -c -o ffi.o ffi.c -fPIC
clang -shared -o libffi.so ffi.o
LD_LIBRARY_PATH=. plc ffi_used_in_plc.st -lffi -L. -i header.st --linker clang -o a.out

and then running the compiled object with
LD_LIBRARY_PATH=./a.out
the printf statement in the C function will print an empty line (in this case, this might differ depending on input parameters).
This can lead to very frustrating errors to debug, since it is not immediately apparent where one might have made a mistake.

For external functions with aggregate returns, we should additionally add a hint, pointing the user in the right direction in case they use a C library.

@mhasel mhasel added enhancement New feature or request validation candidate for syntactic or semantic validation labels Oct 30, 2024
@mhasel mhasel changed the title Validate parameter count/types for external functions Add a hint for external functions with aggregate return types Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request validation candidate for syntactic or semantic validation
Projects
None yet
Development

No branches or pull requests

1 participant