Need advise on GCC instrumentation, asan and memory reference hooking

I’m trying to create some sort of poor man’s mmap() on a platform that does not support generic mmap directly (we’re talking about esp32s3). I realize that this approach will have a significant impact on performance, but nevertheless I would like to give it a try in specific places in the project. The idea is to generate code around each memory load, check the pointer value and decide if the load should happen from other address.

Pseudocode illustrating the idea:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>char * mapped_file;
read(somefile, mapped_file, file_size);
char load_hook(void * ptr) {
if(ptr & 0xff000000) {
return mapped_file[ptr & 0x00ffffff];
} else {
return *ptr;
}
}
char s[] = "some data";
char * p = 0xff000002;
char load_1 = *s;
char load_2 = *p;
// load_1 will contain first byte in s[],
// load_2 will contain third byte in mapped_file
</code>
<code>char * mapped_file; read(somefile, mapped_file, file_size); char load_hook(void * ptr) { if(ptr & 0xff000000) { return mapped_file[ptr & 0x00ffffff]; } else { return *ptr; } } char s[] = "some data"; char * p = 0xff000002; char load_1 = *s; char load_2 = *p; // load_1 will contain first byte in s[], // load_2 will contain third byte in mapped_file </code>
char * mapped_file;
read(somefile, mapped_file, file_size);

char load_hook(void * ptr) {
  if(ptr & 0xff000000) {
    return mapped_file[ptr & 0x00ffffff];
  } else {
    return *ptr;
  }
}

char s[] = "some data";
char * p = 0xff000002;
char load_1 = *s;
char load_2 = *p;
// load_1 will contain first byte in s[],
// load_2 will contain third byte in mapped_file

Of course load_hook should be more sophisticated and include caching procedures to be able to address large files with smaller amount of physical RAM, but the example is sufficient to illustrate the idea.

Some sort of this behaviour can be achieved with -fsanitize=kernel-address, GCC generates code to call void __asan_load*_*(void * ptr) functions that implement various out-of-bounds, use-after-free and other checks. I have successfully hijacked these calls by implementing these functions by myself and not linking to asan library.

But I can not find a way to make GCC generate code to change the load address. kasan does something like this (on x86_64):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>char fn(char * p) {
char ret = *p;
return ret;
}
...
movq -24(%rbp), %rax |
movq %rax, %rdi | << injected by kasan
call __asan_load1_noabort@PLT |
movq -24(%rbp), %rax << get p pointer
movz bl(%rax), %eax << actually dereference p pointer
...
</code>
<code>char fn(char * p) { char ret = *p; return ret; } ... movq -24(%rbp), %rax | movq %rax, %rdi | << injected by kasan call __asan_load1_noabort@PLT | movq -24(%rbp), %rax << get p pointer movz bl(%rax), %eax << actually dereference p pointer ... </code>
char fn(char * p) {
    char ret = *p;
    return ret;
}

...
movq -24(%rbp), %rax             |
movq %rax, %rdi                  | << injected by kasan
call __asan_load1_noabort@PLT    |
movq -24(%rbp), %rax    << get p pointer
movz bl(%rax), %eax     << actually dereference p pointer
...

I could possibly do some dirty magic in __asan_load.. and search for pointer value in caller function frame on stack, but maybe there is a more straightforward way to achieve that?

The task is even more complicated due to lack of public documentation on ESP32 ISA, on x86 I could possibly add a custom build step and inject desired code straight into assembly text after its generation, but for ESP32 I would prefer not to mess with assembly.

I could possibly write a gcc plugin to preprocess the gimple, but the toolchain is provided by Espressif and I am not sure that all dev headers are there (at least at a first glance).

Maybe I’m moving in a completely wrong direction and you can suggest how to achieve the desired behaviour by other means?

New contributor

Roman Likane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật