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

fix: Crashes due to shadowed global scope object #170

Merged
merged 1 commit into from
Aug 16, 2024

Conversation

zermelo-wisen
Copy link
Contributor

@zermelo-wisen zermelo-wisen commented Aug 8, 2024

Fixes #159.

Summary

This pull request addresses an essential issue by changing instrumentation to prevent potential crashes in environments where global or globalThis objects are shadowed or manipulated. These updates ensure the stability and reliability of the AppMap recording functionality.

Original Code Example

function testFun(arg) {
 return arg + 1;
}

Transformation Before This Fix

function testFun(arg) {
  return global.AppMapRecodHook.call(this, () => {
    return arg + 1;
  }, arguments, __appmapFunctionRegistry[0]);
}

Transformation After This Fix

// These are added to the top of the module/script

function __appmapRecordInit() {
 let g = null;
 try {
   g = global.AppMapRecordHook;
 } catch (e) {
   try {
     g = globalThis.AppMapRecordHook;
   } catch (e) {}
   // If global/globalThis is shadowed in the top level, we'll get:
   // ReferenceError: Cannot access 'global' before initialization.   
 }
 // Bypass recording if we can't access recorder to prevent a crash.
 return g ?? ((fun, argsArg) => fun.apply(this, argsArg));
}

const __appmapRecord = __appmapRecordInit();

// Not calling global.AppMapRecordHook directly in the function
// prevents shadowing error in the function and pushes the check
// to a single place where it is run just once in the module/script
// initialization.

function testFun(arg) {
  return __appmapRecord.call(this, () => {
    return arg + 1;
  }, arguments, __appmapFunctionRegistry[0]);
}

@zermelo-wisen zermelo-wisen force-pushed the fix/check-global-recod-hook branch from c7c0c8e to d88030b Compare August 8, 2024 15:31
Changes instrumentation to prevent crashes in environments/code where global
or globalThis objects are shadowed or manipulated.
@zermelo-wisen zermelo-wisen force-pushed the fix/check-global-recod-hook branch from d88030b to c84ecfa Compare August 9, 2024 15:41
Copy link
Collaborator

@dividedmind dividedmind left a comment

Choose a reason for hiding this comment

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

Great work! Thanks.

@dustinbyrne dustinbyrne merged commit e05eb01 into main Aug 16, 2024
6 checks passed
@dustinbyrne dustinbyrne deleted the fix/check-global-recod-hook branch August 16, 2024 14:13
@appland-release
Copy link

🎉 This PR is included in version 2.24.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: Cannot read properties of undefined (reading 'call')
4 participants