-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Potential C FFI regression #19342
Comments
There are no FFI related changes in 1.6.2. Most likely is that it's an old bug in your wrapper that now got "activated". Please run it under Linux and valgrind and see what it reports. |
Hello, @johnnovak If you get some free time, could you try your example with git bisect? https://nim-lang.org/docs/intern.html#bisecting-for-regressions Thank you in advance. |
That might well be it. I'll investigate it further when I'll have some time.
Yeah, I'll try that too, thanks. |
I have the same problem with 1.6.2. My opengl-based text editor paravim is using tree sitter for syntax highlighting nim files, and starting with 1.6.2 it crashes when opening a nim file. Very easy to reproduce...clone vim_cubed and run...
...results in:
You can also clone pvim and run The error is happening on this line which is being nil-checked, i'm positive that i am not passing nil to that c function. BTW john thank you for illwill, i friggin love it :D |
Cheers, glad you like it :) I quickly had a look at Otherwise, I don't have easy access to a Linux box. I guess I could screw around with valgrind in WSL on Windows 10, but it's been ages since I used valgrind and I don't have high confidences in WSL... So if someone could take a look that would be appreciated. I could try to narrow it further down to a minimal example. |
I'm pretty sure the opengl use is just a coincidence because my crash is happening in the tree sitter code, which has nothing to do with it. In fact i can reproduce the crash in a small unit test without loading any opengl code at all. |
Maybe post a link here to the branch so we can all give it a go? |
Sure, here it is: https://github.com/paranim/paravim/tree/ffi_crash Here's the unit test: https://github.com/paranim/paravim/blob/ffi_crash/tests/test1.nim If i run
But if i |
This is indeed a regression and looks like a serious one. It's related to Nim optimizing object passing to pass-by-reference when the object is larger than 24 bytes, and it started doing that even for Small code to reproduce: type
TSNode* {.bycopy.} = object
pad*: array[25, uint8] # 25 bytes to trigger the bug
proc myproc_export(): TSNode {.cdecl, exportc.} =
discard
proc myproc(): TSNode {.importc: "myproc_export".}
proc parse =
let node = myproc()
echo node.pad[0]
parse() Generated C code for the N_LIB_PRIVATE N_NIMCALL(void, parse__tast_6)(void) {
tyObject_TSNode__I9aoGqn58venVNwvMj7q6Ug node;
tyArray__nHXaesL0DJZHyVS07ARPRA T1_;
nimZeroMem((void*)(&node), sizeof(tyObject_TSNode__I9aoGqn58venVNwvMj7q6Ug));
myproc_export((&node));
nimZeroMem((void*)T1_, sizeof(tyArray__nHXaesL0DJZHyVS07ARPRA));
T1_[0] = dollar___systemZdollars_9(((NU64) (node.pad[(((NI) 0))- 0])));
echoBinSafe(T1_, 1);
} Another example from @oakes's code ( N_LIB_PRIVATE N_NIMCALL(tySequence__lqtQnDnN75JdopcKXmk8aQ**, parse__OOZsrcZparavimZtree95sitter_163)(void* tree, NI lineCount) {
tySequence__lqtQnDnN75JdopcKXmk8aQ** result;
result = (tySequence__lqtQnDnN75JdopcKXmk8aQ**)0;
{
tyObject_TSNode__3gvtIyLCgQ9cRZvYDNYxBjg node;
if (!!((tree == NIM_NIL))) goto LA3_;
nimZeroMem((void*)(&node), sizeof(tyObject_TSNode__3gvtIyLCgQ9cRZvYDNYxBjg));
ts_tree_root_node(tree, (&node));
result = (tySequence__lqtQnDnN75JdopcKXmk8aQ**) newObj((&NTInodes__H5rtJyrA54YseYSwQ9cU8WA_), sizeof(tySequence__lqtQnDnN75JdopcKXmk8aQ*));
asgnRef((void**) (&(*result)), newSeq__OOZsrcZparavimZtree95sitter_175(((NI) (lineCount))));
parse__OOZsrcZparavimZtree95sitter_74(node, &result);
}
LA3_: ;
return result;
} |
The regression seems to be caused by #19115 as the code works fine with it reverted. |
Great, that proves that I'm not going mad :) For the records, I've checked my C wrappers multiple times and couldn't see anything wrong with them. I really don't have much time to mess around with this low-level jiggery-pokery, that's one of the reasons why I'm using Nim and not C/C++ in the first place, so thanks @Yardanico for putting together a test case! :salute: |
This points to a bit more of a general problem in Nim: the options for selecting a particular ABI (for example the Nim ABI for libraries with name mangling etc, or the C ABI, or in the future, other ABI:s) for the codegen are somewhat sprawling - on the one hand, one wants the freedom to use whatever ABI fits the situation, in the compiler - on the other, for interop, it needs to be very specific - |
For a start, NVRO should not be done for |
fix nim-lang#19342; Imo nrvo shouldn't touch importc procs
fix nim-lang#19342; Imo nrvo shouldn't touch importc object even with cdecl
Aren't you guys going to release a patch for this as a matter of urgency? Or at least announce it on the blog that there's a serious regression in 1.6.2? There must be tons of developers interfacing with C code encountering these issues and scratching their heads... |
I'm waiting on #19363 and then 1.6.4 is ready to go. Sorry for the delay. |
Thanks @Araq |
@johnnovak The 1.6.4RC is now available: https://forum.nim-lang.org/t/8839 (the official release should come later this week, if no new regressions are found) |
Cheers, I'll give it a go. |
* nvro don't touch cdecl types; fix #19342 again
* nvro don't touch cdecl types; fix nim-lang#19342 again
Summary
Found a potential C FFI regression with 1.6.2. A library of mine that has been in use for years has started crashing on 1.6.2. Tried turning off the GC with
gc:none
but the crash still happens, hence I think it's an FFI regression.I can't really isolate it easily, but it's very easy to compile and run an example app included in the project that will result in a crash. No dependencies are required whatsoever.
Tested on
Win 10 (x64) and Mac OS X Big Sur 10.6.2 with Nim 1.6.2 and Nim 1.6.0
How to reproduce
gc:none
or any other GC setting -- the exact same crash will happenThe text was updated successfully, but these errors were encountered: