Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion backend-v2/bridge/ClassLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ ClassLookupByName(const char *className, void *jitEngine) {
JITEngine *engine = static_cast<JITEngine *>(jitEngine);
Class *cls = engine->threadsafeState.classRegistry.getCurrent(className);
if (!cls) {
throwInternalInconsistencyException("ClassLookup: class not found: " +
cls = engine->threadsafeState.protocolRegistry.getCurrent(className);
}
if (!cls) {
throwInternalInconsistencyException("ClassLookup: class/protocol not found: " +
std::string(className));
}
return cls;
Expand All @@ -27,6 +30,9 @@ ClassLookupByRegisterId(int32_t registerId, void *jitEngine) {
}
JITEngine *engine = static_cast<JITEngine *>(jitEngine);
Class *cls = engine->threadsafeState.classRegistry.getCurrent(registerId);
if (!cls) {
cls = engine->threadsafeState.protocolRegistry.getCurrent(registerId);
}
if (!cls) {
return nullptr;
}
Expand Down
82 changes: 55 additions & 27 deletions backend-v2/bridge/Exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ static std::mutex gSymbolizerMutex;

void registerJitFunction(
uword_t address, uword_t size, const char *name, const void *objectData,
size_t objectSize,
std::map<rt::SourceLocation, std::string> locationToForm) {
size_t objectSize, std::map<rt::SourceLocation, std::string> locationToForm,
std::map<rt::BaseSourceLocation, std::string> locationToContextForm) {
std::lock_guard<std::mutex> lock(gJitMapMutex);
JitFunctionEntry entry;
entry.size = size;
Expand All @@ -83,18 +83,19 @@ void registerJitFunction(
objectSize);
}
entry.locationToForm = std::move(locationToForm);
entry.locationToContextForm = std::move(locationToContextForm);
gJitFunctions[address] = std::move(entry);
}

void registerJitFunction(uword_t address, uword_t size, const char *name,
const void *objectData, size_t objectSize) {
registerJitFunction(address, size, name, objectData, objectSize, {});
registerJitFunction(address, size, name, objectData, objectSize, {}, {});
}

extern "C" void registerJitFunction_C(uword_t address, uword_t size,
const char *name, const void *objectData,
size_t objectSize) {
registerJitFunction(address, size, name, objectData, objectSize, {});
registerJitFunction(address, size, name, objectData, objectSize, {}, {});
}

static bool isInfrastructureFrame(const std::string &name,
Expand Down Expand Up @@ -460,15 +461,22 @@ LanguageException::LanguageException(const std::string &name, RTValue message,
for (uint32_t i = 0; i < inlinedInfo.getNumberOfFrames(); ++i) {
auto &info = inlinedInfo.getFrame(i);
if (info.FileName != "<invalid>") {
SourceLocation loc;
loc.file = info.FileName;
loc.line = info.Line;
loc.column = info.Column;
auto formIt = entry.locationToForm.find(loc);

if (formIt == entry.locationToForm.end() &&
!entry.locationToForm.empty()) {
// Try a fuzzy match based on basename and line
SourceLocation exactLoc(info.FileName, info.Line, info.Column, info.Discriminator);
BaseSourceLocation baseLoc(exactLoc);
// Try to find exact form
auto exactIt = entry.locationToForm.find(exactLoc);
if (exactIt != entry.locationToForm.end()) {
this->exactForm = exactIt->second;
}

// Try to find context form
auto contextIt = entry.locationToContextForm.find(baseLoc);
if (contextIt != entry.locationToContextForm.end()) {
this->form = contextIt->second;
}

// If either is missing, try fuzzy matching for BOTH
if (this->exactForm.empty() || this->form.empty()) {
std::string base = info.FileName;
size_t lastS = base.find_last_of('/');
if (lastS != std::string::npos)
Expand All @@ -480,23 +488,35 @@ LanguageException::LanguageException(const std::string &name, RTValue message,
if (kLastS != std::string::npos)
kBase = kBase.substr(kLastS + 1);

// If line is 0, we take the first match for the file
if (kBase == base &&
(k.line == (int)info.Line || info.Line == 0)) {
this->form = v;
std::stringstream locSs;
locSs << info.FileName << ":" << k.line << ":"
<< k.column;
this->sourceLocation = locSs.str();
foundForm = true;
break;
if (kBase == base && k.line == (int)info.Line) {
if (this->exactForm.empty() &&
k.column == (int)info.Column &&
k.discriminator == (int)info.Discriminator) {
this->exactForm = v;
}
}
}

for (auto const &[k, v] : entry.locationToContextForm) {
std::string kBase = k.file;
size_t kLastS = kBase.find_last_of('/');
if (kLastS != std::string::npos)
kBase = kBase.substr(kLastS + 1);

if (kBase == base && k.line == (int)info.Line && k.column == (int)info.Column) {
if (this->form.empty()) {
this->form = v;
}
}
}
} else if (formIt != entry.locationToForm.end()) {
this->form = formIt->second;
}

if (!this->form.empty() || !this->exactForm.empty()) {
std::stringstream locSs;
locSs << info.FileName << ":" << info.Line << ":"
<< info.Column;
if (info.Discriminator > 0)
locSs << " (disc " << info.Discriminator << ")";
this->sourceLocation = locSs.str();
foundForm = true;
break;
Expand All @@ -520,9 +540,10 @@ LanguageException::LanguageException(const std::string &name, RTValue message,

LanguageException::LanguageException(const std::string &name, RTValue message,
RTValue payload, const std::string &form,
const std::string &exactForm,
const std::string &sourceLocation)
: name(name), message(message), payload(payload), form(form),
sourceLocation(sourceLocation) {
exactForm(exactForm), sourceLocation(sourceLocation) {
capturedStack = captureCurrentStack();
}

Expand Down Expand Up @@ -612,6 +633,13 @@ LanguageException::toString(llvm::symbolize::LLVMSymbolizer &symbolizer,
<< c(Colors::RESET, useColor) << "\n";
}

if (!exactForm.empty() && exactForm != form) {
ss << "\n " << c(Colors::BOLD, useColor)
<< "Exact expression:" << c(Colors::RESET, useColor) << "\n";
ss << " " << Colors::CODE(useColor) << exactForm
<< c(Colors::RESET, useColor) << "\n";
}

ss << "\n " << c(Colors::BOLD, useColor)
<< "Stack Trace:" << c(Colors::RESET, useColor) << "\n";
symbolizeStackChain(ss, capturedStack, symbolizer, moduleName, slide, mode,
Expand Down Expand Up @@ -759,7 +787,7 @@ void throwCodeGenerationException(const std::string &errorMessage,
throw rt::LanguageException(
"CodeGenerationException",
RT_boxPtr(::String_createDynamicStr(retval.str().c_str())), RT_boxNil(),
form, locSs.str());
form, form, locSs.str());
}

void throwCodeGenerationException(
Expand Down
9 changes: 7 additions & 2 deletions backend-v2/bridge/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct JitFunctionEntry {
std::string name;
std::vector<uint8_t> objectData;
std::map<SourceLocation, std::string> locationToForm;
std::map<BaseSourceLocation, std::string> locationToContextForm;
};

struct CapturedStack {
Expand All @@ -52,6 +53,7 @@ class LanguageException : public std::exception {
RTValue message;
RTValue payload;
std::string form;
std::string exactForm;
std::string sourceLocation;
std::shared_ptr<CapturedStack> capturedStack;
mutable std::string cachedMessage;
Expand All @@ -60,7 +62,8 @@ class LanguageException : public std::exception {
public:
LanguageException(const std::string &name, RTValue message, RTValue payload);
LanguageException(const std::string &name, RTValue message, RTValue payload,
const std::string &form, const std::string &sourceLocation);
const std::string &form, const std::string &exactForm,
const std::string &sourceLocation);
LanguageException(const LanguageException &other);
LanguageException &operator=(const LanguageException &other);
~LanguageException() noexcept override;
Expand All @@ -77,6 +80,7 @@ class LanguageException : public std::exception {
RTValue getMessage() const { return message; }
RTValue getPayload() const { return payload; }
const std::string &getForm() const { return form; }
const std::string &getExactForm() const { return exactForm; }
const std::string &getSourceLocation() const { return sourceLocation; }
std::shared_ptr<CapturedStack> getCapturedStack() const {
return capturedStack;
Expand All @@ -100,7 +104,8 @@ throwCodeGenerationException(const std::string &errorMessage,
// New registration function with form mapping
void registerJitFunction(
uword_t addr, size_t size, const char *name, const void *objData,
size_t objSize, std::map<rt::SourceLocation, std::string> locationToForm);
size_t objSize, std::map<rt::SourceLocation, std::string> locationToForm,
std::map<rt::BaseSourceLocation, std::string> locationToContextForm);

} // namespace rt

Expand Down
Loading
Loading