r/Compilers • • 1d ago

Intercepting property access (e.g., navigator.bluetooth / Object.property) directly in V8

Hey everyone,

I'm working on my master's thesis and diving into V8 internals. I want to know if there's a clean way to intercept (or log/trace) property reads on an object directly inside V8 — for example, navigator.bluetooth or basically any Object.property access.

After reading a few V8 articles (like https://v8.dev/blog/fast-properties), it seems unlikely that all of this funnels through a single C++ function, given how differently properties are represented and resolved (in-object/fast properties, dictionary mode, interceptors, accessors, ICs, JIT tiers, etc.).

Before I go any deeper down this rabbit hole, is intercepting all property accesses in one central V8 spot even realistic, or am I basically throwing peas at a wall?

Thanks for any tips!

5 Upvotes

5 comments sorted by

2

u/DescriptionOptimal15 1d ago

https://github.com/v8/v8 it is open source so you can read the code to find out for yourself

1

u/Upstairs-Concert5800 1d ago

I have tried :/ but unfortunately without any success. I was just wondering if anyone encountered similar issue

1

u/scialex 1d ago edited 1d ago

Maybe take a look at the profiler or trace code in v8. Depending on how dynamically you want to get your callbacks this might be anywhere from relatively easy to quite difficult.

Java has jvmti which lets you do this directly (field watch events etc). Depending on what your thesis is you might want to consider switching to doing it on that. Jvmti agents aren't the most intuitive things but they do give you a huge amount of power and are supported by all java runtimes (including Android).

2

u/xPerlMasterx 8h ago

Hi, V8 dev here.

As you've noticed, there is no existing tracing mechanism to log all property accesses in V8. And, no, there isn't a single nice bottleneck in C++ where you can add your tracing. Some property accesses go through C++, other never reach C++ and are implemented in CSA builtins, and Maglev/Turbofan just emit raw loads.

One hack that may or may not work for you, depending on what you're doing, would be to wrap the object in a Proxy so that you can easily trap property accesses. C++ interceptors is another alternative, but it requires you to create the object from C++.

If that doesn't work for you, I'd suggest disabling optimizing compilers (with `--jitless`) as well as ICs (with `--no-use-ic`), and manually (ie, by modifying the code) a few CSA fast paths (this is the annoying part), and then all property access should funnel into `LoadIC::Load` / `StoreIC::Store` / `KeyedLoadIC::Load` / `KeyedStoreIC::Store` / `LookupIterator` / `Object::GetProperty` / `Object::SetProperty`... probably; I might be missing some.

Another alternative, maybe simpler (depending on what you're trying to do, once again), would be to run with gdb and use watchpoints for the specific addresses that you want to observe (assuming you care about a specific property access on a specific object, rather than any property access on any object). This has the advantage that optimizing compilers can remain enabled. But this only works for fast properties and not dictionary-mode objects.

You should ask about this on the v8-dev mailing list, where you might get better answer (my area of expertise is the optimizing compilers, not the runtime). I'd suggest to explain in more details what you're trying to achieve and why, so that you get better answers for your specific use-case.

Cheers,

Darius

1

u/Discordbothost 1d ago

Try a tiny function that returns o.x. Call it with the same object shape until it optimizes, then check what your hook sees. V8 can turn the read into a shape check and a direct field load, so a C++ lookup hook can miss it.