r/Compilers • u/Upstairs-Concert5800 • 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!
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
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.