rusty icon indicating copy to clipboard operation
rusty copied to clipboard

Segfault when initializing array field with another array

Open volsa opened this issue 2 years ago • 2 comments

The following needs some further investigation but trying to compile the code returns a segfault on my local M1 machine

TYPE myStruct : STRUCT
    var1 : DINT;
    var2 : ARRAY[1..2] OF DINT;
  END_STRUCT
END_TYPE

FUNCTION main : DINT
VAR
  arr_init	: ARRAY[1..2] OF DINT := (1, 2);
  x : myStruct;
END_VAR
  // x	:= (var1 := 1, var2 := (1, 2));   // Does not segfault
  // x	:= (var1 := 1, var2 := arr_init); // Does segfaults
END_FUNCTION

volsa avatar Jul 24 '23 15:07 volsa

I ran a stacktrace on this and it looks like this happens during optimization - the code compiles when passing ´-Onone´.

The segfault happens regardless of compiling with or without debug-info, and also happens when using the release build of rusty.

stacktrace:

Core was generated by `./target/debug/plc ./target/demo.st --debug'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000055777aa0fccf in llvm::MetadataTracking::track(void*, llvm::Metadata&, llvm::PointerUnion<llvm::MetadataAsValue*, llvm::Metadata*>) ()
[Current thread is 1 (Thread 0x7f22c76ac100 (LWP 162536))]
warning: Missing auto-load script at offset 0 in section .debug_gdb_scripts
of file /home/michael/dev/rusty/target/debug/plc.
Use `info auto-load python-scripts [REGEXP]' to list them.
(gdb) bt
#0  0x000055777aa0fccf in llvm::MetadataTracking::track(void*, llvm::Metadata&, llvm::PointerUnion<llvm::MetadataAsValue*, llvm::Metadata*>) ()
#1  0x000055777d0481bb in llvm::InstCombinerImpl::run() ()
#2  0x000055777d049a8c in combineInstructionsOverFunction(llvm::Function&, llvm::InstructionWorklist&, llvm::AAResults*, llvm::AssumptionCache&, llvm::TargetLibraryInfo&, llvm::TargetTransformInfo&, llvm::DominatorTree&, llvm::OptimizationRemarkEmitter&, llvm::BlockFrequencyInfo*, llvm::ProfileSummaryInfo*, unsigned int, llvm::LoopInfo*) ()
#3  0x000055777d049620 in llvm::InstCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) ()
#4  0x000055777c137c4d in llvm::detail::PassModel<llvm::Function, llvm::InstCombinePass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) ()
#5  0x000055777aa3357e in llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) ()
#6  0x000055777b563d7d in llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&)
    ()
#7  0x000055777aa364c1 in llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) ()
#8  0x000055777b563bad in llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) ()
#9  0x000055777aa322ee in llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) ()
#10 0x000055777c2fdcc5 in LLVMRunPasses ()
#11 0x000055777aee5758 in inkwell::module::Module::run_passes (self=0x7ffcba6d5aa0, 
    passes=..., machine=0x7ffcba6d5138, options=...) at src/module.rs:1491
#12 0x000055777a7ec577 in rusty::codegen::{impl#3}::persist_to_obj::{closure#2} (it=...)
    at src/codegen.rs:334
#13 0x000055777a7404b0 in core::result::Result<inkwell::targets::TargetMachine, rusty::diagnostics::Diagnostic>::and_then<inkwell::targets::TargetMachine, rusty::diagnostics::Diagnostic, (), rusty::codegen::{impl#3}::persist_to_obj::{closure_env#2}> (self=..., 
    op=...)
    at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/result.rs:1340
#14 0x000055777a711a92 in rusty::codegen::GeneratedModule::persist_to_obj (
    self=0x7ffcba6d5aa0, output=..., reloc=inkwell::targets::RelocMode::PIC, 
    target=0x557777f7c078, optimization_level=rusty::OptimizationLevel::Default)
    at src/codegen.rs:332
#15 0x000055777a711c62 in rusty::codegen::GeneratedModule::persist_to_shared_pic_object
    (self=0x7ffcba6d5aa0, 
    output=<error reading variable: Cannot access memory at address 0x2>, 
    target=0x557777f7c078, optimization_level=rusty::OptimizationLevel::Default)
    at src/codegen.rs:376
#16 0x000055777a711291 in rusty::codegen::GeneratedModule::persist (
    self=0x7ffcba6d5aa0, output_dir=..., output_name=..., 
    format=rusty::output::FormatOption::Static, target=0x557777f7c078, 
    optimization_level=rusty::OptimizationLevel::Default) at src/codegen.rs:274

edit: Also unable to generate IR from this.

     demo.ll:20:38: error: invalid use of function-local name
     store %myStruct { i32 1, [2 x i32] %load_arr_init }, %myStruct* %x, align 4

mhasel avatar Aug 02 '23 14:08 mhasel