1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00
Go to file
Raymond Hill 7971b22385
Expand bidi-trie usage in static network filtering engine
Related issues:
- https://github.com/uBlockOrigin/uBlock-issues/issues/761
- https://github.com/uBlockOrigin/uBlock-issues/issues/528

The previous bidi-trie code could only hold filters which
are plain pattern, i.e. no wildcard characters, and which
had no origin option (`domain=`), right and/or left anchor,
and no `csp=` option.

Example of filters that could be moved into a bidi-trie
data structure:

    &ad_box_
    /w/d/capu.php?z=$script,third-party
    ||liveonlinetv247.com/images/muvixx-150x50-watch-now-in-hd-play-btn.gif

Examples of filters that could NOT be moved to a bidi-trie:

    -adap.$domain=~l-adap.org
    /tsc.php?*&ses=
    ||ibsrv.net/*forumsponsor$domain=[...]
    @@||imgspice.com/jquery.cookie.js|$script
    ||view.atdmt.com^*/iview/$third-party
    ||postimg.cc/image/$csp=[...]

Ideally the filters above should be able to be moved to a
bidi-trie since they are basically plain patterns, or at
least partially moved to a bidi-trie when there is only a
single wildcard (i.e. made of two plain patterns).

Also, there were two distinct bidi-tries in which
plain-pattern filters can be moved to: one for patterns
without hostname anchoring and another one for patterns
with hostname-anchoring. This was required because the
hostname-anchored patterns have an extra condition which
is outside the bidi-trie knowledge.

This commit expands the number of filters which can be
stored in the bidi-trie, and also remove the need to
use two distinct bidi-tries.

- Added ability to associate a pattern with an integer
  in the bidi-trie [1].
    - The bidi-trie match code passes this externally
      provided integer when calling an externally
      provided method used for testing extra conditions
      that may be present for a plain pattern found to
      be matching in the bidi-trie.

- Decomposed existing filters into smaller logical units:
    - FilterPlainLeftAnchored =>
        FilterPatternPlain +
        FilterAnchorLeft
    - FilterPlainRightAnchored =>
        FilterPatternPlain +
        FilterAnchorRight
    - FilterExactMatch =>
        FilterPatternPlain +
        FilterAnchorLeft +
        FilterAnchorRight
    - FilterPlainHnAnchored =>
        FilterPatternPlain +
        FilterAnchorHn
    - FilterWildcard1 =>
        FilterPatternPlain + [
          FilterPatternLeft or
          FilterPatternRight
        ]
    - FilterWildcard1HnAnchored =>
        FilterPatternPlain + [
          FilterPatternLeft or
          FilterPatternRight
        ] +
        FilterAnchorHn
    - FilterGenericHnAnchored =>
        FilterPatternGeneric +
        FilterAnchorHn
    - FilterGenericHnAndRightAnchored =>
        FilterPatternGeneric +
        FilterAnchorRight +
        FilterAnchorHn
    - FilterOriginMixedSet =>
        FilterOriginMissSet +
        FilterOriginHitSet
    - Instances of FilterOrigin[...], FilterDataHolder
      can also be added to a composite filter to
      represent `domain=` and `csp=` options.

- Added a new filter class, FilterComposite, for
  filters which are a combination of two or more
  logical units. A FilterComposite instance is a
  match when *all* filters composing it are a
  match.

Since filters are now encoded into combination of
smaller units, it becomes possible to extract the
FilterPatternPlain component and store it in the
bidi-trie, and use the integer as a handle for the
remaining extra conditions, if any.

Since a single pattern in the bidi-trie may be a
component for different filters, the associated
integer points to a sequence of extra conditions,
and a match occurs as soon as one of the extra
conditions (which may itself be a sequence of
conditions) is fulfilled.

Decomposing filters which are currently single
instance into sequences of smaller logical filters
means increasing the storage and CPU overhead when
evaluating such filters. The CPU overhead is
compensated by the fact that more filters can now
moved into the bidi-trie, where the first match is
efficiently evaluated. The extra conditions have to
be evaluated if and only if there is a match in the
bidi-trie.

The storage overhead is compensated by the
bidi-trie's intrinsic nature of merging similar
patterns.

