Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: prevent hard coding stack trace limit #30752

Closed
wants to merge 1 commit into from

Conversation

@legendecas
Copy link
Member

legendecas commented Dec 1, 2019

Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
@legendecas legendecas mentioned this pull request Dec 1, 2019
4 of 4 tasks complete
@devsnek

This comment has been minimized.

Copy link
Member

devsnek commented Dec 1, 2019

the number of frames here should already be the <= stack trace limit. as I understand it our code just provides a lower bound.

@legendecas legendecas force-pushed the legendecas:cpp-error-stack-limit branch from b52d64d to c92a4ac Dec 1, 2019
@joyeecheung

This comment has been minimized.

Copy link
Member

joyeecheung commented Dec 1, 2019

TBH this does not sound like a good idea since v8 is considering removing these non-standard APIs: https://bugs.chromium.org/p/v8/issues/detail?id=6974 By adding support in our internal error printers we are sort of encouraging people to use it - also in the case of --trace-sync-io, it is more of an implementation detail that we use the error-related API to print things.

It is especially tricky to try handling these hooks ourselves considering we also made the mistake (?) of always respecting the Error.prepareStackTrace from the main context.

src/node_errors.cc Outdated Show resolved Hide resolved
src/node_errors.cc Outdated Show resolved Hide resolved
src/node_errors.cc Outdated Show resolved Hide resolved
src/node_errors.cc Outdated Show resolved Hide resolved
src/node_errors.cc Outdated Show resolved Hide resolved
MaybeLocal<Value> stack_trace_limit_val =
context->Global()
->Get(context, error_string)
.ToLocalChecked()

This comment has been minimized.

Copy link
@addaleax

addaleax Dec 1, 2019

Member

This is not a valid .ToLocalChecked(), can you handle errors here (and for the .ToChecked() below, as @lundibundi pointed out)? Ideally, this block here would also be wrapped in a v8::TryCatch in order to swallow these errors from trying to get the stack trace

@legendecas

This comment has been minimized.

Copy link
Member Author

legendecas commented Dec 4, 2019

@joyeecheung Yes, the API Error.stackTraceLimit itself is not standard. The idea of PR is preventing hard-coding numbers in the codebase. Either the limit shall be referred from the global Error.stackTraceLimit or just stored in the node::Environment, it would sound good to me. Since we've already used Error.stackTraceLimit multiple places, I'd think it might be fine at the time. We could remove the usage on Error.stackTraceLimit later if it's time to deprecate the API.

Anyway, if anyone has any strong opinion on this idea, it also sounds good to me if the stacktrace limit shall be referred from some internal store.

@joyeecheung

This comment has been minimized.

Copy link
Member

joyeecheung commented Dec 4, 2019

@legendecas Adding something like Environment::kStackTraceLimit SGTM. I think most internal uses of Error.stackTraceLimit are only optimizations to avoid the overhead of generating a stack trace (with the pattern of setting it to 0 or 1, creating an error, and then restoring the limit), which I don't know if is actually effective at this point considering stack trace generation in v8 is lazy.

@legendecas legendecas force-pushed the legendecas:cpp-error-stack-limit branch 5 times, most recently from 9bf92aa to e6f54bb Dec 6, 2019
Copy link
Member

addaleax left a comment

I guess LGTM although I’d have preferred the solution that reads Error.stackTraceLimit

@legendecas

This comment has been minimized.

Copy link
Member Author

legendecas commented Dec 14, 2019

@joyeecheung @lundibundi may I ask for your reviews on the PR since you have interests in preventing usage on the unstandardized Error.stackTraceLimit?

src/env.h Outdated Show resolved Hide resolved
Copy link
Member

lundibundi left a comment

LGTM. Though I would prefer the previous Error.stackTraceLimit version as it is useful to be able to control the C++-land stack size and to have it consistent with the JS-land. Regarding possible removal of the API, I think it is not much of an issue as this code will only be in once place and we can always replace it with a constant in a few days' time at max.

Also, the PR's name should be changed accordingly to the code.

src/node_errors.cc Show resolved Hide resolved
@legendecas

This comment has been minimized.

Copy link
Member Author

legendecas commented Dec 17, 2019

this code will only be in once place

There are some to be landed recently or in an expected future. #29207 and #30516. I'll update the code accordingly.

@legendecas

This comment has been minimized.

Copy link
Member Author

legendecas commented Dec 17, 2019

@devsnek the number of frames here should already be the <= stack trace limit. as I understand it our code just provides a lower bound.

It's true that in most cases stack trace wasn't very long to exceed the limit. Still, it is possible.

@lundibundi

This comment has been minimized.

Copy link
Member

lundibundi commented Dec 17, 2019

this code will only be in once place

There are some to be landed recently or in an expected future. #29207 and #30516. I'll update the code accordingly.

I meant to have one function like get_stack_size or compute_stack_size and then use it everywhere. Then we can easily just make this function return 10; if needed.

@legendecas legendecas changed the title src: respect Error.stackTraceLimit in c++ land src: prevent hard coding stack trace limit Dec 19, 2019
@legendecas legendecas force-pushed the legendecas:cpp-error-stack-limit branch 2 times, most recently from 543418f to 4f1f62a Dec 19, 2019
Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.
@legendecas legendecas force-pushed the legendecas:cpp-error-stack-limit branch from 4f1f62a to 7727628 Dec 22, 2019
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment has been minimized.

@BridgeAR

This comment has been minimized.

Copy link
Member

BridgeAR commented Dec 24, 2019

I agree with @addaleax and @lundibundi that the former solution seemed better. If the property will ever be removed, it's definitely possible to react to it appropriately. So far it's not likely that this is happening anytime soon.

@joyeecheung

This comment has been minimized.

Copy link
Member

joyeecheung commented Dec 24, 2019

I would not oppose to a solution that allows the JS land to configure the stack trace limit, but I would be -1 if this is Error.stackTraceLimit for the following reasons (other than it's non-standard):

  1. It's an implementation detail that we use the error stack trace API to print stack traces that are not necessarily related to errors (e.g. --trace-sync-io) (actually, I don't think we currently even use the error stack trace API at all in these cases, so we'd be adding dependencies instead)
  2. It's unsafe to access Error.stackTraceLimit (which can be an accessor) in some of the paths where stack traces are printed from C++ (e.g. in Environment::Exit)
@nodejs-github-bot

This comment has been minimized.

BridgeAR added a commit that referenced this pull request Dec 25, 2019
Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.

PR-URL: #30752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
@BridgeAR

This comment has been minimized.

Copy link
Member

BridgeAR commented Dec 25, 2019

Landed in 8baee5e 🎉

@BridgeAR BridgeAR closed this Dec 25, 2019
BridgeAR added a commit that referenced this pull request Jan 3, 2020
Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.

PR-URL: #30752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
@legendecas legendecas deleted the legendecas:cpp-error-stack-limit branch Jan 5, 2020
@BridgeAR BridgeAR mentioned this pull request Jan 7, 2020
targos added a commit that referenced this pull request Jan 14, 2020
Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.

PR-URL: #30752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
targos added a commit that referenced this pull request Jan 14, 2020
Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.

PR-URL: #30752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.