Skip to content

Commit

Permalink
Only frees instruction if allocated (keystone-engine#375)
Browse files Browse the repository at this point in the history
* Only frees instruction if allocated

* Avoids memory leak in ks_close for EVM
  • Loading branch information
catenacyber authored and aquynh committed Sep 13, 2018
1 parent 1ee0248 commit 0f19fa9
Show file tree
Hide file tree
Showing 28 changed files with 109 additions and 54 deletions.
6 changes: 4 additions & 2 deletions kstool/kstool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ int main(int argc, char **argv)
uint64_t start_addr = 0;
char *input = NULL;
size_t count;
unsigned char *insn;
unsigned char *insn = NULL;
size_t size;

if (argc == 2) {
Expand Down Expand Up @@ -308,7 +308,9 @@ int main(int argc, char **argv)
}

// NOTE: free insn after usage to avoid leaking memory
ks_free(insn);
if (insn != NULL) {
ks_free(insn);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
1 change: 1 addition & 0 deletions llvm/keystone/ks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ ks_err ks_close(ks_engine *ks)

if (ks->arch == KS_ARCH_EVM) {
// handle EVM differently
delete ks;
return KS_ERR_OK;
}

Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_arm64_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_arm_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_arm_armbe.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_arm_armv8be.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_arm_thumb.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_arm_thumbbe.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_arm_thumbv8.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_arm_thumbv8be.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_armv8_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_evm.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_mips.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_mips64.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_mips64be.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_mipsbe.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_ppc32be.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_ppc64.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
6 changes: 4 additions & 2 deletions suite/fuzz/fuzz_asm_ppc64be.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
unsigned char *encode = NULL;
size_t size;
char * assembler;

Expand Down Expand Up @@ -55,7 +55,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

free(assembler);
// NOTE: free encode after usage to avoid leaking memory
ks_free(encode);
if (encode != NULL) {
ks_free(encode);
}

// close Keystone instance when done
ks_close(ks);
Expand Down
Loading

0 comments on commit 0f19fa9

Please sign in to comment.