别在main操作了

main
dancer114514 1 year ago
parent e250894c7b
commit 5258ef6f8d

@ -1 +0,0 @@
王浩!浩啊!撒大大

101
init.c

@ -1,101 +0,0 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#ifdef LOSCFG_QUICK_START
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define QUICKSTART_IOC_MAGIC 'T'
#define QUICKSTART_INITSTEP2 _IO(QUICKSTART_IOC_MAGIC, 0)
#define WAIT_FOR_SAMPLE 300000 // wait 300ms for sample
#endif
int main(int argc, char * const *argv)
{
(void)argv;
int ret; // 声明一个整数变量ret用于存储函数的返回值
pid_t gid; // 声明一个pid_t类型的变量gid用于存储进程组ID
const char *shellPath = "/bin/mksh";// 定义一个字符串常量shellPath存储shell的路径这里使用的是mksh shell
// 以下部分是一个条件编译块如果定义了LOSCFG_QUICK_START宏这段代码会被编译进程序
#ifdef LOSCFG_QUICK_START
const char *samplePath = "/dev/shm/sample_quickstart";// 定义一个字符串常量samplePath存储sample_quickstart的路径
// 使用fork函数创建子进程返回值存储在ret变量中
// 如果ret小于0说明创建子进程失败如果ret等于0说明这是子进程如果ret大于0说明这是父进程ret是子进程的ID
ret = fork();
if (ret < 0) { // 如果创建子进程失败,输出错误消息
printf("Failed to fork for sample_quickstart\n");
} else if (ret == 0) {// 如果是子进程获取进程组ID并存储在gid变量中如果获取失败输出错误消息并退出
(void)execve(samplePath, NULL, NULL);
exit(0);
}
usleep(WAIT_FOR_SAMPLE);
int fd = open("/dev/quickstart", O_RDONLY);
if (fd != -1) {
ioctl(fd, QUICKSTART_INITSTEP2);
close(fd);
}
#endif
ret = fork();
if (ret < 0) {
printf("Failed to fork for shell\n");
} else if (ret == 0) {
gid = getpgrp();
if (gid < 0) {
printf("get group id failed, pgrpid %d, errno %d\n", gid, errno);
exit(0);
}
ret = tcsetpgrp(STDIN_FILENO, gid);
if (ret != 0) {// 如果是子进程尝试将STDIN标准输入重定向到指定的进程组通过tcsetpgrp函数实现如果重定向失败输出错误消息并退出
printf("tcsetpgrp failed, errno %d\n", errno);
exit(0);
}
// 如果是子进程使用execve函数执行shellPath指定的shell程序参数为NULL环境变量为NULL
(void)execve(shellPath, NULL, NULL);
exit(0);
}
while (1) { // 如果是父进程进入一个无限循环不断地调用waitpid函数等待子进程的状态变化通过WNOHANG标志实现
ret = waitpid(-1, 0, WNOHANG); // 如果子进程还在运行等待1秒后再次检查如果子进程已经停止运行继续等待下一次检查通过waitpid函数实现
if (ret == 0) {
sleep(1);
}
};
}

