Skip to content

Commit

Permalink
Adapt Felix's manifest parsing to Celix.
Browse files Browse the repository at this point in the history
Not only does it fix the memory safety issue when dealing with line continuation, it is also more memory efficient (0.5K stack usage vs 3.5K stack usage).
  • Loading branch information
PengZheng committed Aug 16, 2023
1 parent 6dfef56 commit 78b31d5
Show file tree
Hide file tree
Showing 15 changed files with 527 additions and 191 deletions.
1 change: 1 addition & 0 deletions libs/error_injector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ add_subdirectory(malloc)
add_subdirectory(asprintf)
add_subdirectory(stdio)
add_subdirectory(stdlib)
add_subdirectory(string)
add_subdirectory(eventfd)
add_subdirectory(stat)
add_subdirectory(fts)
Expand Down
3 changes: 3 additions & 0 deletions libs/error_injector/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ target_link_options(stdio_ei INTERFACE
LINKER:--wrap,fwrite
LINKER:--wrap,remove
LINKER:--wrap,open_memstream
LINKER:--wrap,fseek
LINKER:--wrap,ftell
LINKER:--wrap,fread
)
add_library(Celix::stdio_ei ALIAS stdio_ei)
6 changes: 6 additions & 0 deletions libs/error_injector/stdio/include/stdio_ei.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ CELIX_EI_DECLARE(remove, int);

CELIX_EI_DECLARE(open_memstream, FILE *);

CELIX_EI_DECLARE(fseek, int);

CELIX_EI_DECLARE(ftell, long);

CELIX_EI_DECLARE(fread, size_t);

#ifdef __cplusplus
}
#endif
Expand Down
48 changes: 36 additions & 12 deletions libs/error_injector/stdio/src/stdio_ei.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,66 @@
#include "stdio_ei.h"

