- The problem *****
See motivation examples in compiler-rt/test/dfsan/pair.cpp. The current
DFSan always uses a 16bit shadow value for a variable with any type by
combining all shadow values of all bytes of the variable. So it cannot
distinguish two fields of a struct: each field's shadow value equals the
combined shadow value of all fields. This introduces an overtaint issue.
Consider a parsing function
std::pair<char*, int> get_token(char* p);
where p points to a buffer to parse, the returned pair includes the next
token and the pointer to the position in the buffer after the token.
If the token is tainted, then both the returned pointer and int ar
tainted. If the parser keeps on using get_token for the rest parsing,
all the following outputs are tainted because of the tainted pointer.
The CL is the first change to address the issue.
- The proposed improvement ******
Eventually all fields and indices have their own shadow values in
variables and memory.
For example, variables with type {i1, i3}, [2 x i1], {[2 x i4], i8},
[2 x {i1, i1}] have shadow values with type {i16, i16}, [2 x i16],
{[2 x i16], i16}, [2 x {i16, i16}] correspondingly; variables with
primary type still have shadow values i16.
- An potential implementation plan *******
The idea is to adopt the change incrementially.
- This CL
Support field-level accuracy at variables/args/ret in TLS/Fast16 mode, load/store/alloca still use combined shadow values.
After the alloca promotion and SSA construction phases (>=-O1), we assume alloca and memory operations are reduced. So if struct variables do not relate to memory, their tracking is accurate at field level.
- Support field-level accuracy at alloca
- Support field-level accuracy at load/store
These two should make O0 and real memory access work.
- Support vector if necessary.
- Support Args mode if necessary.
- Support passing more accurate shadow values via custom functions if necessary.
- Support legacy non-fast16 mode if necessary.
***- About this CL. ***
The CL did the following
- extended TLS arg/ret to work with aggregate types. This is similar to what MSan does.
- implemented how to map between an original type/value/zero-const to its shadow type/value/zero-const.
- extended (insert|extract)value to use field/index-level progagation.
- for other instructions, propagation rules are combining inputs by or. The CL converts between aggragate and primary shadow values at the cases.
- Custom function interfaces also need such a conversion because all existing custom functions use i16. It is unclear whether custom functions need more accurate shadow propagation yet.
- Added test cases for aggregate type related cases.
Nit: Moving this helper function closer to where it's used would improve readability.