Implement uc_context_free (#1336)

* Implement uc_context_free

* Use uc_context_free for python bindings

* Format code

* Simplify code

* Move next,context inside while loop

* Add my name to CREDITS.TXT
This commit is contained in:
lazymio
2020-09-24 22:28:55 +08:00
committed by GitHub
parent 4441394258
commit 1044403d38
5 changed files with 54 additions and 8 deletions

32
uc.c
View File

@ -348,6 +348,16 @@ uc_err uc_close(uc_engine *uc)
free(uc->mapped_blocks);
// free the saved contexts list and notify them that uc has been closed.
cur = uc->saved_contexts.head;
while (cur != NULL) {
struct list_item *next = cur->next;
struct uc_context *context = (struct uc_context*)cur->data;
context->uc = NULL;
cur = next;
}
list_clear(&uc->saved_contexts);
// finally, free uc itself.
memset(uc, 0, sizeof(*uc));
free(uc);
@ -1327,7 +1337,12 @@ uc_err uc_context_alloc(uc_engine *uc, uc_context **context)
if (*_context) {
(*_context)->jmp_env_size = sizeof(*uc->cpu->jmp_env);
(*_context)->context_size = cpu_context_size(uc->arch, uc->mode);
return UC_ERR_OK;
(*_context)->uc = uc;
if (list_insert(&uc->saved_contexts, *_context)) {
return UC_ERR_OK;
} else {
return UC_ERR_NOMEM;
}
} else {
return UC_ERR_NOMEM;
}
@ -1360,7 +1375,20 @@ UNICORN_EXPORT
uc_err uc_context_restore(uc_engine *uc, uc_context *context)
{
memcpy(uc->cpu->env_ptr, context->data, context->context_size);
memcpy(uc->cpu->jmp_env, context->data + context->context_size, context->jmp_env_size);
if (list_exists(&uc->saved_contexts, context)) {
memcpy(uc->cpu->jmp_env, context->data + context->context_size, context->jmp_env_size);
}
return UC_ERR_OK;
}
UNICORN_EXPORT
uc_err uc_context_free(uc_context *context)
{
uc_engine* uc = context->uc;
// if uc is NULL, it means that uc_engine has been free-ed.
if (uc) {
list_remove(&uc->saved_contexts, context);
}
return uc_free(context);
}