Skip to content

Add API to set and get the V8 cage base address#3190

Merged
erikcorry merged 6 commits intomainfrom
erikcorry/get-set-cage-base
Dec 3, 2024
Merged

Add API to set and get the V8 cage base address#3190
erikcorry merged 6 commits intomainfrom
erikcorry/get-set-cage-base

Conversation

@erikcorry
Copy link
Contributor

I want to use this to call madvise just before a crash so the V8 heap
for the crashing isolate is part of the core dump.

@erikcorry erikcorry requested review from a team as code owners November 29, 2024 12:42

// Set the location of the pointer cage base for the current isolate. This is only
// used by getJsCageBase().
void setJsCageBase(void* cage_base);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: prefer camelCase

if (!v8Initialized) return;
v8::Isolate* isolate = v8::Isolate::TryGetCurrent();
if (isolate == nullptr) return;
// Returns null if we are not using pointer cages.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this comment makes sense -- probably copy-pasta?

if (!v8Initialized) return nullptr;
v8::Isolate* isolate = v8::Isolate::TryGetCurrent();
if (isolate == nullptr) return nullptr;
// Returns null if we are not using pointer cages.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this comment refer to the line above, where we return nullptr? Or the line below? If the line below, I'm not sure it makes sense, since someone could probably call setJsCageBase() with a non-null value even if we're not using pointer cages, right?

v8::Isolate* isolate = v8::Isolate::TryGetCurrent();
if (isolate == nullptr) return nullptr;
// Returns null if we are not using pointer cages.
return isolate->GetData(4);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the magic number 4 come from? Can it be hoisted out to a constexpr variable?

registry.template registerStructProperty<name##_JSG_NAME_DO_NOT_USE_DIRECTLY, \
decltype(::kj::instance<Self>().name), &Self::name>()

enum SetDataIndex {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a documentation/comment for each one of these values?

return isolate->GetData(SET_DATA_CAGE_BASE);
}

void setJsCageBase(void* cageBase) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Unless you plan to call this from lots of different call sites it seems like these methods mainly serve to silently hide subtle, presumably unexpected error cases (the early returns). Maybe it would be better to just not have these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a layering issue - Edgeworker doesn't have a lot of files that deal directly with the v8 Isolate, so I thought it made more sense to put it in jsg. The early returns are modelled on getJsStackTrace, and like that function the getJsCageBase is intended to be called during crash handling so it is written in a defensive style.

@erikcorry erikcorry merged commit a866c69 into main Dec 3, 2024
@erikcorry erikcorry deleted the erikcorry/get-set-cage-base branch December 3, 2024 08:05
danlapid pushed a commit that referenced this pull request Dec 3, 2024
Add API to set and get the V8 cage base address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants