FRET-LibAFL/libafl_frida/test_harness.cpp
mkravchik b3fe744e57
Pr/fasan multithreading fixes upstream (#2955)
* Fixing the test_harness library name

* Fasan works, but testing of all features is pending

* Tests pass, before fixing clippy and fmt

* CLippy+fmt

* CLippy+fmt+tests running on linux

* Clippy

* Not stalkering the fuzzer. In the correct way

* Removing the instrumentation upon crash. Proper hooking of UnmapViewOfFile

* Fixes after the merge from the upstream (before 0.15.0). Still need to add the observer, clippy, fmt, and at least linux compilation

* Adding the helper observer and using it in the test

* Removing the observer from the wrong location

* Adapting to the new helper ownership model

* Adding an observer to shut down instrumentation upon crash

* Clippy + fmt

* Using mimalloc everywhere

* Deactivating before activating with the harness. Otherwise, gets stuck on Linux.

* Fixing imports for windows

* Using the new way of passing the handler

* Using frida_helper_shutdown_observer

* Clippy+fmt

* no-std, clippy

* Fmt

* Stable thread_id

* Clippy 18

* More clippy

* Formatting toml

* Fixing apples

* Fixing apples 2

* Fixing apples 3

* Upping to 0.16.7 (necessary for Windows)

* Clippy+fmt

* Enabling the allocator test after the fix and clarifying the importantce of the static runtime linking.

* Moving has_tls to bolts

* Proper handling of no-std, hopefully

* Another attempt to fix win no-std

* Not mine clippy complaint...

* Not mine clippy complaint #2...

* Dlmalloc not used, removing from dependencies

* Restoring target in config.toml (otherwise fails CI on Linux)

* lots of digging around, pray for us

* fixup?

* Revert "lots of digging around, pray for us"

This reverts commit 706c27201918e906e3401cd0d9e76546f889d1f5.

* Revert "fixup?"

This reverts commit 1d7c5d4fb5b1bd31f5e0c07492aa8ed64c6822f3.

* Revert artifact

* Revert fixups

* Removing unused

* Reverting to upstream/main

---------

Co-authored-by: Addison Crump <addison.crump@cispa.de>
Co-authored-by: Dongjia "toka" Zhang <tokazerkje@outlook.com>
2025-02-14 13:45:38 +01:00

230 lines
5.1 KiB
C++

#include <stdint.h>
#include <stdlib.h>
#include <string>
#ifndef _MSC_VER
#include <string.h>
#endif
#ifdef _MSC_VER
#include <windows.h>
#include <winnt.h>
#include <winternl.h>
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved) {
(void)hModule;
(void)lpReserved;
(void)ul_reason_for_call;
return TRUE;
}
#define EXTERN extern "C" __declspec(dllexport)
#else
#define EXTERN
extern "C" {
#endif
EXTERN int heap_uaf_read(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
int *array = new int[100];
delete[] array;
fprintf(stdout, "%d\n", array[5]);
return 0;
}
EXTERN int heap_uaf_write(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
int *array = new int[100];
delete[] array;
array[5] = 1;
return 0;
}
static volatile bool stop = false;
EXTERN int heap_oob_read(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
// while(!stop);
// OutputDebugStringA("heap_oob_read\n");
int *array = new int[100];
fprintf(stdout, "heap_oob_read %d\n", array[100]);
delete[] array;
return 0;
}
EXTERN int heap_oob_write(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
int *array = new int[100];
array[100] = 1;
delete[] array;
return 0;
}
EXTERN int malloc_heap_uaf_read(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
int *array = static_cast<int *>(malloc(100 * sizeof(int)));
free(array);
fprintf(stdout, "malloc_heap_uaf_read %d\n", array[5]);
return 0;
}
EXTERN int malloc_heap_uaf_write(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
int *array = static_cast<int *>(malloc(100 * sizeof(int)));
free(array);
array[5] = 1;
return 0;
}
EXTERN int malloc_heap_oob_read(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
int *array = static_cast<int *>(malloc(100 * sizeof(int)));
fprintf(stdout, "malloc_heap_oob_read %d\n", array[100]);
free(array);
return 0;
}
EXTERN int malloc_heap_oob_write(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
int *array = static_cast<int *>(malloc(100 * sizeof(int)));
array[100] = 1;
free(array);
return 0;
}
EXTERN int malloc_heap_oob_write_0x12(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
char *array = static_cast<char *>(malloc(0x12));
array[0x12] = 1;
free(array);
return 0;
}
EXTERN int malloc_heap_oob_write_0x14(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
char *array = static_cast<char *>(malloc(0x14));
array[0x14] = 1;
free(array);
return 0;
}
EXTERN int malloc_heap_oob_write_0x17(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
char *array = static_cast<char *>(malloc(0x17));
array[0x17] = 1;
free(array);
return 0;
}
EXTERN int malloc_heap_oob_write_0x17_int_at_0x16(const uint8_t *_data,
size_t _size) {
(void)_data;
(void)_size;
char *array = static_cast<char *>(malloc(0x17));
*(int *)(&array[0x16]) = 1;
free(array);
return 0;
}
EXTERN int malloc_heap_oob_write_0x17_int_at_0x15(const uint8_t *_data,
size_t _size) {
(void)_data;
(void)_size;
char *array = static_cast<char *>(malloc(0x17));
*(int *)(&array[0x15]) = 1;
free(array);
return 0;
}
EXTERN int malloc_heap_oob_write_0x17_int_at_0x14(const uint8_t *_data,
size_t _size) {
(void)_data;
(void)_size;
char *array = static_cast<char *>(malloc(0x17));
*(int *)(&array[0x14]) = 1;
free(array);
return 0;
}
EXTERN int malloc_heap_oob_write_0x17_int_at_0x13(const uint8_t *_data,
size_t _size) {
(void)_data;
(void)_size;
char *array = static_cast<char *>(malloc(0x17));
*(int *)(&array[0x13]) = 1;
free(array);
return 0;
}
EXTERN int heap_oob_memcpy_write(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
const size_t REAL_SIZE = 10;
const size_t LARGER_SIZE = REAL_SIZE + 1;
char *dest = new char[REAL_SIZE];
char *src = new char[LARGER_SIZE];
memcpy(dest, src, LARGER_SIZE);
delete[] dest;
delete[] src;
return 0;
}
EXTERN int heap_oob_memcpy_read(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
const size_t REAL_SIZE = 10;
const size_t LARGER_SIZE = REAL_SIZE + 1;
char *dest = new char[LARGER_SIZE];
char *src = new char[REAL_SIZE];
memcpy(dest, src, LARGER_SIZE);
delete[] dest;
delete[] src;
return 0;
}
EXTERN int heap_oob_memcpy_write_avx(const uint8_t *_data, size_t _size) {
(void)_data;
(void)_size;
// Using 127 bytes to make sure to fall on the AVX instruction in the
// optimized implementation
const size_t REAL_SIZE = 127;
const size_t LARGER_SIZE = REAL_SIZE + 1;
char *dest = new char[LARGER_SIZE];
char *src = new char[REAL_SIZE];
memcpy(dest, src, LARGER_SIZE);
delete[] dest;
delete[] src;
return 0;
}
EXTERN int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// abort();
(void)data;
(void)size;
return 0;
}
#ifndef _MSC_VER
}
#endif