Bug 8078 - [review] Shortcircuiting does not work as expected
Summary: [review] Shortcircuiting does not work as expected
Status: RESOLVED FIXED
Alias: None
Product: Spamassassin
Classification: Unclassified
Component: spamassassin (show other bugs)
Version: SVN Trunk (Latest Devel Version)
Hardware: All All
: P2 blocker
Target Milestone: 4.0.0
Assignee: SpamAssassin Developer Mailing List
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-11-29 22:26 UTC by Kevin A. McGrail
Modified: 2022-12-11 07:32 UTC (History)
4 users (show)



Attachment Type Modified Status Actions Submitter/CLA Status
Possible fix for the issue patch None Giovanni Bechis [HasCLA]
skip meta tests asap patch None Giovanni Bechis [HasCLA]

Note You need to log in before you can comment on or make changes to this bug.
Description Kevin A. McGrail 2022-11-29 22:26:54 UTC
As discussed on list, users found that comparing 3.4.6 to 4.0 SVN that shortcircuiting no longer behaved as expected.

Giovanni confirmed that "r1904981 is the commit that is causing this behavior."

This is a blocker for 4.0.0 IMO
Comment 1 Giovanni Bechis 2022-11-30 10:53:30 UTC
Created attachment 5861 [details]
Possible fix for the issue
Comment 2 Henrik Krohns 2022-11-30 13:35:58 UTC
Not sure why do_meta_tests couldn't be run, any fix will need detailed explanation anyway.. I'll try also looking soon when I have time..
Comment 3 Henrik Krohns 2022-12-04 13:29:11 UTC
There is nothing mysterious about this issue.

Original complaint was that shortcircuiting RCVD_IN_VALIDITY_CERTIFIED results in MISSING_* rules hitting.

If you look at the MISSING_* rules, they are just dumb metas referring to various exists: rules.

meta MISSING_FROM              !__HAS_FROM
header    __HAS_FROM              exists:From

Network rules are run at -100 priority, so if they receive a quick response and shortcircuiting is activated, rules like __HAS_FROM are not even run yet since default rule priority 0 is not reached.

Since we decided to let meta rules evaluate with unrun dependencies, everything works as designed.

Logical fix is to adjust priority for all commonly referred rules in metas. Lowest priority seen in stock ruleset is -1000, for example shortcircuiting USER_IN_WELCOMELIST has the same effect. So __HAS_FROM and others need to be set at -2000 for example. This requires no code commits, only rule changes, I'll change the necessary ones.
Comment 4 Henrik Krohns 2022-12-04 13:47:09 UTC
Sending        rules/20_compensate.cf
Sending        rules/20_head_tests.cf
Sending        rules/20_html_tests.cf
Sending        rules/20_meta_tests.cf
Transmitting file data ....done
Committing transaction...
Committed revision 1905734.
Comment 5 Henrik Krohns 2022-12-04 15:36:21 UTC
Ok I'll reopen this myself per list discussion to try to move things along.

Looking at do_meta_tests(), I think it doesn't have any dependencies to anything aside from start_rules/Reuse, which I guess is never used with shortcircuiting. So not running it looks fine, and should prevent evaluating any rules with unexpected meta results. I'd leave the priority -2000 changes around in case someone is using older trunk.

I would add deadline check, and probably move this before start_rules is called, since we don't want to start anything more?

