Skip to content

Commit 8f9bc85

Browse files
committed
Fix several memory leak/uaf issues
1 parent d9a8b86 commit 8f9bc85

11 files changed

Lines changed: 37 additions & 19 deletions

File tree

include/openminecraft/io/om_io_parser.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace openminecraft::io
99
class OMParser
1010
{
1111
public:
12-
OMParser(std::shared_ptr<std::istream> stream);
12+
OMParser(std::istream *stream);
1313
~OMParser();
1414
bool check();
1515

1616
protected:
17-
std::shared_ptr<std::istream> source;
17+
std::istream *source;
1818
};
1919
} // namespace openminecraft::io
2020

21-
#endif
21+
#endif

include/openminecraft/vm/classfile/om_class_file.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ class OMClassFileParser : public io::OMParser
711711
using ConstantMapping = std::unordered_map<uint16_t, std::shared_ptr<OMClassConstant>>;
712712

713713
public:
714-
OMClassFileParser(std::shared_ptr<std::istream> stream);
714+
OMClassFileParser(std::istream *stream);
715715
~OMClassFileParser();
716716
util::OMResult<std::shared_ptr<OMClassFile>, err::OMValidationError> parse();
717717
ConstantMapping buildConstantMapping(std::vector<std::shared_ptr<OMClassConstant>> c);

include/openminecraft/vm/pixeltower/om_pixeltower.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class OMPixelTower
2121
~OMPixelTower();
2222

2323
void loadClass(std::shared_ptr<classfile::OMClassFile> file);
24-
void loadClass(std::shared_ptr<std::istream> file);
24+
void loadClass(std::istream *file);
2525
std::shared_ptr<OMClass> fetchClass(std::string name);
2626

2727
void execute(std::string clazz, std::string name, std::string desc);
@@ -45,4 +45,4 @@ class OMPixelTower
4545
};
4646
} // namespace openminecraft::vm::pixeltower
4747

48-
#endif
48+
#endif

src/boot/entrypoint.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ int boot(std::vector<std::string> args)
180180
0;
181181
}
182182
});
183-
pt->loadClass(std::make_shared<std::ifstream>(commandBuffer[2], std::ios::binary));
183+
auto stti = new std::ifstream(commandBuffer[2], std::ios::binary);
184+
pt->loadClass(stti);
185+
delete stti;
184186

185187
auto cls = pt->fetchClass("java/lang/String");
186188
void *arr = pt->allocateArray(cls, 2);
@@ -216,7 +218,9 @@ int boot(std::vector<std::string> args)
216218
searchDir(m, std::filesystem::directory_iterator(commandBuffer[2]));
217219
for (auto a : m)
218220
{
219-
pt->loadClass(std::make_shared<std::ifstream>(a, std::ios::binary));
221+
auto stt = new std::ifstream(a, std::ios::binary);
222+
pt->loadClass(stt);
223+
delete stt;
220224
}
221225

222226
auto cmdPrint = [&](std::any data) {

src/i18n/om_i18n_res.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "nlohmann/json_fwd.hpp"
55
#include "openminecraft/log/om_log_common.hpp"
66
#include "openminecraft/vfs/om_vfs_base.hpp"
7+
#include "openminecraft/mem/om_mem_allocator.hpp"
78
#include <SDL3/SDL_locale.h>
89
#include <algorithm>
910
#include <cctype>
@@ -47,6 +48,7 @@ void load()
4748
std::transform(c.begin(), c.end(), c.begin(), ::tolower);
4849
locale = fmt::format("{}_{}", loc[0]->language, c);
4950
}
51+
mem::allocator::tracedFreeSDL(loc);
5052

5153
records.clear();
5254
translates.clear();
@@ -117,4 +119,4 @@ std::string translate(std::string key)
117119
}
118120
return key;
119121
}
120-
} // namespace openminecraft::i18n::res
122+
} // namespace openminecraft::i18n::res

src/io/om_io_parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace openminecraft::io
44
{
5-
OMParser::OMParser(std::shared_ptr<std::istream> stream) : source(stream)
5+
OMParser::OMParser(std::istream *stream) : source(stream)
66
{
77
}
88
OMParser::~OMParser()
@@ -12,4 +12,4 @@ bool OMParser::check()
1212
{
1313
return source->good();
1414
}
15-
} // namespace openminecraft::io
15+
} // namespace openminecraft::io

src/mem/om_mem_allocator.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,16 @@ void operator delete[](void *p) noexcept
6969
{
7070
rec({Free, p, heapSize(p), OM_MEM_CPP});
7171
free(p);
72-
}
72+
}
73+
74+
void operator delete(void *p, size_t l) noexcept
75+
{
76+
rec({Free, p, l, OM_MEM_CPP});
77+
free(p);
78+
}
79+
80+
void operator delete[](void *p, size_t l) noexcept
81+
{
82+
rec({Free, p, l, OM_MEM_CPP});
83+
free(p);
84+
}

src/vm/classfile/om_class_file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using namespace openminecraft::util;
1414

1515
namespace openminecraft::vm::classfile
1616
{
17-
OMClassFileParser::OMClassFileParser(std::shared_ptr<std::istream> str) : io::OMParser(str)
17+
OMClassFileParser::OMClassFileParser(std::istream *str) : io::OMParser(str)
1818
{
1919
this->logger = std::make_shared<log::OMLogger>("OMClassFileParser", this);
2020
}

src/vm/pixeltower/v0/om_pixeltower.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void OMPixelTower::loadClass(std::shared_ptr<classfile::OMClassFile> file)
7474

7575
classloader->appendStagingClass(file);
7676
}
77-
void OMPixelTower::loadClass(std::shared_ptr<std::istream> file)
77+
void OMPixelTower::loadClass(std::istream *file)
7878
{
7979
auto parser = std::make_shared<classfile::OMClassFileParser>(file);
8080
auto clsres = parser->parse();
@@ -118,4 +118,4 @@ void *OMPixelTower::allocateMultiArray(OMArrayType type, int *lengths, int dim)
118118
{
119119
return mm->allocateArray(type, lengths, dim);
120120
}
121-
} // namespace openminecraft::vm::pixeltower
121+
} // namespace openminecraft::vm::pixeltower

src/vm/pixeltower/v0/om_pixeltower_interpreter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ void OMInterpreter::execute(std::shared_ptr<OMClass> clazz, std::shared_ptr<OMMe
182182
while (frame->offset < mi->code->codeLength)
183183
{
184184
operands++;
185+
tower.mm->gc();
185186
switch (codeArea[frame->offset])
186187
{
187188
case op_nop: {

0 commit comments

Comments
 (0)