android libc 有哪些函数_Android scudo功能介绍

一 简述

前面介绍了malloc_debug功能,用来进行内存泄露等检测,其实android可以使用多种方法进行内存相关的监控。比如利用llvm功能再编译时添加内存的相关检测。Android R默认开启了scudo。

scudo这个选项主要功能是再分配内存时会添加64 bit的Header

// Our header requires 64 bits of storage. Having the offset saves us from

// using functions such as GetBlockBegin, that is fairly costly. Our first

// implementation used the MetaData as well, which offers the advantage of

// being stored away from the chunk itself, but accessing it was costly as

// well. The header will be atomically loaded and stored.

typedef u64 PackedHeader;

struct UnpackedHeader {

u64 Checksum : 16;

u64 ClassId : 8;

u64 SizeOrUnusedBytes : 20; // Size for Primary backed allocations, amount of

// unused bytes in the chunk for Secondary ones.

u64 State : 2; // available, allocated, or quarantined

u64 AllocType : 2; // malloc, new, new[], or memalign

u64 Offset : 16; // Offset from the beginning of the backend

// allocation to the beginning of the chunk

// itself, in multiples of MinAlignment. See

// comment about its maximum value and in init().

};

开启scudo后支持的报错类型有如下,主要是用来进行边界检查,检测内存越界

void NORETURN reportCallocOverflow(uptr Count, uptr Size);

void NORETURN reportPvallocOverflow(uptr Size);

void NORETURN reportAllocationAlignmentTooBig(uptr Alignment,

uptr MaxAlignment);

void NORETURN reportAllocationAlignmentNotPowerOfTwo(uptr Alignment);

void NORETURN reportInvalidPosixMemalignAlignment(uptr Alignment);

void NORETURN reportInvalidAlignedAllocAlignment(uptr Size, uptr Alignment);

void NORETURN reportAllocationSizeTooBig(uptr UserSize, uptr TotalSize,

uptr MaxSize);

void NORETURN reportRssLimitExceeded();

void NORETURN reportOutOfMemory(uptr RequestedSize);

二 android R开启scudo功能

Android R使用的不是llvm的scudo功能,因此不是通过soong的scudo的开启来开启,主要使用如下选项

MALLOC_SVELTE := true

go版本默认是关闭scudo功能的。

非go版本四都开启scudo功能的,不过不是通过llvm开启,开启的方法

libc_scudo_product_variables

在 libc_scudo_product_variables 中定义了 -DUSE_SCUDO,具体的内存分配会使用libscudo下的定义,libscudo代码在externl/scudo下,而不是llvm中。

通过搜索USE_SCUDO宏,在宏开启后

libc/bionic/malloc_common.h

#if defined(USE_SCUDO)

#include "scudo.h"

#define Malloc(function)scudo_ ## function

通过hook的方式,将Libscudo中的实现来替换Libc原有的内存分配释放实现。

三 llvm中的scudo实现方式

llvm的scudo主要是在代码路径compiler-rt\lib\scudo中,scudo只能与undefined一块使用

The only other Sanitizer Scudo is compatible with is UBSan (eg: -fsanitize=scudo,undefined)

llvm下主要的生效方式是:

// You'll need to:

// 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in

// your source file. See the notes below for cases when

// INTERCEPTOR_WITH_SUFFIX(...) should be used instead.

// 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".

// INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was

// intercepted successfully.

在库初始化中,INTERCEPT_FUNCTION调用替代原生的函数,从而实现类似hook的方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值