Skip to content

Commit cad07af

Browse files
lantigaDvirDukhan
authored andcommitted
Safely add to arrays + fix for #443 (#449)
* Make sure we reassign the pointer in array_append * Fix case-sensitive comparison for devicestr * Fix sanitizer tests (cherry picked from commit 5c7813e)
1 parent 0a9d953 commit cad07af

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

src/backends/tensorflow.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,12 @@ RAI_Model *RAI_ModelCreateTF(RAI_Backend backend, const char* devicestr, RAI_Mod
392392

393393
char **inputs_ = array_new(char*, ninputs);
394394
for (long long i=0; i<ninputs; i++) {
395-
array_append(inputs_, RedisModule_Strdup(inputs[i]));
395+
inputs_ = array_append(inputs_, RedisModule_Strdup(inputs[i]));
396396
}
397397

398398
char **outputs_ = array_new(char*, noutputs);
399399
for (long long i=0; i<noutputs; i++) {
400-
array_append(outputs_, RedisModule_Strdup(outputs[i]));
400+
outputs_ = array_append(outputs_, RedisModule_Strdup(outputs[i]));
401401
}
402402

403403
char* buffer = RedisModule_Calloc(modellen, sizeof(*buffer));

src/background_workers.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ void *RedisAI_Run_ThreadMain(void *arg) {
111111
}
112112
evicted_items = array_new(queueItem *, run_queue_len);
113113
batch_rinfo = array_new(RedisAI_RunInfo *, run_queue_len);
114-
115-
array_append(evicted_items, item);
116-
array_append(batch_rinfo, rinfo);
114+
// We add the current item to the list of evicted items. If it's the
115+
// first time around this will be the queue front.
116+
evicted_items = array_append(evicted_items, item);
117+
batch_rinfo = array_append(batch_rinfo, rinfo);
117118

118119
if (rinfo->sctx) {
119120
break;
@@ -152,8 +153,10 @@ void *RedisAI_Run_ThreadMain(void *arg) {
152153
break;
153154
}
154155

155-
array_append(evicted_items, next_item);
156-
array_append(batch_rinfo, next_rinfo);
156+
// If all previous checks pass, then keep track of the item
157+
// in the list of evicted items
158+
evicted_items = array_append(evicted_items, next_item);
159+
batch_rinfo = array_append(batch_rinfo, next_rinfo);
157160

158161
current_batchsize += next_batchsize;
159162
next_item = queueNext(next_item);

src/dag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ int RedisAI_DagRunSyntaxParser(RedisModuleCtx *ctx, RedisModuleString **argv,
420420
rinfo->use_local_context = 1;
421421
RAI_DagOp *currentDagOp = NULL;
422422
RAI_InitDagOp(&currentDagOp);
423-
array_append(rinfo->dagOps, currentDagOp);
423+
rinfo->dagOps = array_append(rinfo->dagOps, currentDagOp);
424424

425425
int persistFlag = 0;
426426
int loadFlag = 0;
@@ -463,7 +463,7 @@ int RedisAI_DagRunSyntaxParser(RedisModuleCtx *ctx, RedisModuleString **argv,
463463
rinfo->dagNumberCommands++;
464464
RAI_DagOp *currentDagOp = NULL;
465465
RAI_InitDagOp(&currentDagOp);
466-
array_append(rinfo->dagOps, currentDagOp);
466+
rinfo->dagOps = array_append(rinfo->dagOps, currentDagOp);
467467
}
468468
chainingOpCount++;
469469
} else {

src/model.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,29 @@ static void RAI_Model_AofRewrite(RedisModuleIO *aof, RedisModuleString *key, voi
170170
RedisModuleCtx *ctx = RedisModule_GetContextFromIO(aof);
171171

172172
for (size_t i=0; i<model->ninputs; i++) {
173-
array_append(inputs_, RedisModule_CreateString(ctx, model->inputs[i], strlen(model->inputs[i])));
173+
inputs_ = array_append(inputs_, RedisModule_CreateString(ctx, model->inputs[i], strlen(model->inputs[i])));
174174
}
175175

176176
for (size_t i=0; i<model->noutputs; i++) {
177-
array_append(outputs_, RedisModule_CreateString(ctx, model->outputs[i], strlen(model->outputs[i])));
177+
outputs_ = array_append(outputs_, RedisModule_CreateString(ctx, model->outputs[i], strlen(model->outputs[i])));
178178
}
179179

180+
<<<<<<< HEAD
181+
=======
182+
long long chunk_size = getModelChunkSize();
183+
const size_t n_chunks = len / chunk_size + 1;
184+
RedisModuleString **buffers_ = array_new(RedisModuleString*, n_chunks);
185+
186+
for (size_t i=0; i<n_chunks; i++) {
187+
size_t chunk_len = i < n_chunks - 1 ? chunk_size : len % chunk_size;
188+
buffers_ = array_append(buffers_, RedisModule_CreateString(ctx, buffer + i * chunk_size, chunk_len));
189+
}
190+
191+
if (buffer) {
192+
RedisModule_Free(buffer);
193+
}
194+
195+
>>>>>>> 4f51679... Safely add to arrays + fix for #443 (#449)
180196
const char* backendstr = RAI_BackendName(model->backend);
181197

182198
RedisModule_EmitAOF(aof, "AI.MODELSET", "slccclclcvcvb",

src/tensor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ int RAI_parseTensorSetArgs(RedisModuleCtx *ctx, RedisModuleString **argv, int ar
825825
return -1;
826826
}
827827
ndims++;
828-
array_append(dims, dimension);
828+
dims = array_append(dims, dimension);
829829
len *= dimension;
830830
}
831831
}

test/tests_sanitizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_sanitizer_dagrun_mobilenet_v1(env):
1212
if (not TEST_TF or not TEST_PT):
1313
return
1414
con = env.getConnection()
15-
mem_allocator = con.execute_command('info', 'memory')['mem_allocator']
15+
mem_allocator = con.info()['mem_allocator']
1616
if 'jemalloc' in mem_allocator:
1717
print("exiting sanitizer test given we're not using stdlib allocator")
1818
return
@@ -48,7 +48,7 @@ def test_sanitizer_modelrun_mobilenet_v1(env):
4848
if (not TEST_TF or not TEST_PT):
4949
return
5050
con = env.getConnection()
51-
mem_allocator = con.execute_command('info', 'memory')['mem_allocator']
51+
mem_allocator = con.info()['mem_allocator']
5252
if 'jemalloc' in mem_allocator:
5353
print("exiting sanitizer test given we're not using stdlib allocator")
5454
return

0 commit comments

Comments
 (0)