Skip to content

Commit 0d3248e

Browse files
committed
COMMON: Fixed memory handling issues with UNITs
1 parent 7d7b491 commit 0d3248e

File tree

7 files changed

+46
-59
lines changed

7 files changed

+46
-59
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2016-04-20 (0.12.6)
2+
Fixed memory handling issues with UNITs
3+
14
2016-03-29 (0.12.6)
25
Removed TICKSPERSEC
36
Removed BALLOC, MALLOC and VADR keywords. Removed duplicate ENVIRON

Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ deb:
1313
test:
1414
(cd @TEST_DIR@ && make test)
1515

16+
leak-test:
17+
(cd src/platform/unix && make leak-test)
18+
1619
release:
1720
(cd $(SUBDIRS) && make release)
1821

src/common/scan.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ bc_symbol_rec_t *add_imptable_rec(const char *proc_name, int lib_id, int symbol_
108108
comp_imptable.elem = (bc_symbol_rec_t **)realloc(comp_imptable.elem,
109109
(comp_imptable.count + 1) * sizeof(bc_symbol_rec_t **));
110110
} else {
111-
comp_imptable.elem = (bc_symbol_rec_t **)malloc(comp_imptable.count * sizeof(bc_symbol_rec_t **));
111+
comp_imptable.elem = (bc_symbol_rec_t **)malloc(sizeof(bc_symbol_rec_t **));
112112
}
113113
comp_imptable.elem[comp_imptable.count] = sym;
114114
comp_imptable.count++;
@@ -128,7 +128,7 @@ void add_libtable_rec(const char *lib, int uid, int type) {
128128
comp_libtable.elem = (bc_lib_rec_t **)realloc(comp_libtable.elem,
129129
(comp_libtable.count + 1) * sizeof(bc_lib_rec_t **));
130130
} else {
131-
comp_libtable.elem = (bc_lib_rec_t **)malloc(comp_libtable.count * sizeof(bc_lib_rec_t **));
131+
comp_libtable.elem = (bc_lib_rec_t **)malloc(sizeof(bc_lib_rec_t **));
132132
}
133133
comp_libtable.elem[comp_libtable.count] = imlib;
134134
comp_libtable.count++;
@@ -4346,31 +4346,32 @@ byte_code comp_create_bin() {
43464346
hdr.var_count = comp_varcount;
43474347
hdr.lab_count = comp_labcount;
43484348
hdr.data_ip = comp_first_data_ip;
4349-
hdr.size = sizeof(bc_head_t) + comp_prog.count + (comp_labcount * ADDRSZ)
4350-
+ sizeof(unit_sym_t) * comp_expcount + sizeof(bc_lib_rec_t) * comp_libcount
4351-
+ sizeof(bc_symbol_rec_t) * comp_impcount;
4352-
4349+
hdr.size = sizeof(bc_head_t) + comp_prog.count + (comp_labcount * ADDRSZ) +
4350+
sizeof(unit_sym_t) * comp_expcount +
4351+
sizeof(bc_lib_rec_t) * comp_libcount +
4352+
sizeof(bc_symbol_rec_t) * comp_impcount;
43534353
if (comp_unit_flag) {
43544354
hdr.size += sizeof(unit_file_t);
43554355
}
43564356

43574357
hdr.lib_count = comp_libcount;
43584358
hdr.sym_count = comp_impcount;
4359-
43604359
if (comp_unit_flag) {
4360+
memset(&uft, 0, sizeof(unit_file_t));
4361+
43614362
// it is a unit... add more info
4362-
bc.size = hdr.size + 4;
4363+
bc.size = hdr.size;
43634364
bc.code = malloc(bc.size);
43644365

43654366
// unit header
43664367
memcpy(&uft.sign, "SBUn", 4);
43674368
uft.version = 1;
4369+
43684370
strcpy(uft.base, comp_unit_name);
43694371
uft.sym_count = comp_expcount;
43704372

4371-
cp = bc.code;
4372-
memcpy(cp, &uft, sizeof(unit_file_t));
4373-
cp += sizeof(unit_file_t);
4373+
memcpy(bc.code, &uft, sizeof(unit_file_t));
4374+
cp = bc.code + sizeof(unit_file_t);
43744375

43754376
// unit symbol table (export)
43764377
for (i = 0; i < uft.sym_count; i++) {
@@ -4423,7 +4424,6 @@ byte_code comp_create_bin() {
44234424
if (!opt_quiet && !opt_interactive) {
44244425
log_printf("\n");
44254426
log_printf(RES_NUMBER_OF_VARS, comp_varcount, comp_varcount - 18);
4426-
// system variables
44274427
log_printf(RES_NUMBER_OF_LABS, comp_labcount);
44284428
log_printf(RES_NUMBER_OF_UDPS, comp_udpcount);
44294429
log_printf(RES_CODE_SIZE, comp_prog.count);
@@ -4514,11 +4514,12 @@ int comp_compile(const char *sb_file_name) {
45144514
}
45154515
}
45164516

4517+
int is_unit = comp_unit_flag;
45174518
comp_close();
45184519
close_task(tid);
45194520
activate_task(prev_tid);
45204521

4521-
if (opt_nosave) {
4522+
if (opt_nosave && !is_unit) {
45224523
ctask->bytecode = bc.code;
45234524
} else if (bc.code) {
45244525
free(bc.code);

src/common/smbas.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ extern "C" {
2828
*/
2929
typedef struct {
3030
char sign[4]; /**< always "SBEx" */
31-
byte unused;
32-
word ver; /**< version of this structure */
31+
dword ver; /**< version of this structure */
3332
dword sbver; /**< version of SB */
3433
dword flags; /**< flags
3534
b0 = Big-endian CPU
@@ -41,12 +40,10 @@ typedef struct {
4140
dword var_count; /**< number of variables */
4241
dword lab_count; /**< number of labels */
4342
dword data_ip; /**< default DATA position */
44-
dword uds_tab_ip; /**< location of struct mapping table */
4543

4644
// ver 2
47-
word lib_count; /**< libraries count (needed units) */
45+
dword lib_count; /**< libraries count (needed units) */
4846
dword sym_count; /**< symbol count (linked-symbols) */
49-
char reserved[26];
5047
} bc_head_t;
5148

5249
/**
@@ -173,7 +170,6 @@ EXTERN char gsb_last_errmsg[SB_ERRMSG_SIZE + 1]; /**< last error message */
173170
#define prog_libtable ctask->sbe.exec.libtable
174171
#define prog_symtable ctask->sbe.exec.symtable
175172
#define prog_exptable ctask->sbe.exec.exptable
176-
#define prog_uds_tab_ip ctask->sbe.exec.uds_tab_ip
177173
#define prog_timer ctask->sbe.exec.timer
178174
#define comp_extfunctable ctask->sbe.comp.extfunctable
179175
#define comp_extfunccount ctask->sbe.comp.extfunccount
@@ -207,7 +203,6 @@ EXTERN char gsb_last_errmsg[SB_ERRMSG_SIZE + 1]; /**< last error message */
207203
#define comp_udpcount ctask->sbe.comp.udpcount
208204
#define comp_udpsize ctask->sbe.comp.udpsize
209205
#define comp_next_field_id ctask->sbe.comp.next_field_id
210-
#define comp_uds_tab_ip ctask->sbe.comp.uds_tab_ip
211206
#define comp_use_global_vartable ctask->sbe.comp.use_global_vartable
212207
#define comp_stack ctask->sbe.comp.stack
213208
#define comp_sp ctask->sbe.comp.stack.count

src/common/units.c

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void unit_mgr_close() {
4141
unit_count = 0;
4242
if (units) {
4343
free(units);
44+
units = NULL;
4445
}
4546
}
4647

@@ -105,7 +106,7 @@ int find_unit(const char *name, char *file) {
105106
* @return the unit handle or -1 on error
106107
*/
107108
int open_unit(const char *file) {
108-
int h, i;
109+
int h;
109110
unit_t u;
110111
int uid = -1;
111112

@@ -161,34 +162,23 @@ int open_unit(const char *file) {
161162
}
162163

163164
// load symbol-table
164-
u.symbols = (unit_sym_t *) malloc(u.hdr.sym_count * sizeof(unit_sym_t));
165-
read(h, u.symbols, u.hdr.sym_count * sizeof(unit_sym_t));
165+
if (u.hdr.sym_count) {
166+
u.symbols = (unit_sym_t *)malloc(u.hdr.sym_count * sizeof(unit_sym_t));
167+
read(h, u.symbols, u.hdr.sym_count * sizeof(unit_sym_t));
168+
}
166169

167170
// setup the rest
168171
strcpy(u.name, unitname);
169172
u.status = unit_loaded;
170173

171174
// add unit
172-
if (unit_count == 0) {
173-
// this is the first unit
174-
uid = unit_count;
175-
unit_count++;
176-
units = (unit_t *) malloc(unit_count * sizeof(unit_t));
177-
} else {
178-
// search for an empty entry
179-
for (i = 0; i < unit_count; i++) {
180-
if (units[i].status == unit_undefined) {
181-
uid = i;
182-
break;
183-
}
184-
}
175+
uid = unit_count;
176+
unit_count++;
185177

186-
// resize the table
187-
if (uid == -1) {
188-
uid = unit_count;
189-
unit_count++;
190-
units = (unit_t *) realloc(units, unit_count * sizeof(unit_t));
191-
}
178+
if (units == NULL) {
179+
units = (unit_t *)malloc(unit_count * sizeof(unit_t));
180+
} else {
181+
units = (unit_t *)realloc(units, unit_count * sizeof(unit_t));
192182
}
193183

194184
// copy unit's data
@@ -207,9 +197,7 @@ int open_unit(const char *file) {
207197
*/
208198
int close_unit(int uid) {
209199
if (uid >= 0) {
210-
unit_t *u;
211-
212-
u = &units[uid];
200+
unit_t *u = &units[uid];
213201
if (u->status == unit_loaded) {
214202
u->status = unit_undefined;
215203
free(u->symbols);
@@ -233,11 +221,8 @@ int import_unit(int uid) {
233221
char buf[SB_KEYWORD_SIZE + 1];int i;
234222

235223
if (uid >= 0) {
236-
unit_t *u;
237-
238-
u = &units[uid];
224+
unit_t *u = &units[uid];
239225
if (u->status == unit_loaded) {
240-
241226
for (i = 0; i < u->hdr.sym_count; i++) {
242227
// build the name
243228
// with any path component removed from the name
@@ -246,13 +231,13 @@ int import_unit(int uid) {
246231
dir_sep ? dir_sep + 1 : u->hdr.base, u->symbols[i].symbol);
247232

248233
switch (u->symbols[i].type) {
249-
case stt_function:
234+
case stt_function:
250235
comp_add_external_func(buf, uid | UID_UNIT_BIT);
251236
break;
252-
case stt_procedure:
237+
case stt_procedure:
253238
comp_add_external_proc(buf, uid | UID_UNIT_BIT);
254239
break;
255-
case stt_variable:
240+
case stt_variable:
256241
comp_add_external_var(buf, uid | UID_UNIT_BIT);
257242
break;
258243
};
@@ -272,7 +257,7 @@ int import_unit(int uid) {
272257
/**
273258
* execute a call to a unit
274259
*/
275-
int unit_exec(int lib_id, int index, var_t * ret) {
260+
int unit_exec(int lib_id, int index, var_t *ret) {
276261
unit_sym_t *us; // unit's symbol data
277262
bc_symbol_rec_t *ps; // program's symbol data
278263
int my_tid;
@@ -282,15 +267,12 @@ int unit_exec(int lib_id, int index, var_t * ret) {
282267
ps = &prog_symtable[index];
283268
us = &(taskinfo(ps->task_id)->sbe.exec.exptable[ps->exp_idx]);
284269

285-
//
286270
switch (ps->type) {
287271
case stt_variable:
288272
break;
289273
case stt_procedure:
290274
exec_sync_variables(1);
291-
292275
cmd_call_unit_udp(kwPROC, ps->task_id, us->address, 0);
293-
294276
activate_task(ps->task_id);
295277
if (prog_error) {
296278
gsb_last_error = prog_error;
@@ -309,9 +291,7 @@ int unit_exec(int lib_id, int index, var_t * ret) {
309291

310292
case stt_function:
311293
exec_sync_variables(1);
312-
313294
cmd_call_unit_udp(kwFUNC, ps->task_id, us->address, us->vid);
314-
315295
activate_task(ps->task_id);
316296
if (prog_error) {
317297
gsb_last_error = prog_error;

src/common/units.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ typedef enum {
6464
* unit: file header
6565
*/
6666
typedef struct {
67-
dword sign[4]; /**< Always "SBUn" */
67+
char sign[4]; /**< Always "SBUn" */
6868
int version; /**< version of this structure, always 1 */
6969
char base[SB_KEYWORD_SIZE + 1]; /**< unit-base name */
7070
int sym_count; /**< number of symbols */

src/platform/unix/Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ test: ${bin_PROGRAMS}
3737
cat test.out; \
3838
fi ; \
3939
done;
40+
41+
leak-test: ${bin_PROGRAMS}
42+
@for utest in $(UNIT_TESTS); do \
43+
valgrind --leak-check=full ./${bin_PROGRAMS} ${TEST_DIR}/$${utest}.bas 1>/dev/null; \
44+
done;

0 commit comments

Comments
 (0)