sub do_meta_tests {
  my ($self, $pms, $priority, $finish) = @_;

  return if $pms->{deadline_exceeded} || $pms->{shortcircuited};


+1, vote to commit?
Comment 6 Bill Cole 2022-12-04 16:05:32 UTC
+1 to commit that conditional return at line 283 of lib/Mail/SpamAssassin/Plugin/Check.pm

Thanks, Henrik.
Comment 7 Henrik Krohns 2022-12-04 17:47:45 UTC
(In reply to Bill Cole from comment #6)
> 
> Thanks, Henrik.

Please redirect thanks to Giovanni, I'm just confirming his solution was correct. :-)
Comment 8 Giovanni Bechis 2022-12-05 15:07:25 UTC
Created attachment 5863 [details]
skip meta tests asap

patch as suggested in
https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8078#c5
Comment 9 Kevin A. McGrail 2022-12-05 17:48:20 UTC
+1 to the latest patch.  Do we need to revert the few rule changes that were made recently to work on this issue as well?
Comment 10 Bill Cole 2022-12-05 18:43:53 UTC
(In reply to Kevin A. McGrail from comment #9)
> +1 to the latest patch.  Do we need to revert the few rule changes that were
> made recently to work on this issue as well?

No, those priority changes to a few very low-cost header presence rules make sense with or without the patch.
Comment 11 Henrik Krohns 2022-12-05 19:14:25 UTC
As said the rule priority fixes are useful "in case someone is using older trunk".
Comment 12 Henrik Krohns 2022-12-05 19:16:06 UTC
Enough votes so commit and back to release schedule:

Sending        trunk/lib/Mail/SpamAssassin/Plugin/Check.pm
Transmitting file data .done
Committing transaction...
Committed revision 1905778.
Comment 13 DC 2022-12-10 20:56:05 UTC
FWIW, this maybe causing some unexpected/unintended behavior for 3.x installations...

I manage SA for two sites which run 3.4.0 & 3.4.2 respectively.  Starting the day they got the updated rules (revision 1905734) the sa-compile process began to issue several warnings:

-------------------------------
body_neg2000.c: In function ‘XS_Mail__SpamAssassin__CompiledRegexps__body_neg2000_scan’:
body_neg2000.xs:45:17: warning: variable ‘pend’ set but not used [-Wunused-but-set-variable]
  unsigned char *pend;
                 ^
body_neg2000.xs:43:17: warning: unused variable ‘cursor’ [-Wunused-variable]
  unsigned char *cursor;
                 ^
body_neg2000.xs:42:8: warning: unused variable ‘match’ [-Wunused-variable]
  char *match;
        ^
body_neg2000.c: At top level:
body_neg2000.xs:17:3: warning: ‘split_and_add’ defined but not used [-Wunused-function]
   split_and_add (AV *results, char *match)
   ^
Running Mkbootstrap for Mail::SpamAssassin::CompiledRegexps::body_neg2000 ()
-------------------------------


After a couple of hours of reverse engineering how this all works, I determined that it was due to these new -2000 priority designations -- more specifically the two body rules.  Thanks to the comment in that revision, it lead me here.

What seems to happen is that the BodyRuleBaseExtractor produces an empty list of "bases" because of the nature of those two rules not being typical string matches.  As a result of that, rule2xs in sa-compile creates the .xs file with the PREINIT section that declares those variables.  However, because $numscans is still 0 (due to no bases), it skips the loop that builds $xscode that would normally reference them.  As such, warnings about unused/etc.


For anyone else affected by this, a workaround is to ensure there is at least one body rule that will get picked up by sa-compile.  Probably a bad idea, but my quick'n'dirty fix was to create one like this:

body IGNOREME_NEG_2000 /neg2000/
score IGNOREME_NEG_2000 0.001
priority IGNOREME_NEG_2000 -2000
describe IGNOREME_NEG_2000 Just a workaround, see Bug 8078
Comment 14 Henrik Krohns 2022-12-11 07:32:05 UTC
(In reply to DC from comment #13)
> FWIW, this maybe causing some unexpected/unintended behavior for 3.x
> installations...
> 
> I manage SA for two sites which run 3.4.0 & 3.4.2 respectively.  Starting
> the day they got the updated rules (revision 1905734) the sa-compile process
> began to issue several warnings:

Thanks for analyzing. It seems on your system compiler uses many -W flags, so I could only reproduce it manually adding them. From what I see, this is only informational warnings and in no way fatal. I've created Bug 8091 if someone wants to look at it (personally I'd rather get rid of whole sa-compile mess).