@@ -12,11 +12,45 @@ static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
1212	vfprintf (ctx , fmt , args );
1313}
1414
15+ /* Write raw BTF to file, return number of bytes written or negative errno */ 
16+ static  ssize_t  btf_raw_write (struct  btf  * btf , char  * file )
17+ {
18+ 	ssize_t  written  =  0 ;
19+ 	const  void  * data ;
20+ 	__u32  size  =  0 ;
21+ 	int  fd , ret ;
22+ 
23+ 	fd  =  mkstemp (file );
24+ 	if  (!ASSERT_GE (fd , 0 , "create_file" ))
25+ 		return  - errno ;
26+ 
27+ 	data  =  btf__raw_data (btf , & size );
28+ 	if  (!ASSERT_OK_PTR (data , "btf__raw_data" )) {
29+ 		close (fd );
30+ 		return  - EINVAL ;
31+ 	}
32+ 	while  (written  <  size ) {
33+ 		ret  =  write (fd , data  +  written , size  -  written );
34+ 		if  (!ASSERT_GE (ret , 0 , "write succeeded" )) {
35+ 			close (fd );
36+ 			return  - errno ;
37+ 		}
38+ 		written  +=  ret ;
39+ 	}
40+ 	close (fd );
41+ 	return  written ;
42+ }
43+ 
1544static  void  __test_btf_split (bool  multi )
1645{
46+ 	char  multisplit_btf_file [] =  "/tmp/test_btf_multisplit.XXXXXX" ;
47+ 	char  split_btf_file [] =  "/tmp/test_btf_split.XXXXXX" ;
48+ 	char  base_btf_file [] =  "/tmp/test_btf_base.XXXXXX" ;
49+ 	ssize_t  multisplit_btf_sz  =  0 , split_btf_sz  =  0 , base_btf_sz  =  0 ;
1750	struct  btf_dump  * d  =  NULL ;
18- 	const  struct  btf_type  * t ;
19- 	struct  btf  * btf1 , * btf2 , * btf3  =  NULL ;
51+ 	const  struct  btf_type  * t , * ot ;
52+ 	struct  btf  * btf1  =  NULL , * btf2  =  NULL , * btf3  =  NULL ;
53+ 	struct  btf  * btf4  =  NULL , * btf5  =  NULL , * btf6  =  NULL ;
2054	int  str_off , i , err ;
2155
2256	btf1  =  btf__new_empty ();
@@ -123,6 +157,38 @@ static void __test_btf_split(bool multi)
123157"	int uf2;\n" 
124158"};\n\n" , "c_dump" );
125159
160+ 	/* write base, split BTFs to files and ensure parsing succeeds */ 
161+ 	base_btf_sz  =  btf_raw_write (btf1 , base_btf_file );
162+ 	if  (base_btf_sz  <  0 )
163+ 		goto cleanup ;
164+ 	split_btf_sz  =  btf_raw_write (btf2 , split_btf_file );
165+ 	if  (split_btf_sz  <  0 )
166+ 		goto cleanup ;
167+ 	btf4  =  btf__parse (base_btf_file , NULL );
168+ 	if  (!ASSERT_OK_PTR (btf4 , "parse_base" ))
169+ 		goto cleanup ;
170+ 	btf5  =  btf__parse_split (split_btf_file , btf4 );
171+ 	if  (!ASSERT_OK_PTR (btf5 , "parse_split" ))
172+ 		goto cleanup ;
173+ 	if  (multi ) {
174+ 		multisplit_btf_sz  =  btf_raw_write (btf3 , multisplit_btf_file );
175+ 		if  (multisplit_btf_sz  <  0 )
176+ 			goto cleanup ;
177+ 		btf6  =  btf__parse_split (multisplit_btf_file , btf5 );
178+ 		if  (!ASSERT_OK_PTR (btf6 , "parse_multisplit" ))
179+ 			goto cleanup ;
180+ 	} else  {
181+ 		btf6  =  btf5 ;
182+ 	}
183+ 
184+ 	/* compare parsed to original BTF */ 
185+ 	for  (i  =  1 ; i  <  btf__type_cnt (btf6 ); i ++ ) {
186+ 		t  =  btf__type_by_id (btf6 , i );
187+ 		ot  =  btf__type_by_id (btf3 , i );
188+ 		if  (!ASSERT_EQ (memcmp (t , ot , sizeof (* ot )), 0 , "cmp_parsed_orig_btf" ))
189+ 			goto cleanup ;
190+ 	}
191+ 
126192cleanup :
127193	if  (dump_buf_file )
128194		fclose (dump_buf_file );
@@ -132,6 +198,16 @@ static void __test_btf_split(bool multi)
132198	btf__free (btf2 );
133199	if  (btf2  !=  btf3 )
134200		btf__free (btf3 );
201+ 	btf__free (btf4 );
202+ 	btf__free (btf5 );
203+ 	if  (btf5  !=  btf6 )
204+ 		btf__free (btf6 );
205+ 	if  (base_btf_sz  >  0 )
206+ 		unlink (base_btf_file );
207+ 	if  (split_btf_sz  >  0 )
208+ 		unlink (split_btf_file );
209+ 	if  (multisplit_btf_sz  >  0 )
210+ 		unlink (multisplit_btf_file );
135211}
136212
137213void  test_btf_split (void )
0 commit comments