Furthermore, the storage overhead is reduced by no
longer using JavaScript array to store collection
of filters (which is what FilterComposite is):
the same technique used in [2] is imported to store
sequences of filters.

A sequence of filters is a sequence of integer pairs
where the first integer is an index to an actual
filter instance stored in a global array of filters
(`filterUnits`), while the second integer is an index
to the next pair in the sequence -- which means all
sequences of filters are encoded in one single array
of integers (`filterSequences` => Uint32Array). As
a result, a sequence of filters can be represented by
one single integer -- an index to the first pair --
regardless of the number of filters in the sequence.

This representation is further leveraged to replace
the use of JavaScript array in FilterBucket [3],
which used a JavaScript array to store collection
of filters. Doing so means there is no more need for
FilterPair [4], which purpose was to be a lightweight
representation when there was only two filters in a
collection.

As a result of the above changes, the map of `token`
(integer)  => filter instance (object) used to
associate tokens to filters or collections of filters
is replaced with a more efficient map of `token`
(integer) to filter unit index (integer) to lookup a
filter object from the global `filterUnits` array.

Another consequence of using one single global
array to store all filter instances means we can reuse
existing instances when a logical filter instance is
parameter-less, which is the case for FilterAnchorLeft,
FilterAnchorRight, FilterAnchorHn, the index to these
single instances is reused where needed.

`urlTokenizer` now stores the character codes of the
scanned URL into a bidi-trie buffer, for reuse when
string matching methods are called.

New method: `tokenHistogram()`, used to generate
histograms of occurrences of token extracted from URLs
in built-in benchmark. The top results of the "miss"
histogram are used as "bad tokens", i.e. tokens to
avoid if possible when compiling filter lists.

All plain pattern strings are now stored in the
bidi-trie memory buffer, regardless of whether they
will be used in the trie proper or not.

Three methods have been added to the bidi-trie to test
stored string against the URL which is also stored in
then bidi-trie.

FilterParser is now instanciated on demand and
released when no longer used.

***

