Index: compiler-rt/lib/dfsan/dfsan_custom.cpp =================================================================== --- compiler-rt/lib/dfsan/dfsan_custom.cpp +++ compiler-rt/lib/dfsan/dfsan_custom.cpp @@ -204,6 +204,55 @@ return const_cast(ret); } +SANITIZER_INTERFACE_ATTRIBUTE char *__dfsw_strsep(char **s, const char *delim, + dfsan_label s_label, + dfsan_label delim_label, + dfsan_label *ret_label) { + char *base = *s; + char *res = strsep(s, delim); + if (res != *s) { + char *token_start = res; + int token_length = strlen(res); + // the delimiter byte has been set to NULL + dfsan_set_label(0, token_start + token_length, 1); + } + + if (flags().strict_data_dependencies) { + *ret_label = res ? dfsan_read_label(base, sizeof(base)) : 0; + } else { + *ret_label = + dfsan_union(dfsan_read_label(base, sizeof(base)), + dfsan_union(dfsan_read_label(delim, strlen(delim) + 1), + dfsan_union(s_label, delim_label))); + } + + return res; +} + +SANITIZER_INTERFACE_ATTRIBUTE char *__dfso_strsep( + char **s, const char *delim, dfsan_label s_label, dfsan_label delim_label, + dfsan_label *ret_label, dfsan_origin s_origin, dfsan_origin delim_origin, + dfsan_origin *ret_origin) { + char *base = *s; + char *res = __dfsw_strsep(s, delim, s_label, delim_label, ret_label); + if (flags().strict_data_dependencies) { + if (res) + *ret_origin = dfsan_read_origin_of_first_taint(base, strlen(base)); + } else { + if (*ret_label) { + dfsan_origin o = dfsan_read_origin_of_first_taint(base, strlen(base)); + if (o) { + *ret_origin = o; + } else { + o = dfsan_read_origin_of_first_taint(delim, strlen(delim) + 1); + *ret_origin = o ? o : (s_label ? s_origin : delim_origin); + } + } + } + + return res; +} + static int dfsan_memcmp_bcmp(const void *s1, const void *s2, size_t n, size_t *bytes_read) { const char *cs1 = (const char *) s1, *cs2 = (const char *) s2; Index: compiler-rt/lib/dfsan/done_abilist.txt =================================================================== --- compiler-rt/lib/dfsan/done_abilist.txt +++ compiler-rt/lib/dfsan/done_abilist.txt @@ -283,6 +283,7 @@ fun:strpbrk=custom fun:strrchr=custom fun:strstr=custom +fun:strsep=custom # Functions which take action based on global state, such as running a callback # set by a separate function. Index: compiler-rt/test/dfsan/custom.cpp =================================================================== --- compiler-rt/test/dfsan/custom.cpp +++ compiler-rt/test/dfsan/custom.cpp @@ -1627,6 +1627,50 @@ #endif } +void test_strsep() { + char *s = strdup("Hello world/"); + char *delim = strdup(" /"); + + char *p_s = s; + char *base = s; + char *p_delim = delim; + + // taint delim bytes + dfsan_set_label(i_label, p_delim, strlen(p_delim)); + // taint delim pointer + dfsan_set_label(j_label, &p_delim, sizeof(&p_delim)); + // taint the string data bytes + dfsan_set_label(k_label, s, 5); + // taint the string pointer + dfsan_set_label(m_label, &p_s, sizeof(&p_s)); + + char *rv = strsep(&p_s, p_delim); + assert(rv == &base[0]); +#ifdef STRICT_DATA_DEPENDENCIES + ASSERT_LABEL(rv, k_label); + ASSERT_READ_LABEL(rv, strlen(rv), k_label); +#else + ASSERT_LABEL(rv, dfsan_union(dfsan_union(i_label, j_label), k_label)); + ASSERT_INIT_ORIGIN_EQ_ORIGIN(&rv, *s); +#endif + + // taint the remaining string's pointer + char **pp_s = &p_s; + char **pp_s_base = pp_s; + dfsan_set_label(n_label, &pp_s, sizeof(&pp_s)); + + rv = strsep(pp_s, p_delim); + + assert(rv == &base[6]); +#ifdef STRICT_DATA_DEPENDENCIES + ASSERT_ZERO_LABEL(rv); + ASSERT_ZERO_ORIGIN(rv); +#else + ASSERT_LABEL(rv, dfsan_union(i_label, dfsan_union(j_label, n_label))); + ASSERT_INIT_ORIGIN_EQ_ORIGIN(&rv, *p_delim); +#endif +} + void test_memchr() { char str1[] = "str1"; dfsan_set_label(i_label, &str1[3], 1); @@ -2041,6 +2085,7 @@ test_strncmp(); test_strncpy(); test_strpbrk(); + test_strsep(); test_strrchr(); test_strstr(); test_strtod();