@@ -12,11 +12,43 @@ static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
1212 vfprintf (ctx , fmt , args );
1313}
1414
15+ static int btf_raw_write (struct btf * btf , char * file )
16+ {
17+ ssize_t written = 0 ;
18+ const void * data ;
19+ __u32 size = 0 ;
20+ int fd , ret ;
21+
22+ fd = mkstemp (file );
23+ if (!ASSERT_GE (fd , 0 , "create_file" ))
24+ return - errno ;
25+
26+ data = btf__raw_data (btf , & size );
27+ if (!ASSERT_OK_PTR (data , "btf__raw_data" )) {
28+ close (fd );
29+ return - EINVAL ;
30+ }
31+ while (written < size ) {
32+ ret = write (fd , data + written , size - written );
33+ if (!ASSERT_GE (ret , 0 , "write succeeded" )) {
34+ close (fd );
35+ return - errno ;
36+ }
37+ written += ret ;
38+ }
39+ close (fd );
40+ return 0 ;
41+ }
42+
1543static void __test_btf_split (bool multi )
1644{
45+ char multisplit_btf_file [] = "/tmp/test_btf_multisplit.XXXXXX" ;
46+ char split_btf_file [] = "/tmp/test_btf_split.XXXXXX" ;
47+ char base_btf_file [] = "/tmp/test_btf_base.XXXXXX" ;
1748 struct btf_dump * d = NULL ;
18- const struct btf_type * t ;
49+ const struct btf_type * t , * ot ;
1950 struct btf * btf1 , * btf2 , * btf3 = NULL ;
51+ struct btf * btf4 , * btf5 , * btf6 = NULL ;
2052 int str_off , i , err ;
2153
2254 btf1 = btf__new_empty ();
@@ -123,6 +155,35 @@ static void __test_btf_split(bool multi)
123155" int uf2;\n"
124156"};\n\n" , "c_dump" );
125157
158+ /* write base, split BTFs to files and ensure parsing succeeds */
159+ if (btf_raw_write (btf1 , base_btf_file ) != 0 )
160+ goto cleanup ;
161+ if (btf_raw_write (btf2 , split_btf_file ) != 0 )
162+ goto cleanup ;
163+ btf4 = btf__parse (base_btf_file , NULL );
164+ if (!ASSERT_OK_PTR (btf4 , "parse_base" ))
165+ goto cleanup ;
166+ btf5 = btf__parse_split (split_btf_file , btf4 );
167+ if (!ASSERT_OK_PTR (btf5 , "parse_split" ))
168+ goto cleanup ;
169+ if (multi ) {
170+ if (btf_raw_write (btf3 , multisplit_btf_file ) != 0 )
171+ goto cleanup ;
172+ btf6 = btf__parse_split (multisplit_btf_file , btf5 );
173+ if (!ASSERT_OK_PTR (btf5 , "parse_multisplit" ))
174+ goto cleanup ;
175+ } else {
176+ btf6 = btf5 ;
177+ }
178+
179+ /* compare parsed to original BTF */
180+ for (i = 1 ; i < btf__type_cnt (btf6 ); i ++ ) {
181+ t = btf__type_by_id (btf6 , i );
182+ ot = btf__type_by_id (btf3 , i );
183+ if (!ASSERT_EQ (memcmp (t , ot , sizeof (* ot )), 0 , "cmp_parsed_orig_btf" ))
184+ goto cleanup ;
185+ }
186+
126187cleanup :
127188 if (dump_buf_file )
128189 fclose (dump_buf_file );
@@ -132,6 +193,14 @@ static void __test_btf_split(bool multi)
132193 btf__free (btf2 );
133194 if (btf2 != btf3 )
134195 btf__free (btf3 );
196+ btf__free (btf4 );
197+ btf__free (btf5 );
198+ if (btf5 != btf6 )
199+ btf__free (btf6 );
200+ unlink (base_btf_file );
201+ unlink (split_btf_file );
202+ if (multi )
203+ unlink (multisplit_btf_file );
135204}
136205
137206void test_btf_split (void )
0 commit comments