[1] 135a45a878/src/js/strie.js (L120)
[2] e94024d350
[3] 135a45a878/src/js/static-net-filtering.js (L1630)
[4] 135a45a878/src/js/static-net-filtering.js (L1566)
2019-10-21 08:15:58 -04:00
.github Update ISSUE_TEMPLATE.md 2018-04-15 13:55:07 -04:00
assets Remove "RUS: AdGuard Russian" from stock filter list 2019-10-11 10:50:25 -04:00
dist Make Firefox dev build auto-update 2019-10-17 17:30:02 -04:00
doc Update CPU chart image with name ublock instead of μblock (#3566) 2018-03-02 06:47:47 -05:00
docs Fix title 2019-09-15 14:53:18 -04:00
platform Do not bypass network listener in suspended mode 2019-10-07 08:13:37 -04:00
src Expand bidi-trie usage in static network filtering engine 2019-10-21 08:15:58 -04:00
tools Fix thunderbird build script 2019-10-11 18:01:05 -04:00
.gitignore git ignore ./tmp/ 2016-04-05 07:55:22 -04:00
.jshintrc Work toward modernizing code base: promisification 2019-09-16 09:45:17 -04:00
.travis.yml Generate new access token for Travis deployment 2019-05-28 09:06:02 -04:00
CONTRIBUTING.md Update CONTRIBUTING.md 2018-04-15 11:52:41 -04:00
LICENSE.txt first commit 2014-06-23 18:42:43 -04:00
MANIFESTO.md Update MANIFESTO.md 2015-03-12 23:22:30 +00:00
README.md Add heads up for Safari 13+ users (#3758) 2019-10-08 14:43:09 -04:00

Build Crowdin License


uBlock Origin

pronounced you-block origin (/ˈjuːˌblɒk/) — you decide what enters your browser.
English, Chinese (中文), Korean (한국어), Português (Brasil)

BEWARE! uBlock Origin is (and has always been) COMPLETELY UNRELATED to the web site ublock.org.


An efficient blocker add-on for various browsers. Fast, potent, and lean.

uBlock Origin is NOT an "ad blocker": it is a wide-spectrum blocker -- which happens to be able to function as a mere "ad blocker". The default behavior of uBlock Origin when newly installed is to block ads, trackers and malware sites -- through EasyList, EasyPrivacy, Peter Lowes ad/tracking/malware servers, various lists of malware sites, and uBlock Origin's own filter lists.

Documentation

Basic mode Advanced-user mode
Popup user interface A point-and-click firewall which can be configured on a per-site basis

.
.

Configure as you wish:
picture shows 3rd-party scripts and frames blocked by default everywhere

Visit the uBlock Origin's wiki for documentation.

For support/questions/help, there is /r/uBlockOrigin on Reddit.

Philosophy

uBlock Origin (or uBlock₀) is not an ad blocker; it's a general-purpose blocker. uBlock Origin blocks ads through its support of the Adblock Plus filter syntax. uBlock Origin extends the syntax and is designed to work with custom rules and filters. Furthermore, advanced mode allows uBlock Origin to work in default-deny mode, which mode will cause all 3rd-party network requests to be blocked by default, unless allowed by the user.

That said, it's important to note that using a blocker is NOT theft. Don't fall for this creepy idea. The ultimate logical consequence of blocking = theft is the criminalisation of the inalienable right to privacy.

Ads, "unintrusive" or not, are just the visible portions of privacy-invading apparatus entering your browser when you visit most sites nowadays. uBlock Origin's main goal is to help users neutralize such privacy-invading apparatus — in a way that welcomes those users who don't wish to use more technical, involved means (such as uMatrix).

EasyList, Peter Lowe's Adservers, EasyPrivacy and Malware domains are enabled by default when you install uBlock Origin. Many more lists are readily available to block trackers, analytics, and more. Hosts files are also supported.

Once you install uBlock Origin, you may easily un-select any of the pre-selected filter lists if you think uBlock Origin blocks too much. For reference, Adblock Plus installs with only EasyList enabled by default.

Installation

Feel free to read about the extension's required permissions.

Chromium

You can install the latest version manually, from the Chrome Web Store, or from the Opera add-ons.

There is also a development version in the Chrome Web Store if you want to test uBlock Origin with the latest changes: see uBlock Origin dev build.

It is expected that uBlock Origin is compatible with any Chromium-based browsers.

Firefox / Firefox for Android

Firefox Add-ons web site.

There is also a development version if you want to test uBlock Origin with the latest changes: for installation, see Install / Firefox webext / For beta version

uBlock Origin is compatible with SeaMonkey, Pale Moon, and possibly other browsers based on Firefox: for installation, see Install / Firefox legacy.

uBO mat also be installable as a Debian package:

  • Firefox 56-: apt-get install xul-ext-ublock-origin
  • Firefox 55+: apt-get install webext-ublock-origin

There is no guarantee the package will be available on your specific platform -- in which case, you will have to install from Firefox Add-ons web site.

Microsoft Edge

Developer: @nikrolls.

Chromium-based Edge: Stable version available in Microsoft Edge Insider Addons.

Legacy Edge: Stable version available in Microsoft Store.

Development version available at https://github.com/nikrolls/uBlock-Edge#edge.

Note that issues specific to the Edge fork are the responsibility of the current maintainer, I have no control over the code base of the fork.

Safari (macOS)

Developer: @el1t.

Development version available at https://github.com/el1t/uBlock-Safari#ublock-originfor-safari.

Warning: It is not possible for extensions like uBlock Origin to work with Safari 13+. See https://github.com/el1t/uBlock-Safari/issues/158.

Note that issues specific to the Safari fork are the responsibility of the current maintainer, I have no control over the code base of the fork.

Note for all browsers

To benefit from uBlock Origin's higher efficiency, it's advised that you don't use other inefficient blockers at the same time (such as AdBlock or Adblock Plus). uBlock Origin will do as well or better than most popular ad blockers. Other blockers can also prevent uBlock Origin's privacy or anti-blocker features from working properly.

Deploying

Below is documentation to assist administrators in deploying uBlock Origin:

Release History

See the releases pages for a history of releases and highlights for each release.

About

uBlock Origin's manifesto.

Free. Open source. For users by users. No donations sought.

Without the preset lists of filters, this extension is nothing. So if ever you really do want to contribute something, think about the people working hard to maintain the filter lists you are using, which were made available to use by all for free.

You can contribute by helping translate uBlock Origin on Crowdin.

License

GPLv3.