Skip to content

Commit bc757c8

Browse files
committed
fix(objectmanager): fix some bugs
1. fix some bugs of dependency-libraries 2. fix some bugs of object-manager 3. fix a miswording please refer to the work log for details
1 parent 718016b commit bc757c8

File tree

14 files changed

+242
-100
lines changed

14 files changed

+242
-100
lines changed

demos/demo.stvc

0 Bytes
Binary file not shown.

demos/demo1.stvc

0 Bytes
Binary file not shown.

demos/demo2.stvc

0 Bytes
Binary file not shown.

doc/AstRunner开发文档.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ void ThrowUnknownOperatorError();
7070
void ThrowUnknownMemberError(int id);
7171
```
7272
73-
2. 利用excute方法执行Running-Ast,它的函数原型是:
73+
2. 利用execute方法执行Running-Ast,它的函数原型是:
7474
7575
```C++
76-
RetStatus excute(
76+
RetStatus execute(
7777
AstNode* main_node, bool isGC, int vm_mem_limit,
7878
ArrayList<DataType*> tableConst, ArrayList<String> args,
7979
STMException* e

doc/工作日志/20250423.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 2025/04/23 工作日志
2+
3+
本次更新发行``2.4.45``
4+
本次更新修复了一些漏洞,提升了对象管理器的性能。
5+
6+
### 修复了依赖库的标准C实现中的漏洞
7+
8+
1. 对于``ArrayList.hpp``中销毁内存的实现,不应该直接调用析构函数,这会导致不可控的结果。目前我封装了一个destroy_cache方法
9+
2. 对于``strie.h``中的``ClearTrie````DestroyTrie``,我整理了代码逻辑,使其可读性更强
10+
3. 对于``ByteMap.hpp``中的``getValList``,我在判断循环条件时忘记加上逻辑非运算符了,这会导致该函数直接返回一个空列表;循环中加入的元素全部都是根节点而不是当前遍历节点。我解决了这些问题
11+
12+
### 整理了AstRunner的代码逻辑
13+
14+
1. 对于获取空对象,我不直接调用``getNullConst``,而是调用``MallocObject````MallocObject``再返回``getNullConst``,这让代码整体更统一了
15+
2. 以往的某些函数可能会在返回时直接返回``MallocObject``,而不先判断``MallocObject``是否抛出异常,该问题已被解决
16+
3. 类对象在被初始化后,会调用``PopScope``,而对象管理器会误删这个类对象。为此我专门在对象管理器中加入了``PopMemberTabScope``,来防止类对象被误删
17+
18+
### 修复并优化了ObjectManager
19+
20+
做了如下优化:
21+
22+
1. 对于``MarkScopeObject``的数列扫描和对象扫描部分,我整理并删除了冗余代码。
23+
2.``getNullConst``类似的,我设立了“整数复用池”,这是一个存储了从-5到+256的整数对象的数组。在申请整数时,如果整数在从-5到+256的范围内,就会直接返回该数组中的对象。这样一来,当代码频繁申请小整数时,对象管理器不用反复新建对象,而是可以直接返回数组中的元素。此举措减少了内存分配次数,提升了性能。
24+
3. 为了统一对象申请,我使用了模板特化,并将对象申请的主体部分迁移到了``_malloc_object``里(不建议直接调用此方法),来特别优化空对象和整数对象的申请。这样申请对象就可以全部统一使用``MallocObject``
25+
26+
解决了如下问题:
27+
28+
1. 对于``FreeObject``,不应该直接删除,而是要用内存池来帮忙删除,并且更新对象占用内存大小的统计变量
29+
2. 对于``~ObjectManager``,应该把内存池的剩余空闲内存也清理掉
30+
3. 在初始化一个新的对象时,要把其gc_flag置为false
31+
32+
### 修复了一个用词错误
33+
34+
我注意到执行的英文单词是“execute”而不是“excute”,目前所有相关的用词错误已被修复。

doc/工作日志后记集.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
本文档记录了作者在项目开发时遇到的事情及其心路历程,你可以将本文档视作“番外篇”。
77
本文档补充了我在一些时刻做出一些决定的理由。
88

9+
### 2025/04/23 后记
10+
11+
这是离开赛场后的第一次更新。算是一次常规更新。
12+
比赛方面,并没有打出我期待的成绩,但这个成绩说到底也是我能预料到的。我接触了很多比我优秀的人,他们也给了我一些意见,谢谢!
13+
本次的提交算是一个常规提交,其实这些漏洞我花了四天时间修复,这可能是今年以来我遇到最复杂的漏洞了,这些漏洞互相遮掩对方,使得我以前从来没有发现,并且这些致命性漏洞在机缘巧合之下,可以让程序正常运行!
14+
有关ArrayList的漏洞是最让人啼笑皆非的——我只要开启O2优化,就会崩溃,开启O0则什么事情都没有。最后花了两天时间,终于调查出是ArrayList的析构函数被优化出了一些不可控的行为。
15+
我也要开始着手准备明年的比赛了,虽然还有好几个月,但是留给我的时间不多了。我可能会开创新项目,但是怎么对新项目和旧项目进行平衡,这是个问题。
16+
917
### 2025/04/12 后记
1018

1119
本更新提交于赛场上。

include/stdc_implemented/ArrayList.hpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ template<typename T> class ArrayList {
2121
* 我们规定:当实际占用的容量小于缓冲区容量的二分之一时,将缓冲区缩小为原来的一半
2222
* 这样,我们将ArrayList的分配次数从线性级将为对数级
2323
*/
24-
24+
2525
T *cache = NULL;
2626
int cache_length = 0;
2727
int length = 0;
@@ -36,7 +36,7 @@ template<typename T> class ArrayList {
3636
// 尽可能地复制
3737
}
3838

39-
this->~ArrayList(); // 销毁原来的cache
39+
destroy_cache(); // 销毁原来的cache
4040

4141
cache = temp;
4242
cache_length = cache_length / 2;
@@ -48,14 +48,20 @@ template<typename T> class ArrayList {
4848
temp[i] = cache[i];
4949
}
5050

51-
this->~ArrayList(); // 销毁原来的cache
51+
destroy_cache(); // 销毁原来的cache
5252

5353
cache = temp;
5454
cache_length = cache_length * 2;
5555
}
5656
// 如果不满足缩小条件,也不满足扩容条件,就什么都不做
5757
}
5858

59+
void destroy_cache() {
60+
if (cache != NULL) {
61+
delete[] cache;
62+
}
63+
}
64+
5965
public:
6066
ArrayList() {
6167
cache = new T[1];
@@ -98,7 +104,7 @@ template<typename T> class ArrayList {
98104
}
99105

100106
ArrayList<T> &operator=(const ArrayList<T> &list) {
101-
this->~ArrayList();
107+
destroy_cache();
102108

103109
if (list.size() == 0) {
104110
// 缓冲区至少要有一个元素
@@ -119,7 +125,7 @@ template<typename T> class ArrayList {
119125
}
120126

121127
ArrayList<T> &operator=(ArrayList<T> &&list) {
122-
this->~ArrayList();
128+
destroy_cache();
123129

124130
cache = list.cache;
125131
cache_length = list.cache_length;
@@ -164,7 +170,7 @@ template<typename T> class ArrayList {
164170
} // 判断是否为空
165171

166172
void clear() {
167-
this->~ArrayList(); // 销毁
173+
destroy_cache(); // 销毁
168174
cache = new T[1]; // 新建缓冲区
169175
cache_length = 1;
170176
length = 0; // 长度清零
@@ -197,8 +203,6 @@ template<typename T> class ArrayList {
197203
}
198204

199205
~ArrayList() {
200-
if (cache != NULL) {
201-
delete[] cache;
202-
}
206+
destroy_cache();
203207
}
204208
};

include/stdc_implemented/ByteMap.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ template<typename T> class ByteMap {
6363

6464
stack.push(map.get());
6565

66-
while (stack.empty()) {
66+
while (stack.empty()==0) {
6767
STRIE *temp = stack.pop();
6868
if (temp != NULL) {
6969
for (int i = 0; i < 256; i++) {
7070
stack.push(temp->child[i]);
7171
}
72-
if (map.get()->isexist == 1) {
72+
if (temp->isexist == 1) {
7373
result.add(cast_class(list_T, temp->data));
7474
}
7575
}

include/stdc_implemented/strie.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,12 @@ int ClearTrie(STRIE* trie) {
160160
}
161161
PushStack(stack, trie);
162162
while(StackEmpty(stack)==0) {
163-
STRIE* tmp = (STRIE*)PopStack(stack);
164-
STRIE** tmp2 = tmp->child;
165-
if(tmp!=NULL) {
163+
STRIE* node = (STRIE*)PopStack(stack);
164+
if(node!=NULL) {
166165
for(int i=0; i<256; i++) {
167-
PushStack(stack, tmp2[i]);
166+
PushStack(stack,node->child[i]);
168167
}
169-
if(tmp!=trie) free(tmp);
168+
if(node!=trie) free(node);
170169
}
171170
}
172171
DestroyStack(stack);
@@ -177,9 +176,9 @@ int DestroyTrie(STRIE* trie) {
177176
if(trie==NULL) {
178177
return 0;
179178
}
180-
ClearTrie(trie);
179+
int status = ClearTrie(trie);
181180
free(trie);
182-
return 1;
181+
return status;
183182
}
184183

185184
int TrieEmpty(STRIE* trie) {

src/Stamon.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ namespace stamon {
163163

164164
vm::AstRunner runner;
165165

166-
runner.excute(
166+
runner.execute(
167167
running_node, isGC, MemLimit, converter.tableConst,
168168
ArrayList<String>(), PoolCacheSize ,ex
169169
);

0 commit comments

Comments
 (0)