PLC-lang / rusty

Structured Text Parser and LLVM Frontend
GNU Lesser General Public License v3.0
181 stars 47 forks source link

Passing parameters to VAR blocks in stateful POUs is not validated and can result in termination of the process by the kernel #1201

Open mhasel opened 1 month ago

mhasel commented 1 month ago

Describe the bug We currently do not validate against passing parameters to function blocks outside of the intended variable blocks.

Given we define a function block without input parameters:

FUNCTION_BLOCK FOO
    VAR
        val : STRING[65536];
    END_VAR
END_FUNCTION_BLOCK

If we then try to pass in a parameter regardless, like so:

FUNCTION main : DINT
VAR
    str: STRING[65536];
    fb: FOO;
END_VAR
    fb(str);
END_FUNCTION

Then we either compile without error (for small enough parameters) or the kernel terminates compilation (large aggregate params) and the console prints Killed.

To Reproduce Copy the above example in a file and then try to compile with plc file.st

Expected behavior Aborting compilation with a diagnostic rather than the kernel killing the process.

Additional context Generating IR for this example will fail in the verify stage:

error[E071]: /tmp/.tmpdVP6Xf/target/demo.st.ll:19:13: error: invalid forward reference to function 'FOO' with wrong type: expected 'void (%FOO*)*' but was 'void (%FOO*, [65537 x i8])*'
  call void @FOO(%FOO* %fb, [65537 x i8] %load_str)
            ^

Querying the kernel with dmesg -T| grep -E -i -B100 'killed process':

[Mon Apr 15 14:48:54 2024] [  54051]  1000 54051  2103862  1761352 14778368    33858             0 plc
[Mon Apr 15 14:48:54 2024] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/,task=plc,pid=54051,uid=1000
[Mon Apr 15 14:48:54 2024] Out of memory: Killed process 54051 (plc) total-vm:8415448kB, anon-rss:7045404kB, file-rss:4kB, shmem-rss:0kB, UID:1000 pgtables:14432kB oom_score_adj:0

Tested in WSL and native Ubuntu