1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:48:11 +00:00

LibJS+AK: Move cross-platform stack bounds code from JS::Heap to AK::StackInfo

This will be useful for other things than the Heap, maybe even outside
of LibJS.
This commit is contained in:
Linus Groh 2020-11-08 12:48:16 +00:00 committed by Andreas Kling
parent 2d96a07b26
commit 9c3ead8f91
5 changed files with 136 additions and 40 deletions

View file

@ -26,6 +26,7 @@
#include <AK/Badge.h>
#include <AK/HashTable.h>
#include <AK/StackInfo.h>
#include <AK/TemporaryChange.h>
#include <LibCore/ElapsedTimer.h>
#include <LibJS/Heap/Allocator.h>
@ -35,17 +36,8 @@
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Object.h>
#include <setjmp.h>
#include <stdio.h>
#ifdef __serenity__
# include <serenity.h>
#elif __linux__ or __APPLE__
# include <pthread.h>
#endif
#ifdef __serenity__
//#define HEAP_DEBUG
#endif
namespace JS {
@ -151,39 +143,10 @@ void Heap::gather_conservative_roots(HashTable<Cell*>& roots)
for (size_t i = 0; i < ((size_t)sizeof(buf)) / sizeof(FlatPtr); i += sizeof(FlatPtr))
possible_pointers.set(raw_jmp_buf[i]);
FlatPtr stack_base;
size_t stack_size;
#ifdef __serenity__
if (get_stack_bounds(&stack_base, &stack_size) < 0) {
perror("get_stack_bounds");
ASSERT_NOT_REACHED();
}
#elif __linux__
pthread_attr_t attr = {};
if (int rc = pthread_getattr_np(pthread_self(), &attr) != 0) {
fprintf(stderr, "pthread_getattr_np: %s\n", strerror(-rc));
ASSERT_NOT_REACHED();
}
if (int rc = pthread_attr_getstack(&attr, (void**)&stack_base, &stack_size) != 0) {
fprintf(stderr, "pthread_attr_getstack: %s\n", strerror(-rc));
ASSERT_NOT_REACHED();
}
pthread_attr_destroy(&attr);
#elif __APPLE__
stack_base = (FlatPtr)pthread_get_stackaddr_np(pthread_self());
pthread_attr_t attr = {};
if (int rc = pthread_attr_getstacksize(&attr, &stack_size) != 0) {
fprintf(stderr, "pthread_attr_getstacksize: %s\n", strerror(-rc));
ASSERT_NOT_REACHED();
}
pthread_attr_destroy(&attr);
#endif
FlatPtr stack_reference = reinterpret_cast<FlatPtr>(&dummy);
FlatPtr stack_top = stack_base + stack_size;
auto& stack_info = m_vm.stack_info();
for (FlatPtr stack_address = stack_reference; stack_address < stack_top; stack_address += sizeof(FlatPtr)) {
for (FlatPtr stack_address = stack_reference; stack_address < stack_info.top(); stack_address += sizeof(FlatPtr)) {
auto data = *reinterpret_cast<FlatPtr*>(stack_address);
possible_pointers.set(data);
}

View file

@ -29,6 +29,7 @@
#include <AK/FlyString.h>
#include <AK/HashMap.h>
#include <AK/RefCounted.h>
#include <AK/StackInfo.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/CommonPropertyNames.h>
#include <LibJS/Runtime/ErrorTypes.h>
@ -165,6 +166,8 @@ public:
Value last_value() const { return m_last_value; }
void set_last_value(Badge<Interpreter>, Value value) { m_last_value = value; }
const StackInfo& stack_info() const { return m_stack_info; };
bool underscore_is_last_value() const { return m_underscore_is_last_value; }
void set_underscore_is_last_value(bool b) { m_underscore_is_last_value = b; }
@ -248,6 +251,8 @@ private:
ScopeType m_unwind_until { ScopeType::None };
FlyString m_unwind_until_label;
StackInfo m_stack_info;
bool m_underscore_is_last_value { false };
HashMap<String, Symbol*> m_global_symbol_map;