extern "C" {
FILE *__real_fopen (const char *__filename, const char *__modes);
CELIX_EI_DEFINE(fopen, FILE *)
FILE *__wrap_fopen (const char *__filename, const char *__modes) {
FILE* __real_fopen(const char* __filename, const char* __modes);
CELIX_EI_DEFINE(fopen, FILE*)
FILE* __wrap_fopen(const char* __filename, const char* __modes) {
errno = EMFILE;
CELIX_EI_IMPL(fopen);
errno = 0;
return __real_fopen(__filename, __modes);
}


size_t __real_fwrite (const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __s);
size_t __real_fwrite(const void* __restrict __ptr, size_t __size, size_t __n, FILE* __restrict __s);
CELIX_EI_DEFINE(fwrite, size_t)
size_t __wrap_fwrite (const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __s) {
size_t __wrap_fwrite(const void* __restrict __ptr, size_t __size, size_t __n, FILE* __restrict __s) {
errno = ENOSPC;
CELIX_EI_IMPL(fwrite);
errno = 0;
return __real_fwrite(__ptr, __size, __n, __s);
}


int __real_remove (const char *__filename);
int __real_remove(const char* __filename);
CELIX_EI_DEFINE(remove, int)
int __wrap_remove (const char *__filename) {
int __wrap_remove(const char* __filename) {
errno = EACCES;
CELIX_EI_IMPL(remove);
errno = 0;
return __real_remove(__filename);
}

FILE *__real_open_memstream (char **__bufloc, size_t *__sizeloc);
CELIX_EI_DEFINE(open_memstream, FILE *)
FILE *__wrap_open_memstream (char **__bufloc, size_t *__sizeloc) {
FILE* __real_open_memstream(char** __bufloc, size_t* __sizeloc);
CELIX_EI_DEFINE(open_memstream, FILE*)
FILE* __wrap_open_memstream(char** __bufloc, size_t* __sizeloc) {
errno = ENOMEM;
CELIX_EI_IMPL(open_memstream);
errno = 0;
return __real_open_memstream(__bufloc, __sizeloc);
}

int __real_fseek(FILE* __stream, long int __off, int __whence);
CELIX_EI_DEFINE(fseek, int)
int __wrap_fseek(FILE* __stream, long int __off, int __whence) {
errno = EACCES;
CELIX_EI_IMPL(fseek);
errno = 0;
return __real_fseek(__stream, __off, __whence);
}

long __real_ftell(FILE* __stream);
CELIX_EI_DEFINE(ftell, long)
long __wrap_ftell(FILE* __stream) {
if (ftell_ret == -1) {
errno = EACCES;
}
CELIX_EI_IMPL(ftell);
errno = 0;
return __real_ftell(__stream);
}

size_t __real_fread(void* __restrict __ptr, size_t __size, size_t __n, FILE* __restrict __s);
CELIX_EI_DEFINE(fread, size_t)
size_t __wrap_fread(void* __restrict __ptr, size_t __size, size_t __n, FILE* __restrict __s) {
CELIX_EI_IMPL(fread);
return __real_fread(__ptr, __size, __n, __s);
}
}
26 changes: 26 additions & 0 deletions libs/error_injector/string/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

add_library(string_ei STATIC src/string_ei.cc)

target_include_directories(string_ei PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
target_link_libraries(string_ei PUBLIC Celix::error_injector)

target_link_options(string_ei INTERFACE
LINKER:--wrap,strndup
)
add_library(Celix::string_ei ALIAS string_ei)
35 changes: 35 additions & 0 deletions libs/error_injector/string/include/string_ei.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

#ifndef CELIX_STRING_EI_H
#define CELIX_STRING_EI_H
#ifdef __cplusplus
extern "C" {
#endif

#include <string.h>
#include "celix_error_injector.h"

CELIX_EI_DECLARE(strndup, char *);

#ifdef __cplusplus
}
#endif
#endif // CELIX_STRING_EI_H
33 changes: 33 additions & 0 deletions libs/error_injector/string/src/string_ei.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

#include "string_ei.h"
#include <errno.h>

extern "C" {
char* __real_strndup(const char* s, size_t n);
CELIX_EI_DEFINE(strndup, char*)
char* __wrap_strndup(const char* s, size_t n) {
errno = ENOMEM;
CELIX_EI_IMPL(strndup);
errno = 0;
return __real_strndup(s, n);
}
}
2 changes: 2 additions & 0 deletions libs/framework/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ if (LINKER_WRAP_SUPPORTED)
Celix::stat_ei
Celix::threads_ei
Celix::hmap_ei
Celix::stdio_ei
Celix::string_ei
GTest::gtest GTest::gtest_main
)

Expand Down
77 changes: 77 additions & 0 deletions libs/framework/gtest/src/ManifestErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
*/

#include <gtest/gtest.h>
#include <limits.h>
#include <stdio.h>

#include "celix_err.h"
#include "celix_properties_ei.h"
#include "celix_stdio_cleanup.h"
#include "celix_utils_ei.h"
#include "hmap_ei.h"
#include "malloc_ei.h"
#include "manifest.h"
#include "stdio_ei.h"
#include "string_ei.h"

class ManifestErrorInjectionTestSuite : public ::testing::Test {
public:
Expand All @@ -41,6 +45,11 @@ class ManifestErrorInjectionTestSuite : public ::testing::Test {
celix_ei_expect_celix_properties_create(nullptr, 0, nullptr);
celix_ei_expect_celix_properties_copy(nullptr, 0, nullptr);
celix_ei_expect_hashMap_create(nullptr, 0, nullptr);
celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr);
celix_ei_expect_fseek(nullptr, 0, -1);
celix_ei_expect_ftell(nullptr, 0, -1);
celix_ei_expect_fread(nullptr, 0, 0);
celix_ei_expect_strndup(nullptr, 0, nullptr);
}
};

Expand Down Expand Up @@ -94,6 +103,74 @@ TEST_F(ManifestErrorInjectionTestSuite, NoMemoryForManifestCloneTest) {
EXPECT_EQ(nullptr, manifest_clone(manifest));
teardownErrorInjectors();

celix_ei_expect_celix_utils_strdup((void*)manifest_clone, 0, nullptr);
EXPECT_EQ(nullptr, manifest_clone(manifest));
teardownErrorInjectors();

celix_ei_expect_celix_properties_copy((void*)manifest_clone, 0, nullptr);
EXPECT_EQ(nullptr, manifest_clone(manifest));
teardownErrorInjectors();
}

TEST_F(ManifestErrorInjectionTestSuite, ReadFromStreamErrorTest) {
std::string content = "Manifest-Version: 1.0\n"
"DeploymentPackage-Icon: %icon\n"
"DeploymentPackage-SymbolicName: com.third._3d\n"
"DeploymentPacakge-Version: 1.2.3.build22032005\n"
"\n"
"Name: bundles/3dlib.jar\n"
"SHA1-Digest: MOez1l4gXHBo8ycYdAxstK3UvEg=\n"
"Bundle-SymbolicName: com.third._3d\n"
"Bundle-Version: 2.3.1\n"
"\n"
"Name: bundles/3dnative.jar\n"
"SHA1-Digest: N8Ow2UY4yjnHZv5zeq2I1Uv/+uE=\n"
"Bundle-SymbolicName: com.third._3d.native\n"
"Bundle-Version: 1.5.3\n"
"\n"
"Name: bundles/3dnative1.jar\n"
"SHA1-Digest: N8Ow2UY4yjnHZv5zeq2I1Uv/+uF=\n"
"Bundle-SymbolicName: com.third._3d.native1\n"
"Bundle-Version: 1.5.4\n"
"\n";
celix_autoptr(FILE) manifestFile = fmemopen((void*)content.c_str(), content.size(), "r");
celix_auto(manifest_pt) manifest = nullptr;
manifest_create(&manifest);
celix_ei_expect_fseek((void*)manifest_readFromStream, 0, -1);
EXPECT_EQ(EACCES, manifest_readFromStream(manifest, manifestFile));
teardownErrorInjectors();

celix_ei_expect_ftell((void*)manifest_readFromStream, 0, -1);
EXPECT_EQ(EACCES, manifest_readFromStream(manifest, manifestFile));
teardownErrorInjectors();

celix_ei_expect_ftell((void*)manifest_readFromStream, 0, (long)INT_MAX+1);
EXPECT_EQ(CELIX_BUNDLE_EXCEPTION, manifest_readFromStream(manifest, manifestFile));
celix_err_printErrors(stdout, "Errors are expected[", "]\n");
teardownErrorInjectors();

celix_ei_expect_malloc((void*)manifest_readFromStream, 0, nullptr);
EXPECT_EQ(CELIX_ENOMEM, manifest_readFromStream(manifest, manifestFile));
celix_err_printErrors(stdout, "Errors are expected[", "]\n");
teardownErrorInjectors();

celix_ei_expect_fread((void*)manifest_readFromStream, 0, 1);
EXPECT_EQ(CELIX_FILE_IO_EXCEPTION, manifest_readFromStream(manifest, manifestFile));
teardownErrorInjectors();

for (int i = 0; i < 3; ++i) {
celix_auto(manifest_pt) mfs = nullptr;
manifest_create(&mfs);
celix_ei_expect_celix_utils_strdup((void*)manifest_readFromStream, 0, nullptr, i+1);
EXPECT_EQ(CELIX_ENOMEM, manifest_readFromStream(mfs, manifestFile));
teardownErrorInjectors();
}

for (int i = 0; i < 3; ++i) {
celix_auto(manifest_pt) mfs = nullptr;
manifest_create(&mfs);
celix_ei_expect_celix_properties_create((void*)manifest_readFromStream, 0, nullptr, i+1);
EXPECT_EQ(CELIX_ENOMEM, manifest_readFromStream(mfs, manifestFile));
teardownErrorInjectors();
}
}
Loading

0 comments on commit 78b31d5

Please sign in to comment.