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

Fixing memory issues #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions src/jet/live/DataTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ namespace jet
uint64_t relocationSymbolHash = 0; /** Relocation symbol hash. */

uint8_t size = 0; /** Size of relocation entry, in bytes (4, 8). */

void apply(const LiveContext* context); /** Applies relocation. */
};

// Mach-O specific structures
Expand Down
1 change: 1 addition & 0 deletions src/jet/live/FunctionsHookingStep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace jet
} else {
hookedFunctions++;
}
subhook_free(hook);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/jet/live/StaticsCopyStep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace jet
break;
}
}
if (!oldVarPtr) {
if (!oldVarPtr || oldVarSize == 0) {
continue;
}

Expand Down
37 changes: 31 additions & 6 deletions src/jet/live/_linux/ElfProgramInfoLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ namespace jet
const auto& section = elfFile.sections[i];
elfContext.sectionNames[i] = section->get_name();

// This one is needed to apply relocations of STT_SECTION symbols
Symbol sectionSymbol;
sectionSymbol.checkHash = false;
sectionSymbol.name = section->get_name();
sectionSymbol.runtimeAddress = baseAddress + section->get_offset();
sectionSymbol.size = 0;
res.variables[sectionSymbol.name].push_back(sectionSymbol);

if (section->get_type() == SHT_SYMTAB) {
const ELFIO::symbol_section_accessor symbols{elfFile, section};
for (ElfW(Xword) j = 0; j < symbols.get_symbols_num(); j++) {
Expand Down Expand Up @@ -297,16 +305,16 @@ namespace jet
* Retrieved via r_offset.
* S: Relocation entry’s correspondent symbol value. Z: Size of relocations entry’s symbol.
*/
uintptr_t symRelAddr = 0;
intptr_t symRelAddr = 0;
switch (type) {
// Link-time relocation, we should fix it by ourself
case R_X86_64_PC32: // 32, S + A – P
reloc.size = sizeof(int32_t);
symRelAddr = static_cast<uintptr_t>(addend + 4);
symRelAddr = static_cast<intptr_t>(addend + 4);
break;
case R_X86_64_PC64: // 64, S + A – P
reloc.size = sizeof(int64_t);
symRelAddr = static_cast<uintptr_t>(addend + 4);
symRelAddr = static_cast<intptr_t>(addend + 4);
break;

// Load time relocations, will be fixed by dynamic linker
Expand Down Expand Up @@ -343,9 +351,26 @@ namespace jet
continue;
}

auto symFound = symbolsInSections[sectionIndex].find(symRelAddr);
if (symFound == symbolsInSections[sectionIndex].end()) {
context->events->addLog(LogSeverity::kError, "WTF");
const auto& symsInSection = symbolsInSections[sectionIndex];
auto symFound = symsInSection.find(static_cast<uintptr_t>(symRelAddr));
if (symRelAddr < 0) {
symFound = symsInSection.begin();
}
if (symFound == symsInSection.end()) {
symFound = symsInSection.upper_bound(static_cast<uintptr_t>(symRelAddr));
if (symFound != symsInSection.begin()) {
symFound--;
}
}
if (symFound == symsInSection.end()) {
context->events->addLog(LogSeverity::kError,
"WTF: file " + el + ", " +
"section " + section->get_name() + ", " +
"target section index " + std::to_string(sectionIndex) + ", " +
"offset " + std::to_string(offset) + ", " +
"symbol " + std::to_string(symbol) + ", " +
"type " + std::to_string(type) + ", "
"addend " + std::to_string(addend));
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/jet/live/_linux/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <elf.h>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <sstream>
#include <unistd.h>
#include <iterator>

namespace jet
{
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ target_sources(tests
src/utility/ReloadAfterFailedCompilation1.cpp
src/utility/ReloadAfterFailedCompilation2.cpp
src/utility/OneFrameCompileReload.cpp
src/utility/ArrayRelocation.cpp

src/good/ClassInstanceMethod_test.cpp
src/good/CommonSection_test.cpp
Expand Down Expand Up @@ -99,6 +100,7 @@ target_sources(tests
src/good/LostModification_test.cpp
src/good/ReloadAfterFailedCompilation_test.cpp
src/good/OneFrameCompileReload_test.cpp
src/good/ArrayRelocation_test.cpp

src/bad/LambdaFunctionWithCapturesBadCase2_test.cpp
src/bad/LambdaFunctionWithCapturesBadCase_test.cpp
Expand Down
20 changes: 20 additions & 0 deletions tests/src/good/ArrayRelocation_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

#include <catch.hpp>
#include "Globals.hpp"
#include "WaitForReload.hpp"
#include <iostream>
#include "utility/ArrayRelocation.hpp"

TEST_CASE("Relocation of array", "[variable]")
{
arelTouchValues();

auto beforeReload = arelGetValue();
REQUIRE(beforeReload == 543);

std::cout << "JET_TEST: disable(arel_var:1); enable(arel_var:2)" << std::endl;
waitForReload();

auto afterReload = arelGetValue();
REQUIRE(afterReload == 878);
}
24 changes: 24 additions & 0 deletions tests/src/utility/ArrayRelocation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#include "ArrayRelocation.hpp"

namespace
{
int values[] = {
533, 868
};
}

int arelTouchValues()
{
int res = 0;
for (auto& el : values) {
res += (el += 10);
}
return res;
}

int arelGetValue()
{
return values[0]; // <jet_tag: arel_var:1>
// return values[1]; // <jet_tag: arel_var:2>
}
5 changes: 5 additions & 0 deletions tests/src/utility/ArrayRelocation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#pragma once

int arelTouchValues();
int arelGetValue();