-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Folded Ref Structs with Fixed Buffer causes System.AccessViolationException. #67799
Comments
|
@VitaliiBachulis I can see the IL generated for the |
If you set |
The semantic meaning for constructors of folded ref structs - not instantiation (allocation) of it's fields (ref struct) on the evaluation stack - but it's initialization. |
If you analyze IL code generated for instantiation of the Room struct. We can see that it generated - what is expected.
|
but the constructor of the Room struct does not follow the same initialization logic in it's implementation.
|
sharplab does not allow to understand the problem. I have spent a lot of time using ILDasm and WinDbg to clarify what happens. |
I think that for the Value types the newobj is acceptable in case of creating and than boxing a value type. Are I wrong? |
At first time I was fooled that problem is in a JIT compiler (because program crashed only in QuickJit and looked as worked fine in Optimized compilation). But after apply an IL fix - I checked all variants and in all cases JIT generated ASM correctly and program works fine. Also in the current state the program does not always crashes even in QuickJit - depending on the size of fixed-sized buffer and iteration counts - if it was small - I saw a false positive behavior. Current IL codegen makes JIT compiler madness :(. It produces 2 different semantic logic (ASMx64 result) for the same MSIL depending on the level of JIT compilation and all have incorrect beheviour. |
For really see the picture the best way is to look at process virtual addresses values on an evaluation stack. I did step by step ASM's commands running and watched the result of commands on the stack. |
I think this case could be included in a test plan for: ##67826 |
I believe the current behavior is by design. The instance field initializer corresponds to an assignment to the field (see language specification), and since the field is a |
@cston let me discuss with you:
You are right that "As a result, the Span<> in the copied value refers to the fixed buffer in the original instance." But it also has side effects:
P.S. They are rhetorical questions. |
The compiler will in some cases initialize
I agree this is a case we need to consider when thinking about inline arrays. The inline array case though is very different because it's entirely safe code. It is up to the compiler to ensure that the code doesn't end up referring to temporary artifacts. I'm pretty sure the existing lifetime rules account for this but it's a good case to make sure we test. The case you present here is very different. It's using This particular behavior is "By Design" in that we do not guarantee, nor do we wish to guarantee, that copies do not happen in the |
Version Used: C# 11
Steps to Reproduce:
Compiler has to generate:
The text was updated successfully, but these errors were encountered: