Skip to content

Commit a135276

Browse files
authored
HOTFIX free after simplify bool, call munmap after each request (#4115)
Fixes a problem that surfaced in the `kontrol` upgrade (symptom: server crashes unexpectedly after some iterations). The LLVM backend was changed to not call `munmap` within `free_all_kore_mem` any more, which makes the `mmap` calls to initialise thread-local heap address space fail after a number of requests. This is a hot-fix which calls `reset_munmap_all_arenas`, a function not currently included in the LLVM backend C API. The function will be included in the API once runtimeverification/llvm-backend#1210 is merged, then the header file and the function name should be aligned with the API again. The backend is _not going to work with older LLVM backends_ because [the function that needs to be called is new as of June 7](runtimeverification/llvm-backend@facee2b).
1 parent 6642ad4 commit a135276

File tree

7 files changed

+109
-11
lines changed

7 files changed

+109
-11
lines changed

booster/cbits/kllvm-c.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#ifndef __cplusplus
55
#include <stdbool.h>
66
#include <stddef.h>
7+
#include <stdint.h>
78
#else
89
#include <cstddef>
10+
#include <cstdint>
911
#endif
1012

1113
#ifdef __cplusplus
@@ -75,7 +77,7 @@ typedef struct kore_sort kore_sort;
7577
typedef struct kore_symbol kore_symbol;
7678
typedef struct block block;
7779

78-
/* KOREPattern */
80+
/* kore_pattern */
7981

8082
char *kore_pattern_dump(kore_pattern const *);
8183

@@ -127,8 +129,11 @@ void kore_simplify(
127129
void kore_simplify_binary(
128130
kore_error *, char *, size_t, kore_sort const *, char **, size_t *);
129131

132+
block *take_steps(int64_t depth, block *term);
130133

131-
/* KORESort */
134+
void init_static_objects(void);
135+
136+
/* kore_sort */
132137

133138
char *kore_sort_dump(kore_sort const *);
134139

@@ -142,7 +147,7 @@ bool kore_sort_is_k(kore_sort const *);
142147
kore_sort *kore_composite_sort_new(char const *);
143148
void kore_composite_sort_add_argument(kore_sort const *, kore_sort const *);
144149

145-
/* KORESymbol */
150+
/* kore_symbol */
146151

147152
kore_symbol *kore_symbol_new(char const *);
148153

@@ -154,11 +159,24 @@ void kore_symbol_add_formal_argument(kore_symbol *, kore_sort const *);
154159

155160
void kllvm_init(void);
156161
void kllvm_free_all_memory(void);
162+
void reset_munmap_all_arenas(void);
157163

158164
/* Sort-specific functions */
159165

160166
bool kllvm_mutable_bytes_enabled(void);
161167

168+
/* Definitions */
169+
170+
/**
171+
* Parse the given KORE definition, then if any of its axioms have a `label`
172+
* attribute that matches the supplied label, return the name of the function
173+
* symbol that attempts matching a pattern against that axiom (and will
174+
* therefore populate the backend's global matching log).
175+
*
176+
* If no such axiom exists, return `nullptr`.
177+
*/
178+
char *kore_match_function_name(char const *defn_path, char const *label);
179+
162180
#ifdef __cplusplus
163181
}
164182
#endif

booster/cbits/stdint.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* minimal stdint.h according to POSIX, see
2+
https://pubs.opengroup.org/onlinepubs/009695399/basedefs/stdint.h.html
3+
*/
4+
5+
#ifndef _STDINT_H
6+
#define _STDINT_H
7+
8+
typedef signed char int8_t;
9+
typedef short int int16_t;
10+
typedef int int32_t;
11+
typedef long long int int64_t;
12+
13+
typedef unsigned char uint8_t;
14+
typedef unsigned short int uint16_t;
15+
typedef unsigned int uint32_t;
16+
typedef unsigned long long int uint64_t;
17+
18+
typedef int8_t int_least8_t;
19+
typedef int16_t int_least16_t;
20+
typedef int32_t int_least32_t;
21+
typedef int64_t int_least64_t;
22+
23+
typedef uint8_t uint_least8_t;
24+
typedef uint16_t uint_least16_t;
25+
typedef uint32_t uint_least32_t;
26+
typedef uint64_t uint_least64_t;
27+
28+
typedef int8_t int_fast8_t;
29+
typedef int16_t int_fast16_t;
30+
typedef int32_t int_fast32_t;
31+
typedef int64_t int_fast64_t;
32+
33+
typedef uint8_t uint_fast8_t;
34+
typedef uint16_t uint_fast16_t;
35+
typedef uint32_t uint_fast32_t;
36+
typedef uint64_t uint_fast64_t;
37+
38+
#ifdef _WIN64
39+
typedef signed __int64 intptr_t;
40+
typedef unsigned __int64 uintptr_t;
41+
#else
42+
typedef signed int intptr_t;
43+
typedef unsigned int uintptr_t;
44+
#endif
45+
46+
typedef int64_t intmax_t;
47+
typedef uint64_t uintmax_t;
48+
49+
#define INT8_MIN (-127 - 1)
50+
#define INT16_MIN (-32767 - 1)
51+
#define INT32_MIN (-2147483647 - 1)
52+
#define INT64_MIN (-9223372036854775807LL - 1)
53+
54+
#define INT8_MAX 127
55+
#define INT16_MAX 32767
56+
#define INT32_MAX 2147483647
57+
#define INT64_MAX 9223372036854775807LL
58+
59+
#define UINT8_MAX 255
60+
#define UINT16_MAX 65535
61+
#define UINT32_MAX 4294967295U
62+
#define UINT64_MAX 18446744073709551615ULL
63+
64+
#define INTPTR_MIN INT64_MIN
65+
#define INTPTR_MAX INT64_MAX
66+
#define UINTPTR_MAX UINT64_MAX
67+
#define INTMAX_MIN INT64_MIN
68+
#define INTMAX_MAX INT64_MAX
69+
#define UINTMAX_MAX UINT64_MAX
70+
71+
#endif

booster/hs-backend-booster.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 1.12
22

3-
-- This file has been generated from package.yaml by hpack version 0.36.0.
3+
-- This file has been generated from package.yaml by hpack version 0.37.0.
44
--
55
-- see: https://github.com/sol/hpack
66

@@ -217,6 +217,7 @@ extra-source-files:
217217
cbits/kllvm-c.h
218218
cbits/stdbool.h
219219
cbits/stddef.h
220+
cbits/stdint.h
220221

221222
source-repository head
222223
type: git

booster/library/Booster/JsonRpc.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Booster.JsonRpc (
1919
import Control.Applicative ((<|>))
2020
import Control.Concurrent (MVar, putMVar, readMVar, takeMVar)
2121
import Control.Monad
22+
import Control.Monad.Extra (whenJust)
2223
import Control.Monad.IO.Class
2324
import Control.Monad.Trans.Except (catchE, except, runExcept, runExceptT, throwE, withExceptT)
2425
import Crypto.Hash (SHA256 (..), hashWith)
@@ -41,7 +42,7 @@ import Booster.CLOptions (RewriteOptions (..))
4142
import Booster.Definition.Attributes.Base (getUniqueId, uniqueId)
4243
import Booster.Definition.Base (KoreDefinition (..))
4344
import Booster.Definition.Base qualified as Definition (RewriteRule (..))
44-
import Booster.LLVM as LLVM (API)
45+
import Booster.LLVM as LLVM (API, llvmReset)
4546
import Booster.Log
4647
import Booster.Pattern.ApplyEquations qualified as ApplyEquations
4748
import Booster.Pattern.Base (Pattern (..), Sort (SortApp))
@@ -435,9 +436,11 @@ respond stateVar request =
435436
withModule mbMainModule action = do
436437
state <- liftIO $ readMVar stateVar
437438
let mainName = fromMaybe state.defaultMain mbMainModule
439+
purgeLlvmLib = whenJust state.mLlvmLibrary llvmReset
438440
case Map.lookup mainName state.definitions of
439441
Nothing -> pure $ Left $ RpcError.backendError $ RpcError.CouldNotFindModule mainName
440-
Just d -> action (d, state.mLlvmLibrary, state.mSMTOptions, state.rewriteOptions)
442+
Just d ->
443+
action (d, state.mLlvmLibrary, state.mSMTOptions, state.rewriteOptions) <* purgeLlvmLib
441444

442445
handleSmtError :: JsonRpcHandler
443446
handleSmtError = JsonRpcHandler $ \case

booster/library/Booster/LLVM.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module Booster.LLVM (
2+
llvmReset,
23
simplifyBool,
34
simplifyTerm,
45
Internal.API,
@@ -22,6 +23,9 @@ import Booster.Pattern.Binary
2223
import Booster.Pattern.Util
2324
import Booster.Util (secWithUnit, timed)
2425

26+
llvmReset :: LoggerMIO io => Internal.API -> io ()
27+
llvmReset api = liftIO api.munmap -- releases thread-local mmap'ed memory in the LLVM backend
28+
2529
simplifyBool :: LoggerMIO io => Internal.API -> Term -> io (Either Internal.LlvmError Bool)
2630
simplifyBool api trm = ioWithTiming $ Internal.runLLVM api $ do
2731
kore <- Internal.ask
@@ -40,7 +44,6 @@ simplifyTerm api def trm sort = ioWithTiming $ Internal.runLLVM api $ do
4044
trmPtr <- Internal.marshallTerm trm
4145
sortPtr <- Internal.marshallSort sort
4246
mbinary <- liftIO $ kore.simplify trmPtr sortPtr
43-
liftIO kore.collect
4447
case mbinary of
4548
Left err -> pure $ Left err
4649
Right binary -> do

booster/library/Booster/LLVM/Internal.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Data.Data (Data)
3333
import Data.HashMap.Strict (HashMap)
3434
import Data.HashMap.Strict qualified as HM
3535
import Data.IORef (IORef, modifyIORef', newIORef, readIORef)
36-
import Foreign (ForeignPtr, finalizeForeignPtr, newForeignPtr, withForeignPtr)
36+
import Foreign (ForeignPtr, Int64, finalizeForeignPtr, newForeignPtr, withForeignPtr)
3737
import Foreign qualified
3838
import Foreign.C qualified as C
3939
import Foreign.C.Types (CSize (..))
@@ -52,6 +52,7 @@ data KoreSymbol
5252
data KoreError
5353
data Block
5454
type SizeT = CSize
55+
type Int64T = Foreign.Int64
5556

5657
type KorePatternPtr = ForeignPtr KorePattern
5758
type KoreSymbolPtr = ForeignPtr KoreSymbol
@@ -104,6 +105,7 @@ data API = API
104105
, simplifyBool :: KorePatternPtr -> IO (Either LlvmError Bool)
105106
, simplify :: KorePatternPtr -> KoreSortPtr -> IO (Either LlvmError ByteString)
106107
, collect :: IO ()
108+
, munmap :: IO ()
107109
, mutex :: MVar ()
108110
}
109111

@@ -118,7 +120,7 @@ withDLib dlib = Linker.withDL dlib [Linker.RTLD_LAZY]
118120

119121
runLLVM :: API -> LLVM a -> IO a
120122
runLLVM api (LLVM m) =
121-
withMVar api.mutex $ const $ runReaderT m api
123+
withMVar api.mutex $ const (runReaderT m api <* api.collect)
122124

123125
mkAPI :: Linker.DL -> IO API
124126
mkAPI dlib = flip runReaderT dlib $ do
@@ -274,6 +276,7 @@ mkAPI dlib = flip runReaderT dlib $ do
274276
pure $ Right result
275277
else Left . LlvmError <$> errorMessage errPtr
276278

279+
munmap <- resetMunmapAllArenas -- HACK. Adjust name after llvm-backend dependency upgrade
277280
mutableBytesEnabled <-
278281
kllvmMutableBytesEnabled `catch` \(_ :: IOException) -> pure (pure 0)
279282
liftIO $
@@ -283,7 +286,7 @@ mkAPI dlib = flip runReaderT dlib $ do
283286
"[Warn] Using an LLVM backend compiled with --llvm-mutable-bytes (unsound byte array semantics)"
284287

285288
mutex <- liftIO $ newMVar ()
286-
pure API{patt, symbol, sort, simplifyBool, simplify, collect, mutex}
289+
pure API{patt, symbol, sort, simplifyBool, simplify, collect, munmap, mutex}
287290

288291
ask :: LLVM API
289292
ask = LLVM Reader.ask

scripts/performance-tests-kontrol.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,3 @@ fi
149149

150150
cd $SCRIPT_DIR
151151
python3 compare.py logs/kontrol-$KONTROL_VERSION-$FEATURE_BRANCH_NAME.log logs/kontrol-$KONTROL_VERSION-master-$MASTER_COMMIT_SHORT.log > logs/kontrol-$KONTROL_VERSION-master-$MASTER_COMMIT_SHORT-$FEATURE_BRANCH_NAME-compare
152-
fi

0 commit comments

Comments
 (0)