From 4996b6fa2216122cb8310b12f931b4a4114eb1f5 Mon Sep 17 00:00:00 2001 From: WJ Date: Wed, 18 Oct 2023 11:08:09 +0800 Subject: [PATCH 1/6] add 1801GN.c --- Network/README | 4 ++- Network/gjb_S0101801GN.c | 73 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 Network/gjb_S0101801GN.c diff --git a/Network/README b/Network/README index 35dd104..76bc247 100644 --- a/Network/README +++ b/Network/README @@ -1 +1,3 @@ -###网络管理 \ No newline at end of file +###网络管理 + +需要切换到root执行:sudo su \ No newline at end of file diff --git a/Network/gjb_S0101801GN.c b/Network/gjb_S0101801GN.c new file mode 100644 index 0000000..1ecbbfa --- /dev/null +++ b/Network/gjb_S0101801GN.c @@ -0,0 +1,73 @@ +/********************************************************************************************************* +** +** GJB ��׼���Լ� +** +** Copyright All Rights Reserved +** +**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +** +** �� �� ��: gjb_S0101801GN.c +** +** �ļ���������: 2021 �� 1 �� 12 �� +** +** �� ��: �����豸�������ܲ��ԡ����Ʋ����������룬ʵ�ֶ����� +** �豸�����ú͹���������������IP��ַ���������롢���ء� +** +** ͬʱ����ʹ�������޸�IP��ַ, ����, ����: +** ifconfig eth0 192.168.1.110 netmask 255.255.255.0 gateway 192.168.1.1 +*********************************************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//int argc, char *argv[] +int main() +{ + int fd = 0; + struct ifreq ifr = {0}; + struct ifconf ifc = {0}; + int ret = 0; + int i; + uint8_t ip[4] = {192,168,8,1}; + + memset(&ifr, 0, sizeof(ifr)); + // ifr.ifr_addr.sa_family = AF_INET; + strncpy(ifr.ifr_name, "ens33", IFNAMSIZ); /* �������� */ + + /* ����socket������ */ + fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); + if (fd <= 0) + { + printf("create socket fd failed,%s\n", strerror(errno)); + return -1; + } + + //����IP���� + ifr.ifr_addr.sa_family = AF_INET; + struct sockaddr_in *addr = (struct sockaddr_in *)&(ifr.ifr_addr); + memcpy(&(addr->sin_addr.s_addr), ip, 4); + + ret = ioctl(fd, SIOCSIFADDR, &ifr); + if (ret < 0) + { + printf("ioctl failed,%s\n", strerror(errno)); + goto __exit; + } + +__exit: + if (fd != 0) + { + close(fd); + } + return ret; +} + -- Gitee From a9305a53abb7eb27c5b2a78cbeae82c9259171d0 Mon Sep 17 00:00:00 2001 From: WJ Date: Mon, 6 Nov 2023 20:46:56 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E7=BD=91=E7=BB=9C=E5=92=8C=E5=AE=89?= =?UTF-8?q?=E5=85=A8=EF=BC=8C=E9=83=A8=E5=88=86=E6=9C=AA=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Network/README | 11 +- Network/gjb_S0101801GN.c | 14 +- Network/gjb_S0101802GN.c | 319 ++++++++++ Network/gjb_S0101803GN_1.c | 171 ++++++ Network/gjb_S0101803GN_2.c | 84 +++ Network/gjb_S0101803GN_3.c | 80 +++ Network/gjb_S0101803GN_4.c | 76 +++ Network/gjb_S0101803GN_5.c | 77 +++ Network/gjb_S0101804GN_3.c | 70 +++ Network/gjb_S0101804GN_4.c | 173 ++++++ Network/gjb_S0101804GN_5.c | 103 ++++ security/README | 6 +- security/gjb_S0101611AQ.c | 369 ++++++++++++ security/gjb_S0102601AQ_deadlock.c | 214 +++++++ security/gjb_S0102602AQ.c | 255 ++++++++ security/gjb_S0102603AQ.c | 744 ++++++++++++++++++++++++ security/gjb_S0102605AQ.c | 128 ++++ security/gjb_S0102606AQ_addr_nonalign.c | 120 ++++ security/gjb_S0102606AQ_addr_overflow.c | 115 ++++ security/gjb_S0102606AQ_illgel_code.c | 114 ++++ 20 files changed, 3234 insertions(+), 9 deletions(-) create mode 100644 Network/gjb_S0101802GN.c create mode 100644 Network/gjb_S0101803GN_1.c create mode 100644 Network/gjb_S0101803GN_2.c create mode 100644 Network/gjb_S0101803GN_3.c create mode 100644 Network/gjb_S0101803GN_4.c create mode 100644 Network/gjb_S0101803GN_5.c create mode 100644 Network/gjb_S0101804GN_3.c create mode 100644 Network/gjb_S0101804GN_4.c create mode 100644 Network/gjb_S0101804GN_5.c create mode 100644 security/gjb_S0101611AQ.c create mode 100644 security/gjb_S0102601AQ_deadlock.c create mode 100644 security/gjb_S0102602AQ.c create mode 100644 security/gjb_S0102603AQ.c create mode 100644 security/gjb_S0102605AQ.c create mode 100644 security/gjb_S0102606AQ_addr_nonalign.c create mode 100644 security/gjb_S0102606AQ_addr_overflow.c create mode 100644 security/gjb_S0102606AQ_illgel_code.c diff --git a/Network/README b/Network/README index 76bc247..f68a27f 100644 --- a/Network/README +++ b/Network/README @@ -1,3 +1,12 @@ ###网络管理 -需要切换到root执行:sudo su \ No newline at end of file +## 1.需要切换到root执行:sudo su +## 2.pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。 gcc gjb_S0101802GN.c -o 1802GN -lpthread +## 3.1 需要切换到root执行:sudo su +## 3.2 程序可正常运行,抓包工具找不到IP包 +## 3.3 程序可正常运行,抓包工具抓的为ICMPV6包 +## 3.4 程序可正常运行,出现TCP建立连接包 +## 3.5 程序可正常运行,无UDP包 +## 4.1,4.2功能实现不确定 +## 4.3 应该正常 +## 4.4 暂时缺少服务端。 \ No newline at end of file diff --git a/Network/gjb_S0101801GN.c b/Network/gjb_S0101801GN.c index 1ecbbfa..e1425e9 100644 --- a/Network/gjb_S0101801GN.c +++ b/Network/gjb_S0101801GN.c @@ -1,19 +1,19 @@ /********************************************************************************************************* ** -** GJB ��׼���Լ� +** GJB 标准测试集 ** ** Copyright All Rights Reserved ** -**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +**--------------文件信息-------------------------------------------------------------------------------- ** -** �� �� ��: gjb_S0101801GN.c +** 文 件 名: gjb_S0101801GN.c ** -** �ļ���������: 2021 �� 1 �� 12 �� +** 文件创建日期: 2021 年 1 月 12 日 ** -** �� ��: �����豸�������ܲ��ԡ����Ʋ����������룬ʵ�ֶ����� -** �豸�����ú͹���������������IP��ַ���������롢���ء� +** 描 述: 网络设备管理功能测试。编制测试用例代码,实现对网络 +** 设备的配置和管理,尤其是设置IP地址、子网掩码、网关。 ** -** ͬʱ����ʹ�������޸�IP��ַ, ����, ����: +** 同时可以使用命令修改IP地址, 掩码, 网关: ** ifconfig eth0 192.168.1.110 netmask 255.255.255.0 gateway 192.168.1.1 *********************************************************************************************************/ diff --git a/Network/gjb_S0101802GN.c b/Network/gjb_S0101802GN.c new file mode 100644 index 0000000..0392c16 --- /dev/null +++ b/Network/gjb_S0101802GN.c @@ -0,0 +1,319 @@ +/********************************************************************************************************* +** +** GJB ׼Լ +** +** Copyright All Rights Reserved +** +**--------------ļϢ-------------------------------------------------------------------------------- +** +** : gjb_S0101802GN.c +** +** ļ: 2021 1 12 +** +** : ׼׽ֱ̽ӿڹܲԡñ׼׽ֽӿڱд +** ͻͷʵ繦ܡ +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef TEST_IP +#define TEST_IP "192.168.6.128" +#endif + +#define IP_ADDR TEST_IP + +void server_test() +{ + int s,on = 1; + int ret1 = -2; + int ret2 = -2; + + int newSock; + struct sockaddr_in addr; + int addrLen; + + int ret3 = -2; + int ret4 = -2; + int ret5 = -2; + int ret6 = -2; + + char recvbuf[1024] = {0}; + int buf_len = sizeof(recvbuf); + char sendbuf[] = "This is a message from 192.168.1.110"; + int buf_len1 = sizeof(sendbuf); + struct sockaddr_in srvr; + + s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (s == -1) { + printf("socket error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "socket error!\n"); + // return (GJB_PRI_FAIL()); + } else { + printf("socket success!\n"); + // GJB_PRT_INFO( "socket success!\n"); + } + + /* + * socket ѡ SO_REUSEADDR + ˿ڸüsetsockopt + */ + ret1 = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); + if(ret1 == -1) { + printf("setsockopt error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "setsockopt error!\n"); + // return (GJB_PRI_FAIL()); + } else { + printf("setsockopt success!\n"); + // GJB_PRT_INFO( "setsockopt success!\n"); + } + + int size = sizeof(on); + ret6 = getsockopt( s, SOL_SOCKET, SO_REUSEADDR, + (char *)&on, (socklen_t *)&size); + if(ret6 == -1) { + printf("getsockopt error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "getsockopt error!\n"); + // return (GJB_PRI_FAIL()); + } else { + printf("getsockopt success!\n"); + // GJB_PRT_INFO( "getsockopt success!\n"); + } + + memset(&srvr, 0, sizeof(srvr)); +/* +struct sockaddr_in { + short sa_family;//ַ壬2ֽ + unsigned short int sin_port;//˿ںţ2ֽ + struct in_addr sin_addr;//IPַ4ֽ + unsigned char sin_zero[];//0Աstruct sockaddrͬС8ֽ +} +*/ + srvr.sin_family = AF_INET; + srvr.sin_port = htons(8081); + srvr.sin_addr.s_addr = inet_addr(IP_ADDR); + + ret2 = bind(s, (struct sockaddr *)&srvr, sizeof(srvr)); + if (ret2 == -1) { + printf("bind error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "bind error!\n"); + // return (GJB_PRI_FAIL()); + + } else { + printf("bind success!\n"); + // GJB_PRT_INFO( "bind success!\n"); + } + + /* + * socket + */ + ret3 = listen(s, 10); + if (ret3 == -1) { + printf("listen error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "listen error!\n"); + // return (GJB_PRI_FAIL()); + } else { + printf("listen success!\n"); + // GJB_PRT_INFO( "listen success!\n"); + } + + memset(&addr, 0, sizeof(addr)); + addrLen = sizeof (struct sockaddr); + + newSock = accept(s, (struct sockaddr *)&addr, (socklen_t *)&addrLen); + if (newSock == -1) { + printf("accept error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "accept error!\n"); + // return (GJB_PRI_FAIL()); + } else { + printf("accept success!\n"); + // GJB_PRT_INFO( "accept success!\n"); + } + + ret4 = recv(newSock, recvbuf, buf_len, 0); + if(ret4 == -1) { + printf("recv error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "recv error!\n"); + // return (GJB_PRI_FAIL()); + } else { + printf("recv success!\n"); + // GJB_PRT_INFO( "recv success!\n"); + } + + ret5 = send(newSock, sendbuf, buf_len1, 0); + if (ret5 == -1) { + printf("send error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "send error!\n"); + // return (GJB_PRI_FAIL()); + } else { + printf("send success!\n"); + // GJB_PRT_INFO( "send success!\n"); + } + + close(newSock); + close(s); + + // return 0; +} + +void client_test() +{ + int s; + int ret1 = -2; + int ret2 = -2; + + int newSock; + struct sockaddr_in addr; + int addrLen; + + char sendbuf[] = "This is a message from 192.168.1.88"; + int buf_len1 = sizeof(sendbuf); + struct sockaddr_in srvr; + + /* + * socket TCP + */ + s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (s == -1) { + printf("child socket error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "child socket error!\n"); + // return (GJB_PRI_FAIL()); + + } else { + printf("child socket success!\n"); + // GJB_PRT_INFO( "child socket success!\n"); + } + + memset(&srvr, 0, sizeof(srvr)); + + srvr.sin_family = AF_INET; + srvr.sin_port = htons(8080); + srvr.sin_addr.s_addr = inet_addr(IP_ADDR); + + /* + * IP ַ IP_ADDR ˿ 8080 + */ + ret2 = bind(s, (struct sockaddr *)&srvr, sizeof(srvr)); + if (ret2 == -1) { + printf("child bind error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "child bind error = %d!\n", errno); + // return (GJB_PRI_FAIL()); + + } else { + printf("child bind success!\n"); + // GJB_PRT_INFO( "child bind success!\n"); + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(8081); + addr.sin_addr.s_addr = inet_addr(IP_ADDR); + addrLen = sizeof (addr); + + /* + * Ӹʱ֤Ѿ accept. + */ + sleep(1); + + /* + * IP_ADDR ˿ 8081 + */ + newSock = connect(s, (struct sockaddr *)&addr, addrLen); + if (newSock == -1) { + printf("child connect error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "child connect error!\n"); + // return (GJB_PRI_FAIL()); + + } else { + printf("child connect success!\n"); + // GJB_PRT_INFO( "child connect success!\n"); + } + + /* + * + */ + ret1 = send(s, sendbuf, buf_len1, 0); + if(ret1 == -1) { + printf("child send error!\n"); + // return -1; + // GJB_PRT_ERROR_INFO( "child send error!\n"); + // return (GJB_PRI_FAIL()); + } else { + printf("child send success!\n"); + // GJB_PRT_INFO( "child send success!\n"); + } + + close(newSock); + close(s); + // return 0; +} + +int main() +{ + pthread_t tid, tid1; + int ret; + void *retval1; + void *retval2; + + ret = pthread_create(&tid, NULL, (void *)server_test, NULL); + if (ret != 0) { + printf("server_test create failed!\n"); + return -1; + // GJB_PRT_ERROR_INFO( "server_test create failed!\n"); + // return (GJB_PRI_FAIL()); + } + + ret = pthread_create(&tid1, NULL, (void *)client_test, NULL); + if (ret != 0) { + printf("client_test create failed!\n"); + return -1; + // GJB_PRT_ERROR_INFO( "client_test create failed!\n"); + // return (GJB_PRI_FAIL()); + } + + ret = pthread_join(tid, &retval1); + if (ret != 0) { + printf("pthread join server_test failed!\n"); + return -1; + // GJB_PRT_ERROR_INFO( "pthread join server_test failed!\n"); + // return (GJB_PRI_FAIL()); + } + + ret = pthread_join(tid1, &retval2); + if (ret != 0) { + printf("pthread join client_test failed!\n"); + return -1; + // GJB_PRT_ERROR_INFO( "pthread join client_test failed!\n"); + // return (GJB_PRI_FAIL()); + } + + if (((long)retval1 == 0) && ((long)retval2 == 0)) { + return 0; + // return (GJB_PRI_PASS()); + } else { + return -1; + // return (GJB_PRI_FAIL()); + } +} diff --git a/Network/gjb_S0101803GN_1.c b/Network/gjb_S0101803GN_1.c new file mode 100644 index 0000000..97efaf1 --- /dev/null +++ b/Network/gjb_S0101803GN_1.c @@ -0,0 +1,171 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0101803GN_6.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 基本网络协议支持功能测试。编制测试用例代码,通过调 +** 用TCP、UDP、IP、ICMP和ARP协议接口,验证各项协议正 +** 确性,同时利用网络协议分析工具,分析网络包的正确性。 +** ARP +** +** !!!!使用 wireshark 抓包工具进行分析查看 +*********************************************************************************************************/ + + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#define IPV4_LENGTH 4 +#define Dev "ens33" //网卡名 +#define buffer_len 60 //ARP请求包大小为60B,,抓包时会抓到一些42B的包,这是抓包软件没有显示18B的Padding字段,Padding全0填充在包的末尾 +unsigned char sender_ip[4] = {192,168,6,128}; //ARP请求的源IP +unsigned char target_ip[4] = {192,168,6,1}; //ARP请求的目标IP +/*ARP包结构*/ +/*字段顺序不可更改,发包时是直接将buffer发出*/ +struct arp_head +{ + unsigned short hardware_type; //硬件类型#1:Ethernet + unsigned short protocol_type; //协议类型#0x0800:IPv4 + unsigned char hardware_size; //MAC地址长度#6 + unsigned char protocol_size; //IP地址长度#4 + unsigned short opcode; //ARP类型#1:request;2:reply + unsigned char sender_mac[ETH_ALEN]; //源MAC地址 + unsigned char sender_ip[IPV4_LENGTH]; //源IP地址 + unsigned char target_mac[ETH_ALEN]; //目标MAC地址 + unsigned char target_ip[IPV4_LENGTH]; //目标IP地址 +}; + +int main() +{ + //创建buffer + unsigned char buffer[buffer_len]; + memset(buffer, 0, buffer_len); + //创建以太网头部指针,指向buffer + struct ethhdr *eth_req = (struct ethhdr*)buffer; + //创建ARP包指针,指向buffer的后46字节,因为以太网头包含:2*6B(MAC地址)+2B(协议地址)=14B + struct arp_head *arp_req = (struct arp_head*)(buffer+14); + //创建sockaddr_ll结构地址 + struct sockaddr_ll sock_addr; + //创建socket + /***int socket(int __domain, int __type, int __protocol) + *** __domain + * PF_PACKET指示为二层协议簇 + * 使用AF_PACKET也可,socket.h中有#define AF_PACKET PF_PACKET + *** __type + * 使用PF_PACKET的后,__type只能选择SOCK_RAW或者SOCK_DGRAM + * 其中SOCK_RAW可以自己构造帧头,SOCK_DGRAM不行 + * 帧头使用sockaddr_ll结构体构建,这个结构体在if_packet.h中 + * 有一些资料这里选择的是SOCK_PACKET,这个类型目前已经被建议弃用 + *** __protocol + * ETH_P_ARP意味着我们仅仅接受ARP类型 + * 如果是ETH_P_ALL就意味着我们接受所有类型帧 + * 更多选项参看if_ether.h中定义 + */ + int sock_fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); + if(sock_fd == -1){ + perror("socket()"); + exit(-1); + } + /**获取网卡等需要的信息 + * ifreq结构体可以用于设置或者获取网卡等相关信息,定义在if.h中 + * 配合ioctl()一起使用 + * ioctl()的具体参数用法和系统实现相关,不是通用的,具体参见ioctls.h + * 以下获取的信息都会保存在ifreq不同字段之中 + */ + struct ifreq ifr; + + /*根据网卡设备名获取Index*/ + strcpy(ifr.ifr_name, Dev); + if(ioctl(sock_fd, SIOCGIFINDEX, &ifr) == -1) + { + perror("SIOCGIFINDEX"); + exit(-1); + } + int ifindex = ifr.ifr_ifindex; + printf("网卡索引为:%d\n",ifindex); + + /*获取网卡设备MAC地址*/ + if(ioctl(sock_fd, SIOCGIFHWADDR, &ifr) == -1) + { + perror("SIOCGIFHWADDR"); + exit(-1); + } + + /*将MAC地址写入所需结构*/ + for(int i=0;i<6;i++) + { + //以太网帧的目标MAC,即广播MAC,全1 + eth_req->h_dest[i] = (unsigned char)0xff; + //ARP请求包目标MAC,全0 + arp_req->target_mac[i] = (unsigned char)0x00; + //以太网帧源MAC,即本机MAC + //ifr_hwaddr是sockaddr结构体格式 + eth_req->h_source[i] = (unsigned char)ifr.ifr_hwaddr.sa_data[i]; + //ARP请求包源MAC,即本机MAC + arp_req->sender_mac[i] = (unsigned char)ifr.ifr_hwaddr.sa_data[i]; + //sockaddr中的MAC,也是本地MAC + sock_addr.sll_addr[i] = (unsigned char)ifr.ifr_hwaddr.sa_data[i]; + } + + /*打印MAC地址*/ + printf("网卡MAC地址: %02X:%02X:%02X:%02X:%02X:%02X\n", + eth_req->h_source[0], + eth_req->h_source[1], + eth_req->h_source[2], + eth_req->h_source[3], + eth_req->h_source[4], + eth_req->h_source[5]); + + /*完善sockaddr_ll结构体*/ + sock_addr.sll_family = PF_PACKET; + sock_addr.sll_protocol = htons(ETH_P_ARP); + sock_addr.sll_ifindex = ifindex; + sock_addr.sll_hatype = htons(ARPHRD_ETHER); + sock_addr.sll_halen = ETH_ALEN; + + /*完善以太网帧头*/ + eth_req->h_proto = htons(ETH_P_ARP); + + /*完善ARP包头*/ + arp_req->hardware_type = htons(0x01); + arp_req->protocol_type = htons(ETH_P_IP); + arp_req->hardware_size = ETH_ALEN; + arp_req->protocol_size = IPV4_LENGTH; + arp_req->opcode = htons(ARPOP_REQUEST); + memcpy(arp_req->sender_ip,sender_ip,IPV4_LENGTH); + memcpy(arp_req->target_ip,target_ip,IPV4_LENGTH); + + /*发送ARP请求*/ + if(sendto(sock_fd, buffer, 60, 0, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) == -1) + { + perror("sendto()"); + exit(-1); + } + printf("发送ARP请求包:"); + for(int i=0;i<60;i++) + { + if(i%16==0) + printf("\n\t"); + printf("%02X ",buffer[i]); + } + close(sock_fd); + return 0; +} \ No newline at end of file diff --git a/Network/gjb_S0101803GN_2.c b/Network/gjb_S0101803GN_2.c new file mode 100644 index 0000000..cfac623 --- /dev/null +++ b/Network/gjb_S0101803GN_2.c @@ -0,0 +1,84 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0101803GN_2.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 基本网络协议支持功能测试。编制测试用例代码,通过调 +** 用TCP、UDP、IP、ICMP和ARP协议接口,验证各项协议正 +** 确性,同时利用网络协议分析工具,分析网络包的正确性。 +** IP +** +** !!!!使用 wireshark 抓包工具进行分析查看 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#include +#include +#include +#include +#include +#include + +#ifndef TEST_IP +#define TEST_IP "192.168.6.128" +#endif + +#define IP_ADDR TEST_IP + +int main(int argc, char **argv) +{ + char buf[] = { "hello world" }; + struct sockaddr_in addr; + // int fd = socket(AF_INET, SOCK_RAW, IPPROTO_IP); + // int fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)); + int fd; + if ((fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0) + { + perror(strerror(errno)); + fprintf(stdout, "create socket error\n"); + exit(0); + } + + //socket建立测试 + // if (fd < 0) { + // printf( "child socket error!\n"); + // return (-1); + // } + + //清空接收服务器地址 + memset(&addr, 0, sizeof(addr)); + + //配置接收端服务器 + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(IP_ADDR); + addr.sin_port = htons(8080); + + //发送数据 + sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, sizeof(addr)); + + //关闭socket + close(fd); + + return 0; +} diff --git a/Network/gjb_S0101803GN_3.c b/Network/gjb_S0101803GN_3.c new file mode 100644 index 0000000..238f73f --- /dev/null +++ b/Network/gjb_S0101803GN_3.c @@ -0,0 +1,80 @@ +/********************************************************************************************************* +** +** GJB ׼Լ +** +** Copyright All Rights Reserved +** +**--------------ļϢ-------------------------------------------------------------------------------- +** +** : gjb_S0101803GN_3.c +** +** ļ: 2021 1 12 +** +** : Эֹ֧ܲԡƲ룬ͨ +** TCPUDPIPICMPARPЭӿڣ֤Э +** ȷԣͬʱЭߣȷԡ +** ICMP +** +** ping ԶICMPв +** +** !!!!ʹ wireshark ץ߽з鿴 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef TEST_PC_IP +#define TEST_PC_IP "192.168.6.128" +#endif + +#define IP_ADDR TEST_PC_IP + +int main(int argc, char **argv) +{ + char buf[] = { "hello world" }; + struct sockaddr_in addr; + int fd ; + if((socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0){ + perror(strerror(errno)); + printf("child socket error!\n"); + return -1; + } + + //socket + // if (fd < 0) { + // printf("child socket error!\n"); + // return -1; + // // GJB_PRT_ERROR_INFO( "child socket error!\n"); + // // return (GJB_PRI_FAIL()); + // } + + //սշַ + memset(&addr, 0, sizeof(addr)); + + //ýն˷ + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(IP_ADDR); + addr.sin_port = htons(8080); + + // + sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, sizeof(addr)); + + //رsocket + close(fd); + return 0; + // return (GJB_PRI_PASS()); +} + + diff --git a/Network/gjb_S0101803GN_4.c b/Network/gjb_S0101803GN_4.c new file mode 100644 index 0000000..8703a13 --- /dev/null +++ b/Network/gjb_S0101803GN_4.c @@ -0,0 +1,76 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0101803GN_4.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 基本网络协议支持功能测试。编制测试用例代码,通过调 +** 用TCP、UDP、IP、ICMP和ARP协议接口,验证各项协议正 +** 确性,同时利用网络协议分析工具,分析网络包的正确性。 +** TCP +** +** !!!!使用 wireshark 抓包工具进行分析查看 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#ifndef TEST_PC_IP +#define TEST_PC_IP "192.168.6.128" +#endif + +#define IP_ADDR TEST_PC_IP + +int main(int argc, char **argv) +{ + char buf[] = { "hello world" }; + struct sockaddr_in addr; + // int fd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP); + int fd; + if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0) + { + perror(strerror(errno)); + fprintf(stdout, "create socket error\n"); + exit(0); + } + //socket建立测试 + // if (fd < 0) { + // GJB_PRT_ERROR_INFO( "socket error!\n"); + // return (GJB_PRI_FAIL()); + // } + + //清空接收服务器地址 + memset(&addr, 0, sizeof(addr)); + + //配置接收端服务器 + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(IP_ADDR); + addr.sin_port = htons(8080); + + //发送数据 + sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, sizeof(addr)); + + //关闭socket + close(fd); + return 0; + // return (GJB_PRI_PASS()); +} diff --git a/Network/gjb_S0101803GN_5.c b/Network/gjb_S0101803GN_5.c new file mode 100644 index 0000000..0ed4256 --- /dev/null +++ b/Network/gjb_S0101803GN_5.c @@ -0,0 +1,77 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0101803GN_5.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 基本网络协议支持功能测试。编制测试用例代码,通过调 +** 用TCP、UDP、IP、ICMP和ARP协议接口,验证各项协议正 +** 确性,同时利用网络协议分析工具,分析网络包的正确性。 +** UDP +** +** !!!!使用 wireshark 抓包工具进行分析查看 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef TEST_PC_IP +#define TEST_PC_IP "192.168.6.128" +#endif + +#define IP_ADDR TEST_PC_IP + +int main(int argc, char **argv) +{ + char buf[] = { "hello world" }; + int fd ; + struct sockaddr_in addr; + + //socket建立测试 + if ((socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) < 0) { + perror(strerror(errno)); + fprintf(stdout, "create socket error\n"); + exit(0); + // return -1; + // GJB_PRT_ERROR_INFO( "socket error!\n"); + // return (GJB_PRI_FAIL()); + } + + //清空接收服务器地址 + memset(&addr, 0, sizeof(addr)); + + //配置接收端服务器 + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(IP_ADDR); + addr.sin_port = htons(8080); + + if(connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) + printf("connect() error!"); + + + //发送数据 + sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, sizeof(addr)); + + //关闭socket + close(fd); + + return (0); +} diff --git a/Network/gjb_S0101804GN_3.c b/Network/gjb_S0101804GN_3.c new file mode 100644 index 0000000..3ad7dc5 --- /dev/null +++ b/Network/gjb_S0101804GN_3.c @@ -0,0 +1,70 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0101804GN_3.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 协议栈信息统计功能测试。编制测试用例代码,调用恰当 +** 接口函数获取协议栈统计信息 +** +** routes。 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + + + +int main() { + // 创建套接字并绑定到任意接口 + int sock = socket(AF_INET, SOCK_STREAM, 0); + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_ANY); + addr.sin_port = htons(80); // 假设使用HTTP协议监听所有接口 + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("bind failed"); + exit(1); + } + + // 获取网络接口信息 + struct ifaddrs *ifaddr, *ifa; + getifaddrs(&ifaddr); + + // 遍历接口并输出路由信息 + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr->sa_family == AF_INET) { // 检查是否为IPv4接口 + struct sockaddr_in *addrp = (struct sockaddr_in *)ifa->ifa_addr; + char ip[INET_ADDRSTRLEN]; + if (inet_ntop(AF_INET, &addrp->sin_addr, ip, sizeof(ip))) { // 将IP地址转换为字符串 + printf("Interface %s has IP address %s\n", ifa->ifa_name, ip); + struct sockaddr_in gw; + if (getnameinfo((struct sockaddr *)&ifa->ifa_dstaddr, ifa->ifa_dstaddr->sa_family == AF_INET ? sizeof(gw) : 0, NULL, 0, NULL, 0, NI_NUMERICHOST ) == 0) { // 获取网关地址并输出 + printf("Interface %s has route to %s\n", ifa->ifa_name, ifa->ifa_dstaddr->sa_family == AF_INET ? inet_ntoa(gw.sin_addr) : "any"); + } else { + // printf("Failed to get route to %s\n", ifa->ifa_dstaddr->sa_family == AF_INET ? inet_ntoa(*(struct in_addr *)&ifa->ifa_dstaddr->sa_addr) : "any"); + } + } + } + } + + // 释放接口信息结构体的内存 + freeifaddrs(ifaddr); // 只需要调用一次freeifaddrs函数,因为它已经将所有接口信息释放了内 + close(sock); // 关闭套接字,释放资源。在C语言中,我们通常在完成操作后关闭资源,以防止资源泄漏。这里我们 + return 0; // 程序正常结束并返回0表示成功。如果在处理过程中发生错误或异常情况,需要返回一 +} diff --git a/Network/gjb_S0101804GN_4.c b/Network/gjb_S0101804GN_4.c new file mode 100644 index 0000000..e0df7eb --- /dev/null +++ b/Network/gjb_S0101804GN_4.c @@ -0,0 +1,173 @@ +/********************************************************************************************************* +** +** GJB ��׼���Լ� +** +** Copyright All Rights Reserved +** +**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +** +** �� �� ��: gjb_S0101804GN_4.c +** +** �ļ���������: 2021 �� 1 �� 12 �� +** +** �� ��: Э��ջ��Ϣͳ�ƹ��ܲ��ԡ����Ʋ����������룬����ǡ�� +** �ӿں�����ȡЭ��ջͳ����Ϣ-- +** +** tcp�� +** ��������Ҫʹ�� tools/USR-TCP232-Test.exe ���߽�����ϲ��� +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// tcp+ +#include +#include + + + +#ifndef TEST_IP +#define TEST_IP "192.168.6.128" +#endif +#ifndef TEST_PC_IP +#define TEST_PC_IP "192.168.6.128" +#endif + +int tcp_show(); + +int main() +{ + int s,on = 1; + int ret1 = -2; + int ret2 = -2; + int ret3 = -2; + int ret4 = -2; + int ret5 = -2; + int ret6 = -2; + int newSock; + struct sockaddr_in addr; + struct sockaddr_in srvr; + socklen_t addrLen; + char recvbuf[1024] = {0}; + int buf_len = sizeof(recvbuf); + char sendbuf[] = "This is a message from " TEST_PC_IP; + int buf_len1 = sizeof(sendbuf); + + s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (s == -1) { + printf( "socket error!\n"); + return (-1); + } else { + printf( "socket success!\n"); + } + + ret1 = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); + if (ret1 == -1) { + printf( "setsockopt error!\n"); + return (-1); + } else { + printf( "setsockopt success!\n"); + } + + socklen_t size = sizeof(on); + ret6 = getsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, &size); + if (ret6 == -1) { + printf( "getsockopt error!\n"); + return (-1); + } else { + printf( "getsockopt success!\n"); + } + + memset(&srvr, 0, sizeof(srvr)); + + srvr.sin_family = AF_INET; + srvr.sin_port = htons(8080); + srvr.sin_addr.s_addr =inet_addr(TEST_IP); + + ret2 = bind(s, (struct sockaddr *)&srvr, sizeof(srvr)); + if (ret2 == -1) { + printf( "bind error!\n"); + return (-1); + } else { + printf( "bind success!\n"); + } + + ret3 = listen(s, 10); + if (ret3 == -1) { + printf( "listen error!\n"); + return (-1); + } else { + printf( "listen success!\n"); + } + + memset(&addr, 0, sizeof(addr)); + + addrLen = sizeof (struct sockaddr); + + printf("please start PC TCP client and push \"start\"\n"); + + newSock = accept(s, (struct sockaddr *)&addr, &addrLen); + if (newSock == -1) { + printf( "accept error!\n"); + return (-1); + } else { + printf( "accept success!\n"); + } + + ret4 = recv(newSock, recvbuf, buf_len, 0); + if (ret4 == -1) { + printf( "recv error!\n"); + return (-1); + } else { + printf("recvbuf is %s\n",recvbuf); + printf( "recv success!\n"); + } + + ret5 = send(newSock, sendbuf, buf_len1, 0); + if (ret5 == -1) { + printf( "send error!\n"); + return (-1); + } else { + printf( "send success!\n"); + } + + printf("display TCP information:\n"); + // tcp_show(); + + return (0); +} + +int tcp_show(){ + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock == -1) { + perror("socket"); + exit(1); + } + + struct tcp_info tcpinfo; + socklen_t len = sizeof(tcpinfo); + if (getsockopt(sock, IPPROTO_TCP, TCP_INFO, &tcpinfo, &len) == -1) { + perror("getsockopt"); + close(sock); + exit(1); + } + + printf("TCP Statistics:\n"); + printf(" RTT: %u ms\n", tcpinfo.tcpi_rtt); + printf(" RTO: %u ms\n", tcpinfo.tcpi_rto); + printf(" Send buffer size: %u bytes\n", tcpinfo.tcpi_rcv_mss); + printf(" Receive buffer size: %u bytes\n", tcpinfo.tcpi_rcv_mss); + printf(" Number of retransmissions: %u\n", tcpinfo.tcpi_total_retrans); + + close(sock); + return 0; +} diff --git a/Network/gjb_S0101804GN_5.c b/Network/gjb_S0101804GN_5.c new file mode 100644 index 0000000..003a830 --- /dev/null +++ b/Network/gjb_S0101804GN_5.c @@ -0,0 +1,103 @@ +/********************************************************************************************************* +** +** GJB ��׼���Լ� +** +** Copyright All Rights Reserved +** +**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +** +** �� �� ��: gjb_S0101804GN_5.c +** +** �ļ���������: 2021 �� 1 �� 12 �� +** +** �� ��: Э��ջ��Ϣͳ�ƹ��ܲ��ԡ����Ʋ����������룬����ǡ�� +** �ӿں�����ȡЭ��ջͳ����Ϣ-- +** +** udp. +** +** ��������Ҫʹ�� tools/USR-TCP232-Test.exe ���߽�����ϲ��� +** +** TEST_IP ��Ҫ����Ϊ���ǵ� PC ip +*********************************************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifndef TEST_IP +#define TEST_IP "192.168.6.128" +#endif + +int main() +{ + int s,on = 1; + int ret; + socklen_t len; + struct sockaddr_in srvr,clientAddr; + char recvBuf[64] = {0}; + + memset(&srvr, 0, sizeof(srvr)); + memset(&clientAddr, 0, sizeof(clientAddr)); + memset(recvBuf, 0, 64); + srvr.sin_family = AF_INET; + srvr.sin_addr.s_addr = htonl(INADDR_ANY); + srvr.sin_port = htons(8080); + + clientAddr.sin_family = AF_INET; + clientAddr.sin_port = htons(8080); + clientAddr.sin_addr.s_addr = inet_addr(TEST_IP); + + s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (s == -1) { + printf( "socket error!\n"); + return (-1); + } else { + printf( "socket success!\n"); + } + + ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); + if (ret == -1) { + printf( "setsockopt error!\n"); + return (-1); + } else { + printf( "setsockopt success!\n"); + } + + ret = bind(s, (struct sockaddr *)&srvr, sizeof(srvr)); + if (ret == -1) { + printf( "bind error!\n"); + return (-1); + + } else { + printf( "bind success!\n"); + } + + printf("please start PC UDP server add push \"start\"\n"); + + len = sizeof(srvr); + + while (1) { + ret = recvfrom(s, recvBuf, 64, 0, (struct sockaddr *)&clientAddr, &len); + if (ret == -1) { + printf( "recvfrom error!\n"); + return (-1); + } else { + printf("recvbuf is %s\n",recvBuf); + printf( "recvfrom success!\n"); + } + + printf("display UDP information:\n"); + // udp_show(); + } + + return 0; + // return (GJB_PRI_PASS()); +} \ No newline at end of file diff --git a/security/README b/security/README index 506d597..4880371 100644 --- a/security/README +++ b/security/README @@ -1 +1,5 @@ -###安全管理 \ No newline at end of file +###安全管理 + +## 2:pthread_delay报错,gjb_os_binding 改为bind 剩下glibc错误 +##3 TIMER_RELATIVE_C 报错找不到解决办法,O_RDWR未定义 +##5 gjb_os_illeagal_code找不到。pthread_delay报错 \ No newline at end of file diff --git a/security/gjb_S0101611AQ.c b/security/gjb_S0101611AQ.c new file mode 100644 index 0000000..b9b6d77 --- /dev/null +++ b/security/gjb_S0101611AQ.c @@ -0,0 +1,369 @@ +/* + * S0101604GN.c + * + * Created on: Jan 12, 2021 + * Author: xiaolixue + * + * Description: + * 文件系统掉电安全性测试。目标机上电启动,使用继电器控制电源, + * 使操作系统在正常运行过程中目标机断开电源,模拟异常掉电情况, + * 重复操作200次;编写测试用例代码,在目标机上创建文件,并进行 + * 读写操作,验证异常掉电后文件系统仍能正常工作. + * + * 针对 SylixOS 平台掉电安全文件系统测试程序 + * + * 使用说明, 该程序需要在 SylixOS 系统启动之后自动启动, 一段时间后(例如: 2分钟)将目标板进行断电, + * 然后再上电, 如此过程连续操作 200 次或更多, 通常可以通过继电器实现自动 断电和上电 操作. + * + * 该程序编译完成功之后, 可以修改 /etc/startup.sh 的内容, 将该程序加入启动脚本 + */ +#include +#include +#include +#include +#include +#include + +#define FILESIZE (500 * 1024 * 1024) +#define FILEA "/apps/testsuite_ml/result.log" +#define FILEB "/apps/testsuite_ml/abc12.txt" +#define FILEC "/media/sdcard3/c.txt" +#define FILED "/media/sdcard3/d.txt" +#define FILEE "/media/hdd0/e.txt" +#define FILEF "/media/hdd0/f.txt" + +/******************************************************************************************************* +** 函数名称: tTest1 +** 功能描述: eMMC测试 +** 输 入 : +** 输 出 : +*******************************************************************************************************/ +PVOID tTest1_fs_611(PVOID pvArg) +{ + int testfdA; + int testfdB; + int i = 1; + char buf[5] = { 0 }; + struct stat stA; + struct stat stB; + + sleep(2); + + while (1) { + if (access(FILEA, F_OK) == 0) { + stat(FILEA, &stA); + + if (stA.st_size < FILESIZE) { + testfdA = open(FILEA, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdA < 0) { + fprintf(stderr, "open fileA1: files failed.\n"); + return NULL; + } + + while (1) { + sprintf(buf, "%d ", i); + write(testfdA, buf, sizeof(buf)); + fsync(testfdA); + i++; + if (i % 1000 == 0) { + printf("file %s write.\n", FILEA); + } + } + + close(testfdA); + } else { + + remove(FILEA); + + printf("file %s operater success.\n", FILEA); + + testfdB = open(FILEB, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdB < 0) { + fprintf(stderr, "open fileB1: files failed.\n"); + return NULL; + } + + close(testfdB); + continue; + } + + } else if (access(FILEB, F_OK) == 0) { + stat(FILEB, &stB); + + if (stB.st_size < FILESIZE) { + + testfdB = open(FILEB, O_RDWR | O_CREAT | O_APPEND,0666); + + while (1) { + sprintf(buf, "%d ", i); + + if (testfdB < 0) { + fprintf(stderr, "open fileB2: files failed.\n"); + return NULL; + } + + write(testfdB, buf, sizeof(buf)); + + fsync(testfdB); + i++; + + if (i % 1000 == 0) { + printf("file %s write.\n", FILEB); + } + } + + close(testfdB); + } else { + remove(FILEB); + + printf("file %s operater success.\n", FILEB); + testfdA = open(FILEA, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdA < 0) { + fprintf(stderr, "open fileA2: files failed.\n"); + return NULL; + } + + close(testfdA); + continue; + } + } else { + testfdA = open(FILEA, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdA < 0) { + fprintf(stderr, "open fileA3: files failed.\n"); + return NULL; + } + close(testfdA); + } + } + + return NULL; +} +/******************************************************************************************************* +** 函数名称: tTest2 +** 功能描述: SD-Card 测试 +** 输 入 : +** 输 出 : +*******************************************************************************************************/ +PVOID tTest2_fs_611(PVOID pvArg) +{ + int testfdC; + int testfdD; + int i = 1; + char buf[5] = { 0 }; + struct stat stC; + struct stat stD; + + sleep(2); + + while (1) { + if (access(FILEC, F_OK) == 0) { + stat(FILEC, &stC); + if (stC.st_size < FILESIZE) { + testfdC = open(FILEC, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdC < 0) { + fprintf(stderr, "open fileC1: files failed.\n"); + return NULL; + } + + while (1) { + sprintf(buf, "%d ", i); + write(testfdC, buf, sizeof(buf)); + + fsync(testfdC); + i++; + } + close(testfdC); + + } else { + remove(FILEC); + testfdD = open(FILED, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdD < 0) { + fprintf(stderr, "open fileD1: files failed.\n"); + return NULL; + } + close(testfdD); + continue; + } + + } else if (access(FILED, F_OK) == 0) { + stat(FILED, &stD); + + if (stD.st_size < FILESIZE) { + + testfdD = open(FILED, O_RDWR | O_CREAT | O_APPEND,0666); + + while (1) { + sprintf(buf, "%d ", i); + + if (testfdD < 0) { + fprintf(stderr, "open fileD2: files failed.\n"); + return NULL; + } + + write(testfdD, buf, sizeof(buf)); + + fsync(testfdD); + i++; + } + + close(testfdD); + } else { + remove(FILED); + + testfdC = open(FILEC, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdC < 0) { + fprintf(stderr, "open fileC2: files failed.\n"); + return NULL; + } + + close(testfdC); + continue; + } + } else { + testfdC = open(FILEC, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdC < 0) { + fprintf(stderr, "open fileC3: files failed.\n"); + return NULL; + } + close(testfdC); + } + } + + return NULL; +} +/******************************************************************************************************* +** 函数名称: tTest3 +** 功能描述: SSD 测试 +** 输 入 : +** 输 出 : +*******************************************************************************************************/ +PVOID tTest3_fs_611(PVOID pvArg) +{ + int testfdE; + int testfdF; + int i = 1; + char buf[5] = { 0 }; + struct stat stE; + struct stat stF; + + sleep(2); + + while (1) { + if (access(FILEE, F_OK) == 0) { + stat(FILEE, &stE); + if (stE.st_size < FILESIZE) { + testfdE = open(FILEE, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdE < 0) { + fprintf(stderr, "open fileE1: files failed.\n"); + return NULL; + } + + while (1) { + sprintf(buf, "%d ", i); + write(testfdE, buf, sizeof(buf)); + + fsync(testfdE); + i++; + + } + close(testfdE); + + } else { + remove(FILEE); + + printf("file %s operater success.\n", FILEE); + + testfdF = open(FILEB, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdF < 0) { + fprintf(stderr, "open fileF1: files failed.\n"); + return NULL; + } + close(testfdF); + continue; + } + + } else if (access(FILEF, F_OK) == 0) { + stat(FILEF, &stF); + + if (stF.st_size < FILESIZE) { + + testfdF = open(FILEF, O_RDWR | O_CREAT | O_APPEND,0666); + + while (1) { + sprintf(buf, "%d ", i); + + if (testfdF < 0) { + fprintf(stderr, "open fileF2: files failed.\n"); + return NULL; + } + + write(testfdF, buf, sizeof(buf)); + + fsync(testfdF); + i++; + } + close(testfdF); + + } else { + remove(FILEF); + + printf("file %s operater success.\n", FILEF); + + testfdE = open(FILEE, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdE < 0) { + fprintf(stderr, "open fileE2: files failed.\n"); + return NULL; + } + close(testfdE); + continue; + } + + } else { + testfdE = open(FILEE, O_RDWR | O_CREAT | O_APPEND,0666); + if (testfdE < 0) { + fprintf(stderr, "open fileE3: files failed.\n"); + return NULL; + } + close(testfdE); + } + } + + return NULL; +} +/******************************************************************************************************* +** 函数名称: tTest3 +** 功能描述: 测试程序主函数 +** 输 入 : +** 输 出 : +*******************************************************************************************************/ +int gjb_S0101611AQ(int argc, char **argv) +{ + pthread_t hThreadId1, hThreadId2,hThreadId3; + pthread_attr_t threadattr1, threadattr2,threadattr3; + cpu_set_t cpuset; + + pthread_attr_init(&threadattr1); +// pthread_attr_init(&threadattr2); + //pthread_attr_init(&threadattr3); // SD卡测试 + + pthread_create(&hThreadId1, &threadattr1, tTest1_fs_611, NULL); +// pthread_create(&hThreadId2, &threadattr2, tTest2_fs_611, NULL); + //pthread_create(&hThreadId3, &threadattr3, tTest3_fs_611, NULL); // SD卡测试 + + CPU_ZERO(&cpuset); + CPU_SET(0, &cpuset); + pthread_setaffinity_np(hThreadId1, sizeof(cpu_set_t), &cpuset); /* 设置线程hThreadld1在CPU1上运行*/ + +// CPU_ZERO(&cpuset); +// CPU_SET(2, &cpuset); +// pthread_setaffinity_np(hThreadId2, sizeof(cpu_set_t), &cpuset); /* 设置线程hThreadld2在CPU2上运行*/ + + /* + * SD 卡测试 + */ + //CPU_ZERO(&cpuset); + //CPU_SET(3, &cpuset); + //pthread_setaffinity_np(hThreadId3, sizeof(cpu_set_t), &cpuset); /* 设置线程hThreadld3在CPU3上运行*/ + + return (ERROR_NONE); +} diff --git a/security/gjb_S0102601AQ_deadlock.c b/security/gjb_S0102601AQ_deadlock.c new file mode 100644 index 0000000..b2769c9 --- /dev/null +++ b/security/gjb_S0102601AQ_deadlock.c @@ -0,0 +1,214 @@ +/********************************************************************************************************* +** +** GJB ׼Լ +** +** Copyright All Rights Reserved +** +**--------------ļϢ-------------------------------------------------------------------------------- +** +** : gjb_S0102601AQ_deadlock.c +** +** ļ: 2021 1 12 +** +** : , sylixOS ͨ tp 鿴״̬ +*********************************************************************************************************/ +#ifdef SYLIXOS +#define __SYLIXOS_KERNEL +#endif + +#include "gjb.h" + +sem_t Csem; +sem_t Dsem; + +static pthread_t thread_id3; +static pthread_t thread_id4; +static pthread_attr_t thread_attr3; +static pthread_attr_t thread_attr4; + +void *Fun3_security_1 (void *arg) +{ + int res = -100; + int nDelaytime = 800; + + GJB_PRT_INFO("Task3 is running\n"); + + //ȡCֵźԴ + res = sem_wait(&Csem); + if (res == 0) { + GJB_PRT_INFO("Task3 Get Csem Succesful\n"); + } else { + GJB_PRT_ERROR_INFO("Task3 Get Csem Failed\n"); + } + + //ʱ + sleep(2); + + //ȡDźԴ + res = -100; + res = sem_wait(&Dsem); + if (res == 0) { + GJB_PRT_INFO("Task3 Get Dsem Succesful\n"); + + } else { + GJB_PRT_ERROR_INFO("Task3 Get Dsem Failed\n"); + } + + //ʱ + pthread_delay(nDelaytime); //1000ticks + res = -100; + res = sem_post(&Csem); + if (res == 0) { + GJB_PRT_INFO("Task3 Release Csem succesful\n"); + + } else { + GJB_PRT_ERROR_INFO("Task3 Release Csem Failed\n"); + } + + //ʱ + pthread_delay(nDelaytime); //1000ticks + + res = -100; + res = sem_post(&Dsem); + if (res == 0) { + GJB_PRT_INFO("Task3 Release Dsem succesful\n"); + } else { + GJB_PRT_ERROR_INFO("Task3 Release Dsem Failed\n"); + } + + return NULL; +} + +void *Fun4_security_1 (void *arg) +{ + int res = -100; + int nDelaytime = 1000; //ʱʱ + + GJB_PRT_INFO("Task4 is running\n"); + + //ȡCֵźԴ + res = sem_wait(&Dsem); + if (res == 0) { + GJB_PRT_INFO("Task4 Get Dsem succesful\n"); + + } else { + GJB_PRT_ERROR_INFO("Task4 Get Dsem Failed\n"); + goto error; + } + + //ʱ + sleep(2); + + //ȡDźԴ + res = -100; + res = sem_wait(&Csem); + if (res == 0) { + GJB_PRT_INFO("Task4 Get Csem succesful\n"); + + } else { + GJB_PRT_ERROR_INFO("Task4 Get Csem Failed\n"); + goto error; + } + + //ʱ + pthread_delay(nDelaytime); //1000ticks + + res = -100; + res = sem_post(&Dsem); + if (res == 0) { + GJB_PRT_INFO("Task4 Release Dsem succesful\n"); + + } else { + GJB_PRT_ERROR_INFO("Task4 Release Dsem Failed\n"); + goto error; + } + + //ʱ + pthread_delay(nDelaytime); //1000ticks + + res = -100; + res = sem_post(&Csem); + if (res == 0) { + GJB_PRT_INFO("Task4 Release Csem succesful\n"); + + } else { + GJB_PRT_ERROR_INFO("Task4 Release Csem Failed\n"); + goto error; + } + + return NULL; + +error: + return ((void *)123); +} + +//ں +int Test_S0102101AQ_2 (void) +{ + int status = -100; + + //ֵźʼ + sem_init(&Csem, 0, 1); + sem_init(&Dsem, 0, 1); + + // + pthread_attr_init(&thread_attr3); + pthread_attr_init(&thread_attr4); + + thread_attr3.stackaddr = NULL; + thread_attr3.stacksize = PTHREAD_STACK_MIN * 2; + thread_attr3.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr3.schedpolicy = SCHED_FIFO; + thread_attr3.schedparam.sched_priority = 100; + thread_attr3.name = "Task3"; + + thread_attr4.stackaddr = NULL; + thread_attr4.stacksize = PTHREAD_STACK_MIN * 2; + thread_attr4.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr4.schedpolicy = SCHED_FIFO; + thread_attr4.schedparam.sched_priority = 100; + thread_attr4.name = "Task4"; + + status = -100; + + status = pthread_create(&thread_id3, &thread_attr3, Fun3_security_1, NULL); + if (status == 0) { + GJB_PRT_INFO("Task3 create succesful\n"); + + } else { + GJB_PRT_ERROR_INFO("Task3 create Failed\n"); + } + + status = -100; + status = pthread_create(&thread_id4, &thread_attr4, Fun4_security_1, NULL); + if (status == 0) { + GJB_PRT_INFO( "Task4 create succesful\n"); + + } else { + GJB_PRT_ERROR_INFO( "Task4 create Failed\n"); + } + + pthread_join(thread_id3, NULL); + pthread_join(thread_id4, NULL); + + return (0); +} +/********************************************************************** + * ƣ GJB_ENTRY() + * û + * + * + * ֵ + * ˵ + **************************************************************************/ +int gjb_S0102601AQ_deadlock(int argc, char **argv) +{ + int result; + + result = Test_S0102101AQ_2(); /* security */ + if (result != 0) { + return (GJB_PRI_FAIL()); + } + + return (GJB_PRI_PASS()); +} diff --git a/security/gjb_S0102602AQ.c b/security/gjb_S0102602AQ.c new file mode 100644 index 0000000..2857381 --- /dev/null +++ b/security/gjb_S0102602AQ.c @@ -0,0 +1,255 @@ +/********************************************************************************************************* +** +** GJB ��׼���Լ� +** +** Copyright All Rights Reserved +** +**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +** +** �� �� ��: gjb_S0102602AQ.c +** +** �ļ���������: 2021 �� 1 �� 12 �� +** +** �� ��: ��֤���ȼ���ת�㷨, ����3���̣߳����ȼ�Ϊ �� �� ��, �����ȼ��߳��Ȼ�û�����, �����ȼ��߳̿�ʼ����, һ��ʱ�� +** �����ȼ��߳����в����Ի���������ȡʧ�ܣ������ȼ��߳̿�ʼ����(��ʱ�����ȼ��߳�) +*********************************************************************************************************/ +include +#include +#include +#include +#include + +#ifdef SYLIXOS +#define __SYLIXOS_KERNEL +#endif + +//#include "gjb.h" + +static pthread_mutex_t Amutex; +static pthread_barrier_t bt; +static pthread_t thread_id1; +static pthread_t thread_id2; +static pthread_t thread_id3; +static pthread_attr_t thread_attr1; +static pthread_attr_t thread_attr2; +static pthread_attr_t thread_attr3; + +void *Fun1_2 (void *arg) +{ + volatile int i = 0, j = 0; + int res; + + bind(pthread_self(), 0); + + pthread_barrier_wait(&bt); + + printf("low-task is running\n"); + + //��ȡ������A��Դ + res = -100; + res = pthread_mutex_lock(&Amutex); + + if (res == 0) { + printf("low-task Get Amutex succesful\n"); + + } else { + printf("low-task Get Amutex Failed\n"); + return ((void *)123); + } + + printf("low-task Get Amutex and running\n"); + + for (i = 0; i < 1000; i++) { + for (j = 0; j < 100000; j++); + for (j = 0; j < 1000000; j++); + if (i % 100 == 0) { + printf("low-task Get Amutex and running\n"); + } + } + + res = -100; + res = pthread_mutex_unlock(&Amutex); // �������ȼ��ָ������Իᵼ�¸����ȼ��ȴ�ӡ�ͷ����ɹ� + if (res == 0) { + printf("low-task Release Amutex succesful\n"); + + } else { + printf("low-task Release Amutex Failed\n"); + return ((void *)123); + } + + return NULL; +} + +void *Fun2_2 (void *arg) +{ + volatile int i = 0, j = 0; + + bind(pthread_self(), 0); + pthread_barrier_wait(&bt); + + printf("mid-task is running\n"); + + pthread_delay(200); // ��������ȼ���ʼ���У������Ƴ���֤�����ȼ������� + + for (i = 0; i < 1000; i++) { + for (j = 0; j < 100000; j++); + for (j = 0; j < 10000; j++); + if (i % 100 == 0) { + printf("mid-task is running\n"); + } + } + + return NULL; +} + +void *Fun3_3 (void *arg) +{ + int i = 0; + int res; + + bind(pthread_self(), 0); + pthread_barrier_wait(&bt); + + printf("high-task is running\n"); + + pthread_delay(800); // ������ȼ��ó�CPU, ��֤�����ȼ���ʼ���� + printf("high-task reques muext Amutex\n"); + + //��ȡ������A��Դ + res = -100; + res = pthread_mutex_lock(&Amutex); + if (res == 0) { + printf("high-task Get Amutex succesful\n"); + + } else { + printf("high-task Get Amutex Failed\n"); + } + + for (i = 0; i < 10000; i++) { + if (i % 100 == 0) { + printf("high-task Get Amutex and running\n"); + } + } + + res = -100; + res = pthread_mutex_unlock(&Amutex); + if (res == 0) { + printf("high-task Release Amutex succesful\n"); + + } else { + printf("high-task Release Amutex Failed\n"); + return ((void *)123); + } + + return NULL; +} + +//��ں��� +int Test_S0102102AQ_1 (void) +{ + int status = -100; + int nDelaytime = 2; //��ʱ2��ticks + + bind(pthread_self(), 0); + + pthread_barrier_init(&bt, NULL, 4); + + //��������ʼ�� + pthread_mutex_init(&Amutex, NULL); + + //�������� + pthread_attr_init(&thread_attr1); + pthread_attr_init(&thread_attr2); + pthread_attr_init(&thread_attr3); + + // thread_attr1.stackaddr = NULL; + // thread_attr1.stacksize = PTHREAD_STACK_MIN * 2; + // thread_attr1.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr1.schedpolicy = SCHED_RR; + // thread_attr1.schedparam.sched_priority = 100; + // thread_attr1.name = "low"; + + // thread_attr2.stackaddr = NULL; + // thread_attr2.stacksize = PTHREAD_STACK_MIN * 2; + // thread_attr2.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr2.schedpolicy = SCHED_RR; + // thread_attr2.schedparam.sched_priority = 110; + // thread_attr2.name = "mid"; + + // thread_attr3.stackaddr = NULL; + // thread_attr3.stacksize = PTHREAD_STACK_MIN * 2; + // thread_attr3.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr3.schedpolicy = SCHED_RR; + // thread_attr3.schedparam.sched_priority = 120; + // thread_attr3.name = "high"; + + status = -100; + + status = pthread_create(&thread_id3, &thread_attr3, Fun3_3, NULL); + if (status == 0) { + printf("high-task create succesful\n"); + + } else { + printf("high-task create Failed\n"); + goto error; + } + + //��ʱ + pthread_delay(nDelaytime); // 2��ticks + + status = -100; + + status = pthread_create(&thread_id2, &thread_attr2, Fun2_2, NULL); + if (status == 0) { + printf("mid-task create succesful\n"); + + } else { + printf("mid-task create Failed\n"); + goto error; + } + + //��ʱ + pthread_delay(nDelaytime); // 2��ticks + + status = -100; + + status = pthread_create(&thread_id1, &thread_attr1, Fun1_2, NULL); + if (status == 0) { + printf("low-task create succesful\n"); + + } else { + printf("low-task create Failed\n"); + goto error; + } + + + pthread_barrier_wait(&bt); + + pthread_join(thread_id1, NULL); + pthread_join(thread_id2, NULL); + pthread_join(thread_id3, NULL); + + return (0); + +error: + return (-1); +} +/********************************************************************** + * �������ƣ� GJB_ENTRY() + * ���������� �û�������� + * ��������� �� + * ��������� �� + * �� �� ֵ�� �� + * ����˵���� �� + **************************************************************************/ +int main(int argc, char **argv) +{ + int result; + + result = Test_S0102102AQ_1(); /* security */ + if (result != 0) { + return (-1); + } + + return (0); +} diff --git a/security/gjb_S0102603AQ.c b/security/gjb_S0102603AQ.c new file mode 100644 index 0000000..1072fde --- /dev/null +++ b/security/gjb_S0102603AQ.c @@ -0,0 +1,744 @@ +/********************************************************************************************************* +** +** GJB ��׼���Լ� +** +** Copyright All Rights Reserved +** +**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +** +** �� �� ��: gjb_S0102603AQ.c +** +** �ļ���������: 2021 �� 1 �� 20 �� +** +** �� ��: �����꺯���ӿڳ����ȶ��Բ���, ͨ�����ò�ͬ�ĺ����ӿڲ��жϺ����ķ���ֵ +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include + + + + +#ifdef SYLIXOS +#define __SYLIXOS_KERNEL +#endif + +// #include "gjb.h" + +#define TimeLen 1800 +#define DelayTime 30 + +static pthread_t thread_id1; +static pthread_t thread_id2; +static pthread_t thread_id3; +static pthread_t thread_id4; +static pthread_t thread_id5; +static pthread_t thread_id6; +static pthread_t thread_id7; +static pthread_t thread_id8; +static pthread_t thread_id9; +static pthread_t thread_id10; + +static pthread_attr_t thread_attr1; +static pthread_attr_t thread_attr2; +static pthread_attr_t thread_attr3; +static pthread_attr_t thread_attr4; +static pthread_attr_t thread_attr5; +static pthread_attr_t thread_attr6; +static pthread_attr_t thread_attr7; +static pthread_attr_t thread_attr8; +static pthread_attr_t thread_attr9; +static pthread_attr_t thread_attr10; + +static pthread_mutex_t Amutex; +static sem_t Bsem; + +static int bFlag = 0; + +static void *Fun1_security(void *arg); +static void *Fun2_security(void *arg); +static void *Fun3_security(void *arg); +static void *Fun4_security(void *arg); +static void *Fun5_security(void *arg); +static void *Fun6_security(void *arg); +void *Fun7_security(void *arg); +void *Fun8_security(void *arg); +void *Fun9_security(void *arg); +void *Fun10_security(void *arg); +int S0102103AQ(void); + +void handler_xx (int signo) +{ + printf("Fun handler is running\n"); + bFlag = 1; +} + +int Test_S0102103AQ (void) +{ + int res = 0; + clockid_t clock_id = CLOCK_REALTIME; + struct timespec tp; + struct timespec rqtp; + long nTime1, nTime2; + int nRunTimes=0; + + pthread_mutex_init(&Amutex, NULL); + + sem_init(&Bsem, 0, 2); + + if (res == 0) { + res = clock_gettime(clock_id, &tp); + if (res == 0) { + nTime1 = tp.tv_sec; + printf("nTime1 = %ld\n", nTime1); + + } else { + printf("clock_gettime nTime1 error\n"); + return (-1); + } + + } else { + printf("clock_getcpuclockid error\n"); + return (-1); + } + + while (1) { + if (nRunTimes++ % 50 == 0) { + printf("Rounds:%d\n", nRunTimes); + mem_show(); + } + + S0102103AQ(); + + clock_gettime(clock_id, &tp); + + nTime2 = tp.tv_sec; + + printf("Time:\tnTime1 = %ld\tnTime2 = %ld\tnTime2 - nTime1 = %ld\n", nTime1, nTime2, nTime2 - nTime1); + + if (nTime2 - nTime1 > TimeLen) { + pthread_delay(1000); + printf("Time Over! \t nTime1 = %ld \t nTime2 = %ld \t nTime2 - nTime1 = %ld\n", nTime1, nTime2, nTime2 - nTime1); + break; + + } else { + rqtp.tv_sec = DelayTime; + rqtp.tv_nsec = 0; + nanosleep(&rqtp, NULL); + } + } + + return (0); +} + +int S0102103AQ (void) +{ + int res = -100; + + pthread_attr_init(&thread_attr1); + pthread_attr_init(&thread_attr2); + pthread_attr_init(&thread_attr3); + pthread_attr_init(&thread_attr4); + pthread_attr_init(&thread_attr5); + pthread_attr_init(&thread_attr6); + pthread_attr_init(&thread_attr7); + pthread_attr_init(&thread_attr8); + pthread_attr_init(&thread_attr9); + pthread_attr_init(&thread_attr10); + + // thread_attr1.stackaddr = NULL; + // thread_attr1.stacksize = PTHREAD_STACK_MIN*2; + // thread_attr1.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr1.schedpolicy = SCHED_FIFO; + // thread_attr1.schedparam.sched_priority = 90; + // thread_attr1.name = "Task1"; + + // thread_attr2.stackaddr = NULL; + // thread_attr2.stacksize = PTHREAD_STACK_MIN*2; + // thread_attr2.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr2.schedpolicy = SCHED_FIFO; + // thread_attr2.schedparam.sched_priority = 90; + // thread_attr2.name = "Task2"; + + // thread_attr3.stackaddr = NULL; + // thread_attr3.stacksize = PTHREAD_STACK_MIN*2; + // thread_attr3.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr3.schedpolicy = SCHED_FIFO; + // thread_attr3.schedparam.sched_priority = 90; + // thread_attr3.name = "Task3"; + + // thread_attr4.stackaddr = NULL; + // thread_attr4.stacksize = PTHREAD_STACK_MIN*2; + // thread_attr4.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr4.schedpolicy = SCHED_FIFO; + // thread_attr4.schedparam.sched_priority = 100; + // thread_attr4.name = "Task4"; + + // thread_attr5.stackaddr = NULL; + // thread_attr5.stacksize = PTHREAD_STACK_MIN*2; + // thread_attr5.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr5.schedpolicy = SCHED_FIFO; + // thread_attr5.schedparam.sched_priority = 100; + // thread_attr5.name = "Task5"; + + // thread_attr6.stackaddr = NULL; + // thread_attr6.stacksize = PTHREAD_STACK_MIN*2; + // thread_attr6.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr6.schedpolicy = SCHED_FIFO; + // thread_attr6.schedparam.sched_priority = 100; + // thread_attr6.name = "Task6"; + + // thread_attr7.stackaddr = NULL; + // thread_attr7.stacksize = PTHREAD_STACK_MIN*2; + // thread_attr7.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr7.schedpolicy = SCHED_FIFO; + // thread_attr7.schedparam.sched_priority = 110; + // thread_attr7.name = "Task7"; + + // thread_attr8.stackaddr = NULL; + // thread_attr8.stacksize = PTHREAD_STACK_MIN*2; + // thread_attr8.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr8.schedpolicy = SCHED_FIFO; + // thread_attr8.schedparam.sched_priority = 110; + // thread_attr8.name = "Task8"; + + // thread_attr9.stackaddr = NULL; + // thread_attr9.stacksize = PTHREAD_STACK_MIN*2; + // thread_attr9.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr9.schedpolicy = SCHED_FIFO; + // thread_attr9.schedparam.sched_priority = 110; + // thread_attr9.name = "Task9"; + + // thread_attr10.stackaddr = NULL; + // thread_attr10.stacksize = PTHREAD_STACK_MIN*2; + // thread_attr10.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr10.schedpolicy = SCHED_FIFO; + // thread_attr10.schedparam.sched_priority = 120; + // thread_attr10.name = "Task10"; + + res = -100; + res = pthread_create( &thread_id1,&thread_attr1, Fun1_security, NULL ); + if (res == 0) { + printf( "Task1 create succesful\n"); + + } else { + printf( "Task1 create Failed\n"); + goto error; + } + + res = -100; + res = pthread_create( &thread_id2,&thread_attr2, Fun2_security, NULL ); + if (res == 0) { + printf( "Task2 create succesful\n"); + + } else { + printf( "Task2 create Failed\n"); + goto error; + } + + res = -100; + res = pthread_create( &thread_id3,&thread_attr3, Fun3_security, NULL ); + if (res == 0) { + printf( "Task3 create succesful\n"); + + } else { + printf( "Task3 create Failed\n"); + goto error; + } + + res = -100; + res = pthread_create( &thread_id4,&thread_attr4, Fun4_security, NULL ); + if (res == 0) { + printf( "Task4 create succesful\n"); + } else { + printf( "Task4 create Failed\n"); + goto error; + } + + res = -100; + res = pthread_create( &thread_id5,&thread_attr5, Fun5_security, NULL ); + if (res == 0) { + printf( "Task5 create succesful\n"); + + } else { + printf( "Task5 create Failed\n"); + goto error; + } + + res = -100; + res = pthread_create(&thread_id6,&thread_attr6, Fun6_security, NULL); + if (res == 0) { + printf( "Task6 create succesful\n"); + + } else { + printf( "Task6 create Failed\n"); + goto error; + } + + res = -100; + res = pthread_create(&thread_id7,&thread_attr7, Fun7_security, NULL); + if (res == 0) { + printf( "Task7 create succesful\n"); + + } else { + printf( "Task7 create Failed\n"); + goto error; + } + + res = -100; + res = pthread_create(&thread_id8,&thread_attr8, Fun8_security, NULL); + if (res == 0) { + printf( "Task8 create succesful\n"); + + } else { + printf( "Task8 create Failed\n"); + goto error; + } + + res = -100; + res = pthread_create( &thread_id9,&thread_attr9, Fun9_security, NULL); + if (res == 0) { + printf( "Task9 create succesful\n"); + + } else { + printf( "Task9 create Failed\n"); + goto error; + } + + res = -100; + res = pthread_create(&thread_id10,&thread_attr10, Fun10_security, NULL); + if (res == 0) { + printf( "Task10 create succesful\n"); + + } else { + printf( "Task10 create Failed\n"); + goto error; + } + + pthread_join(thread_id1, NULL); + pthread_join(thread_id2, NULL); + pthread_join(thread_id3, NULL); + pthread_join(thread_id4, NULL); + pthread_join(thread_id5, NULL); + pthread_join(thread_id6, NULL); + pthread_join(thread_id7, NULL); + pthread_join(thread_id8, NULL); + pthread_join(thread_id9, NULL); + pthread_join(thread_id10, NULL); + + return (0); + +error: + return (-1); +} + +void *Fun1_security (void *arg) +{ + int res = -100; + int nDelaytime = 20; + + res = pthread_mutex_lock(&Amutex); + if (res != 0) { + printf("Task1:Get Amutex Failed\n"); + return ((void *)123); + } + + pthread_delay(nDelaytime);//1000??ticks + + res = -100; + res = pthread_mutex_unlock(&Amutex); + + if (res != 0) { + printf("Task1:Release Amutex Failed\tres = %d \n", res); + return ((void *)123); + } + + return NULL; +} + +void *Fun2_security (void *arg) +{ + int res = -100; + int nDelaytime = 20; + + res = pthread_mutex_lock(&Amutex); + if (res != 0) { + printf("Task2:Get Amutex Failed\n"); + return ((void *)123); + } + + pthread_delay(nDelaytime); + + res = -100; + res = pthread_mutex_unlock(&Amutex); + + if (res != 0) { + printf("Task2:Release Amutex Failed\n"); + return ((void *)123); + } + + return NULL; +} + +void *Fun3_security (void *arg) +{ + int res = -100; + int nDelaytime = 20; + + res = sem_wait(&Bsem); + if (res != 0) { + printf("Task3:Get Bsem Failed\n"); + return ((void *)123); + } + + pthread_delay(nDelaytime); + + res = sem_post(&Bsem); + if (res != 0) { + printf("Task3:Release Bsem Failed\n"); + return ((void *)123); + } + + return NULL; +} + +void *Fun4_security (void *arg) +{ + int res = -100; + int nDelaytime = 20; + + res = pthread_mutex_lock(&Amutex); + if (res !=0 ) { + printf("Task4:Get Amutex Failed\n"); + return ((void *)123); + } + + pthread_delay(nDelaytime); + + res = -100; + res = pthread_mutex_unlock(&Amutex); + if (res != 0) { + printf("Task4:Release Amutex Failed\n"); + return ((void *)123); + } + + return NULL; +} + +void *Fun5_security (void *arg) +{ + int res = -100; + int nDelaytime = 20; + + res = sem_wait(&Bsem); + if (res != 0) { + printf("Task5:Get Bsem Failed\n"); + return ((void *)123); + } + + pthread_delay(nDelaytime); + + res = sem_post(&Bsem); + if (res != 0) { + printf("Task5:Release Bsem Failed\n"); + return ((void *)123); + } + + return NULL; +} + +void *Fun6_security (void *arg) +{ + int res = -100; + int nDelaytime = 20; + + res = sem_wait(&Bsem); + if (res != 0) { + printf("Task6:Get Bsem Failed\n"); + return ((void *)123); + } + + pthread_delay(nDelaytime); + + res = sem_post(&Bsem); + if (res != 0) { + printf("Task6:Release Bsem Failed\n"); + return ((void *)123); + } + + return NULL; +} + +void *Fun7_security (void *arg) +{ + int res = -100; + int nDelaytime = 20; + + res = sem_wait(&Bsem); + if (res != 0) { + printf("Task7:Get Bsem Failed\n"); + return ((void *)123); + } + + pthread_delay(nDelaytime); + + res = sem_post(&Bsem); + if (res != 0) { + printf("Task7:Release Bsem Failed\n"); + return ((void *)123); + } + + return NULL; +} + +void *Fun8_security (void *arg) +{ + int res = -100; + timer_t tid; + struct itimerspec ovalue; + struct itimerspec its; + struct sigevent ev; + struct sigaction act; + + ev.sigev_notify = SIGEV_SIGNAL; + ev.sigev_signo = SIGALRM; + + act.sa_handler = handler_xx; + act.sa_flags=0; + + if (sigemptyset(&act.sa_mask) != 0) { + printf("sigemptyset() was not successful\n"); + return ((void *)123); + } + + if (sigaction(SIGALRM, &act, 0) != 0) { + printf("sigaction() was not successful\n"); + return ((void *)123); + } + + bFlag = 0; + + res = timer_create(CLOCK_REALTIME, &ev, &tid); + if (res != 0) { + printf("timer_create Failed\n"); + return ((void *)123); + } + + its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; + its.it_value.tv_sec = 1; its.it_value.tv_nsec = 0; + + res = timer_settime(tid, TIMER_RELATIVE_C, &its, &ovalue); + if (res != 0) { + printf("flag = TIMER_RELACTIVE_C, timer_settime Failed\n"); + return ((void *)123); + } + + sleep(2); + + if (bFlag != 1) { + printf("Task8:DSQ Operate Error\n"); + return ((void *)123); + } + + res = timer_delete(tid); + if (res != 0) { + printf("timer_delete Failed\n"); + return ((void *)123); + } + + return NULL; +} + +void *Fun9_security (void *arg) +{ +#ifdef __TMS320C6X__ +#define MALLOC_NUM (1) +#define MALLOC_ONCE_SIZE (1024 * 1024) +#else +#define MALLOC_NUM (10) +#define MALLOC_ONCE_SIZE (10 * 1024 * 1024) +#endif + int nDelaytime = 20; + int i = 0; + char *nfp[MALLOC_NUM] = {NULL}; + int mid; + int res; + + for (i = 0; i < MALLOC_NUM; i++) { + nfp[i] = (char*)malloc(1024 * (i + 1)); + if (!nfp[i]) { + printf("Task9:malloc Failed\t i = %d\n", i); + return ((void *)123); + } + } + + pthread_delay(2 * nDelaytime); + + for (i = 0; i < MALLOC_NUM; i++) { + if (nfp[i]) { + free(nfp[i]); + } + } + + char *acoinfoaddr = NULL; + acoinfoaddr = (char *)malloc(MALLOC_ONCE_SIZE); + if (acoinfoaddr == NULL) { + printf("malloc error errno: %d!\n", errno); + return ((void *)123); + } + + res = mpart_create(acoinfoaddr, MALLOC_ONCE_SIZE, &mid); + if (res != 0) { + perror("mpart_create"); + printf("Task9:mpart_create Failed %d\n", res); + return ((void *)123); + } + + for (i = 0; i < MALLOC_NUM; i++) { + nfp[i] = (char*)mpart_alloc(mid, 1024 * (i + 1)); + if (!nfp[i]) { + printf("Task9:mpart_alloc Failed\t i = %d\n", i); + return ((void *)123); + } + } + + pthread_delay(nDelaytime); + + for (i = 0; i < MALLOC_NUM; i++) { + if (nfp[i]) { + res = mpart_free(mid, nfp[i]); + if (res != 0) { + printf("Task9: Failed Release\n"); + return ((void *)123); + } + } + } + + res = mpart_delete(mid); + if (res != 0) { + printf("Task9:mpart_delete Failed\n"); + return ((void *)123); + } + + free(acoinfoaddr); + + return NULL; +} + +void *Fun10_security (void *arg) +{ + int res = -100; + int nDelaytime = 3; + char ctr1[50] = {0}; + char ctr2[50] = {0}; + char cname[50] = {0}; + int findle = 0; + int i = 0; + int nflag = 0; + static int ncount = 0; + + sprintf(cname, "%s%d%s", "abc", (ncount++) % 20, ".txt"); + + if (ncount < 21) { + res = creat(cname, 0666); + if (res != -1) { + close(res); + + } else { + printf("Task10: File create Failed\tres = %d\n", res); + printf("errno = %d\n", errno); + return ((void *)123); + } + } + + for (i = 0; i < 50; i++) { + if (i % 2 == 0) { + ctr1[i] = 'A'; + } else { + ctr1[i] = '5'; + } + } + + ctr1[49] = 0; + findle = open(cname, O_RDWR, 0666); + if (findle == -1) { + printf("Write, Open File Failed\n"); + printf("errno = %d\n", errno); + printf("ncount = %d\n", ncount); + return ((void *)123); + } + + res = write(findle, ctr1, 50); + if (res == -1) { + printf("Write File Failed\n"); + printf("errno = %d\n", errno); + close(findle); + return ((void *)123); + } + + pthread_delay(10 * nDelaytime); + + res = close(findle); + if (res != 0) { + printf("Close File Failed\n"); + return ((void *)123); + } + + findle = open(cname, O_RDWR, 0666); + if (findle == -1) { + printf("Read, Open File Failed\n"); + return ((void *)123); + } + + res = read(findle, ctr2, 50); + if (res == -1) { + printf("Read File Failed\n"); + return ((void *)123); + } + + nflag = 0; + + for (i = 0; i < 50; i++) { + if (ctr1[i] != ctr2[i]) { + nflag = 1; + printf("ctr1 = %c\tctr2 = %c\n", ctr1[i], ctr2[i]); + break; + } + } + + if (nflag == 1) { + printf("Read Data Failed\n"); + return ((void *)123); + } + + res = close(findle); + if (res != 0) { + printf("Close File Failed\n"); + return ((void *)123); + } + + return NULL; +} +/********************************************************************** + * �������ƣ� GJB_ENTRY() + * ���������� �û�������� + * ��������� �� + * ��������� �� + * �� �� ֵ�� �� + * ����˵���� �� + **************************************************************************/ +int main(int argc, char **argv) +{ + int result; + + result = Test_S0102103AQ(); /* security */ + if (result != 0) { + return (-1); + } + + return (0); +} diff --git a/security/gjb_S0102605AQ.c b/security/gjb_S0102605AQ.c new file mode 100644 index 0000000..a595a64 --- /dev/null +++ b/security/gjb_S0102605AQ.c @@ -0,0 +1,128 @@ +/********************************************************************************************************* +** +** GJB ׼Լ +** +** Copyright All Rights Reserved +** +**--------------ļϢ-------------------------------------------------------------------------------- +** +** : S0102605.c +** +** ļ: 2021 1 12 +** +** : ͨǷָ쳣, ֤ϵͳ쳣 +*********************************************************************************************************/ +#ifdef SYLIXOS +#define __SYLIXOS_KERNEL +#endif +#include + +// #include "gjb.h" + +static pthread_attr_t thread_attr1; +static pthread_t thread_id1; + +void *Fun1_security_1 (void *arg) +{ + printf("Task1 is running-1\n"); + + /* + * Ƿָ쳣, ͬƽ̨úʵֲͬ + */ + gjb_os_illeagal_code(); + + printf("Task1 is running-2\n"); + + return NULL; +} + +void pagefails (void) +{ + char *p; + int i; + + // ֻڴ + p = malloc(1024 * 1024); + if (p) { + // õʱͨȱҳ쳣ڴ + memset(p, 'a', 1024 * 1024); + + for (i = 0; i < 1024 * 1024; ++i) { + if (*(p + i) != 'a') { + printf("pagefails test failure."); + } + } + + free(p); + } +} + +/* + * Ƿָ쳣ں + */ +int S0102605 (void) +{ + int status = -100; + int nDelaytime = 2000; + int i = 0; + char a = 255; + char b = 255; + char c; + + // ȱҳ쳣 + pagefails(); + + // , c ϵͳӦó쳣 + c = a + b; + printf("%d\n", c); + + /* + * ʼ߳thread_attr1 + */ + pthread_attr_init(&thread_attr1); + + // thread_attr1.stackaddr = NULL; + // thread_attr1.stacksize = PTHREAD_STACK_MIN * 2; + // thread_attr1.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr1.schedpolicy = SCHED_FIFO; + // thread_attr1.schedparam.sched_priority = 100; + // thread_attr1.name = "Task1"; + + status = pthread_create(&thread_id1, &thread_attr1, Fun1_security_1, NULL); + if (status == 0) { + printf( "Task1 create succesful\n"); + } else { + printf( "Task1 create Failed\n"); + return (-1); + } + + /* + * ʱ2000 TICK + */ + pthread_delay(nDelaytime); + + for (i = 0; i < 10; i++) { + printf("MainFun is running\n"); + } + + return (0); +} +/********************************************************************************************************* + * ƣ GJB_ENTRY() + * û + * + * + * ֵ + * ˵ +*********************************************************************************************************/ +int main(int argc, char **argv) +{ + int result; + + result = S0102605(); + if (result != 0) { + return (-1); + } + + return (0); +} diff --git a/security/gjb_S0102606AQ_addr_nonalign.c b/security/gjb_S0102606AQ_addr_nonalign.c new file mode 100644 index 0000000..0046816 --- /dev/null +++ b/security/gjb_S0102606AQ_addr_nonalign.c @@ -0,0 +1,120 @@ +/********************************************************************************************************* +** +** GJB ��׼���Լ� +** +** Copyright All Rights Reserved +** +**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +** +** �� �� ��: gjb_S0102606AQ_addr_nonalign.c +** +** �ļ���������: 2021 �� 1 �� 12 �� +** +** �� ��: ͨ�������ַ�Ƕ����쳣��֤����ϵͳ���쳣�������� +*********************************************************************************************************/ +#ifdef SYLIXOS +#define __SYLIXOS_KERNEL +#endif +#include +// #include "gjb.h" + +static pthread_attr_t thread_attr3; +static pthread_t thread_id3; +static int exec_flags = 0; + +void ExFunsecurity(int exc, REG_SET* reg) +{ + gjb_os_printk("excFun is running\n"); + exec_flags++; + /* + * �����쳣��, ʹPC�Ĵ���ָ����һ������ָ�� + */ + gjb_os_pc_resume(reg); + + gjb_os_printk("excFun is running over\n"); +} + + +void *Fun3_security_4 (void *arg) +{ + printf("Task3 is running-1\n"); + + /* + * �����ַ�Ƕ����쳣, ��ͬ��ƽ̨�ú�����ʵ�ֲ�ͬ + */ + gjb_os_address_noalign(); + printf("Task3 is running-2\n"); + + return NULL; +} + +//��ַ�����쳣������ں��� +int Test_S0102105AQ3 (void) +{ + int status = -100; + int nDelaytime = 2000; + int i = 0; + EXC_HANDLER old; + + /* + * ע��һ���û��쳣�������� + */ + old = exception_handler_set((EXC_HANDLER)ExFunsecurity); + + printf("exception_handler_set status = %x\n", (unsigned int)(long)old); + + //�������� + pthread_attr_init(&thread_attr3); + + thread_attr3.stackaddr = NULL; + thread_attr3.stacksize = PTHREAD_STACK_MIN * 2; + thread_attr3.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr3.schedpolicy = SCHED_FIFO; + thread_attr3.schedparam.sched_priority = 100; + thread_attr3.name = "Task3"; + + status = pthread_create(&thread_id3, &thread_attr3, Fun3_security_4, NULL); + if (status == 0) { + printf( "Task3 Create succesful\n"); + + } else { + printf( "Task3 Create Failed\n"); + return (-1); + } + + pthread_delay(nDelaytime); // 2000��ticks + + for (i = 0; i < 10; i++) { + printf("MainFun is running\n"); + } + + /* + * �ָ��û��쳣�������� + */ + exception_handler_set(NULL); // ֻ�ǻָ�һ��֮ǰ�Ļ��� + + if (!exec_flags) { + return (-1); + } + + return (0); +} +/********************************************************************** + * �������ƣ� GJB_ENTRY() + * ���������� �û�������� + * ��������� �� + * ��������� �� + * �� �� ֵ�� �� + * ����˵���� �� + **************************************************************************/ +int main(int argc, char **argv) +{ + int result; + + result = Test_S0102105AQ3(); /* exec test */ + if (result != 0) { + return (-1); + } + + return (0); +} diff --git a/security/gjb_S0102606AQ_addr_overflow.c b/security/gjb_S0102606AQ_addr_overflow.c new file mode 100644 index 0000000..da5100d --- /dev/null +++ b/security/gjb_S0102606AQ_addr_overflow.c @@ -0,0 +1,115 @@ +/********************************************************************************************************* +** +** GJB ׼Լ +** +** Copyright All Rights Reserved +** +**--------------ļϢ-------------------------------------------------------------------------------- +** +** : gjb_S0102606AQ_addr_overflow.c +** +** ļ: 2021 1 12 +** +** : ַ쳣, ֤ϵͳ쳣 +*********************************************************************************************************/ +#ifdef SYLIXOS +#define __SYLIXOS_KERNEL +#endif +#include +// #include "gjb.h" + +static int exec_flags_2 = 0; +static pthread_attr_t thread_attr2; +static pthread_t thread_id2; + +void ExFunsecurity_1(int exc, REG_SET* reg) +{ + printf("excFun is running\n"); + exec_flags_2++; + gjb_os_pc_resume(reg); + + printf("excFun is running over\n"); +} + +void *Fun2security_4() +{ + printf("Task2 is running-1\n"); + + /* + * ַ쳣, ͬƽ̨úʵֲͬ + */ + gjb_array_overflow(); + printf("Task2 is running-2\n"); + + return (NULL); +} + +//ַԽ쳣ں +int Test_S0102105AQ2 (void) +{ + int status = -100; + int nDelaytime = 2000; + int i = 0; + EXC_HANDLER old; + + /* + * עһû쳣 + */ + old = exception_handler_set((EXC_HANDLER)ExFunsecurity_1); + + printf("exception_handler_set status = %x\n", (unsigned int)(long)old); + + pthread_attr_init(&thread_attr2); + + thread_attr2.stackaddr = NULL; + thread_attr2.stacksize = PTHREAD_STACK_MIN*2; + thread_attr2.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr2.schedpolicy = SCHED_FIFO; + thread_attr2.schedparam.sched_priority = 100; + thread_attr2.name = "Task2"; + + /* + * һ + */ + status = pthread_create(&thread_id2, &thread_attr2, Fun2security_4, NULL); + if (status == 0) { + printf( "Task2 Create seccesful\n"); + + } else { + printf( "Task2 Create Failed\n"); + return (-1); + } + + pthread_delay(nDelaytime); // 2000ticks + + for (i = 0; i < 10; i++) { + printf("MainFun is running\n"); + } + + exception_handler_set(NULL); // ֻǻָһ֮ǰĻ + + if (!exec_flags_2) { + return (-1); + } + + return (0); +} +/********************************************************************** + * ƣ GJB_ENTRY() + * û + * + * + * ֵ + * ˵ + **************************************************************************/ +int main(int argc, char **argv) +{ + int result; + + result = Test_S0102105AQ2(); /* exec test */ + if (result != 0) { + return (-1); + } + + return (0); +} diff --git a/security/gjb_S0102606AQ_illgel_code.c b/security/gjb_S0102606AQ_illgel_code.c new file mode 100644 index 0000000..62e6c48 --- /dev/null +++ b/security/gjb_S0102606AQ_illgel_code.c @@ -0,0 +1,114 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0102606AQ_illgel_code.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 通过构造非法指令异常, 验证系统异常处理功能 +*********************************************************************************************************/ +#ifdef SYLIXOS +#define __SYLIXOS_KERNEL +#endif +#include +// #include "gjb.h" + +static int exec_flags_1 = 0; +static pthread_attr_t thread_attr1; +static pthread_t thread_id1; + +void *Fun1_security_5 (void *arg) +{ + printf("Task1 is running-1\n"); + + /* + * 构造非法指令异常, 不同的平台该函数的实现不同 + */ + gjb_os_illeagal_code(); + + printf("Task1 is running-2\n"); + + return NULL; +} + +void ExFunxxxx(int exc, REG_SET* reg) +{ + printf("excFun is running\n"); + exec_flags_1++; + gjb_os_pc_resume(reg); + + printf("excFun is running over\n"); +} + +//非法指令异常测试入口函数 +int Test_S0102105AQ1 (void) +{ + int status = -100; + int nDelaytime = 2000; + int i = 0; + EXC_HANDLER old_exc; + + /* + * 注册一个用户异常处理函数 + */ + old_exc = exception_handler_set((EXC_HANDLER)ExFunxxxx); + + printf("exception_handler_set status = %x\n", (unsigned int)(long)old_exc); + + //创建任务 + pthread_attr_init(&thread_attr1); + + thread_attr1.stackaddr = NULL; + thread_attr1.stacksize = PTHREAD_STACK_MIN * 2; + thread_attr1.inheritsched = PTHREAD_EXPLICIT_SCHED; + thread_attr1.schedpolicy = SCHED_FIFO; + thread_attr1.schedparam.sched_priority = 100; + thread_attr1.name = "Task1"; + + status = pthread_create(&thread_id1, &thread_attr1, Fun1_security_5, NULL); + if (status == 0) { + printf( "Task1 create succesful\n"); + + } else { + printf( "Task1 create Failed\n"); + return (-1); + } + + pthread_delay(nDelaytime); //2000个ticks + + for (i = 0; i < 10; i++) { + printf("MainFun is running\n"); + } + + exception_handler_set(NULL); // 只是恢复一下之前的环境 + + if (!exec_flags_1) { + return (-1); + } + + return (0); +} +/********************************************************************** + * 函数名称: GJB_ENTRY() + * 功能描述: 用户程序入口 + * 输入参数: 无 + * 输出参数: 无 + * 返 回 值: 无 + * 其它说明: 无 + **************************************************************************/ +int main(int argc, char **argv) +{ + int result; + + result = Test_S0102105AQ1(); /* exec test */ + if (result != 0) { + return (-1); + } + + return (0); +} -- Gitee From 32cd5083d850b554fa42e9b3897383fe58a5ed50 Mon Sep 17 00:00:00 2001 From: WJ Date: Tue, 14 Nov 2023 16:25:39 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Network/gjb_S0101802GN.c | 36 +++++++++--------- Network/gjb_S0101803GN_3.c | 30 +++++++-------- Network/gjb_S0101803GN_5.c | 2 +- Network/gjb_S0101804GN_1.c | 36 ++++++++++++++++++ Network/gjb_S0101804GN_2.c | 39 +++++++++++++++++++ Network/gjb_S0101804GN_3.c | 77 +++++++++++++++++++++----------------- Network/gjb_S0101804GN_4.c | 75 +++++++++++++++++++------------------ Network/gjb_S0101804GN_5.c | 23 ++++++------ Network/gjb_S0101805GN.c | 33 ++++++++++++++++ Network/tcp_clint.c | 44 ++++++++++++++++++++++ Network/udp_client.c | 68 +++++++++++++++++++++++++++++++++ 11 files changed, 346 insertions(+), 117 deletions(-) create mode 100644 Network/gjb_S0101804GN_1.c create mode 100644 Network/gjb_S0101804GN_2.c create mode 100644 Network/gjb_S0101805GN.c create mode 100644 Network/tcp_clint.c create mode 100644 Network/udp_client.c diff --git a/Network/gjb_S0101802GN.c b/Network/gjb_S0101802GN.c index 0392c16..e955851 100644 --- a/Network/gjb_S0101802GN.c +++ b/Network/gjb_S0101802GN.c @@ -1,17 +1,17 @@ /********************************************************************************************************* ** -** GJB ׼Լ +** GJB 标准测试集 ** ** Copyright All Rights Reserved ** -**--------------ļϢ-------------------------------------------------------------------------------- +**--------------文件信息-------------------------------------------------------------------------------- ** -** : gjb_S0101802GN.c +** 文 件 名: gjb_S0101802GN.c ** -** ļ: 2021 1 12 +** 文件创建日期: 2021 年 1 月 12 日 ** -** : ׼׽ֱ̽ӿڹܲԡñ׼׽ֽӿڱд -** ͻͷʵ繦ܡ +** 描 述: 标准套接字编程接口功能测试。利用标准套接字接口编写 +** 客户和服务器端网络程序,实现网络功能。 *********************************************************************************************************/ #include #include @@ -67,8 +67,8 @@ void server_test() } /* - * socket ѡ SO_REUSEADDR - ˿ڸüsetsockopt + * 设置 socket 选项 SO_REUSEADDR + 端口复用技术(setsockopt()) */ ret1 = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); if(ret1 == -1) { @@ -97,10 +97,10 @@ void server_test() memset(&srvr, 0, sizeof(srvr)); /* struct sockaddr_in { - short sa_family;//ַ壬2ֽ - unsigned short int sin_port;//˿ںţ2ֽ - struct in_addr sin_addr;//IPַ4ֽ - unsigned char sin_zero[];//0Աstruct sockaddrͬС8ֽ + short sa_family;//地址族,2字节 + unsigned short int sin_port;//端口号,2字节 + struct in_addr sin_addr;//IP地址,4字节 + unsigned char sin_zero[];//填充0以保持与struct sockaddr同样大小,8字节 } */ srvr.sin_family = AF_INET; @@ -120,7 +120,7 @@ struct sockaddr_in { } /* - * socket + * 监听 socket */ ret3 = listen(s, 10); if (ret3 == -1) { @@ -190,7 +190,7 @@ void client_test() struct sockaddr_in srvr; /* - * socket TCP + * 创建 socket TCP */ s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (s == -1) { @@ -211,7 +211,7 @@ void client_test() srvr.sin_addr.s_addr = inet_addr(IP_ADDR); /* - * IP ַ IP_ADDR ˿ 8080 + * 绑定 IP 地址 IP_ADDR 端口 8080 */ ret2 = bind(s, (struct sockaddr *)&srvr, sizeof(srvr)); if (ret2 == -1) { @@ -232,12 +232,12 @@ void client_test() addrLen = sizeof (addr); /* - * Ӹʱ֤Ѿ accept. + * 这里加个延时保证服务器程序已经 accept. */ sleep(1); /* - * IP_ADDR ˿ 8081 + * 连接 IP_ADDR 端口 8081 */ newSock = connect(s, (struct sockaddr *)&addr, addrLen); if (newSock == -1) { @@ -252,7 +252,7 @@ void client_test() } /* - * + * 发送数据 */ ret1 = send(s, sendbuf, buf_len1, 0); if(ret1 == -1) { diff --git a/Network/gjb_S0101803GN_3.c b/Network/gjb_S0101803GN_3.c index 238f73f..ee2c746 100644 --- a/Network/gjb_S0101803GN_3.c +++ b/Network/gjb_S0101803GN_3.c @@ -1,23 +1,23 @@ /********************************************************************************************************* ** -** GJB ׼Լ +** GJB 标准测试集 ** ** Copyright All Rights Reserved ** -**--------------ļϢ-------------------------------------------------------------------------------- +**--------------文件信息-------------------------------------------------------------------------------- ** -** : gjb_S0101803GN_3.c +** 文 件 名: gjb_S0101803GN_3.c ** -** ļ: 2021 1 12 +** 文件创建日期: 2021 年 1 月 12 日 ** -** : Эֹ֧ܲԡƲ룬ͨ -** TCPUDPIPICMPARPЭӿڣ֤Э -** ȷԣͬʱЭߣȷԡ +** 描 述: 基本网络协议支持功能测试。编制测试用例代码,通过调 +** 用TCP、UDP、IP、ICMP和ARP协议接口,验证各项协议正 +** 确性,同时利用网络协议分析工具,分析网络包的正确性。 ** ICMP ** -** ping ԶICMPв +** ping 命令可以对ICMP进行测试 ** -** !!!!ʹ wireshark ץ߽з鿴 +** !!!!使用 wireshark 抓包工具进行分析查看 *********************************************************************************************************/ #include #include @@ -46,13 +46,13 @@ int main(int argc, char **argv) char buf[] = { "hello world" }; struct sockaddr_in addr; int fd ; - if((socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0){ + if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0){ perror(strerror(errno)); printf("child socket error!\n"); return -1; } - //socket + //socket建立测试 // if (fd < 0) { // printf("child socket error!\n"); // return -1; @@ -60,18 +60,18 @@ int main(int argc, char **argv) // // return (GJB_PRI_FAIL()); // } - //սշַ + //清空接收服务器地址 memset(&addr, 0, sizeof(addr)); - //ýն˷ + //配置接收端服务器 addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr(IP_ADDR); addr.sin_port = htons(8080); - // + //发送数据 sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, sizeof(addr)); - //رsocket + //关闭socket close(fd); return 0; // return (GJB_PRI_PASS()); diff --git a/Network/gjb_S0101803GN_5.c b/Network/gjb_S0101803GN_5.c index 0ed4256..e2d711d 100644 --- a/Network/gjb_S0101803GN_5.c +++ b/Network/gjb_S0101803GN_5.c @@ -46,7 +46,7 @@ int main(int argc, char **argv) struct sockaddr_in addr; //socket建立测试 - if ((socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) < 0) { + if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) < 0) { perror(strerror(errno)); fprintf(stdout, "create socket error\n"); exit(0); diff --git a/Network/gjb_S0101804GN_1.c b/Network/gjb_S0101804GN_1.c new file mode 100644 index 0000000..9292f3b --- /dev/null +++ b/Network/gjb_S0101804GN_1.c @@ -0,0 +1,36 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0101804GN_1.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 协议栈信息统计功能测试。编制测试用例代码,调用恰当 +** 接口函数获取协议栈统计信息--mbuf。 +*********************************************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +int main(int argc, char **argv) +{ + printf("display mbuf information:\n"); + mbuf_show(); + return (0); +} diff --git a/Network/gjb_S0101804GN_2.c b/Network/gjb_S0101804GN_2.c new file mode 100644 index 0000000..5971ab3 --- /dev/null +++ b/Network/gjb_S0101804GN_2.c @@ -0,0 +1,39 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0101804GN_2.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 协议栈信息统计功能测试。编制测试用例代码,调用恰当 +** 接口函数获取协议栈统计信息-- +** +** ifconfig。 +*********************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +int main(int argc, char **argv) +{ + printf("display network interface information:\n"); + // if_show(); + system("ifconfig"); + + return (0); +} diff --git a/Network/gjb_S0101804GN_3.c b/Network/gjb_S0101804GN_3.c index 3ad7dc5..d107d7a 100644 --- a/Network/gjb_S0101804GN_3.c +++ b/Network/gjb_S0101804GN_3.c @@ -28,43 +28,50 @@ #include +int main(int argc, char **argv) +{ + printf("display routes information:\n"); + // routes_show(); + system("netstat -r"); + return (0); +} -int main() { - // 创建套接字并绑定到任意接口 - int sock = socket(AF_INET, SOCK_STREAM, 0); - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl(INADDR_ANY); - addr.sin_port = htons(80); // 假设使用HTTP协议监听所有接口 - if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - perror("bind failed"); - exit(1); - } +// int main() { +// // 创建套接字并绑定到任意接口 +// int sock = socket(AF_INET, SOCK_STREAM, 0); +// struct sockaddr_in addr; +// addr.sin_family = AF_INET; +// addr.sin_addr.s_addr = htonl(INADDR_ANY); +// addr.sin_port = htons(80); // 假设使用HTTP协议监听所有接口 +// if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { +// perror("bind failed"); +// exit(1); +// } - // 获取网络接口信息 - struct ifaddrs *ifaddr, *ifa; - getifaddrs(&ifaddr); +// // 获取网络接口信息 +// struct ifaddrs *ifaddr, *ifa; +// getifaddrs(&ifaddr); - // 遍历接口并输出路由信息 - for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { - if (ifa->ifa_addr->sa_family == AF_INET) { // 检查是否为IPv4接口 - struct sockaddr_in *addrp = (struct sockaddr_in *)ifa->ifa_addr; - char ip[INET_ADDRSTRLEN]; - if (inet_ntop(AF_INET, &addrp->sin_addr, ip, sizeof(ip))) { // 将IP地址转换为字符串 - printf("Interface %s has IP address %s\n", ifa->ifa_name, ip); - struct sockaddr_in gw; - if (getnameinfo((struct sockaddr *)&ifa->ifa_dstaddr, ifa->ifa_dstaddr->sa_family == AF_INET ? sizeof(gw) : 0, NULL, 0, NULL, 0, NI_NUMERICHOST ) == 0) { // 获取网关地址并输出 - printf("Interface %s has route to %s\n", ifa->ifa_name, ifa->ifa_dstaddr->sa_family == AF_INET ? inet_ntoa(gw.sin_addr) : "any"); - } else { - // printf("Failed to get route to %s\n", ifa->ifa_dstaddr->sa_family == AF_INET ? inet_ntoa(*(struct in_addr *)&ifa->ifa_dstaddr->sa_addr) : "any"); - } - } - } - } +// // 遍历接口并输出路由信息 +// for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { +// if (ifa->ifa_addr->sa_family == AF_INET) { // 检查是否为IPv4接口 +// struct sockaddr_in *addrp = (struct sockaddr_in *)ifa->ifa_addr; +// char ip[INET_ADDRSTRLEN]; +// if (inet_ntop(AF_INET, &addrp->sin_addr, ip, sizeof(ip))) { // 将IP地址转换为字符串 +// printf("Interface %s has IP address %s\n", ifa->ifa_name, ip); +// struct sockaddr_in gw; +// if (getnameinfo((struct sockaddr *)&ifa->ifa_dstaddr, ifa->ifa_dstaddr->sa_family == AF_INET ? sizeof(gw) : 0, NULL, 0, NULL, 0, NI_NUMERICHOST ) == 0) { // 获取网关地址并输出 +// printf("Interface %s has route to %s\n", ifa->ifa_name, ifa->ifa_dstaddr->sa_family == AF_INET ? inet_ntoa(gw.sin_addr) : "any"); +// } else { +// // printf("Failed to get route to %s\n", ifa->ifa_dstaddr->sa_family == AF_INET ? inet_ntoa(*(struct in_addr *)&ifa->ifa_dstaddr->sa_addr) : "any"); +// } +// } +// } +// } - // 释放接口信息结构体的内存 - freeifaddrs(ifaddr); // 只需要调用一次freeifaddrs函数,因为它已经将所有接口信息释放了内 - close(sock); // 关闭套接字,释放资源。在C语言中,我们通常在完成操作后关闭资源,以防止资源泄漏。这里我们 - return 0; // 程序正常结束并返回0表示成功。如果在处理过程中发生错误或异常情况,需要返回一 -} +// // 释放接口信息结构体的内存 +// freeifaddrs(ifaddr); // 只需要调用一次freeifaddrs函数,因为它已经将所有接口信息释放了内 +// close(sock); // 关闭套接字,释放资源。在C语言中,我们通常在完成操作后关闭资源,以防止资源泄漏。这里我们 +// return 0; // 程序正常结束并返回0表示成功。如果在处理过程中发生错误或异常情况,需要返回一 +// } diff --git a/Network/gjb_S0101804GN_4.c b/Network/gjb_S0101804GN_4.c index e0df7eb..be807a6 100644 --- a/Network/gjb_S0101804GN_4.c +++ b/Network/gjb_S0101804GN_4.c @@ -1,20 +1,20 @@ /********************************************************************************************************* ** -** GJB ��׼���Լ� +** GJB 标准测试集 ** ** Copyright All Rights Reserved ** -**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +**--------------文件信息-------------------------------------------------------------------------------- ** -** �� �� ��: gjb_S0101804GN_4.c +** 文 件 名: gjb_S0101804GN_4.c ** -** �ļ���������: 2021 �� 1 �� 12 �� +** 文件创建日期: 2021 年 1 月 12 日 ** -** �� ��: Э��ջ��Ϣͳ�ƹ��ܲ��ԡ����Ʋ����������룬����ǡ�� -** �ӿں�����ȡЭ��ջͳ����Ϣ-- +** 描 述: 协议栈信息统计功能测试。编制测试用例代码,调用恰当 +** 接口函数获取协议栈统计信息-- ** -** tcp�� -** ��������Ҫʹ�� tools/USR-TCP232-Test.exe ���߽�����ϲ��� +** tcp。 +** 该用例需要使用 tools/USR-TCP232-Test.exe 工具进行配合测试 *********************************************************************************************************/ #include #include @@ -36,13 +36,13 @@ #ifndef TEST_IP -#define TEST_IP "192.168.6.128" +#define TEST_IP "192.168.80.14" #endif #ifndef TEST_PC_IP -#define TEST_PC_IP "192.168.6.128" +#define TEST_PC_IP "192.168.80.14" #endif -int tcp_show(); +// int tcp_show(); int main() { @@ -90,7 +90,7 @@ int main() memset(&srvr, 0, sizeof(srvr)); srvr.sin_family = AF_INET; - srvr.sin_port = htons(8080); + srvr.sin_port = htons(9999); srvr.sin_addr.s_addr =inet_addr(TEST_IP); ret2 = bind(s, (struct sockaddr *)&srvr, sizeof(srvr)); @@ -142,32 +142,33 @@ int main() printf("display TCP information:\n"); // tcp_show(); + system("netstat -t"); return (0); } -int tcp_show(){ - int sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock == -1) { - perror("socket"); - exit(1); - } - - struct tcp_info tcpinfo; - socklen_t len = sizeof(tcpinfo); - if (getsockopt(sock, IPPROTO_TCP, TCP_INFO, &tcpinfo, &len) == -1) { - perror("getsockopt"); - close(sock); - exit(1); - } - - printf("TCP Statistics:\n"); - printf(" RTT: %u ms\n", tcpinfo.tcpi_rtt); - printf(" RTO: %u ms\n", tcpinfo.tcpi_rto); - printf(" Send buffer size: %u bytes\n", tcpinfo.tcpi_rcv_mss); - printf(" Receive buffer size: %u bytes\n", tcpinfo.tcpi_rcv_mss); - printf(" Number of retransmissions: %u\n", tcpinfo.tcpi_total_retrans); - - close(sock); - return 0; -} +// int tcp_show(){ +// int sock = socket(AF_INET, SOCK_STREAM, 0); +// if (sock == -1) { +// perror("socket"); +// exit(1); +// } + +// struct tcp_info tcpinfo; +// socklen_t len = sizeof(tcpinfo); +// if (getsockopt(sock, IPPROTO_TCP, TCP_INFO, &tcpinfo, &len) == -1) { +// perror("getsockopt"); +// close(sock); +// exit(1); +// } + +// printf("TCP Statistics:\n"); +// printf(" RTT: %u ms\n", tcpinfo.tcpi_rtt); +// printf(" RTO: %u ms\n", tcpinfo.tcpi_rto); +// printf(" Send buffer size: %u bytes\n", tcpinfo.tcpi_rcv_mss); +// printf(" Receive buffer size: %u bytes\n", tcpinfo.tcpi_rcv_mss); +// printf(" Number of retransmissions: %u\n", tcpinfo.tcpi_total_retrans); + +// close(sock); +// return 0; +// } diff --git a/Network/gjb_S0101804GN_5.c b/Network/gjb_S0101804GN_5.c index 003a830..e3caa77 100644 --- a/Network/gjb_S0101804GN_5.c +++ b/Network/gjb_S0101804GN_5.c @@ -1,23 +1,23 @@ /********************************************************************************************************* ** -** GJB ��׼���Լ� +** GJB 标准测试集 ** ** Copyright All Rights Reserved ** -**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +**--------------文件信息-------------------------------------------------------------------------------- ** -** �� �� ��: gjb_S0101804GN_5.c +** 文 件 名: gjb_S0101804GN_5.c ** -** �ļ���������: 2021 �� 1 �� 12 �� +** 文件创建日期: 2021 年 1 月 12 日 ** -** �� ��: Э��ջ��Ϣͳ�ƹ��ܲ��ԡ����Ʋ����������룬����ǡ�� -** �ӿں�����ȡЭ��ջͳ����Ϣ-- +** 描 述: 协议栈信息统计功能测试。编制测试用例代码,调用恰当 +** 接口函数获取协议栈统计信息-- ** ** udp. ** -** ��������Ҫʹ�� tools/USR-TCP232-Test.exe ���߽�����ϲ��� +** 该用例需要使用 tools/USR-TCP232-Test.exe 工具进行配合测试 ** -** TEST_IP ��Ҫ����Ϊ���ǵ� PC ip +** TEST_IP 需要设置为真是的 PC ip *********************************************************************************************************/ #include @@ -33,7 +33,7 @@ #include #include #ifndef TEST_IP -#define TEST_IP "192.168.6.128" +#define TEST_IP "192.168.80.14" #endif int main() @@ -49,10 +49,10 @@ int main() memset(recvBuf, 0, 64); srvr.sin_family = AF_INET; srvr.sin_addr.s_addr = htonl(INADDR_ANY); - srvr.sin_port = htons(8080); + srvr.sin_port = htons(9999); clientAddr.sin_family = AF_INET; - clientAddr.sin_port = htons(8080); + clientAddr.sin_port = htons(9999); clientAddr.sin_addr.s_addr = inet_addr(TEST_IP); s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -95,6 +95,7 @@ int main() } printf("display UDP information:\n"); + system("netstat -u"); // udp_show(); } diff --git a/Network/gjb_S0101805GN.c b/Network/gjb_S0101805GN.c new file mode 100644 index 0000000..fc19385 --- /dev/null +++ b/Network/gjb_S0101805GN.c @@ -0,0 +1,33 @@ +/********************************************************************************************************* +** +** GJB 标准测试集 +** +** Copyright All Rights Reserved +** +**--------------文件信息-------------------------------------------------------------------------------- +** +** 文 件 名: gjb_S0101805GN.c +** +** 文件创建日期: 2021 年 1 月 12 日 +** +** 描 述: 应用层协议功能测试。编制测试用例代码,调用HTTP、TFTP、 +** FTP、TELNET应用层协议,实现与其他网络设备进行通信。 +** +** 该用例需要借助外部工具进行测试: +** +** tftp: SylixOS 平台可以通过 RealEvo-IDE 启动 tftp 服务器, +** SylixOS端通过 tftp 命令进行传输文件 +** +** ftp: 使用 filezilla 工具链接 SylixOS 平台验证 FTP 功能 +** +** telnet: 使用telnet 链接工具(例如: SecureCRT) 链接 SylixOS 平台 +** +** http: SylixOS平台 goahead 工具在 SylixOS 平台创建 web 服务器, +** 然后通过 PC 机网页访问该 web 服务器 +*********************************************************************************************************/ + + +int main(int argc, char **argv) +{ + return (0); +} diff --git a/Network/tcp_clint.c b/Network/tcp_clint.c new file mode 100644 index 0000000..e1017a8 --- /dev/null +++ b/Network/tcp_clint.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + int sock; + struct sockaddr_in serv_addr; + char message[30]; + int str_len; + + + sock = socket(AF_INET, SOCK_STREAM, 0); + if(sock == -1) + printf("socket() error"); + + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family=AF_INET; + serv_addr.sin_addr.s_addr = inet_addr("192.168.6.128"); + serv_addr.sin_port = htons(atoi("9999")); + + if(connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1) + printf("connect() error!"); + char *sendData = "hello, I am clint!\n"; + send(sock, sendData, strlen(sendData), 0); + str_len = read(sock, message, sizeof(message) - 1); + if(str_len == -1) + printf("read() error!"); + + printf("Message from server: %s \n", message); + close(sock); + return 0; +} diff --git a/Network/udp_client.c b/Network/udp_client.c new file mode 100644 index 0000000..c971389 --- /dev/null +++ b/Network/udp_client.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define DEST_PORT 9999 +#define DSET_IP_ADDRESS "192.168.80.14" + + +int main() +{ + /* socket文件描述符 */ + int sock_fd; + + /* 建立udp socket */ + sock_fd = socket(AF_INET, SOCK_DGRAM, 0); + if(sock_fd < 0) + { + perror("socket"); + exit(1); + } + + /* 设置address */ + struct sockaddr_in addr_serv; + int len; + memset(&addr_serv, 0, sizeof(addr_serv)); + addr_serv.sin_family = AF_INET; + addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS); + addr_serv.sin_port = htons(DEST_PORT); + len = sizeof(addr_serv); + + + int send_num; + int recv_num; + char send_buf[20] = "hey, who are you?"; + char recv_buf[20]; + + printf("client send: %s\n", send_buf); + + send_num = sendto(sock_fd, send_buf, strlen(send_buf), 0, (struct sockaddr *)&addr_serv, len); + + if(send_num < 0) + { + perror("sendto error:"); + exit(1); + } + + recv_num = recvfrom(sock_fd, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&addr_serv, (socklen_t *)&len); + + if(recv_num < 0) + { + perror("recvfrom error:"); + exit(1); + } + + recv_buf[recv_num] = '\0'; + printf("client receive %d bytes: %s\n", recv_num, recv_buf); + + close(sock_fd); + + return 0; +} \ No newline at end of file -- Gitee From d1a66aa042504131c62f9666d6b46a398a4e19f9 Mon Sep 17 00:00:00 2001 From: WJ Date: Wed, 15 Nov 2023 16:43:42 +0800 Subject: [PATCH 4/6] network chenge systen fun & security top 5 --- Network/gjb_S0101804GN_1.c | 3 ++- Network/gjb_S0101804GN_3.c | 2 +- Network/gjb_S0101804GN_4.c | 2 +- Network/gjb_S0101804GN_5.c | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Network/gjb_S0101804GN_1.c b/Network/gjb_S0101804GN_1.c index 9292f3b..52a0257 100644 --- a/Network/gjb_S0101804GN_1.c +++ b/Network/gjb_S0101804GN_1.c @@ -31,6 +31,7 @@ int main(int argc, char **argv) { printf("display mbuf information:\n"); - mbuf_show(); + // mbuf_show(); + system("netstat"); return (0); } diff --git a/Network/gjb_S0101804GN_3.c b/Network/gjb_S0101804GN_3.c index d107d7a..b374dc6 100644 --- a/Network/gjb_S0101804GN_3.c +++ b/Network/gjb_S0101804GN_3.c @@ -32,7 +32,7 @@ int main(int argc, char **argv) { printf("display routes information:\n"); // routes_show(); - system("netstat -r"); + system("route"); return (0); } diff --git a/Network/gjb_S0101804GN_4.c b/Network/gjb_S0101804GN_4.c index be807a6..0ad69be 100644 --- a/Network/gjb_S0101804GN_4.c +++ b/Network/gjb_S0101804GN_4.c @@ -142,7 +142,7 @@ int main() printf("display TCP information:\n"); // tcp_show(); - system("netstat -t"); + system("netstat -t -na"); return (0); } diff --git a/Network/gjb_S0101804GN_5.c b/Network/gjb_S0101804GN_5.c index e3caa77..e9a5c99 100644 --- a/Network/gjb_S0101804GN_5.c +++ b/Network/gjb_S0101804GN_5.c @@ -95,7 +95,7 @@ int main() } printf("display UDP information:\n"); - system("netstat -u"); + system("netstat -u -na"); // udp_show(); } -- Gitee From 70626b0ade35d794091fa01c28e4c8e1aac8cfa1 Mon Sep 17 00:00:00 2001 From: WJ Date: Wed, 15 Nov 2023 16:44:08 +0800 Subject: [PATCH 5/6] network chenge systen fun & security top 5 --- security/README | 3 + security/gjb_S0101611AQ.c | 20 ++- security/gjb_S0102601AQ_deadlock.c | 164 ++++++++++-------- security/gjb_S0102602AQ.c | 80 ++++++--- security/gjb_S0102603AQ.c | 264 +++++++++++++++++++++++++++-- security/gjb_S0102605AQ.c | 75 +++++--- 6 files changed, 479 insertions(+), 127 deletions(-) diff --git a/security/README b/security/README index 4880371..9c69680 100644 --- a/security/README +++ b/security/README @@ -1,5 +1,8 @@ ###安全管理 +## 1611 :gcc gjb_S0101611AQ.c -o 1611 -lpthread +## 2603 gcc gjb_S0102603AQ.c -o 2603 -lpthread -lrt + ## 2:pthread_delay报错,gjb_os_binding 改为bind 剩下glibc错误 ##3 TIMER_RELATIVE_C 报错找不到解决办法,O_RDWR未定义 ##5 gjb_os_illeagal_code找不到。pthread_delay报错 \ No newline at end of file diff --git a/security/gjb_S0101611AQ.c b/security/gjb_S0101611AQ.c index b9b6d77..599763e 100644 --- a/security/gjb_S0101611AQ.c +++ b/security/gjb_S0101611AQ.c @@ -17,6 +17,11 @@ * * 该程序编译完成功之后, 可以修改 /etc/startup.sh 的内容, 将该程序加入启动脚本 */ +// #define __USE_GNU /* 这句要放#include 前面 */ +#define _GNU_SOURCE +#include + + #include #include #include @@ -24,6 +29,16 @@ #include #include +#include +#include +#include + +#include + + + + + #define FILESIZE (500 * 1024 * 1024) #define FILEA "/apps/testsuite_ml/result.log" #define FILEB "/apps/testsuite_ml/abc12.txt" @@ -31,7 +46,10 @@ #define FILED "/media/sdcard3/d.txt" #define FILEE "/media/hdd0/e.txt" #define FILEF "/media/hdd0/f.txt" +#define ERROR_NONE 0 + +typedef void *PVOID; /******************************************************************************************************* ** 函数名称: tTest1 ** 功能描述: eMMC测试 @@ -336,7 +354,7 @@ PVOID tTest3_fs_611(PVOID pvArg) ** 输 入 : ** 输 出 : *******************************************************************************************************/ -int gjb_S0101611AQ(int argc, char **argv) +int main(int argc, char **argv) { pthread_t hThreadId1, hThreadId2,hThreadId3; pthread_attr_t threadattr1, threadattr2,threadattr3; diff --git a/security/gjb_S0102601AQ_deadlock.c b/security/gjb_S0102601AQ_deadlock.c index b2769c9..24a2289 100644 --- a/security/gjb_S0102601AQ_deadlock.c +++ b/security/gjb_S0102601AQ_deadlock.c @@ -1,22 +1,27 @@ /********************************************************************************************************* ** -** GJB ׼Լ +** GJB 标准测试集 ** ** Copyright All Rights Reserved ** -**--------------ļϢ-------------------------------------------------------------------------------- +**--------------文件信息-------------------------------------------------------------------------------- ** -** : gjb_S0102601AQ_deadlock.c +** 文 件 名: gjb_S0102601AQ_deadlock.c ** -** ļ: 2021 1 12 +** 文件创建日期: 2021 年 1 月 12 日 ** -** : , sylixOS ͨ tp 鿴״̬ +** 描 述: 构造死锁功能, sylixOS 可通过 tp 命令查看死锁状态 *********************************************************************************************************/ #ifdef SYLIXOS #define __SYLIXOS_KERNEL #endif -#include "gjb.h" +#include +#include +#include +#include +#include +// #include "gjb.h" sem_t Csem; sem_t Dsem; @@ -26,54 +31,56 @@ static pthread_t thread_id4; static pthread_attr_t thread_attr3; static pthread_attr_t thread_attr4; +int pthread_delay(int ticks); + void *Fun3_security_1 (void *arg) { int res = -100; int nDelaytime = 800; - GJB_PRT_INFO("Task3 is running\n"); + printf("Task3 is running\n"); - //ȡCֵźԴ + //获取C二值信号量资源 res = sem_wait(&Csem); if (res == 0) { - GJB_PRT_INFO("Task3 Get Csem Succesful\n"); + printf("Task3 Get Csem Succesful\n"); } else { - GJB_PRT_ERROR_INFO("Task3 Get Csem Failed\n"); + printf("Task3 Get Csem Failed\n"); } - //ʱ + //延时 sleep(2); - //ȡDźԴ + //获取D信号量资源 res = -100; res = sem_wait(&Dsem); if (res == 0) { - GJB_PRT_INFO("Task3 Get Dsem Succesful\n"); + printf("Task3 Get Dsem Succesful\n"); } else { - GJB_PRT_ERROR_INFO("Task3 Get Dsem Failed\n"); + printf("Task3 Get Dsem Failed\n"); } - //ʱ - pthread_delay(nDelaytime); //1000ticks + //延时 + pthread_delay(nDelaytime); //1000个ticks res = -100; res = sem_post(&Csem); if (res == 0) { - GJB_PRT_INFO("Task3 Release Csem succesful\n"); + printf("Task3 Release Csem succesful\n"); } else { - GJB_PRT_ERROR_INFO("Task3 Release Csem Failed\n"); + printf("Task3 Release Csem Failed\n"); } - //ʱ - pthread_delay(nDelaytime); //1000ticks + //延时 + pthread_delay(nDelaytime); //1000个ticks res = -100; res = sem_post(&Dsem); if (res == 0) { - GJB_PRT_INFO("Task3 Release Dsem succesful\n"); + printf("Task3 Release Dsem succesful\n"); } else { - GJB_PRT_ERROR_INFO("Task3 Release Dsem Failed\n"); + printf("Task3 Release Dsem Failed\n"); } return NULL; @@ -82,57 +89,57 @@ void *Fun3_security_1 (void *arg) void *Fun4_security_1 (void *arg) { int res = -100; - int nDelaytime = 1000; //ʱʱ + int nDelaytime = 1000; //延时时间 - GJB_PRT_INFO("Task4 is running\n"); + printf("Task4 is running\n"); - //ȡCֵźԴ + //获取C二值信号量资源 res = sem_wait(&Dsem); if (res == 0) { - GJB_PRT_INFO("Task4 Get Dsem succesful\n"); + printf("Task4 Get Dsem succesful\n"); } else { - GJB_PRT_ERROR_INFO("Task4 Get Dsem Failed\n"); + printf("Task4 Get Dsem Failed\n"); goto error; } - //ʱ + //延时 sleep(2); - //ȡDźԴ + //获取D信号量资源 res = -100; res = sem_wait(&Csem); if (res == 0) { - GJB_PRT_INFO("Task4 Get Csem succesful\n"); + printf("Task4 Get Csem succesful\n"); } else { - GJB_PRT_ERROR_INFO("Task4 Get Csem Failed\n"); + printf("Task4 Get Csem Failed\n"); goto error; } - //ʱ - pthread_delay(nDelaytime); //1000ticks + //延时 + pthread_delay(nDelaytime); //1000个ticks res = -100; res = sem_post(&Dsem); if (res == 0) { - GJB_PRT_INFO("Task4 Release Dsem succesful\n"); + printf("Task4 Release Dsem succesful\n"); } else { - GJB_PRT_ERROR_INFO("Task4 Release Dsem Failed\n"); + printf("Task4 Release Dsem Failed\n"); goto error; } - //ʱ - pthread_delay(nDelaytime); //1000ticks + //延时 + pthread_delay(nDelaytime); //1000个ticks res = -100; res = sem_post(&Csem); if (res == 0) { - GJB_PRT_INFO("Task4 Release Csem succesful\n"); + printf("Task4 Release Csem succesful\n"); } else { - GJB_PRT_ERROR_INFO("Task4 Release Csem Failed\n"); + printf("Task4 Release Csem Failed\n"); goto error; } @@ -142,50 +149,50 @@ error: return ((void *)123); } -//ں +//测试入口函数 int Test_S0102101AQ_2 (void) { int status = -100; - //ֵźʼ + //二值信号量初始化 sem_init(&Csem, 0, 1); sem_init(&Dsem, 0, 1); - // + //创建任务 pthread_attr_init(&thread_attr3); pthread_attr_init(&thread_attr4); - thread_attr3.stackaddr = NULL; - thread_attr3.stacksize = PTHREAD_STACK_MIN * 2; - thread_attr3.inheritsched = PTHREAD_EXPLICIT_SCHED; - thread_attr3.schedpolicy = SCHED_FIFO; - thread_attr3.schedparam.sched_priority = 100; - thread_attr3.name = "Task3"; + // thread_attr3.stackaddr = NULL; + // thread_attr3.stacksize = PTHREAD_STACK_MIN * 2; + // thread_attr3.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr3.schedpolicy = SCHED_FIFO; + // thread_attr3.schedparam.sched_priority = 100; + // thread_attr3.name = "Task3"; - thread_attr4.stackaddr = NULL; - thread_attr4.stacksize = PTHREAD_STACK_MIN * 2; - thread_attr4.inheritsched = PTHREAD_EXPLICIT_SCHED; - thread_attr4.schedpolicy = SCHED_FIFO; - thread_attr4.schedparam.sched_priority = 100; - thread_attr4.name = "Task4"; + // thread_attr4.stackaddr = NULL; + // thread_attr4.stacksize = PTHREAD_STACK_MIN * 2; + // thread_attr4.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr4.schedpolicy = SCHED_FIFO; + // thread_attr4.schedparam.sched_priority = 100; + // thread_attr4.name = "Task4"; status = -100; status = pthread_create(&thread_id3, &thread_attr3, Fun3_security_1, NULL); if (status == 0) { - GJB_PRT_INFO("Task3 create succesful\n"); + printf("Task3 create succesful\n"); } else { - GJB_PRT_ERROR_INFO("Task3 create Failed\n"); + printf("Task3 create Failed\n"); } status = -100; status = pthread_create(&thread_id4, &thread_attr4, Fun4_security_1, NULL); if (status == 0) { - GJB_PRT_INFO( "Task4 create succesful\n"); + printf( "Task4 create succesful\n"); } else { - GJB_PRT_ERROR_INFO( "Task4 create Failed\n"); + printf( "Task4 create Failed\n"); } pthread_join(thread_id3, NULL); @@ -194,21 +201,42 @@ int Test_S0102101AQ_2 (void) return (0); } /********************************************************************** - * ƣ GJB_ENTRY() - * û - * - * - * ֵ - * ˵ + * 函数名称: GJB_ENTRY() + * 功能描述: 用户程序入口 + * 输入参数: 无 + * 输出参数: 无 + * 返 回 值: 无 + * 其它说明: 无 **************************************************************************/ -int gjb_S0102601AQ_deadlock(int argc, char **argv) +int main(int argc, char **argv) { int result; - result = Test_S0102101AQ_2(); /* security */ + result = Test_S0102101AQ_2(); /* security 死锁情况 */ if (result != 0) { - return (GJB_PRI_FAIL()); + return (-1); } - return (GJB_PRI_PASS()); + return (0); +} + +typedef unsigned long long u64; +typedef unsigned int u32; +#define SYSTEM_TICKS_USEC 1000 +#define EMINITED 305 +u64 g_pthread_delay_tick = 0; +int is_set_sys_rate = 0; +u32 g_sys_uleep_tick = 0; +int pthread_delay(int ticks) +{ + g_pthread_delay_tick = ticks; + sched_yield(); + if(is_set_sys_rate) + { + return usleep(g_sys_uleep_tick * ticks); + } + else + { + return usleep(SYSTEM_TICKS_USEC * ticks); + } } diff --git a/security/gjb_S0102602AQ.c b/security/gjb_S0102602AQ.c index 2857381..17f88f5 100644 --- a/security/gjb_S0102602AQ.c +++ b/security/gjb_S0102602AQ.c @@ -1,24 +1,28 @@ /********************************************************************************************************* ** -** GJB ��׼���Լ� +** GJB 标准测试集 ** ** Copyright All Rights Reserved ** -**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +**--------------文件信息-------------------------------------------------------------------------------- ** -** �� �� ��: gjb_S0102602AQ.c +** 文 件 名: gjb_S0102602AQ.c ** -** �ļ���������: 2021 �� 1 �� 12 �� +** 文件创建日期: 2021 年 1 月 12 日 ** -** �� ��: ��֤���ȼ���ת�㷨, ����3���̣߳����ȼ�Ϊ �� �� ��, �����ȼ��߳��Ȼ�û�����, �����ȼ��߳̿�ʼ����, һ��ʱ�� -** �����ȼ��߳����в����Ի���������ȡʧ�ܣ������ȼ��߳̿�ʼ����(��ʱ�����ȼ��߳�) +** 描 述: 验证优先级反转算法, 创建3个线程,优先级为 高 中 低, 低优先级线程先获得互斥锁, 中优先级线程开始运行, 一段时间 +** 高优先级线程运行并尝试互斥锁,获取失败,低优先级线程开始运行(此时中优先级线程) *********************************************************************************************************/ -include +#define _GNU_SOURCE +#include #include #include #include #include + + + #ifdef SYLIXOS #define __SYLIXOS_KERNEL #endif @@ -34,6 +38,8 @@ static pthread_attr_t thread_attr1; static pthread_attr_t thread_attr2; static pthread_attr_t thread_attr3; +int pthread_delay(int ticks); + void *Fun1_2 (void *arg) { volatile int i = 0, j = 0; @@ -45,7 +51,7 @@ void *Fun1_2 (void *arg) printf("low-task is running\n"); - //��ȡ������A��Դ + //获取互斥量A资源 res = -100; res = pthread_mutex_lock(&Amutex); @@ -68,7 +74,7 @@ void *Fun1_2 (void *arg) } res = -100; - res = pthread_mutex_unlock(&Amutex); // �������ȼ��ָ������Իᵼ�¸����ȼ��ȴ�ӡ�ͷ����ɹ� + res = pthread_mutex_unlock(&Amutex); // 会有优先级恢复,所以会导致高优先级先打印释放锁成功 if (res == 0) { printf("low-task Release Amutex succesful\n"); @@ -89,7 +95,7 @@ void *Fun2_2 (void *arg) printf("mid-task is running\n"); - pthread_delay(200); // ��������ȼ���ʼ���У������Ƴ���֤�����ȼ������� + pthread_delay(200); // 如果中优先级开始运行,我们推出保证第优先级先运行 for (i = 0; i < 1000; i++) { for (j = 0; j < 100000; j++); @@ -112,10 +118,10 @@ void *Fun3_3 (void *arg) printf("high-task is running\n"); - pthread_delay(800); // ������ȼ��ó�CPU, ��֤�����ȼ���ʼ���� + pthread_delay(800); // 最高优先级让出CPU, 保证第优先级开始运行 printf("high-task reques muext Amutex\n"); - //��ȡ������A��Դ + //获取互斥量A资源 res = -100; res = pthread_mutex_lock(&Amutex); if (res == 0) { @@ -144,20 +150,20 @@ void *Fun3_3 (void *arg) return NULL; } -//��ں��� +//入口函数 int Test_S0102102AQ_1 (void) { int status = -100; - int nDelaytime = 2; //��ʱ2��ticks + int nDelaytime = 2; //延时2个ticks bind(pthread_self(), 0); pthread_barrier_init(&bt, NULL, 4); - //��������ʼ�� + //互斥量初始化 pthread_mutex_init(&Amutex, NULL); - //�������� + //创建任务 pthread_attr_init(&thread_attr1); pthread_attr_init(&thread_attr2); pthread_attr_init(&thread_attr3); @@ -193,9 +199,8 @@ int Test_S0102102AQ_1 (void) printf("high-task create Failed\n"); goto error; } - - //��ʱ - pthread_delay(nDelaytime); // 2��ticks + //延时 + pthread_delay(nDelaytime); // 2个ticks status = -100; @@ -208,8 +213,8 @@ int Test_S0102102AQ_1 (void) goto error; } - //��ʱ - pthread_delay(nDelaytime); // 2��ticks + //延时 + pthread_delay(nDelaytime); // 2个ticks status = -100; @@ -235,12 +240,12 @@ error: return (-1); } /********************************************************************** - * �������ƣ� GJB_ENTRY() - * ���������� �û�������� - * ��������� �� - * ��������� �� - * �� �� ֵ�� �� - * ����˵���� �� + * 函数名称: GJB_ENTRY() + * 功能描述: 用户程序入口 + * 输入参数: 无 + * 输出参数: 无 + * 返 回 值: 无 + * 其它说明: 无 **************************************************************************/ int main(int argc, char **argv) { @@ -253,3 +258,24 @@ int main(int argc, char **argv) return (0); } + +typedef unsigned long long u64; +typedef unsigned int u32; +#define SYSTEM_TICKS_USEC 1000 +#define EMINITED 305 +u64 g_pthread_delay_tick = 0; +int is_set_sys_rate = 0; +u32 g_sys_uleep_tick = 0; +int pthread_delay(int ticks) +{ + g_pthread_delay_tick = ticks; + sched_yield(); + if(is_set_sys_rate) + { + return usleep(g_sys_uleep_tick * ticks); + } + else + { + return usleep(SYSTEM_TICKS_USEC * ticks); + } +} diff --git a/security/gjb_S0102603AQ.c b/security/gjb_S0102603AQ.c index 1072fde..8100b37 100644 --- a/security/gjb_S0102603AQ.c +++ b/security/gjb_S0102603AQ.c @@ -1,17 +1,18 @@ /********************************************************************************************************* ** -** GJB ��׼���Լ� +** GJB 标准测试集 ** ** Copyright All Rights Reserved ** -**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +**--------------文件信息-------------------------------------------------------------------------------- ** -** �� �� ��: gjb_S0102603AQ.c +** 文 件 名: gjb_S0102603AQ.c ** -** �ļ���������: 2021 �� 1 �� 20 �� +** 文件创建日期: 2021 年 1 月 20 日 ** -** �� ��: �����꺯���ӿڳ����ȶ��Բ���, ͨ�����ò�ͬ�ĺ����ӿڲ��жϺ����ķ���ֵ +** 描 述: 国军标函数接口长期稳定性测试, 通过调用不同的函数接口并判断函数的返回值 *********************************************************************************************************/ +#define _XOPEN_SOURCE #include #include #include @@ -19,8 +20,19 @@ #include #include #include +#include +#include +#include + +#include + + +#include +#include + +#include #ifdef SYLIXOS @@ -32,6 +44,17 @@ #define TimeLen 1800 #define DelayTime 30 +#define TIMER_RELATIVE_C 0 + +# define CLOCK_REALTIME 0 + +// mpart_create增加 +typedef int mpart_id; +mpart_id MPARTID = 0; +#define MAX_PART 256 +#define EMNOTINITED 0x7A +#define BASE_MID 300 + static pthread_t thread_id1; static pthread_t thread_id2; static pthread_t thread_id3; @@ -70,6 +93,13 @@ void *Fun8_security(void *arg); void *Fun9_security(void *arg); void *Fun10_security(void *arg); int S0102103AQ(void); +int mpart_create(char *addr,size_t size,mpart_id *mid); +int pthread_delay(int ticks); +int mpart_delete(mpart_id mid); +int mpart_free(mpart_id mid,char* addr); +void* mpart_alloc(mpart_id mid,size_t size); + + void handler_xx (int signo) { @@ -109,7 +139,8 @@ int Test_S0102103AQ (void) while (1) { if (nRunTimes++ % 50 == 0) { printf("Rounds:%d\n", nRunTimes); - mem_show(); + // mem_show(); + system("cat /proc/meminfo"); } S0102103AQ(); @@ -494,6 +525,31 @@ void *Fun7_security (void *arg) return NULL; } +// zijitianjia +struct itimerspec +{ + struct timespec it_interval; //定时器周期值 + struct timespec it_value; //定时器到期值 +}; + +union sigval { + int sival_int; + void *sival_ptr; + }; + +struct sigevent { + int sigev_notify; /* 通知方式 */ + int sigev_signo; /* 通知信号 */ + union sigval sigev_value; /* 伴随通知所传递的数据 */ + void (*sigev_notify_function) (union sigval); + /* 通知方式设置为"线程通知" (SIGEV_THREAD)时新线程的线程函数 */ + void *sigev_notify_attributes; + /* 通知方式设置为"线程通知" (SIGEV_THREAD)时新线程的属性 */ + pid_t sigev_notify_thread_id; + /* 通知方式为SIGEV_THREAD_ID时,接收信号的线程的pid */ + }; + + void *Fun8_security (void *arg) { @@ -724,12 +780,12 @@ void *Fun10_security (void *arg) return NULL; } /********************************************************************** - * �������ƣ� GJB_ENTRY() - * ���������� �û�������� - * ��������� �� - * ��������� �� - * �� �� ֵ�� �� - * ����˵���� �� + * 函数名称: GJB_ENTRY() + * 功能描述: 用户程序入口 + * 输入参数: 无 + * 输出参数: 无 + * 返 回 值: 无 + * 其它说明: 无 **************************************************************************/ int main(int argc, char **argv) { @@ -742,3 +798,187 @@ int main(int argc, char **argv) return (0); } +typedef int mpart_id; +// mpart_id MPARTID = 0; +#define MAX_PART 256 +#define EMNOTINITED 0x7A +#define BASE_MID 300 +#define SLICE_MAX 10 +typedef unsigned int uint; +mpart_id MPARTID; +struct mc_node { + mpart_id mid; + struct mc_node *next ; +} Node; +typedef struct __offset_part +{ + char * addr; + char isinuse; + size_t len; +}offset_part; +typedef struct _tag_addr_mpart +{ + char* mpart; + size_t size; + size_t free; + int used; + uint offset_index; + offset_part offset[SLICE_MAX]; +}addr_mpart; + +addr_mpart mpart_global[MAX_PART]; +int mpart_create(char *addr,size_t size,mpart_id *mid) +{ + if(addr == NULL ||mid ==NULL || size ==(size_t)-1) + { + errno=EINVAL; + return -1; + } + if(MPARTID>MAX_PART-1 || size==0x7fffffff) + { + errno=EAGAIN; + return -1; + } + if(MPARTID == -1) + { + errno=EMNOTINITED; + return -1; + } + mpart_global[MPARTID].mpart=addr; + mpart_global[MPARTID].size=size; + mpart_global[MPARTID].free=size; + mpart_global[MPARTID].used=0; + *mid=BASE_MID+MPARTID++; + + return 0; +} + +typedef unsigned long long u64; +typedef unsigned int u32; +#define SYSTEM_TICKS_USEC 1000 +#define EMINITED 305 +u64 g_pthread_delay_tick = 0; +int is_set_sys_rate = 0; +u32 g_sys_uleep_tick = 0; +int pthread_delay(int ticks) +{ + g_pthread_delay_tick = ticks; + sched_yield(); + if(is_set_sys_rate) + { + return usleep(g_sys_uleep_tick * ticks); + } + else + { + return usleep(SYSTEM_TICKS_USEC * ticks); + } +} + +int mpart_delete(mpart_id mid){ + int i=0; + if(mid==0) + { + errno= EINVAL; + return -1; + } + if(mid (BASE_MID+MPARTID)) + { + errno=EINVAL; + return -1; + } + mid=mid-BASE_MID; + + if(mpart_global[mid].used==-1) + { + return -1; + } + mpart_global[mid].offset_index=0; + mpart_global[mid].free= mpart_global[mid].size; + mpart_global[mid].used=-1; + memset(mpart_global[mid].offset,0,SLICE_MAX*sizeof(offset_part)); + return 0; +} + +int mpart_free(mpart_id mid,char* addr) +{ + int ret = 0; + int i = 0; + if(mid (BASE_MID+MPARTID)) + { + errno=EINVAL; + return -1; + } + mid=mid-BASE_MID; + if(addr == NULL||mpart_global[mid].mpart ==NULL){ + errno= EINVAL; + return -1; + } + if(MPARTID == -1) + { + errno=EMINITED; + return -1; + }addr_mpart mpart_global[MAX_PART]; + for(i=0;i (BASE_MID+MPARTID)) + { + errno=EINVAL; + return NULL; + } + mid=mid-BASE_MID; + if (size >= 0x7fffffff) + { + errno = ENOMEM; + return NULL; + } + if( size<=0 ||size == (size_t)-1 ) + { + errno=EINVAL; + return NULL; + } + if(size>mpart_global[mid].free) + { + errno=ENOMEM; + return NULL; + } + if(MPARTID == -1) + { + errno=0x7A; + return NULL; + } + + mpart_global[mid].free -= size; + char *ptr; + + ptr= mpart_global[mid].mpart+mpart_global[mid].used; + mpart_global[mid].used +=size; + int index=mpart_global[mid].offset_index; + mpart_global[mid].offset[index].addr=ptr; + mpart_global[mid].offset[index].isinuse=1; + mpart_global[mid].offset[index].len=size; + mpart_global[mid].offset_index++; + return ptr; + +} diff --git a/security/gjb_S0102605AQ.c b/security/gjb_S0102605AQ.c index a595a64..9d2bcfd 100644 --- a/security/gjb_S0102605AQ.c +++ b/security/gjb_S0102605AQ.c @@ -1,33 +1,48 @@ /********************************************************************************************************* ** -** GJB ׼Լ +** GJB 标准测试集 ** ** Copyright All Rights Reserved ** -**--------------ļϢ-------------------------------------------------------------------------------- +**--------------文件信息-------------------------------------------------------------------------------- ** -** : S0102605.c +** 文 件 名: S0102605.c ** -** ļ: 2021 1 12 +** 文件创建日期: 2021 年 1 月 12 日 ** -** : ͨǷָ쳣, ֤ϵͳ쳣 +** 描 述: 通过构造非法指令异常, 验证系统异常处理功能 *********************************************************************************************************/ #ifdef SYLIXOS #define __SYLIXOS_KERNEL #endif #include +#include // #include "gjb.h" static pthread_attr_t thread_attr1; static pthread_t thread_id1; +static void gjb_illeagal_code(void ) +{ + typedef void (*funcTest)(void); + int temp[10] = {0}; + funcTest test; + test = temp; + pthread_delay(1000); + test(); +} + +#define gjb_os_illeagal_code gjb_illeagal_code + +int pthread_delay(int ticks); + void *Fun1_security_1 (void *arg) { printf("Task1 is running-1\n"); /* - * Ƿָ쳣, ͬƽ̨úʵֲͬ + * 构造非法指令异常, 不同的平台该函数的实现不同 */ gjb_os_illeagal_code(); @@ -41,10 +56,10 @@ void pagefails (void) char *p; int i; - // ֻڴ + // 这里只分配虚拟内存 p = malloc(1024 * 1024); if (p) { - // õʱͨȱҳ쳣ڴ + // 这里真正用得时候会通过缺页异常分配真正得物理内存 memset(p, 'a', 1024 * 1024); for (i = 0; i < 1024 * 1024; ++i) { @@ -58,7 +73,7 @@ void pagefails (void) } /* - * Ƿָ쳣ں + * 非法指令异常测试入口函数 */ int S0102605 (void) { @@ -69,15 +84,15 @@ int S0102605 (void) char b = 255; char c; - // ȱҳ쳣 + // 缺页异常测试 pagefails(); - // , c ϵͳӦó쳣 + // 运算符溢出测试, c 发生了溢出系统不应该出现异常 c = a + b; printf("%d\n", c); /* - * ʼ߳thread_attr1 + * 初始化线程thread_attr1 */ pthread_attr_init(&thread_attr1); @@ -97,7 +112,7 @@ int S0102605 (void) } /* - * ʱ2000 TICK + * 延时2000 TICK */ pthread_delay(nDelaytime); @@ -108,12 +123,12 @@ int S0102605 (void) return (0); } /********************************************************************************************************* - * ƣ GJB_ENTRY() - * û - * - * - * ֵ - * ˵ + * 函数名称: GJB_ENTRY() + * 功能描述: 用户程序入口 + * 输入参数: 无 + * 输出参数: 无 + * 返 回 值: 无 + * 其它说明: 无 *********************************************************************************************************/ int main(int argc, char **argv) { @@ -126,3 +141,25 @@ int main(int argc, char **argv) return (0); } + + +typedef unsigned long long u64; +typedef unsigned int u32; +#define SYSTEM_TICKS_USEC 1000 +#define EMINITED 305 +u64 g_pthread_delay_tick = 0; +int is_set_sys_rate = 0; +u32 g_sys_uleep_tick = 0; +int pthread_delay(int ticks) +{ + g_pthread_delay_tick = ticks; + sched_yield(); + if(is_set_sys_rate) + { + return usleep(g_sys_uleep_tick * ticks); + } + else + { + return usleep(SYSTEM_TICKS_USEC * ticks); + } +} -- Gitee From 89b7bb1e61c9617bb65b655e54ffc28336551fc6 Mon Sep 17 00:00:00 2001 From: WJ Date: Thu, 16 Nov 2023 15:51:49 +0800 Subject: [PATCH 6/6] =?UTF-8?q?security=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- security/README | 3 - security/gjb_S0102606AQ_addr_nonalign.c | 248 +++++++++++++++++++++--- security/gjb_S0102606AQ_addr_overflow.c | 175 ++++++++++++++--- security/gjb_S0102606AQ_illgel_code.c | 149 +++++++++++++- 4 files changed, 516 insertions(+), 59 deletions(-) diff --git a/security/README b/security/README index 9c69680..5c88eac 100644 --- a/security/README +++ b/security/README @@ -3,6 +3,3 @@ ## 1611 :gcc gjb_S0101611AQ.c -o 1611 -lpthread ## 2603 gcc gjb_S0102603AQ.c -o 2603 -lpthread -lrt -## 2:pthread_delay报错,gjb_os_binding 改为bind 剩下glibc错误 -##3 TIMER_RELATIVE_C 报错找不到解决办法,O_RDWR未定义 -##5 gjb_os_illeagal_code找不到。pthread_delay报错 \ No newline at end of file diff --git a/security/gjb_S0102606AQ_addr_nonalign.c b/security/gjb_S0102606AQ_addr_nonalign.c index 0046816..ef7fced 100644 --- a/security/gjb_S0102606AQ_addr_nonalign.c +++ b/security/gjb_S0102606AQ_addr_nonalign.c @@ -1,33 +1,182 @@ /********************************************************************************************************* ** -** GJB ��׼���Լ� +** GJB 标准测试集 ** ** Copyright All Rights Reserved ** -**--------------�ļ���Ϣ-------------------------------------------------------------------------------- +**--------------文件信息-------------------------------------------------------------------------------- ** -** �� �� ��: gjb_S0102606AQ_addr_nonalign.c +** 文 件 名: gjb_S0102606AQ_addr_nonalign.c ** -** �ļ���������: 2021 �� 1 �� 12 �� +** 文件创建日期: 2021 年 1 月 12 日 ** -** �� ��: ͨ�������ַ�Ƕ����쳣��֤����ϵͳ���쳣�������� +** 描 述: 通过构造地址非对齐异常验证操作系统的异常处理功能 *********************************************************************************************************/ #ifdef SYLIXOS #define __SYLIXOS_KERNEL #endif #include +#include +#include +#include // #include "gjb.h" static pthread_attr_t thread_attr3; static pthread_t thread_id3; static int exec_flags = 0; + + + +typedef unsigned int u32; +typedef u32 ARCH_REG_T; +typedef struct { + ARCH_REG_T REG_uiCpsr; + ARCH_REG_T REG_uiR14; + ARCH_REG_T REG_uiR13; + ARCH_REG_T REG_uiR0; + ARCH_REG_T REG_uiR1; + ARCH_REG_T REG_uiR2; + ARCH_REG_T REG_uiR3; + ARCH_REG_T REG_uiR4; + ARCH_REG_T REG_uiR5; + ARCH_REG_T REG_uiR6; + ARCH_REG_T REG_uiR7; + ARCH_REG_T REG_uiR8; + ARCH_REG_T REG_uiR9; + ARCH_REG_T REG_uiR10; + ARCH_REG_T REG_uiR11; + ARCH_REG_T REG_uiR12; + ARCH_REG_T REG_uiR15; + ARCH_REG_T REG_ulCP0Epc; + ARCH_REG_T REG_ulPc; + ARCH_REG_T REG_XIP; + +#define REG_uiFp REG_uiR11 +#define REG_uiIp REG_uiR12 +#define REG_uiSp REG_uiR13 +#define REG_uiLr REG_uiR14 +#define REG_uiPc REG_uiR15 +} ARCH_REG_CTX; +typedef ARCH_REG_CTX REG_SET; +typedef void (*EXC_HANDLER) (int, REG_SET* ); +EXC_HANDLER user_handler = NULL; +jmp_buf EXCEPTION_ENV_MASK; +REG_SET no_use_reg; +EXC_HANDLER exception_handler_set(EXC_HANDLER handler); + + +#define gjb_os_printk gjb_printk +#define gjb_printk(fmt, arg...) ({_PrintFormat(fmt, ##arg); _PrintFormat("\r\n");}) + +#define gjb_os_pc_resume gjb_pc_resume +void gjb_pc_resume (REG_SET* reg) +{ +#if defined(__aarch64__) + reg->REG_ulPc += 8; +#endif + +#if defined(__mips64) + reg->REG_ulCP0Epc += 8; +#endif + +#if defined(__mips__) + reg->REG_ulCP0Epc += 4; +#endif + +#if defined(__loongarch64) + reg->REG_ulCP0Epc += 4; +#endif + +#if defined(__arm__) + reg->REG_uiPc += 4; +#endif + +#if defined(__i386__) + reg->REG_XIP += 4; +#endif + +#if defined(__x86_64__) + reg->REG_XIP += 8; +#endif +} + +#define gjb_os_address_noalign gjb_address_noalign +static inline void gjb_address_noalign (void) +{ +#ifdef __arm__ + long a = 0x123456; + + *(int *)(a + 1) = 12; +#endif + +#if defined(__x86_64__) || (__i386__) + long p = 0x0; + *(int *)p = 1; +#endif + +#ifdef __aarch64__ + struct foo x = {{0, 1, 2, 3, 4, 5, 6, 7}}; + atomic_t *v = (atomic_t *)(x.bar + 3); + + LONG lOld = 0; + LONG lNew = 1; + LONG lTemp; + LONG lOldVal; + + __asm__ __volatile__( + "MRS %[tmp], SCTLR_EL1\n" + "ORR %[tmp], %[tmp], #(1 << 1)\n" + "MSR SCTLR_EL1, %[tmp]\n" + "ldxr %w[oldval], %[v] \n" + "nop\n" + "nop\n" + "nop\n" + "nop\n" + "nop\n" + "nop\n" + "nop\n" + "nop\n" + :[tmp] "=&r" (lTemp),[oldval] "=&r" (lOldVal), [v] "+Q"(v->counter) + :[old]"Lr"(lOld),[new]"r"(lNew) + :"memory" + ); +#endif + +#if defined(__loongarch64) || defined(__mips__) + struct foo x = {{0, 1, 2, 3, 4, 5, 6, 7}}; + + unsigned int *p = (unsigned int *)(x.bar + 3); + + long p1 = 0x0; + + *(int *)p1 = 1; + + *p = 0xdeadface; + + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + + p = (unsigned int *)x.bar; + + *p = 1; +#endif +} +#define _PrintFormat printf +int pthread_delay(int ticks); + void ExFunsecurity(int exc, REG_SET* reg) { gjb_os_printk("excFun is running\n"); exec_flags++; /* - * �����쳣��, ʹPC�Ĵ���ָ����һ������ָ�� + * 发生异常后, 使PC寄存器指向下一条正常指令 */ gjb_os_pc_resume(reg); @@ -40,15 +189,14 @@ void *Fun3_security_4 (void *arg) printf("Task3 is running-1\n"); /* - * �����ַ�Ƕ����쳣, ��ͬ��ƽ̨�ú�����ʵ�ֲ�ͬ + * 构造地址非对齐异常, 不同的平台该函数的实现不同 */ gjb_os_address_noalign(); printf("Task3 is running-2\n"); return NULL; } - -//��ַ�����쳣������ں��� +//地址对齐异常测试入口函数 int Test_S0102105AQ3 (void) { int status = -100; @@ -57,21 +205,21 @@ int Test_S0102105AQ3 (void) EXC_HANDLER old; /* - * ע��һ���û��쳣�������� + * 注册一个用户异常处理函数 */ old = exception_handler_set((EXC_HANDLER)ExFunsecurity); printf("exception_handler_set status = %x\n", (unsigned int)(long)old); - //�������� + //创建任务 pthread_attr_init(&thread_attr3); - thread_attr3.stackaddr = NULL; - thread_attr3.stacksize = PTHREAD_STACK_MIN * 2; - thread_attr3.inheritsched = PTHREAD_EXPLICIT_SCHED; - thread_attr3.schedpolicy = SCHED_FIFO; - thread_attr3.schedparam.sched_priority = 100; - thread_attr3.name = "Task3"; + // thread_attr3.stackaddr = NULL; + // thread_attr3.stacksize = PTHREAD_STACK_MIN * 2; + // thread_attr3.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr3.schedpolicy = SCHED_FIFO; + // thread_attr3.schedparam.sched_priority = 100; + // thread_attr3.name = "Task3"; status = pthread_create(&thread_id3, &thread_attr3, Fun3_security_4, NULL); if (status == 0) { @@ -88,10 +236,10 @@ int Test_S0102105AQ3 (void) printf("MainFun is running\n"); } - /* - * �ָ��û��쳣�������� + /* + * 恢复用户异常处理函数 */ - exception_handler_set(NULL); // ֻ�ǻָ�һ��֮ǰ�Ļ��� + exception_handler_set(NULL); // 只是恢复一下之前的环境 if (!exec_flags) { return (-1); @@ -100,12 +248,12 @@ int Test_S0102105AQ3 (void) return (0); } /********************************************************************** - * �������ƣ� GJB_ENTRY() - * ���������� �û�������� - * ��������� �� - * ��������� �� - * �� �� ֵ�� �� - * ����˵���� �� + * 函数名称: GJB_ENTRY() + * 功能描述: 用户程序入口 + * 输入参数: 无 + * 输出参数: 无 + * 返 回 值: 无 + * 其它说明: 无 **************************************************************************/ int main(int argc, char **argv) { @@ -118,3 +266,51 @@ int main(int argc, char **argv) return (0); } + +typedef unsigned long long u64; +typedef unsigned int u32; +#define SYSTEM_TICKS_USEC 1000 + +u64 g_pthread_delay_tick = 0; +int is_set_sys_rate = 0; +u32 g_sys_uleep_tick = 0; +int pthread_delay(int ticks) +{ + g_pthread_delay_tick = ticks; + sched_yield(); + if(is_set_sys_rate) + { + return usleep(g_sys_uleep_tick * ticks); + } + else + { + return usleep(SYSTEM_TICKS_USEC * ticks); + } +} +void signal_handler_fun(int signum) +{ + // printf("catch signal %d\n", signum); + errno = EINVAL; + if(user_handler != NULL) + { + user_handler(signum, &no_use_reg); + } + siglongjmp(EXCEPTION_ENV_MASK, 1); +} +EXC_HANDLER exception_handler_set(EXC_HANDLER handler) +{ + #define SIGSEGV 11 /* Invalid access to storage. */ + #define SIGBUS 10 /* Bus error. */ + #define SIGTERM 15 /* Termination request. */ + #define SIGILL 4 /* Illegal instruction. */ + signal(SIGSEGV, signal_handler_fun); + signal(SIGBUS, signal_handler_fun); + signal(SIGTERM, signal_handler_fun); + signal(SIGILL, signal_handler_fun); + + // signal(SIGUSR1, signal_handler_fun); + // signal(SIGUSR1, signal_handler_fun); + // signal(SIGUSR1, signal_handler_fun); + user_handler = handler; + return handler; +} \ No newline at end of file diff --git a/security/gjb_S0102606AQ_addr_overflow.c b/security/gjb_S0102606AQ_addr_overflow.c index da5100d..ca8d0d5 100644 --- a/security/gjb_S0102606AQ_addr_overflow.c +++ b/security/gjb_S0102606AQ_addr_overflow.c @@ -1,27 +1,110 @@ /********************************************************************************************************* ** -** GJB ׼Լ +** GJB 标准测试集 ** ** Copyright All Rights Reserved ** -**--------------ļϢ-------------------------------------------------------------------------------- +**--------------文件信息-------------------------------------------------------------------------------- ** -** : gjb_S0102606AQ_addr_overflow.c +** 文 件 名: gjb_S0102606AQ_addr_overflow.c ** -** ļ: 2021 1 12 +** 文件创建日期: 2021 年 1 月 12 日 ** -** : ַ쳣, ֤ϵͳ쳣 +** 描 述: 构造地址溢出异常, 验证系统异常处理功能 *********************************************************************************************************/ #ifdef SYLIXOS #define __SYLIXOS_KERNEL #endif #include +#include +#include +#include // #include "gjb.h" static int exec_flags_2 = 0; static pthread_attr_t thread_attr2; static pthread_t thread_id2; +typedef unsigned int u32; +typedef u32 ARCH_REG_T; +typedef struct { + ARCH_REG_T REG_uiCpsr; + ARCH_REG_T REG_uiR14; + ARCH_REG_T REG_uiR13; + ARCH_REG_T REG_uiR0; + ARCH_REG_T REG_uiR1; + ARCH_REG_T REG_uiR2; + ARCH_REG_T REG_uiR3; + ARCH_REG_T REG_uiR4; + ARCH_REG_T REG_uiR5; + ARCH_REG_T REG_uiR6; + ARCH_REG_T REG_uiR7; + ARCH_REG_T REG_uiR8; + ARCH_REG_T REG_uiR9; + ARCH_REG_T REG_uiR10; + ARCH_REG_T REG_uiR11; + ARCH_REG_T REG_uiR12; + ARCH_REG_T REG_uiR15; + ARCH_REG_T REG_ulCP0Epc; + ARCH_REG_T REG_ulPc; + ARCH_REG_T REG_XIP; + +#define REG_uiFp REG_uiR11 +#define REG_uiIp REG_uiR12 +#define REG_uiSp REG_uiR13 +#define REG_uiLr REG_uiR14 +#define REG_uiPc REG_uiR15 +} ARCH_REG_CTX; +typedef ARCH_REG_CTX REG_SET; +typedef void (*EXC_HANDLER) (int, REG_SET* ); +EXC_HANDLER user_handler = NULL; +jmp_buf EXCEPTION_ENV_MASK; +REG_SET no_use_reg; + +#define gjb_os_pc_resume gjb_pc_resume +void gjb_pc_resume (REG_SET* reg) +{ +#if defined(__aarch64__) + reg->REG_ulPc += 8; +#endif + +#if defined(__mips64) + reg->REG_ulCP0Epc += 8; +#endif + +#if defined(__mips__) + reg->REG_ulCP0Epc += 4; +#endif + +#if defined(__loongarch64) + reg->REG_ulCP0Epc += 4; +#endif + +#if defined(__arm__) + reg->REG_uiPc += 4; +#endif + +#if defined(__i386__) + reg->REG_XIP += 4; +#endif + +#if defined(__x86_64__) + reg->REG_XIP += 8; +#endif +} + +EXC_HANDLER exception_handler_set(EXC_HANDLER handler); + +static inline void gjb_array_overflow (void) +{ + char tempchar[10]; + sprintf(tempchar[1], "abdcdddddddddfffffffffffffffffffffffffffffddd"); //给字符变量赋值字符串 + // sprintf(tempchar, "abdcdddddddddfffffffffffffffffffffffffffffddd"); //给字符变量赋值字符串 + tempchar[10] = 0xaa; +} + +int pthread_delay(int ticks); + void ExFunsecurity_1(int exc, REG_SET* reg) { printf("excFun is running\n"); @@ -36,7 +119,7 @@ void *Fun2security_4() printf("Task2 is running-1\n"); /* - * ַ쳣, ͬƽ̨úʵֲͬ + * 构造地址溢出异常, 不同的平台该函数的实现不同 */ gjb_array_overflow(); printf("Task2 is running-2\n"); @@ -44,7 +127,7 @@ void *Fun2security_4() return (NULL); } -//ַԽ쳣ں +//地址越界异常测试入口函数 int Test_S0102105AQ2 (void) { int status = -100; @@ -53,7 +136,7 @@ int Test_S0102105AQ2 (void) EXC_HANDLER old; /* - * עһû쳣 + * 注册一个用户异常处理函数 */ old = exception_handler_set((EXC_HANDLER)ExFunsecurity_1); @@ -61,15 +144,15 @@ int Test_S0102105AQ2 (void) pthread_attr_init(&thread_attr2); - thread_attr2.stackaddr = NULL; - thread_attr2.stacksize = PTHREAD_STACK_MIN*2; - thread_attr2.inheritsched = PTHREAD_EXPLICIT_SCHED; - thread_attr2.schedpolicy = SCHED_FIFO; - thread_attr2.schedparam.sched_priority = 100; - thread_attr2.name = "Task2"; + // thread_attr2.stackaddr = NULL; + // thread_attr2.stacksize = PTHREAD_STACK_MIN*2; + // thread_attr2.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr2.schedpolicy = SCHED_FIFO; + // thread_attr2.schedparam.sched_priority = 100; + // thread_attr2.name = "Task2"; /* - * һ + * 创建一个任务 */ status = pthread_create(&thread_id2, &thread_attr2, Fun2security_4, NULL); if (status == 0) { @@ -80,13 +163,13 @@ int Test_S0102105AQ2 (void) return (-1); } - pthread_delay(nDelaytime); // 2000ticks + pthread_delay(nDelaytime); // 2000个ticks for (i = 0; i < 10; i++) { printf("MainFun is running\n"); } - exception_handler_set(NULL); // ֻǻָһ֮ǰĻ + exception_handler_set(NULL); // 只是恢复一下之前的环境 if (!exec_flags_2) { return (-1); @@ -95,12 +178,12 @@ int Test_S0102105AQ2 (void) return (0); } /********************************************************************** - * ƣ GJB_ENTRY() - * û - * - * - * ֵ - * ˵ + * 函数名称: GJB_ENTRY() + * 功能描述: 用户程序入口 + * 输入参数: 无 + * 输出参数: 无 + * 返 回 值: 无 + * 其它说明: 无 **************************************************************************/ int main(int argc, char **argv) { @@ -113,3 +196,49 @@ int main(int argc, char **argv) return (0); } +void signal_handler_fun(int signum) +{ + // printf("catch signal %d\n", signum); + errno = EINVAL; + if(user_handler != NULL) + { + user_handler(signum, &no_use_reg); + } + siglongjmp(EXCEPTION_ENV_MASK, 1); +} +EXC_HANDLER exception_handler_set(EXC_HANDLER handler) +{ + #define SIGSEGV 11 /* Invalid access to storage. */ + #define SIGBUS 10 /* Bus error. */ + #define SIGTERM 15 /* Termination request. */ + #define SIGILL 4 /* Illegal instruction. */ + signal(SIGSEGV, signal_handler_fun); + signal(SIGBUS, signal_handler_fun); + signal(SIGTERM, signal_handler_fun); + signal(SIGILL, signal_handler_fun); + + // signal(SIGUSR1, signal_handler_fun); + // signal(SIGUSR1, signal_handler_fun); + // signal(SIGUSR1, signal_handler_fun); + user_handler = handler; + return handler; +} +typedef unsigned long long u64; +typedef unsigned int u32; +#define SYSTEM_TICKS_USEC 1000 +u64 g_pthread_delay_tick = 0; +int is_set_sys_rate = 0; +u32 g_sys_uleep_tick = 0; +int pthread_delay(int ticks) +{ + g_pthread_delay_tick = ticks; + sched_yield(); + if(is_set_sys_rate) + { + return usleep(g_sys_uleep_tick * ticks); + } + else + { + return usleep(SYSTEM_TICKS_USEC * ticks); + } +} \ No newline at end of file diff --git a/security/gjb_S0102606AQ_illgel_code.c b/security/gjb_S0102606AQ_illgel_code.c index 62e6c48..b084575 100644 --- a/security/gjb_S0102606AQ_illgel_code.c +++ b/security/gjb_S0102606AQ_illgel_code.c @@ -16,12 +16,99 @@ #define __SYLIXOS_KERNEL #endif #include +#include +#include +#include +#include // #include "gjb.h" +static void gjb_illeagal_code(void ) +{ + typedef void (*funcTest)(void); + int temp[10] = {0}; + funcTest test; + test = temp; + pthread_delay(1000); + test(); +} +#define gjb_os_illeagal_code gjb_illeagal_code + + +static int exec_flags_2 = 0; +static pthread_attr_t thread_attr2; +static pthread_t thread_id2; +typedef unsigned int u32; +typedef u32 ARCH_REG_T; +typedef struct { + ARCH_REG_T REG_uiCpsr; + ARCH_REG_T REG_uiR14; + ARCH_REG_T REG_uiR13; + ARCH_REG_T REG_uiR0; + ARCH_REG_T REG_uiR1; + ARCH_REG_T REG_uiR2; + ARCH_REG_T REG_uiR3; + ARCH_REG_T REG_uiR4; + ARCH_REG_T REG_uiR5; + ARCH_REG_T REG_uiR6; + ARCH_REG_T REG_uiR7; + ARCH_REG_T REG_uiR8; + ARCH_REG_T REG_uiR9; + ARCH_REG_T REG_uiR10; + ARCH_REG_T REG_uiR11; + ARCH_REG_T REG_uiR12; + ARCH_REG_T REG_uiR15; + ARCH_REG_T REG_ulCP0Epc; + ARCH_REG_T REG_ulPc; + ARCH_REG_T REG_XIP; + +#define REG_uiFp REG_uiR11 +#define REG_uiIp REG_uiR12 +#define REG_uiSp REG_uiR13 +#define REG_uiLr REG_uiR14 +#define REG_uiPc REG_uiR15 +} ARCH_REG_CTX; +typedef ARCH_REG_CTX REG_SET; +typedef void (*EXC_HANDLER) (int, REG_SET* ); +EXC_HANDLER user_handler = NULL; +jmp_buf EXCEPTION_ENV_MASK; +REG_SET no_use_reg; +EXC_HANDLER exception_handler_set(EXC_HANDLER handler); static int exec_flags_1 = 0; static pthread_attr_t thread_attr1; static pthread_t thread_id1; +#define gjb_os_pc_resume gjb_pc_resume +void gjb_pc_resume (REG_SET* reg) +{ +#if defined(__aarch64__) + reg->REG_ulPc += 8; +#endif + +#if defined(__mips64) + reg->REG_ulCP0Epc += 8; +#endif + +#if defined(__mips__) + reg->REG_ulCP0Epc += 4; +#endif + +#if defined(__loongarch64) + reg->REG_ulCP0Epc += 4; +#endif + +#if defined(__arm__) + reg->REG_uiPc += 4; +#endif + +#if defined(__i386__) + reg->REG_XIP += 4; +#endif + +#if defined(__x86_64__) + reg->REG_XIP += 8; +#endif +} + void *Fun1_security_5 (void *arg) { printf("Task1 is running-1\n"); @@ -56,19 +143,19 @@ int Test_S0102105AQ1 (void) /* * 注册一个用户异常处理函数 */ - old_exc = exception_handler_set((EXC_HANDLER)ExFunxxxx); + old_exc = exception_handler_set((EXC_HANDLER) ExFunxxxx); printf("exception_handler_set status = %x\n", (unsigned int)(long)old_exc); //创建任务 pthread_attr_init(&thread_attr1); - thread_attr1.stackaddr = NULL; - thread_attr1.stacksize = PTHREAD_STACK_MIN * 2; - thread_attr1.inheritsched = PTHREAD_EXPLICIT_SCHED; - thread_attr1.schedpolicy = SCHED_FIFO; - thread_attr1.schedparam.sched_priority = 100; - thread_attr1.name = "Task1"; + // thread_attr1.stackaddr = NULL; + // thread_attr1.stacksize = PTHREAD_STACK_MIN * 2; + // thread_attr1.inheritsched = PTHREAD_EXPLICIT_SCHED; + // thread_attr1.schedpolicy = SCHED_FIFO; + // thread_attr1.schedparam.sched_priority = 100; + // thread_attr1.name = "Task1"; status = pthread_create(&thread_id1, &thread_attr1, Fun1_security_5, NULL); if (status == 0) { @@ -112,3 +199,51 @@ int main(int argc, char **argv) return (0); } + +typedef unsigned long long u64; +typedef unsigned int u32; +#define SYSTEM_TICKS_USEC 1000 + +u64 g_pthread_delay_tick = 0; +int is_set_sys_rate = 0; +u32 g_sys_uleep_tick = 0; +int pthread_delay(int ticks) +{ + g_pthread_delay_tick = ticks; + sched_yield(); + if(is_set_sys_rate) + { + return usleep(g_sys_uleep_tick * ticks); + } + else + { + return usleep(SYSTEM_TICKS_USEC * ticks); + } +} +void signal_handler_fun(int signum) +{ + // printf("catch signal %d\n", signum); + errno = EINVAL; + if(user_handler != NULL) + { + user_handler(signum, &no_use_reg); + } + siglongjmp(EXCEPTION_ENV_MASK, 1); +} +EXC_HANDLER exception_handler_set(EXC_HANDLER handler) +{ + #define SIGSEGV 11 /* Invalid access to storage. */ + #define SIGBUS 10 /* Bus error. */ + #define SIGTERM 15 /* Termination request. */ + #define SIGILL 4 /* Illegal instruction. */ + signal(SIGSEGV, signal_handler_fun); + signal(SIGBUS, signal_handler_fun); + signal(SIGTERM, signal_handler_fun); + signal(SIGILL, signal_handler_fun); + + // signal(SIGUSR1, signal_handler_fun); + // signal(SIGUSR1, signal_handler_fun); + // signal(SIGUSR1, signal_handler_fun); + user_handler = handler; + return handler; +} \ No newline at end of file -- Gitee