bytenode/bytenode

access property of undefined variable in a large project casue stack overflow

maoyiluo opened this issue · 3 comments

I have a line fo code looks like this

// if obj is undefined, when executing this line, the whole program crashes
fn(obj.a)

and a try catch block can't prevent the program from crashing

// still crash
try{
 fn(obj.a)
}catch(e){
}

here is the output

# Fatal error in , line 0
# Check failed: stack_overflow().
#
#
#
#FailureMessage Object: 0x7ffcabcf8420
 1: 0xaa8411  [/data/home/maoyiluo/.nvm/versions/node/v14.20.0/bin/node]
 2: 0x1a47214 V8_Fatal(char const*, ...) [/data/home/maoyiluo/.nvm/versions/node/v14.20.0/bin/node]
 3: 0xfe18d1 v8::internal::Parser::DoParseFunction(v8::internal::Isolate*, v8::internal::ParseInfo*, int, int, int, v8::internal::AstRawString const*) [/data/home/maoyiluo/.nvm/versions/node/v14.20.0/bin/node]
 4: 0xfe1d0b v8::internal::Parser::ParseFunction(v8::internal::Isolate*, v8::internal::ParseInfo*, v8::internal::Handle<v8::internal::SharedFunctionInfo>) [/data/home/maoyiluo/.nvm/versions/node/v14.20.0/bin/node]
 5: 0xfe6a81 v8::internal::parsing::ParseFunction(v8::internal::ParseInfo*, v8::internal::Handle<v8::internal::SharedFunctionInfo>, v8::internal::Isolate*, v8::internal::parsing::ReportErrorsAndStatisticsMode) [/data/home/maoyiluo/.nvm/versions/node/v14.20.0/bin/node]
 6: 0xfe6ea5 v8::internal::parsing::ParseAny(v8::internal::ParseInfo*, v8::internal::Handle<v8::internal::SharedFunctionInfo>, v8::internal::Isolate*, v8::internal::parsing::ReportErrorsAndStatisticsMode) [/data/home/maoyiluo/.nvm/versions/node/v14.20.0/bin/node]
 7: 0xd29e39 v8::internal::ErrorUtils::ThrowLoadFromNullOrUndefined(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Object>, v8::internal::MaybeHandle<v8::internal::Object>) [/data/home/maoyiluo/.nvm/versions/node/v14.20.0/bin/node]
 8: 0xe16866 v8::internal::LoadIC::Load(v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Name>, bool) [/data/home/maoyiluo/.nvm/versions/node/v14.20.0/bin/node]
 9: 0xe173b7 v8::internal::Runtime_LoadNoFeedbackIC_Miss(int, unsigned long*, v8::internal::Isolate*) [/data/home/maoyiluo/.nvm/versions/node/v14.20.0/bin/node]
10: 0x1448f59  [/data/home/maoyiluo/.nvm/versions/node/v14.20.0/bin/node]

After adding some protecting code, the program works fine and never crash again.

cosnt getValue = (obj, key) => {
   if(!obj) return undefined;
   return obj[key]
}

try{
 fn(getValue(obj.a))
}catch(e){
}

I have tried some really simple example but none of them crashed, so I have no idea why mine code crashed.

Does anyone else have the same issue?

I found out if you are in a async function accessing property of undefined will cause this problem.

Here is the code that crash after compiling to jsc

const f1 = async () => {
  const b = undefined;
  console.log(b.c);
};

f1();

Async arrow functions are represented differently inside v8, and they cause all sorts of problems. This should be fixed in v8 itself, but it's quite hard to convince them to do so, because the way we [ab]use the cachedData to protect the source code: isn't on their list of priorities.

So, basically there is nothing to be fixed here in bytenode, and even not in node itself. This has to be fixed upstream in v8.

You could transpile all your async arrow functions to async regular functions. This should solve most of the problems.

Thanks a lot for your response!