|
| 1 | +E9Tool/E9Patch Web Browser Guide |
| 2 | +================================ |
| 3 | + |
| 4 | +This is a short demo for instrumenting modern web browsers using |
| 5 | +E9Tool/E9Patch. |
| 6 | + |
| 7 | +--- |
| 8 | +## Instrument Firefox |
| 9 | + |
| 10 | +Modern versions of Firefox should be straightforward to instrument. |
| 11 | +Below is a basic example: |
| 12 | + |
| 13 | +0. Install E9Tool/E9Patch: |
| 14 | + |
| 15 | + * [https://github.com/GJDuck/e9patch/releases](https://github.com/GJDuck/e9patch/releases) |
| 16 | + |
| 17 | +1. Download |
| 18 | +[Firefox](https://ftp.mozilla.org/pub/firefox/releases/108.0.2/linux-x86_64/en-US/firefox-108.0.2.tar.bz2): |
| 19 | + |
| 20 | + $ wget https://ftp.mozilla.org/pub/firefox/releases/108.0.2/linux-x86_64/en-US/firefox-108.0.2.tar.bz2 |
| 21 | + |
| 22 | + For this demo we use Firefox version (108.0.2). |
| 23 | + Other versions of Firefox may also work (untested). |
| 24 | +2. Extract Firefox: |
| 25 | + |
| 26 | + $ tar xvfj firefox-108.0.2.tar.bz2 |
| 27 | + $ cd firefox |
| 28 | + |
| 29 | +3. Compile the instrumentation. |
| 30 | + For this demo we use `counter.c`: |
| 31 | + |
| 32 | + $ e9compile /usr/share/e9tool/examples/counter.c |
| 33 | + |
| 34 | +4. Instrument the `libxul.so` binary. |
| 35 | + Note that `libxul.so` is the main Firefox binary |
| 36 | + (`firefox-bin` is mostly a wrapper that dynamically loads `libxul`). |
| 37 | + In this example, we will insert `counter` instrumentation for each jump |
| 38 | + instruction: |
| 39 | + |
| 40 | + $ mv libxul.so libxul.orig.so |
| 41 | + $ e9tool -M jmp -P 'entry()@counter' -c 5 -o libxul.so ./libxul.orig.so |
| 42 | + |
| 43 | + Here: |
| 44 | + |
| 45 | + * "`-M jmp`" matches all jump and conditional jump instructions. |
| 46 | + * "`-P entry()@counter`" inserts a call to the `entry()` function defined |
| 47 | + in `counter.c` for each matching instruction. |
| 48 | + * "`-c 5`" tells E9Patch not to aggressively compress the output binary. |
| 49 | + Since the input binary is quite large (~145MB), the output binary may use |
| 50 | + more mappings beyond the system default. |
| 51 | + This option reduces the number of mappings in exchange for a larger |
| 52 | + output binary. |
| 53 | + * "`-o libxul.so`" specifies that the output binary should be called |
| 54 | + `libxul.so` (replacing the original). |
| 55 | + * "`libxul.orig.so`" is the input binary (renamed from `libxul.so`). |
| 56 | + |
| 57 | +5. Run Firefox with the instrumented `libxul.so`: |
| 58 | + |
| 59 | + $ ./firefox |
| 60 | + count = 1000000 |
| 61 | + count = 2000000 |
| 62 | + count = 3000000 |
| 63 | + count = 4000000 |
| 64 | + ... |
| 65 | + |
| 66 | +Notes: |
| 67 | + |
| 68 | +* The `counter` instrumentation is not thread safe, so the counts will be |
| 69 | + inaccurate for multi-threaded programs like Firefox. |
| 70 | + However, this is just an example for demonstration purposes. |
| 71 | +* The instrumented Firefox should be stable and behave the same as the |
| 72 | + original, except for being slower and printing count information. |
| 73 | +* Other kinds of instrumentation/rewriting is possible. |
| 74 | + Please see the [E9Tool User |
| 75 | + Guide](https://github.com/GJDuck/e9patch/blob/master/doc/e9tool-user-guide.md) for more information. |
| 76 | + |
| 77 | +--- |
| 78 | +## Instrument Google Chrome? |
| 79 | + |
| 80 | +It is also possible to instrument Google Chrome using E9Tool/E9Patch. |
| 81 | +However, for modern versions of Chrome, this can be troublesome: |
| 82 | + |
| 83 | +1. Chrome frequently uses data-in-code; and |
| 84 | +2. Chrome seems to copy some code to different locations at runtime. |
| 85 | + This breaks some of the basic assumptions for static binary rewriting. |
| 86 | + |
| 87 | +It is possible to manually exclude affected regions from rewriting (see |
| 88 | +E9Tool's `-E` option). |
| 89 | +However, this process is manual, and depends on the specific Chrome version. |
| 90 | + |
| 91 | +For older versions of Chrome (circa 2020), it is possible to successfully |
| 92 | +rewrite Chrome by excluding all code before the "`ChromeMain`" symbol: |
| 93 | + |
| 94 | + $ e9tool -E '.text..ChromeMain' ... |
| 95 | + |
| 96 | +Here: |
| 97 | + |
| 98 | +* "`-E '.text..ChromeMain'`" tells E9Tool to ignore all code in the |
| 99 | +"`.text`" section before `ChromeMain`, which is usually <3% of all code. |
| 100 | + |
| 101 | +This simple trick no longer works for modern versions of Chrome. |
| 102 | + |
0 commit comments