Skip to content

Commit af25b30

Browse files
authored
Add install feature in CMake (#29)
* Add install feature * Add missing include * Review code * Add example with cmake
1 parent bccbf16 commit af25b30

File tree

8 files changed

+183
-24
lines changed

8 files changed

+183
-24
lines changed

CMakeLists.txt

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
cmake_minimum_required(VERSION 3.13)
22

3-
project(rlbox-cpp17
3+
project(rlbox-sandboxing-api
44
VERSION 0.1
5-
DESCRIPTION "RLBox safe sandboxing API in C++17")
5+
DESCRIPTION "RLBox safe sandboxing API in C++17"
6+
HOMEPAGE_URL "https://github.com/PLSysSec/rlbox_sandboxing_api")
67

78
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
89

@@ -95,12 +96,21 @@ if(NOT Catch2_FOUND)
9596
list(APPEND CMAKE_MODULE_PATH "${catch2_SOURCE_DIR}/contrib")
9697
endif()
9798

99+
file(GLOB_RECURSE
100+
RLBOX_SOURCE_FILES
101+
code/include/*.[chi]pp
102+
code/include/*.[chi]xx
103+
code/include/*.cc
104+
code/include/*.hh
105+
code/include/*.ii
106+
code/include/*.[CHI])
107+
98108
# Documentation ###################
109+
99110
find_package(Doxygen QUIET)
100111

101112
if(DOXYGEN_FOUND)
102-
# Technique from https://devblogs.microsoft.com/cppblog/clear-functional-c
103-
# -documentation-with-sphinx-breathe-doxygen-cmake/
113+
# Technique from https://devblogs.microsoft.com/cppblog/clear-functional-c-documentation-with-sphinx-breathe-doxygen-cmake/
104114
set(DOXYGEN_PROJECT_NAME "RLBox")
105115
set(DOXYGEN_INPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/code/include)
106116
set(DOXYGEN_OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/docs/doxygen)
@@ -111,15 +121,6 @@ if(DOXYGEN_FOUND)
111121
# Replace variables inside @@ with the current values
112122
configure_file(${DOXYFILE_IN} ${DOXYFILE_OUT} @ONLY)
113123

114-
file(GLOB_RECURSE
115-
RLBOX_SOURCE_FILES
116-
code/include/*.[chi]pp
117-
code/include/*.[chi]xx
118-
code/include/*.cc
119-
code/include/*.hh
120-
code/include/*.ii
121-
code/include/*.[CHI])
122-
123124
file(MAKE_DIRECTORY ${DOXYGEN_OUTPUT_DIR})
124125
add_custom_command(OUTPUT ${DOXYGEN_INDEX_FILE}
125126
DEPENDS ${RLBOX_SOURCE_FILES}
@@ -158,17 +159,50 @@ endif()
158159

159160
# Targets ###################
160161

162+
include(GNUInstallDirs)
163+
161164
find_package(Threads REQUIRED)
162-
add_library(rlbox-cpp17-lib INTERFACE)
163-
target_include_directories(rlbox-cpp17-lib INTERFACE code/include)
164-
target_link_libraries(rlbox-cpp17-lib INTERFACE ${CMAKE_THREAD_LIBS_INIT})
165+
add_library(${PROJECT_NAME} INTERFACE)
166+
target_include_directories(${PROJECT_NAME} INTERFACE
167+
$<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/code/include>
168+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
169+
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
170+
target_link_libraries(${PROJECT_NAME} INTERFACE ${CMAKE_THREAD_LIBS_INIT})
171+
set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${RLBOX_SOURCE_FILES}")
172+
173+
# Install ###################
174+
175+
include(CMakePackageConfigHelpers)
176+
177+
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
178+
VERSION ${CMAKE_PROJECT_VERSION}
179+
COMPATIBILITY SameMajorVersion)
180+
181+
set(DATAROOT_CONFIG_DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}")
182+
configure_package_config_file(${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in
183+
${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
184+
INSTALL_DESTINATION ${DATAROOT_CONFIG_DESTINATION}
185+
PATH_VARS CMAKE_INSTALL_INCLUDEDIR)
186+
install(TARGETS ${PROJECT_NAME}
187+
EXPORT ${PROJECT_NAME}-targets
188+
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/rlbox"
189+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
190+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
191+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
192+
install(EXPORT ${PROJECT_NAME}-targets
193+
FILE ${PROJECT_NAME}Targets.cmake
194+
DESTINATION ${DATAROOT_CONFIG_DESTINATION})
195+
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
196+
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
197+
DESTINATION ${DATAROOT_CONFIG_DESTINATION})
165198

166199
# Tests ###################
167200

168201
include(CTest)
169202
include(Catch)
170203

171204
# Test rlbox features
205+
172206
add_executable(test_rlbox
173207
code/tests/test_main.cpp
174208
code/tests/rlbox/test_app_pointers.cpp
@@ -193,7 +227,7 @@ add_executable(test_rlbox
193227

194228
target_include_directories(test_rlbox PRIVATE code/tests/rlbox)
195229

196-
target_link_libraries(test_rlbox Catch2::Catch2 rlbox-cpp17-lib)
230+
target_link_libraries(test_rlbox Catch2::Catch2 ${PROJECT_NAME})
197231

198232
catch_discover_tests(test_rlbox)
199233

@@ -205,7 +239,7 @@ add_executable(test_rlbox_transition_timers
205239

206240
target_include_directories(test_rlbox_transition_timers PRIVATE code/tests/rlbox)
207241

208-
target_link_libraries(test_rlbox_transition_timers Catch2::Catch2 rlbox-cpp17-lib)
242+
target_link_libraries(test_rlbox_transition_timers Catch2::Catch2 ${PROJECT_NAME})
209243

210244
catch_discover_tests(test_rlbox_transition_timers)
211245

@@ -217,11 +251,12 @@ add_executable(test_rlbox_transition_customization
217251

218252
target_include_directories(test_rlbox_transition_customization PRIVATE code/tests/rlbox)
219253

220-
target_link_libraries(test_rlbox_transition_customization Catch2::Catch2 rlbox-cpp17-lib)
254+
target_link_libraries(test_rlbox_transition_customization Catch2::Catch2 ${PROJECT_NAME})
221255

222256
catch_discover_tests(test_rlbox_transition_customization)
223257

224258
# Test rlbox glue
259+
225260
add_library(rlbox_glue_lib_static STATIC code/tests/rlbox_glue/lib/libtest.c)
226261
target_include_directories(rlbox_glue_lib_static
227262
PUBLIC code/tests/rlbox_glue/lib)
@@ -237,7 +272,7 @@ target_include_directories(test_rlbox_glue PUBLIC code/tests/rlbox_glue)
237272

238273
target_link_libraries(test_rlbox_glue
239274
Catch2::Catch2
240-
rlbox-cpp17-lib
275+
${PROJECT_NAME}
241276
rlbox_glue_lib_static)
242277

243278
catch_discover_tests(test_rlbox_glue)
@@ -249,7 +284,7 @@ target_include_directories(test_rlbox_glue_embedder_vars PUBLIC code/tests/rlbox
249284

250285
target_link_libraries(test_rlbox_glue_embedder_vars
251286
Catch2::Catch2
252-
rlbox-cpp17-lib
287+
${PROJECT_NAME}
253288
rlbox_glue_lib_static)
254289

255290
catch_discover_tests(test_rlbox_glue_embedder_vars)
@@ -263,15 +298,14 @@ target_compile_definitions(test_rlbox_glue_dylib PUBLIC GLUE_LIB_PATH="$<TARGET_
263298

264299
target_link_libraries(test_rlbox_glue_dylib
265300
Catch2::Catch2
266-
rlbox-cpp17-lib
301+
${PROJECT_NAME}
267302
${CMAKE_DL_LIBS})
268303

269304
add_dependencies(test_rlbox_glue_dylib rlbox_glue_lib_shared)
270305

271306
catch_discover_tests(test_rlbox_glue_dylib)
272307

273-
274-
# Shortcuts ###################
308+
# make check
275309

276310
add_custom_target(
277311
check
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
4+
5+
check_required_components("@PROJECT_NAME@")

code/include/rlbox_helpers.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <stdexcept>
88
#include <type_traits>
99
#include <utility>
10+
#ifndef RLBOX_USE_CUSTOM_SHARED_LOCK
11+
#include <mutex>
12+
#endif
1013

1114
#include "rlbox_stdlib_polyfill.hpp"
1215

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
project(rlbox_cmake_test
4+
VERSION 0.1
5+
DESCRIPTION "RLBox rlbox_cmake_test")
6+
7+
find_package(rlbox-sandboxing-api CONFIG REQUIRED)
8+
9+
add_library(lib mylib.c)
10+
11+
add_executable(testt main.cpp)
12+
target_link_libraries(testt rlbox-sandboxing-api lib)

examples/hello-world-cmake/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
This is a simple hello-world example that uses the RLBox API to call functions
2+
in a simple sandboxed library. This example uses the NOOP-sandbox; we will
3+
extend the example to use the Wasm-based sandboxed in a later example.
4+
5+
- `mylib.{h,c}` is the simple library
6+
- `main.cpp` is our main program
7+
8+
### Build and run
9+
10+
```
11+
make
12+
./hello
13+
```
14+
15+
Running the program should produce:
16+
17+
```
18+
Hello world from mylib
19+
Adding... 3+4 = 7
20+
OK? = 1
21+
> mylib: hi hi!
22+
hello_cb: hi again!
23+
```

examples/hello-world-cmake/main.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#define RLBOX_SINGLE_THREADED_INVOCATIONS
2+
#define RLBOX_USE_STATIC_CALLS() rlbox_noop_sandbox_lookup_symbol
3+
4+
#include <stdio.h>
5+
#include <rlbox/rlbox.hpp>
6+
#include <rlbox/rlbox_noop_sandbox.hpp>
7+
#include "mylib.h"
8+
9+
using namespace rlbox;
10+
11+
void hello_cb(rlbox_sandbox<rlbox_noop_sandbox>& _,
12+
tainted<const char*, rlbox_noop_sandbox> str) {
13+
auto checked_string =
14+
str.copy_and_verify_string([](std::unique_ptr<char[]> val) {
15+
return std::strlen(val.get()) < 1024 ? std::move(val) : nullptr;
16+
});
17+
printf("hello_cb: %s\n", checked_string.get());
18+
}
19+
20+
int main(int argc, char const *argv[]) {
21+
// Create a new sandbox
22+
rlbox::rlbox_sandbox<rlbox_noop_sandbox> sandbox;
23+
sandbox.create_sandbox();
24+
25+
// call the library hello function
26+
sandbox.invoke_sandbox_function(hello);
27+
28+
// call the add function and check the result:
29+
auto ok = sandbox.invoke_sandbox_function(add, 3, 4).copy_and_verify([](unsigned ret){
30+
printf("Adding... 3+4 = %d\n", ret);
31+
return ret == 7;
32+
});
33+
printf("OK? = %d\n", ok);
34+
35+
// call the library echo function
36+
const char* helloStr = "hi hi!";
37+
size_t helloSize = strlen(helloStr) + 1;
38+
auto taintedStr = sandbox.malloc_in_sandbox<char>(helloSize);
39+
std::strncpy(taintedStr.unverified_safe_pointer_because(helloSize, "writing to region"), helloStr, helloSize);
40+
sandbox.invoke_sandbox_function(echo, taintedStr);
41+
sandbox.free_in_sandbox(taintedStr);
42+
43+
// register callback and call it
44+
auto cb = sandbox.register_callback(hello_cb);
45+
sandbox.invoke_sandbox_function(call_cb, cb);
46+
47+
// destroy sandbox
48+
sandbox.destroy_sandbox();
49+
50+
return 0;
51+
}

examples/hello-world-cmake/mylib.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "mylib.h"
2+
3+
#include <stdio.h>
4+
5+
void hello() {
6+
printf("Hello world from mylib\n");
7+
}
8+
9+
unsigned add(unsigned a, unsigned b) {
10+
return a + b;
11+
}
12+
13+
void echo(const char* str) {
14+
printf("> mylib: %s\n", str);
15+
}
16+
17+
void call_cb(void (*cb) (const char* str)) {
18+
cb("hi again!");
19+
}

examples/hello-world-cmake/mylib.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
void hello();
7+
unsigned add(unsigned, unsigned);
8+
void echo(const char* str);
9+
void call_cb(void (*cb) (const char* str));
10+
#ifdef __cplusplus
11+
}
12+
#endif

0 commit comments

Comments
 (0)