Skip to content

Commit 158b38f

Browse files
committed
Add strlen rule + test
1 parent d8fb0f0 commit 158b38f

7 files changed

Lines changed: 69 additions & 0 deletions

File tree

rules/cstring/ir_unsafe.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,5 +388,30 @@
388388
"type": "*const u8",
389389
"is_unsafe_pointer": true
390390
}
391+
},
392+
"f7": {
393+
"body": [
394+
{
395+
"text": "libc::strlen("
396+
},
397+
{
398+
"placeholder": {
399+
"arg": 0,
400+
"access": "read"
401+
}
402+
},
403+
{
404+
"text": " as *const i8) as u64"
405+
}
406+
],
407+
"params": {
408+
"a0": {
409+
"type": "*const u8",
410+
"is_unsafe_pointer": true
411+
}
412+
},
413+
"return_type": {
414+
"type": "u64"
415+
}
391416
}
392417
}

rules/cstring/src.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ int f3(const void *s1, const void *s2, size_t n) { return memcmp(s1, s2, n); }
1212
void *f4(void *dst, const void *src, size_t n) { return memmove(dst, src, n); }
1313

1414
char *f5(const char *a0, int a1) { return strchr(a0, a1); }
15+
16+
size_t f7(const char *a0) { return strlen(a0); }

rules/cstring/tgt_unsafe.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ unsafe fn f5(a0: *const u8, a1: i32) -> *mut u8 {
4343
unsafe fn f6(a0: *const u8, a1: i32) -> *const u8 {
4444
libc::strchr(a0 as *const i8, a1) as *const u8
4545
}
46+
47+
unsafe fn f7(a0: *const u8) -> u64 {
48+
libc::strlen(a0 as *const i8) as u64
49+
}

tests/unit/cstring.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,18 @@ static void test_strchr() {
4343
assert(std::strchr(s, 'z') == nullptr);
4444
}
4545

46+
static void test_strlen() {
47+
assert(std::strlen("") == 0);
48+
assert(std::strlen("hello") == 5);
49+
assert(std::strlen("hello world") == 11);
50+
}
51+
4652
int main() {
4753
test_memcpy();
4854
test_memset();
4955
test_memcmp();
5056
test_memmove();
5157
test_strchr();
58+
test_strlen();
5259
return 0;
5360
}

tests/unit/out/unsafe/cstring.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ pub unsafe fn test_strchr_4() {
151151
assert!((((*r) as i32) == (('w' as u8) as i32)));
152152
assert!((libc::strchr(s as *const i8, (('z' as u8) as i32)) as *const u8).is_null());
153153
}
154+
pub unsafe fn test_strlen_5() {
155+
assert!(((libc::strlen(b"\0".as_ptr() as *const i8) as u64) == (0_u64)));
156+
assert!(((libc::strlen(b"hello\0".as_ptr() as *const i8) as u64) == (5_u64)));
157+
assert!(((libc::strlen(b"hello world\0".as_ptr() as *const i8) as u64) == (11_u64)));
158+
}
154159
pub fn main() {
155160
unsafe {
156161
std::process::exit(main_0() as i32);
@@ -162,5 +167,6 @@ unsafe fn main_0() -> i32 {
162167
(unsafe { test_memcmp_2() });
163168
(unsafe { test_memmove_3() });
164169
(unsafe { test_strchr_4() });
170+
(unsafe { test_strlen_5() });
165171
return 0;
166172
}

tests/unit/out/unsafe/string_h.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ pub unsafe fn test_strchr_4() {
175175
!= 0)
176176
);
177177
}
178+
pub unsafe fn test_strlen_5() {
179+
assert!(
180+
((((libc::strlen((b"\0".as_ptr().cast_mut()).cast_const() as *const i8) as u64) == (0_u64))
181+
as i32)
182+
!= 0)
183+
);
184+
assert!(
185+
((((libc::strlen((b"hello\0".as_ptr().cast_mut()).cast_const() as *const i8) as u64)
186+
== (5_u64)) as i32)
187+
!= 0)
188+
);
189+
assert!(
190+
((((libc::strlen((b"hello world\0".as_ptr().cast_mut()).cast_const() as *const i8) as u64)
191+
== (11_u64)) as i32)
192+
!= 0)
193+
);
194+
}
178195
pub fn main() {
179196
unsafe {
180197
std::process::exit(main_0() as i32);
@@ -186,5 +203,6 @@ unsafe fn main_0() -> i32 {
186203
(unsafe { test_memcmp_2() });
187204
(unsafe { test_memmove_3() });
188205
(unsafe { test_strchr_4() });
206+
(unsafe { test_strlen_5() });
189207
return 0;
190208
}

tests/unit/string_h.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,18 @@ static void test_strchr(void) {
4343
assert(strchr((char *)s, 'z') == NULL);
4444
}
4545

46+
static void test_strlen(void) {
47+
assert(strlen("") == 0);
48+
assert(strlen("hello") == 5);
49+
assert(strlen("hello world") == 11);
50+
}
51+
4652
int main(void) {
4753
test_memcpy();
4854
test_memset();
4955
test_memcmp();
5056
test_memmove();
5157
test_strchr();
58+
test_strlen();
5259
return 0;
5360
}

0 commit comments

Comments
 (0)