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

Syntax Highlighter Crashing #637

Open
1 task done
Halalaluyafail3 opened this issue Jun 10, 2023 · 4 comments
Open
1 task done

Syntax Highlighter Crashing #637

Halalaluyafail3 opened this issue Jun 10, 2023 · 4 comments

Comments

@Halalaluyafail3
Copy link

Checklist

  • This problem exists even with the setting "C_Cpp.enhancedColorization": "Disabled"

If Disabling that^ makes the problem go away, then follow this to make an issue on the C++ extension:
https://github.com/microsoft/vscode-cpptools/issues/new/choose

The code with a problem is:

typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<typename::std::add_pointer<int>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type>::type ptr{nullptr};

It looks like:

This code is crashing Visual Studio Code when there are no plugins enabled (which I presume is the syntax highlighter). The Visual Studio Code repository linked here so I am reporting this crash here.

It should look like:

It should highlight this code normally without crashing.

@9291Sam
Copy link

9291Sam commented Jun 10, 2023

Confirmed to be reproduceable on 1.79 with no extensions enabled.

@jeff-hykin
Copy link
Owner

I'd bet that this shares the same underlying issue as this
#578 (comment)

@RedCMD
Copy link

RedCMD commented Nov 9, 2023

I had a little look into this

"#typename" is responsible for the highlighting in that code snippet
and is definitely the cause of the lag
the size of the regex is not the problem, but the number of recursive calls "#typename" makes back to itself
image

"#typename" has some very weird nesting going on
from testing; the number of calls it executes seems to grow at 3^n
so from that code snippet, which has 20 nested levels
it causes 3^20 = 3486784401 regex executions on the int token alone
or sum_(n=0)^20 3^n = 5230176601 for the total number of calls to "#typename"
image

#template_call_range gets called 3 times at every level, every iteration
(hence the 3 in 3^n)
#template_call_range calls #template_call_context which calls #storage_types and full circle back to #typename
I suspect this is a remnant from microsoft/vscode-textmate#208
image

I managed to simplify typename down; to try to understand it
you can see that capture groups 2, 3 and 4 nest inside each other and that #template_call_range is called on all of them
when only a single call is needed
image

"test2": {
	"match": "([\\w:]++)(<\\g<0>>)*+",
	"captures": {
		"1": { "name": "keyword $1" },
		"2": { "patterns": [ { "include": "#template_call_range" } ] },
		"3": { "patterns": [ { "include": "#template_call_range" } ] },
		"4": { "patterns": [ { "include": "#template_call_range" } ] }
	}
},
"typename": {
	"match": "(?x)(?<_1>\\w++)(?<_2>(?<_3>(?>::)?+(?>\\w++(?<_4><(?>\\g<nest>|[^<>]++)*+>)?+::)*+)?+\\w++(?<nest><(?>\\g<nest>|[^<>]++)*+>)?+)",
	"comment": "(?x)(#1\ntypename)(#6\n(#12\n(?>::)?(?>[a-zA-Z0-9_]+(#14\n<(?>\\g<5>|[^<>]++)*>)?::)*+)?[a-zA-Z0-9_]+(#17\n<(?>\\g<5>|[^<>]++)*>)?)",
	"captures": {
		"1": { "name": "keyword $1" },
		"2": { "patterns": [ { "include": "#template_call_range" } ] },
		"3": { "patterns": [ { "include": "#template_call_range" } ] },
		"4": { "patterns": [ { "include": "#template_call_range" } ] }
	}
}

and heres a simplified version of the actual problem
keyword<keyword<keyword<keyword<keyword<keyword<keyword<keyword<keyword<keyword<keyword<keyword>>>>>>>>>>>

"typename": {
	"match": "\\w+(((<\\g<0>>)))?",
	"captures": {
		"1": { "patterns": [ { "include": "#typename" } ] },
		"2": { "patterns": [ { "include": "#typename" } ] },
		"3": { "patterns": [ { "include": "#typename" } ] }
	}
},

image

the problem is that #typename ends up calling itself 3 times at every nested level, and each of those 3 calls, then call #typename 3 more times at every nested level
spiraling out of control

which I guess is exactly the same as doing this:
image
typing aaaaaaaaaaaaaaaaaaaa will cause the complete and utter destruction of the vscode client thread

also remember simply having a "patterns" array inside a capture group incurs a medium performance hit
however the "patterns" array for "begin"/"end" rules does not seem to have the issue

@jeff-hykin
Copy link
Owner

which I guess is exactly the same as doing this:
image
typing aaaaaaaaaaaaaaaaaaaa will cause the complete and utter destruction of the vscode client thread

Oh that is very interesting. And that is weird that ranges dont have the same performance hit.

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

No branches or pull requests

4 participants