1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:57:45 +00:00

LibJS: Pre-allocate the out-of-memory error string on the VM

If we are out of memory, we can't try to allocate a string that could
fail as well. When Error is converted to String, this would result in an
endless OOM-throwing loop. Instead, pre-allocate the string on the VM,
and use it to construct the Error.

Note that as of this commit, the OOM string is still a DeprecatedString.
This is just preporatory for Error's conversion to String.
This commit is contained in:
Timothy Flynn 2023-02-16 12:55:22 -05:00 committed by Tim Flynn
parent 93ad25fbe5
commit 4d10911f96
5 changed files with 42 additions and 31 deletions

View file

@ -91,6 +91,17 @@ public:
return *m_single_ascii_character_strings[character];
}
// This represents the list of errors from ErrorTypes.h whose messages are used in contexts which
// must not fail to allocate when they are used. For example, we cannot allocate when we raise an
// out-of-memory error, thus we pre-allocate that error string at VM creation time.
enum class ErrorMessage {
OutOfMemory,
// Keep this last:
__Count,
};
DeprecatedString const& error_message(ErrorMessage) const;
bool did_reach_stack_space_limit() const
{
// Address sanitizer (ASAN) used to check for more space but
@ -285,6 +296,7 @@ private:
PrimitiveString* m_empty_string { nullptr };
PrimitiveString* m_single_ascii_character_strings[128] {};
AK::Array<DeprecatedString, to_underlying(ErrorMessage::__Count)> m_error_messages;
struct StoredModule {
ScriptOrModule referencing_script_or_module;