You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2024/09/04 20:02:10 UTC

(couchdb) branch main updated: Avoid scanning functions with non-deterministic behavior

This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/main by this push:
     new 54bc60b73 Avoid scanning functions with non-deterministic behavior
54bc60b73 is described below

commit 54bc60b7319bf0d7b6e25ff4c2de2547ecc7fa45
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Wed Sep 4 15:16:29 2024 -0400

    Avoid scanning functions with non-deterministic behavior
    
    A large number of false positives seen so far have to deal with users using
    `Date.now()` or `Math.random()` in the map or reduce function bodies. Needless
    to mention those will always produce false positive results.
    
    Attempt a heuristic approach to avoid scanning views which contain the most
    common sources of determinism found so far. These include current date fetching
    and `Math.random()`.
---
 .../src/couch_quickjs_scanner_plugin.erl           | 20 ++++++++--
 .../test/couch_quickjs_scanner_plugin_tests.erl    | 45 +++++++++++++++++++++-
 2 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl b/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl
index b29be2c26..ed9ab91d3 100644
--- a/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl
+++ b/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl
@@ -56,6 +56,10 @@
 -define(LIB, <<"lib">>).
 -define(VDU, <<"validate_doc_update">>).
 
+% Avoid checking indeterministic functions. They always show as false positives
+%
+-define(INTEDERMINISM_HEURISTICS, [<<"Math.random()">>, <<"Date.now()">>, <<"new Date()">>]).
+
 % Behavior callbacks
 
 start(SId, #{}) ->
@@ -337,14 +341,24 @@ start_or_reset_sm_proc(#st{sm_proc = #proc{} = Proc} = St) ->
 
 valid_views(#{} = Views) ->
     Fun = fun
-        (?LIB, _) -> false;
-        (<<_/binary>>, #{?MAP := <<_/binary>>}) -> true;
-        (_, _) -> false
+        (?LIB, _) ->
+            false;
+        (<<_/binary>>, #{?MAP := <<MapFun/binary>>, ?REDUCE := <<RedFun/binary>>}) ->
+            no_indeterminism(MapFun) andalso no_indeterminism(RedFun);
+        (<<_/binary>>, #{?MAP := <<MapFun/binary>>}) ->
+            no_indeterminism(MapFun);
+        (_, _) ->
+            false
     end,
     maps:filter(Fun, Views);
 valid_views(_) ->
     #{}.
 
+% Math.random(), Date.now() or new Date() will always show as false postives
+%
+no_indeterminism(<<FunSrc/binary>>) ->
+    binary:match(FunSrc, ?INTEDERMINISM_HEURISTICS) =:= nomatch.
+
 lib_load(#st{qjs_proc = Qjs, sm_proc = Sm}, #{} = Views) ->
     case maps:get(?LIB, Views, undefined) of
         Lib when is_map(Lib) andalso map_size(Lib) > 0 ->
diff --git a/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl b/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl
index d3f2e5d27..566666177 100644
--- a/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl
+++ b/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl
@@ -28,7 +28,8 @@ couch_quickjs_scanner_plugin_test_() ->
             ?TDEF_FE(t_filter_only, 10),
             ?TDEF_FE(t_filter_with_expected_error, 10),
             ?TDEF_FE(t_empty_ddoc, 10),
-            ?TDEF_FE(t_multi_emit_map, 10)
+            ?TDEF_FE(t_multi_emit_map, 10),
+            ?TDEF_FE(t_non_deterministic_views, 10)
         ]
     }.
 
@@ -304,6 +305,26 @@ t_multi_emit_map({_, DbName}) ->
             ok
     end.
 
+t_non_deterministic_views({_, DbName}) ->
+    ok = add_doc(DbName, ?DDOC1, ddoc_view_non_determinism(#{})),
+    meck:reset(couch_scanner_server),
+    meck:reset(?PLUGIN),
+    config:set("couch_scanner_plugins", atom_to_list(?PLUGIN), "true", false),
+    wait_exit(10000),
+    ?assertEqual(1, num_calls(start, 2)),
+    case couch_server:with_spidermonkey() of
+        true ->
+            ?assertEqual(1, num_calls(complete, 1)),
+            ?assert(num_calls(doc, 3) >= 5),
+            ?assertEqual(0, couch_stats:sample([couchdb, query_server, process_error_exits])),
+            ?assertEqual(0, couch_stats:sample([couchdb, query_server, process_errors])),
+            ?assertEqual(0, couch_stats:sample([couchdb, query_server, process_exits])),
+            % start and complete = 2, no errors
+            ?assertEqual(2, log_calls(warning));
+        false ->
+            ok
+    end.
+
 reset_stats() ->
     Counters = [
         [couchdb, query_server, process_error_exits],
@@ -484,3 +505,25 @@ ddoc_view_multi_emit(Doc) ->
             }
         }
     }.
+
+ddoc_view_non_determinism(Doc) ->
+    % String.prototype.startsWith used as a differentiator between
+    % SM and QuickJS. Make both emit items in different order. But at the
+    % end of the day, that doesn't matter as those are sorted anyway
+    Doc#{
+        views => #{
+            v1 => #{
+                map => <<"function(doc) {emit(Math.random(), 1);}">>
+            },
+            v2 => #{
+                map => <<"function(doc) {emit(Date.now(), 1);}">>
+            },
+            v3 => #{
+                map => <<"function(doc) {emit(new Date(), 1);}">>
+            },
+            v4 => #{
+                map => <<"function(doc) {emit(1,2);}">>,
+                reduce => <<"function(ks, vs, rr){return Date.now();}">>
+            }
+        }
+    }.