@ -1,258 +0,0 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//函数BufWriteTest用于向给定的缓冲区中写入字符'a'从指定的start位置开始一直到end位置。
static void BufWriteTest(void *buf, int start, int end)
{
for (int i = start; i <= end; i++) {
((char *)buf)[i] = 'a';
}
}
//函数BufReadTest则是读取给定的缓冲区中的内容从start位置开始一直到end位置但是读取的内容并未被使用。
static void BufReadTest(void *buf, int start, int end)
{
char tmp;
for (int i = start; i <= end; i++) {
tmp = ((char *)buf)[i];
}
}
//函数LmsMallocTest首先分配了一个大小为TEST_SIZE的内存块然后尝试在[-1, TEST_SIZE]的范围内进行读写操作,预期是触发读写溢出的错误。然后它释放了这块内存。
static void LmsMallocTest(void)
{
#define TEST_SIZE 16
printf("\n-------- LmsMallocTest Start --------\n");
char *buf = (char *)malloc(TEST_SIZE);
if (buf == NULL) {
return;
}
printf("[LmsMallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
BufReadTest(buf, -1, TEST_SIZE);
printf("[LmsMallocTest] write overflow error should be triggered, write range[0, TEST_SIZE]\n");
BufWriteTest(buf, 0, TEST_SIZE);
free(buf);
printf("\n-------- LmsMallocTest End --------\n");
}
//函数LmsReallocTest首先分配了一个大小为TEST_SIZE的内存块然后在[-1, TEST_SIZE]的范围内进行读操作预期是触发读写溢出的错误。然后它尝试将这块内存的大小调整为TEST_SIZE_MIN并再次在[-1, TEST_SIZE_MIN]的范围内进行读操作,预期同样是触发读写溢出的错误。最后,它释放了这块内存
static void LmsReallocTest(void)
{
#define TEST_SIZE 64
#define TEST_SIZE_MIN 32
printf("\n-------- LmsReallocTest Start --------\n");
char *buf = (char *)malloc(TEST_SIZE);
printf("[LmsReallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
BufReadTest(buf, -1, TEST_SIZE);
char *buf1 = (char *)realloc(buf, TEST_SIZE_MIN);
if (buf1 == NULL) {
free(buf);
return;
}
buf = NULL;
printf("[LmsReallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE_MIN]\n");
BufReadTest(buf1, -1, TEST_SIZE_MIN);
free(buf1);
printf("\n-------- LmsReallocTest End --------\n");
}
// 此函数使用calloc函数分配内存calloc会初始化分配的内存为零。然后它调用BufReadTest函数来读取分配的内存范围但实际上并未使用这块内存。如果内存分配失败则calloc返回NULL函数会直接返回。最后使用free释放内存。
static void LmsCallocTest(void)
{
#define TEST_SIZE 16
printf("\n-------- LmsCallocTest Start --------\n");
char *buf = (char *)calloc(4, 4); /* 4: test size */
if (buf == NULL) {
return;
}
printf("[LmsCallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
BufReadTest(buf, -1, TEST_SIZE);
free(buf);
printf("\n-------- LmsCallocTest End --------\n");
}
//LmsVallocTest: 此函数使用valloc函数分配内存valloc会根据页的边界来分配内存。然后它调用BufReadTest函数来读取分配的内存范围。如果内存分配失败则valloc返回NULL函数会直接返回。最后使用free释放内存。
static void LmsVallocTest(void)
{
#define TEST_SIZE 4096
printf("\n-------- LmsVallocTest Start --------\n");
char *buf = (char *)valloc(TEST_SIZE);
if (buf == NULL) {
return;
}
printf("[LmsVallocTest] read overflow & underflow error should be triggered, read range[-1, TEST_SIZE]\n");
BufReadTest(buf, -1, TEST_SIZE);
free(buf);
printf("\n-------- LmsVallocTest End --------\n");
}
//LmsAlignedAllocTest: 此函数使用aligned_alloc函数分配内存aligned_alloc会分配一块特定对齐的内存。然后它调用BufReadTest函数来读取分配的内存范围。如果内存分配失败则aligned_alloc返回NULL函数会直接返回。最后使用free释放内存。
static void LmsAlignedAllocTest(void)
{
#define TEST_ALIGN_SIZE 64
#define TEST_SIZE 128
printf("\n-------- LmsAlignedAllocTest Start --------\n");
char *buf = (char *)aligned_alloc(TEST_ALIGN_SIZE, TEST_SIZE);
if (buf == NULL) {
return;
}
printf("[LmsAlignedAllocTest] read overflow & underflow error should be triggered, read range[-1,128]\n");
BufReadTest(buf, -1, 128);
free(buf);
printf("\n-------- LmsAlignedAllocTest End --------\n");
}
//LmsMemsetTest: 此函数使用malloc函数分配内存然后使用memset函数将该内存块设置为0。注意这里的memset被用于填充比分配的内存还要多的字节TEST_SIZE + 1这可能会导致溢出错误。然后使用free释放内存。
static void LmsMemsetTest(void)
{
#define TEST_SIZE 32
printf("\n-------- LmsMemsetTest Start --------\n");
char *buf = (char *)malloc(TEST_SIZE);
if (buf == NULL) {
return;
}
printf("[LmsMemsetTest] memset overflow & underflow error should be triggered, memset size:%d\n", TEST_SIZE + 1);
memset(buf, 0, TEST_SIZE + 1);
free(buf);
printf("\n-------- LmsMemsetTest End --------\n");
}
//: 该函数旨在测试memcpy函数的功能。首先它使用malloc分配一块内存然后尝试使用memcpy将一个数组的内容复制到这块内存中复制的大小超过分配的内存大小从而触发溢出错误。
static void LmsMemcpyTest(void)
{
#define TEST_SIZE 20
printf("\n-------- LmsMemcpyTest Start --------\n");
char *buf = (char *)malloc(TEST_SIZE);
if (buf == NULL) {
return;
}
char localBuf[32] = {0}; /* 32: test size */
printf("[LmsMemcpyTest] memcpy overflow error should be triggered, memcpy size:%d\n", TEST_SIZE + 1);
memcpy(buf, localBuf, TEST_SIZE + 1);
free(buf);
printf("\n-------- LmsMemcpyTest End --------\n");
}
//LmsMemmoveTest: 该函数测试memmove函数的功能。memmove函数用于复制一定数量的字节从一个位置到另一个位置即使这两个位置是重叠的。该函数尝试复制的内容超过了分配的内存大小从而触发溢出错误。
static void LmsMemmoveTest(void)
{
#define TEST_SIZE 20
printf("\n-------- LmsMemmoveTest Start --------\n");
char *buf = (char *)malloc(TEST_SIZE);
if (buf == NULL) {
return;
}
printf("[LmsMemmoveTest] memmove overflow error should be triggered\n");
memmove(buf + 12, buf, 10); /* 12 and 10: test size */
free(buf);
printf("\n-------- LmsMemmoveTest End --------\n");
}
LmsStrcpyTest: strcpystrcpy
static void LmsStrcpyTest(void)
{
#define TEST_SIZE 16
printf("\n-------- LmsStrcpyTest Start --------\n");
char *buf = (char *)malloc(TEST_SIZE);
if (buf == NULL) {
return;
}
char *testStr = "bbbbbbbbbbbbbbbbb";
printf("[LmsStrcpyTest] strcpy overflow error should be triggered, src string buf size:%d\n", strlen(testStr) + 1);
strcpy(buf, testStr);
free(buf);
printf("\n-------- LmsStrcpyTest End --------\n");
}
//LmsStrcatTest: 该函数测试strcat函数的功能。strcat函数用于将一个字符串连接到另一个字符串的末尾。该函数首先分配一块内存并尝试将一个字符串连接到这块内存的末尾连接后的字符串长度超过了分配的内存大小从而触发溢出错误。
static void LmsStrcatTest(void)
{
#define TEST_SIZE 16
printf("\n-------- LmsStrcatTest Start --------\n");
char *buf = (char *)malloc(TEST_SIZE);
if (buf == NULL) {
return;
}
buf[0] = 'a';
buf[1] = 'b';
buf[2] = 0;
char *testStr = "cccccccccccccc";
printf("[LmsStrcatTest] strcat overflow error should be triggered, src string:%s dest string:%s"
"total buf size:%d\n",
testStr, buf, strlen(testStr) + strlen(buf) + 1);
strcat(buf, testStr);
free(buf);
printf("\n-------- LmsStrcatTest End --------\n");
}
//static void LmsFreeTest(void): 这是一个静态函数名为LmsFreeTest不接受任何参数并且没有返回值。该函数主要用于测试free函数的功能。
static void LmsFreeTest(void)
{
#define TEST_SIZE 16
printf("\n-------- LmsFreeTest Start --------\n");
char *buf = (char *)malloc(TEST_SIZE);
if (buf == NULL) {
return;
}
printf("[LmsFreeTest] free size:%d\n", TEST_SIZE);
free(buf);
printf("[LmsFreeTest] Use after free error should be triggered, read range[1,1]\n");
BufReadTest(buf, 1, 1);
printf("[LmsFreeTest] double free error should be triggered\n");
free(buf);
printf("\n-------- LmsFreeTest End --------\n");
}
int main(int argc, char * const *argv)
{
(void)argc;
(void)argv;
printf("\n############### Lms Test start ###############\n");
char *tmp = (char *)malloc(5000); /* 5000: test mem size */
if (tmp == NULL) {
return;
}
LmsMallocTest();
LmsReallocTest();
LmsCallocTest();
LmsVallocTest();
LmsAlignedAllocTest();
LmsMemsetTest();
LmsMemcpyTest();
LmsMemmoveTest();
LmsStrcpyTest();
LmsStrcatTest();
LmsFreeTest();
free(tmp);
printf("\n############### Lms Test End ###############\n");
}
//int main(int argc, char * const *argv): 定义主函数,接受命令行参数。
//(void)argc; (void)argv;: 这两行代码忽略了命令行参数,实际上它们没有用到。
//printf("\n############### Lms Test start ###############\n");: 打印一条消息表示Lms测试开始。
// *tmp = (char *)malloc(5000);: 分配一块大小为5000的内存用于后续的测试函数。
//if (tmp == NULL) { return; }: 检查malloc是否成功分配了内存。如果分配失败主函数直接返回。
//调用多个内存管理测试函数例如LmsMallocTest()、LmsReallocTest()等。这些函数的具体实现没有在给出的代码中给出,因此我无法对它们进行注释。
//LmsFreeTest();: 调用LmsFreeTest函数进行free函数的测试。
//free(tmp);: 释放之前使用malloc分配的内存。
//printf("\n############### Lms Test End ###############\n");: 打印一条消息表示Lms测试结束
Loading…
Cancel
Save