nimble-code / Cobra

An interactive (fast) static source code analyzer
139 stars 31 forks source link

Assertion `strlen(c)+strlen(f->s) < n' failed. #3

Closed keck-in-space closed 4 years ago

keck-in-space commented 5 years ago

When running Cobra with the following command

cobra -v -f p10 *.[ch]

I get

cobra: cobra_prim.c:236: check_args: Assertion `strlen(c)+strlen(f->s) < n' failed.
Aborted (core dumped)
nimble-code commented 5 years ago

I'll check it out -- thanks for reporting this! -g

On Fri, Sep 6, 2019 at 11:34 AM David Keck notifications@github.com wrote:

When running Cobra with the following command

cobra -v -f p10 *.[ch]

I get

cobra: cobra_prim.c:236: check_args: Assertion `strlen(c)+strlen(f->s) < n' failed. Aborted (core dumped)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/nimble-code/Cobra/issues/3?email_source=notifications&email_token=AK6L6ILP2YJPZ4UDE73BMCTQIKPCZA5CNFSM4IUL4PRKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HJ35VKQ, or mute the thread https://github.com/notifications/unsubscribe-auth/AK6L6IJ6MGIJG3BH36G4KPDQIKPCZANCNFSM4IUL4PRA .

nimble-code commented 5 years ago

i cannot reproduce this error -- i tried on Windows and on Ubuntu linux. what platform did you use when you saw this problem?

keck-in-space commented 5 years ago

Ah. That could be the issue. I'm running Ubuntu in Windows Subsystem for Linux on Windows 10.

keck-in-space commented 5 years ago

If it's useful to you I ran all of predefined checks and here's a list of the predefined checks that have the error.

The JPL and P10 checks get somewhat far before having the issue. I may switch to Cygwin so I can run this without error for now and if I do I'll let you know if I see the issue again.

Thank you

keck-in-space commented 5 years ago

I just tested this with Cygwin and got the same error. Perhaps its something in the code I'm analyzing.

nimble-code commented 5 years ago

is it always in the same file, and does it also give the error if you give it just that one file? I'm running a newer version of Cobra, so it could be that this is something that I fixed before. I'll retest with the version from the distribution though. (I tried it on the cobra sources themselves and didn't see the assertion failure happening.

keck-in-space commented 5 years ago

It appears to occur in multiple files in the source, but not in every file. Every time I give it those specific files, however, I do get the error.

nimble-code commented 5 years ago

retested with the cobra.exe from the distribution on the cobra sources *.[ch] but don't get the problem. if you have a sample file that triggers the bug, can you send it to me? gerard.holzmann@gmail.com

keck-in-space commented 5 years ago

Interesting. I can't send any code from the project I'm analyzing, unfortunately, but I can see if I can generate a file that produces this issue. Thanks!

keck-in-space commented 5 years ago

Interestingly this is enough to cause the assertion failure with p10 in Cygwin.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main()
{
   return 0; 
}
nimble-code commented 5 years ago

fascinating -- it doesn't happen on my system, and i don't see how it could... it is triggered by the call to scope_check in the definition of p10_rule6b(rn) on line 210 in file rules/p10.def -- the arguments $FLAGS and $ARGS are expanded in the function 'check_args()' with some counting to make sure we allocate enough space for the expanded system call. it's easy enough to just double the amount allocated and be done with it, but i'd like to understand what setting could possibly trigger this. would you be willing to add a printf at the start of check_args to just print the parameters passed in? (that's s and c_base) -- that'll help to nail this down.

keck-in-space commented 5 years ago

I'm guessing there was something wrong with my test setup because I can no longer generate the error on the simple case I had above. The error still occurs with the original source files that I have been analyzing. Here is the output with the printf you mentioned.

s: s c_base: /
assertion "strlen(c)+strlen(f->s) < n" failed: file "cobra_prim.c", line 237, function: check_args
Aborted (core dumped)
nimble-code commented 5 years ago

if the argument s is really equal to "s" then the 3 strstr calls at the start of the function will all return 0 and the function will return s immediately, and not get to the failing assertion....

On Fri, Sep 13, 2019 at 7:09 AM David Keck notifications@github.com wrote:

I'm guessing there was something wrong with my test setup because I can no longer generate the error on the simple case I had above. The error still occurs with the original source files that I have been analyzing. Here is the output with the printf you mentioned.

s: s c_base: / assertion "strlen(c)+strlen(f->s) < n" failed: file "cobra_prim.c", line 237, function: check_args Aborted (core dumped)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/nimble-code/Cobra/issues/3?email_source=notifications&email_token=AK6L6IKADF7DMR25EEBGUHLQJONILA5CNFSM4IUL4PRKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6VED7Q#issuecomment-531251710, or mute the thread https://github.com/notifications/unsubscribe-auth/AK6L6IN4BKXWNG3KIUA6P7DQJONILANCNFSM4IUL4PRA .

keck-in-space commented 5 years ago

Very curious. Are there any other printfs I can grab that would help figure out the cause of this assertion failure?

nimble-code commented 5 years ago

No that printf should tell us what's going on -- but the value of argument 's' has to contain the macros that are expanded, or else the assertion isn't reached. can you check?

nimble-code commented 4 years ago

closing this issue as not reproducible -- please resubmit if it reoccurs and there's an example to reproduce the behavior

keck-in-space commented 4 years ago

I just tried the latest Cobra release v3.1 on the source of FreeRTOS v10.2.1 and I see the error there.

:p10_rule6b
assertion "strlen(c)+strlen(f->s) < n" failed: file "cobra_prim.c", line 225, function: check_args
Aborted (core dumped)
keck-in-space commented 4 years ago

I suppose it's possible I've misconfigured Cobra. All I have to do to see the error with FreeRTOS is place the contents of the Source/include directory into the Source directory and run the p10 rule set on that folder.

nimble-code commented 4 years ago

Ah, yes, I can reproduce it now. Thanks! I'll look into it and will fix it.

keck-in-space commented 4 years ago

Thanks! I was happy to find an open-source code base where the error occurred. Best of luck! I'm still thinking it is possibly due to my setup (cygwin on win10). Let me know if I can provide more information.

nimble-code commented 4 years ago

I believe this is now fixed. new files uploaded. The underlying issue was that when none of the input files can successfully be preprocessed (which the p10 rules do by default), then there are 0 files to process, which led to the unusual case trapped in the assertion. this is now fixed -- cobra will now just terminate with an error, after reporting all the compiler errors on the incomplete input provided. to prevent the default invocation of cpp in the p10 rules, add the command-line argument -nocpp as in, for instance: cobra -I./include -nocpp -f p10 *.c

keck-in-space commented 4 years ago

I can confirm the issue is fixed in the latest commit for my source as well. Thanks so much!